source: trunk/Mars/mbase/MGMenu.cc@ 20003

Last change on this file since 20003 was 19991, checked in by tbretz, 4 years ago
Root 6.22 requires additonal explicit includes.
File size: 6.6 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 4/2003 <mailto:tbretz@astro-uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2003
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MGMenu, MGPopupMenu
28//
29/////////////////////////////////////////////////////////////////////////////
30#include "MGMenu.h"
31
32#include <TVirtualX.h> // gVirtualX <6.22>
33
34#if ROOT_VERSION_CODE < ROOT_VERSION(6,00,00)
35MGMenuEntry::MGMenuEntry(TGMenuEntry *ent)
36{
37 memcpy(this, ent, sizeof(*ent));
38}
39#endif
40
41// -------------------------------------------------------------
42//
43// Return the keycode corresponding to the hot string of
44// a TGmenuEntry
45//
46UInt_t MGPopupMenu::GetKeyCode(TGMenuEntry *el)
47{
48 TGHotString *str = MGMenuEntry(el).GetLabel();
49 return gVirtualX->KeysymToKeycode(str->GetHotChar());
50}
51
52// -------------------------------------------------------------
53//
54// Bind all hot keys used in this popup menu subsequentlt
55// to the TGMainFrame and send a HandleKey message to the
56// TGWindow
57//
58void MGPopupMenu::BindKeys(const TGWindow *w, TGMainFrame *frame)
59{
60 TIter Next(fEntryList);
61 TGMenuEntry *el;
62
63 //
64 // Loop Through all entries
65 //
66 while ((el=(TGMenuEntry*)Next()))
67 {
68 switch (el->GetType())
69 {
70 //
71 // For seperators and labels nothing to do
72 //
73 case kMenuSeparator:
74 case kMenuLabel:
75 continue;
76
77 //
78 // For an entry and a popup menu bind the hot key
79 // In case of a popup menu call BindKeys subsequently
80 //
81 case kMenuEntry:
82 case kMenuPopup:
83 frame->BindKey(w, GetKeyCode(el), kKeyMod1Mask);
84 if (el->GetType()==kMenuPopup)
85 static_cast<MGPopupMenu*>(MGMenuEntry(el).GetPopup())->BindKeys(w, frame);
86 continue;
87 }
88 }
89}
90
91/*
92 kMenuActiveMask = BIT(0),
93 kMenuEnableMask = BIT(1),
94 kMenuDefaultMask = BIT(2),
95 kMenuCheckedMask = BIT(3),
96 kMenuRadioMask = BIT(4),
97 kMenuHideMask = BIT(5)
98 */
99
100// -------------------------------------------------------------
101//
102// Handle a keyboard event. Return kFALSE in case of a
103// successfully send message, which means: close all open
104// popups.
105//
106Bool_t MGPopupMenu::HandleKey(Event_t *evt)
107{
108 //
109 // Loop through all entries in this popup menu. If the entry is
110 // an open popup menu send the key event to the open popup.
111 //
112 TIter Next(fEntryList);
113 TGMenuEntry *el;
114 while ((el=(TGMenuEntry*)Next()))
115 {
116 if (el->GetType()==kMenuPopup && el->GetStatus()&kMenuActiveMask)
117 return MGMenuEntry(el).GetPopup()->HandleKey(evt);
118 }
119
120 Next.Reset();
121
122 //
123 // If no open popup is found search the pressed key in this
124 // popup menu.
125 //
126 while ((el=(TGMenuEntry*)Next()))
127 {
128 switch (el->GetType())
129 {
130 //
131 // Do nothing
132 //
133 case kMenuSeparator:
134 case kMenuLabel:
135 continue;
136
137 //
138 // If the keycode corresponds to the hot key
139 // of this entry and the entry is enabled activate the entry
140 // and send the corresponding message/
141 //
142 case kMenuEntry:
143 if (GetKeyCode(el)==evt->fCode && el->GetStatus()&kMenuEnableMask)
144 {
145 Activate(el);
146 SendMessage(fMsgWindow, MK_MSG(kC_COMMAND, kCM_MENU),
147 el->GetEntryId(), (Long_t)MGMenuEntry(el).GetUserData());
148 return kFALSE;
149 }
150 continue;
151
152 //
153 // If the keycode corresponds to the hot key
154 // of this popup menu activate the popup menu.
155 //
156 case kMenuPopup:
157 if (GetKeyCode(el)!=evt->fCode)
158 continue;
159
160 Activate(el);
161 HandleTimer(NULL);
162 return kTRUE;
163 }
164 }
165 return kTRUE;
166}
167
168// -------------------------------------------------------------
169//
170// Bind the keys of all popups subsequently to the given main
171// frame. The menu bar hot keys are already bound by TGMenuBar.
172// Bind the Escape key to close the popups, too.
173//
174void MGMenuBar::BindKeys(TGMainFrame *frame)
175{
176 TGFrameElement *el=NULL;
177 TIter Next(fList);
178 while ((el = (TGFrameElement *)Next()))
179 ((MGPopupMenu*)((TGMenuTitle *) el->fFrame)->GetMenu())->BindKeys(this, frame);
180
181 frame->BindKey(this, 9/*ESC*/, 0);
182}
183
184// -------------------------------------------------------------
185//
186// Handle the keyboard event send to this menu bar.
187//
188Bool_t MGMenuBar::HandleKey(Event_t *event)
189{
190#if ROOT_VERSION_CODE < ROOT_VERSION(4,02,00)
191 //
192 // If this isn't a pressed key do nothing
193 //
194 if (event->fType != kGKeyPress)
195 return kTRUE;
196
197 //
198 // Check whether one popup is alreadu open
199 //
200 TGFrameElement *el=NULL;
201 TIter Next(fList);
202 while ((el = (TGFrameElement *)Next()))
203 {
204 if (!((TGMenuTitle*)el->fFrame)->GetState())
205 continue;
206
207 TGMenuTitle &f = *(TGMenuTitle*)el->fFrame;
208
209 //
210 // If a open popup is found redirect the key event to this
211 // popup menu
212 //
213 Bool_t rc = ((MGPopupMenu*)f.GetMenu())->HandleKey(event);
214
215 //
216 // If a message could be successfully send or the escape key
217 // was pressed close the popup.
218 //
219 if (rc && event->fCode!=9/*ESC*/)
220 return kTRUE;
221
222 f.SetState(kFALSE);
223 gVirtualX->GrabPointer(0, 0, 0, 0, kFALSE); // ungrab pointer
224 gVirtualX->SetKeyAutoRepeat(kTRUE); // set in TGMainFrame::HandleKey
225 fCurrent = 0;
226 return kTRUE;
227 }
228#endif
229
230 return TGMenuBar::HandleKey(event);
231}
232
233void MGMenuBar::BindKeys(Bool_t b)
234{
235#if ROOT_VERSION_CODE >= ROOT_VERSION(4,02,00)
236 TGMenuBar::BindKeys(b);
237#endif
238} // root>=4.02.00
Note: See TracBrowser for help on using the repository browser.