1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Base class for toolbar classes
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_TBARBASE_H_
12 #define _WX_TBARBASE_H_
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
22 #include "wx/bitmap.h"
24 #include "wx/control.h"
26 class WXDLLIMPEXP_FWD_CORE wxToolBarBase
;
27 class WXDLLIMPEXP_FWD_CORE wxToolBarToolBase
;
28 class WXDLLIMPEXP_FWD_CORE wxImage
;
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 extern WXDLLIMPEXP_DATA_CORE(const char) wxToolBarNameStr
[];
35 extern WXDLLIMPEXP_DATA_CORE(const wxSize
) wxDefaultSize
;
36 extern WXDLLIMPEXP_DATA_CORE(const wxPoint
) wxDefaultPosition
;
38 enum wxToolBarToolStyle
40 wxTOOL_STYLE_BUTTON
= 1,
41 wxTOOL_STYLE_SEPARATOR
= 2,
45 // ----------------------------------------------------------------------------
46 // wxToolBarTool is a toolbar element.
48 // It has a unique id (except for the separators which always have id wxID_ANY), the
49 // style (telling whether it is a normal button, separator or a control), the
50 // state (toggled or not, enabled or not) and short and long help strings. The
51 // default implementations use the short help string for the tooltip text which
52 // is popped up when the mouse pointer enters the tool and the long help string
53 // for the applications status bar.
54 // ----------------------------------------------------------------------------
56 class WXDLLIMPEXP_CORE wxToolBarToolBase
: public wxObject
62 // generic ctor for any kind of tool
63 wxToolBarToolBase(wxToolBarBase
*tbar
= NULL
,
64 int toolid
= wxID_SEPARATOR
,
65 const wxString
& label
= wxEmptyString
,
66 const wxBitmap
& bmpNormal
= wxNullBitmap
,
67 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
68 wxItemKind kind
= wxITEM_NORMAL
,
69 wxObject
*clientData
= NULL
,
70 const wxString
& shortHelpString
= wxEmptyString
,
71 const wxString
& longHelpString
= wxEmptyString
)
73 m_shortHelpString(shortHelpString
),
74 m_longHelpString(longHelpString
)
79 toolid
== wxID_SEPARATOR
? wxTOOL_STYLE_SEPARATOR
80 : wxTOOL_STYLE_BUTTON
,
81 toolid
== wxID_ANY
? wxWindow::NewControlId()
86 m_clientData
= clientData
;
88 m_bmpNormal
= bmpNormal
;
89 m_bmpDisabled
= bmpDisabled
;
92 // ctor for controls only
93 wxToolBarToolBase(wxToolBarBase
*tbar
,
95 const wxString
& label
)
98 Init(tbar
, wxTOOL_STYLE_CONTROL
, control
->GetId(), wxITEM_MAX
);
103 virtual ~wxToolBarToolBase();
109 int GetId() const { return m_id
; }
111 wxControl
*GetControl() const
113 wxASSERT_MSG( IsControl(), wxT("this toolbar tool is not a control") );
118 wxToolBarBase
*GetToolBar() const { return m_tbar
; }
121 bool IsStretchable() const { return m_stretchable
; }
122 bool IsButton() const { return m_toolStyle
== wxTOOL_STYLE_BUTTON
; }
123 bool IsControl() const { return m_toolStyle
== wxTOOL_STYLE_CONTROL
; }
124 bool IsSeparator() const { return m_toolStyle
== wxTOOL_STYLE_SEPARATOR
; }
125 bool IsStretchableSpace() const { return IsSeparator() && IsStretchable(); }
126 int GetStyle() const { return m_toolStyle
; }
127 wxItemKind
GetKind() const
129 wxASSERT_MSG( IsButton(), wxT("only makes sense for buttons") );
134 void MakeStretchable()
136 wxASSERT_MSG( IsSeparator(), "only separators can be stretchable" );
138 m_stretchable
= true;
142 bool IsEnabled() const { return m_enabled
; }
143 bool IsToggled() const { return m_toggled
; }
144 bool CanBeToggled() const
145 { return m_kind
== wxITEM_CHECK
|| m_kind
== wxITEM_RADIO
; }
148 const wxBitmap
& GetNormalBitmap() const { return m_bmpNormal
; }
149 const wxBitmap
& GetDisabledBitmap() const { return m_bmpDisabled
; }
151 const wxBitmap
& GetBitmap() const
152 { return IsEnabled() ? GetNormalBitmap() : GetDisabledBitmap(); }
154 const wxString
& GetLabel() const { return m_label
; }
156 const wxString
& GetShortHelp() const { return m_shortHelpString
; }
157 const wxString
& GetLongHelp() const { return m_longHelpString
; }
159 wxObject
*GetClientData() const
161 if ( m_toolStyle
== wxTOOL_STYLE_CONTROL
)
163 return (wxObject
*)m_control
->GetClientData();
171 // modifiers: return true if the state really changed
172 virtual bool Enable(bool enable
);
173 virtual bool Toggle(bool toggle
);
174 virtual bool SetToggle(bool toggle
);
175 virtual bool SetShortHelp(const wxString
& help
);
176 virtual bool SetLongHelp(const wxString
& help
);
178 void Toggle() { Toggle(!IsToggled()); }
180 virtual void SetNormalBitmap(const wxBitmap
& bmp
) { m_bmpNormal
= bmp
; }
181 virtual void SetDisabledBitmap(const wxBitmap
& bmp
) { m_bmpDisabled
= bmp
; }
183 virtual void SetLabel(const wxString
& label
) { m_label
= label
; }
185 void SetClientData(wxObject
*clientData
)
187 if ( m_toolStyle
== wxTOOL_STYLE_CONTROL
)
189 m_control
->SetClientData(clientData
);
193 m_clientData
= clientData
;
197 // add tool to/remove it from a toolbar
198 virtual void Detach() { m_tbar
= NULL
; }
199 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
; }
209 // common part of all ctors
210 void Init(wxToolBarBase
*tbar
,
211 wxToolBarToolStyle style
,
222 m_stretchable
= false;
227 m_dropdownMenu
= NULL
;
232 wxToolBarBase
*m_tbar
; // the toolbar to which we belong (may be NULL)
235 wxToolBarToolStyle m_toolStyle
;
236 wxWindowIDRef m_id
; // the tool id, wxID_SEPARATOR for separator
237 wxItemKind m_kind
; // for normal buttons may be wxITEM_NORMAL/CHECK/RADIO
239 // as controls have their own client data, no need to waste memory
242 wxObject
*m_clientData
;
243 wxControl
*m_control
;
246 // true if this tool is stretchable: currently is only value for separators
253 // normal and disabled bitmaps for the tool, both can be invalid
254 wxBitmap m_bmpNormal
;
255 wxBitmap m_bmpDisabled
;
260 // short and long help strings
261 wxString m_shortHelpString
;
262 wxString m_longHelpString
;
265 wxMenu
*m_dropdownMenu
;
268 DECLARE_DYNAMIC_CLASS_NO_COPY(wxToolBarToolBase
)
271 // a list of toolbar tools
272 WX_DECLARE_EXPORTED_LIST(wxToolBarToolBase
, wxToolBarToolsList
);
274 // ----------------------------------------------------------------------------
275 // the base class for all toolbars
276 // ----------------------------------------------------------------------------
278 class WXDLLIMPEXP_CORE wxToolBarBase
: public wxControl
282 virtual ~wxToolBarBase();
284 // toolbar construction
285 // --------------------
287 // the full AddTool() function
289 // If bmpDisabled is wxNullBitmap, a shadowed version of the normal bitmap
290 // is created and used as the disabled image.
291 wxToolBarToolBase
*AddTool(int toolid
,
292 const wxString
& label
,
293 const wxBitmap
& bitmap
,
294 const wxBitmap
& bmpDisabled
,
295 wxItemKind kind
= wxITEM_NORMAL
,
296 const wxString
& shortHelp
= wxEmptyString
,
297 const wxString
& longHelp
= wxEmptyString
,
298 wxObject
*data
= NULL
)
300 return DoAddTool(toolid
, label
, bitmap
, bmpDisabled
, kind
,
301 shortHelp
, longHelp
, data
);
304 // the most common AddTool() version
305 wxToolBarToolBase
*AddTool(int toolid
,
306 const wxString
& label
,
307 const wxBitmap
& bitmap
,
308 const wxString
& shortHelp
= wxEmptyString
,
309 wxItemKind kind
= wxITEM_NORMAL
)
311 return AddTool(toolid
, label
, bitmap
, wxNullBitmap
, kind
, shortHelp
);
314 // add a check tool, i.e. a tool which can be toggled
315 wxToolBarToolBase
*AddCheckTool(int toolid
,
316 const wxString
& label
,
317 const wxBitmap
& bitmap
,
318 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
319 const wxString
& shortHelp
= wxEmptyString
,
320 const wxString
& longHelp
= wxEmptyString
,
321 wxObject
*data
= NULL
)
323 return AddTool(toolid
, label
, bitmap
, bmpDisabled
, wxITEM_CHECK
,
324 shortHelp
, longHelp
, data
);
327 // add a radio tool, i.e. a tool which can be toggled and releases any
328 // other toggled radio tools in the same group when it happens
329 wxToolBarToolBase
*AddRadioTool(int toolid
,
330 const wxString
& label
,
331 const wxBitmap
& bitmap
,
332 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
333 const wxString
& shortHelp
= wxEmptyString
,
334 const wxString
& longHelp
= wxEmptyString
,
335 wxObject
*data
= NULL
)
337 return AddTool(toolid
, label
, bitmap
, bmpDisabled
, wxITEM_RADIO
,
338 shortHelp
, longHelp
, data
);
342 // insert the new tool at the given position, if pos == GetToolsCount(), it
343 // is equivalent to AddTool()
344 virtual wxToolBarToolBase
*InsertTool
348 const wxString
& label
,
349 const wxBitmap
& bitmap
,
350 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
351 wxItemKind kind
= wxITEM_NORMAL
,
352 const wxString
& shortHelp
= wxEmptyString
,
353 const wxString
& longHelp
= wxEmptyString
,
354 wxObject
*clientData
= NULL
357 virtual wxToolBarToolBase
*AddTool (wxToolBarToolBase
*tool
);
358 virtual wxToolBarToolBase
*InsertTool (size_t pos
, wxToolBarToolBase
*tool
);
360 // add an arbitrary control to the toolbar (notice that the control will be
361 // deleted by the toolbar and that it will also adjust its position/size)
363 // the label is optional and, if specified, will be shown near the control
364 // NB: the control should have toolbar as its parent
365 virtual wxToolBarToolBase
*
366 AddControl(wxControl
*control
, const wxString
& label
= wxEmptyString
);
368 virtual wxToolBarToolBase
*
369 InsertControl(size_t pos
, wxControl
*control
,
370 const wxString
& label
= wxEmptyString
);
372 // get the control with the given id or return NULL
373 virtual wxControl
*FindControl( int toolid
);
375 // add a separator to the toolbar
376 virtual wxToolBarToolBase
*AddSeparator();
377 virtual wxToolBarToolBase
*InsertSeparator(size_t pos
);
379 // add a stretchable space to the toolbar: this is similar to a separator
380 // except that it's always blank and that all the extra space the toolbar
381 // has is [equally] distributed among the stretchable spaces in it
382 virtual wxToolBarToolBase
*AddStretchableSpace();
383 virtual wxToolBarToolBase
*InsertStretchableSpace(size_t pos
);
385 // remove the tool from the toolbar: the caller is responsible for actually
386 // deleting the pointer
387 virtual wxToolBarToolBase
*RemoveTool(int toolid
);
389 // delete tool either by index or by position
390 virtual bool DeleteToolByPos(size_t pos
);
391 virtual bool DeleteTool(int toolid
);
394 virtual void ClearTools();
396 // must be called after all buttons have been created to finish toolbar
399 // derived class versions should call the base one first, before doing
400 // platform-specific stuff
401 virtual bool Realize();
406 virtual void EnableTool(int toolid
, bool enable
);
407 virtual void ToggleTool(int toolid
, bool toggle
);
409 // Set this to be togglable (or not)
410 virtual void SetToggle(int toolid
, bool toggle
);
412 // set/get tools client data (not for controls)
413 virtual wxObject
*GetToolClientData(int toolid
) const;
414 virtual void SetToolClientData(int toolid
, wxObject
*clientData
);
416 // returns tool pos, or wxNOT_FOUND if tool isn't found
417 virtual int GetToolPos(int id
) const;
419 // return true if the tool is toggled
420 virtual bool GetToolState(int toolid
) const;
422 virtual bool GetToolEnabled(int toolid
) const;
424 virtual void SetToolShortHelp(int toolid
, const wxString
& helpString
);
425 virtual wxString
GetToolShortHelp(int toolid
) const;
426 virtual void SetToolLongHelp(int toolid
, const wxString
& helpString
);
427 virtual wxString
GetToolLongHelp(int toolid
) const;
429 virtual void SetToolNormalBitmap(int WXUNUSED(id
),
430 const wxBitmap
& WXUNUSED(bitmap
)) {}
431 virtual void SetToolDisabledBitmap(int WXUNUSED(id
),
432 const wxBitmap
& WXUNUSED(bitmap
)) {}
435 // margins/packing/separation
436 // --------------------------
438 virtual void SetMargins(int x
, int y
);
439 void SetMargins(const wxSize
& size
)
440 { SetMargins((int) size
.x
, (int) size
.y
); }
441 virtual void SetToolPacking(int packing
)
442 { m_toolPacking
= packing
; }
443 virtual void SetToolSeparation(int separation
)
444 { m_toolSeparation
= separation
; }
446 virtual wxSize
GetToolMargins() const { return wxSize(m_xMargin
, m_yMargin
); }
447 virtual int GetToolPacking() const { return m_toolPacking
; }
448 virtual int GetToolSeparation() const { return m_toolSeparation
; }
453 // set the number of toolbar rows
454 virtual void SetRows(int nRows
);
456 // the toolbar can wrap - limit the number of columns or rows it may take
457 void SetMaxRowsCols(int rows
, int cols
)
458 { m_maxRows
= rows
; m_maxCols
= cols
; }
459 int GetMaxRows() const { return m_maxRows
; }
460 int GetMaxCols() const { return m_maxCols
; }
462 // get/set the size of the bitmaps used by the toolbar: should be called
463 // before adding any tools to the toolbar
464 virtual void SetToolBitmapSize(const wxSize
& size
)
465 { m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
; }
466 virtual wxSize
GetToolBitmapSize() const
467 { return wxSize(m_defaultWidth
, m_defaultHeight
); }
469 // the button size in some implementations is bigger than the bitmap size:
470 // get the total button size (by default the same as bitmap size)
471 virtual wxSize
GetToolSize() const
472 { return GetToolBitmapSize(); }
474 // returns a (non separator) tool containing the point (x, y) or NULL if
475 // there is no tool at this point (coordinates are client)
476 virtual wxToolBarToolBase
*FindToolForPosition(wxCoord x
,
477 wxCoord y
) const = 0;
479 // find the tool by id
480 wxToolBarToolBase
*FindById(int toolid
) const;
482 // return true if this is a vertical toolbar, otherwise false
483 bool IsVertical() const;
485 // these methods allow to access tools by their index in the toolbar
486 size_t GetToolsCount() const { return m_tools
.GetCount(); }
487 const wxToolBarToolBase
*GetToolByPos(int pos
) const { return m_tools
[pos
]; }
489 #if WXWIN_COMPATIBILITY_2_8
490 // the old versions of the various methods kept for compatibility
491 // don't use in the new code!
492 // --------------------------------------------------------------
494 wxToolBarToolBase
*AddTool(int toolid
,
495 const wxBitmap
& bitmap
,
496 const wxBitmap
& bmpDisabled
,
498 wxObject
*clientData
= NULL
,
499 const wxString
& shortHelpString
= wxEmptyString
,
500 const wxString
& longHelpString
= wxEmptyString
)
502 return AddTool(toolid
, wxEmptyString
,
504 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
505 shortHelpString
, longHelpString
, clientData
);
508 wxToolBarToolBase
*AddTool(int toolid
,
509 const wxBitmap
& bitmap
,
510 const wxString
& shortHelpString
= wxEmptyString
,
511 const wxString
& longHelpString
= wxEmptyString
)
513 return AddTool(toolid
, wxEmptyString
,
514 bitmap
, wxNullBitmap
, wxITEM_NORMAL
,
515 shortHelpString
, longHelpString
, NULL
);
518 wxToolBarToolBase
*AddTool(int toolid
,
519 const wxBitmap
& bitmap
,
520 const wxBitmap
& bmpDisabled
,
523 wxCoord yPos
= wxDefaultCoord
,
524 wxObject
*clientData
= NULL
,
525 const wxString
& shortHelp
= wxEmptyString
,
526 const wxString
& longHelp
= wxEmptyString
)
528 return DoAddTool(toolid
, wxEmptyString
, bitmap
, bmpDisabled
,
529 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
530 shortHelp
, longHelp
, clientData
, xPos
, yPos
);
533 wxToolBarToolBase
*InsertTool(size_t pos
,
535 const wxBitmap
& bitmap
,
536 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
538 wxObject
*clientData
= NULL
,
539 const wxString
& shortHelp
= wxEmptyString
,
540 const wxString
& longHelp
= wxEmptyString
)
542 return InsertTool(pos
, toolid
, wxEmptyString
, bitmap
, bmpDisabled
,
543 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
544 shortHelp
, longHelp
, clientData
);
546 #endif // WXWIN_COMPATIBILITY_2_8
551 // NB: these functions are deprecated, use EVT_TOOL_XXX() instead!
553 // Only allow toggle if returns true. Call when left button up.
554 virtual bool OnLeftClick(int toolid
, bool toggleDown
);
556 // Call when right button down.
557 virtual void OnRightClick(int toolid
, long x
, long y
);
559 // Called when the mouse cursor enters a tool bitmap.
560 // Argument is wxID_ANY if mouse is exiting the toolbar.
561 virtual void OnMouseEnter(int toolid
);
563 // more deprecated functions
564 // -------------------------
566 // use GetToolMargins() instead
567 wxSize
GetMargins() const { return GetToolMargins(); }
570 // helper functions to create toolbar tools
571 // -------------------------
572 virtual wxToolBarToolBase
*CreateTool(int toolid
,
573 const wxString
& label
,
574 const wxBitmap
& bmpNormal
,
575 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
576 wxItemKind kind
= wxITEM_NORMAL
,
577 wxObject
*clientData
= NULL
,
578 const wxString
& shortHelp
= wxEmptyString
,
579 const wxString
& longHelp
= wxEmptyString
) = 0;
581 virtual wxToolBarToolBase
*CreateTool(wxControl
*control
,
582 const wxString
& label
) = 0;
584 // this one is not virtual but just a simple helper/wrapper around
585 // CreateTool() for separators
586 wxToolBarToolBase
*CreateSeparator()
588 return CreateTool(wxID_SEPARATOR
,
590 wxNullBitmap
, wxNullBitmap
,
591 wxITEM_SEPARATOR
, NULL
,
592 wxEmptyString
, wxEmptyString
);
596 // implementation only from now on
597 // -------------------------------
599 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
600 virtual void UpdateWindowUI(long flags
= wxUPDATE_UI_NONE
) ;
602 // don't want toolbars to accept the focus
603 virtual bool AcceptsFocus() const { return false; }
607 bool SetDropdownMenu(int toolid
, wxMenu
*menu
);
611 // choose the default border for this window
612 virtual wxBorder
GetDefaultBorder() const { return wxBORDER_NONE
; }
614 // to implement in derived classes
615 // -------------------------------
617 // create a new toolbar tool and add it to the toolbar, this is typically
618 // implemented by just calling InsertTool()
619 virtual wxToolBarToolBase
*DoAddTool
622 const wxString
& label
,
623 const wxBitmap
& bitmap
,
624 const wxBitmap
& bmpDisabled
,
626 const wxString
& shortHelp
= wxEmptyString
,
627 const wxString
& longHelp
= wxEmptyString
,
628 wxObject
*clientData
= NULL
,
629 wxCoord xPos
= wxDefaultCoord
,
630 wxCoord yPos
= wxDefaultCoord
633 // the tool is not yet inserted into m_tools list when this function is
634 // called and will only be added to it if this function succeeds
635 virtual bool DoInsertTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
637 // the tool is still in m_tools list when this function is called, it will
638 // only be deleted from it if it succeeds
639 virtual bool DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
641 // called when the tools enabled flag changes
642 virtual void DoEnableTool(wxToolBarToolBase
*tool
, bool enable
) = 0;
644 // called when the tool is toggled
645 virtual void DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
) = 0;
647 // called when the tools "can be toggled" flag changes
648 virtual void DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
) = 0;
654 // call this from derived class ctor/Create() to ensure that we have either
655 // wxTB_HORIZONTAL or wxTB_VERTICAL style, there is a lot of existing code
656 // which randomly checks either one or the other of them and gets confused
657 // if neither is set (and making one of them 0 is not an option neither as
658 // then the existing tests would break down)
661 // un-toggle all buttons in the same radio group
662 void UnToggleRadioGroup(wxToolBarToolBase
*tool
);
664 // make the size of the buttons big enough to fit the largest bitmap size
665 void AdjustToolBitmapSize();
667 // calls InsertTool() and deletes the tool if inserting it failed
668 wxToolBarToolBase
*DoInsertNewTool(size_t pos
, wxToolBarToolBase
*tool
)
670 if ( !InsertTool(pos
, tool
) )
679 // the list of all our tools
680 wxToolBarToolsList m_tools
;
682 // the offset of the first tool
686 // the maximum number of toolbar rows/columns
690 // the tool packing and separation
694 // the size of the toolbar bitmaps
695 wxCoord m_defaultWidth
, m_defaultHeight
;
698 DECLARE_EVENT_TABLE()
699 wxDECLARE_NO_COPY_CLASS(wxToolBarBase
);
702 // deprecated function for creating the image for disabled buttons, use
703 // wxImage::ConvertToGreyscale() instead
704 #if WXWIN_COMPATIBILITY_2_8
706 wxDEPRECATED( bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
) );
708 #endif // WXWIN_COMPATIBILITY_2_8
711 #endif // wxUSE_TOOLBAR