]> git.saurik.com Git - wxWidgets.git/blob - src/common/tbarbase.cpp
wxCommandEvent::IsChecked() now returns the toolbar button checked status as well
[wxWidgets.git] / src / common / tbarbase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/tbarbase.cpp
3 // Purpose: wxToolBarBase implementation
4 // Author: Julian Smart
5 // Modified by: VZ at 11.12.99 (wxScrollableToolBar splitted off)
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "tbarbase.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/wx.h"
33 #endif
34
35 #include "wx/frame.h"
36
37 // For ::UpdateWindow
38 #ifdef __WXMSW__
39 #include <windows.h>
40 #endif
41
42 #if wxUSE_TOOLBAR
43
44 #include "wx/tbarbase.h"
45
46 // ----------------------------------------------------------------------------
47 // wxWindows macros
48 // ----------------------------------------------------------------------------
49
50 #if !USE_SHARED_LIBRARY
51 BEGIN_EVENT_TABLE(wxToolBarBase, wxControl)
52 EVT_IDLE(wxToolBarBase::OnIdle)
53 END_EVENT_TABLE()
54 #endif
55
56 IMPLEMENT_CLASS(wxToolBarBase, wxControl)
57
58 #include "wx/listimpl.cpp"
59
60 WX_DEFINE_LIST(wxToolBarToolsList);
61
62 // ============================================================================
63 // implementation
64 // ============================================================================
65
66 // ----------------------------------------------------------------------------
67 // wxToolBarToolBase
68 // ----------------------------------------------------------------------------
69
70 bool wxToolBarToolBase::Enable(bool enable)
71 {
72 if ( m_enabled == enable )
73 return FALSE;
74
75 m_enabled = enable;
76
77 return TRUE;
78 }
79
80 bool wxToolBarToolBase::Toggle(bool toggle)
81 {
82 wxASSERT_MSG( m_isToggle, _T("can't toggle this tool") );
83
84 if ( m_toggled == toggle )
85 return FALSE;
86
87 m_toggled = toggle;
88
89 return TRUE;
90 }
91
92 bool wxToolBarToolBase::SetToggle(bool toggle)
93 {
94 if ( m_isToggle == toggle )
95 return FALSE;
96
97 m_isToggle = toggle;
98
99 return TRUE;
100 }
101
102 bool wxToolBarToolBase::SetShortHelp(const wxString& help)
103 {
104 if ( m_shortHelpString == help )
105 return FALSE;
106
107 m_shortHelpString = help;
108
109 return TRUE;
110 }
111
112 bool wxToolBarToolBase::SetLongHelp(const wxString& help)
113 {
114 if ( m_longHelpString == help )
115 return FALSE;
116
117 m_longHelpString = help;
118
119 return TRUE;
120 }
121
122 wxToolBarToolBase::~wxToolBarToolBase()
123 {
124 }
125
126 // ----------------------------------------------------------------------------
127 // wxToolBarBase adding/deleting items
128 // ----------------------------------------------------------------------------
129
130 wxToolBarBase::wxToolBarBase()
131 {
132 // the list owns the pointers
133 m_tools.DeleteContents(TRUE);
134
135 m_xMargin = m_yMargin = 0;
136
137 m_maxRows = m_maxCols = 0;
138 }
139
140 wxToolBarToolBase *wxToolBarBase::AddTool(int id,
141 const wxBitmap& bitmap,
142 const wxBitmap& pushedBitmap,
143 bool toggle,
144 wxCoord WXUNUSED(xPos),
145 wxCoord WXUNUSED(yPos),
146 wxObject *clientData,
147 const wxString& helpString1,
148 const wxString& helpString2)
149 {
150 return InsertTool(GetToolsCount(), id, bitmap, pushedBitmap,
151 toggle, clientData, helpString1, helpString2);
152 }
153
154 wxToolBarToolBase *wxToolBarBase::InsertTool(size_t pos,
155 int id,
156 const wxBitmap& bitmap,
157 const wxBitmap& pushedBitmap,
158 bool toggle,
159 wxObject *clientData,
160 const wxString& helpString1,
161 const wxString& helpString2)
162 {
163 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
164 _T("invalid position in wxToolBar::InsertTool()") );
165
166 wxToolBarToolBase *tool = CreateTool(id, bitmap, pushedBitmap, toggle,
167 clientData, helpString1, helpString2);
168
169 if ( !tool || !DoInsertTool(pos, tool) )
170 {
171 delete tool;
172
173 return NULL;
174 }
175
176 m_tools.Insert(pos, tool);
177
178 return tool;
179 }
180
181 wxToolBarToolBase *wxToolBarBase::AddControl(wxControl *control)
182 {
183 return InsertControl(GetToolsCount(), control);
184 }
185
186 wxToolBarToolBase *wxToolBarBase::InsertControl(size_t pos, wxControl *control)
187 {
188 wxCHECK_MSG( control, (wxToolBarToolBase *)NULL,
189 _T("toolbar: can't insert NULL control") );
190
191 wxCHECK_MSG( control->GetParent() == this, (wxToolBarToolBase *)NULL,
192 _T("control must have toolbar as parent") );
193
194 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
195 _T("invalid position in wxToolBar::InsertControl()") );
196
197 wxToolBarToolBase *tool = CreateTool(control);
198
199 if ( !tool || !DoInsertTool(pos, tool) )
200 {
201 delete tool;
202
203 return NULL;
204 }
205
206 m_tools.Insert(pos, tool);
207
208 return tool;
209 }
210
211 wxToolBarToolBase *wxToolBarBase::AddSeparator()
212 {
213 return InsertSeparator(GetToolsCount());
214 }
215
216 wxToolBarToolBase *wxToolBarBase::InsertSeparator(size_t pos)
217 {
218 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
219 _T("invalid position in wxToolBar::InsertSeparator()") );
220
221 wxToolBarToolBase *tool = CreateTool(wxID_SEPARATOR,
222 wxNullBitmap, wxNullBitmap,
223 FALSE, (wxObject *)NULL,
224 wxEmptyString, wxEmptyString);
225
226 if ( !tool || !DoInsertTool(pos, tool) )
227 {
228 delete tool;
229
230 return NULL;
231 }
232
233 m_tools.Insert(pos, tool);
234
235 return tool;
236 }
237
238 wxToolBarToolBase *wxToolBarBase::RemoveTool(int id)
239 {
240 size_t pos = 0;
241 wxToolBarToolsList::Node *node;
242 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
243 {
244 if ( node->GetData()->GetId() == id )
245 break;
246
247 pos++;
248 }
249
250 if ( !node )
251 {
252 // don't give any error messages - sometimes we might call RemoveTool()
253 // without knowing whether the tool is or not in the toolbar
254 return (wxToolBarToolBase *)NULL;
255 }
256
257 wxToolBarToolBase *tool = node->GetData();
258 if ( !DoDeleteTool(pos, tool) )
259 {
260 return (wxToolBarToolBase *)NULL;
261 }
262
263 // the node would delete the data, so set it to NULL to avoid this
264 node->SetData(NULL);
265
266 m_tools.DeleteNode(node);
267
268 return tool;
269 }
270
271 bool wxToolBarBase::DeleteToolByPos(size_t pos)
272 {
273 wxCHECK_MSG( pos < GetToolsCount(), FALSE,
274 _T("invalid position in wxToolBar::DeleteToolByPos()") );
275
276 wxToolBarToolsList::Node *node = m_tools.Item(pos);
277
278 if ( !DoDeleteTool(pos, node->GetData()) )
279 {
280 return FALSE;
281 }
282
283 m_tools.DeleteNode(node);
284
285 return TRUE;
286 }
287
288 bool wxToolBarBase::DeleteTool(int id)
289 {
290 size_t pos = 0;
291 wxToolBarToolsList::Node *node;
292 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
293 {
294 if ( node->GetData()->GetId() == id )
295 break;
296
297 pos++;
298 }
299
300 if ( !node || !DoDeleteTool(pos, node->GetData()) )
301 {
302 return FALSE;
303 }
304
305 m_tools.DeleteNode(node);
306
307 return TRUE;
308 }
309
310 wxToolBarToolBase *wxToolBarBase::FindById(int id) const
311 {
312 wxToolBarToolBase *tool = (wxToolBarToolBase *)NULL;
313
314 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
315 node;
316 node = node->GetNext() )
317 {
318 tool = node->GetData();
319 if ( tool->GetId() == id )
320 {
321 // found
322 break;
323 }
324 }
325
326 return tool;
327 }
328
329 void wxToolBarBase::ClearTools()
330 {
331 m_tools.Clear();
332 }
333
334 bool wxToolBarBase::Realize()
335 {
336 return TRUE;
337 }
338
339 wxToolBarBase::~wxToolBarBase()
340 {
341 }
342
343 // ----------------------------------------------------------------------------
344 // wxToolBarBase tools state
345 // ----------------------------------------------------------------------------
346
347 void wxToolBarBase::EnableTool(int id, bool enable)
348 {
349 wxToolBarToolBase *tool = FindById(id);
350 if ( tool )
351 {
352 if ( tool->Enable(enable) )
353 {
354 DoEnableTool(tool, enable);
355 }
356 }
357 }
358
359 void wxToolBarBase::ToggleTool(int id, bool toggle)
360 {
361 wxToolBarToolBase *tool = FindById(id);
362 if ( tool && tool->CanBeToggled() )
363 {
364 if ( tool->Toggle(toggle) )
365 {
366 DoToggleTool(tool, toggle);
367 }
368 }
369 }
370
371 void wxToolBarBase::SetToggle(int id, bool toggle)
372 {
373 wxToolBarToolBase *tool = FindById(id);
374 if ( tool )
375 {
376 if ( tool->SetToggle(toggle) )
377 {
378 DoSetToggle(tool, toggle);
379 }
380 }
381 }
382
383 void wxToolBarBase::SetToolShortHelp(int id, const wxString& help)
384 {
385 wxToolBarToolBase *tool = FindById(id);
386 if ( tool )
387 {
388 (void)tool->SetShortHelp(help);
389 }
390 }
391
392 void wxToolBarBase::SetToolLongHelp(int id, const wxString& help)
393 {
394 wxToolBarToolBase *tool = FindById(id);
395 if ( tool )
396 {
397 (void)tool->SetLongHelp(help);
398 }
399 }
400
401 wxObject *wxToolBarBase::GetToolClientData(int id) const
402 {
403 wxToolBarToolBase *tool = FindById(id);
404
405 return tool ? tool->GetClientData() : (wxObject *)NULL;
406 }
407
408 void wxToolBarBase::SetToolClientData(int id, wxObject *clientData)
409 {
410 wxToolBarToolBase *tool = FindById(id);
411
412 wxCHECK_RET( tool, _T("no such tool in wxToolBar::SetToolClientData") );
413
414 tool->SetClientData(clientData);
415 }
416
417 bool wxToolBarBase::GetToolState(int id) const
418 {
419 wxToolBarToolBase *tool = FindById(id);
420 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
421
422 return tool->IsToggled();
423 }
424
425 bool wxToolBarBase::GetToolEnabled(int id) const
426 {
427 wxToolBarToolBase *tool = FindById(id);
428 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
429
430 return tool->IsEnabled();
431 }
432
433 wxString wxToolBarBase::GetToolShortHelp(int id) const
434 {
435 wxToolBarToolBase *tool = FindById(id);
436 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
437
438 return tool->GetShortHelp();
439 }
440
441 wxString wxToolBarBase::GetToolLongHelp(int id) const
442 {
443 wxToolBarToolBase *tool = FindById(id);
444 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
445
446 return tool->GetLongHelp();
447 }
448
449 // ----------------------------------------------------------------------------
450 // wxToolBarBase geometry
451 // ----------------------------------------------------------------------------
452
453 void wxToolBarBase::SetMargins(int x, int y)
454 {
455 m_xMargin = x;
456 m_yMargin = y;
457 }
458
459 void wxToolBarBase::SetRows(int WXUNUSED(nRows))
460 {
461 // nothing
462 }
463
464 // ----------------------------------------------------------------------------
465 // event processing
466 // ----------------------------------------------------------------------------
467
468 // Only allow toggle if returns TRUE
469 bool wxToolBarBase::OnLeftClick(int id, bool toggleDown)
470 {
471 wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, id);
472 event.SetEventObject(this);
473
474 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
475 event.SetInt((int)toggleDown);
476
477 // and SetExtraLong() for backwards compatibility
478 event.SetExtraLong((long)toggleDown);
479
480 // Send events to this toolbar instead (and thence up the window hierarchy)
481 GetEventHandler()->ProcessEvent(event);
482
483 return TRUE;
484 }
485
486 // Call when right button down.
487 void wxToolBarBase::OnRightClick(int id,
488 long WXUNUSED(x),
489 long WXUNUSED(y))
490 {
491 wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, id);
492 event.SetEventObject(this);
493 event.SetInt(id);
494
495 GetEventHandler()->ProcessEvent(event);
496 }
497
498 // Called when the mouse cursor enters a tool bitmap (no button pressed).
499 // Argument is -1 if mouse is exiting the toolbar.
500 // Note that for this event, the id of the window is used,
501 // and the integer parameter of wxCommandEvent is used to retrieve
502 // the tool id.
503 void wxToolBarBase::OnMouseEnter(int id)
504 {
505 wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, GetId());
506 event.SetEventObject(this);
507 event.SetInt(id);
508
509 (void)GetEventHandler()->ProcessEvent(event);
510
511 wxToolBarToolBase *tool = FindById(id);
512 if ( !tool || !tool->GetLongHelp() )
513 return;
514
515 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
516 if ( !frame )
517 return;
518
519 frame->SetStatusText(tool->GetLongHelp());
520 }
521
522 // ----------------------------------------------------------------------------
523 // UI updates
524 // ----------------------------------------------------------------------------
525
526 void wxToolBarBase::OnIdle(wxIdleEvent& event)
527 {
528 DoToolbarUpdates();
529
530 event.Skip();
531 }
532
533 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
534 void wxToolBarBase::DoToolbarUpdates()
535 {
536 wxWindow* parent = this;
537 while (parent->GetParent())
538 parent = parent->GetParent();
539
540 #ifdef __WXMSW__
541 wxWindow* focusWin = wxFindFocusDescendant(parent);
542 #else
543 wxWindow* focusWin = (wxWindow*) NULL;
544 #endif
545
546 wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler() ;
547
548 for ( wxToolBarToolsList::Node* node = m_tools.GetFirst();
549 node;
550 node = node->GetNext() )
551 {
552 int id = node->GetData()->GetId();
553
554 wxUpdateUIEvent event(id);
555 event.SetEventObject(this);
556
557 if ( evtHandler->ProcessEvent(event) )
558 {
559 if ( event.GetSetEnabled() )
560 EnableTool(id, event.GetEnabled());
561 if ( event.GetSetChecked() )
562 ToggleTool(id, event.GetChecked());
563 #if 0
564 if ( event.GetSetText() )
565 // Set tooltip?
566 #endif // 0
567 }
568 }
569 }
570
571 #endif // wxUSE_TOOLBAR