/// <summary> /// Creates the character set menu with encodings as sub menu items. /// </summary> /// <param name="parent">The parent tool strip menu item.</param> /// <param name="singleCodePageResults">A flag indicating if character sets containing only single encoding should be returned.</param> /// <param name="data">Additional data to be assigned to the encoding tool strip menu item.</param> public static void CreateCharacterSetMenu(ToolStripMenuItem parent, bool singleCodePageResults, object data) { // create an instance of the EncodingCharacterSet class.. var encodingCharacterSet = new EncodingCharacterSet(); // get the character sets contained in the EncodingCharacterSet class.. var charSets = encodingCharacterSet.GetCharacterSetList(singleCodePageResults); // loop through the character sets.. foreach (var item in charSets) { // create a "dummy" menu to contain the actual encodings for the character set.. ToolStripMenuItem menuItem = new ToolStripMenuItem(encodingCharacterSet.GetCharacterSetName(item)) { Tag = item }; // set the tag to contain the character set enumeration value.. // get the encodings for the character set.. var encodings = encodingCharacterSet[item]; // loop through the encodings within the character set.. foreach (var encoding in encodings) { if (encoding == null) { continue; } // create a menu item for the encoding.. DataToolStripMenuItem menuItemEncoding = new DataToolStripMenuItem(encoding.EncodingName) { Tag = encoding, Data = data }; // set the Tag property to contain the encoding.. // set the user given additional data for the menu item.. // subscribe the click event.. menuItemEncoding.Click += MenuItemEncoding_Click; // add the menu item to the character set menu.. menuItem.DropDownItems.Add(menuItemEncoding); } // add the character set menu item to the given parent menu.. parent.DropDownItems.Add(menuItem); } }
// an internal event handler to raise the EncodingMenuClicked event if subscribed.. private static void MenuItemEncoding_Click(object sender, EventArgs e) { // get the sender and assume a type of DataToolStripMenuItem.. DataToolStripMenuItem dataToolStripMenuItem = (DataToolStripMenuItem)sender; // raise the event if subscribed.. EncodingMenuClicked?. Invoke(sender, new EncodingMenuClickEventArgs { Encoding = (System.Text.Encoding)dataToolStripMenuItem.Tag, Data = dataToolStripMenuItem.Data }); }
/// <summary> /// Creates a recent files menu to a given parent menu. /// </summary> /// <param name="menuItem">The menu item to add the recent files list.</param> /// <param name="session">A name of the session to which the history documents belong to.</param> /// <param name="maxCount">Maximum count of recent file entries to add to the given <paramref name="menuItem"/>.</param> /// <param name="addMenuOpenAll">A flag indicating whether the menu should contain an item to open all recent files.</param> /// <param name="hideItems">A list of tool strip items to hide if there are no recent files.</param> public static void CreateRecentFilesMenu(ToolStripMenuItem menuItem, FileSession session, int maxCount, bool addMenuOpenAll, params ToolStripItem[] hideItems) { // dispose of the previous menu items.. DisposeRecentFilesMenu(menuItem); // get the recent files from the database.. var recentFiles = ScriptNotepadDbContext.DbContext.RecentFiles .OrderByDescending(f => f.ClosedDateTime).Where(f => f.Session.SessionName == session.SessionName && !ScriptNotepadDbContext.DbContext.FileSaves.Any(s => !s.IsHistory && s.Session.SessionName == f.Session.SessionName && s.FileNameFull == f.FileNameFull)).Take(maxCount); if (addMenuOpenAll) { List <RecentFile> recentFilesAll = recentFiles.ToList(); if (recentFilesAll.Count > 1) { // create a menu item for all the recent files.. DataToolStripMenuItem menuItemRecentFile = new DataToolStripMenuItem( string.IsNullOrWhiteSpace(MenuOpenAllRecentText) ? "Open all recent files..." : MenuOpenAllRecentText) { Data = recentFilesAll }; // set the user given additional data for the menu item.. // subscribe the click event.. menuItemRecentFile.Click += MenuItemRecentFile_Click; // add the menu item to the recent files menu.. menuItem.DropDownItems.Add(menuItemRecentFile); // add a separator menu item to the recent files menu.. menuItem.DropDownItems.Add(new ToolStripSeparator()); } } // loop through the results.. foreach (var recentFile in recentFiles) { // create a menu item for the encoding.. DataToolStripMenuItem menuItemRecentFile = new DataToolStripMenuItem(recentFile.ToString()) { Data = recentFile }; // set the user given additional data for the menu item.. // subscribe the click event.. menuItemRecentFile.Click += MenuItemRecentFile_Click; // add the menu item to the recent files menu.. menuItem.DropDownItems.Add(menuItemRecentFile); } // the recent file menu should only be visible if there are any drop down items.. menuItem.Visible = menuItem.DropDownItems.Count > 0; // hide or show the items which are "depended" of the visibility of the // recent files menu.. foreach (ToolStripItem item in hideItems) { item.Visible = menuItem.DropDownItems.Count > 0; } }