1 ///////////////////////////////////////////////////////////////////////////// 2 // Name: wx/tbarbase.h 3 // Purpose: Base class for toolbar classes 4 // Author: Julian Smart 5 // Modified by: 6 // Created: 01/02/97 7 // RCS-ID: $Id: tbarbase.h,v 1.1 2006/12/02 15:58:27 scara Exp $ 8 // Copyright: (c) Julian Smart and Markus Holzem 9 // Licence: wxWindows licence 10 ///////////////////////////////////////////////////////////////////////////// 11 12 #ifndef _WX_TBARBASE_H_ 13 #define _WX_TBARBASE_H_ 14 15 // ---------------------------------------------------------------------------- 16 // headers 17 // ---------------------------------------------------------------------------- 18 19 #if defined(__GNUG__) && !defined(__APPLE__) 20 #pragma interface "tbarbase.h" 21 #endif 22 23 #include "wx/defs.h" 24 25 #if wxUSE_TOOLBAR 26 27 #include "wx/bitmap.h" 28 #include "wx/list.h" 29 #include "wx/control.h" 30 31 class WXDLLEXPORT wxToolBarBase; 32 class WXDLLEXPORT wxToolBarToolBase; 33 class WXDLLEXPORT wxImage; 34 35 // ---------------------------------------------------------------------------- 36 // constants 37 // ---------------------------------------------------------------------------- 38 39 WXDLLEXPORT_DATA(extern const wxChar*) wxToolBarNameStr; 40 WXDLLEXPORT_DATA(extern const wxSize) wxDefaultSize; 41 WXDLLEXPORT_DATA(extern const wxPoint) wxDefaultPosition; 42 43 enum wxToolBarToolStyle 44 { 45 wxTOOL_STYLE_BUTTON = 1, 46 wxTOOL_STYLE_SEPARATOR = 2, 47 wxTOOL_STYLE_CONTROL 48 }; 49 50 // ---------------------------------------------------------------------------- 51 // wxToolBarTool is a toolbar element. 52 // 53 // It has a unique id (except for the separators which always have id -1), the 54 // style (telling whether it is a normal button, separator or a control), the 55 // state (toggled or not, enabled or not) and short and long help strings. The 56 // default implementations use the short help string for the tooltip text which 57 // is popped up when the mouse pointer enters the tool and the long help string 58 // for the applications status bar. 59 // ---------------------------------------------------------------------------- 60 61 class WXDLLEXPORT wxToolBarToolBase : public wxObject 62 { 63 public: 64 // ctors & dtor 65 // ------------ 66 67 wxToolBarToolBase(wxToolBarBase *tbar = (wxToolBarBase *)NULL, 68 int id = wxID_SEPARATOR, 69 const wxString& label = wxEmptyString, 70 const wxBitmap& bmpNormal = wxNullBitmap, 71 const wxBitmap& bmpDisabled = wxNullBitmap, 72 wxItemKind kind = wxITEM_NORMAL, 73 wxObject *clientData = (wxObject *) NULL, 74 const wxString& shortHelpString = wxEmptyString, 75 const wxString& longHelpString = wxEmptyString) m_label(label)76 : m_label(label), 77 m_shortHelpString(shortHelpString), 78 m_longHelpString(longHelpString) 79 { 80 m_tbar = tbar; 81 m_id = id; 82 m_clientData = clientData; 83 84 m_bmpNormal = bmpNormal; 85 m_bmpDisabled = bmpDisabled; 86 87 m_kind = kind; 88 89 m_enabled = TRUE; 90 m_toggled = FALSE; 91 92 m_toolStyle = id == wxID_SEPARATOR ? wxTOOL_STYLE_SEPARATOR 93 : wxTOOL_STYLE_BUTTON; 94 } 95 wxToolBarToolBase(wxToolBarBase * tbar,wxControl * control)96 wxToolBarToolBase(wxToolBarBase *tbar, wxControl *control) 97 { 98 m_tbar = tbar; 99 m_control = control; 100 m_id = control->GetId(); 101 102 m_kind = wxITEM_MAX; // invalid value 103 104 m_enabled = TRUE; 105 m_toggled = FALSE; 106 107 m_toolStyle = wxTOOL_STYLE_CONTROL; 108 } 109 110 ~wxToolBarToolBase(); 111 112 // accessors 113 // --------- 114 115 // general GetId()116 int GetId() const { return m_id; } 117 GetControl()118 wxControl *GetControl() const 119 { 120 wxASSERT_MSG( IsControl(), _T("this toolbar tool is not a control") ); 121 122 return m_control; 123 } 124 GetToolBar()125 wxToolBarBase *GetToolBar() const { return m_tbar; } 126 127 // style IsButton()128 bool IsButton() const { return m_toolStyle == wxTOOL_STYLE_BUTTON; } IsControl()129 bool IsControl() const { return m_toolStyle == wxTOOL_STYLE_CONTROL; } IsSeparator()130 bool IsSeparator() const { return m_toolStyle == wxTOOL_STYLE_SEPARATOR; } GetStyle()131 int GetStyle() const { return m_toolStyle; } GetKind()132 wxItemKind GetKind() const 133 { 134 wxASSERT_MSG( IsButton(), _T("only makes sense for buttons") ); 135 136 return m_kind; 137 } 138 139 // state IsEnabled()140 bool IsEnabled() const { return m_enabled; } IsToggled()141 bool IsToggled() const { return m_toggled; } CanBeToggled()142 bool CanBeToggled() const 143 { return m_kind == wxITEM_CHECK || m_kind == wxITEM_RADIO; } 144 145 // attributes GetNormalBitmap()146 const wxBitmap& GetNormalBitmap() const { return m_bmpNormal; } GetDisabledBitmap()147 const wxBitmap& GetDisabledBitmap() const { return m_bmpDisabled; } 148 GetBitmap()149 const wxBitmap& GetBitmap() const 150 { return IsEnabled() ? GetNormalBitmap() : GetDisabledBitmap(); } 151 GetLabel()152 wxString GetLabel() const { return m_label; } 153 GetShortHelp()154 wxString GetShortHelp() const { return m_shortHelpString; } GetLongHelp()155 wxString GetLongHelp() const { return m_longHelpString; } 156 GetClientData()157 wxObject *GetClientData() const 158 { 159 if ( m_toolStyle == wxTOOL_STYLE_CONTROL ) 160 { 161 return (wxObject*)m_control->GetClientData(); 162 } 163 else 164 { 165 return m_clientData; 166 } 167 } 168 169 // modifiers: return TRUE if the state really changed 170 bool Enable(bool enable); 171 bool Toggle(bool toggle); 172 bool SetToggle(bool toggle); 173 bool SetShortHelp(const wxString& help); 174 bool SetLongHelp(const wxString& help); 175 Toggle()176 void Toggle() { Toggle(!IsToggled()); } 177 SetNormalBitmap(const wxBitmap & bmp)178 void SetNormalBitmap(const wxBitmap& bmp) { m_bmpNormal = bmp; } SetDisabledBitmap(const wxBitmap & bmp)179 void SetDisabledBitmap(const wxBitmap& bmp) { m_bmpDisabled = bmp; } 180 SetLabel(const wxString & label)181 virtual void SetLabel(const wxString& label) { m_label = label; } 182 SetClientData(wxObject * clientData)183 void SetClientData(wxObject *clientData) 184 { 185 if ( m_toolStyle == wxTOOL_STYLE_CONTROL ) 186 { 187 m_control->SetClientData(clientData); 188 } 189 else 190 { 191 m_clientData = clientData; 192 } 193 } 194 195 // add tool to/remove it from a toolbar Detach()196 virtual void Detach() { m_tbar = (wxToolBarBase *)NULL; } Attach(wxToolBarBase * tbar)197 virtual void Attach(wxToolBarBase *tbar) { m_tbar = tbar; } 198 199 // compatibility only, don't use 200 #if WXWIN_COMPATIBILITY_2_2 GetBitmap1()201 const wxBitmap& GetBitmap1() const { return GetNormalBitmap(); } GetBitmap2()202 const wxBitmap& GetBitmap2() const { return GetDisabledBitmap(); } 203 SetBitmap1(const wxBitmap & bmp)204 void SetBitmap1(const wxBitmap& bmp) { SetNormalBitmap(bmp); } SetBitmap2(const wxBitmap & bmp)205 void SetBitmap2(const wxBitmap& bmp) { SetDisabledBitmap(bmp); } 206 #endif // WXWIN_COMPATIBILITY_2_2 207 208 protected: 209 wxToolBarBase *m_tbar; // the toolbar to which we belong (may be NULL) 210 211 // tool parameters 212 int m_toolStyle; // see enum wxToolBarToolStyle 213 int m_id; // the tool id, wxID_SEPARATOR for separator 214 wxItemKind m_kind; // for normal buttons may be wxITEM_NORMAL/CHECK/RADIO 215 216 // as controls have their own client data, no need to waste memory 217 union 218 { 219 wxObject *m_clientData; 220 wxControl *m_control; 221 }; 222 223 // tool state 224 bool m_toggled; 225 bool m_enabled; 226 227 // normal and disabled bitmaps for the tool, both can be invalid 228 wxBitmap m_bmpNormal; 229 wxBitmap m_bmpDisabled; 230 231 // the button label 232 wxString m_label; 233 234 // short and long help strings 235 wxString m_shortHelpString; 236 wxString m_longHelpString; 237 }; 238 239 // a list of toolbar tools 240 WX_DECLARE_EXPORTED_LIST(wxToolBarToolBase, wxToolBarToolsList); 241 242 // ---------------------------------------------------------------------------- 243 // the base class for all toolbars 244 // ---------------------------------------------------------------------------- 245 246 class WXDLLEXPORT wxToolBarBase : public wxControl 247 { 248 public: 249 wxToolBarBase(); 250 virtual ~wxToolBarBase(); 251 252 // toolbar construction 253 // -------------------- 254 255 // the full AddTool() function 256 // 257 // If bmpDisabled is wxNullBitmap, a shadowed version of the normal bitmap 258 // is created and used as the disabled image. 259 wxToolBarToolBase *AddTool(int id, 260 const wxString& label, 261 const wxBitmap& bitmap, 262 const wxBitmap& bmpDisabled, 263 wxItemKind kind = wxITEM_NORMAL, 264 const wxString& shortHelp = wxEmptyString, 265 const wxString& longHelp = wxEmptyString, 266 wxObject *data = NULL) 267 { 268 return DoAddTool(id, label, bitmap, bmpDisabled, kind, 269 shortHelp, longHelp, data); 270 } 271 272 // the most common AddTool() version 273 wxToolBarToolBase *AddTool(int id, 274 const wxString& label, 275 const wxBitmap& bitmap, 276 const wxString& shortHelp = wxEmptyString, 277 wxItemKind kind = wxITEM_NORMAL) 278 { 279 return AddTool(id, label, bitmap, wxNullBitmap, kind, shortHelp); 280 } 281 282 // add a check tool, i.e. a tool which can be toggled 283 wxToolBarToolBase *AddCheckTool(int id, 284 const wxString& label, 285 const wxBitmap& bitmap, 286 const wxBitmap& bmpDisabled = wxNullBitmap, 287 const wxString& shortHelp = wxEmptyString, 288 const wxString& longHelp = wxEmptyString, 289 wxObject *data = NULL) 290 { 291 return AddTool(id, label, bitmap, bmpDisabled, wxITEM_CHECK, 292 shortHelp, longHelp, data); 293 } 294 295 // add a radio tool, i.e. a tool which can be toggled and releases any 296 // other toggled radio tools in the same group when it happens 297 wxToolBarToolBase *AddRadioTool(int id, 298 const wxString& label, 299 const wxBitmap& bitmap, 300 const wxBitmap& bmpDisabled = wxNullBitmap, 301 const wxString& shortHelp = wxEmptyString, 302 const wxString& longHelp = wxEmptyString, 303 wxObject *data = NULL) 304 { 305 return AddTool(id, label, bitmap, bmpDisabled, wxITEM_RADIO, 306 shortHelp, longHelp, data); 307 } 308 309 310 // insert the new tool at the given position, if pos == GetToolsCount(), it 311 // is equivalent to AddTool() 312 virtual wxToolBarToolBase *InsertTool 313 ( 314 size_t pos, 315 int id, 316 const wxString& label, 317 const wxBitmap& bitmap, 318 const wxBitmap& bmpDisabled = wxNullBitmap, 319 wxItemKind kind = wxITEM_NORMAL, 320 const wxString& shortHelp = wxEmptyString, 321 const wxString& longHelp = wxEmptyString, 322 wxObject *clientData = NULL 323 ); 324 325 // add an arbitrary control to the toolbar, return TRUE if ok (notice that 326 // the control will be deleted by the toolbar and that it will also adjust 327 // its position/size) 328 // 329 // NB: the control should have toolbar as its parent 330 virtual wxToolBarToolBase *AddControl(wxControl *control); 331 virtual wxToolBarToolBase *InsertControl(size_t pos, wxControl *control); 332 333 // get the control with the given id or return NULL 334 virtual wxControl *FindControl( int id ); 335 336 // add a separator to the toolbar 337 virtual wxToolBarToolBase *AddSeparator(); 338 virtual wxToolBarToolBase *InsertSeparator(size_t pos); 339 340 // remove the tool from the toolbar: the caller is responsible for actually 341 // deleting the pointer 342 virtual wxToolBarToolBase *RemoveTool(int id); 343 344 // delete tool either by index or by position 345 virtual bool DeleteToolByPos(size_t pos); 346 virtual bool DeleteTool(int id); 347 348 // delete all tools 349 virtual void ClearTools(); 350 351 // must be called after all buttons have been created to finish toolbar 352 // initialisation 353 virtual bool Realize(); 354 355 // tools state 356 // ----------- 357 358 virtual void EnableTool(int id, bool enable); 359 virtual void ToggleTool(int id, bool toggle); 360 361 // Set this to be togglable (or not) 362 virtual void SetToggle(int id, bool toggle); 363 364 // set/get tools client data (not for controls) 365 virtual wxObject *GetToolClientData(int id) const; 366 virtual void SetToolClientData(int id, wxObject *clientData); 367 368 // return TRUE if the tool is toggled 369 virtual bool GetToolState(int id) const; 370 371 virtual bool GetToolEnabled(int id) const; 372 373 virtual void SetToolShortHelp(int id, const wxString& helpString); 374 virtual wxString GetToolShortHelp(int id) const; 375 virtual void SetToolLongHelp(int id, const wxString& helpString); 376 virtual wxString GetToolLongHelp(int id) const; 377 378 // margins/packing/separation 379 // -------------------------- 380 381 virtual void SetMargins(int x, int y); SetMargins(const wxSize & size)382 void SetMargins(const wxSize& size) 383 { SetMargins((int) size.x, (int) size.y); } SetToolPacking(int packing)384 virtual void SetToolPacking(int packing) 385 { m_toolPacking = packing; } SetToolSeparation(int separation)386 virtual void SetToolSeparation(int separation) 387 { m_toolSeparation = separation; } 388 GetToolMargins()389 virtual wxSize GetToolMargins() const { return wxSize(m_xMargin, m_yMargin); } GetToolPacking()390 virtual int GetToolPacking() const { return m_toolPacking; } GetToolSeparation()391 virtual int GetToolSeparation() const { return m_toolSeparation; } 392 393 // toolbar geometry 394 // ---------------- 395 396 // set the number of toolbar rows 397 virtual void SetRows(int nRows); 398 399 // the toolbar can wrap - limit the number of columns or rows it may take SetMaxRowsCols(int rows,int cols)400 void SetMaxRowsCols(int rows, int cols) 401 { m_maxRows = rows; m_maxCols = cols; } GetMaxRows()402 int GetMaxRows() const { return m_maxRows; } GetMaxCols()403 int GetMaxCols() const { return m_maxCols; } 404 405 // get/set the size of the bitmaps used by the toolbar: should be called 406 // before adding any tools to the toolbar SetToolBitmapSize(const wxSize & size)407 virtual void SetToolBitmapSize(const wxSize& size) 408 { m_defaultWidth = size.x; m_defaultHeight = size.y; }; GetToolBitmapSize()409 virtual wxSize GetToolBitmapSize() const 410 { return wxSize(m_defaultWidth, m_defaultHeight); } 411 412 // the button size in some implementations is bigger than the bitmap size: 413 // get the total button size (by default the same as bitmap size) GetToolSize()414 virtual wxSize GetToolSize() const 415 { return GetToolBitmapSize(); } ; 416 417 // returns a (non separator) tool containing the point (x, y) or NULL if 418 // there is no tool at this point (corrdinates are client) 419 virtual wxToolBarToolBase *FindToolForPosition(wxCoord x, 420 wxCoord y) const = 0; 421 422 // return TRUE if this is a vertical toolbar, otherwise FALSE IsVertical()423 bool IsVertical() const { return HasFlag(wxTB_VERTICAL); } 424 425 426 // the old versions of the various methods kept for compatibility 427 // don't use in the new code! 428 // -------------------------------------------------------------- 429 430 wxToolBarToolBase *AddTool(int id, 431 const wxBitmap& bitmap, 432 const wxBitmap& bmpDisabled, 433 bool toggle = FALSE, 434 wxObject *clientData = NULL, 435 const wxString& shortHelpString = wxEmptyString, 436 const wxString& longHelpString = wxEmptyString) 437 { 438 return AddTool(id, wxEmptyString, 439 bitmap, bmpDisabled, 440 toggle ? wxITEM_CHECK : wxITEM_NORMAL, 441 shortHelpString, longHelpString, clientData); 442 } 443 444 wxToolBarToolBase *AddTool(int id, 445 const wxBitmap& bitmap, 446 const wxString& shortHelpString = wxEmptyString, 447 const wxString& longHelpString = wxEmptyString) 448 { 449 return AddTool(id, wxEmptyString, 450 bitmap, wxNullBitmap, wxITEM_NORMAL, 451 shortHelpString, longHelpString, NULL); 452 } 453 454 wxToolBarToolBase *AddTool(int id, 455 const wxBitmap& bitmap, 456 const wxBitmap& bmpDisabled, 457 bool toggle, 458 wxCoord xPos, 459 wxCoord yPos = -1, 460 wxObject *clientData = NULL, 461 const wxString& shortHelp = wxEmptyString, 462 const wxString& longHelp = wxEmptyString) 463 { 464 return DoAddTool(id, wxEmptyString, bitmap, bmpDisabled, 465 toggle ? wxITEM_CHECK : wxITEM_NORMAL, 466 shortHelp, longHelp, clientData, xPos, yPos); 467 } 468 469 wxToolBarToolBase *InsertTool(size_t pos, 470 int id, 471 const wxBitmap& bitmap, 472 const wxBitmap& bmpDisabled = wxNullBitmap, 473 bool toggle = FALSE, 474 wxObject *clientData = NULL, 475 const wxString& shortHelp = wxEmptyString, 476 const wxString& longHelp = wxEmptyString) 477 { 478 return InsertTool(pos, id, wxEmptyString, bitmap, bmpDisabled, 479 toggle ? wxITEM_CHECK : wxITEM_NORMAL, 480 shortHelp, longHelp, clientData); 481 } 482 483 // event handlers 484 // -------------- 485 486 // NB: these functions are deprecated, use EVT_TOOL_XXX() instead! 487 488 // Only allow toggle if returns TRUE. Call when left button up. 489 virtual bool OnLeftClick(int id, bool toggleDown); 490 491 // Call when right button down. 492 virtual void OnRightClick(int id, long x, long y); 493 494 // Called when the mouse cursor enters a tool bitmap. 495 // Argument is -1 if mouse is exiting the toolbar. 496 virtual void OnMouseEnter(int id); 497 498 // more deprecated functions 499 // ------------------------- 500 501 #if WXWIN_COMPATIBILITY SetDefaultSize(int w,int h)502 void SetDefaultSize(int w, int h) { SetDefaultSize(wxSize(w, h)); } GetDefaultWidth()503 long GetDefaultWidth() const { return m_defaultWidth; } GetDefaultHeight()504 long GetDefaultHeight() const { return m_defaultHeight; } GetDefaultButtonWidth()505 int GetDefaultButtonWidth() const { return (int) GetDefaultButtonSize().x; }; GetDefaultButtonHeight()506 int GetDefaultButtonHeight() const { return (int) GetDefaultButtonSize().y; }; SetDefaultSize(const wxSize & size)507 virtual void SetDefaultSize(const wxSize& size) { SetToolBitmapSize(size); } GetDefaultSize()508 virtual wxSize GetDefaultSize() const { return GetToolBitmapSize(); } GetDefaultButtonSize()509 virtual wxSize GetDefaultButtonSize() const { return GetToolSize(); } 510 #endif // WXWIN_COMPATIBILITY 511 512 // use GetToolMargins() instead GetMargins()513 wxSize GetMargins() const { return GetToolMargins(); } 514 515 // implementation only from now on 516 // ------------------------------- 517 GetToolsCount()518 size_t GetToolsCount() const { return m_tools.GetCount(); } 519 520 void OnIdle(wxIdleEvent& event); 521 522 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers) 523 virtual void DoToolbarUpdates(); 524 525 // don't want toolbars to accept the focus AcceptsFocus()526 virtual bool AcceptsFocus() const { return FALSE; } 527 528 protected: 529 // to implement in derived classes 530 // ------------------------------- 531 532 // create a new toolbar tool and add it to the toolbar, this is typically 533 // implemented by just calling InsertTool() 534 virtual wxToolBarToolBase *DoAddTool 535 ( 536 int id, 537 const wxString& label, 538 const wxBitmap& bitmap, 539 const wxBitmap& bmpDisabled, 540 wxItemKind kind, 541 const wxString& shortHelp = wxEmptyString, 542 const wxString& longHelp = wxEmptyString, 543 wxObject *clientData = NULL, 544 wxCoord xPos = -1, 545 wxCoord yPos = -1 546 ); 547 548 // the tool is not yet inserted into m_tools list when this function is 549 // called and will only be added to it if this function succeeds 550 virtual bool DoInsertTool(size_t pos, wxToolBarToolBase *tool) = 0; 551 552 // the tool is still in m_tools list when this function is called, it will 553 // only be deleted from it if it succeeds 554 virtual bool DoDeleteTool(size_t pos, wxToolBarToolBase *tool) = 0; 555 556 // called when the tools enabled flag changes 557 virtual void DoEnableTool(wxToolBarToolBase *tool, bool enable) = 0; 558 559 // called when the tool is toggled 560 virtual void DoToggleTool(wxToolBarToolBase *tool, bool toggle) = 0; 561 562 // called when the tools "can be toggled" flag changes 563 virtual void DoSetToggle(wxToolBarToolBase *tool, bool toggle) = 0; 564 565 // the functions to create toolbar tools 566 virtual wxToolBarToolBase *CreateTool(int id, 567 const wxString& label, 568 const wxBitmap& bmpNormal, 569 const wxBitmap& bmpDisabled, 570 wxItemKind kind, 571 wxObject *clientData, 572 const wxString& shortHelp, 573 const wxString& longHelp) = 0; 574 575 virtual wxToolBarToolBase *CreateTool(wxControl *control) = 0; 576 577 // helper functions 578 // ---------------- 579 580 // find the tool by id 581 wxToolBarToolBase *FindById(int id) const; 582 583 // the list of all our tools 584 wxToolBarToolsList m_tools; 585 586 // the offset of the first tool 587 int m_xMargin; 588 int m_yMargin; 589 590 // the maximum number of toolbar rows/columns 591 int m_maxRows; 592 int m_maxCols; 593 594 // the tool packing and separation 595 int m_toolPacking, 596 m_toolSeparation; 597 598 // the size of the toolbar bitmaps 599 wxCoord m_defaultWidth, m_defaultHeight; 600 601 private: 602 DECLARE_EVENT_TABLE() 603 DECLARE_CLASS(wxToolBarBase) 604 }; 605 606 // Helper function for creating the image for disabled buttons 607 bool wxCreateGreyedImage(const wxImage& in, wxImage& out) ; 608 609 #endif // wxUSE_TOOLBAR 610 611 #endif 612 // _WX_TBARBASE_H_ 613 614