* TreeView.cs: Don't draw the selected node when we lose
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / MenuItem.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2005 Novell, Inc.
21 //
22 // Authors:
23 //      Jordi Mas i Hernandez, jordi@ximian.com
24 //
25 //
26
27 // NOT COMPLETE
28
29 using System.Collections;
30 using System.ComponentModel;
31 using System.ComponentModel.Design;
32 using System.Drawing;
33 using System.Drawing.Text;
34
35 namespace System.Windows.Forms
36 {
37         [DefaultProperty("Text")]
38         [DefaultEvent("Click")]
39         [DesignTimeVisible(false)]
40         [ToolboxItem(false)]
41         public class MenuItem : Menu
42         {               
43                 internal bool separator;
44                 internal bool break_;
45                 internal bool bar_break;
46                 private Shortcut shortcut;
47                 private string text;
48                 private bool checked_;
49                 private bool radiocheck;
50                 private bool enabled;
51                 private char mnemonic;
52                 private bool showshortcut;
53                 private int index;
54                 private bool mdilist;
55                 private bool defaut_item;
56                 private bool visible;
57                 private bool ownerdraw;
58                 private int menuid;
59                 private int mergeorder;
60                 private int xtab;
61                 private int menuheight;
62                 private bool menubar;
63                 private DrawItemState status;
64                 private MenuMerge mergetype;
65                 
66                 public MenuItem (): base (null)
67                 {       
68                         CommonConstructor (string.Empty);
69                         shortcut = Shortcut.None;
70                 }
71
72                 public MenuItem (string text) : base (null)
73                 {
74                         CommonConstructor (text);
75                         shortcut = Shortcut.None;
76                 }
77
78                 public MenuItem (string text, EventHandler onClick) : base (null)
79                 {
80                         CommonConstructor (text);
81                         shortcut = Shortcut.None;
82                         Click += onClick;
83                 }
84
85                 public MenuItem (string text, MenuItem[] items) : base (items)
86                 {
87                         CommonConstructor (text);
88                         shortcut = Shortcut.None;
89                 }
90
91                 public MenuItem (string text, EventHandler onClick, Shortcut shortcut) : base (null)
92                 {
93                         CommonConstructor (text);
94                         Click += onClick;
95                         this.shortcut = shortcut;
96                 }
97
98                 public MenuItem (MenuMerge mergeType, int mergeOrder, Shortcut shortcut, string text,
99                         EventHandler onClick, EventHandler onPopup,  EventHandler onSelect,  MenuItem[] items)
100                         : base (items)
101                 {
102                         CommonConstructor (text);
103                         this.shortcut = shortcut;
104                         mergeorder = mergeOrder;
105                         mergetype = mergeType;
106
107                         Click += onClick;
108                         Popup += onPopup;
109                         Select += onSelect;
110                 }
111
112                 private void CommonConstructor (string text)
113                 {
114                         defaut_item = false;
115                         separator = false;
116                         break_ = false;
117                         bar_break = false;
118                         checked_ = false;
119                         radiocheck = false;
120                         enabled = true;
121                         showshortcut = true;
122                         visible = true;
123                         ownerdraw = false;
124                         status = DrawItemState.None;
125                         menubar = false;
126                         menuheight = 0;
127                         xtab = 0;
128                         index = -1;
129                         mnemonic = '\0';
130                         menuid = -1;
131                         mergeorder = 0;
132                         mergetype = MenuMerge.Add;
133                         Text = text;    // Text can change separator status
134                 }
135
136                 #region Events
137                 public event EventHandler Click;
138                 public event DrawItemEventHandler DrawItem;
139                 public event MeasureItemEventHandler MeasureItem;
140                 public event EventHandler Popup;
141                 public event EventHandler Select;
142                 #endregion // Events
143
144                 #region Public Properties
145
146                 [Browsable(false)]
147                 [DefaultValue(false)]
148                 public bool BarBreak {
149                         get { return break_; }
150                         set { break_ = value; }
151                 }
152
153                 [Browsable(false)]
154                 [DefaultValue(false)]
155                 public bool Break {
156                         get { return bar_break; }
157                         set { bar_break = value; }
158                 }
159
160                 [DefaultValue(false)]
161                 public bool Checked {
162                         get { return checked_; }
163                         set { checked_ = value; }
164                 }
165
166                 [DefaultValue(false)]
167                 public bool DefaultItem {
168                         get { return defaut_item; }
169                         set { defaut_item = value; }
170                 }
171
172                 [DefaultValue(true)]
173                 [Localizable(true)]
174                 public bool Enabled {
175                         get { return enabled; }
176                         set { enabled = value; }
177                 }
178
179                 [Browsable(false)]
180                 public int Index {
181                         get { return index; }
182                         set { index = value; }
183                 }
184
185                 [Browsable(false)]
186                 public override bool IsParent {
187                         get { return IsPopup; }
188                 }
189
190                 [DefaultValue(false)]
191                 public bool MdiList {
192                         get { return mdilist; }
193                         set { mdilist = value; }
194                 }
195
196                 protected int MenuID {
197                         get { return menuid; }
198                 }
199
200                 [DefaultValue(0)]
201                 public int MergeOrder {
202                         get { return mergeorder; }
203                         set { mergeorder = value; }
204                 }
205
206                 [DefaultValue(MenuMerge.Add)]
207                 public MenuMerge MergeType {
208                         get { return mergetype; }
209                         set {
210                                 if (!Enum.IsDefined (typeof (MenuMerge), value))
211                                         throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for MenuMerge", value));
212
213                                 mergetype = value;
214                         }
215                 }
216
217                 [Browsable(false)]
218                 public char Mnemonic {
219                         get { return mnemonic; }
220                 }
221
222                 [DefaultValue(false)]
223                 public bool OwnerDraw {
224                         get { return ownerdraw; }
225                         set { ownerdraw = value; }
226                 }
227
228                 [Browsable(false)]
229                 public Menu Parent {
230                         get { return parent_menu;}
231                 }
232
233                 [DefaultValue(false)]
234                 public bool RadioCheck {
235                         get { return radiocheck; }
236                         set { radiocheck = value; }
237                 }
238
239                 [DefaultValue(Shortcut.None)]
240                 [Localizable(true)]
241                 public Shortcut Shortcut {
242                         get { return shortcut;}
243                         set {
244                                 if (!Enum.IsDefined (typeof (Shortcut), value))
245                                         throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for Shortcut", value));
246
247                                 shortcut = value;
248                         }
249                 }
250
251                 [DefaultValue(true)]
252                 [Localizable(true)]
253                 public bool ShowShortcut {
254                         get { return showshortcut;}
255                         set { showshortcut = value; }
256                 }
257
258                 [Localizable(true)]
259                 public string Text {
260                         get { return text; }
261                         set {
262                                 text = value;
263
264                                 if (text == "-")
265                                         separator = true;
266                                 else
267                                         separator = false;
268
269                                 ProcessMnemonic ();
270                         }
271                 }
272
273                 [DefaultValue(true)]
274                 [Localizable(true)]
275                 public bool Visible {
276                         get { return visible;}
277                         set { 
278                                 if (value == visible)
279                                         return;
280
281                                 visible = value;
282
283                                 if (menu_items != null) {
284                                         foreach (MenuItem mi in menu_items)
285                                                 mi.Visible = value;
286                                 }
287
288                                 if (parent_menu != null) {
289                                         parent_menu.IsDirty = true;
290                                         parent_menu.creating = false;
291                                 }
292                         }
293                 }
294
295                 #endregion Public Properties
296
297                 #region Private Properties
298
299                 internal bool IsPopup {
300                         get {
301                                 if (menu_items.Count > 0)
302                                         return true;
303                                 else
304                                         return false;
305                         }
306                 }
307                 
308                 internal bool MeasureEventDefined {
309                         get { 
310                                 if (ownerdraw == true && MeasureItem != null) {
311                                         return true;
312                                 } else {
313                                         return false;
314                                 }
315                         }
316                 }
317                 
318                 internal bool MenuBar {
319                         get { return menubar; }
320                         set { menubar = value; }
321                 }
322                 
323                 internal int MenuHeight {
324                         get { return menuheight; }
325                         set { menuheight = value; }
326                 }       
327
328                 internal bool Separator {
329                         get { return separator; }
330                         set { separator = value; }
331                 }
332                 
333                 internal DrawItemState Status {
334                         get { return status; }
335                         set { status = value; }
336                 }
337                 
338                 internal int XTab {
339                         get { return xtab; }
340                         set { xtab = value; }
341                 }
342
343                 #endregion Private Properties
344
345                 #region Public Methods
346
347                 public virtual MenuItem CloneMenu ()
348                 {
349                         MenuItem item = new MenuItem ();
350                         item.CloneMenu (this);
351                         return item;
352                 }
353
354                 protected void CloneMenu (MenuItem menuitem)
355                 {
356                         base.CloneMenu (menuitem); // Copy subitems
357
358                         // Properties
359                         BarBreak = menuitem.BarBreak;
360                         Break = menuitem.Break;
361                         Checked = menuitem.Checked;
362                         DefaultItem = menuitem.DefaultItem;
363                         Enabled = menuitem.Enabled;                     
364                         MergeOrder = menuitem.MergeOrder;
365                         MergeType = menuitem.MergeType;
366                         OwnerDraw = menuitem.OwnerDraw;
367                         //Parent = menuitem.Parent;
368                         RadioCheck = menuitem.RadioCheck;
369                         Shortcut = menuitem.Shortcut;
370                         ShowShortcut = menuitem.ShowShortcut;
371                         Text = menuitem.Text;
372                         Visible = menuitem.Visible;
373
374                         // Events
375                         Click = menuitem.Click;
376                         DrawItem = menuitem.DrawItem;
377                         MeasureItem = menuitem.MeasureItem;
378                         Popup = menuitem.Popup;
379                         Select = menuitem.Select;
380                 }
381
382                 protected override void Dispose (bool disposing)
383                 {
384                         base.Dispose (disposing);                       
385                 }
386
387                 // This really clones the item
388                 public virtual MenuItem MergeMenu ()
389                 {
390                         MenuItem item = new MenuItem ();
391                         item.CloneMenu (this);
392                         return item;
393                 }
394
395                 public void MergeMenu (MenuItem menuitem)
396                 {
397                         base.MergeMenu (menuitem);
398                 }
399
400                 protected virtual void OnClick (EventArgs e)
401                 {
402                         if (Click != null)
403                                 Click (this, e);
404                 }
405
406                 protected virtual void OnDrawItem (DrawItemEventArgs e)
407                 {
408                         if (DrawItem != null) {
409                                 DrawItem (this, e);
410                                 return;
411                         }
412                         
413                         ThemeEngine.Current.DrawMenuItem (this, e);     
414                 }
415
416
417                 protected virtual void OnInitMenuPopup (EventArgs e)
418                 {
419                         OnPopup (e);
420                 }
421
422                 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
423                 {
424                         if (MeasureItem != null)
425                                 MeasureItem (this, e);
426                 }
427
428                 protected virtual void OnPopup (EventArgs e)
429                 {
430                         if (Popup != null)
431                                 Popup (this, e);
432                 }
433
434                 protected virtual void OnSelect (EventArgs e)
435                 {
436                         if (Select != null)
437                                 Select (this, e);
438                 }
439
440                 public void PerformClick ()
441                 {
442                         OnClick (EventArgs.Empty);
443                 }
444
445                 public virtual void PerformSelect ()
446                 {
447                         OnSelect (EventArgs.Empty);
448                 }
449
450                 public override string ToString ()
451                 {
452                         return base.ToString () + ", Items.Count: " + MenuItems.Count + ", Text: " + text;
453                 }
454
455                 #endregion Public Methods
456
457                 #region Private Methods
458
459                 internal void Create ()
460                 {
461                         IntPtr hSubMenu = IntPtr.Zero;
462
463                         menuid = index = MenuAPI.InsertMenuItem (Parent.Handle, -1, true, this, ref hSubMenu);
464                         IsDirty = false;
465
466                         if (IsPopup) {
467                                 menu_handle = hSubMenu;
468                                 CreateItems ();
469                         }
470                 }
471                 
472                 internal void PerformDrawItem (DrawItemEventArgs e)
473                 {
474                         OnDrawItem (e);
475                 }
476                 
477                 internal void PerformMeasureItem (MeasureItemEventArgs e)
478                 {
479                         OnMeasureItem (e);
480                 }
481
482                 private void ProcessMnemonic ()
483                 {
484                         if (text.Length < 2) {
485                                 mnemonic = '\0';
486                                 return;
487                         }
488
489                         bool bPrevAmp = false;
490                         for (int i = 0; i < text.Length -1 ; i++) {
491                                 if (text[i] == '&') {
492                                         if (bPrevAmp == false &&  (text[i+1] != '&')) {
493                                                 mnemonic = Char.ToUpper (text[i+1]);
494                                                 return;
495                                         }
496
497                                         bPrevAmp = true;
498                                 }
499                                 else
500                                         bPrevAmp = false;
501                         }
502
503                         mnemonic = '\0';
504                 }
505
506                 private string GetShortCutTextCtrl () { return "Ctrl"; }
507                 private string GetShortCutTextAlt () { return "Alt"; }
508                 private string GetShortCutTextShift () { return "Shift"; }              
509
510                 internal string GetShortCutText ()
511                 {
512                         /* Ctrl+A - Ctrl+Z */
513                         if (Shortcut >= Shortcut.CtrlA && Shortcut <= Shortcut.CtrlZ)
514                                 return GetShortCutTextCtrl () + "+" + (char)((int) 'A' + (int)(Shortcut - Shortcut.CtrlA));
515
516                         /* Alt+0 - Alt+9 */
517                         if (Shortcut >= Shortcut.Alt0 && Shortcut <= Shortcut.Alt9)
518                                 return GetShortCutTextAlt () + "+" + (char)((int) '0' + (int)(Shortcut - Shortcut.Alt0));
519
520                         /* Alt+F1 - Alt+F2 */
521                         if (Shortcut >= Shortcut.AltF1 && Shortcut <= Shortcut.AltF9)
522                                 return GetShortCutTextAlt () + "+F" + (char)((int) '1' + (int)(Shortcut - Shortcut.AltF1));
523
524                         /* Ctrl+0 - Ctrl+9 */
525                         if (Shortcut >= Shortcut.Ctrl0 && Shortcut <= Shortcut.Ctrl9)
526                                 return GetShortCutTextCtrl () + "+" + (char)((int) '0' + (int)(Shortcut - Shortcut.Ctrl0));
527                                                         
528                         /* Ctrl+F0 - Ctrl+F9 */
529                         if (Shortcut >= Shortcut.CtrlF1 && Shortcut <= Shortcut.CtrlF9)
530                                 return GetShortCutTextCtrl () + "+F" + (char)((int) '1' + (int)(Shortcut - Shortcut.CtrlF1));
531                                 
532                         /* Ctrl+Shift+0 - Ctrl+Shift+9 */
533                         if (Shortcut >= Shortcut.CtrlShift0 && Shortcut <= Shortcut.CtrlShift9)
534                                 return GetShortCutTextCtrl () + "+" + GetShortCutTextShift () + "+" + (char)((int) '0' + (int)(Shortcut - Shortcut.CtrlShift0));
535                                 
536                         /* Ctrl+Shift+A - Ctrl+Shift+Z */
537                         if (Shortcut >= Shortcut.CtrlShiftA && Shortcut <= Shortcut.CtrlShiftZ)
538                                 return GetShortCutTextCtrl () + "+" + GetShortCutTextShift () + "+" + (char)((int) 'A' + (int)(Shortcut - Shortcut.CtrlShiftA));
539
540                         /* Ctrl+Shift+F1 - Ctrl+Shift+F9 */
541                         if (Shortcut >= Shortcut.CtrlShiftF1 && Shortcut <= Shortcut.CtrlShiftF9)
542                                 return GetShortCutTextCtrl () + "+" + GetShortCutTextShift () + "+F" + (char)((int) '1' + (int)(Shortcut - Shortcut.CtrlShiftF1));
543                                 
544                         /* F1 - F9 */
545                         if (Shortcut >= Shortcut.F1 && Shortcut <= Shortcut.F9)
546                                 return "F" + (char)((int) '1' + (int)(Shortcut - Shortcut.F1));
547                                 
548                         /* Shift+F1 - Shift+F9 */
549                         if (Shortcut >= Shortcut.ShiftF1 && Shortcut <= Shortcut.ShiftF9)
550                                 return GetShortCutTextShift () + "+F" + (char)((int) '1' + (int)(Shortcut - Shortcut.ShiftF1));
551                         
552                         /* Special cases */
553                         switch (Shortcut) {
554                                 case Shortcut.AltBksp:
555                                         return "AltBksp";
556                                 case Shortcut.AltF10:
557                                         return GetShortCutTextAlt () + "+F10";
558                                 case Shortcut.AltF11:
559                                         return GetShortCutTextAlt () + "+F11";
560                                 case Shortcut.AltF12:
561                                         return GetShortCutTextAlt () + "+F12";
562                                 case Shortcut.CtrlDel:          
563                                         return GetShortCutTextCtrl () + "+Del";
564                                 case Shortcut.CtrlF10:
565                                         return GetShortCutTextCtrl () + "+F10";
566                                 case Shortcut.CtrlF11:
567                                         return GetShortCutTextCtrl () + "+F11";
568                                 case Shortcut.CtrlF12:
569                                         return GetShortCutTextCtrl () + "+F12";
570                                 case Shortcut.CtrlIns:
571                                         return GetShortCutTextCtrl () + "+Ins";
572                                 case Shortcut.CtrlShiftF10:
573                                         return GetShortCutTextCtrl () + "+" + GetShortCutTextShift () + "+F10";
574                                 case Shortcut.CtrlShiftF11:
575                                         return GetShortCutTextCtrl () + "+" + GetShortCutTextShift () + "+F11";
576                                 case Shortcut.CtrlShiftF12:
577                                         return GetShortCutTextCtrl () + "+" + GetShortCutTextShift () + "+F12";
578                                 case Shortcut.Del:
579                                         return "Del";
580                                 case Shortcut.F10:
581                                         return "F10";   
582                                 case Shortcut.F11:
583                                         return "F11";   
584                                 case Shortcut.F12:
585                                         return "F12";   
586                                 case Shortcut.Ins:
587                                         return "Ins";   
588                                 case Shortcut.None:
589                                         return "None";  
590                                 case Shortcut.ShiftDel:
591                                         return GetShortCutTextShift () + "+Del";
592                                 case Shortcut.ShiftF10:
593                                         return GetShortCutTextShift () + "+F10";
594                                 case Shortcut.ShiftF11:
595                                         return GetShortCutTextShift () + "+F11";
596                                 case Shortcut.ShiftF12:
597                                         return GetShortCutTextShift () + "+F12";                                
598                                 case Shortcut.ShiftIns:
599                                         return GetShortCutTextShift () + "+Ins";
600                                 default:
601                                         break;
602                                 }
603                                 
604                         return "";
605                 }
606
607                 #endregion Private Methods
608
609         }
610 }
611
612