| 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 | #if ROOT_VERSION_CODE < ROOT_VERSION(6,00,00)
|
|---|
| 33 | MGMenuEntry::MGMenuEntry(TGMenuEntry *ent)
|
|---|
| 34 | {
|
|---|
| 35 | memcpy(this, ent, sizeof(*ent));
|
|---|
| 36 | }
|
|---|
| 37 | #endif
|
|---|
| 38 |
|
|---|
| 39 | // -------------------------------------------------------------
|
|---|
| 40 | //
|
|---|
| 41 | // Return the keycode corresponding to the hot string of
|
|---|
| 42 | // a TGmenuEntry
|
|---|
| 43 | //
|
|---|
| 44 | UInt_t MGPopupMenu::GetKeyCode(TGMenuEntry *el)
|
|---|
| 45 | {
|
|---|
| 46 | TGHotString *str = MGMenuEntry(el).GetLabel();
|
|---|
| 47 | return gVirtualX->KeysymToKeycode(str->GetHotChar());
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | // -------------------------------------------------------------
|
|---|
| 51 | //
|
|---|
| 52 | // Bind all hot keys used in this popup menu subsequentlt
|
|---|
| 53 | // to the TGMainFrame and send a HandleKey message to the
|
|---|
| 54 | // TGWindow
|
|---|
| 55 | //
|
|---|
| 56 | void MGPopupMenu::BindKeys(const TGWindow *w, TGMainFrame *frame)
|
|---|
| 57 | {
|
|---|
| 58 | TIter Next(fEntryList);
|
|---|
| 59 | TGMenuEntry *el;
|
|---|
| 60 |
|
|---|
| 61 | //
|
|---|
| 62 | // Loop Through all entries
|
|---|
| 63 | //
|
|---|
| 64 | while ((el=(TGMenuEntry*)Next()))
|
|---|
| 65 | {
|
|---|
| 66 | switch (el->GetType())
|
|---|
| 67 | {
|
|---|
| 68 | //
|
|---|
| 69 | // For seperators and labels nothing to do
|
|---|
| 70 | //
|
|---|
| 71 | case kMenuSeparator:
|
|---|
| 72 | case kMenuLabel:
|
|---|
| 73 | continue;
|
|---|
| 74 |
|
|---|
| 75 | //
|
|---|
| 76 | // For an entry and a popup menu bind the hot key
|
|---|
| 77 | // In case of a popup menu call BindKeys subsequently
|
|---|
| 78 | //
|
|---|
| 79 | case kMenuEntry:
|
|---|
| 80 | case kMenuPopup:
|
|---|
| 81 | frame->BindKey(w, GetKeyCode(el), kKeyMod1Mask);
|
|---|
| 82 | if (el->GetType()==kMenuPopup)
|
|---|
| 83 | static_cast<MGPopupMenu*>(MGMenuEntry(el).GetPopup())->BindKeys(w, frame);
|
|---|
| 84 | continue;
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /*
|
|---|
| 90 | kMenuActiveMask = BIT(0),
|
|---|
| 91 | kMenuEnableMask = BIT(1),
|
|---|
| 92 | kMenuDefaultMask = BIT(2),
|
|---|
| 93 | kMenuCheckedMask = BIT(3),
|
|---|
| 94 | kMenuRadioMask = BIT(4),
|
|---|
| 95 | kMenuHideMask = BIT(5)
|
|---|
| 96 | */
|
|---|
| 97 |
|
|---|
| 98 | // -------------------------------------------------------------
|
|---|
| 99 | //
|
|---|
| 100 | // Handle a keyboard event. Return kFALSE in case of a
|
|---|
| 101 | // successfully send message, which means: close all open
|
|---|
| 102 | // popups.
|
|---|
| 103 | //
|
|---|
| 104 | Bool_t MGPopupMenu::HandleKey(Event_t *evt)
|
|---|
| 105 | {
|
|---|
| 106 | //
|
|---|
| 107 | // Loop through all entries in this popup menu. If the entry is
|
|---|
| 108 | // an open popup menu send the key event to the open popup.
|
|---|
| 109 | //
|
|---|
| 110 | TIter Next(fEntryList);
|
|---|
| 111 | TGMenuEntry *el;
|
|---|
| 112 | while ((el=(TGMenuEntry*)Next()))
|
|---|
| 113 | {
|
|---|
| 114 | if (el->GetType()==kMenuPopup && el->GetStatus()&kMenuActiveMask)
|
|---|
| 115 | return MGMenuEntry(el).GetPopup()->HandleKey(evt);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | Next.Reset();
|
|---|
| 119 |
|
|---|
| 120 | //
|
|---|
| 121 | // If no open popup is found search the pressed key in this
|
|---|
| 122 | // popup menu.
|
|---|
| 123 | //
|
|---|
| 124 | while ((el=(TGMenuEntry*)Next()))
|
|---|
| 125 | {
|
|---|
| 126 | switch (el->GetType())
|
|---|
| 127 | {
|
|---|
| 128 | //
|
|---|
| 129 | // Do nothing
|
|---|
| 130 | //
|
|---|
| 131 | case kMenuSeparator:
|
|---|
| 132 | case kMenuLabel:
|
|---|
| 133 | continue;
|
|---|
| 134 |
|
|---|
| 135 | //
|
|---|
| 136 | // If the keycode corresponds to the hot key
|
|---|
| 137 | // of this entry and the entry is enabled activate the entry
|
|---|
| 138 | // and send the corresponding message/
|
|---|
| 139 | //
|
|---|
| 140 | case kMenuEntry:
|
|---|
| 141 | if (GetKeyCode(el)==evt->fCode && el->GetStatus()&kMenuEnableMask)
|
|---|
| 142 | {
|
|---|
| 143 | Activate(el);
|
|---|
| 144 | SendMessage(fMsgWindow, MK_MSG(kC_COMMAND, kCM_MENU),
|
|---|
| 145 | el->GetEntryId(), (Long_t)MGMenuEntry(el).GetUserData());
|
|---|
| 146 | return kFALSE;
|
|---|
| 147 | }
|
|---|
| 148 | continue;
|
|---|
| 149 |
|
|---|
| 150 | //
|
|---|
| 151 | // If the keycode corresponds to the hot key
|
|---|
| 152 | // of this popup menu activate the popup menu.
|
|---|
| 153 | //
|
|---|
| 154 | case kMenuPopup:
|
|---|
| 155 | if (GetKeyCode(el)!=evt->fCode)
|
|---|
| 156 | continue;
|
|---|
| 157 |
|
|---|
| 158 | Activate(el);
|
|---|
| 159 | HandleTimer(NULL);
|
|---|
| 160 | return kTRUE;
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 | return kTRUE;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | // -------------------------------------------------------------
|
|---|
| 167 | //
|
|---|
| 168 | // Bind the keys of all popups subsequently to the given main
|
|---|
| 169 | // frame. The menu bar hot keys are already bound by TGMenuBar.
|
|---|
| 170 | // Bind the Escape key to close the popups, too.
|
|---|
| 171 | //
|
|---|
| 172 | void MGMenuBar::BindKeys(TGMainFrame *frame)
|
|---|
| 173 | {
|
|---|
| 174 | TGFrameElement *el=NULL;
|
|---|
| 175 | TIter Next(fList);
|
|---|
| 176 | while ((el = (TGFrameElement *)Next()))
|
|---|
| 177 | ((MGPopupMenu*)((TGMenuTitle *) el->fFrame)->GetMenu())->BindKeys(this, frame);
|
|---|
| 178 |
|
|---|
| 179 | frame->BindKey(this, 9/*ESC*/, 0);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | // -------------------------------------------------------------
|
|---|
| 183 | //
|
|---|
| 184 | // Handle the keyboard event send to this menu bar.
|
|---|
| 185 | //
|
|---|
| 186 | Bool_t MGMenuBar::HandleKey(Event_t *event)
|
|---|
| 187 | {
|
|---|
| 188 | #if ROOT_VERSION_CODE < ROOT_VERSION(4,02,00)
|
|---|
| 189 | //
|
|---|
| 190 | // If this isn't a pressed key do nothing
|
|---|
| 191 | //
|
|---|
| 192 | if (event->fType != kGKeyPress)
|
|---|
| 193 | return kTRUE;
|
|---|
| 194 |
|
|---|
| 195 | //
|
|---|
| 196 | // Check whether one popup is alreadu open
|
|---|
| 197 | //
|
|---|
| 198 | TGFrameElement *el=NULL;
|
|---|
| 199 | TIter Next(fList);
|
|---|
| 200 | while ((el = (TGFrameElement *)Next()))
|
|---|
| 201 | {
|
|---|
| 202 | if (!((TGMenuTitle*)el->fFrame)->GetState())
|
|---|
| 203 | continue;
|
|---|
| 204 |
|
|---|
| 205 | TGMenuTitle &f = *(TGMenuTitle*)el->fFrame;
|
|---|
| 206 |
|
|---|
| 207 | //
|
|---|
| 208 | // If a open popup is found redirect the key event to this
|
|---|
| 209 | // popup menu
|
|---|
| 210 | //
|
|---|
| 211 | Bool_t rc = ((MGPopupMenu*)f.GetMenu())->HandleKey(event);
|
|---|
| 212 |
|
|---|
| 213 | //
|
|---|
| 214 | // If a message could be successfully send or the escape key
|
|---|
| 215 | // was pressed close the popup.
|
|---|
| 216 | //
|
|---|
| 217 | if (rc && event->fCode!=9/*ESC*/)
|
|---|
| 218 | return kTRUE;
|
|---|
| 219 |
|
|---|
| 220 | f.SetState(kFALSE);
|
|---|
| 221 | gVirtualX->GrabPointer(0, 0, 0, 0, kFALSE); // ungrab pointer
|
|---|
| 222 | gVirtualX->SetKeyAutoRepeat(kTRUE); // set in TGMainFrame::HandleKey
|
|---|
| 223 | fCurrent = 0;
|
|---|
| 224 | return kTRUE;
|
|---|
| 225 | }
|
|---|
| 226 | #endif
|
|---|
| 227 |
|
|---|
| 228 | return TGMenuBar::HandleKey(event);
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | void MGMenuBar::BindKeys(Bool_t b)
|
|---|
| 232 | {
|
|---|
| 233 | #if ROOT_VERSION_CODE >= ROOT_VERSION(4,02,00)
|
|---|
| 234 | TGMenuBar::BindKeys(b);
|
|---|
| 235 | #endif
|
|---|
| 236 | } // root>=4.02.00
|
|---|