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