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 WXDLLEXPORT wxToolBarBase
;
28 class WXDLLEXPORT wxToolBarToolBase
;
29 class WXDLLEXPORT wxImage
;
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 extern WXDLLEXPORT_DATA(const wxChar
*) wxToolBarNameStr
;
36 extern WXDLLEXPORT_DATA(const wxSize
) wxDefaultSize
;
37 extern WXDLLEXPORT_DATA(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 WXDLLEXPORT wxToolBarToolBase
: public wxObject
63 wxToolBarToolBase(wxToolBarBase
*tbar
= (wxToolBarBase
*)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
= (wxObject
*) NULL
,
70 const wxString
& shortHelpString
= wxEmptyString
,
71 const wxString
& longHelpString
= wxEmptyString
)
73 m_shortHelpString(shortHelpString
),
74 m_longHelpString(longHelpString
)
80 m_clientData
= clientData
;
82 m_bmpNormal
= bmpNormal
;
83 m_bmpDisabled
= bmpDisabled
;
90 m_toolStyle
= toolid
== wxID_SEPARATOR
? wxTOOL_STYLE_SEPARATOR
91 : wxTOOL_STYLE_BUTTON
;
94 wxToolBarToolBase(wxToolBarBase
*tbar
, wxControl
*control
)
98 m_id
= control
->GetId();
100 m_kind
= wxITEM_MAX
; // invalid value
105 m_toolStyle
= wxTOOL_STYLE_CONTROL
;
108 ~wxToolBarToolBase(){}
114 int GetId() const { return m_id
; }
116 wxControl
*GetControl() const
118 wxASSERT_MSG( IsControl(), _T("this toolbar tool is not a control") );
123 wxToolBarBase
*GetToolBar() const { return m_tbar
; }
126 bool IsButton() const { return m_toolStyle
== wxTOOL_STYLE_BUTTON
; }
127 bool IsControl() const { return m_toolStyle
== wxTOOL_STYLE_CONTROL
; }
128 bool IsSeparator() const { return m_toolStyle
== wxTOOL_STYLE_SEPARATOR
; }
129 int GetStyle() const { return m_toolStyle
; }
130 wxItemKind
GetKind() const
132 wxASSERT_MSG( IsButton(), _T("only makes sense for buttons") );
138 bool IsEnabled() const { return m_enabled
; }
139 bool IsToggled() const { return m_toggled
; }
140 bool CanBeToggled() const
141 { return m_kind
== wxITEM_CHECK
|| m_kind
== wxITEM_RADIO
; }
144 const wxBitmap
& GetNormalBitmap() const { return m_bmpNormal
; }
145 const wxBitmap
& GetDisabledBitmap() const { return m_bmpDisabled
; }
147 const wxBitmap
& GetBitmap() const
148 { return IsEnabled() ? GetNormalBitmap() : GetDisabledBitmap(); }
150 const wxString
& GetLabel() const { return m_label
; }
152 const wxString
& GetShortHelp() const { return m_shortHelpString
; }
153 const wxString
& GetLongHelp() const { return m_longHelpString
; }
155 wxObject
*GetClientData() const
157 if ( m_toolStyle
== wxTOOL_STYLE_CONTROL
)
159 return (wxObject
*)m_control
->GetClientData();
167 // modifiers: return true if the state really changed
168 bool Enable(bool enable
);
169 bool Toggle(bool toggle
);
170 bool SetToggle(bool toggle
);
171 bool SetShortHelp(const wxString
& help
);
172 bool SetLongHelp(const wxString
& help
);
174 void Toggle() { Toggle(!IsToggled()); }
176 void SetNormalBitmap(const wxBitmap
& bmp
) { m_bmpNormal
= bmp
; }
177 void SetDisabledBitmap(const wxBitmap
& bmp
) { m_bmpDisabled
= bmp
; }
179 virtual void SetLabel(const wxString
& label
) { m_label
= label
; }
181 void SetClientData(wxObject
*clientData
)
183 if ( m_toolStyle
== wxTOOL_STYLE_CONTROL
)
185 m_control
->SetClientData(clientData
);
189 m_clientData
= clientData
;
193 // add tool to/remove it from a toolbar
194 virtual void Detach() { m_tbar
= (wxToolBarBase
*)NULL
; }
195 virtual void Attach(wxToolBarBase
*tbar
) { m_tbar
= tbar
; }
197 // compatibility only, don't use
198 #if WXWIN_COMPATIBILITY_2_2
199 wxDEPRECATED( const wxBitmap
& GetBitmap1() const );
200 wxDEPRECATED( const wxBitmap
& GetBitmap2() const );
202 wxDEPRECATED( void SetBitmap1(const wxBitmap
& bmp
) );
203 wxDEPRECATED( void SetBitmap2(const wxBitmap
& bmp
) );
204 #endif // WXWIN_COMPATIBILITY_2_2
207 wxToolBarBase
*m_tbar
; // the toolbar to which we belong (may be NULL)
210 int m_toolStyle
; // see enum wxToolBarToolStyle
211 int m_id
; // the tool id, wxID_SEPARATOR for separator
212 wxItemKind m_kind
; // for normal buttons may be wxITEM_NORMAL/CHECK/RADIO
214 // as controls have their own client data, no need to waste memory
217 wxObject
*m_clientData
;
218 wxControl
*m_control
;
225 // normal and disabled bitmaps for the tool, both can be invalid
226 wxBitmap m_bmpNormal
;
227 wxBitmap m_bmpDisabled
;
232 // short and long help strings
233 wxString m_shortHelpString
;
234 wxString m_longHelpString
;
236 DECLARE_DYNAMIC_CLASS_NO_COPY(wxToolBarToolBase
)
239 // a list of toolbar tools
240 WX_DECLARE_EXPORTED_LIST(wxToolBarToolBase
, wxToolBarToolsList
);
242 // ----------------------------------------------------------------------------
243 // the base class for all toolbars
244 // ----------------------------------------------------------------------------
246 class WXDLLEXPORT wxToolBarBase
: public wxControl
250 virtual ~wxToolBarBase();
252 // toolbar construction
253 // --------------------
255 // the full AddTool() function
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 toolid
,
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
)
268 return DoAddTool(toolid
, label
, bitmap
, bmpDisabled
, kind
,
269 shortHelp
, longHelp
, data
);
272 // the most common AddTool() version
273 wxToolBarToolBase
*AddTool(int toolid
,
274 const wxString
& label
,
275 const wxBitmap
& bitmap
,
276 const wxString
& shortHelp
= wxEmptyString
,
277 wxItemKind kind
= wxITEM_NORMAL
)
279 return AddTool(toolid
, label
, bitmap
, wxNullBitmap
, kind
, shortHelp
);
282 // add a check tool, i.e. a tool which can be toggled
283 wxToolBarToolBase
*AddCheckTool(int toolid
,
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
)
291 return AddTool(toolid
, label
, bitmap
, bmpDisabled
, wxITEM_CHECK
,
292 shortHelp
, longHelp
, data
);
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 toolid
,
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
)
305 return AddTool(toolid
, label
, bitmap
, bmpDisabled
, wxITEM_RADIO
,
306 shortHelp
, longHelp
, data
);
310 // insert the new tool at the given position, if pos == GetToolsCount(), it
311 // is equivalent to AddTool()
312 virtual wxToolBarToolBase
*InsertTool
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
325 virtual wxToolBarToolBase
*AddTool (wxToolBarToolBase
*tool
);
326 virtual wxToolBarToolBase
*InsertTool (size_t pos
, wxToolBarToolBase
*tool
);
328 // add an arbitrary control to the toolbar (notice that
329 // the control will be deleted by the toolbar and that it will also adjust
330 // its position/size)
332 // NB: the control should have toolbar as its parent
333 virtual wxToolBarToolBase
*AddControl(wxControl
*control
);
334 virtual wxToolBarToolBase
*InsertControl(size_t pos
, wxControl
*control
);
336 // get the control with the given id or return NULL
337 virtual wxControl
*FindControl( int toolid
);
339 // add a separator to the toolbar
340 virtual wxToolBarToolBase
*AddSeparator();
341 virtual wxToolBarToolBase
*InsertSeparator(size_t pos
);
343 // remove the tool from the toolbar: the caller is responsible for actually
344 // deleting the pointer
345 virtual wxToolBarToolBase
*RemoveTool(int toolid
);
347 // delete tool either by index or by position
348 virtual bool DeleteToolByPos(size_t pos
);
349 virtual bool DeleteTool(int toolid
);
352 virtual void ClearTools();
354 // must be called after all buttons have been created to finish toolbar
356 virtual bool Realize();
361 virtual void EnableTool(int toolid
, bool enable
);
362 virtual void ToggleTool(int toolid
, bool toggle
);
364 // Set this to be togglable (or not)
365 virtual void SetToggle(int toolid
, bool toggle
);
367 // set/get tools client data (not for controls)
368 virtual wxObject
*GetToolClientData(int toolid
) const;
369 virtual void SetToolClientData(int toolid
, wxObject
*clientData
);
371 // returns tool pos, or wxNOT_FOUND if tool isn't found
372 virtual int GetToolPos(int id
) const;
374 // return true if the tool is toggled
375 virtual bool GetToolState(int toolid
) const;
377 virtual bool GetToolEnabled(int toolid
) const;
379 virtual void SetToolShortHelp(int toolid
, const wxString
& helpString
);
380 virtual wxString
GetToolShortHelp(int toolid
) const;
381 virtual void SetToolLongHelp(int toolid
, const wxString
& helpString
);
382 virtual wxString
GetToolLongHelp(int toolid
) const;
384 // margins/packing/separation
385 // --------------------------
387 virtual void SetMargins(int x
, int y
);
388 void SetMargins(const wxSize
& size
)
389 { SetMargins((int) size
.x
, (int) size
.y
); }
390 virtual void SetToolPacking(int packing
)
391 { m_toolPacking
= packing
; }
392 virtual void SetToolSeparation(int separation
)
393 { m_toolSeparation
= separation
; }
395 virtual wxSize
GetToolMargins() const { return wxSize(m_xMargin
, m_yMargin
); }
396 virtual int GetToolPacking() const { return m_toolPacking
; }
397 virtual int GetToolSeparation() const { return m_toolSeparation
; }
402 // set the number of toolbar rows
403 virtual void SetRows(int nRows
);
405 // the toolbar can wrap - limit the number of columns or rows it may take
406 void SetMaxRowsCols(int rows
, int cols
)
407 { m_maxRows
= rows
; m_maxCols
= cols
; }
408 int GetMaxRows() const { return m_maxRows
; }
409 int GetMaxCols() const { return m_maxCols
; }
411 // get/set the size of the bitmaps used by the toolbar: should be called
412 // before adding any tools to the toolbar
413 virtual void SetToolBitmapSize(const wxSize
& size
)
414 { m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
; };
415 virtual wxSize
GetToolBitmapSize() const
416 { return wxSize(m_defaultWidth
, m_defaultHeight
); }
418 // the button size in some implementations is bigger than the bitmap size:
419 // get the total button size (by default the same as bitmap size)
420 virtual wxSize
GetToolSize() const
421 { return GetToolBitmapSize(); } ;
423 // returns a (non separator) tool containing the point (x, y) or NULL if
424 // there is no tool at this point (corrdinates are client)
425 virtual wxToolBarToolBase
*FindToolForPosition(wxCoord x
,
426 wxCoord y
) const = 0;
428 // find the tool by id
429 wxToolBarToolBase
*FindById(int toolid
) const;
431 // return true if this is a vertical toolbar, otherwise false
432 bool IsVertical() const { return HasFlag(wxTB_VERTICAL
); }
435 // the old versions of the various methods kept for compatibility
436 // don't use in the new code!
437 // --------------------------------------------------------------
439 wxToolBarToolBase
*AddTool(int toolid
,
440 const wxBitmap
& bitmap
,
441 const wxBitmap
& bmpDisabled
,
443 wxObject
*clientData
= NULL
,
444 const wxString
& shortHelpString
= wxEmptyString
,
445 const wxString
& longHelpString
= wxEmptyString
)
447 return AddTool(toolid
, wxEmptyString
,
449 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
450 shortHelpString
, longHelpString
, clientData
);
453 wxToolBarToolBase
*AddTool(int toolid
,
454 const wxBitmap
& bitmap
,
455 const wxString
& shortHelpString
= wxEmptyString
,
456 const wxString
& longHelpString
= wxEmptyString
)
458 return AddTool(toolid
, wxEmptyString
,
459 bitmap
, wxNullBitmap
, wxITEM_NORMAL
,
460 shortHelpString
, longHelpString
, NULL
);
463 wxToolBarToolBase
*AddTool(int toolid
,
464 const wxBitmap
& bitmap
,
465 const wxBitmap
& bmpDisabled
,
468 wxCoord yPos
= wxDefaultCoord
,
469 wxObject
*clientData
= NULL
,
470 const wxString
& shortHelp
= wxEmptyString
,
471 const wxString
& longHelp
= wxEmptyString
)
473 return DoAddTool(toolid
, wxEmptyString
, bitmap
, bmpDisabled
,
474 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
475 shortHelp
, longHelp
, clientData
, xPos
, yPos
);
478 wxToolBarToolBase
*InsertTool(size_t pos
,
480 const wxBitmap
& bitmap
,
481 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
483 wxObject
*clientData
= NULL
,
484 const wxString
& shortHelp
= wxEmptyString
,
485 const wxString
& longHelp
= wxEmptyString
)
487 return InsertTool(pos
, toolid
, wxEmptyString
, bitmap
, bmpDisabled
,
488 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
489 shortHelp
, longHelp
, clientData
);
495 // NB: these functions are deprecated, use EVT_TOOL_XXX() instead!
497 // Only allow toggle if returns true. Call when left button up.
498 virtual bool OnLeftClick(int toolid
, bool toggleDown
);
500 // Call when right button down.
501 virtual void OnRightClick(int toolid
, long x
, long y
);
503 // Called when the mouse cursor enters a tool bitmap.
504 // Argument is wxID_ANY if mouse is exiting the toolbar.
505 virtual void OnMouseEnter(int toolid
);
507 // more deprecated functions
508 // -------------------------
510 // use GetToolMargins() instead
511 wxSize
GetMargins() const { return GetToolMargins(); }
513 // implementation only from now on
514 // -------------------------------
516 size_t GetToolsCount() const { return m_tools
.GetCount(); }
518 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
519 virtual void UpdateWindowUI(long flags
= wxUPDATE_UI_NONE
) ;
521 // don't want toolbars to accept the focus
522 virtual bool AcceptsFocus() const { return false; }
525 // to implement in derived classes
526 // -------------------------------
528 // create a new toolbar tool and add it to the toolbar, this is typically
529 // implemented by just calling InsertTool()
530 virtual wxToolBarToolBase
*DoAddTool
533 const wxString
& label
,
534 const wxBitmap
& bitmap
,
535 const wxBitmap
& bmpDisabled
,
537 const wxString
& shortHelp
= wxEmptyString
,
538 const wxString
& longHelp
= wxEmptyString
,
539 wxObject
*clientData
= NULL
,
540 wxCoord xPos
= wxDefaultCoord
,
541 wxCoord yPos
= wxDefaultCoord
544 // the tool is not yet inserted into m_tools list when this function is
545 // called and will only be added to it if this function succeeds
546 virtual bool DoInsertTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
548 // the tool is still in m_tools list when this function is called, it will
549 // only be deleted from it if it succeeds
550 virtual bool DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
552 // called when the tools enabled flag changes
553 virtual void DoEnableTool(wxToolBarToolBase
*tool
, bool enable
) = 0;
555 // called when the tool is toggled
556 virtual void DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
) = 0;
558 // called when the tools "can be toggled" flag changes
559 virtual void DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
) = 0;
561 // the functions to create toolbar tools
562 virtual wxToolBarToolBase
*CreateTool(int toolid
,
563 const wxString
& label
,
564 const wxBitmap
& bmpNormal
,
565 const wxBitmap
& bmpDisabled
,
567 wxObject
*clientData
,
568 const wxString
& shortHelp
,
569 const wxString
& longHelp
) = 0;
571 virtual wxToolBarToolBase
*CreateTool(wxControl
*control
) = 0;
576 // un-toggle all buttons in the same radio group
577 void UnToggleRadioGroup(wxToolBarToolBase
*tool
);
579 // the list of all our tools
580 wxToolBarToolsList m_tools
;
582 // the offset of the first tool
586 // the maximum number of toolbar rows/columns
590 // the tool packing and separation
594 // the size of the toolbar bitmaps
595 wxCoord m_defaultWidth
, m_defaultHeight
;
598 DECLARE_EVENT_TABLE()
599 DECLARE_NO_COPY_CLASS(wxToolBarBase
)
602 // Helper function for creating the image for disabled buttons
603 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
) ;
605 #endif // wxUSE_TOOLBAR