2007-04-02 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripManager.cs
1 //
2 // ToolStripManager.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28
29 #if NET_2_0
30 using System.Drawing;
31 using System.Runtime.InteropServices;
32 using System.ComponentModel;
33 using System.Collections.Generic;
34
35 namespace System.Windows.Forms
36 {
37         public sealed class ToolStripManager
38         {
39                 private static ToolStripRenderer renderer;
40                 private static ToolStripManagerRenderMode render_mode;
41                 private static bool visual_styles_enabled;
42                 private static List<ToolStrip> toolstrips;
43                 private static List<ToolStripMenuItem> menu_items;
44                 
45                 #region Static Constructor
46                 static ToolStripManager ()
47                 {
48                         toolstrips = new List<ToolStrip> ();
49                         menu_items = new List<ToolStripMenuItem> ();
50                         ToolStripManager.renderer = new ToolStripProfessionalRenderer ();
51                         ToolStripManager.render_mode = ToolStripManagerRenderMode.Professional;
52                         ToolStripManager.visual_styles_enabled = Application.RenderWithVisualStyles;
53                 }
54
55                 private ToolStripManager ()
56                 {
57                 }
58                 #endregion
59
60                 #region Public Properties
61                 public static ToolStripRenderer Renderer {
62                         get { return ToolStripManager.renderer; }
63                         set {
64                                 if (ToolStripManager.Renderer != value) {
65                                         ToolStripManager.renderer = value;
66                                         ToolStripManager.OnRendererChanged (EventArgs.Empty);
67                                 }
68                         }
69                 }
70
71                 public static ToolStripManagerRenderMode RenderMode {
72                         get { return ToolStripManager.render_mode; }
73                         set {
74                                 if (!Enum.IsDefined (typeof (ToolStripManagerRenderMode), value))
75                                         throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripManagerRenderMode", value));
76
77                                 if (ToolStripManager.render_mode != value) {
78                                         ToolStripManager.render_mode = value;
79
80                                         switch (value) {
81                                                 case ToolStripManagerRenderMode.Custom:
82                                                         throw new NotSupportedException ();
83                                                 case ToolStripManagerRenderMode.System:
84                                                         ToolStripManager.Renderer = new ToolStripProfessionalRenderer ();
85                                                         break;
86                                                 case ToolStripManagerRenderMode.Professional:
87                                                         ToolStripManager.Renderer = new ToolStripProfessionalRenderer ();
88                                                         break;
89                                         }
90                                 }
91                         }
92                 }
93
94                 public static bool VisualStylesEnabled {
95                         get { return ToolStripManager.visual_styles_enabled; }
96                         set {
97                                 if (ToolStripManager.visual_styles_enabled != value) {
98                                         ToolStripManager.visual_styles_enabled = value;
99
100                                         if (ToolStripManager.render_mode == ToolStripManagerRenderMode.Professional) {
101                                                 (ToolStripManager.renderer as ToolStripProfessionalRenderer).ColorTable.UseSystemColors = !value;
102                                                 ToolStripManager.OnRendererChanged (EventArgs.Empty);
103                                         }
104                                 }
105                         }
106                 }
107                 #endregion
108
109                 #region Public Methods
110                 public static ToolStrip FindToolStrip (string toolStripName)
111                 {
112                         lock (toolstrips)
113                                 foreach (ToolStrip ts in toolstrips)
114                                         if (ts.Name == toolStripName)
115                                                 return ts;
116                                                 
117                         return null;
118                 }
119                 
120                 public static bool IsShortcutDefined (Keys shortcut)
121                 {
122                         lock (menu_items)
123                                 foreach (ToolStripMenuItem tsmi in menu_items)
124                                         if (tsmi.ShortcutKeys == shortcut)
125                                                 return true;
126
127                         return false;
128                 }
129                 
130                 public static bool IsValidShortcut (Keys shortcut)
131                 {
132                         // Anything with an F1 - F12 is a shortcut
133                         if ((shortcut & Keys.F1) == Keys.F1)
134                                 return true;
135                         else if ((shortcut & Keys.F2) == Keys.F2)
136                                 return true;
137                         else if ((shortcut & Keys.F3) == Keys.F3)
138                                 return true;
139                         else if ((shortcut & Keys.F4) == Keys.F4)
140                                 return true;
141                         else if ((shortcut & Keys.F5) == Keys.F5)
142                                 return true;
143                         else if ((shortcut & Keys.F6) == Keys.F6)
144                                 return true;
145                         else if ((shortcut & Keys.F7) == Keys.F7)
146                                 return true;
147                         else if ((shortcut & Keys.F8) == Keys.F8)
148                                 return true;
149                         else if ((shortcut & Keys.F9) == Keys.F9)
150                                 return true;
151                         else if ((shortcut & Keys.F10) == Keys.F10)
152                                 return true;
153                         else if ((shortcut & Keys.F11) == Keys.F11)
154                                 return true;
155                         else if ((shortcut & Keys.F12) == Keys.F12)
156                                 return true;
157                                 
158                         // Modifier keys alone are not shortcuts
159                         switch (shortcut) {
160                                 case Keys.Alt:
161                                 case Keys.Control:
162                                 case Keys.Shift:
163                                 case Keys.Alt | Keys.Control:
164                                 case Keys.Alt | Keys.Shift:
165                                 case Keys.Control | Keys.Shift:
166                                 case Keys.Alt | Keys.Control | Keys.Shift:
167                                         return false;
168                         }
169         
170                         // Anything else with a modifier key is a shortcut
171                         if ((shortcut & Keys.Alt) == Keys.Alt)
172                                 return true;
173                         else if ((shortcut & Keys.Control) == Keys.Control)
174                                 return true;
175                         else if ((shortcut & Keys.Shift) == Keys.Shift)
176                                 return true;
177
178                         // Anything else is not a shortcut
179                         return false;
180                 }
181                 #endregion
182                 
183                 #region Public Events
184                 public static event EventHandler RendererChanged;
185                 #endregion
186
187                 #region Private/Internal Methods
188                 internal static void AddToolStrip (ToolStrip ts)
189                 {
190                         lock (toolstrips)
191                                 toolstrips.Add (ts);
192                 }
193
194                 internal static bool ProcessCmdKey (ref Message m, Keys keyData)
195                 {
196                         lock (menu_items)
197                                 foreach (ToolStripMenuItem tsmi in menu_items)
198                                         if (tsmi.ProcessCmdKey (ref m, keyData) == true)
199                                                 return true;
200                         
201                         return false;
202                 }
203                 
204                 internal static void AddToolStripMenuItem (ToolStripMenuItem tsmi)
205                 {
206                         lock (menu_items)
207                                 menu_items.Add (tsmi);
208                 }
209                 
210                 internal static void RemoveToolStrip (ToolStrip ts)
211                 {
212                         lock (toolstrips)
213                                 toolstrips.Remove (ts);
214                 }
215
216                 internal static void RemoveToolStripMenuItem (ToolStripMenuItem tsmi)
217                 {
218                         lock (menu_items)
219                                 menu_items.Remove (tsmi);
220                 }
221
222                 internal static void FireAppClicked ()
223                 {
224                         if (AppClicked != null) AppClicked (null, EventArgs.Empty);
225                 }
226
227                 internal static void FireAppFocusChanged (Form form)
228                 {
229                         if (AppFocusChange != null) AppFocusChange (form, EventArgs.Empty);
230                 }
231
232                 internal static void FireAppFocusChanged (object sender)
233                 {
234                         if (AppFocusChange != null) AppFocusChange (sender, EventArgs.Empty);
235                 }
236                 
237                 private static void OnRendererChanged (EventArgs e)
238                 {
239                         if (RendererChanged != null) RendererChanged (null, e);
240                 }
241
242                 internal static event EventHandler AppClicked;
243                 internal static event EventHandler AppFocusChange;
244                 #endregion
245         }
246 }
247 #endif