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 // ----------------------------------------------------------------------------
19 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma interface "tbarbase.h"
27 #include "wx/bitmap.h"
29 #include "wx/control.h"
31 class WXDLLEXPORT wxToolBarBase
;
32 class WXDLLEXPORT wxToolBarToolBase
;
33 class WXDLLEXPORT wxImage
;
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 extern WXDLLEXPORT_DATA(const wxChar
*) wxToolBarNameStr
;
40 extern WXDLLEXPORT_DATA(const wxSize
) wxDefaultSize
;
41 extern WXDLLEXPORT_DATA(const wxPoint
) wxDefaultPosition
;
43 enum wxToolBarToolStyle
45 wxTOOL_STYLE_BUTTON
= 1,
46 wxTOOL_STYLE_SEPARATOR
= 2,
50 // ----------------------------------------------------------------------------
51 // wxToolBarTool is a toolbar element.
53 // It has a unique id (except for the separators which always have id wxID_ANY), 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 // ----------------------------------------------------------------------------
61 class WXDLLEXPORT wxToolBarToolBase
: public wxObject
67 wxToolBarToolBase(wxToolBarBase
*tbar
= (wxToolBarBase
*)NULL
,
68 int toolid
= 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
)
77 m_shortHelpString(shortHelpString
),
78 m_longHelpString(longHelpString
)
84 m_clientData
= clientData
;
86 m_bmpNormal
= bmpNormal
;
87 m_bmpDisabled
= bmpDisabled
;
94 m_toolStyle
= toolid
== wxID_SEPARATOR
? wxTOOL_STYLE_SEPARATOR
95 : wxTOOL_STYLE_BUTTON
;
98 wxToolBarToolBase(wxToolBarBase
*tbar
, wxControl
*control
)
102 m_id
= control
->GetId();
104 m_kind
= wxITEM_MAX
; // invalid value
109 m_toolStyle
= wxTOOL_STYLE_CONTROL
;
112 ~wxToolBarToolBase(){}
118 int GetId() const { return m_id
; }
120 wxControl
*GetControl() const
122 wxASSERT_MSG( IsControl(), _T("this toolbar tool is not a control") );
127 wxToolBarBase
*GetToolBar() const { return m_tbar
; }
130 bool IsButton() const { return m_toolStyle
== wxTOOL_STYLE_BUTTON
; }
131 bool IsControl() const { return m_toolStyle
== wxTOOL_STYLE_CONTROL
; }
132 bool IsSeparator() const { return m_toolStyle
== wxTOOL_STYLE_SEPARATOR
; }
133 int GetStyle() const { return m_toolStyle
; }
134 wxItemKind
GetKind() const
136 wxASSERT_MSG( IsButton(), _T("only makes sense for buttons") );
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 wxString
GetLabel() const { return m_label
; }
156 wxString
GetShortHelp() const { return m_shortHelpString
; }
157 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 bool Enable(bool enable
);
173 bool Toggle(bool toggle
);
174 bool SetToggle(bool toggle
);
175 bool SetShortHelp(const wxString
& help
);
176 bool SetLongHelp(const wxString
& help
);
178 void Toggle() { Toggle(!IsToggled()); }
180 void SetNormalBitmap(const wxBitmap
& bmp
) { m_bmpNormal
= bmp
; }
181 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
= (wxToolBarBase
*)NULL
; }
199 virtual void Attach(wxToolBarBase
*tbar
) { m_tbar
= tbar
; }
201 // compatibility only, don't use
202 #if WXWIN_COMPATIBILITY_2_2
203 wxDEPRECATED( const wxBitmap
& GetBitmap1() const );
204 wxDEPRECATED( const wxBitmap
& GetBitmap2() const );
206 wxDEPRECATED( void SetBitmap1(const wxBitmap
& bmp
) );
207 wxDEPRECATED( void SetBitmap2(const wxBitmap
& bmp
) );
208 #endif // WXWIN_COMPATIBILITY_2_2
211 wxToolBarBase
*m_tbar
; // the toolbar to which we belong (may be NULL)
214 int m_toolStyle
; // see enum wxToolBarToolStyle
215 int m_id
; // the tool id, wxID_SEPARATOR for separator
216 wxItemKind m_kind
; // for normal buttons may be wxITEM_NORMAL/CHECK/RADIO
218 // as controls have their own client data, no need to waste memory
221 wxObject
*m_clientData
;
222 wxControl
*m_control
;
229 // normal and disabled bitmaps for the tool, both can be invalid
230 wxBitmap m_bmpNormal
;
231 wxBitmap m_bmpDisabled
;
236 // short and long help strings
237 wxString m_shortHelpString
;
238 wxString m_longHelpString
;
240 DECLARE_DYNAMIC_CLASS_NO_COPY(wxToolBarToolBase
)
243 // a list of toolbar tools
244 WX_DECLARE_EXPORTED_LIST(wxToolBarToolBase
, wxToolBarToolsList
);
246 // ----------------------------------------------------------------------------
247 // the base class for all toolbars
248 // ----------------------------------------------------------------------------
250 class WXDLLEXPORT wxToolBarBase
: public wxControl
254 virtual ~wxToolBarBase();
256 // toolbar construction
257 // --------------------
259 // the full AddTool() function
261 // If bmpDisabled is wxNullBitmap, a shadowed version of the normal bitmap
262 // is created and used as the disabled image.
263 wxToolBarToolBase
*AddTool(int toolid
,
264 const wxString
& label
,
265 const wxBitmap
& bitmap
,
266 const wxBitmap
& bmpDisabled
,
267 wxItemKind kind
= wxITEM_NORMAL
,
268 const wxString
& shortHelp
= wxEmptyString
,
269 const wxString
& longHelp
= wxEmptyString
,
270 wxObject
*data
= NULL
)
272 return DoAddTool(toolid
, label
, bitmap
, bmpDisabled
, kind
,
273 shortHelp
, longHelp
, data
);
276 // the most common AddTool() version
277 wxToolBarToolBase
*AddTool(int toolid
,
278 const wxString
& label
,
279 const wxBitmap
& bitmap
,
280 const wxString
& shortHelp
= wxEmptyString
,
281 wxItemKind kind
= wxITEM_NORMAL
)
283 return AddTool(toolid
, label
, bitmap
, wxNullBitmap
, kind
, shortHelp
);
286 // add a check tool, i.e. a tool which can be toggled
287 wxToolBarToolBase
*AddCheckTool(int toolid
,
288 const wxString
& label
,
289 const wxBitmap
& bitmap
,
290 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
291 const wxString
& shortHelp
= wxEmptyString
,
292 const wxString
& longHelp
= wxEmptyString
,
293 wxObject
*data
= NULL
)
295 return AddTool(toolid
, label
, bitmap
, bmpDisabled
, wxITEM_CHECK
,
296 shortHelp
, longHelp
, data
);
299 // add a radio tool, i.e. a tool which can be toggled and releases any
300 // other toggled radio tools in the same group when it happens
301 wxToolBarToolBase
*AddRadioTool(int toolid
,
302 const wxString
& label
,
303 const wxBitmap
& bitmap
,
304 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
305 const wxString
& shortHelp
= wxEmptyString
,
306 const wxString
& longHelp
= wxEmptyString
,
307 wxObject
*data
= NULL
)
309 return AddTool(toolid
, label
, bitmap
, bmpDisabled
, wxITEM_RADIO
,
310 shortHelp
, longHelp
, data
);
314 // insert the new tool at the given position, if pos == GetToolsCount(), it
315 // is equivalent to AddTool()
316 virtual wxToolBarToolBase
*InsertTool
320 const wxString
& label
,
321 const wxBitmap
& bitmap
,
322 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
323 wxItemKind kind
= wxITEM_NORMAL
,
324 const wxString
& shortHelp
= wxEmptyString
,
325 const wxString
& longHelp
= wxEmptyString
,
326 wxObject
*clientData
= NULL
329 virtual wxToolBarToolBase
*AddTool (wxToolBarToolBase
*tool
);
330 virtual wxToolBarToolBase
*InsertTool (size_t pos
, wxToolBarToolBase
*tool
);
332 // add an arbitrary control to the toolbar (notice that
333 // the control will be deleted by the toolbar and that it will also adjust
334 // its position/size)
336 // NB: the control should have toolbar as its parent
337 virtual wxToolBarToolBase
*AddControl(wxControl
*control
);
338 virtual wxToolBarToolBase
*InsertControl(size_t pos
, wxControl
*control
);
340 // get the control with the given id or return NULL
341 virtual wxControl
*FindControl( int toolid
);
343 // add a separator to the toolbar
344 virtual wxToolBarToolBase
*AddSeparator();
345 virtual wxToolBarToolBase
*InsertSeparator(size_t pos
);
347 // remove the tool from the toolbar: the caller is responsible for actually
348 // deleting the pointer
349 virtual wxToolBarToolBase
*RemoveTool(int toolid
);
351 // delete tool either by index or by position
352 virtual bool DeleteToolByPos(size_t pos
);
353 virtual bool DeleteTool(int toolid
);
356 virtual void ClearTools();
358 // must be called after all buttons have been created to finish toolbar
360 virtual bool Realize();
365 virtual void EnableTool(int toolid
, bool enable
);
366 virtual void ToggleTool(int toolid
, bool toggle
);
368 // Set this to be togglable (or not)
369 virtual void SetToggle(int toolid
, bool toggle
);
371 // set/get tools client data (not for controls)
372 virtual wxObject
*GetToolClientData(int toolid
) const;
373 virtual void SetToolClientData(int toolid
, wxObject
*clientData
);
375 // returns tool pos, or wxNOT_FOUND if tool isn't found
376 virtual int GetToolPos(int id
) const;
378 // return true if the tool is toggled
379 virtual bool GetToolState(int toolid
) const;
381 virtual bool GetToolEnabled(int toolid
) const;
383 virtual void SetToolShortHelp(int toolid
, const wxString
& helpString
);
384 virtual wxString
GetToolShortHelp(int toolid
) const;
385 virtual void SetToolLongHelp(int toolid
, const wxString
& helpString
);
386 virtual wxString
GetToolLongHelp(int toolid
) const;
388 // margins/packing/separation
389 // --------------------------
391 virtual void SetMargins(int x
, int y
);
392 void SetMargins(const wxSize
& size
)
393 { SetMargins((int) size
.x
, (int) size
.y
); }
394 virtual void SetToolPacking(int packing
)
395 { m_toolPacking
= packing
; }
396 virtual void SetToolSeparation(int separation
)
397 { m_toolSeparation
= separation
; }
399 virtual wxSize
GetToolMargins() const { return wxSize(m_xMargin
, m_yMargin
); }
400 virtual int GetToolPacking() const { return m_toolPacking
; }
401 virtual int GetToolSeparation() const { return m_toolSeparation
; }
406 // set the number of toolbar rows
407 virtual void SetRows(int nRows
);
409 // the toolbar can wrap - limit the number of columns or rows it may take
410 void SetMaxRowsCols(int rows
, int cols
)
411 { m_maxRows
= rows
; m_maxCols
= cols
; }
412 int GetMaxRows() const { return m_maxRows
; }
413 int GetMaxCols() const { return m_maxCols
; }
415 // get/set the size of the bitmaps used by the toolbar: should be called
416 // before adding any tools to the toolbar
417 virtual void SetToolBitmapSize(const wxSize
& size
)
418 { m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
; };
419 virtual wxSize
GetToolBitmapSize() const
420 { return wxSize(m_defaultWidth
, m_defaultHeight
); }
422 // the button size in some implementations is bigger than the bitmap size:
423 // get the total button size (by default the same as bitmap size)
424 virtual wxSize
GetToolSize() const
425 { return GetToolBitmapSize(); } ;
427 // returns a (non separator) tool containing the point (x, y) or NULL if
428 // there is no tool at this point (corrdinates are client)
429 virtual wxToolBarToolBase
*FindToolForPosition(wxCoord x
,
430 wxCoord y
) const = 0;
432 // find the tool by id
433 wxToolBarToolBase
*FindById(int toolid
) const;
435 // return true if this is a vertical toolbar, otherwise false
436 bool IsVertical() const { return HasFlag(wxTB_VERTICAL
); }
439 // the old versions of the various methods kept for compatibility
440 // don't use in the new code!
441 // --------------------------------------------------------------
443 wxToolBarToolBase
*AddTool(int toolid
,
444 const wxBitmap
& bitmap
,
445 const wxBitmap
& bmpDisabled
,
447 wxObject
*clientData
= NULL
,
448 const wxString
& shortHelpString
= wxEmptyString
,
449 const wxString
& longHelpString
= wxEmptyString
)
451 return AddTool(toolid
, wxEmptyString
,
453 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
454 shortHelpString
, longHelpString
, clientData
);
457 wxToolBarToolBase
*AddTool(int toolid
,
458 const wxBitmap
& bitmap
,
459 const wxString
& shortHelpString
= wxEmptyString
,
460 const wxString
& longHelpString
= wxEmptyString
)
462 return AddTool(toolid
, wxEmptyString
,
463 bitmap
, wxNullBitmap
, wxITEM_NORMAL
,
464 shortHelpString
, longHelpString
, NULL
);
467 wxToolBarToolBase
*AddTool(int toolid
,
468 const wxBitmap
& bitmap
,
469 const wxBitmap
& bmpDisabled
,
472 wxCoord yPos
= wxDefaultCoord
,
473 wxObject
*clientData
= NULL
,
474 const wxString
& shortHelp
= wxEmptyString
,
475 const wxString
& longHelp
= wxEmptyString
)
477 return DoAddTool(toolid
, wxEmptyString
, bitmap
, bmpDisabled
,
478 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
479 shortHelp
, longHelp
, clientData
, xPos
, yPos
);
482 wxToolBarToolBase
*InsertTool(size_t pos
,
484 const wxBitmap
& bitmap
,
485 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
487 wxObject
*clientData
= NULL
,
488 const wxString
& shortHelp
= wxEmptyString
,
489 const wxString
& longHelp
= wxEmptyString
)
491 return InsertTool(pos
, toolid
, wxEmptyString
, bitmap
, bmpDisabled
,
492 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
493 shortHelp
, longHelp
, clientData
);
499 // NB: these functions are deprecated, use EVT_TOOL_XXX() instead!
501 // Only allow toggle if returns true. Call when left button up.
502 virtual bool OnLeftClick(int toolid
, bool toggleDown
);
504 // Call when right button down.
505 virtual void OnRightClick(int toolid
, long x
, long y
);
507 // Called when the mouse cursor enters a tool bitmap.
508 // Argument is wxID_ANY if mouse is exiting the toolbar.
509 virtual void OnMouseEnter(int toolid
);
511 // more deprecated functions
512 // -------------------------
514 // use GetToolMargins() instead
515 wxSize
GetMargins() const { return GetToolMargins(); }
517 // implementation only from now on
518 // -------------------------------
520 size_t GetToolsCount() const { return m_tools
.GetCount(); }
522 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
523 virtual void UpdateWindowUI(long flags
= wxUPDATE_UI_NONE
) ;
525 // don't want toolbars to accept the focus
526 virtual bool AcceptsFocus() const { return false; }
529 // to implement in derived classes
530 // -------------------------------
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
537 const wxString
& label
,
538 const wxBitmap
& bitmap
,
539 const wxBitmap
& bmpDisabled
,
541 const wxString
& shortHelp
= wxEmptyString
,
542 const wxString
& longHelp
= wxEmptyString
,
543 wxObject
*clientData
= NULL
,
544 wxCoord xPos
= wxDefaultCoord
,
545 wxCoord yPos
= wxDefaultCoord
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;
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;
556 // called when the tools enabled flag changes
557 virtual void DoEnableTool(wxToolBarToolBase
*tool
, bool enable
) = 0;
559 // called when the tool is toggled
560 virtual void DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
) = 0;
562 // called when the tools "can be toggled" flag changes
563 virtual void DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
) = 0;
565 // the functions to create toolbar tools
566 virtual wxToolBarToolBase
*CreateTool(int toolid
,
567 const wxString
& label
,
568 const wxBitmap
& bmpNormal
,
569 const wxBitmap
& bmpDisabled
,
571 wxObject
*clientData
,
572 const wxString
& shortHelp
,
573 const wxString
& longHelp
) = 0;
575 virtual wxToolBarToolBase
*CreateTool(wxControl
*control
) = 0;
580 // un-toggle all buttons in the same radio group
581 void UnToggleRadioGroup(wxToolBarToolBase
*tool
);
583 // the list of all our tools
584 wxToolBarToolsList m_tools
;
586 // the offset of the first tool
590 // the maximum number of toolbar rows/columns
594 // the tool packing and separation
598 // the size of the toolbar bitmaps
599 wxCoord m_defaultWidth
, m_defaultHeight
;
602 DECLARE_EVENT_TABLE()
603 DECLARE_NO_COPY_CLASS(wxToolBarBase
)
606 // Helper function for creating the image for disabled buttons
607 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
) ;
609 #endif // wxUSE_TOOLBAR