Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/TGButton.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // @(#)root/gui:$Id$
0002 // Author: Fons Rademakers   06/01/98
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TGButton
0013 #define ROOT_TGButton
0014 
0015 
0016 //////////////////////////////////////////////////////////////////////////
0017 //                                                                      //
0018 // TGButton, TGTextButton, TGPictureButton, TGCheckButton TGRadioButton //
0019 // and TGSplitButton                                                    //
0020 //                                                                      //
0021 // This header defines all GUI button widgets.                          //
0022 //                                                                      //
0023 // TGButton is a button abstract base class. It defines general button  //
0024 // behaviour.                                                           //
0025 //                                                                      //
0026 // Selecting a text or picture button will generate the event:          //
0027 // kC_COMMAND, kCM_BUTTON, button id, user data.                        //
0028 //                                                                      //
0029 // Selecting a check button will generate the event:                    //
0030 // kC_COMMAND, kCM_CHECKBUTTON, button id, user data.                   //
0031 //                                                                      //
0032 // Selecting a radio button will generate the event:                    //
0033 // kC_COMMAND, kCM_RADIOBUTTON, button id, user data.                   //
0034 //                                                                      //
0035 // If a command string has been specified (via SetCommand()) then this  //
0036 // command string will be executed via the interpreter whenever a       //
0037 // button is selected. A command string can contain the macros:         //
0038 // $MSG   -- kC_COMMAND, kCM[CHECK|RADIO]BUTTON packed message          //
0039 //           (use GET_MSG() and GET_SUBMSG() to unpack)                 //
0040 // $PARM1 -- button id                                                  //
0041 // $PARM2 -- user data pointer                                          //
0042 // Before executing these macros are expanded into the respective       //
0043 // Long_t's                                                             //
0044 //                                                                      //
0045 //////////////////////////////////////////////////////////////////////////
0046 
0047 #include "TGFrame.h"
0048 #include "TGWidget.h"
0049 
0050 //--- Button states
0051 
0052 enum EButtonState {
0053    kButtonUp,
0054    kButtonDown,
0055    kButtonEngaged,
0056    kButtonDisabled
0057 };
0058 
0059 
0060 class TGWidget;
0061 class TGHotString;
0062 class TGPicture;
0063 class TGToolTip;
0064 class TGButtonGroup;
0065 class TGPopupMenu;
0066 class TGTextLayout;
0067 
0068 class TGButton : public TGFrame, public TGWidget {
0069 
0070 friend class TGButtonGroup;
0071 
0072 protected:
0073    UInt_t         fTWidth;      ///< button width
0074    UInt_t         fTHeight;     ///< button height
0075    EButtonState   fState;       ///< button state
0076    Bool_t         fStayDown;    ///< true if button has to stay down
0077    GContext_t     fNormGC;      ///< graphics context used for drawing button
0078    void          *fUserData;    ///< pointer to user data structure
0079    TGToolTip     *fTip;         ///< tool tip associated with button
0080    TGButtonGroup *fGroup;       ///< button group this button belongs to
0081    Pixel_t        fBgndColor;   ///< actual background color
0082    Pixel_t        fHighColor;   ///< highlight color
0083    UInt_t         fStyle;       ///< button style (modern or classic)
0084 
0085    virtual void   SetToggleButton(Bool_t) {}
0086    virtual void   EmitSignals(Bool_t wasUp);
0087 
0088    static const TGGC *fgDefaultGC;
0089    static const TGGC *fgHibckgndGC;
0090 
0091    static Window_t fgReleaseBtn; // the last released button
0092 
0093 private:
0094    TGButton(const TGButton&) = delete;
0095    TGButton& operator=(const TGButton&) = delete;
0096 
0097 public:
0098    static const TGGC   &GetDefaultGC();
0099    static const TGGC   &GetHibckgndGC();
0100 
0101    TGButton(const TGWindow *p = nullptr, Int_t id = -1, GContext_t norm = GetDefaultGC()(),
0102             UInt_t option = kRaisedFrame | kDoubleBorder);
0103    ~TGButton() override;
0104 
0105    Bool_t               HandleButton(Event_t *event) override;
0106    Bool_t               HandleCrossing(Event_t *event) override;
0107    virtual void         SetUserData(void *userData) { fUserData = userData; }
0108    virtual void        *GetUserData() const { return fUserData; }
0109    virtual void         SetToolTipText(const char *text, Long_t delayms = 400);  //*MENU*
0110    virtual TGToolTip   *GetToolTip() const { return fTip; }
0111    virtual void         SetState(EButtonState state, Bool_t emit = kFALSE);
0112    virtual EButtonState GetState() const { return fState; }
0113    virtual void         AllowStayDown(Bool_t a) { fStayDown = a; }
0114    virtual void         SetGroup(TGButtonGroup *gr);
0115    TGButtonGroup       *GetGroup() const { return fGroup; }
0116 
0117    virtual Bool_t       IsDown() const;// { return !(fOptions & kRaisedFrame); }
0118    virtual void         SetDown(Bool_t on = kTRUE, Bool_t emit = kFALSE);
0119    virtual Bool_t       IsOn() const { return IsDown(); }
0120    virtual void         SetOn(Bool_t on = kTRUE,  Bool_t emit = kFALSE) { SetDown(on, emit); }
0121    virtual Bool_t       IsToggleButton() const { return kFALSE; }
0122    virtual Bool_t       IsExclusiveToggle() const { return kFALSE; }
0123    virtual void         Toggle(Bool_t emit = kFALSE) { SetDown(IsDown() ? kFALSE : kTRUE, emit); }
0124    virtual void         SetEnabled(Bool_t e = kTRUE); //*TOGGLE* *GETTER=IsEnabled
0125    virtual UInt_t       GetStyle() const { return fStyle; }
0126    virtual void         SetStyle(UInt_t newstyle);
0127    virtual void         SetStyle(const char *style);
0128 
0129    void                 SavePrimitive(std::ostream &out, Option_t *option = "") override;
0130 
0131    GContext_t           GetNormGC() const { return fNormGC; }
0132 
0133    virtual void Pressed()  { Emit("Pressed()"); }   // *SIGNAL*
0134    virtual void Released() { Emit("Released()"); }  // *SIGNAL*
0135    virtual void Clicked()  { Emit("Clicked()"); }   // *SIGNAL*
0136    virtual void Toggled(Bool_t on) { Emit("Toggled(Bool_t)", on); }  // *SIGNAL*
0137 
0138    ClassDefOverride(TGButton,0)  // Button widget abstract base class
0139 };
0140 
0141 
0142 class TGTextButton : public TGButton {
0143 
0144 protected:
0145    TGHotString   *fLabel;         // button text
0146    Int_t          fMLeft;         // margin left
0147    Int_t          fMRight;        // margin right
0148    Int_t          fMTop;          // margin top
0149    Int_t          fMBottom;       // margin bottom
0150    Int_t          fTMode;         // text justify mode
0151    Int_t          fWrapLength;    // wrap length
0152    Int_t          fHKeycode;      // hotkey
0153    TGTextLayout  *fTLayout;       // text layout
0154    FontStruct_t   fFontStruct;    // font to draw text
0155    Bool_t         fHasOwnFont;    // kTRUE - font defined locally,  kFALSE - globally
0156    Bool_t         fStateOn;       // bit to save the state across disable/enable
0157    Bool_t         fPrevStateOn;   // bit to save previous state On/Off
0158 
0159    static const TGFont *fgDefaultFont;
0160 
0161    void Init();
0162    void DoRedraw() override;
0163 
0164 private:
0165    TGTextButton(const TGTextButton&) = delete;
0166    TGTextButton& operator=(const TGTextButton&) = delete;
0167 
0168 public:
0169    static FontStruct_t GetDefaultFontStruct();
0170 
0171    TGTextButton(const TGWindow *p, TGHotString *s, Int_t id = -1,
0172                 GContext_t norm = GetDefaultGC()(),
0173                 FontStruct_t font = GetDefaultFontStruct(),
0174                 UInt_t option = kRaisedFrame | kDoubleBorder);
0175    TGTextButton(const TGWindow *p = nullptr, const char *s = nullptr, Int_t id = -1,
0176                 GContext_t norm = GetDefaultGC()(),
0177                 FontStruct_t font = GetDefaultFontStruct(),
0178                 UInt_t option = kRaisedFrame | kDoubleBorder);
0179    TGTextButton(const TGWindow *p, const char *s, const char *cmd,
0180                 Int_t id = -1, GContext_t norm = GetDefaultGC()(),
0181                 FontStruct_t font = GetDefaultFontStruct(),
0182                 UInt_t option = kRaisedFrame | kDoubleBorder);
0183 
0184    ~TGTextButton() override;
0185 
0186    TGDimension        GetDefaultSize() const override;
0187 
0188    Bool_t             HandleKey(Event_t *event) override;
0189    const TGHotString *GetText() const { return fLabel; }
0190    const char        *GetTitle() const override { return fLabel->Data(); }
0191    TString            GetString() const { return TString(fLabel->GetString()); }
0192    virtual void       SetTextJustify(Int_t tmode);
0193    Int_t GetTextJustify() const { return fTMode; }
0194    virtual void       SetText(TGHotString *new_label);
0195    virtual void       SetText(const TString &new_label);
0196    virtual void       SetTitle(const char *label) { SetText(label); }
0197    virtual void       SetFont(FontStruct_t font, Bool_t global = kFALSE);
0198    virtual void       SetFont(const char *fontName, Bool_t global = kFALSE);
0199    virtual void       SetTextColor(Pixel_t color, Bool_t global = kFALSE);
0200    void               SetForegroundColor(Pixel_t fore) override { SetTextColor(fore); }
0201    Bool_t             HasOwnFont() const;
0202    void               SetWrapLength(Int_t wl) { fWrapLength = wl; Layout(); }
0203    Int_t              GetWrapLength() const { return fWrapLength; }
0204    void               SetMargins(Int_t left=0, Int_t right=0, Int_t top=0, Int_t bottom=0)
0205                         { fMLeft = left; fMRight = right; fMTop = top; fMBottom = bottom; }
0206 
0207    virtual void       SetLeftMargin(Int_t val)   { fMLeft = val; }
0208    virtual void       SetRightMargin(Int_t val)  { fMRight = val; }
0209    virtual void       SetTopMargin(Int_t val)    { fMTop = val; }
0210    virtual void       SetBottomMargin(Int_t val) { fMBottom = val; }
0211 
0212    Int_t              GetLeftMargin() const { return fMLeft; }
0213    Int_t              GetRightMargin() const { return fMRight; }
0214    Int_t              GetTopMargin() const { return fMTop; }
0215    Int_t              GetBottomMargin() const { return fMBottom; }
0216 
0217    void               ChangeText(const char *title)  { SetTitle(title); } //*MENU*icon=bld_rename.png*
0218 
0219    FontStruct_t       GetFontStruct() const { return fFontStruct; }
0220 
0221    void               Layout() override;
0222    void               SavePrimitive(std::ostream &out, Option_t *option = "") override;
0223 
0224    ClassDefOverride(TGTextButton,0)  // A text button widget
0225 };
0226 
0227 
0228 class TGPictureButton : public TGButton {
0229 
0230 protected:
0231    const TGPicture   *fPic;            ///< picture to be put in button
0232    const TGPicture   *fPicD;           ///< picture shown when button disabled
0233    Bool_t             fOwnDisabledPic; ///< kTRUE if disabled picture was autogenerated
0234 
0235    void DoRedraw() override;
0236    virtual void CreateDisabledPicture();
0237 
0238 private:
0239    TGPictureButton(const TGPictureButton&) = delete;
0240    TGPictureButton& operator=(const TGPictureButton&) = delete;
0241 
0242 public:
0243    TGPictureButton(const TGWindow *p, const TGPicture *pic, Int_t id = -1,
0244                    GContext_t norm = GetDefaultGC()(),
0245                    UInt_t option = kRaisedFrame | kDoubleBorder);
0246    TGPictureButton(const TGWindow *p, const TGPicture *pic, const char *cmd,
0247                    Int_t id = -1, GContext_t norm = GetDefaultGC()(),
0248                    UInt_t option = kRaisedFrame | kDoubleBorder);
0249    TGPictureButton(const TGWindow *p = nullptr, const char* pic = nullptr, Int_t id = -1,
0250                    GContext_t norm = GetDefaultGC()(),
0251                    UInt_t option = kRaisedFrame | kDoubleBorder);
0252    ~TGPictureButton() override;
0253 
0254    virtual void     SetPicture(const TGPicture *new_pic);
0255    virtual void     SetDisabledPicture(const TGPicture *pic);
0256    const TGPicture *GetPicture() const { return fPic; };
0257    const TGPicture *GetDisabledPicture() const { return fPicD; };
0258    void             SavePrimitive(std::ostream &out, Option_t *option = "") override;
0259 
0260    ClassDefOverride(TGPictureButton,0)  // A picture button widget
0261 };
0262 
0263 
0264 class TGCheckButton : public TGTextButton {
0265 
0266 private:
0267 
0268    TGCheckButton(const TGCheckButton&) = delete;
0269    TGCheckButton& operator=(const TGCheckButton&) = delete;
0270 
0271 protected:
0272    EButtonState       fPrevState;     ///< previous check button state
0273    const TGPicture   *fOn;            ///< button ON picture
0274    const TGPicture   *fOff;           ///< button OFF picture
0275    const TGPicture   *fDisOn;         ///< button disabled and was ON picture
0276    const TGPicture   *fDisOff;        ///< button disabled and was OFF picture
0277 
0278    void Init();
0279    void PSetState(EButtonState state, Bool_t emit);
0280    void DoRedraw() override;
0281    void EmitSignals(Bool_t wasUp = kTRUE) override;
0282 
0283    static const TGFont *fgDefaultFont;
0284    static const TGGC   *fgDefaultGC;
0285 
0286 public:
0287    static FontStruct_t  GetDefaultFontStruct();
0288    static const TGGC   &GetDefaultGC();
0289 
0290    TGCheckButton(const TGWindow *p, TGHotString *s, Int_t id = -1,
0291                  GContext_t norm = GetDefaultGC()(),
0292                  FontStruct_t font = GetDefaultFontStruct(),
0293                  UInt_t option = 0);
0294    TGCheckButton(const TGWindow *p = nullptr, const char *s = nullptr, Int_t id = -1,
0295                  GContext_t norm = GetDefaultGC()(),
0296                  FontStruct_t font = GetDefaultFontStruct(),
0297                  UInt_t option = 0);
0298    TGCheckButton(const TGWindow *p, const char *s, const char *cmd, Int_t id = -1,
0299                  GContext_t norm = GetDefaultGC()(),
0300                  FontStruct_t font = GetDefaultFontStruct(),
0301                  UInt_t option = 0);
0302    ~TGCheckButton() override;
0303 
0304    TGDimension    GetDefaultSize() const override;
0305 
0306    Bool_t         HandleButton(Event_t *event) override;
0307    Bool_t         HandleKey(Event_t *event) override;
0308    Bool_t         HandleCrossing(Event_t *event) override;
0309    Bool_t         IsToggleButton() const override { return kTRUE; }
0310    Bool_t         IsOn() const override { return fState == kButtonDown; }
0311    Bool_t         IsDown() const override { return fState == kButtonDown; }
0312    virtual Bool_t IsDisabledAndSelected() const { return ((fState == kButtonDisabled) && fStateOn); }
0313    virtual void   SetDisabledAndSelected(Bool_t);
0314    void           SetState(EButtonState state, Bool_t emit = kFALSE) override;
0315    void           SavePrimitive(std::ostream &out, Option_t *option = "") override;
0316 
0317    ClassDefOverride(TGCheckButton,0)  // A check button widget
0318 };
0319 
0320 
0321 class TGRadioButton : public TGTextButton {
0322 
0323 private:
0324    TGRadioButton(const TGRadioButton&) = delete;
0325    TGRadioButton& operator=(const TGRadioButton&) = delete;
0326 
0327 protected:
0328    EButtonState       fPrevState;   ///< previous radio button state
0329    const TGPicture   *fOn;          ///< button ON picture
0330    const TGPicture   *fOff;         ///< button OFF picture
0331    const TGPicture   *fDisOn;       ///< button disabled and was ON picture
0332    const TGPicture   *fDisOff;      ///< button disabled and was OFF picture
0333 
0334    void Init();
0335    void PSetState(EButtonState state, Bool_t emit);
0336    void DoRedraw() override;
0337    void EmitSignals(Bool_t wasUp = kTRUE) override;
0338 
0339    static const TGFont *fgDefaultFont;
0340    static const TGGC   *fgDefaultGC;
0341 
0342 public:
0343    static FontStruct_t  GetDefaultFontStruct();
0344    static const TGGC   &GetDefaultGC();
0345 
0346    TGRadioButton(const TGWindow *p, TGHotString *s, Int_t id = -1,
0347                  GContext_t norm = GetDefaultGC()(),
0348                  FontStruct_t font = GetDefaultFontStruct(),
0349                  UInt_t option = 0);
0350    TGRadioButton(const TGWindow *p = nullptr, const char *s = nullptr, Int_t id = -1,
0351                  GContext_t norm = GetDefaultGC()(),
0352                  FontStruct_t font = GetDefaultFontStruct(),
0353                  UInt_t option = 0);
0354    TGRadioButton(const TGWindow *p, const char *s, const char *cmd, Int_t id = -1,
0355                  GContext_t norm = GetDefaultGC()(),
0356                  FontStruct_t font = GetDefaultFontStruct(),
0357                  UInt_t option = 0);
0358    ~TGRadioButton() override;
0359 
0360    TGDimension    GetDefaultSize() const override;
0361 
0362    Bool_t         HandleButton(Event_t *event) override;
0363    Bool_t         HandleKey(Event_t *event) override;
0364    Bool_t         HandleCrossing(Event_t *event) override;
0365    void           SetState(EButtonState state, Bool_t emit = kFALSE) override;
0366    virtual void   SetDisabledAndSelected(Bool_t);
0367    Bool_t         IsToggleButton() const override { return kTRUE; }
0368    Bool_t         IsExclusiveToggle() const override { return kTRUE; }
0369    Bool_t         IsOn() const override { return fStateOn; }
0370    Bool_t         IsDown() const override { return fStateOn; }
0371    virtual Bool_t IsDisabledAndSelected() const { return ((fState == kButtonDisabled) && fStateOn); }
0372    void           SavePrimitive(std::ostream &out, Option_t *option = "") override;
0373 
0374    ClassDefOverride(TGRadioButton,0)  // A radio button widget
0375 };
0376 
0377 
0378 class TGSplitButton : public TGTextButton {
0379 
0380 friend class TGPopupMenu;
0381 
0382 private:
0383    TGSplitButton(const TGSplitButton&) = delete;
0384    TGSplitButton& operator=(const TGSplitButton&) = delete;
0385 
0386    void CalcSize();
0387    void DrawTriangle(const GContext_t gc, Int_t x, Int_t y);
0388    Bool_t HandleSButton(Event_t *event);
0389    Bool_t HandleSCrossing(Event_t *event);
0390    Bool_t HandleSKey(Event_t *event);
0391    void SetMenuState(Bool_t state) ;
0392 
0393 protected:
0394 ///@{
0395 /// @name Data members for menu functionality
0396    Bool_t       fSplit;         ///< kTRUE if menu is in split mode
0397    EButtonState fMBState;       ///< state for menu button in split mode
0398    UInt_t       fTBWidth;       ///< width of text button
0399    UInt_t       fMBWidth;       ///< width of menu button
0400    TGPopupMenu *fPopMenu;       ///< pointer to popup menu
0401    Int_t        fEntryId;       ///< Id of the currently active menu entry
0402    TGHotString *fMenuLabel;     ///< Label of the menu;
0403    Cursor_t     fDefaultCursor; ///< Default Cursor
0404    Bool_t       fKeyNavigate;   ///< kTRUE if key navigation is being used
0405    TGString     fWidestLabel;   ///< longest label that can be on the button
0406    TGString     fHeighestLabel; ///< highest label that can be on the button
0407 ///@]
0408 
0409    void DoRedraw() override;
0410    void Init();
0411    void BindKeys(Bool_t on = kTRUE);
0412    void BindMenuKeys(Bool_t on = kTRUE);
0413 
0414 public:
0415    TGSplitButton(const TGWindow *p, TGHotString *menulabel,
0416                 TGPopupMenu *popmenu, Bool_t split = kTRUE,
0417                 Int_t id = -1, GContext_t norm = GetDefaultGC()(),
0418                 FontStruct_t fontstruct = GetDefaultFontStruct(),
0419                 UInt_t option = kRaisedFrame | kDoubleBorder);
0420 
0421    ~TGSplitButton() override;
0422 
0423    TGDimension  GetDefaultSize() const override;
0424 
0425    void         SetText(TGHotString *new_label) override;
0426    void         SetText(const TString &new_label) override;
0427    void         SetFont(FontStruct_t font, Bool_t global = kFALSE) override;
0428    void         SetFont(const char *fontName, Bool_t global = kFALSE) override;
0429    virtual void SetMBState(EButtonState state);
0430    virtual void SetSplit(Bool_t split);
0431    Bool_t       IsSplit() { return fSplit; }
0432    Bool_t       HandleButton(Event_t *event) override;
0433    Bool_t       HandleCrossing(Event_t *event) override;
0434    Bool_t       HandleKey(Event_t *event) override;
0435    Bool_t       HandleMotion(Event_t *event) override;
0436    void         Layout() override;
0437 
0438    virtual void MBPressed()  { Emit("MBPressed()"); }   // *SIGNAL*
0439    virtual void MBReleased() { Emit("MBReleased()"); }  // *SIGNAL*
0440    virtual void MBClicked()  { Emit("MBClicked()"); }   // *SIGNAL*
0441    virtual void ItemClicked(Int_t id) { Emit("ItemClicked(Int_t)", id); } // *SIGNAL*
0442 
0443    // Slots
0444    void HandleMenu(Int_t id);
0445 
0446    ClassDefOverride(TGSplitButton,0) //a split button widget
0447 };
0448 
0449 #endif