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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Jonathan Pobst
26 // Jonathan Pobst (monkey@jpobst.com)
31 using System.Runtime.InteropServices;
32 using System.ComponentModel;
33 using System.Collections.Generic;
35 namespace System.Windows.Forms
37 public sealed class ToolStripManager
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;
45 #region Static Constructor
46 static ToolStripManager ()
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;
55 private ToolStripManager ()
60 #region Public Properties
61 public static ToolStripRenderer Renderer {
62 get { return ToolStripManager.renderer; }
64 if (ToolStripManager.Renderer != value) {
65 ToolStripManager.renderer = value;
66 ToolStripManager.OnRendererChanged (EventArgs.Empty);
71 public static ToolStripManagerRenderMode RenderMode {
72 get { return ToolStripManager.render_mode; }
74 if (!Enum.IsDefined (typeof (ToolStripManagerRenderMode), value))
75 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripManagerRenderMode", value));
77 if (ToolStripManager.render_mode != value) {
78 ToolStripManager.render_mode = value;
81 case ToolStripManagerRenderMode.Custom:
82 throw new NotSupportedException ();
83 case ToolStripManagerRenderMode.System:
84 ToolStripManager.Renderer = new ToolStripProfessionalRenderer ();
86 case ToolStripManagerRenderMode.Professional:
87 ToolStripManager.Renderer = new ToolStripProfessionalRenderer ();
94 public static bool VisualStylesEnabled {
95 get { return ToolStripManager.visual_styles_enabled; }
97 if (ToolStripManager.visual_styles_enabled != value) {
98 ToolStripManager.visual_styles_enabled = value;
100 if (ToolStripManager.render_mode == ToolStripManagerRenderMode.Professional) {
101 (ToolStripManager.renderer as ToolStripProfessionalRenderer).ColorTable.UseSystemColors = !value;
102 ToolStripManager.OnRendererChanged (EventArgs.Empty);
109 #region Public Methods
110 public static ToolStrip FindToolStrip (string toolStripName)
113 foreach (ToolStrip ts in toolstrips)
114 if (ts.Name == toolStripName)
120 public static bool IsShortcutDefined (Keys shortcut)
123 foreach (ToolStripMenuItem tsmi in menu_items)
124 if (tsmi.ShortcutKeys == shortcut)
130 public static bool IsValidShortcut (Keys shortcut)
132 // Anything with an F1 - F12 is a shortcut
133 if ((shortcut & Keys.F1) == Keys.F1)
135 else if ((shortcut & Keys.F2) == Keys.F2)
137 else if ((shortcut & Keys.F3) == Keys.F3)
139 else if ((shortcut & Keys.F4) == Keys.F4)
141 else if ((shortcut & Keys.F5) == Keys.F5)
143 else if ((shortcut & Keys.F6) == Keys.F6)
145 else if ((shortcut & Keys.F7) == Keys.F7)
147 else if ((shortcut & Keys.F8) == Keys.F8)
149 else if ((shortcut & Keys.F9) == Keys.F9)
151 else if ((shortcut & Keys.F10) == Keys.F10)
153 else if ((shortcut & Keys.F11) == Keys.F11)
155 else if ((shortcut & Keys.F12) == Keys.F12)
158 // Modifier keys alone are not shortcuts
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:
170 // Anything else with a modifier key is a shortcut
171 if ((shortcut & Keys.Alt) == Keys.Alt)
173 else if ((shortcut & Keys.Control) == Keys.Control)
175 else if ((shortcut & Keys.Shift) == Keys.Shift)
178 // Anything else is not a shortcut
183 #region Public Events
184 public static event EventHandler RendererChanged;
187 #region Private/Internal Methods
188 internal static void AddToolStrip (ToolStrip ts)
194 internal static bool ProcessCmdKey (ref Message m, Keys keyData)
197 foreach (ToolStripMenuItem tsmi in menu_items)
198 if (tsmi.ProcessCmdKey (ref m, keyData) == true)
204 internal static void AddToolStripMenuItem (ToolStripMenuItem tsmi)
207 menu_items.Add (tsmi);
210 internal static void RemoveToolStrip (ToolStrip ts)
213 toolstrips.Remove (ts);
216 internal static void RemoveToolStripMenuItem (ToolStripMenuItem tsmi)
219 menu_items.Remove (tsmi);
222 internal static void FireAppClicked ()
224 if (AppClicked != null) AppClicked (null, EventArgs.Empty);
227 internal static void FireAppFocusChanged (Form form)
229 if (AppFocusChange != null) AppFocusChange (form, EventArgs.Empty);
232 internal static void FireAppFocusChanged (object sender)
234 if (AppFocusChange != null) AppFocusChange (sender, EventArgs.Empty);
237 private static void OnRendererChanged (EventArgs e)
239 if (RendererChanged != null) RendererChanged (null, e);
242 internal static event EventHandler AppClicked;
243 internal static event EventHandler AppFocusChange;