]> git.saurik.com Git - wxWidgets.git/blob - src/common/tbarbase.cpp
fix for group names containing ']'
[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 #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
324 return tool;
325 }
326
327 void wxToolBarBase::ClearTools()
328 {
329 m_tools.Clear();
330 }
331
332 bool wxToolBarBase::Realize()
333 {
334 return TRUE;
335 }
336
337 wxToolBarBase::~wxToolBarBase()
338 {
339 }
340
341 // ----------------------------------------------------------------------------
342 // wxToolBarBase tools state
343 // ----------------------------------------------------------------------------
344
345 void wxToolBarBase::EnableTool(int id, bool enable)
346 {
347 wxToolBarToolBase *tool = FindById(id);
348 if ( tool )
349 {
350 if ( tool->Enable(enable) )
351 {
352 DoEnableTool(tool, enable);
353 }
354 }
355 }
356
357 void wxToolBarBase::ToggleTool(int id, bool toggle)
358 {
359 wxToolBarToolBase *tool = FindById(id);
360 if ( tool && tool->CanBeToggled() )
361 {
362 if ( tool->Toggle(toggle) )
363 {
364 DoToggleTool(tool, toggle);
365 }
366 }
367 }
368
369 void wxToolBarBase::SetToggle(int id, bool toggle)
370 {
371 wxToolBarToolBase *tool = FindById(id);
372 if ( tool )
373 {
374 if ( tool->SetToggle(toggle) )
375 {
376 DoSetToggle(tool, toggle);
377 }
378 }
379 }
380
381 void wxToolBarBase::SetToolShortHelp(int id, const wxString& help)
382 {
383 wxToolBarToolBase *tool = FindById(id);
384 if ( tool )
385 {
386 (void)tool->SetShortHelp(help);
387 }
388 }
389
390 void wxToolBarBase::SetToolLongHelp(int id, const wxString& help)
391 {
392 wxToolBarToolBase *tool = FindById(id);
393 if ( tool )
394 {
395 (void)tool->SetLongHelp(help);
396 }
397 }
398
399 wxObject *wxToolBarBase::GetToolClientData(int id) const
400 {
401 wxToolBarToolBase *tool = FindById(id);
402
403 return tool ? tool->GetClientData() : (wxObject *)NULL;
404 }
405
406 void wxToolBarBase::SetToolClientData(int id, wxObject *clientData)
407 {
408 wxToolBarToolBase *tool = FindById(id);
409
410 wxCHECK_RET( tool, _T("no such tool in wxToolBar::SetToolClientData") );
411
412 tool->SetClientData(clientData);
413 }
414
415 bool wxToolBarBase::GetToolState(int id) const
416 {
417 wxToolBarToolBase *tool = FindById(id);
418 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
419
420 return tool->IsToggled();
421 }
422
423 bool wxToolBarBase::GetToolEnabled(int id) const
424 {
425 wxToolBarToolBase *tool = FindById(id);
426 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
427
428 return tool->IsEnabled();
429 }
430
431 wxString wxToolBarBase::GetToolShortHelp(int id) const
432 {
433 wxToolBarToolBase *tool = FindById(id);
434 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
435
436 return tool->GetShortHelp();
437 }
438
439 wxString wxToolBarBase::GetToolLongHelp(int id) const
440 {
441 wxToolBarToolBase *tool = FindById(id);
442 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
443
444 return tool->GetLongHelp();
445 }
446
447 // ----------------------------------------------------------------------------
448 // wxToolBarBase geometry
449 // ----------------------------------------------------------------------------
450
451 void wxToolBarBase::SetMargins(int x, int y)
452 {
453 m_xMargin = x;
454 m_yMargin = y;
455 }
456
457 void wxToolBarBase::SetRows(int WXUNUSED(nRows))
458 {
459 // nothing
460 }
461
462 // ----------------------------------------------------------------------------
463 // event processing
464 // ----------------------------------------------------------------------------
465
466 // Only allow toggle if returns TRUE
467 bool wxToolBarBase::OnLeftClick(int id, bool toggleDown)
468 {
469 wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, id);
470 event.SetEventObject(this);
471 event.SetExtraLong((long) toggleDown);
472
473 // Send events to this toolbar instead (and thence up the window hierarchy)
474 GetEventHandler()->ProcessEvent(event);
475
476 return TRUE;
477 }
478
479 // Call when right button down.
480 void wxToolBarBase::OnRightClick(int id,
481 long WXUNUSED(x),
482 long WXUNUSED(y))
483 {
484 wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, id);
485 event.SetEventObject(this);
486 event.SetInt(id);
487
488 GetEventHandler()->ProcessEvent(event);
489 }
490
491 // Called when the mouse cursor enters a tool bitmap (no button pressed).
492 // Argument is -1 if mouse is exiting the toolbar.
493 // Note that for this event, the id of the window is used,
494 // and the integer parameter of wxCommandEvent is used to retrieve
495 // the tool id.
496 void wxToolBarBase::OnMouseEnter(int id)
497 {
498 wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, GetId());
499 event.SetEventObject(this);
500 event.SetInt(id);
501
502 (void)GetEventHandler()->ProcessEvent(event);
503
504 wxToolBarToolBase *tool = FindById(id);
505 if ( !tool || !tool->GetLongHelp() )
506 return;
507
508 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
509 if ( !frame )
510 return;
511
512 frame->SetStatusText(tool->GetLongHelp());
513 }
514
515 // ----------------------------------------------------------------------------
516 // UI updates
517 // ----------------------------------------------------------------------------
518
519 void wxToolBarBase::OnIdle(wxIdleEvent& event)
520 {
521 DoToolbarUpdates();
522
523 event.Skip();
524 }
525
526 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
527 void wxToolBarBase::DoToolbarUpdates()
528 {
529 wxEvtHandler* evtHandler = GetEventHandler();
530
531 for ( wxToolBarToolsList::Node* node = m_tools.GetFirst();
532 node;
533 node = node->GetNext() )
534 {
535 int id = node->GetData()->GetId();
536
537 wxUpdateUIEvent event(id);
538 event.SetEventObject(this);
539
540 if ( evtHandler->ProcessEvent(event) )
541 {
542 if ( event.GetSetEnabled() )
543 EnableTool(id, event.GetEnabled());
544 if ( event.GetSetChecked() )
545 ToggleTool(id, event.GetChecked());
546 #if 0
547 if ( event.GetSetText() )
548 // Set tooltip?
549 #endif // 0
550 }
551 }
552 }
553
554 #endif // wxUSE_TOOLBAR