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"
25 #include "wx/bitmap.h"
27 #include "wx/control.h"
29 class WXDLLEXPORT wxToolBarBase
;
30 class WXDLLEXPORT wxToolBarToolBase
;
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 WXDLLEXPORT_DATA(extern const wxChar
*) wxToolBarNameStr
;
37 WXDLLEXPORT_DATA(extern const wxSize
) wxDefaultSize
;
38 WXDLLEXPORT_DATA(extern const wxPoint
) wxDefaultPosition
;
40 enum wxToolBarToolStyle
42 wxTOOL_STYLE_BUTTON
= 1,
43 wxTOOL_STYLE_SEPARATOR
= 2,
47 // ----------------------------------------------------------------------------
48 // wxToolBarTool is a toolbar element.
50 // It has a unique id (except for the separators which always have id -1), the
51 // style (telling whether it is a normal button, separator or a control), the
52 // state (toggled or not, enabled or not) and short and long help strings. The
53 // default implementations use the short help string for the tooltip text which
54 // is popped up when the mouse pointer enters the tool and the long help string
55 // for the applications status bar.
56 // ----------------------------------------------------------------------------
58 class WXDLLEXPORT wxToolBarToolBase
: public wxObject
64 wxToolBarToolBase(wxToolBarBase
*tbar
= (wxToolBarBase
*)NULL
,
65 int id
= wxID_SEPARATOR
,
66 const wxBitmap
& bitmap1
= wxNullBitmap
,
67 const wxBitmap
& bitmap2
= wxNullBitmap
,
69 wxObject
*clientData
= (wxObject
*) NULL
,
70 const wxString
& shortHelpString
= wxEmptyString
,
71 const wxString
& longHelpString
= wxEmptyString
)
72 : m_shortHelpString(shortHelpString
),
73 m_longHelpString(longHelpString
)
77 m_clientData
= clientData
;
86 m_toolStyle
= id
== wxID_SEPARATOR
? wxTOOL_STYLE_SEPARATOR
87 : wxTOOL_STYLE_BUTTON
;
90 wxToolBarToolBase(wxToolBarBase
*tbar
, wxControl
*control
)
94 m_id
= control
->GetId();
100 m_toolStyle
= wxTOOL_STYLE_CONTROL
;
103 ~wxToolBarToolBase();
109 int GetId() const { return m_id
; }
111 wxControl
*GetControl() const
113 wxASSERT_MSG( IsControl(), _T("this toolbar tool is not a control") );
118 wxToolBarBase
*GetToolBar() const { return m_tbar
; }
121 int IsButton() const { return m_toolStyle
== wxTOOL_STYLE_BUTTON
; }
122 int IsControl() const { return m_toolStyle
== wxTOOL_STYLE_CONTROL
; }
123 int IsSeparator() const { return m_toolStyle
== wxTOOL_STYLE_SEPARATOR
; }
124 int GetStyle() const { return m_toolStyle
; }
127 bool IsEnabled() const { return m_enabled
; }
128 bool IsToggled() const { return m_toggled
; }
129 bool CanBeToggled() const { return m_isToggle
; }
132 const wxBitmap
& GetBitmap1() const { return m_bitmap1
; }
133 const wxBitmap
& GetBitmap2() const { return m_bitmap2
; }
135 const wxBitmap
& GetBitmap() const
136 { return IsToggled() ? m_bitmap2
: m_bitmap1
; }
138 wxString
GetShortHelp() const { return m_shortHelpString
; }
139 wxString
GetLongHelp() const { return m_longHelpString
; }
141 wxObject
*GetClientData() const
143 if ( m_toolStyle
== wxTOOL_STYLE_CONTROL
)
145 return (wxObject
*)m_control
->GetClientData();
153 // modifiers: return TRUE if the state really changed
154 bool Enable(bool enable
);
155 bool Toggle(bool toggle
);
156 bool SetToggle(bool toggle
);
157 bool SetShortHelp(const wxString
& help
);
158 bool SetLongHelp(const wxString
& help
);
160 void Toggle() { Toggle(!IsToggled()); }
162 void SetBitmap1(const wxBitmap
& bmp
) { m_bitmap1
= bmp
; }
163 void SetBitmap2(const wxBitmap
& bmp
) { m_bitmap2
= bmp
; }
165 void SetClientData(wxObject
*clientData
)
167 if ( m_toolStyle
== wxTOOL_STYLE_CONTROL
)
169 m_control
->SetClientData(clientData
);
173 m_clientData
= clientData
;
177 // add tool to/remove it from a toolbar
178 virtual void Detach() { m_tbar
= (wxToolBarBase
*)NULL
; }
179 virtual void Attach(wxToolBarBase
*tbar
) { m_tbar
= tbar
; }
182 wxToolBarBase
*m_tbar
; // the toolbar to which we belong (may be NULL)
184 int m_toolStyle
; // see enum wxToolBarToolStyle
185 int m_id
; // the tool id, wxID_SEPARATOR for separator
187 // as controls have their own client data, no need to waste memory
190 wxObject
*m_clientData
;
191 wxControl
*m_control
;
199 // normal and toggles bitmaps
203 // short and long help strings
204 wxString m_shortHelpString
;
205 wxString m_longHelpString
;
208 // a list of toolbar tools
209 WX_DECLARE_EXPORTED_LIST(wxToolBarToolBase
, wxToolBarToolsList
);
211 // ----------------------------------------------------------------------------
212 // the base class for all toolbars
213 // ----------------------------------------------------------------------------
215 class WXDLLEXPORT wxToolBarBase
: public wxControl
219 virtual ~wxToolBarBase();
221 // toolbar construction
222 // --------------------
224 // the most commonly used version of AddTool()
225 wxToolBarToolBase
*AddTool(int id
,
226 const wxBitmap
& bitmap
,
227 const wxString
& shortHelpString
= wxEmptyString
,
228 const wxString
& longHelpString
= wxEmptyString
)
230 return AddTool(id
, bitmap
, wxNullBitmap
, FALSE
, NULL
,
231 shortHelpString
, longHelpString
);
234 // If pushedBitmap is NULL, a reversed version of bitmap is created and
235 // used as the pushed/toggled image. If toggle is TRUE, the button toggles
236 // between the two states.
237 wxToolBarToolBase
*AddTool(int id
,
238 const wxBitmap
& bitmap
,
239 const wxBitmap
& pushedBitmap
= wxNullBitmap
,
241 wxObject
*clientData
= NULL
,
242 const wxString
& shortHelpString
= wxEmptyString
,
243 const wxString
& longHelpString
= wxEmptyString
)
245 return AddTool(id
, bitmap
, pushedBitmap
, toggle
,
246 -1, -1, clientData
, shortHelpString
, longHelpString
);
249 // the old version of AddTool() kept for compatibility
250 virtual wxToolBarToolBase
*AddTool
253 const wxBitmap
& bitmap
,
254 const wxBitmap
& pushedBitmap
,
258 wxObject
*clientData
= NULL
,
259 const wxString
& helpString1
= wxEmptyString
,
260 const wxString
& helpString2
= wxEmptyString
263 // insert the new tool at the given position, if pos == GetToolsCount(), it
264 // is equivalent to AddTool()
265 virtual wxToolBarToolBase
*InsertTool
269 const wxBitmap
& bitmap
,
270 const wxBitmap
& pushedBitmap
= wxNullBitmap
,
272 wxObject
*clientData
= NULL
,
273 const wxString
& help1
= wxEmptyString
,
274 const wxString
& help2
= wxEmptyString
277 // add an arbitrary control to the toolbar, return TRUE if ok (notice that
278 // the control will be deleted by the toolbar and that it will also adjust
279 // its position/size)
281 // NB: the control should have toolbar as its parent
282 virtual wxToolBarToolBase
*AddControl(wxControl
*control
);
283 virtual wxToolBarToolBase
*InsertControl(size_t pos
, wxControl
*control
);
285 // add a separator to the toolbar
286 virtual wxToolBarToolBase
*AddSeparator();
287 virtual wxToolBarToolBase
*InsertSeparator(size_t pos
);
289 // remove the tool from the toolbar: the caller is responsible for actually
290 // deleting the pointer
291 virtual wxToolBarToolBase
*RemoveTool(int id
);
293 // delete tool either by index or by position
294 virtual bool DeleteToolByPos(size_t pos
);
295 virtual bool DeleteTool(int id
);
298 virtual void ClearTools();
300 // must be called after all buttons have been created to finish toolbar
302 virtual bool Realize();
307 virtual void EnableTool(int id
, bool enable
);
308 virtual void ToggleTool(int id
, bool toggle
);
310 // Set this to be togglable (or not)
311 virtual void SetToggle(int id
, bool toggle
);
313 // set/get tools client data (not for controls)
314 virtual wxObject
*GetToolClientData(int id
) const;
315 virtual void SetToolClientData(int id
, wxObject
*clientData
);
317 // return TRUE if the tool is toggled
318 virtual bool GetToolState(int id
) const;
320 virtual bool GetToolEnabled(int id
) const;
322 virtual void SetToolShortHelp(int id
, const wxString
& helpString
);
323 virtual wxString
GetToolShortHelp(int id
) const;
324 virtual void SetToolLongHelp(int id
, const wxString
& helpString
);
325 virtual wxString
GetToolLongHelp(int id
) const;
327 // margins/packing/separation
328 // --------------------------
330 virtual void SetMargins(int x
, int y
);
331 void SetMargins(const wxSize
& size
)
332 { SetMargins((int) size
.x
, (int) size
.y
); }
333 virtual void SetToolPacking(int packing
)
334 { m_toolPacking
= packing
; }
335 virtual void SetToolSeparation(int separation
)
336 { m_toolSeparation
= separation
; }
338 virtual wxSize
GetToolMargins() { return wxSize(m_xMargin
, m_yMargin
); }
339 virtual int GetToolPacking() { return m_toolPacking
; }
340 virtual int GetToolSeparation() { return m_toolSeparation
; }
343 wxSize
GetMargins() const { return GetToolMargins(); }
348 // set the number of toolbar rows
349 virtual void SetRows(int nRows
);
351 // the toolbar can wrap - limit the number of columns or rows it may take
352 void SetMaxRowsCols(int rows
, int cols
)
353 { m_maxRows
= rows
; m_maxCols
= cols
; }
354 int GetMaxRows() const { return m_maxRows
; }
355 int GetMaxCols() const { return m_maxCols
; }
357 // get/set the size of the bitmaps used by the toolbar: should be called
358 // before adding any tools to the toolbar
359 virtual void SetToolBitmapSize(const wxSize
& size
)
360 { m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
; };
361 virtual wxSize
GetToolBitmapSize() const
362 { return wxSize(m_defaultWidth
, m_defaultHeight
); }
364 // the button size in some implementations is bigger than the bitmap size:
365 // get the total button size (by default the same as bitmap size)
366 virtual wxSize
GetToolSize() const
367 { return GetToolBitmapSize(); } ;
369 // returns a (non separator) tool containing the point (x, y) or NULL if
370 // there is no tool at this point (corrdinates are client)
371 virtual wxToolBarToolBase
*FindToolForPosition(wxCoord x
,
372 wxCoord y
) const = 0;
377 // NB: these functions are deprecated, use EVT_TOOL_XXX() instead!
379 // Only allow toggle if returns TRUE. Call when left button up.
380 virtual bool OnLeftClick(int id
, bool toggleDown
);
382 // Call when right button down.
383 virtual void OnRightClick(int id
, long x
, long y
);
385 // Called when the mouse cursor enters a tool bitmap.
386 // Argument is -1 if mouse is exiting the toolbar.
387 virtual void OnMouseEnter(int id
);
389 // more deprecated functions
390 // -------------------------
392 #if WXWIN_COMPATIBILITY
393 void SetDefaultSize(int w
, int h
) { SetDefaultSize(wxSize(w
, h
)); }
394 long GetDefaultWidth() const { return m_defaultWidth
; }
395 long GetDefaultHeight() const { return m_defaultHeight
; }
396 int GetDefaultButtonWidth() const { return (int) GetDefaultButtonSize().x
; };
397 int GetDefaultButtonHeight() const { return (int) GetDefaultButtonSize().y
; };
398 virtual void SetDefaultSize(const wxSize
& size
) { SetToolBitmapSize(size
); }
399 virtual wxSize
GetDefaultSize() const { return GetToolBitmapSize(); }
400 virtual wxSize
GetDefaultButtonSize() const { return GetToolSize(); }
401 #endif // WXWIN_COMPATIBILITY
403 // implementation only from now on
404 // -------------------------------
406 size_t GetToolsCount() const { return m_tools
.GetCount(); }
408 void OnIdle(wxIdleEvent
& event
);
410 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
411 virtual void DoToolbarUpdates();
413 // Don't want toolbars to accept the focus
414 bool AcceptsFocus() const { return FALSE
; }
417 // to implement in derived classes
418 // -------------------------------
420 // the tool is not yet inserted into m_tools list when this function is
421 // called and will only be added to it if this function succeeds
422 virtual bool DoInsertTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
424 // the tool is still in m_tools list when this function is called, it will
425 // only be deleted from it if it succeeds
426 virtual bool DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
) = 0;
428 // called when the tools enabled flag changes
429 virtual void DoEnableTool(wxToolBarToolBase
*tool
, bool enable
) = 0;
431 // called when the tool is toggled
432 virtual void DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
) = 0;
434 // called when the tools "can be toggled" flag changes
435 virtual void DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
) = 0;
437 // the functions to create toolbar tools
438 virtual wxToolBarToolBase
*CreateTool(int id
,
439 const wxBitmap
& bitmap1
,
440 const wxBitmap
& bitmap2
,
442 wxObject
*clientData
,
443 const wxString
& shortHelpString
,
444 const wxString
& longHelpString
) = 0;
445 virtual wxToolBarToolBase
*CreateTool(wxControl
*control
) = 0;
450 // find the tool by id
451 wxToolBarToolBase
*FindById(int id
) const;
453 // the list of all our tools
454 wxToolBarToolsList m_tools
;
456 // the offset of the first tool
460 // the maximum number of toolbar rows/columns
464 // the tool packing and separation
468 // the size of the toolbar bitmaps
469 wxCoord m_defaultWidth
, m_defaultHeight
;
472 DECLARE_EVENT_TABLE()
473 DECLARE_CLASS(wxToolBarBase
)