private void AddAttrib(KmlNode toItem, KmlAttrib beforeItem) { KmlNode node = toItem; string input; string preset = ""; bool loop = true; while (loop && DlgInput.Show("Enter the name for the new attribute:", "NEW attribute", Icons.Add, preset, out input)) { string attrib = input; if (attrib.Length > 0 && attrib.IndexOf('=') < 0) { attrib = attrib + "="; } KmlItem item = KmlItem.CreateItem(attrib); if (item is KmlAttrib && beforeItem != null) { node.InsertBefore(beforeItem, (KmlAttrib)item); loop = false; // View will be refreshed in AttribChanged event } else if (item is KmlAttrib) { node.Add((KmlAttrib)item); loop = false; // View will be refreshed in AttribChanged event } else { DlgMessage.Show("Attribute name is not allowed to be empty or contain following characters: {}", "NEW attribute", Icons.Warning); preset = input; // Input will pop up again while loop == true } } }
private void Load(string filename) { TabsManager.Load(filename); Filename = filename; DlgSearch.SearchReset(); try { string programFilesX64 = Environment.ExpandEnvironmentVariables("%ProgramW6432%"); string programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); bool isProg = filename.StartsWith(programFilesX64, StringComparison.InvariantCultureIgnoreCase) || filename.StartsWith(programFilesX86, StringComparison.InvariantCultureIgnoreCase); bool isAdmin = (new WindowsPrincipal(WindowsIdentity.GetCurrent())).IsInRole(WindowsBuiltInRole.Administrator); if (isProg && !isAdmin) { DlgMessage.Show("This file is located under 'program files' and hence protected by Windows.\n" + "So KML will not be able to overwrite it, when you choose to save your changes.\n\n" + "Please run KML as administrator or manually pick the changed file from 'program data'\n" + "and put it back to original place.", "Permission warning", (new GuiIcons16()).Warning); } } catch { ; // something went wrong with reading env-var or admin flag } }
private void AddChildNode(KmlNode toItem, KmlNode beforeItem) { KmlNode node = toItem; string input; string preset = ""; bool loop = true; while (loop && DlgInput.Show("Enter the tag for the new node:", "NEW node", Icons.Add, preset, out input)) { KmlItem item = KmlItem.CreateItem(input); if (item is KmlNode && beforeItem != null) { node.InsertBefore(beforeItem, (KmlNode)item); loop = false; // View will be refreshed in ChildrenChanged event } else if (item is KmlNode) { node.Add((KmlNode)item); loop = false; // View will be refreshed in ChildrenChanged event } else { DlgMessage.Show("Tag is not allowed to be empty or contain following characters: ={}", "NEW node", Icons.Warning); preset = input; // Input will pop up again while loop == true } } }
private void NodeInsertBefore_Click(object sender, RoutedEventArgs e) { KmlNode node = ((sender as MenuItem).DataContext as KmlNode); if (node.Parent != null) { AddChildNode(node.Parent, node); } else { DlgMessage.Show("Can not insert, node has no parent", "NEW node", Icons.Warning); } }
private void DockRepair_Click(object sender, RoutedEventArgs e) { KmlPartDock dock = (sender as MenuItem).DataContext as KmlPartDock; dock.Repair(); DlgMessage.ShowAndClear(Syntax.Messages); // Refresh view IGuiManager manager = GuiTabsManager.GetCurrent().VesselsManager; if (dock.Parent is KmlVessel && dock.Parent == manager.GetSelectedItem()) { manager.Select(dock.Parent); } }
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { try { GuiIcons ico = new GuiIcons16(); string txt = "Sorry, an unhandled exception occurred!\n\n" + "Please report follwing details on the KSP forum thread or via GitHub issue:\n\n" + e.Exception.ToString(); DlgMessage.Show(txt, "KML Exception", ico.Error); } catch { // maybe something breaks the DlgMessage, e.g. there is no main window (to center onto), then fall back to MessageBox MessageBox.Show(e.Exception.ToString(), "KML Exception"); } e.Handled = true; }
private void AttribInsertBefore_Click(object sender, RoutedEventArgs e) { // TODO GuiTreeAttrib.AttribInsertBefore_Click(): Almost same code as private GuiTreeNode.AddAttrib() KmlAttrib beforeItem = ((sender as MenuItem).DataContext as KmlAttrib); KmlNode node = beforeItem.Parent; if (node != null) { string input; string preset = ""; bool loop = true; while (loop && DlgInput.Show("Enter the name for the new attribute:", "NEW attribute", Icons.Add, preset, out input)) { string attrib = input; if (attrib.Length > 0 && attrib.IndexOf('=') < 0) { attrib = attrib + "="; } KmlItem item = KmlItem.CreateItem(attrib); if (item is KmlAttrib && beforeItem != null) { node.InsertBefore(beforeItem, (KmlAttrib)item); loop = false; // View will be refreshed in AttribChanged event } else if (item is KmlAttrib) { node.Add((KmlAttrib)item); loop = false; // View will be refreshed in AttribChanged event } else { DlgMessage.Show("Attribute name is not allowed to be empty or contain following characters: {}", "NEW attribute", Icons.Warning); preset = input; // Input will pop up again while loop == true } } } else { DlgMessage.Show("Can not insert, attribute has no parent", "NEW attribute", Icons.Warning); } }
/// <summary> /// Show a dialog window with given message. /// </summary> /// <param name="message">The Syntax.Message to show</param> /// <returns>True if "Ok" was clicked, false otherwise</returns> public static bool?Show(Syntax.Message message) { string title = null; Image img = null; if (message is Syntax.ErrorMessage) { title = "KML Error"; img = Icons.Error; } else if (message is Syntax.WarningMessage) { title = "KML Error"; img = Icons.Warning; } DlgMessage dlg = new DlgMessage(message.ToString(true), title, img); return(dlg.ShowDialog()); }
/// <summary> /// Write the given list of root items to a data file. /// Child items of these roots a written recursively. /// If a file with given name already exists, it is renamed for backup /// to "zKMLBACKUP<timestamp>-<filename>" in same directory. /// </summary> /// <param name="filename">The full path and filename of the data file to write</param> /// <param name="items">A list of root / top level KmlItems</param> public static void WriteFile(string filename, List <KmlItem> items) { try { string Backupname = ""; if (File.Exists(filename)) { string dir = Path.GetDirectoryName(filename) + @"\"; string name = Path.GetFileNameWithoutExtension(filename); string ext = Path.GetExtension(filename); string timestamp = string.Format("{0:yyyyMMddHHmmss}", DateTime.Now); Backupname = dir + "zKMLBACKUP" + timestamp + "-" + name + ext; File.Move(filename, Backupname); } // Explicit setting UTF8 doesn't look the same, if I compare loaded and saved with WinMerge // StreamWriter file = new StreamWriter(Filename, false, Encoding.UTF8); StreamWriter file = new StreamWriter(filename); try { foreach (KmlItem item in items) { WriteFile(file, item, 0); } file.Close(); } catch (Exception e) { file.Close(); if (Backupname.Length > 0) { File.Delete(filename); File.Move(Backupname, filename); } throw e; } } catch (Exception e) { DlgMessage.Show("Error saving to " + filename + "\n\n" + e.Message); } }
/// <summary> /// Show a dialog with a summary of all given messages. /// </summary> /// <param name="messages">A list of Syntax.Messages to show</param> /// <returns>True if "Ok" was clicked, false otherwise</returns> public static bool?Show(List <Syntax.Message> messages) { string title = null; Image img = null; StringBuilder str = new StringBuilder(); if (messages.Any(x => x is Syntax.ErrorMessage)) { title = "KML Error"; img = Icons.Error; } else if (messages.Any(x => x is Syntax.WarningMessage)) { title = "KML Warning"; img = Icons.Warning; } // TODO DlgMessage.Show(List<Syntax.Message>): Supposed to use messages instead of Syntax.Messages? if (Syntax.Messages.Count > 0) { str.Append(Syntax.Messages[0].ToString(true)); } else { // Usually Show is called when either a warning/error // or a successful message is expected. // Should be avoided to have no feedback. string txt = "Sorry, nothing happened or no feedback from action!\n\n" + "Please report your steps and any details on the KSP forum thread or via GitHub issue."; title = "KML Warning"; img = Icons.Warning; str.Append(txt); } for (int i = 1; i < Syntax.Messages.Count; i++) { str.AppendLine(); str.AppendLine(); str.Append(Syntax.Messages[i].ToString(true)); } DlgMessage dlg = new DlgMessage(str.ToString(), title, img); return(dlg.ShowDialog()); }
/// <summary> /// Parse a KSP persistence file and return a list of the root nodes. /// In general there may be more than one item not containing other items. /// With correct KSP persistence data this doesn't happen and the list will /// usually contain just one item. /// </summary> /// <param name="filename">The full path and filename of the data file to read</param> /// <returns>A list of root / top level KmlItems</returns> public static List <KmlItem> ParseFile(string filename) { List <KmlItem> list = new List <KmlItem>(); try { // Explicit setting UTF8 doesn't look the same, if I compare loaded and saved with MinMerge // StreamReader file = new StreamReader(Filename, Encoding.UTF8); StreamReader file = new StreamReader(filename); list.AddRange(ParseFile(file)); file.Close(); CallFinalize(list); } catch (Exception e) { DlgMessage.Show("Error loading from " + filename + "\n\n" + e.Message); } return(list); }
private void AttribPasteBefore_Click(object sender, RoutedEventArgs e) { KmlAttrib attrib = ((sender as MenuItem).DataContext as KmlAttrib); if (attrib.Parent != null) { var textNode = Clipboard.GetText(TextDataFormat.UnicodeText); var items = KmlItem.ParseItems(new StringReader(textNode)).Where(i => i is KmlAttrib).ToList(); if (!items.Any()) { DlgMessage.Show("Can not paste attribute from clipboard", "Paste attribute", Icons.Warning); } attrib.Parent.InsertBeforeRange(attrib, items); } else { DlgMessage.Show("Can not insert, attribute has no parent", "Paste attribute", Icons.Warning); } }
/// <summary> /// Show a dialog window with given title, message and image. /// </summary> /// <param name="message">The message to show</param> /// <param name="title">The window title</param> /// <param name="image">The image for the window icon</param> /// <returns>True if "Ok" was clicked, false otherwise</returns> public static bool Show(string message, string title, Image image) { DlgMessage dlg = new DlgMessage(message, title, image); return(dlg.ShowDialog() == true); }