1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Base class for toolbar classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TBARBASE_H_
13 #define _WX_TBARBASE_H_
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
23 #include "wx/bitmap.h"
25 #include "wx/control.h"
27 class WXDLLIMPEXP_FWD_CORE wxToolBarBase
;
28 class WXDLLIMPEXP_FWD_CORE wxToolBarToolBase
;
29 class WXDLLIMPEXP_FWD_CORE wxImage
;
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 extern WXDLLIMPEXP_DATA_CORE(const char) wxToolBarNameStr
[];
36 extern WXDLLIMPEXP_DATA_CORE(const wxSize
) wxDefaultSize
;
37 extern WXDLLIMPEXP_DATA_CORE(const wxPoint
) wxDefaultPosition
;
39 enum wxToolBarToolStyle
41 wxTOOL_STYLE_BUTTON
= 1,
42 wxTOOL_STYLE_SEPARATOR
= 2,
46 // ----------------------------------------------------------------------------
47 // wxToolBarTool is a toolbar element.
49 // It has a unique id (except for the separators which always have id wxID_ANY), the
50 // style (telling whether it is a normal button, separator or a control), the
51 // state (toggled or not, enabled or not) and short and long help strings. The
52 // default implementations use the short help string for the tooltip text which
53 // is popped up when the mouse pointer enters the tool and the long help string
54 // for the applications status bar.
55 // ----------------------------------------------------------------------------
57 class WXDLLIMPEXP_CORE wxToolBarToolBase
: public wxObject
63 // generic ctor for any kind of tool
64 wxToolBarToolBase(wxToolBarBase
*tbar
= NULL
,
65 int toolid
= wxID_SEPARATOR
,
66 const wxString
& label
= wxEmptyString
,
67 const wxBitmap
& bmpNormal
= wxNullBitmap
,
68 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
69 wxItemKind kind
= wxITEM_NORMAL
,
70 wxObject
*clientData
= NULL
,
71 const wxString
& shortHelpString
= wxEmptyString
,
72 const wxString
& longHelpString
= wxEmptyString
)
74 m_shortHelpString(shortHelpString
),
75 m_longHelpString(longHelpString
)
80 toolid
== wxID_SEPARATOR
? wxTOOL_STYLE_SEPARATOR
81 : wxTOOL_STYLE_BUTTON
,
82 toolid
== wxID_ANY
? wxWindow::NewControlId()
87 m_clientData
= clientData
;
89 m_bmpNormal
= bmpNormal
;
90 m_bmpDisabled
= bmpDisabled
;
93 // ctor for controls only
94 wxToolBarToolBase(wxToolBarBase
*tbar
,
96 const wxString
& label
)
99 Init(tbar
, wxTOOL_STYLE_CONTROL
, control
->GetId(), wxITEM_MAX
);
104 virtual ~wxToolBarToolBase();
110 int GetId() const { return m_id
; }
112 wxControl
*GetControl() const
114 wxASSERT_MSG( IsControl(), wxT("this toolbar tool is not a control") );
119 wxToolBarBase
*GetToolBar() const { return m_tbar
; }
122 bool IsStretchable() const { return m_stretchable
; }
123 bool IsButton() const { return m_toolStyle
== wxTOOL_STYLE_BUTTON
; }
124 bool IsControl() const { return m_toolStyle
== wxTOOL_STYLE_CONTROL
; }
125 bool IsSeparator() const { return m_toolStyle
== wxTOOL_STYLE_SEPARATOR
; }
126 bool IsStretchableSpace() const { return IsSeparator() && IsStretchable(); }
127 int GetStyle() const { return m_toolStyle
; }
128 wxItemKind
GetKind() const
130 wxASSERT_MSG( IsButton(), wxT("only makes sense for buttons") );
135 void MakeStretchable()
137 wxASSERT_MSG( IsSeparator(), "only separators can be stretchable" );
139 m_stretchable
= true;
143 bool IsEnabled() const { return m_enabled
; }
144 bool IsToggled() const { return m_toggled
; }
145 bool CanBeToggled() const
146 { return m_kind
== wxITEM_CHECK
|| m_kind
== wxITEM_RADIO
; }
149 const wxBitmap
& GetNormalBitmap() const { return m_bmpNormal
; }
150 const wxBitmap
& GetDisabledBitmap() const { return m_bmpDisabled
; }
152 const wxBitmap
& GetBitmap() const
153 { return IsEnabled() ? GetNormalBitmap() : GetDisabledBitmap(); }
155 const wxString
& GetLabel() const { return m_label
; }
157 const wxString
& GetShortHelp() const { return m_shortHelpString
; }
158 const wxString
& GetLongHelp() const { return m_longHelpString
; }
160 wxObject
*GetClientData() const
162 if ( m_toolStyle
== wxTOOL_STYLE_CONTROL
)
164 return (wxObject
*)m_control
->GetClientData();
172 // modifiers: return true if the state really changed
173 virtual bool Enable(bool enable
);
174 virtual bool Toggle(bool toggle
);
175 virtual bool SetToggle(bool toggle
);
176 virtual bool SetShortHelp(const wxString
& help
);
177 virtual bool SetLongHelp(const wxString
& help
);
179 void Toggle() { Toggle(!IsToggled()); }
181 virtual void SetNormalBitmap(const wxBitmap
& bmp
) { m_bmpNormal
= bmp
; }
182 virtual void SetDisabledBitmap(const wxBitmap
& bmp
) { m_bmpDisabled
= bmp
; }
184 virtual void SetLabel(const wxString
& label
) { m_label
= label
; }
186 void SetClientData(wxObject
*clientData
)
188 if ( m_toolStyle
== wxTOOL_STYLE_CONTROL
)
190 m_control
->SetClientData(clientData
);
194 m_clientData
= clientData
;
198 // add tool to/remove it from a toolbar
199 virtual void Detach() { m_tbar
= NULL
; }
200 virtual void Attach(wxToolBarBase
*tbar
) { m_tbar
= tbar
; }
203 // these methods are only for tools of wxITEM_DROPDOWN kind (but even such
204 // tools can have a NULL associated menu)
205 virtual void SetDropdownMenu(wxMenu
*menu
);
206 wxMenu
*GetDropdownMenu() const { return m_dropdownMenu
; }
210 // common part of all ctors
211 void Init(wxToolBarBase
*tbar
,
212 wxToolBarToolStyle style
,
223 m_stretchable
= false;
228 m_dropdownMenu
= NULL
;
233 wxToolBarBase
*m_tbar
; // the toolbar to which we belong (may be NULL)
236 wxToolBarToolStyle m_toolStyle
;
237 wxWindowIDRef m_id
; // the tool id, wxID_SEPARATOR for separator
238 wxItemKind m_kind
; // for normal buttons may be wxITEM_NORMAL/CHECK/RADIO
240 // as controls have their own client data, no need to waste memory
243 wxObject
*m_clientData
;
244 wxControl
*m_control
;
247 // true if this tool is stretchable: currently is only value for separators
254 // normal and disabled bitmaps for the tool, both can be invalid
255 wxBitmap m_bmpNormal
;
256 wxBitmap m_bmpDisabled
;
261 // short and long help strings
262 wxString m_shortHelpString
;
263 wxString m_longHelpString
;
266 wxMenu
*m_dropdownMenu
;
269 DECLARE_DYNAMIC_CLASS_NO_COPY(wxToolBarToolBase
)
272 // a list of toolbar tools
273 WX_DECLARE_EXPORTED_LIST(wxToolBarToolBase
, wxToolBarToolsList
);
275 // ----------------------------------------------------------------------------
276 // the base class for all toolbars
277 // ----------------------------------------------------------------------------
279 class WXDLLIMPEXP_CORE wxToolBarBase
: public wxControl
283 virtual ~wxToolBarBase();
285 // toolbar construction
286 // --------------------
288 // the full AddTool() function
290 // If bmpDisabled is wxNullBitmap, a shadowed version of the normal bitmap
291 // is created and used as the disabled image.
292 wxToolBarToolBase
*AddTool(int toolid
,
293 const wxString
& label
,
294 const wxBitmap
& bitmap
,
295 const wxBitmap
& bmpDisabled
,
296 wxItemKind kind
= wxITEM_NORMAL
,
297 const wxString
& shortHelp
= wxEmptyString
,
298 const wxString
& longHelp
= wxEmptyString
,
299 wxObject
*data
= NULL
)
301 return DoAddTool(toolid
, label
, bitmap
, bmpDisabled
, kind
,
302 shortHelp
, longHelp
, data
);
305 // the most common AddTool() version
306 wxToolBarToolBase
*AddTool(int toolid
,
307 const wxString
& label
,
308 const wxBitmap
& bitmap
,
309 const wxString
& shortHelp
= wxEmptyString
,
310 wxItemKind kind
= wxITEM_NORMAL
)
312 return AddTool(toolid
, label
, bitmap
, wxNullBitmap
, kind
, shortHelp
);
315 // add a check tool, i.e. a tool which can be toggled
316 wxToolBarToolBase
*AddCheckTool(int toolid
,
317 const wxString
& label
,
318 const wxBitmap
& bitmap
,
319 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
320 const wxString
& shortHelp
= wxEmptyString
,
321 const wxString
& longHelp
= wxEmptyString
,
322 wxObject
*data
= NULL
)
324 return AddTool(toolid
, label
, bitmap
, bmpDisabled
, wxITEM_CHECK
,
325 shortHelp
, longHelp
, data
);
328 // add a radio tool, i.e. a tool which can be toggled and releases any
329 // other toggled radio tools in the same group when it happens
330 wxToolBarToolBase
*AddRadioTool(int toolid
,
331 const wxString
& label
,
332 const wxBitmap
& bitmap
,
333 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
334 const wxString
& shortHelp
= wxEmptyString
,
335 const wxString
& longHelp
= wxEmptyString
,
336 wxObject
*data
= NULL
)
338 return AddTool(toolid
, label
, bitmap
, bmpDisabled
, wxITEM_RADIO
,
339 shortHelp
, longHelp
, data
);
343 // insert the new tool at the given position, if pos == GetToolsCount(), it
344 // is equivalent to AddTool()
345 virtual wxToolBarToolBase
*InsertTool
349 const wxString
& label
,
350 const wxBitmap
& bitmap
,
351 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
352 wxItemKind kind
= wxITEM_NORMAL
,
353 const wxString
& shortHelp
= wxEmptyString
,
354 const wxString
& longHelp
= wxEmptyString
,
355 wxObject
*clientData
= NULL
358 virtual wxToolBarToolBase
*AddTool (wxToolBarToolBase
*tool
);
359 virtual wxToolBarToolBase
*InsertTool (size_t pos
, wxToolBarToolBase
*tool
);
361 // add an arbitrary control to the toolbar (notice that the control will be
362 // deleted by the toolbar and that it will also adjust its position/size)
364 // the label is optional and, if specified, will be shown near the control
365 // NB: the control should have toolbar as its parent
366 virtual wxToolBarToolBase
*
367 AddControl(wxControl
*control
, const wxString
& label
= wxEmptyString
);
369 virtual wxToolBarToolBase
*
370 InsertControl(size_t pos
, wxControl
*control
,
371 const wxString
& label
= wxEmptyString
);
373 // get the control with the given id or return NULL
374 virtual wxControl
*FindControl( int toolid
);
376 // add a separator to the toolbar
377 virtual wxToolBarToolBase
*AddSeparator();
378 virtual wxToolBarToolBase
*InsertSeparator(size_t pos
);
380 // add a stretchable space to the toolbar: this is similar to a separator
381 // except that it's always blank and that all the extra space the toolbar
382 // has is [equally] distributed among the stretchable spaces in it
383 virtual wxToolBarToolBase
*AddStretchableSpace();
384 virtual wxToolBarToolBase
*InsertStretchableSpace(size_t pos
);
386 // remove the tool from the toolbar: the caller is responsible for actually
387 // deleting the pointer
388 virtual wxToolBarToolBase
*RemoveTool(int toolid
);
390 // delete tool either by index or by position
391 virtual bool DeleteToolByPos(size_t pos
);
392 virtual bool DeleteTool(int toolid
);
395 virtual void ClearTools();
397 // must be called after all buttons have been created to finish toolbar
400 // derived class versions should call the base one first, before doing
401 // platform-specific stuff
402 virtual bool Realize();
407 virtual void EnableTool(int toolid
, bool enable
);
408 virtual void ToggleTool(int toolid
, bool toggle
);
410 // Set this to be togglable (or not)
411 virtual void SetToggle(int toolid
, bool toggle
);
413 // set/get tools client data (not for controls)
414 virtual wxObject
*GetToolClientData(int toolid
) const;
415 virtual void SetToolClientData(int toolid
, wxObject
*clientData
);
417 // returns tool pos, or wxNOT_FOUND if tool isn't found
418 virtual int GetToolPos(int id
) const;
420 // return true if the tool is toggled
421 virtual bool GetToolState(int toolid
) const;
423 virtual bool GetToolEnabled(int toolid
) const;
425 virtual void SetToolShortHelp(int toolid
, const wxString
& helpString
);
426 virtual wxString
GetToolShortHelp(int toolid
) const;
427 virtual void SetToolLongHelp(int toolid
, const wxString
& helpString
);
428 virtual wxString
GetToolLongHelp(int toolid
) const;
430 virtual void SetToolNormalBitmap(int WXUNUSED(id
),
431 const wxBitmap
& WXUNUSED(bitmap
)) {}
432 virtual void SetToolDisabledBitmap(int WXUNUSED(id
),
433 const wxBitmap
& WXUNUSED(bitmap
)) {}
436 // margins/packing/separation
437 // --------------------------
439 virtual void SetMargins(int x
, int y
);
440 void SetMargins(const wxSize
& size
)
441 { SetMargins((int) size
.x
, (int) size
.y
); }
442 virtual void SetToolPacking(int packing
)
443 { m_toolPacking
= packing
; }
444 virtual void SetToolSeparation(int separation
)
445 { m_toolSeparation
= separation
; }
447 virtual wxSize
GetToolMargins() const { return wxSize(m_xMargin
, m_yMargin
); }
448 virtual int GetToolPacking() const { return m_toolPacking
; }
449 virtual int GetToolSeparation() const { return m_toolSeparation
; }
454 // set the number of toolbar rows
455 virtual void SetRows(int nRows
);
457 // the toolbar can wrap - limit the number of columns or rows it may take
458 void SetMaxRowsCols(int rows
, int cols
)
459 { m_maxRows
= rows
; m_maxCols
= cols
; }
460 int GetMaxRows() const { return m_maxRows
; }
461 int GetMaxCols() const { return m_maxCols
; }
463 // get/set the size of the bitmaps used by the toolbar: should be called
464 // before adding any tools to the toolbar
465 virtual void SetToolBitmapSize(const wxSize
& size
)
466 { m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
; }
467 virtual wxSize
GetToolBitmapSize() const
468 { return wxSize(m_defaultWidth
, m_defaultHeight
); }
470 // the button size in some implementations is bigger than the bitmap size:
471 // get the total button size (by default the same as bitmap size)
472 virtual wxSize
GetToolSize() const
473 { return GetToolBitmapSize(); }
475 // returns a (non separator) tool containing the point (x, y) or NULL if
476 // there is no tool at this point (corrdinates are client)
477 virtual wxToolBarToolBase
*FindToolForPosition(wxCoord x
,
478 wxCoord y
) const = 0;
480 // find the tool by id
481 wxToolBarToolBase
*FindById(int toolid
) const;
483 // return true if this is a vertical toolbar, otherwise false
484 bool IsVertical() const;
486 // these methods allow to access tools by their index in the toolbar
487 size_t GetToolsCount() const { return m_tools
.GetCount(); }
488 const wxToolBarToolBase
*GetToolByPos(int pos
) const { return m_tools
[pos
]; }
490 #if WXWIN_COMPATIBILITY_2_8
491 // the old versions of the various methods kept for compatibility
492 // don't use in the new code!
493 // --------------------------------------------------------------
495 wxToolBarToolBase
*AddTool(int toolid
,
496 const wxBitmap
& bitmap
,
497 const wxBitmap
& bmpDisabled
,
499 wxObject
*clientData
= NULL
,
500 const wxString
& shortHelpString
= wxEmptyString
,
501 const wxString
& longHelpString
= wxEmptyString
)
503 return AddTool(toolid
, wxEmptyString
,
505 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
506 shortHelpString
, longHelpString
, clientData
);
509 wxToolBarToolBase
*AddTool(int toolid
,
510 const wxBitmap
& bitmap
,
511 const wxString
& shortHelpString
= wxEmptyString
,
512 const wxString
& longHelpString
= wxEmptyString
)
514 return AddTool(toolid
, wxEmptyString
,
515 bitmap
, wxNullBitmap
, wxITEM_NORMAL
,
516 shortHelpString
, longHelpString
, NULL
);
519 wxToolBarToolBase
*AddTool(int toolid
,
520 const wxBitmap
& bitmap
,
521 const wxBitmap
& bmpDisabled
,
524 wxCoord yPos
= wxDefaultCoord
,
525 wxObject
*clientData
= NULL
,
526 const wxString
& shortHelp
= wxEmptyString
,
527 const wxString
& longHelp
= wxEmptyString
)
529 return DoAddTool(toolid
, wxEmptyString
, bitmap
, bmpDisabled
,
530 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
531 shortHelp
, longHelp
, clientData
, xPos
, yPos
);
534 wxToolBarToolBase
*InsertTool(size_t pos
,
536 const wxBitmap
& bitmap
,
537 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
539 wxObject
*clientData
= NULL
,
540 const wxString
& shortHelp
= wxEmptyString
,
541 const wxString
& longHelp
= wxEmptyString
)
543 return InsertTool(pos
, toolid
, wxEmptyString
, bitmap
, bmpDisabled
,
544 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
545 shortHelp
, longHelp
, clientData
);
547 #endif // WXWIN_COMPATIBILITY_2_8
552 // NB: these functions are deprecated, use EVT_TOOL_XXX() instead!
554 // Only allow toggle if returns true. Call when left button up.
555 virtual bool OnLeftClick(int toolid
, bool toggleDown
);
557 // Call when right button down.
558 virtual void OnRightClick(int toolid
, long x
, long y
);
560 // Called when the mouse cursor enters a tool bitmap.
561 // Argument is wxID_ANY if mouse is exiting the toolbar.
562 virtual void OnMouseEnter(int toolid
);
564 // more deprecated functions
565 // -------------------------
567 // use GetToolMargins() instead
568 wxSize
GetMargins() const { return GetToolMargins(); }
570 // implementation only from now on
571 // -------------------------------
573 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
574 virtual void UpdateWindowUI(long flags
= wxUPDATE_UI_NONE
) ;
576 // don't want toolbars to accept the focus
577 virtual bool AcceptsFocus() const { return false; }
581 bool SetDropdownMenu(int toolid
, wxMenu
*menu
);
585 // to implement in derived classes
586 // -------------------------------
588 // create a new toolbar tool and add it to the toolbar, this is typically
589 // implemented by just calling InsertTool()
590 virtual wxToolBarToolBase
*DoAddTool
593 const wxString
& label
,
594 const wxBitmap
& bitmap
,
595 const wxBitmap
& bmpDisabled
,
597 const wxString
& shortHelp
= wxEmptyString
,
598 const wxString
& longHelp
= wxEmptyString
,
599 wxObject
*clientData
= NULL
,
600 wxCoord xPos
= wxDefaultCoord
,
601 wxCoord yPos
= wxDefaultCoord
604 // the tool is not yet inserted into m_tools list when this function is
605 // called and will only be added to it if this function succeeds
606 virtual bool DoInsertTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
608 // the tool is still in m_tools list when this function is called, it will
609 // only be deleted from it if it succeeds
610 virtual bool DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
612 // called when the tools enabled flag changes
613 virtual void DoEnableTool(wxToolBarToolBase
*tool
, bool enable
) = 0;
615 // called when the tool is toggled
616 virtual void DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
) = 0;
618 // called when the tools "can be toggled" flag changes
619 virtual void DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
) = 0;
621 // the functions to create toolbar tools
622 virtual wxToolBarToolBase
*CreateTool(int toolid
,
623 const wxString
& label
,
624 const wxBitmap
& bmpNormal
,
625 const wxBitmap
& bmpDisabled
,
627 wxObject
*clientData
,
628 const wxString
& shortHelp
,
629 const wxString
& longHelp
) = 0;
631 virtual wxToolBarToolBase
*CreateTool(wxControl
*control
,
632 const wxString
& label
) = 0;
634 // this one is not virtual but just a simple helper/wrapper around
635 // CreateTool() for separators
636 wxToolBarToolBase
*CreateSeparator()
638 return CreateTool(wxID_SEPARATOR
,
640 wxNullBitmap
, wxNullBitmap
,
641 wxITEM_SEPARATOR
, NULL
,
642 wxEmptyString
, wxEmptyString
);
648 // call this from derived class ctor/Create() to ensure that we have either
649 // wxTB_HORIZONTAL or wxTB_VERTICAL style, there is a lot of existing code
650 // which randomly checks either one or the other of them and gets confused
651 // if neither is set (and making one of them 0 is not an option neither as
652 // then the existing tests would break down)
655 // un-toggle all buttons in the same radio group
656 void UnToggleRadioGroup(wxToolBarToolBase
*tool
);
658 // make the size of the buttons big enough to fit the largest bitmap size
659 void AdjustToolBitmapSize();
661 // calls InsertTool() and deletes the tool if inserting it failed
662 wxToolBarToolBase
*DoInsertNewTool(size_t pos
, wxToolBarToolBase
*tool
)
664 if ( !InsertTool(pos
, tool
) )
673 // the list of all our tools
674 wxToolBarToolsList m_tools
;
676 // the offset of the first tool
680 // the maximum number of toolbar rows/columns
684 // the tool packing and separation
688 // the size of the toolbar bitmaps
689 wxCoord m_defaultWidth
, m_defaultHeight
;
692 DECLARE_EVENT_TABLE()
693 wxDECLARE_NO_COPY_CLASS(wxToolBarBase
);
696 // deprecated function for creating the image for disabled buttons, use
697 // wxImage::ConvertToGreyscale() instead
698 #if WXWIN_COMPATIBILITY_2_8
700 wxDEPRECATED( bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
) );
702 #endif // WXWIN_COMPATIBILITY_2_8
705 #endif // wxUSE_TOOLBAR