FWPopupMenu

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include "TGMenu.h"
#include "KeySymbols.h"
#include "TVirtualX.h"

class FWPopupMenu : public TGPopupMenu {
public:
  FWPopupMenu(const TGWindow* p = nullptr, UInt_t w = 10, UInt_t h = 10, UInt_t options = 0)
      : TGPopupMenu(p, w, h, options) {
    AddInput(kKeyPressMask);
  }

  // virtual void	PlaceMenu(Int_t x, Int_t y, Bool_t stick_mode, Bool_t grab_pointer)
  // {
  //    TGPopupMenu::PlaceMenu(x, y, stick_mode, grab_pointer);
  //    gVirtualX->GrabKey(fId, 0l, kAnyModifier, kTRUE);
  // }

  void PoppedUp() override {
    TGPopupMenu::PoppedUp();
    gVirtualX->SetInputFocus(fId);
    gVirtualX->GrabKey(fId, 0l, kAnyModifier, kTRUE);
  }

  void PoppedDown() override {
    gVirtualX->GrabKey(fId, 0l, kAnyModifier, kFALSE);
    TGPopupMenu::PoppedDown();
  }

  Bool_t HandleKey(Event_t* event) override {
    if (event->fType != kGKeyPress)
      return kTRUE;

    UInt_t keysym;
    char tmp[2];
    gVirtualX->LookupString(event, tmp, sizeof(tmp), keysym);

    TGMenuEntry* ce = fCurrent;

    switch (keysym) {
      case kKey_Up: {
        if (ce)
          ce = (TGMenuEntry*)GetListOfEntries()->Before(ce);
        while (ce && ((ce->GetType() == kMenuSeparator) || (ce->GetType() == kMenuLabel) ||
                      !(ce->GetStatus() & kMenuEnableMask))) {
          ce = (TGMenuEntry*)GetListOfEntries()->Before(ce);
        }
        if (!ce)
          ce = (TGMenuEntry*)GetListOfEntries()->Last();
        Activate(ce);
        break;
      }
      case kKey_Down: {
        if (ce)
          ce = (TGMenuEntry*)GetListOfEntries()->After(ce);
        while (ce && ((ce->GetType() == kMenuSeparator) || (ce->GetType() == kMenuLabel) ||
                      !(ce->GetStatus() & kMenuEnableMask))) {
          ce = (TGMenuEntry*)GetListOfEntries()->After(ce);
        }
        if (!ce)
          ce = (TGMenuEntry*)GetListOfEntries()->First();
        Activate(ce);
        break;
      }
      case kKey_Enter:
      case kKey_Return: {
        Event_t ev;
        ev.fType = kButtonRelease;
        ev.fWindow = fId;
        return HandleButton(&ev);
      }
      case kKey_Escape: {
        fCurrent = nullptr;
        void* dummy = nullptr;
        return EndMenu(dummy);
      }
      default: {
        break;
      }
    }

    return kTRUE;
  }
};