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 // ----------------------------------------------------------------------------
20 #pragma interface "tbarbase.h"
27 #include "wx/bitmap.h"
29 #include "wx/control.h"
31 class WXDLLEXPORT wxToolBarBase
;
32 class WXDLLEXPORT wxToolBarToolBase
;
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 WXDLLEXPORT_DATA(extern const wxChar
*) wxToolBarNameStr
;
39 WXDLLEXPORT_DATA(extern const wxSize
) wxDefaultSize
;
40 WXDLLEXPORT_DATA(extern const wxPoint
) wxDefaultPosition
;
42 enum wxToolBarToolStyle
44 wxTOOL_STYLE_BUTTON
= 1,
45 wxTOOL_STYLE_SEPARATOR
= 2,
49 // ----------------------------------------------------------------------------
50 // wxToolBarTool is a toolbar element.
52 // It has a unique id (except for the separators which always have id -1), the
53 // style (telling whether it is a normal button, separator or a control), the
54 // state (toggled or not, enabled or not) and short and long help strings. The
55 // default implementations use the short help string for the tooltip text which
56 // is popped up when the mouse pointer enters the tool and the long help string
57 // for the applications status bar.
58 // ----------------------------------------------------------------------------
60 class WXDLLEXPORT wxToolBarToolBase
: public wxObject
66 wxToolBarToolBase(wxToolBarBase
*tbar
= (wxToolBarBase
*)NULL
,
67 int id
= wxID_SEPARATOR
,
68 const wxBitmap
& bitmap1
= wxNullBitmap
,
69 const wxBitmap
& bitmap2
= wxNullBitmap
,
71 wxObject
*clientData
= (wxObject
*) NULL
,
72 const wxString
& shortHelpString
= wxEmptyString
,
73 const wxString
& longHelpString
= wxEmptyString
)
74 : m_shortHelpString(shortHelpString
),
75 m_longHelpString(longHelpString
)
79 m_clientData
= clientData
;
88 m_toolStyle
= id
== wxID_SEPARATOR
? wxTOOL_STYLE_SEPARATOR
89 : wxTOOL_STYLE_BUTTON
;
92 wxToolBarToolBase(wxToolBarBase
*tbar
, wxControl
*control
)
96 m_id
= control
->GetId();
102 m_toolStyle
= wxTOOL_STYLE_CONTROL
;
105 ~wxToolBarToolBase();
111 int GetId() const { return m_id
; }
113 wxControl
*GetControl() const
115 wxASSERT_MSG( IsControl(), _T("this toolbar tool is not a control") );
120 wxToolBarBase
*GetToolBar() const { return m_tbar
; }
123 int IsButton() const { return m_toolStyle
== wxTOOL_STYLE_BUTTON
; }
124 int IsControl() const { return m_toolStyle
== wxTOOL_STYLE_CONTROL
; }
125 int IsSeparator() const { return m_toolStyle
== wxTOOL_STYLE_SEPARATOR
; }
126 int GetStyle() const { return m_toolStyle
; }
129 bool IsEnabled() const { return m_enabled
; }
130 bool IsToggled() const { return m_toggled
; }
131 bool CanBeToggled() const { return m_isToggle
; }
134 const wxBitmap
& GetBitmap1() const { return m_bitmap1
; }
135 const wxBitmap
& GetBitmap2() const { return m_bitmap2
; }
137 const wxBitmap
& GetBitmap() const
138 { return IsToggled() ? m_bitmap2
: m_bitmap1
; }
140 wxString
GetShortHelp() const { return m_shortHelpString
; }
141 wxString
GetLongHelp() const { return m_longHelpString
; }
143 wxObject
*GetClientData() const
145 if ( m_toolStyle
== wxTOOL_STYLE_CONTROL
)
147 return (wxObject
*)m_control
->GetClientData();
155 // modifiers: return TRUE if the state really changed
156 bool Enable(bool enable
);
157 bool Toggle(bool toggle
);
158 bool SetToggle(bool toggle
);
159 bool SetShortHelp(const wxString
& help
);
160 bool SetLongHelp(const wxString
& help
);
162 void Toggle() { Toggle(!IsToggled()); }
164 void SetBitmap1(const wxBitmap
& bmp
) { m_bitmap1
= bmp
; }
165 void SetBitmap2(const wxBitmap
& bmp
) { m_bitmap2
= bmp
; }
167 void SetClientData(wxObject
*clientData
)
169 if ( m_toolStyle
== wxTOOL_STYLE_CONTROL
)
171 m_control
->SetClientData(clientData
);
175 m_clientData
= clientData
;
179 // add tool to/remove it from a toolbar
180 virtual void Detach() { m_tbar
= (wxToolBarBase
*)NULL
; }
181 virtual void Attach(wxToolBarBase
*tbar
) { m_tbar
= tbar
; }
184 wxToolBarBase
*m_tbar
; // the toolbar to which we belong (may be NULL)
186 int m_toolStyle
; // see enum wxToolBarToolStyle
187 int m_id
; // the tool id, wxID_SEPARATOR for separator
189 // as controls have their own client data, no need to waste memory
192 wxObject
*m_clientData
;
193 wxControl
*m_control
;
201 // normal and toggles bitmaps
205 // short and long help strings
206 wxString m_shortHelpString
;
207 wxString m_longHelpString
;
210 // a list of toolbar tools
211 WX_DECLARE_EXPORTED_LIST(wxToolBarToolBase
, wxToolBarToolsList
);
213 // ----------------------------------------------------------------------------
214 // the base class for all toolbars
215 // ----------------------------------------------------------------------------
217 class WXDLLEXPORT wxToolBarBase
: public wxControl
221 virtual ~wxToolBarBase();
223 // toolbar construction
224 // --------------------
226 // the most commonly used version of AddTool()
227 wxToolBarToolBase
*AddTool(int id
,
228 const wxBitmap
& bitmap
,
229 const wxString
& shortHelpString
= wxEmptyString
,
230 const wxString
& longHelpString
= wxEmptyString
)
232 return AddTool(id
, bitmap
, wxNullBitmap
, FALSE
, NULL
,
233 shortHelpString
, longHelpString
);
236 // If pushedBitmap is NULL, a reversed version of bitmap is created and
237 // used as the pushed/toggled image. If toggle is TRUE, the button toggles
238 // between the two states.
239 wxToolBarToolBase
*AddTool(int id
,
240 const wxBitmap
& bitmap
,
241 const wxBitmap
& pushedBitmap
,
243 wxObject
*clientData
= NULL
,
244 const wxString
& shortHelpString
= wxEmptyString
,
245 const wxString
& longHelpString
= wxEmptyString
)
247 return AddTool(id
, bitmap
, pushedBitmap
, toggle
,
248 -1, -1, clientData
, shortHelpString
, longHelpString
);
251 // the old version of AddTool() kept for compatibility
252 virtual wxToolBarToolBase
*AddTool
255 const wxBitmap
& bitmap
,
256 const wxBitmap
& pushedBitmap
,
260 wxObject
*clientData
= NULL
,
261 const wxString
& helpString1
= wxEmptyString
,
262 const wxString
& helpString2
= wxEmptyString
265 // insert the new tool at the given position, if pos == GetToolsCount(), it
266 // is equivalent to AddTool()
267 virtual wxToolBarToolBase
*InsertTool
271 const wxBitmap
& bitmap
,
272 const wxBitmap
& pushedBitmap
= wxNullBitmap
,
274 wxObject
*clientData
= NULL
,
275 const wxString
& help1
= wxEmptyString
,
276 const wxString
& help2
= wxEmptyString
279 // add an arbitrary control to the toolbar, return TRUE if ok (notice that
280 // the control will be deleted by the toolbar and that it will also adjust
281 // its position/size)
283 // NB: the control should have toolbar as its parent
284 virtual wxToolBarToolBase
*AddControl(wxControl
*control
);
285 virtual wxToolBarToolBase
*InsertControl(size_t pos
, wxControl
*control
);
287 // add a separator to the toolbar
288 virtual wxToolBarToolBase
*AddSeparator();
289 virtual wxToolBarToolBase
*InsertSeparator(size_t pos
);
291 // remove the tool from the toolbar: the caller is responsible for actually
292 // deleting the pointer
293 virtual wxToolBarToolBase
*RemoveTool(int id
);
295 // delete tool either by index or by position
296 virtual bool DeleteToolByPos(size_t pos
);
297 virtual bool DeleteTool(int id
);
300 virtual void ClearTools();
302 // must be called after all buttons have been created to finish toolbar
304 virtual bool Realize();
309 virtual void EnableTool(int id
, bool enable
);
310 virtual void ToggleTool(int id
, bool toggle
);
312 // Set this to be togglable (or not)
313 virtual void SetToggle(int id
, bool toggle
);
315 // set/get tools client data (not for controls)
316 virtual wxObject
*GetToolClientData(int id
) const;
317 virtual void SetToolClientData(int id
, wxObject
*clientData
);
319 // return TRUE if the tool is toggled
320 virtual bool GetToolState(int id
) const;
322 virtual bool GetToolEnabled(int id
) const;
324 virtual void SetToolShortHelp(int id
, const wxString
& helpString
);
325 virtual wxString
GetToolShortHelp(int id
) const;
326 virtual void SetToolLongHelp(int id
, const wxString
& helpString
);
327 virtual wxString
GetToolLongHelp(int id
) const;
329 // margins/packing/separation
330 // --------------------------
332 virtual void SetMargins(int x
, int y
);
333 void SetMargins(const wxSize
& size
)
334 { SetMargins((int) size
.x
, (int) size
.y
); }
335 virtual void SetToolPacking(int packing
)
336 { m_toolPacking
= packing
; }
337 virtual void SetToolSeparation(int separation
)
338 { m_toolSeparation
= separation
; }
340 virtual wxSize
GetToolMargins() { return GetMargins(); }
341 virtual int GetToolPacking() { return m_toolPacking
; }
342 virtual int GetToolSeparation() { return m_toolSeparation
; }
345 wxSize
GetMargins() const { return wxSize(m_xMargin
, m_yMargin
); }
350 // set the number of toolbar rows
351 virtual void SetRows(int nRows
);
353 // the toolbar can wrap - limit the number of columns or rows it may take
354 void SetMaxRowsCols(int rows
, int cols
)
355 { m_maxRows
= rows
; m_maxCols
= cols
; }
356 int GetMaxRows() const { return m_maxRows
; }
357 int GetMaxCols() const { return m_maxCols
; }
359 // get/set the size of the bitmaps used by the toolbar: should be called
360 // before adding any tools to the toolbar
361 virtual void SetToolBitmapSize(const wxSize
& size
)
362 { m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
; };
363 virtual wxSize
GetToolBitmapSize() const
364 { return wxSize(m_defaultWidth
, m_defaultHeight
); }
366 // the button size in some implementations is bigger than the bitmap size:
367 // get the total button size (by default the same as bitmap size)
368 virtual wxSize
GetToolSize() const
369 { return GetToolBitmapSize(); } ;
371 // returns a (non separator) tool containing the point (x, y) or NULL if
372 // there is no tool at this point (corrdinates are client)
373 virtual wxToolBarToolBase
*FindToolForPosition(wxCoord x
,
374 wxCoord y
) const = 0;
379 // NB: these functions are deprecated, use EVT_TOOL_XXX() instead!
381 // Only allow toggle if returns TRUE. Call when left button up.
382 virtual bool OnLeftClick(int id
, bool toggleDown
);
384 // Call when right button down.
385 virtual void OnRightClick(int id
, long x
, long y
);
387 // Called when the mouse cursor enters a tool bitmap.
388 // Argument is -1 if mouse is exiting the toolbar.
389 virtual void OnMouseEnter(int id
);
391 // more deprecated functions
392 // -------------------------
394 #if WXWIN_COMPATIBILITY
395 void SetDefaultSize(int w
, int h
) { SetDefaultSize(wxSize(w
, h
)); }
396 long GetDefaultWidth() const { return m_defaultWidth
; }
397 long GetDefaultHeight() const { return m_defaultHeight
; }
398 int GetDefaultButtonWidth() const { return (int) GetDefaultButtonSize().x
; };
399 int GetDefaultButtonHeight() const { return (int) GetDefaultButtonSize().y
; };
400 virtual void SetDefaultSize(const wxSize
& size
) { SetToolBitmapSize(size
); }
401 virtual wxSize
GetDefaultSize() const { return GetToolBitmapSize(); }
402 virtual wxSize
GetDefaultButtonSize() const { return GetToolSize(); }
403 #endif // WXWIN_COMPATIBILITY
405 // implementation only from now on
406 // -------------------------------
408 size_t GetToolsCount() const { return m_tools
.GetCount(); }
410 void OnIdle(wxIdleEvent
& event
);
412 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
413 virtual void DoToolbarUpdates();
415 // don't want toolbars to accept the focus
416 virtual bool AcceptsFocus() const { return FALSE
; }
419 // to implement in derived classes
420 // -------------------------------
422 // the tool is not yet inserted into m_tools list when this function is
423 // called and will only be added to it if this function succeeds
424 virtual bool DoInsertTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
426 // the tool is still in m_tools list when this function is called, it will
427 // only be deleted from it if it succeeds
428 virtual bool DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
430 // called when the tools enabled flag changes
431 virtual void DoEnableTool(wxToolBarToolBase
*tool
, bool enable
) = 0;
433 // called when the tool is toggled
434 virtual void DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
) = 0;
436 // called when the tools "can be toggled" flag changes
437 virtual void DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
) = 0;
439 // the functions to create toolbar tools
440 virtual wxToolBarToolBase
*CreateTool(int id
,
441 const wxBitmap
& bitmap1
,
442 const wxBitmap
& bitmap2
,
444 wxObject
*clientData
,
445 const wxString
& shortHelpString
,
446 const wxString
& longHelpString
) = 0;
447 virtual wxToolBarToolBase
*CreateTool(wxControl
*control
) = 0;
452 // find the tool by id
453 wxToolBarToolBase
*FindById(int id
) const;
455 // the list of all our tools
456 wxToolBarToolsList m_tools
;
458 // the offset of the first tool
462 // the maximum number of toolbar rows/columns
466 // the tool packing and separation
470 // the size of the toolbar bitmaps
471 wxCoord m_defaultWidth
, m_defaultHeight
;
474 DECLARE_EVENT_TABLE()
475 DECLARE_CLASS(wxToolBarBase
)
478 #endif // wxUSE_TOOLBAR