]> git.saurik.com Git - wxWidgets.git/blob - include/wx/tbarbase.h
Remove now unused header. Forward declaration is enough for wxBookCtrl and wxNotebook.
[wxWidgets.git] / include / wx / tbarbase.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/tbarbase.h
3 // Purpose: Base class for toolbar classes
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_TBARBASE_H_
13 #define _WX_TBARBASE_H_
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #include "wx/defs.h"
20
21 #if wxUSE_TOOLBAR
22
23 #include "wx/bitmap.h"
24 #include "wx/list.h"
25 #include "wx/control.h"
26
27 class WXDLLEXPORT wxToolBarBase;
28 class WXDLLEXPORT wxToolBarToolBase;
29 class WXDLLEXPORT wxImage;
30
31 // ----------------------------------------------------------------------------
32 // constants
33 // ----------------------------------------------------------------------------
34
35 extern WXDLLEXPORT_DATA(const wxChar*) wxToolBarNameStr;
36 extern WXDLLEXPORT_DATA(const wxSize) wxDefaultSize;
37 extern WXDLLEXPORT_DATA(const wxPoint) wxDefaultPosition;
38
39 enum wxToolBarToolStyle
40 {
41 wxTOOL_STYLE_BUTTON = 1,
42 wxTOOL_STYLE_SEPARATOR = 2,
43 wxTOOL_STYLE_CONTROL
44 };
45
46 // ----------------------------------------------------------------------------
47 // wxToolBarTool is a toolbar element.
48 //
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 // ----------------------------------------------------------------------------
56
57 class WXDLLEXPORT wxToolBarToolBase : public wxObject
58 {
59 public:
60 // ctors & dtor
61 // ------------
62
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)
72 : m_label(label),
73 m_shortHelpString(shortHelpString),
74 m_longHelpString(longHelpString)
75 {
76 m_tbar = tbar;
77 m_id = toolid;
78 if (m_id == wxID_ANY)
79 m_id = wxNewId();
80 m_clientData = clientData;
81
82 m_bmpNormal = bmpNormal;
83 m_bmpDisabled = bmpDisabled;
84
85 m_kind = kind;
86
87 m_enabled = true;
88 m_toggled = false;
89
90 m_toolStyle = toolid == wxID_SEPARATOR ? wxTOOL_STYLE_SEPARATOR
91 : wxTOOL_STYLE_BUTTON;
92 }
93
94 wxToolBarToolBase(wxToolBarBase *tbar, wxControl *control)
95 {
96 m_tbar = tbar;
97 m_control = control;
98 m_id = control->GetId();
99
100 m_kind = wxITEM_MAX; // invalid value
101
102 m_enabled = true;
103 m_toggled = false;
104
105 m_toolStyle = wxTOOL_STYLE_CONTROL;
106 }
107
108 ~wxToolBarToolBase(){}
109
110 // accessors
111 // ---------
112
113 // general
114 int GetId() const { return m_id; }
115
116 wxControl *GetControl() const
117 {
118 wxASSERT_MSG( IsControl(), _T("this toolbar tool is not a control") );
119
120 return m_control;
121 }
122
123 wxToolBarBase *GetToolBar() const { return m_tbar; }
124
125 // style
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
131 {
132 wxASSERT_MSG( IsButton(), _T("only makes sense for buttons") );
133
134 return m_kind;
135 }
136
137 // state
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; }
142
143 // attributes
144 const wxBitmap& GetNormalBitmap() const { return m_bmpNormal; }
145 const wxBitmap& GetDisabledBitmap() const { return m_bmpDisabled; }
146
147 const wxBitmap& GetBitmap() const
148 { return IsEnabled() ? GetNormalBitmap() : GetDisabledBitmap(); }
149
150 const wxString& GetLabel() const { return m_label; }
151
152 const wxString& GetShortHelp() const { return m_shortHelpString; }
153 const wxString& GetLongHelp() const { return m_longHelpString; }
154
155 wxObject *GetClientData() const
156 {
157 if ( m_toolStyle == wxTOOL_STYLE_CONTROL )
158 {
159 return (wxObject*)m_control->GetClientData();
160 }
161 else
162 {
163 return m_clientData;
164 }
165 }
166
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);
173
174 void Toggle() { Toggle(!IsToggled()); }
175
176 void SetNormalBitmap(const wxBitmap& bmp) { m_bmpNormal = bmp; }
177 void SetDisabledBitmap(const wxBitmap& bmp) { m_bmpDisabled = bmp; }
178
179 virtual void SetLabel(const wxString& label) { m_label = label; }
180
181 void SetClientData(wxObject *clientData)
182 {
183 if ( m_toolStyle == wxTOOL_STYLE_CONTROL )
184 {
185 m_control->SetClientData(clientData);
186 }
187 else
188 {
189 m_clientData = clientData;
190 }
191 }
192
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; }
196
197 // compatibility only, don't use
198 #if WXWIN_COMPATIBILITY_2_2
199 wxDEPRECATED( const wxBitmap& GetBitmap1() const );
200 wxDEPRECATED( const wxBitmap& GetBitmap2() const );
201
202 wxDEPRECATED( void SetBitmap1(const wxBitmap& bmp) );
203 wxDEPRECATED( void SetBitmap2(const wxBitmap& bmp) );
204 #endif // WXWIN_COMPATIBILITY_2_2
205
206 protected:
207 wxToolBarBase *m_tbar; // the toolbar to which we belong (may be NULL)
208
209 // tool parameters
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
213
214 // as controls have their own client data, no need to waste memory
215 union
216 {
217 wxObject *m_clientData;
218 wxControl *m_control;
219 };
220
221 // tool state
222 bool m_toggled;
223 bool m_enabled;
224
225 // normal and disabled bitmaps for the tool, both can be invalid
226 wxBitmap m_bmpNormal;
227 wxBitmap m_bmpDisabled;
228
229 // the button label
230 wxString m_label;
231
232 // short and long help strings
233 wxString m_shortHelpString;
234 wxString m_longHelpString;
235
236 DECLARE_DYNAMIC_CLASS_NO_COPY(wxToolBarToolBase)
237 };
238
239 // a list of toolbar tools
240 WX_DECLARE_EXPORTED_LIST(wxToolBarToolBase, wxToolBarToolsList);
241
242 // ----------------------------------------------------------------------------
243 // the base class for all toolbars
244 // ----------------------------------------------------------------------------
245
246 class WXDLLEXPORT wxToolBarBase : public wxControl
247 {
248 public:
249 wxToolBarBase();
250 virtual ~wxToolBarBase();
251
252 // toolbar construction
253 // --------------------
254
255 // the full AddTool() function
256 //
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)
267 {
268 return DoAddTool(toolid, label, bitmap, bmpDisabled, kind,
269 shortHelp, longHelp, data);
270 }
271
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)
278 {
279 return AddTool(toolid, label, bitmap, wxNullBitmap, kind, shortHelp);
280 }
281
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)
290 {
291 return AddTool(toolid, label, bitmap, bmpDisabled, wxITEM_CHECK,
292 shortHelp, longHelp, data);
293 }
294
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)
304 {
305 return AddTool(toolid, label, bitmap, bmpDisabled, wxITEM_RADIO,
306 shortHelp, longHelp, data);
307 }
308
309
310 // insert the new tool at the given position, if pos == GetToolsCount(), it
311 // is equivalent to AddTool()
312 virtual wxToolBarToolBase *InsertTool
313 (
314 size_t pos,
315 int toolid,
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
323 );
324
325 virtual wxToolBarToolBase *AddTool (wxToolBarToolBase *tool);
326 virtual wxToolBarToolBase *InsertTool (size_t pos, wxToolBarToolBase *tool);
327
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)
331 //
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);
335
336 // get the control with the given id or return NULL
337 virtual wxControl *FindControl( int toolid );
338
339 // add a separator to the toolbar
340 virtual wxToolBarToolBase *AddSeparator();
341 virtual wxToolBarToolBase *InsertSeparator(size_t pos);
342
343 // remove the tool from the toolbar: the caller is responsible for actually
344 // deleting the pointer
345 virtual wxToolBarToolBase *RemoveTool(int toolid);
346
347 // delete tool either by index or by position
348 virtual bool DeleteToolByPos(size_t pos);
349 virtual bool DeleteTool(int toolid);
350
351 // delete all tools
352 virtual void ClearTools();
353
354 // must be called after all buttons have been created to finish toolbar
355 // initialisation
356 virtual bool Realize();
357
358 // tools state
359 // -----------
360
361 virtual void EnableTool(int toolid, bool enable);
362 virtual void ToggleTool(int toolid, bool toggle);
363
364 // Set this to be togglable (or not)
365 virtual void SetToggle(int toolid, bool toggle);
366
367 // set/get tools client data (not for controls)
368 virtual wxObject *GetToolClientData(int toolid) const;
369 virtual void SetToolClientData(int toolid, wxObject *clientData);
370
371 // returns tool pos, or wxNOT_FOUND if tool isn't found
372 virtual int GetToolPos(int id) const;
373
374 // return true if the tool is toggled
375 virtual bool GetToolState(int toolid) const;
376
377 virtual bool GetToolEnabled(int toolid) const;
378
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;
383
384 // margins/packing/separation
385 // --------------------------
386
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; }
394
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; }
398
399 // toolbar geometry
400 // ----------------
401
402 // set the number of toolbar rows
403 virtual void SetRows(int nRows);
404
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; }
410
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); }
417
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(); } ;
422
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;
427
428 // find the tool by id
429 wxToolBarToolBase *FindById(int toolid) const;
430
431 // return true if this is a vertical toolbar, otherwise false
432 bool IsVertical() const { return HasFlag(wxTB_VERTICAL); }
433
434
435 // the old versions of the various methods kept for compatibility
436 // don't use in the new code!
437 // --------------------------------------------------------------
438
439 wxToolBarToolBase *AddTool(int toolid,
440 const wxBitmap& bitmap,
441 const wxBitmap& bmpDisabled,
442 bool toggle = false,
443 wxObject *clientData = NULL,
444 const wxString& shortHelpString = wxEmptyString,
445 const wxString& longHelpString = wxEmptyString)
446 {
447 return AddTool(toolid, wxEmptyString,
448 bitmap, bmpDisabled,
449 toggle ? wxITEM_CHECK : wxITEM_NORMAL,
450 shortHelpString, longHelpString, clientData);
451 }
452
453 wxToolBarToolBase *AddTool(int toolid,
454 const wxBitmap& bitmap,
455 const wxString& shortHelpString = wxEmptyString,
456 const wxString& longHelpString = wxEmptyString)
457 {
458 return AddTool(toolid, wxEmptyString,
459 bitmap, wxNullBitmap, wxITEM_NORMAL,
460 shortHelpString, longHelpString, NULL);
461 }
462
463 wxToolBarToolBase *AddTool(int toolid,
464 const wxBitmap& bitmap,
465 const wxBitmap& bmpDisabled,
466 bool toggle,
467 wxCoord xPos,
468 wxCoord yPos = wxDefaultCoord,
469 wxObject *clientData = NULL,
470 const wxString& shortHelp = wxEmptyString,
471 const wxString& longHelp = wxEmptyString)
472 {
473 return DoAddTool(toolid, wxEmptyString, bitmap, bmpDisabled,
474 toggle ? wxITEM_CHECK : wxITEM_NORMAL,
475 shortHelp, longHelp, clientData, xPos, yPos);
476 }
477
478 wxToolBarToolBase *InsertTool(size_t pos,
479 int toolid,
480 const wxBitmap& bitmap,
481 const wxBitmap& bmpDisabled = wxNullBitmap,
482 bool toggle = false,
483 wxObject *clientData = NULL,
484 const wxString& shortHelp = wxEmptyString,
485 const wxString& longHelp = wxEmptyString)
486 {
487 return InsertTool(pos, toolid, wxEmptyString, bitmap, bmpDisabled,
488 toggle ? wxITEM_CHECK : wxITEM_NORMAL,
489 shortHelp, longHelp, clientData);
490 }
491
492 // event handlers
493 // --------------
494
495 // NB: these functions are deprecated, use EVT_TOOL_XXX() instead!
496
497 // Only allow toggle if returns true. Call when left button up.
498 virtual bool OnLeftClick(int toolid, bool toggleDown);
499
500 // Call when right button down.
501 virtual void OnRightClick(int toolid, long x, long y);
502
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);
506
507 // more deprecated functions
508 // -------------------------
509
510 // use GetToolMargins() instead
511 wxSize GetMargins() const { return GetToolMargins(); }
512
513 // implementation only from now on
514 // -------------------------------
515
516 size_t GetToolsCount() const { return m_tools.GetCount(); }
517
518 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
519 virtual void UpdateWindowUI(long flags = wxUPDATE_UI_NONE) ;
520
521 // don't want toolbars to accept the focus
522 virtual bool AcceptsFocus() const { return false; }
523
524 protected:
525 // to implement in derived classes
526 // -------------------------------
527
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
531 (
532 int toolid,
533 const wxString& label,
534 const wxBitmap& bitmap,
535 const wxBitmap& bmpDisabled,
536 wxItemKind kind,
537 const wxString& shortHelp = wxEmptyString,
538 const wxString& longHelp = wxEmptyString,
539 wxObject *clientData = NULL,
540 wxCoord xPos = wxDefaultCoord,
541 wxCoord yPos = wxDefaultCoord
542 );
543
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;
547
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;
551
552 // called when the tools enabled flag changes
553 virtual void DoEnableTool(wxToolBarToolBase *tool, bool enable) = 0;
554
555 // called when the tool is toggled
556 virtual void DoToggleTool(wxToolBarToolBase *tool, bool toggle) = 0;
557
558 // called when the tools "can be toggled" flag changes
559 virtual void DoSetToggle(wxToolBarToolBase *tool, bool toggle) = 0;
560
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,
566 wxItemKind kind,
567 wxObject *clientData,
568 const wxString& shortHelp,
569 const wxString& longHelp) = 0;
570
571 virtual wxToolBarToolBase *CreateTool(wxControl *control) = 0;
572
573 // helper functions
574 // ----------------
575
576 // un-toggle all buttons in the same radio group
577 void UnToggleRadioGroup(wxToolBarToolBase *tool);
578
579 // the list of all our tools
580 wxToolBarToolsList m_tools;
581
582 // the offset of the first tool
583 int m_xMargin;
584 int m_yMargin;
585
586 // the maximum number of toolbar rows/columns
587 int m_maxRows;
588 int m_maxCols;
589
590 // the tool packing and separation
591 int m_toolPacking,
592 m_toolSeparation;
593
594 // the size of the toolbar bitmaps
595 wxCoord m_defaultWidth, m_defaultHeight;
596
597 private:
598 DECLARE_EVENT_TABLE()
599 DECLARE_NO_COPY_CLASS(wxToolBarBase)
600 };
601
602 // Helper function for creating the image for disabled buttons
603 bool wxCreateGreyedImage(const wxImage& in, wxImage& out) ;
604
605 #endif // wxUSE_TOOLBAR
606
607 #endif
608 // _WX_TBARBASE_H_
609