1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Base class for toolbar classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TBARBASE_H_
13 #define _WX_TBARBASE_H_
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #if defined(__GNUG__) && !defined(__APPLE__)
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 WXDLLEXPORT_DATA(extern const wxChar
*) wxToolBarNameStr
;
40 WXDLLEXPORT_DATA(extern const wxSize
) wxDefaultSize
;
41 WXDLLEXPORT_DATA(extern 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 -1), 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 id
= 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
)
82 m_clientData
= clientData
;
84 m_bmpNormal
= bmpNormal
;
85 m_bmpDisabled
= bmpDisabled
;
92 m_toolStyle
= id
== wxID_SEPARATOR
? wxTOOL_STYLE_SEPARATOR
93 : wxTOOL_STYLE_BUTTON
;
96 wxToolBarToolBase(wxToolBarBase
*tbar
, wxControl
*control
)
100 m_id
= control
->GetId();
102 m_kind
= wxITEM_MAX
; // invalid value
107 m_toolStyle
= wxTOOL_STYLE_CONTROL
;
110 ~wxToolBarToolBase();
116 int GetId() const { return m_id
; }
118 wxControl
*GetControl() const
120 wxASSERT_MSG( IsControl(), _T("this toolbar tool is not a control") );
125 wxToolBarBase
*GetToolBar() const { return m_tbar
; }
128 bool IsButton() const { return m_toolStyle
== wxTOOL_STYLE_BUTTON
; }
129 bool IsControl() const { return m_toolStyle
== wxTOOL_STYLE_CONTROL
; }
130 bool IsSeparator() const { return m_toolStyle
== wxTOOL_STYLE_SEPARATOR
; }
131 int GetStyle() const { return m_toolStyle
; }
132 wxItemKind
GetKind() const
134 wxASSERT_MSG( IsButton(), _T("only makes sense for buttons") );
140 bool IsEnabled() const { return m_enabled
; }
141 bool IsToggled() const { return m_toggled
; }
142 bool CanBeToggled() const
143 { return m_kind
== wxITEM_CHECK
|| m_kind
== wxITEM_RADIO
; }
146 const wxBitmap
& GetNormalBitmap() const { return m_bmpNormal
; }
147 const wxBitmap
& GetDisabledBitmap() const { return m_bmpDisabled
; }
149 const wxBitmap
& GetBitmap() const
150 { return IsEnabled() ? GetNormalBitmap() : GetDisabledBitmap(); }
152 wxString
GetLabel() const { return m_label
; }
154 wxString
GetShortHelp() const { return m_shortHelpString
; }
155 wxString
GetLongHelp() const { return m_longHelpString
; }
157 wxObject
*GetClientData() const
159 if ( m_toolStyle
== wxTOOL_STYLE_CONTROL
)
161 return (wxObject
*)m_control
->GetClientData();
169 // modifiers: return TRUE if the state really changed
170 bool Enable(bool enable
);
171 bool Toggle(bool toggle
);
172 bool SetToggle(bool toggle
);
173 bool SetShortHelp(const wxString
& help
);
174 bool SetLongHelp(const wxString
& help
);
176 void Toggle() { Toggle(!IsToggled()); }
178 void SetNormalBitmap(const wxBitmap
& bmp
) { m_bmpNormal
= bmp
; }
179 void SetDisabledBitmap(const wxBitmap
& bmp
) { m_bmpDisabled
= bmp
; }
181 virtual void SetLabel(const wxString
& label
) { m_label
= label
; }
183 void SetClientData(wxObject
*clientData
)
185 if ( m_toolStyle
== wxTOOL_STYLE_CONTROL
)
187 m_control
->SetClientData(clientData
);
191 m_clientData
= clientData
;
195 // add tool to/remove it from a toolbar
196 virtual void Detach() { m_tbar
= (wxToolBarBase
*)NULL
; }
197 virtual void Attach(wxToolBarBase
*tbar
) { m_tbar
= tbar
; }
199 // compatibility only, don't use
200 #if WXWIN_COMPATIBILITY_2_2
201 const wxBitmap
& GetBitmap1() const { return GetNormalBitmap(); }
202 const wxBitmap
& GetBitmap2() const { return GetDisabledBitmap(); }
204 void SetBitmap1(const wxBitmap
& bmp
) { SetNormalBitmap(bmp
); }
205 void SetBitmap2(const wxBitmap
& bmp
) { SetDisabledBitmap(bmp
); }
206 #endif // WXWIN_COMPATIBILITY_2_2
209 wxToolBarBase
*m_tbar
; // the toolbar to which we belong (may be NULL)
212 int m_toolStyle
; // see enum wxToolBarToolStyle
213 int m_id
; // the tool id, wxID_SEPARATOR for separator
214 wxItemKind m_kind
; // for normal buttons may be wxITEM_NORMAL/CHECK/RADIO
216 // as controls have their own client data, no need to waste memory
219 wxObject
*m_clientData
;
220 wxControl
*m_control
;
227 // normal and disabled bitmaps for the tool, both can be invalid
228 wxBitmap m_bmpNormal
;
229 wxBitmap m_bmpDisabled
;
234 // short and long help strings
235 wxString m_shortHelpString
;
236 wxString m_longHelpString
;
238 DECLARE_NO_COPY_CLASS(wxToolBarToolBase
)
241 // a list of toolbar tools
242 WX_DECLARE_EXPORTED_LIST(wxToolBarToolBase
, wxToolBarToolsList
);
244 // ----------------------------------------------------------------------------
245 // the base class for all toolbars
246 // ----------------------------------------------------------------------------
248 class WXDLLEXPORT wxToolBarBase
: public wxControl
252 virtual ~wxToolBarBase();
254 // toolbar construction
255 // --------------------
257 // the full AddTool() function
259 // If bmpDisabled is wxNullBitmap, a shadowed version of the normal bitmap
260 // is created and used as the disabled image.
261 wxToolBarToolBase
*AddTool(int id
,
262 const wxString
& label
,
263 const wxBitmap
& bitmap
,
264 const wxBitmap
& bmpDisabled
,
265 wxItemKind kind
= wxITEM_NORMAL
,
266 const wxString
& shortHelp
= wxEmptyString
,
267 const wxString
& longHelp
= wxEmptyString
,
268 wxObject
*data
= NULL
)
270 return DoAddTool(id
, label
, bitmap
, bmpDisabled
, kind
,
271 shortHelp
, longHelp
, data
);
274 // the most common AddTool() version
275 wxToolBarToolBase
*AddTool(int id
,
276 const wxString
& label
,
277 const wxBitmap
& bitmap
,
278 const wxString
& shortHelp
= wxEmptyString
,
279 wxItemKind kind
= wxITEM_NORMAL
)
281 return AddTool(id
, label
, bitmap
, wxNullBitmap
, kind
, shortHelp
);
284 // add a check tool, i.e. a tool which can be toggled
285 wxToolBarToolBase
*AddCheckTool(int id
,
286 const wxString
& label
,
287 const wxBitmap
& bitmap
,
288 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
289 const wxString
& shortHelp
= wxEmptyString
,
290 const wxString
& longHelp
= wxEmptyString
,
291 wxObject
*data
= NULL
)
293 return AddTool(id
, label
, bitmap
, bmpDisabled
, wxITEM_CHECK
,
294 shortHelp
, longHelp
, data
);
297 // add a radio tool, i.e. a tool which can be toggled and releases any
298 // other toggled radio tools in the same group when it happens
299 wxToolBarToolBase
*AddRadioTool(int id
,
300 const wxString
& label
,
301 const wxBitmap
& bitmap
,
302 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
303 const wxString
& shortHelp
= wxEmptyString
,
304 const wxString
& longHelp
= wxEmptyString
,
305 wxObject
*data
= NULL
)
307 return AddTool(id
, label
, bitmap
, bmpDisabled
, wxITEM_RADIO
,
308 shortHelp
, longHelp
, data
);
312 // insert the new tool at the given position, if pos == GetToolsCount(), it
313 // is equivalent to AddTool()
314 virtual wxToolBarToolBase
*InsertTool
318 const wxString
& label
,
319 const wxBitmap
& bitmap
,
320 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
321 wxItemKind kind
= wxITEM_NORMAL
,
322 const wxString
& shortHelp
= wxEmptyString
,
323 const wxString
& longHelp
= wxEmptyString
,
324 wxObject
*clientData
= NULL
327 // add an arbitrary control to the toolbar, return TRUE if ok (notice that
328 // the control will be deleted by the toolbar and that it will also adjust
329 // its position/size)
331 // NB: the control should have toolbar as its parent
332 virtual wxToolBarToolBase
*AddControl(wxControl
*control
);
333 virtual wxToolBarToolBase
*InsertControl(size_t pos
, wxControl
*control
);
335 // get the control with the given id or return NULL
336 virtual wxControl
*FindControl( int id
);
338 // add a separator to the toolbar
339 virtual wxToolBarToolBase
*AddSeparator();
340 virtual wxToolBarToolBase
*InsertSeparator(size_t pos
);
342 // remove the tool from the toolbar: the caller is responsible for actually
343 // deleting the pointer
344 virtual wxToolBarToolBase
*RemoveTool(int id
);
346 // delete tool either by index or by position
347 virtual bool DeleteToolByPos(size_t pos
);
348 virtual bool DeleteTool(int id
);
351 virtual void ClearTools();
353 // must be called after all buttons have been created to finish toolbar
355 virtual bool Realize();
360 virtual void EnableTool(int id
, bool enable
);
361 virtual void ToggleTool(int id
, bool toggle
);
363 // Set this to be togglable (or not)
364 virtual void SetToggle(int id
, bool toggle
);
366 // set/get tools client data (not for controls)
367 virtual wxObject
*GetToolClientData(int id
) const;
368 virtual void SetToolClientData(int id
, wxObject
*clientData
);
370 // return TRUE if the tool is toggled
371 virtual bool GetToolState(int id
) const;
373 virtual bool GetToolEnabled(int id
) const;
375 virtual void SetToolShortHelp(int id
, const wxString
& helpString
);
376 virtual wxString
GetToolShortHelp(int id
) const;
377 virtual void SetToolLongHelp(int id
, const wxString
& helpString
);
378 virtual wxString
GetToolLongHelp(int id
) const;
380 // margins/packing/separation
381 // --------------------------
383 virtual void SetMargins(int x
, int y
);
384 void SetMargins(const wxSize
& size
)
385 { SetMargins((int) size
.x
, (int) size
.y
); }
386 virtual void SetToolPacking(int packing
)
387 { m_toolPacking
= packing
; }
388 virtual void SetToolSeparation(int separation
)
389 { m_toolSeparation
= separation
; }
391 virtual wxSize
GetToolMargins() const { return wxSize(m_xMargin
, m_yMargin
); }
392 virtual int GetToolPacking() const { return m_toolPacking
; }
393 virtual int GetToolSeparation() const { return m_toolSeparation
; }
398 // set the number of toolbar rows
399 virtual void SetRows(int nRows
);
401 // the toolbar can wrap - limit the number of columns or rows it may take
402 void SetMaxRowsCols(int rows
, int cols
)
403 { m_maxRows
= rows
; m_maxCols
= cols
; }
404 int GetMaxRows() const { return m_maxRows
; }
405 int GetMaxCols() const { return m_maxCols
; }
407 // get/set the size of the bitmaps used by the toolbar: should be called
408 // before adding any tools to the toolbar
409 virtual void SetToolBitmapSize(const wxSize
& size
)
410 { m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
; };
411 virtual wxSize
GetToolBitmapSize() const
412 { return wxSize(m_defaultWidth
, m_defaultHeight
); }
414 // the button size in some implementations is bigger than the bitmap size:
415 // get the total button size (by default the same as bitmap size)
416 virtual wxSize
GetToolSize() const
417 { return GetToolBitmapSize(); } ;
419 // returns a (non separator) tool containing the point (x, y) or NULL if
420 // there is no tool at this point (corrdinates are client)
421 virtual wxToolBarToolBase
*FindToolForPosition(wxCoord x
,
422 wxCoord y
) const = 0;
424 // return TRUE if this is a vertical toolbar, otherwise FALSE
425 bool IsVertical() const { return HasFlag(wxTB_VERTICAL
); }
428 // the old versions of the various methods kept for compatibility
429 // don't use in the new code!
430 // --------------------------------------------------------------
432 wxToolBarToolBase
*AddTool(int id
,
433 const wxBitmap
& bitmap
,
434 const wxBitmap
& bmpDisabled
,
436 wxObject
*clientData
= NULL
,
437 const wxString
& shortHelpString
= wxEmptyString
,
438 const wxString
& longHelpString
= wxEmptyString
)
440 return AddTool(id
, wxEmptyString
,
442 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
443 shortHelpString
, longHelpString
, clientData
);
446 wxToolBarToolBase
*AddTool(int id
,
447 const wxBitmap
& bitmap
,
448 const wxString
& shortHelpString
= wxEmptyString
,
449 const wxString
& longHelpString
= wxEmptyString
)
451 return AddTool(id
, wxEmptyString
,
452 bitmap
, wxNullBitmap
, wxITEM_NORMAL
,
453 shortHelpString
, longHelpString
, NULL
);
456 wxToolBarToolBase
*AddTool(int id
,
457 const wxBitmap
& bitmap
,
458 const wxBitmap
& bmpDisabled
,
462 wxObject
*clientData
= NULL
,
463 const wxString
& shortHelp
= wxEmptyString
,
464 const wxString
& longHelp
= wxEmptyString
)
466 return DoAddTool(id
, wxEmptyString
, bitmap
, bmpDisabled
,
467 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
468 shortHelp
, longHelp
, clientData
, xPos
, yPos
);
471 wxToolBarToolBase
*InsertTool(size_t pos
,
473 const wxBitmap
& bitmap
,
474 const wxBitmap
& bmpDisabled
= wxNullBitmap
,
476 wxObject
*clientData
= NULL
,
477 const wxString
& shortHelp
= wxEmptyString
,
478 const wxString
& longHelp
= wxEmptyString
)
480 return InsertTool(pos
, id
, wxEmptyString
, bitmap
, bmpDisabled
,
481 toggle
? wxITEM_CHECK
: wxITEM_NORMAL
,
482 shortHelp
, longHelp
, clientData
);
488 // NB: these functions are deprecated, use EVT_TOOL_XXX() instead!
490 // Only allow toggle if returns TRUE. Call when left button up.
491 virtual bool OnLeftClick(int id
, bool toggleDown
);
493 // Call when right button down.
494 virtual void OnRightClick(int id
, long x
, long y
);
496 // Called when the mouse cursor enters a tool bitmap.
497 // Argument is -1 if mouse is exiting the toolbar.
498 virtual void OnMouseEnter(int id
);
500 // more deprecated functions
501 // -------------------------
503 #if WXWIN_COMPATIBILITY
504 void SetDefaultSize(int w
, int h
) { SetDefaultSize(wxSize(w
, h
)); }
505 long GetDefaultWidth() const { return m_defaultWidth
; }
506 long GetDefaultHeight() const { return m_defaultHeight
; }
507 int GetDefaultButtonWidth() const { return (int) GetDefaultButtonSize().x
; };
508 int GetDefaultButtonHeight() const { return (int) GetDefaultButtonSize().y
; };
509 virtual void SetDefaultSize(const wxSize
& size
) { SetToolBitmapSize(size
); }
510 virtual wxSize
GetDefaultSize() const { return GetToolBitmapSize(); }
511 virtual wxSize
GetDefaultButtonSize() const { return GetToolSize(); }
512 #endif // WXWIN_COMPATIBILITY
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 void OnIdle(wxIdleEvent
& event
);
524 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
525 virtual void DoToolbarUpdates();
527 // don't want toolbars to accept the focus
528 virtual bool AcceptsFocus() const { return FALSE
; }
531 // to implement in derived classes
532 // -------------------------------
534 // create a new toolbar tool and add it to the toolbar, this is typically
535 // implemented by just calling InsertTool()
536 virtual wxToolBarToolBase
*DoAddTool
539 const wxString
& label
,
540 const wxBitmap
& bitmap
,
541 const wxBitmap
& bmpDisabled
,
543 const wxString
& shortHelp
= wxEmptyString
,
544 const wxString
& longHelp
= wxEmptyString
,
545 wxObject
*clientData
= NULL
,
550 // the tool is not yet inserted into m_tools list when this function is
551 // called and will only be added to it if this function succeeds
552 virtual bool DoInsertTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
554 // the tool is still in m_tools list when this function is called, it will
555 // only be deleted from it if it succeeds
556 virtual bool DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
558 // called when the tools enabled flag changes
559 virtual void DoEnableTool(wxToolBarToolBase
*tool
, bool enable
) = 0;
561 // called when the tool is toggled
562 virtual void DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
) = 0;
564 // called when the tools "can be toggled" flag changes
565 virtual void DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
) = 0;
567 // the functions to create toolbar tools
568 virtual wxToolBarToolBase
*CreateTool(int id
,
569 const wxString
& label
,
570 const wxBitmap
& bmpNormal
,
571 const wxBitmap
& bmpDisabled
,
573 wxObject
*clientData
,
574 const wxString
& shortHelp
,
575 const wxString
& longHelp
) = 0;
577 virtual wxToolBarToolBase
*CreateTool(wxControl
*control
) = 0;
582 // find the tool by id
583 wxToolBarToolBase
*FindById(int id
) const;
585 // the list of all our tools
586 wxToolBarToolsList m_tools
;
588 // the offset of the first tool
592 // the maximum number of toolbar rows/columns
596 // the tool packing and separation
600 // the size of the toolbar bitmaps
601 wxCoord m_defaultWidth
, m_defaultHeight
;
604 DECLARE_EVENT_TABLE()
605 DECLARE_CLASS(wxToolBarBase
)
608 // Helper function for creating the image for disabled buttons
609 bool wxCreateGreyedImage(const wxImage
& in
, wxImage
& out
) ;
611 #endif // wxUSE_TOOLBAR