- Implement 2.0 image key feature.
[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                 
44                 #region Static Constructor
45                 static ToolStripManager ()
46                 {
47                         toolstrips = new List<ToolStrip> ();
48                         ToolStripManager.renderer = new ToolStripProfessionalRenderer ();
49                         ToolStripManager.render_mode = ToolStripManagerRenderMode.Professional;
50                         ToolStripManager.visual_styles_enabled = Application.RenderWithVisualStyles;
51                 }
52
53                 private ToolStripManager ()
54                 {
55                 }
56                 #endregion
57
58                 #region Public Properties
59                 public static ToolStripRenderer Renderer {
60                         get { return ToolStripManager.renderer; }
61                         set {
62                                 if (ToolStripManager.Renderer != value) {
63                                         ToolStripManager.renderer = value;
64                                         ToolStripManager.OnRendererChanged (EventArgs.Empty);
65                                 }
66                         }
67                 }
68
69                 public static ToolStripManagerRenderMode RenderMode {
70                         get { return ToolStripManager.render_mode; }
71                         set {
72                                 if (!Enum.IsDefined (typeof (ToolStripManagerRenderMode), value))
73                                         throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ToolStripManagerRenderMode", value));
74
75                                 if (ToolStripManager.render_mode != value) {
76                                         ToolStripManager.render_mode = value;
77
78                                         switch (value) {
79                                                 case ToolStripManagerRenderMode.Custom:
80                                                         throw new NotSupportedException ();
81                                                 case ToolStripManagerRenderMode.System:
82                                                         ToolStripManager.Renderer = new ToolStripProfessionalRenderer ();
83                                                         break;
84                                                 case ToolStripManagerRenderMode.Professional:
85                                                         ToolStripManager.Renderer = new ToolStripProfessionalRenderer ();
86                                                         break;
87                                         }
88                                 }
89                         }
90                 }
91
92                 public static bool VisualStylesEnabled {
93                         get { return ToolStripManager.visual_styles_enabled; }
94                         set {
95                                 if (ToolStripManager.visual_styles_enabled != value) {
96                                         ToolStripManager.visual_styles_enabled = value;
97
98                                         if (ToolStripManager.render_mode == ToolStripManagerRenderMode.Professional) {
99                                                 (ToolStripManager.renderer as ToolStripProfessionalRenderer).ColorTable.UseSystemColors = !value;
100                                                 ToolStripManager.OnRendererChanged (EventArgs.Empty);
101                                         }
102                                 }
103                         }
104                 }
105                 #endregion
106
107                 #region Public Methods
108                 public static ToolStrip FindToolStrip (string toolStripName)
109                 {
110                         lock (toolstrips)
111                                 foreach (ToolStrip ts in toolstrips)
112                                         if (ts.Name == toolStripName)
113                                                 return ts;
114                                                 
115                         return null;
116                 }
117                 #endregion
118                 
119                 #region Public Events
120                 public static event EventHandler RendererChanged;
121                 #endregion
122
123                 #region Private/Internal Methods
124                 internal static void AddToolStrip (ToolStrip ts)
125                 {
126                         lock (toolstrips)
127                                 toolstrips.Add (ts);
128                 }
129
130                 internal static void RemoveToolStrip (ToolStrip ts)
131                 {
132                         lock (toolstrips)
133                                 toolstrips.Remove (ts);
134                 }
135         
136                 internal static void FireAppClicked ()
137                 {
138                         if (AppClicked != null) AppClicked (null, EventArgs.Empty);
139                 }
140
141                 internal static void FireAppFocusChanged (Form form)
142                 {
143                         if (AppFocusChange != null) AppFocusChange (form, EventArgs.Empty);
144                 }
145
146                 internal static void FireAppFocusChanged (object sender)
147                 {
148                         if (AppFocusChange != null) AppFocusChange (sender, EventArgs.Empty);
149                 }
150                 private static void OnRendererChanged (EventArgs e)
151                 {
152                         if (RendererChanged != null) RendererChanged (null, e);
153                 }
154
155                 internal static event EventHandler AppClicked;
156                 internal static event EventHandler AppFocusChange;
157                 #endregion
158         }
159 }
160 #endif