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
; }
202 // these methods are only for tools of wxITEM_DROPDOWN kind (but even such
203 // tools can have a NULL associated menu)
204 virtual void SetDropdownMenu(wxMenu
*menu
);
205 wxMenu
*GetDropdownMenu() const { return m_dropdownMenu
; }
208 // common part of all ctors
209 void Init(wxToolBarBase
*tbar
,
210 wxToolBarToolStyle style
,
221 m_stretchable
= false;
225 m_dropdownMenu
= NULL
;
228 wxToolBarBase
*m_tbar
; // the toolbar to which we belong (may be NULL)
231 wxToolBarToolStyle m_toolStyle
;
232 wxWindowIDRef m_id
; // the tool id, wxID_SEPARATOR for separator
233 wxItemKind m_kind
; // for normal buttons may be wxITEM_NORMAL/CHECK/RADIO
235 // as controls have their own client data, no need to waste memory
238 wxObject
*m_clientData
;
239 wxControl
*m_control
;
242 // true if this tool is stretchable: currently is only value for separators
249 // normal and disabled bitmaps for the tool, both can be invalid
250 wxBitmap m_bmpNormal
;
251 wxBitmap m_bmpDisabled
;
256 // short and long help strings
257 wxString m_shortHelpString
;
258 wxString m_longHelpString
;
260 wxMenu
*m_dropdownMenu
;
262 DECLARE_DYNAMIC_CLASS_NO_COPY(wxToolBarToolBase
)
265 // a list of toolbar tools
266 WX_DECLARE_EXPORTED_LIST(wxToolBarToolBase
, wxToolBarToolsList
);
268 // ----------------------------------------------------------------------------
269 // the base class for all toolbars
270 // ----------------------------------------------------------------------------
272 class WXDLLIMPEXP_CORE wxToolBarBase
: public wxControl
276 virtual ~wxToolBarBase();
278 // toolbar construction
279 // --------------------
281 // the full AddTool() function
283 // If bmpDisabled is wxNullBitmap, a shadowed version of the normal bitmap
284 // is created and used as the disabled image.
285 wxToolBarToolBase
*AddTool(int toolid
,
286 const wxString
& label
,
287 const wxBitmap
& bitmap
,
288 const wxBitmap
& bmpDisabled
,
289 wxItemKind kind
= wxITEM_NORMAL
,
290 const wxString
& shortHelp
= wxEmptyString
,
291 const wxString
& longHelp
= wxEmptyString
,
292 wxObject
*data
= NULL
)
294 return DoAddTool(toolid
, label
, bitmap
, bmpDisabled
, kind
,
295 shortHelp
, longHelp
, data
);
298 // the most common AddTool() version
299 wxToolBarToolBase
*AddTool(int toolid
,
300 const wxString
& label
,
301 const wxBitmap
& bitmap
,
302 const wxString
& shortHelp
= wxEmptyString
,
303 wxItemKind kind
= wxITEM_NORMAL
)
305 return AddTool(toolid
, label
, bitmap
, wxNullBitmap
, kind
, shortHelp
);
308 // add a check tool, i.e. a tool which can be toggled
309 wxToolBarToolBase
*AddCheckTool(int toolid
,
310 const wxString
& label
,
311 const wxBitmap
& bitmap
,
312 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
313 const wxString
& shortHelp
= wxEmptyString
,
314 const wxString
& longHelp
= wxEmptyString
,
315 wxObject
*data
= NULL
)
317 return AddTool(toolid
, label
, bitmap
, bmpDisabled
, wxITEM_CHECK
,
318 shortHelp
, longHelp
, data
);
321 // add a radio tool, i.e. a tool which can be toggled and releases any
322 // other toggled radio tools in the same group when it happens
323 wxToolBarToolBase
*AddRadioTool(int toolid
,
324 const wxString
& label
,
325 const wxBitmap
& bitmap
,
326 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
327 const wxString
& shortHelp
= wxEmptyString
,
328 const wxString
& longHelp
= wxEmptyString
,
329 wxObject
*data
= NULL
)
331 return AddTool(toolid
, label
, bitmap
, bmpDisabled
, wxITEM_RADIO
,
332 shortHelp
, longHelp
, data
);
336 // insert the new tool at the given position, if pos == GetToolsCount(), it
337 // is equivalent to AddTool()
338 virtual wxToolBarToolBase
*InsertTool
342 const wxString
& label
,
343 const wxBitmap
& bitmap
,
344 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
345 wxItemKind kind
= wxITEM_NORMAL
,
346 const wxString
& shortHelp
= wxEmptyString
,
347 const wxString
& longHelp
= wxEmptyString
,
348 wxObject
*clientData
= NULL
351 virtual wxToolBarToolBase
*AddTool (wxToolBarToolBase
*tool
);
352 virtual wxToolBarToolBase
*InsertTool (size_t pos
, wxToolBarToolBase
*tool
);
354 // add an arbitrary control to the toolbar (notice that the control will be
355 // deleted by the toolbar and that it will also adjust its position/size)
357 // the label is optional and, if specified, will be shown near the control
358 // NB: the control should have toolbar as its parent
359 virtual wxToolBarToolBase
*
360 AddControl(wxControl
*control
, const wxString
& label
= wxEmptyString
);
362 virtual wxToolBarToolBase
*
363 InsertControl(size_t pos
, wxControl
*control
,
364 const wxString
& label
= wxEmptyString
);
366 // get the control with the given id or return NULL
367 virtual wxControl
*FindControl( int toolid
);
369 // add a separator to the toolbar
370 virtual wxToolBarToolBase
*AddSeparator();
371 virtual wxToolBarToolBase
*InsertSeparator(size_t pos
);
373 // add a stretchable space to the toolbar: this is similar to a separator
374 // except that it's always blank and that all the extra space the toolbar
375 // has is [equally] distributed among the stretchable spaces in it
376 virtual wxToolBarToolBase
*AddStretchableSpace();
377 virtual wxToolBarToolBase
*InsertStretchableSpace(size_t pos
);
379 // remove the tool from the toolbar: the caller is responsible for actually
380 // deleting the pointer
381 virtual wxToolBarToolBase
*RemoveTool(int toolid
);
383 // delete tool either by index or by position
384 virtual bool DeleteToolByPos(size_t pos
);
385 virtual bool DeleteTool(int toolid
);
388 virtual void ClearTools();
390 // must be called after all buttons have been created to finish toolbar
393 // derived class versions should call the base one first, before doing
394 // platform-specific stuff
395 virtual bool Realize();
400 virtual void EnableTool(int toolid
, bool enable
);
401 virtual void ToggleTool(int toolid
, bool toggle
);
403 // Set this to be togglable (or not)
404 virtual void SetToggle(int toolid
, bool toggle
);
406 // set/get tools client data (not for controls)
407 virtual wxObject
*GetToolClientData(int toolid
) const;
408 virtual void SetToolClientData(int toolid
, wxObject
*clientData
);
410 // returns tool pos, or wxNOT_FOUND if tool isn't found
411 virtual int GetToolPos(int id
) const;
413 // return true if the tool is toggled
414 virtual bool GetToolState(int toolid
) const;
416 virtual bool GetToolEnabled(int toolid
) const;
418 virtual void SetToolShortHelp(int toolid
, const wxString
& helpString
);
419 virtual wxString
GetToolShortHelp(int toolid
) const;
420 virtual void SetToolLongHelp(int toolid
, const wxString
& helpString
);
421 virtual wxString
GetToolLongHelp(int toolid
) const;
423 virtual void SetToolNormalBitmap(int WXUNUSED(id
),
424 const wxBitmap
& WXUNUSED(bitmap
)) {}
425 virtual void SetToolDisabledBitmap(int WXUNUSED(id
),
426 const wxBitmap
& WXUNUSED(bitmap
)) {}
429 // margins/packing/separation
430 // --------------------------
432 virtual void SetMargins(int x
, int y
);
433 void SetMargins(const wxSize
& size
)
434 { SetMargins((int) size
.x
, (int) size
.y
); }
435 virtual void SetToolPacking(int packing
)
436 { m_toolPacking
= packing
; }
437 virtual void SetToolSeparation(int separation
)
438 { m_toolSeparation
= separation
; }
440 virtual wxSize
GetToolMargins() const { return wxSize(m_xMargin
, m_yMargin
); }
441 virtual int GetToolPacking() const { return m_toolPacking
; }
442 virtual int GetToolSeparation() const { return m_toolSeparation
; }
447 // set the number of toolbar rows
448 virtual void SetRows(int nRows
);
450 // the toolbar can wrap - limit the number of columns or rows it may take
451 void SetMaxRowsCols(int rows
, int cols
)
452 { m_maxRows
= rows
; m_maxCols
= cols
; }
453 int GetMaxRows() const { return m_maxRows
; }
454 int GetMaxCols() const { return m_maxCols
; }
456 // get/set the size of the bitmaps used by the toolbar: should be called
457 // before adding any tools to the toolbar
458 virtual void SetToolBitmapSize(const wxSize
& size
)
459 { m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
; }
460 virtual wxSize
GetToolBitmapSize() const
461 { return wxSize(m_defaultWidth
, m_defaultHeight
); }
463 // the button size in some implementations is bigger than the bitmap size:
464 // get the total button size (by default the same as bitmap size)
465 virtual wxSize
GetToolSize() const
466 { return GetToolBitmapSize(); }
468 // returns a (non separator) tool containing the point (x, y) or NULL if
469 // there is no tool at this point (corrdinates are client)
470 virtual wxToolBarToolBase
*FindToolForPosition(wxCoord x
,
471 wxCoord y
) const = 0;
473 // find the tool by id
474 wxToolBarToolBase
*FindById(int toolid
) const;
476 // return true if this is a vertical toolbar, otherwise false
477 bool IsVertical() const;
479 // these methods allow to access tools by their index in the toolbar
480 size_t GetToolsCount() const { return m_tools
.GetCount(); }
481 const wxToolBarToolBase
*GetToolByPos(int pos
) const { return m_tools
[pos
]; }
483 #if WXWIN_COMPATIBILITY_2_8
484 // the old versions of the various methods kept for compatibility
485 // don't use in the new code!
486 // --------------------------------------------------------------
488 wxToolBarToolBase
*AddTool(int toolid
,
489 const wxBitmap
& bitmap
,
490 const wxBitmap
& bmpDisabled
,
492 wxObject
*clientData
= NULL
,
493 const wxString
& shortHelpString
= wxEmptyString
,
494 const wxString
& longHelpString
= wxEmptyString
)
496 return AddTool(toolid
, wxEmptyString
,
498 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
499 shortHelpString
, longHelpString
, clientData
);
502 wxToolBarToolBase
*AddTool(int toolid
,
503 const wxBitmap
& bitmap
,
504 const wxString
& shortHelpString
= wxEmptyString
,
505 const wxString
& longHelpString
= wxEmptyString
)
507 return AddTool(toolid
, wxEmptyString
,
508 bitmap
, wxNullBitmap
, wxITEM_NORMAL
,
509 shortHelpString
, longHelpString
, NULL
);
512 wxToolBarToolBase
*AddTool(int toolid
,
513 const wxBitmap
& bitmap
,
514 const wxBitmap
& bmpDisabled
,
517 wxCoord yPos
= wxDefaultCoord
,
518 wxObject
*clientData
= NULL
,
519 const wxString
& shortHelp
= wxEmptyString
,
520 const wxString
& longHelp
= wxEmptyString
)
522 return DoAddTool(toolid
, wxEmptyString
, bitmap
, bmpDisabled
,
523 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
524 shortHelp
, longHelp
, clientData
, xPos
, yPos
);
527 wxToolBarToolBase
*InsertTool(size_t pos
,
529 const wxBitmap
& bitmap
,
530 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
532 wxObject
*clientData
= NULL
,
533 const wxString
& shortHelp
= wxEmptyString
,
534 const wxString
& longHelp
= wxEmptyString
)
536 return InsertTool(pos
, toolid
, wxEmptyString
, bitmap
, bmpDisabled
,
537 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
538 shortHelp
, longHelp
, clientData
);
540 #endif // WXWIN_COMPATIBILITY_2_8
545 // NB: these functions are deprecated, use EVT_TOOL_XXX() instead!
547 // Only allow toggle if returns true. Call when left button up.
548 virtual bool OnLeftClick(int toolid
, bool toggleDown
);
550 // Call when right button down.
551 virtual void OnRightClick(int toolid
, long x
, long y
);
553 // Called when the mouse cursor enters a tool bitmap.
554 // Argument is wxID_ANY if mouse is exiting the toolbar.
555 virtual void OnMouseEnter(int toolid
);
557 // more deprecated functions
558 // -------------------------
560 // use GetToolMargins() instead
561 wxSize
GetMargins() const { return GetToolMargins(); }
563 // implementation only from now on
564 // -------------------------------
566 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
567 virtual void UpdateWindowUI(long flags
= wxUPDATE_UI_NONE
) ;
569 // don't want toolbars to accept the focus
570 virtual bool AcceptsFocus() const { return false; }
573 bool SetDropdownMenu(int toolid
, wxMenu
*menu
);
576 // to implement in derived classes
577 // -------------------------------
579 // create a new toolbar tool and add it to the toolbar, this is typically
580 // implemented by just calling InsertTool()
581 virtual wxToolBarToolBase
*DoAddTool
584 const wxString
& label
,
585 const wxBitmap
& bitmap
,
586 const wxBitmap
& bmpDisabled
,
588 const wxString
& shortHelp
= wxEmptyString
,
589 const wxString
& longHelp
= wxEmptyString
,
590 wxObject
*clientData
= NULL
,
591 wxCoord xPos
= wxDefaultCoord
,
592 wxCoord yPos
= wxDefaultCoord
595 // the tool is not yet inserted into m_tools list when this function is
596 // called and will only be added to it if this function succeeds
597 virtual bool DoInsertTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
599 // the tool is still in m_tools list when this function is called, it will
600 // only be deleted from it if it succeeds
601 virtual bool DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
603 // called when the tools enabled flag changes
604 virtual void DoEnableTool(wxToolBarToolBase
*tool
, bool enable
) = 0;
606 // called when the tool is toggled
607 virtual void DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
) = 0;
609 // called when the tools "can be toggled" flag changes
610 virtual void DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
) = 0;
612 // the functions to create toolbar tools
613 virtual wxToolBarToolBase
*CreateTool(int toolid
,
614 const wxString
& label
,
615 const wxBitmap
& bmpNormal
,
616 const wxBitmap
& bmpDisabled
,
618 wxObject
*clientData
,
619 const wxString
& shortHelp
,
620 const wxString
& longHelp
) = 0;
622 virtual wxToolBarToolBase
*CreateTool(wxControl
*control
,
623 const wxString
& label
) = 0;
625 // this one is not virtual but just a simple helper/wrapper around
626 // CreateTool() for separators
627 wxToolBarToolBase
*CreateSeparator()
629 return CreateTool(wxID_SEPARATOR
,
631 wxNullBitmap
, wxNullBitmap
,
632 wxITEM_SEPARATOR
, NULL
,
633 wxEmptyString
, wxEmptyString
);
639 // call this from derived class ctor/Create() to ensure that we have either
640 // wxTB_HORIZONTAL or wxTB_VERTICAL style, there is a lot of existing code
641 // which randomly checks either one or the other of them and gets confused
642 // if neither is set (and making one of them 0 is not an option neither as
643 // then the existing tests would break down)
646 // un-toggle all buttons in the same radio group
647 void UnToggleRadioGroup(wxToolBarToolBase
*tool
);
649 // make the size of the buttons big enough to fit the largest bitmap size
650 void AdjustToolBitmapSize();
652 // calls InsertTool() and deletes the tool if inserting it failed
653 wxToolBarToolBase
*DoInsertNewTool(size_t pos
, wxToolBarToolBase
*tool
)
655 if ( !InsertTool(pos
, tool
) )
664 // the list of all our tools
665 wxToolBarToolsList m_tools
;
667 // the offset of the first tool
671 // the maximum number of toolbar rows/columns
675 // the tool packing and separation
679 // the size of the toolbar bitmaps
680 wxCoord m_defaultWidth
, m_defaultHeight
;
683 DECLARE_EVENT_TABLE()
684 wxDECLARE_NO_COPY_CLASS(wxToolBarBase
);
687 // deprecated function for creating the image for disabled buttons, use
688 // wxImage::ConvertToGreyscale() instead
689 #if WXWIN_COMPATIBILITY_2_8
691 wxDEPRECATED( bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
) );
693 #endif // WXWIN_COMPATIBILITY_2_8
696 #endif // wxUSE_TOOLBAR