source: trunk/MagicSoft/Cosy/gui/MGMenu.cc@ 4450

Last change on this file since 4450 was 1799, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 5.1 KB
Line 
1#include "MGMenu.h"
2
3
4MGMenuEntry::MGMenuEntry(TGMenuEntry *ent)
5{
6 memcpy(this, ent, sizeof(*ent));
7}
8
9// -------------------------------------------------------------
10//
11// Return the keycode corresponding to the hot string of
12// a TGmenuEntry
13//
14UInt_t MGPopupMenu::GetKeyCode(TGMenuEntry *el)
15{
16 TGHotString *str = MGMenuEntry(el).GetLabel();
17 return gVirtualX->KeysymToKeycode(str->GetHotChar());
18}
19
20// -------------------------------------------------------------
21//
22// Bind all hot keys used in this popup menu subsequentlt
23// to the TGMainFrame and send a HandleKey message to the
24// TGWindow
25//
26void MGPopupMenu::BindKeys(const TGWindow *w, TGMainFrame *frame)
27{
28 TIter Next(fEntryList);
29 TGMenuEntry *el;
30
31 //
32 // Loop Through all entries
33 //
34 while ((el=(TGMenuEntry*)Next()))
35 {
36 switch (el->GetType())
37 {
38 //
39 // For seperators and labels nothing to do
40 //
41 case kMenuSeparator:
42 case kMenuLabel:
43 continue;
44
45 //
46 // For an entry and a popup menu bind the hot key
47 // In case of a popup menu call BindKeys subsequently
48 //
49 case kMenuEntry:
50 case kMenuPopup:
51 frame->BindKey(w, GetKeyCode(el), kKeyMod1Mask);
52 if (el->GetType()==kMenuPopup)
53 MGMenuEntry(el).GetPopup()->BindKeys(w, frame);
54 continue;
55 }
56 }
57}
58
59/*
60 kMenuActiveMask = BIT(0),
61 kMenuEnableMask = BIT(1),
62 kMenuDefaultMask = BIT(2),
63 kMenuCheckedMask = BIT(3),
64 kMenuRadioMask = BIT(4),
65 kMenuHideMask = BIT(5)
66 */
67
68// -------------------------------------------------------------
69//
70// Handle a keyboard event. Return kFALSE in case of a
71// successfully send message, which means: close all open
72// popups.
73//
74Bool_t MGPopupMenu::HandleKey(Event_t *evt)
75{
76 //
77 // Loop through all entries in this popup menu. If the entry is
78 // an open popup menu send the key event to the open popup.
79 //
80 TIter Next(fEntryList);
81 TGMenuEntry *el;
82 while ((el=(TGMenuEntry*)Next()))
83 {
84 if (el->GetType()==kMenuPopup && el->GetStatus()&kMenuActiveMask)
85 return MGMenuEntry(el).GetPopup()->HandleKey(evt);
86 }
87
88 Next.Reset();
89
90 //
91 // If no open popup is found search the pressed key in this
92 // popup menu.
93 //
94 while ((el=(TGMenuEntry*)Next()))
95 {
96 switch (el->GetType())
97 {
98 //
99 // Do nothing
100 //
101 case kMenuSeparator:
102 case kMenuLabel:
103 continue;
104
105 //
106 // If the keycode corresponds to the hot key
107 // of this entry and the entry is enabled activate the entry
108 // and send the corresponding message/
109 //
110 case kMenuEntry:
111 if (GetKeyCode(el)==evt->fCode && el->GetStatus()&kMenuEnableMask)
112 {
113 Activate(el);
114 SendMessage(fMsgWindow, MK_MSG(kC_COMMAND, kCM_MENU),
115 el->GetEntryId(), (Long_t)MGMenuEntry(el).GetUserData());
116 return kFALSE;
117 }
118 continue;
119
120 //
121 // If the keycode corresponds to the hot key
122 // of this popup menu activate the popup menu.
123 //
124 case kMenuPopup:
125 if (GetKeyCode(el)!=evt->fCode)
126 continue;
127
128 Activate(el);
129 HandleTimer(NULL);
130 return kTRUE;
131 }
132 }
133 return kTRUE;
134}
135
136// -------------------------------------------------------------
137//
138// Bind the keys of all popups subsequently to the given main
139// frame. The menu bar hot keys are already bound by TGMenuBar.
140// Bind the Escape key to close the popups, too.
141//
142void MGMenuBar::BindKeys(TGMainFrame *frame)
143{
144 TGFrameElement *el=NULL;
145 TIter Next(fList);
146 while ((el = (TGFrameElement *)Next()))
147 ((MGPopupMenu*)((TGMenuTitle *) el->fFrame)->GetMenu())->BindKeys(this, frame);
148
149 frame->BindKey(this, 9/*ESC*/, 0);
150}
151
152// -------------------------------------------------------------
153//
154// Handle the keyboard event send to this menu bar.
155//
156Bool_t MGMenuBar::HandleKey(Event_t *event)
157{
158 //
159 // If this isn't a pressed key do nothing
160 //
161 if (event->fType != kGKeyPress)
162 return kTRUE;
163
164 //
165 // Check whether one popup is alreadu open
166 //
167 TGFrameElement *el=NULL;
168 TIter Next(fList);
169 while ((el = (TGFrameElement *)Next()))
170 {
171 if (!((TGMenuTitle*)el->fFrame)->GetState())
172 continue;
173
174 TGMenuTitle &f = *(TGMenuTitle*)el->fFrame;
175
176 //
177 // If a open popup is found redirect the key event to this
178 // popup menu
179 //
180 Bool_t rc = ((MGPopupMenu*)f.GetMenu())->HandleKey(event);
181
182 //
183 // If a message could be successfully send or the escape key
184 // was pressed close the popup.
185 //
186 if (!rc || event->fCode==9/*ESC*/)
187 {
188 f.SetState(kFALSE);
189 gVirtualX->GrabPointer(0, 0, 0, 0, kFALSE); // ungrab pointer
190 fCurrent = 0;
191 }
192
193 return kTRUE;
194 }
195
196 return TGMenuBar::HandleKey(event);
197}
Note: See TracBrowser for help on using the repository browser.