TGButton.h

Go to the documentation of this file.
00001 // @(#)root/gui:$Id: TGButton.h 23115 2008-04-10 13:35:37Z rdm $
00002 // Author: Fons Rademakers   06/01/98
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOT_TGButton
00013 #define ROOT_TGButton
00014 
00015 
00016 //////////////////////////////////////////////////////////////////////////
00017 //                                                                      //
00018 // TGButton, TGTextButton, TGPictureButton, TGCheckButton and           //
00019 // TGRadioButton                                                        //
00020 //                                                                      //
00021 // This header defines all GUI button widgets.                          //
00022 //                                                                      //
00023 // TGButton is a button abstract base class. It defines general button  //
00024 // behaviour.                                                           //
00025 //                                                                      //
00026 // Selecting a text or picture button will generate the event:          //
00027 // kC_COMMAND, kCM_BUTTON, button id, user data.                        //
00028 //                                                                      //
00029 // Selecting a check button will generate the event:                    //
00030 // kC_COMMAND, kCM_CHECKBUTTON, button id, user data.                   //
00031 //                                                                      //
00032 // Selecting a radio button will generate the event:                    //
00033 // kC_COMMAND, kCM_RADIOBUTTON, button id, user data.                   //
00034 //                                                                      //
00035 // If a command string has been specified (via SetCommand()) then this  //
00036 // command string will be executed via the interpreter whenever a       //
00037 // button is selected. A command string can contain the macros:         //
00038 // $MSG   -- kC_COMMAND, kCM[CHECK|RADIO]BUTTON packed message          //
00039 //           (use GET_MSG() and GET_SUBMSG() to unpack)                 //
00040 // $PARM1 -- button id                                                  //
00041 // $PARM2 -- user data pointer                                          //
00042 // Before executing these macros are expanded into the respective       //
00043 // Long_t's                                                             //
00044 //                                                                      //
00045 //////////////////////////////////////////////////////////////////////////
00046 
00047 #ifndef ROOT_TGFrame
00048 #include "TGFrame.h"
00049 #endif
00050 #ifndef ROOT_TGWidget
00051 #include "TGWidget.h"
00052 #endif
00053 
00054 //--- Button states
00055 
00056 enum EButtonState {
00057    kButtonUp,
00058    kButtonDown,
00059    kButtonEngaged,
00060    kButtonDisabled
00061 };
00062 
00063 
00064 class TGWidget;
00065 class TGHotString;
00066 class TGPicture;
00067 class TGToolTip;
00068 class TGButtonGroup;
00069 class TGPopupMenu;
00070 class TGTextLayout;
00071 
00072 class TGButton : public TGFrame, public TGWidget {
00073 
00074 friend class TGButtonGroup;
00075 
00076 protected:
00077    UInt_t         fTWidth;      // button width
00078    UInt_t         fTHeight;     // button height
00079    EButtonState   fState;       // button state
00080    Bool_t         fStayDown;    // true if button has to stay down
00081    GContext_t     fNormGC;      // graphics context used for drawing button
00082    void          *fUserData;    // pointer to user data structure
00083    TGToolTip     *fTip;         // tool tip associated with button
00084    TGButtonGroup *fGroup;       // button group this button belongs to
00085 
00086    virtual void   SetToggleButton(Bool_t) { }
00087    virtual void   EmitSignals(Bool_t wasUp);
00088 
00089    static const TGGC *fgDefaultGC;
00090    static const TGGC *fgHibckgndGC;
00091 
00092    static Window_t fgReleaseBtn; // the last released button
00093 
00094 private:
00095    TGButton(const TGButton&);             // not implemented
00096    TGButton& operator=(const TGButton&);  // not implemented
00097 
00098 public:
00099    static const TGGC   &GetDefaultGC();
00100    static const TGGC   &GetHibckgndGC();
00101 
00102    TGButton(const TGWindow *p = 0, Int_t id = -1, GContext_t norm = GetDefaultGC()(),
00103             UInt_t option = kRaisedFrame | kDoubleBorder);
00104    virtual ~TGButton();
00105 
00106    virtual Bool_t       HandleButton(Event_t *event);
00107    virtual Bool_t       HandleCrossing(Event_t *event);
00108    virtual void         SetUserData(void *userData) { fUserData = userData; }
00109    virtual void        *GetUserData() const { return fUserData; }
00110    virtual void         SetToolTipText(const char *text, Long_t delayms = 400);  //*MENU*
00111    virtual TGToolTip   *GetToolTip() const { return fTip; }
00112    virtual void         SetState(EButtonState state, Bool_t emit = kFALSE);
00113    virtual EButtonState GetState() const { return fState; }
00114    virtual void         AllowStayDown(Bool_t a) { fStayDown = a; }
00115    virtual void         SetGroup(TGButtonGroup *gr);
00116    TGButtonGroup       *GetGroup() const { return fGroup; }
00117 
00118    virtual Bool_t       IsDown() const { return !(fOptions & kRaisedFrame); }
00119    virtual void         SetDown(Bool_t on = kTRUE, Bool_t emit = kFALSE);
00120    virtual Bool_t       IsOn() const { return IsDown(); }
00121    virtual void         SetOn(Bool_t on = kTRUE,  Bool_t emit = kFALSE) { SetDown(on, emit); }
00122    virtual Bool_t       IsToggleButton() const { return kFALSE; }
00123    virtual Bool_t       IsExclusiveToggle() const { return kFALSE; }
00124    virtual void         Toggle(Bool_t emit = kFALSE) { SetDown(IsDown() ? kFALSE : kTRUE, emit); }
00125    virtual void         SetEnabled(Bool_t e = kTRUE); //*TOGGLE* *GETTER=IsEnabled
00126 
00127    virtual void         SavePrimitive(ostream &out, Option_t *option = "");
00128 
00129    GContext_t GetNormGC() const { return fNormGC; }
00130 
00131    virtual void Pressed()  { Emit("Pressed()"); }   // *SIGNAL*
00132    virtual void Released() { Emit("Released()"); }  // *SIGNAL*
00133    virtual void Clicked()  { Emit("Clicked()"); }   // *SIGNAL*
00134    virtual void Toggled(Bool_t on) { Emit("Toggled(Bool_t)", on); }  // *SIGNAL*
00135 
00136    ClassDef(TGButton,0)  // Button widget abstract base class
00137 };
00138 
00139 
00140 class TGTextButton : public TGButton {
00141 
00142 protected:
00143    TGHotString   *fLabel;         // button text
00144    Int_t          fMLeft;         // margin left
00145    Int_t          fMRight;        // margin right
00146    Int_t          fMTop;          // margin top
00147    Int_t          fMBottom;       // margin bottom
00148    Int_t          fTMode;         // text justify mode
00149    Int_t          fWrapLength;    // wrap length
00150    Int_t          fHKeycode;      // hotkey
00151    TGTextLayout  *fTLayout;       // text layout
00152    FontStruct_t   fFontStruct;    // font to draw text
00153    Bool_t         fHasOwnFont;    // kTRUE - font defined locally,  kFALSE - globally
00154    Bool_t         fStateOn;       // bit to save the state across disable/enable
00155    Bool_t         fPrevStateOn;   // bit to save previos state On/Off
00156 
00157    static const TGFont *fgDefaultFont;
00158 
00159    void Init();
00160    virtual void DoRedraw();
00161 
00162 private:
00163    TGTextButton(const TGTextButton&);              // not implemented
00164    TGTextButton& operator=(const TGTextButton&);   // not implemented
00165 
00166 public:
00167    static FontStruct_t GetDefaultFontStruct();
00168 
00169    TGTextButton(const TGWindow *p, TGHotString *s, Int_t id = -1,
00170                 GContext_t norm = GetDefaultGC()(),
00171                 FontStruct_t font = GetDefaultFontStruct(),
00172                 UInt_t option = kRaisedFrame | kDoubleBorder);
00173    TGTextButton(const TGWindow *p = 0, const char *s = 0, Int_t id = -1,
00174                 GContext_t norm = GetDefaultGC()(),
00175                 FontStruct_t font = GetDefaultFontStruct(),
00176                 UInt_t option = kRaisedFrame | kDoubleBorder);
00177    TGTextButton(const TGWindow *p, const char *s, const char *cmd,
00178                 Int_t id = -1, GContext_t norm = GetDefaultGC()(),
00179                 FontStruct_t font = GetDefaultFontStruct(),
00180                 UInt_t option = kRaisedFrame | kDoubleBorder);
00181 
00182    virtual ~TGTextButton();
00183 
00184    virtual TGDimension GetDefaultSize() const;
00185 
00186    virtual Bool_t     HandleKey(Event_t *event);
00187    const TGHotString *GetText() const { return fLabel; }
00188    virtual const char *GetTitle() const { return fLabel->Data(); }
00189    TString            GetString() const { return TString(fLabel->GetString()); }
00190    virtual void       SetTextJustify(Int_t tmode);
00191    Int_t GetTextJustify() const { return fTMode; }
00192    virtual void       SetText(TGHotString *new_label);
00193    virtual void       SetText(const TString &new_label);
00194    virtual void       SetTitle(const char *label) { SetText(label); }
00195    virtual void       SetFont(FontStruct_t font, Bool_t global = kFALSE);
00196    virtual void       SetFont(const char *fontName, Bool_t global = kFALSE);
00197    virtual void       SetTextColor(Pixel_t color, Bool_t global = kFALSE);
00198    virtual void       SetForegroundColor(Pixel_t fore) { SetTextColor(fore); }
00199    Bool_t             HasOwnFont() const;
00200    void               SetWrapLength(Int_t wl) { fWrapLength = wl; Layout(); }
00201    Int_t              GetWrapLength() const { return fWrapLength; }
00202    void               SetMargins(Int_t left=0, Int_t right=0, Int_t top=0, Int_t bottom=0)
00203                         { fMLeft = left; fMRight = right; fMTop = top; fMBottom = bottom; }
00204 
00205    virtual void       SetLeftMargin(Int_t val)   { fMLeft = val; }
00206    virtual void       SetRightMargin(Int_t val)  { fMRight = val; }
00207    virtual void       SetTopMargin(Int_t val)    { fMTop = val; }
00208    virtual void       SetBottomMargin(Int_t val) { fMBottom = val; }
00209 
00210    Int_t              GetLeftMargin() const { return fMLeft; }
00211    Int_t              GetRightMargin() const { return fMRight; }
00212    Int_t              GetTopMargin() const { return fMTop; }
00213    Int_t              GetBottomMargin() const { return fMBottom; }
00214 
00215    void               ChangeText(const char *title)  { SetTitle(title); } //*MENU*icon=bld_rename.png*
00216 
00217    FontStruct_t GetFontStruct() const { return fFontStruct; }
00218 
00219    virtual void       Layout();
00220    virtual void       SavePrimitive(ostream &out, Option_t *option = "");
00221 
00222    ClassDef(TGTextButton,0)  // A text button widget
00223 };
00224 
00225 
00226 class TGPictureButton : public TGButton {
00227 
00228 protected:
00229    const TGPicture   *fPic;            // picture to be put in button
00230    const TGPicture   *fPicD;           // picture shown when button disabled
00231    Bool_t             fOwnDisabledPic; // kTRUE if disabled picture was autogenerated
00232 
00233    virtual void DoRedraw();
00234    virtual void CreateDisabledPicture();
00235 
00236 private:
00237    TGPictureButton(const TGPictureButton&);             // not implemented
00238    TGPictureButton& operator=(const TGPictureButton&);  // not implemented
00239 
00240 public:
00241    TGPictureButton(const TGWindow *p, const TGPicture *pic, Int_t id = -1,
00242                    GContext_t norm = GetDefaultGC()(),
00243                    UInt_t option = kRaisedFrame | kDoubleBorder);
00244    TGPictureButton(const TGWindow *p, const TGPicture *pic, const char *cmd,
00245                    Int_t id = -1, GContext_t norm = GetDefaultGC()(),
00246                    UInt_t option = kRaisedFrame | kDoubleBorder);
00247    TGPictureButton(const TGWindow *p = 0, const char* pic = 0, Int_t id = -1,
00248                    GContext_t norm = GetDefaultGC()(),
00249                    UInt_t option = kRaisedFrame | kDoubleBorder);
00250    virtual ~TGPictureButton();
00251 
00252    virtual void     SetPicture(const TGPicture *new_pic);
00253    virtual void     SetDisabledPicture(const TGPicture *pic);
00254    const TGPicture *GetPicture() const { return fPic; };
00255    const TGPicture *GetDisabledPicture() const { return fPicD; };
00256    virtual void     SavePrimitive(ostream &out, Option_t *option = "");
00257 
00258    ClassDef(TGPictureButton,0)  // A picture button widget
00259 };
00260 
00261 
00262 class TGCheckButton : public TGTextButton {
00263 
00264 private:
00265  
00266    TGCheckButton(const TGCheckButton&); // Not implemented
00267    TGCheckButton& operator=(const TGCheckButton&); // Not implemented
00268 
00269 protected:
00270    EButtonState       fPrevState;     // previous check button state
00271    const TGPicture   *fOn;            // button ON picture
00272    const TGPicture   *fOff;           // button OFF picture
00273    const TGPicture   *fDisOn;         // button disabled and was ON picture
00274    const TGPicture   *fDisOff;        // button disabled and was OFF picture
00275 
00276 
00277    void Init();
00278    void PSetState(EButtonState state, Bool_t emit);
00279    virtual void DoRedraw();
00280    virtual void EmitSignals(Bool_t wasUp = kTRUE);
00281 
00282    static const TGFont *fgDefaultFont;
00283    static const TGGC   *fgDefaultGC;
00284 
00285 public:
00286    static FontStruct_t  GetDefaultFontStruct();
00287    static const TGGC   &GetDefaultGC();
00288 
00289    TGCheckButton(const TGWindow *p, TGHotString *s, Int_t id = -1,
00290                  GContext_t norm = GetDefaultGC()(),
00291                  FontStruct_t font = GetDefaultFontStruct(),
00292                  UInt_t option = 0);
00293    TGCheckButton(const TGWindow *p = 0, const char *s = 0, Int_t id = -1,
00294                  GContext_t norm = GetDefaultGC()(),
00295                  FontStruct_t font = GetDefaultFontStruct(),
00296                  UInt_t option = 0);
00297    TGCheckButton(const TGWindow *p, const char *s, const char *cmd, Int_t id = -1,
00298                  GContext_t norm = GetDefaultGC()(),
00299                  FontStruct_t font = GetDefaultFontStruct(),
00300                  UInt_t option = 0);
00301    virtual ~TGCheckButton();
00302 
00303    virtual TGDimension GetDefaultSize() const;
00304 
00305    virtual Bool_t HandleButton(Event_t *event);
00306    virtual Bool_t HandleKey(Event_t *event);
00307    virtual Bool_t HandleCrossing(Event_t *event);
00308    virtual Bool_t IsToggleButton() const { return kTRUE; }
00309    virtual Bool_t IsOn() const { return fState == kButtonDown; }
00310    virtual Bool_t IsDown() const { return fState == kButtonDown; }
00311    virtual Bool_t IsDisabledAndSelected() const { return kButtonDisabled && fStateOn; }
00312    virtual void   SetDisabledAndSelected(Bool_t);
00313    virtual void   SetState(EButtonState state, Bool_t emit = kFALSE);
00314    virtual void   SavePrimitive(ostream &out, Option_t *option = "");
00315 
00316    ClassDef(TGCheckButton,0)  // A check button widget
00317 };
00318 
00319 
00320 class TGRadioButton : public TGTextButton {
00321 
00322 private:
00323    TGRadioButton(const TGRadioButton&);
00324    TGRadioButton& operator=(const TGRadioButton&);
00325 
00326 protected:
00327    EButtonState       fPrevState;   // previous radio button state
00328    const TGPicture   *fOn;          // button ON picture
00329    const TGPicture   *fOff;         // button OFF picture
00330    const TGPicture   *fDisOn;       // button disabled and was ON picture
00331    const TGPicture   *fDisOff;      // button disabled and was OFF picture
00332 
00333    void Init();
00334    void PSetState(EButtonState state, Bool_t emit);
00335    virtual void DoRedraw();
00336    virtual void EmitSignals(Bool_t wasUp = kTRUE);
00337 
00338    static const TGFont *fgDefaultFont;
00339    static const TGGC   *fgDefaultGC;
00340 
00341 public:
00342    static FontStruct_t  GetDefaultFontStruct();
00343    static const TGGC   &GetDefaultGC();
00344 
00345    TGRadioButton(const TGWindow *p, TGHotString *s, Int_t id = -1,
00346                  GContext_t norm = GetDefaultGC()(),
00347                  FontStruct_t font = GetDefaultFontStruct(),
00348                  UInt_t option = 0);
00349    TGRadioButton(const TGWindow *p = 0, const char *s = 0, Int_t id = -1,
00350                  GContext_t norm = GetDefaultGC()(),
00351                  FontStruct_t font = GetDefaultFontStruct(),
00352                  UInt_t option = 0);
00353    TGRadioButton(const TGWindow *p, const char *s, const char *cmd, Int_t id = -1,
00354                  GContext_t norm = GetDefaultGC()(),
00355                  FontStruct_t font = GetDefaultFontStruct(),
00356                  UInt_t option = 0);
00357    virtual ~TGRadioButton();
00358 
00359    virtual TGDimension GetDefaultSize() const;
00360 
00361    virtual Bool_t HandleButton(Event_t *event);
00362    virtual Bool_t HandleKey(Event_t *event);
00363    virtual Bool_t HandleCrossing(Event_t *event);
00364    virtual void   SetState(EButtonState state, Bool_t emit = kFALSE);
00365    virtual void   SetDisabledAndSelected(Bool_t);
00366    virtual Bool_t IsToggleButton() const { return kTRUE; }
00367    virtual Bool_t IsExclusiveToggle() const { return kTRUE; }
00368    virtual Bool_t IsOn() const { return fStateOn; }
00369    virtual Bool_t IsDown() const { return fStateOn; }
00370    virtual Bool_t IsDisabledAndSelected() const { return kButtonDisabled && fStateOn; }
00371    virtual void   SavePrimitive(ostream &out, Option_t *option = "");
00372 
00373    ClassDef(TGRadioButton,0)  // A radio button widget
00374 };
00375 
00376 
00377 class TGSplitButton : public TGTextButton {
00378 
00379 friend class TGPopupMenu;
00380 
00381 private:
00382    TGSplitButton(const TGSplitButton&);            // Not implemented
00383    TGSplitButton& operator=(const TGSplitButton&); // Not implemented
00384 
00385    void CalcSize();
00386    void DrawTriangle(const GContext_t gc, Int_t x, Int_t y);
00387    Bool_t HandleSButton(Event_t *event);
00388    Bool_t HandleSCrossing(Event_t *event);
00389    Bool_t HandleSKey(Event_t *event);
00390    void SetMenuState(Bool_t state) ;
00391 
00392 protected:
00393    // Data members for menu functionality
00394    Bool_t       fSplit;         // kTRUE if menu is in split mode
00395    EButtonState fMBState;       // state for menu button in split mode
00396    UInt_t       fTBWidth;       // width of text button
00397    UInt_t       fMBWidth;       // width of menu button
00398    TGPopupMenu *fPopMenu;       // pointer to popup menu
00399    Int_t        fEntryId;       // Id of the currently active menu entry
00400    TGHotString *fMenuLabel;     // Label of the menu;
00401    Cursor_t     fDefaultCursor; // Default Cursor
00402    Bool_t       fKeyNavigate;   // kTRUE is keynavigation is being used
00403    TGString     fWidestLabel;  // Longest label that can be on the button
00404    TGString     fHeighestLabel; // Heighest label that can be on the button
00405 
00406    virtual void DoRedraw();
00407    void Init();
00408    void BindKeys(Bool_t on = kTRUE);
00409    void BindMenuKeys(Bool_t on = kTRUE);
00410 
00411 public:
00412    TGSplitButton(const TGWindow *p, TGHotString *menulabel,
00413                 TGPopupMenu *popmenu, Bool_t split = kTRUE, 
00414                 Int_t id = -1, GContext_t norm = GetDefaultGC()(),
00415                 FontStruct_t fontstruct = GetDefaultFontStruct(),
00416                 UInt_t option = kRaisedFrame | kDoubleBorder);
00417    
00418    virtual ~TGSplitButton();
00419    
00420    virtual TGDimension GetDefaultSize() const ;
00421    
00422    virtual void   SetText(TGHotString *new_label);
00423    virtual void   SetText(const TString &new_label);
00424    virtual void   SetFont(FontStruct_t font, Bool_t global = kFALSE);
00425    virtual void   SetFont(const char *fontName, Bool_t global = kFALSE);
00426    virtual void   SetMBState(EButtonState state);
00427    virtual void   SetSplit(Bool_t split);
00428    Bool_t         IsSplit() { return fSplit; }
00429    virtual Bool_t HandleButton(Event_t *event);
00430    virtual Bool_t HandleCrossing(Event_t *event);
00431    virtual Bool_t HandleKey(Event_t *event);
00432    virtual Bool_t HandleMotion(Event_t *event);
00433    virtual void   Layout();
00434 
00435    virtual void MBPressed()  { Emit("MBPressed()"); }   // *SIGNAL*
00436    virtual void MBReleased() { Emit("MBReleased()"); }  // *SIGNAL*
00437    virtual void MBClicked()  { Emit("MBClicked()"); }   // *SIGNAL*
00438    virtual void ItemClicked(Int_t id) { Emit("ItemClicked(Int_t)", id); } // *SIGNAL*
00439 
00440    // Slots
00441    void HandleMenu(Int_t id) ;
00442    
00443    ClassDef(TGSplitButton, 0) //a split button widget
00444 };
00445 
00446 #endif

Generated on Tue Jul 5 14:19:50 2011 for ROOT_528-00b_version by  doxygen 1.5.1