]> git.saurik.com Git - wxWidgets.git/blob - src/common/tbarbase.cpp
applied doc patch for wxPostEvent().
[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 #if wxUSE_TOOLBAR
32
33 #ifndef WX_PRECOMP
34 #include "wx/control.h"
35 #endif
36
37 #include "wx/frame.h"
38
39 // For ::UpdateWindow
40 #ifdef __WXMSW__
41 #include <windows.h>
42 #endif
43
44 #include "wx/tbarbase.h"
45
46 // ----------------------------------------------------------------------------
47 // wxWindows macros
48 // ----------------------------------------------------------------------------
49
50 IMPLEMENT_CLASS(wxToolBarBase, wxControl)
51
52 BEGIN_EVENT_TABLE(wxToolBarBase, wxControl)
53 EVT_IDLE(wxToolBarBase::OnIdle)
54 END_EVENT_TABLE()
55
56 #include "wx/listimpl.cpp"
57
58 WX_DEFINE_LIST(wxToolBarToolsList);
59
60 // ============================================================================
61 // implementation
62 // ============================================================================
63
64 // ----------------------------------------------------------------------------
65 // wxToolBarToolBase
66 // ----------------------------------------------------------------------------
67
68 bool wxToolBarToolBase::Enable(bool enable)
69 {
70 if ( m_enabled == enable )
71 return FALSE;
72
73 m_enabled = enable;
74
75 return TRUE;
76 }
77
78 bool wxToolBarToolBase::Toggle(bool toggle)
79 {
80 wxASSERT_MSG( m_isToggle, _T("can't toggle this tool") );
81
82 if ( m_toggled == toggle )
83 return FALSE;
84
85 m_toggled = toggle;
86
87 return TRUE;
88 }
89
90 bool wxToolBarToolBase::SetToggle(bool toggle)
91 {
92 if ( m_isToggle == toggle )
93 return FALSE;
94
95 m_isToggle = toggle;
96
97 return TRUE;
98 }
99
100 bool wxToolBarToolBase::SetShortHelp(const wxString& help)
101 {
102 if ( m_shortHelpString == help )
103 return FALSE;
104
105 m_shortHelpString = help;
106
107 return TRUE;
108 }
109
110 bool wxToolBarToolBase::SetLongHelp(const wxString& help)
111 {
112 if ( m_longHelpString == help )
113 return FALSE;
114
115 m_longHelpString = help;
116
117 return TRUE;
118 }
119
120 wxToolBarToolBase::~wxToolBarToolBase()
121 {
122 }
123
124 // ----------------------------------------------------------------------------
125 // wxToolBarBase adding/deleting items
126 // ----------------------------------------------------------------------------
127
128 wxToolBarBase::wxToolBarBase()
129 {
130 // the list owns the pointers
131 m_tools.DeleteContents(TRUE);
132
133 m_xMargin = m_yMargin = 0;
134
135 m_maxRows = m_maxCols = 0;
136 }
137
138 wxToolBarToolBase *wxToolBarBase::AddTool(int id,
139 const wxBitmap& bitmap,
140 const wxBitmap& pushedBitmap,
141 bool toggle,
142 wxCoord WXUNUSED(xPos),
143 wxCoord WXUNUSED(yPos),
144 wxObject *clientData,
145 const wxString& helpString1,
146 const wxString& helpString2)
147 {
148 return InsertTool(GetToolsCount(), id, bitmap, pushedBitmap,
149 toggle, clientData, helpString1, helpString2);
150 }
151
152 wxToolBarToolBase *wxToolBarBase::InsertTool(size_t pos,
153 int id,
154 const wxBitmap& bitmap,
155 const wxBitmap& pushedBitmap,
156 bool toggle,
157 wxObject *clientData,
158 const wxString& helpString1,
159 const wxString& helpString2)
160 {
161 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
162 _T("invalid position in wxToolBar::InsertTool()") );
163
164 wxToolBarToolBase *tool = CreateTool(id, bitmap, pushedBitmap, toggle,
165 clientData, helpString1, helpString2);
166
167 if ( !tool || !DoInsertTool(pos, tool) )
168 {
169 delete tool;
170
171 return NULL;
172 }
173
174 m_tools.Insert(pos, tool);
175
176 return tool;
177 }
178
179 wxToolBarToolBase *wxToolBarBase::AddControl(wxControl *control)
180 {
181 return InsertControl(GetToolsCount(), control);
182 }
183
184 wxToolBarToolBase *wxToolBarBase::InsertControl(size_t pos, wxControl *control)
185 {
186 wxCHECK_MSG( control, (wxToolBarToolBase *)NULL,
187 _T("toolbar: can't insert NULL control") );
188
189 wxCHECK_MSG( control->GetParent() == this, (wxToolBarToolBase *)NULL,
190 _T("control must have toolbar as parent") );
191
192 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
193 _T("invalid position in wxToolBar::InsertControl()") );
194
195 wxToolBarToolBase *tool = CreateTool(control);
196
197 if ( !tool || !DoInsertTool(pos, tool) )
198 {
199 delete tool;
200
201 return NULL;
202 }
203
204 m_tools.Insert(pos, tool);
205
206 return tool;
207 }
208
209 wxToolBarToolBase *wxToolBarBase::AddSeparator()
210 {
211 return InsertSeparator(GetToolsCount());
212 }
213
214 wxToolBarToolBase *wxToolBarBase::InsertSeparator(size_t pos)
215 {
216 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
217 _T("invalid position in wxToolBar::InsertSeparator()") );
218
219 wxToolBarToolBase *tool = CreateTool(wxID_SEPARATOR,
220 wxNullBitmap, wxNullBitmap,
221 FALSE, (wxObject *)NULL,
222 wxEmptyString, wxEmptyString);
223
224 if ( !tool || !DoInsertTool(pos, tool) )
225 {
226 delete tool;
227
228 return NULL;
229 }
230
231 m_tools.Insert(pos, tool);
232
233 return tool;
234 }
235
236 wxToolBarToolBase *wxToolBarBase::RemoveTool(int id)
237 {
238 size_t pos = 0;
239 wxToolBarToolsList::Node *node;
240 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
241 {
242 if ( node->GetData()->GetId() == id )
243 break;
244
245 pos++;
246 }
247
248 if ( !node )
249 {
250 // don't give any error messages - sometimes we might call RemoveTool()
251 // without knowing whether the tool is or not in the toolbar
252 return (wxToolBarToolBase *)NULL;
253 }
254
255 wxToolBarToolBase *tool = node->GetData();
256 if ( !DoDeleteTool(pos, tool) )
257 {
258 return (wxToolBarToolBase *)NULL;
259 }
260
261 // the node would delete the data, so set it to NULL to avoid this
262 node->SetData(NULL);
263
264 m_tools.DeleteNode(node);
265
266 return tool;
267 }
268
269 bool wxToolBarBase::DeleteToolByPos(size_t pos)
270 {
271 wxCHECK_MSG( pos < GetToolsCount(), FALSE,
272 _T("invalid position in wxToolBar::DeleteToolByPos()") );
273
274 wxToolBarToolsList::Node *node = m_tools.Item(pos);
275
276 if ( !DoDeleteTool(pos, node->GetData()) )
277 {
278 return FALSE;
279 }
280
281 m_tools.DeleteNode(node);
282
283 return TRUE;
284 }
285
286 bool wxToolBarBase::DeleteTool(int id)
287 {
288 size_t pos = 0;
289 wxToolBarToolsList::Node *node;
290 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
291 {
292 if ( node->GetData()->GetId() == id )
293 break;
294
295 pos++;
296 }
297
298 if ( !node || !DoDeleteTool(pos, node->GetData()) )
299 {
300 return FALSE;
301 }
302
303 m_tools.DeleteNode(node);
304
305 return TRUE;
306 }
307
308 wxToolBarToolBase *wxToolBarBase::FindById(int id) const
309 {
310 wxToolBarToolBase *tool = (wxToolBarToolBase *)NULL;
311
312 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
313 node;
314 node = node->GetNext() )
315 {
316 tool = node->GetData();
317 if ( tool->GetId() == id )
318 {
319 // found
320 break;
321 }
322
323 tool = NULL;
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