1 // MRUManager.h 2 // this file is part of Context Free 3 // --------------------- 4 // Copyright (C) 2008-2012 John Horigan - john@glyphic.com 5 // 6 // This program is free software; you can redistribute it and/or 7 // modify it under the terms of the GNU General Public License 8 // as published by the Free Software Foundation; either version 2 9 // of the License, or (at your option) any later version. 10 // 11 // This program is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with this program; if not, write to the Free Software 18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 // 20 // John Horigan can be contacted at john@glyphic.com or at 21 // John Horigan, 1209 Villa St., Mountain View, CA 94041-1123, USA 22 // 23 // 24 25 #pragma once 26 27 28 // MRU list manager. 29 // 30 // Written by: John Horigan <john@glyphic.com> 31 // in Managed C++ targeting the .NET 2.0 framework 32 // and the ToolStripMenuItem class therein 33 // 34 // based on a MRU list manager 35 // Written by: Alex Farber <alexm@cmt.co.il> 36 // in C#, targeting .NET 1.1 framework 37 38 /******************************************************************************* 39 40 How to use it: 41 42 1) Add menu item Recent Files (or any name you want) to a main application menu. 43 This item is used by MRUManager as popup menu for MRU list. 44 45 -- OR -- 46 47 Add a menu item separator to a main application menu. This is used by MRUManager 48 to indicate where to insert the MRU list in the menu. 49 50 2) Implement MRU click event handler in the form class: 51 52 System::Void Form1::MRU_Click(System::Object^ sender, System::EventArgs^ e) 53 { 54 String^ filename = mruManager->OnMRUClicked(sender, e); 55 56 // open file here 57 } 58 59 3) Add MRUManager member to the form class and initialize it: 60 61 private: OzoneUtil::MRUManager^ mruManager; 62 63 public: 64 Form1(void) 65 { 66 InitializeComponent(); 67 // 68 //TODO: Add the constructor code here 69 // 70 mruManager = gcnew MRUManager(); 71 mruManager->Initialize( 72 this, // owner form 73 gcnew System::EventHandler(this, &Form1::MRU_Click), 74 // MRU click event handler 75 mnuFileMRU, // Recent Files menu item or separator 76 "Software\\MyCompany\\MyProgram"); // Registry path to keep MRU list 77 mruManager->autoDirectory = true; // or false, as you wish 78 mruManager->CurrentDir = "....."; // default is current directory, or the most 79 // recently used directory if autoDirectory is true 80 mruManager->MaxMRULength = ?; // default is 10 81 mruManager->MaxDisplayNameLength = ?; // default is 40 82 } 83 84 85 NOTES: 86 - If Registry path is, for example, "Software\MyCompany\MyProgram", 87 MRU list is kept in 88 HKEY_CURRENT_USER\Software\MyCompany\MyProgram\MRU Registry entry. 89 90 - CurrentDir is used to show file names in the menu. If file is in 91 this directory, only file name is shown. If the file is in a sub- 92 directory then the relative path is shown. 93 94 4) Call MRUManager Add and Remove functions when necessary: 95 96 mruManager.Add(fileName); // when file is successfully opened 97 // or in a successful SaveAs 98 99 mruManager.Remove(fileName); // when Open File operation failed 100 101 *******************************************************************************/ 102 103 namespace OzoneUtil { 104 105 public ref class MRUManager 106 { 107 public: MRUManager()108 MRUManager() 109 : maxNumberOfFiles(10), 110 maxDisplayLength(40), 111 autoDirectory(false), 112 inlineListIsEmpty(true) 113 { 114 lowerSeparator = gcnew System::Windows::Forms::ToolStripSeparator(); 115 } 116 117 protected: ~MRUManager()118 ~MRUManager() 119 { 120 delete lowerSeparator; 121 } 122 123 public: 124 int maxNumberOfFiles; 125 int maxDisplayLength; 126 System::String^ currentDirectory; 127 bool autoDirectory; 128 129 void Initialize(System::Windows::Forms::Form^ owner, 130 System::EventHandler^ handler, 131 System::Windows::Forms::ToolStripItem^ mruItem, 132 System::String^ regPath); 133 void Add(System::String^ file); 134 void Remove(System::String^ file) { Remove(file, true); } 135 System::String^ OnMRUClicked(System::Object^ sender, System::EventArgs^ e); 136 137 private: 138 System::Collections::ArrayList mruList; 139 System::Windows::Forms::Form^ ownerForm; 140 System::Windows::Forms::ToolStripItem^ menuItemMRU; 141 System::Windows::Forms::ToolStripMenuItem^ menuItemParent; 142 System::String^ registryPath; 143 System::EventHandler^ clickHandler; 144 static System::String^ regEntryName = "file"; 145 146 bool isInlineList; 147 System::Windows::Forms::ToolStripSeparator^ lowerSeparator; 148 bool inlineListIsEmpty; 149 150 System::Void OnMRUParentPopup(System::Object^ sender, System::EventArgs^ e); 151 152 System::Void OnOwnerClosing(System::Object^ sender, 153 System::ComponentModel::CancelEventArgs^ e); 154 155 void LoadMRU(); 156 void UpdateMenu(); 157 void Remove(System::String^ file, bool update); 158 159 System::String^ GetDisplayName(System::String^ fullName, int index); 160 System::String^ GetShortDisplayName(System::String^ fullName, int maxLen); 161 }; 162 } 163 164 165 166