]> git.saurik.com Git - wxWidgets.git/blob - src/common/tbarbase.cpp
Fix assert during separator items creation introduced by r64226.
[wxWidgets.git] / src / common / tbarbase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/tbarbase.cpp
3 // Purpose: wxToolBarBase implementation
4 // Author: Julian Smart
5 // Modified by: VZ at 11.12.99 (wxScrollableToolBar split off)
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_TOOLBAR
28
29 #include "wx/toolbar.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/control.h"
33 #include "wx/frame.h"
34 #include "wx/settings.h"
35 #if WXWIN_COMPATIBILITY_2_8
36 #include "wx/image.h"
37 #endif // WXWIN_COMPATIBILITY_2_8
38 #include "wx/menu.h"
39 #endif
40
41 // ----------------------------------------------------------------------------
42 // wxWidgets macros
43 // ----------------------------------------------------------------------------
44
45 BEGIN_EVENT_TABLE(wxToolBarBase, wxControl)
46 END_EVENT_TABLE()
47
48 #include "wx/listimpl.cpp"
49
50 WX_DEFINE_LIST(wxToolBarToolsList)
51
52 // ============================================================================
53 // implementation
54 // ============================================================================
55
56 // ----------------------------------------------------------------------------
57 // wxToolBarToolBase
58 // ----------------------------------------------------------------------------
59
60 IMPLEMENT_DYNAMIC_CLASS(wxToolBarToolBase, wxObject)
61
62 wxToolBarToolBase::~wxToolBarToolBase()
63 {
64 delete m_dropdownMenu;
65 if ( IsControl() )
66 GetControl()->Destroy();
67 }
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( CanBeToggled(), wxT("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 wxItemKind kind = toggle ? wxITEM_CHECK : wxITEM_NORMAL;
95 if ( m_kind == kind )
96 return false;
97
98 m_kind = kind;
99
100 return true;
101 }
102
103 bool wxToolBarToolBase::SetShortHelp(const wxString& help)
104 {
105 if ( m_shortHelpString == help )
106 return false;
107
108 m_shortHelpString = help;
109
110 return true;
111 }
112
113 bool wxToolBarToolBase::SetLongHelp(const wxString& help)
114 {
115 if ( m_longHelpString == help )
116 return false;
117
118 m_longHelpString = help;
119
120 return true;
121 }
122
123
124 void wxToolBarToolBase::SetDropdownMenu(wxMenu* menu)
125 {
126 delete m_dropdownMenu;
127 m_dropdownMenu = menu;
128 }
129
130
131 // ----------------------------------------------------------------------------
132 // wxToolBarBase adding/deleting items
133 // ----------------------------------------------------------------------------
134
135 wxToolBarBase::wxToolBarBase()
136 {
137 // the list owns the pointers
138 m_xMargin = m_yMargin = 0;
139 m_maxRows = m_maxCols = 0;
140 m_toolPacking = m_toolSeparation = 0;
141 m_defaultWidth = 16;
142 m_defaultHeight = 15;
143 }
144
145 void wxToolBarBase::FixupStyle()
146 {
147 if ( !HasFlag(wxTB_TOP | wxTB_LEFT | wxTB_RIGHT | wxTB_BOTTOM) )
148 {
149 // this is the default
150 m_windowStyle |= wxTB_TOP;
151 }
152 }
153
154 wxToolBarToolBase *wxToolBarBase::DoAddTool(int id,
155 const wxString& label,
156 const wxBitmap& bitmap,
157 const wxBitmap& bmpDisabled,
158 wxItemKind kind,
159 const wxString& shortHelp,
160 const wxString& longHelp,
161 wxObject *clientData,
162 wxCoord WXUNUSED(xPos),
163 wxCoord WXUNUSED(yPos))
164 {
165 InvalidateBestSize();
166 return InsertTool(GetToolsCount(), id, label, bitmap, bmpDisabled,
167 kind, shortHelp, longHelp, clientData);
168 }
169
170 wxToolBarToolBase *wxToolBarBase::InsertTool(size_t pos,
171 int id,
172 const wxString& label,
173 const wxBitmap& bitmap,
174 const wxBitmap& bmpDisabled,
175 wxItemKind kind,
176 const wxString& shortHelp,
177 const wxString& longHelp,
178 wxObject *clientData)
179 {
180 wxCHECK_MSG( pos <= GetToolsCount(), NULL,
181 wxT("invalid position in wxToolBar::InsertTool()") );
182
183 return DoInsertNewTool(pos, CreateTool(id, label, bitmap, bmpDisabled, kind,
184 clientData, shortHelp, longHelp));
185 }
186
187 wxToolBarToolBase *wxToolBarBase::AddTool(wxToolBarToolBase *tool)
188 {
189 return InsertTool(GetToolsCount(), tool);
190 }
191
192 wxToolBarToolBase *
193 wxToolBarBase::InsertTool(size_t pos, wxToolBarToolBase *tool)
194 {
195 wxCHECK_MSG( pos <= GetToolsCount(), NULL,
196 wxT("invalid position in wxToolBar::InsertTool()") );
197
198 if ( !tool || !DoInsertTool(pos, tool) )
199 {
200 return NULL;
201 }
202
203 m_tools.Insert(pos, tool);
204 tool->Attach(this);
205
206 return tool;
207 }
208
209 wxToolBarToolBase *
210 wxToolBarBase::AddControl(wxControl *control, const wxString& label)
211 {
212 return InsertControl(GetToolsCount(), control, label);
213 }
214
215 wxToolBarToolBase *
216 wxToolBarBase::InsertControl(size_t pos,
217 wxControl *control,
218 const wxString& label)
219 {
220 wxCHECK_MSG( control, NULL,
221 wxT("toolbar: can't insert NULL control") );
222
223 wxCHECK_MSG( control->GetParent() == this, NULL,
224 wxT("control must have toolbar as parent") );
225
226 return DoInsertNewTool(pos, CreateTool(control, label));
227 }
228
229 wxControl *wxToolBarBase::FindControl( int id )
230 {
231 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
232 node;
233 node = node->GetNext() )
234 {
235 const wxToolBarToolBase * const tool = node->GetData();
236 if ( tool->IsControl() )
237 {
238 wxControl * const control = tool->GetControl();
239
240 if ( !control )
241 {
242 wxFAIL_MSG( wxT("NULL control in toolbar?") );
243 }
244 else if ( control->GetId() == id )
245 {
246 // found
247 return control;
248 }
249 }
250 }
251
252 return NULL;
253 }
254
255 wxToolBarToolBase *wxToolBarBase::AddSeparator()
256 {
257 return InsertSeparator(GetToolsCount());
258 }
259
260 wxToolBarToolBase *wxToolBarBase::InsertSeparator(size_t pos)
261 {
262 return DoInsertNewTool(pos, CreateSeparator());
263 }
264
265 wxToolBarToolBase *wxToolBarBase::AddStretchableSpace()
266 {
267 return InsertStretchableSpace(GetToolsCount());
268 }
269
270 wxToolBarToolBase *wxToolBarBase::InsertStretchableSpace(size_t pos)
271 {
272 wxToolBarToolBase * const tool = CreateSeparator();
273 if ( tool )
274 {
275 // this is a hack but we know that all the current implementations
276 // don't really use the tool when it's created, they will do it
277 // InsertTool() at earliest and maybe even in Realize() much later
278 //
279 // so we can create the tool as a plain separator and mark it as being
280 // a stretchable space later
281 tool->MakeStretchable();
282 }
283
284 return DoInsertNewTool(pos, tool);
285 }
286
287 wxToolBarToolBase *wxToolBarBase::RemoveTool(int id)
288 {
289 size_t pos = 0;
290 wxToolBarToolsList::compatibility_iterator node;
291 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
292 {
293 if ( node->GetData()->GetId() == id )
294 break;
295
296 pos++;
297 }
298
299 if ( !node )
300 {
301 // don't give any error messages - sometimes we might call RemoveTool()
302 // without knowing whether the tool is or not in the toolbar
303 return NULL;
304 }
305
306 wxToolBarToolBase *tool = node->GetData();
307 wxCHECK_MSG( tool, NULL, "NULL tool in the tools list?" );
308
309 if ( !DoDeleteTool(pos, tool) )
310 return NULL;
311
312 m_tools.Erase(node);
313
314 tool->Detach();
315
316 return tool;
317 }
318
319 bool wxToolBarBase::DeleteToolByPos(size_t pos)
320 {
321 wxCHECK_MSG( pos < GetToolsCount(), false,
322 wxT("invalid position in wxToolBar::DeleteToolByPos()") );
323
324 wxToolBarToolsList::compatibility_iterator node = m_tools.Item(pos);
325
326 if ( !DoDeleteTool(pos, node->GetData()) )
327 {
328 return false;
329 }
330
331 delete node->GetData();
332 m_tools.Erase(node);
333
334 return true;
335 }
336
337 bool wxToolBarBase::DeleteTool(int id)
338 {
339 size_t pos = 0;
340 wxToolBarToolsList::compatibility_iterator node;
341 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
342 {
343 if ( node->GetData()->GetId() == id )
344 break;
345
346 pos++;
347 }
348
349 if ( !node || !DoDeleteTool(pos, node->GetData()) )
350 {
351 return false;
352 }
353
354 delete node->GetData();
355 m_tools.Erase(node);
356
357 return true;
358 }
359
360 wxToolBarToolBase *wxToolBarBase::FindById(int id) const
361 {
362 wxToolBarToolBase *tool = NULL;
363
364 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
365 node;
366 node = node->GetNext() )
367 {
368 tool = node->GetData();
369 if ( tool->GetId() == id )
370 {
371 // found
372 break;
373 }
374
375 tool = NULL;
376 }
377
378 return tool;
379 }
380
381 void wxToolBarBase::UnToggleRadioGroup(wxToolBarToolBase *tool)
382 {
383 wxCHECK_RET( tool, wxT("NULL tool in wxToolBarTool::UnToggleRadioGroup") );
384
385 if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO )
386 return;
387
388 wxToolBarToolsList::compatibility_iterator node = m_tools.Find(tool);
389 wxCHECK_RET( node, wxT("invalid tool in wxToolBarTool::UnToggleRadioGroup") );
390
391 wxToolBarToolsList::compatibility_iterator nodeNext = node->GetNext();
392 while ( nodeNext )
393 {
394 wxToolBarToolBase *toolNext = nodeNext->GetData();
395
396 if ( !toolNext->IsButton() || toolNext->GetKind() != wxITEM_RADIO )
397 break;
398
399 if ( toolNext->Toggle(false) )
400 {
401 DoToggleTool(toolNext, false);
402 }
403
404 nodeNext = nodeNext->GetNext();
405 }
406
407 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
408 while ( nodePrev )
409 {
410 wxToolBarToolBase *toolNext = nodePrev->GetData();
411
412 if ( !toolNext->IsButton() || toolNext->GetKind() != wxITEM_RADIO )
413 break;
414
415 if ( toolNext->Toggle(false) )
416 {
417 DoToggleTool(toolNext, false);
418 }
419
420 nodePrev = nodePrev->GetPrevious();
421 }
422 }
423
424 void wxToolBarBase::ClearTools()
425 {
426 while ( GetToolsCount() )
427 {
428 DeleteToolByPos(0);
429 }
430 }
431
432 void wxToolBarBase::AdjustToolBitmapSize()
433 {
434 const wxSize sizeOrig(m_defaultWidth, m_defaultHeight);
435
436 wxSize sizeActual(sizeOrig);
437
438 for ( wxToolBarToolsList::const_iterator i = m_tools.begin();
439 i != m_tools.end();
440 ++i )
441 {
442 const wxBitmap& bmp = (*i)->GetNormalBitmap();
443 if ( bmp.IsOk() )
444 sizeActual.IncTo(bmp.GetSize());
445 }
446
447 if ( sizeActual != sizeOrig )
448 SetToolBitmapSize(sizeActual);
449 }
450
451 bool wxToolBarBase::Realize()
452 {
453 // check if we have anything to do
454 if ( m_tools.empty() )
455 return false;
456
457 // make sure tool size is large enough for all bitmaps to fit in
458 AdjustToolBitmapSize();
459
460 return true;
461 }
462
463 wxToolBarBase::~wxToolBarBase()
464 {
465 WX_CLEAR_LIST(wxToolBarToolsList, m_tools);
466
467 // notify the frame that it doesn't have a tool bar any longer to avoid
468 // dangling pointers
469 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
470 if ( frame && frame->GetToolBar() == this )
471 {
472 frame->SetToolBar(NULL);
473 }
474 }
475
476 // ----------------------------------------------------------------------------
477 // wxToolBarBase tools state
478 // ----------------------------------------------------------------------------
479
480 void wxToolBarBase::EnableTool(int id, bool enable)
481 {
482 wxToolBarToolBase *tool = FindById(id);
483 if ( tool )
484 {
485 if ( tool->Enable(enable) )
486 {
487 DoEnableTool(tool, enable);
488 }
489 }
490 }
491
492 void wxToolBarBase::ToggleTool(int id, bool toggle)
493 {
494 wxToolBarToolBase *tool = FindById(id);
495 if ( tool && tool->CanBeToggled() )
496 {
497 if ( tool->Toggle(toggle) )
498 {
499 UnToggleRadioGroup(tool);
500 DoToggleTool(tool, toggle);
501 }
502 }
503 }
504
505 void wxToolBarBase::SetToggle(int id, bool toggle)
506 {
507 wxToolBarToolBase *tool = FindById(id);
508 if ( tool )
509 {
510 if ( tool->SetToggle(toggle) )
511 {
512 DoSetToggle(tool, toggle);
513 }
514 }
515 }
516
517 void wxToolBarBase::SetToolShortHelp(int id, const wxString& help)
518 {
519 wxToolBarToolBase *tool = FindById(id);
520 if ( tool )
521 {
522 (void)tool->SetShortHelp(help);
523 }
524 }
525
526 void wxToolBarBase::SetToolLongHelp(int id, const wxString& help)
527 {
528 wxToolBarToolBase *tool = FindById(id);
529 if ( tool )
530 {
531 (void)tool->SetLongHelp(help);
532 }
533 }
534
535 wxObject *wxToolBarBase::GetToolClientData(int id) const
536 {
537 wxToolBarToolBase *tool = FindById(id);
538
539 return tool ? tool->GetClientData() : NULL;
540 }
541
542 void wxToolBarBase::SetToolClientData(int id, wxObject *clientData)
543 {
544 wxToolBarToolBase *tool = FindById(id);
545
546 wxCHECK_RET( tool, wxT("no such tool in wxToolBar::SetToolClientData") );
547
548 tool->SetClientData(clientData);
549 }
550
551 int wxToolBarBase::GetToolPos(int id) const
552 {
553 size_t pos = 0;
554 wxToolBarToolsList::compatibility_iterator node;
555
556 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
557 {
558 if ( node->GetData()->GetId() == id )
559 return pos;
560
561 pos++;
562 }
563
564 return wxNOT_FOUND;
565 }
566
567 bool wxToolBarBase::GetToolState(int id) const
568 {
569 wxToolBarToolBase *tool = FindById(id);
570 wxCHECK_MSG( tool, false, wxT("no such tool") );
571
572 return tool->IsToggled();
573 }
574
575 bool wxToolBarBase::GetToolEnabled(int id) const
576 {
577 wxToolBarToolBase *tool = FindById(id);
578 wxCHECK_MSG( tool, false, wxT("no such tool") );
579
580 return tool->IsEnabled();
581 }
582
583 wxString wxToolBarBase::GetToolShortHelp(int id) const
584 {
585 wxToolBarToolBase *tool = FindById(id);
586 wxCHECK_MSG( tool, wxEmptyString, wxT("no such tool") );
587
588 return tool->GetShortHelp();
589 }
590
591 wxString wxToolBarBase::GetToolLongHelp(int id) const
592 {
593 wxToolBarToolBase *tool = FindById(id);
594 wxCHECK_MSG( tool, wxEmptyString, wxT("no such tool") );
595
596 return tool->GetLongHelp();
597 }
598
599 // ----------------------------------------------------------------------------
600 // wxToolBarBase geometry
601 // ----------------------------------------------------------------------------
602
603 void wxToolBarBase::SetMargins(int x, int y)
604 {
605 m_xMargin = x;
606 m_yMargin = y;
607 }
608
609 void wxToolBarBase::SetRows(int WXUNUSED(nRows))
610 {
611 // nothing
612 }
613
614 bool wxToolBarBase::IsVertical() const
615 {
616 return HasFlag(wxTB_LEFT | wxTB_RIGHT);
617 }
618
619
620 // ----------------------------------------------------------------------------
621 // event processing
622 // ----------------------------------------------------------------------------
623
624 // Only allow toggle if returns true
625 bool wxToolBarBase::OnLeftClick(int id, bool toggleDown)
626 {
627 wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, id);
628 event.SetEventObject(this);
629
630 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
631 event.SetInt((int)toggleDown);
632
633 // and SetExtraLong() for backwards compatibility
634 event.SetExtraLong((long)toggleDown);
635
636 // Send events to this toolbar instead (and thence up the window hierarchy)
637 HandleWindowEvent(event);
638
639 return true;
640 }
641
642 // Call when right button down.
643 void wxToolBarBase::OnRightClick(int id,
644 long WXUNUSED(x),
645 long WXUNUSED(y))
646 {
647 wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, id);
648 event.SetEventObject(this);
649 event.SetInt(id);
650
651 GetEventHandler()->ProcessEvent(event);
652 }
653
654 // Called when the mouse cursor enters a tool bitmap (no button pressed).
655 // Argument is wxID_ANY if mouse is exiting the toolbar.
656 // Note that for this event, the id of the window is used,
657 // and the integer parameter of wxCommandEvent is used to retrieve
658 // the tool id.
659 void wxToolBarBase::OnMouseEnter(int id)
660 {
661 wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, GetId());
662 event.SetEventObject(this);
663 event.SetInt(id);
664
665 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
666 if ( frame )
667 {
668 wxString help;
669 if ( id != wxID_ANY )
670 {
671 const wxToolBarToolBase * const tool = FindById(id);
672 if ( tool )
673 help = tool->GetLongHelp();
674 }
675
676 // call DoGiveHelp() even if help string is empty to avoid showing the
677 // help for the previously selected tool when another one is selected
678 frame->DoGiveHelp(help, id != wxID_ANY);
679 }
680
681 (void)GetEventHandler()->ProcessEvent(event);
682 }
683
684 // ----------------------------------------------------------------------------
685 // UI updates
686 // ----------------------------------------------------------------------------
687
688 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
689 void wxToolBarBase::UpdateWindowUI(long flags)
690 {
691 wxWindowBase::UpdateWindowUI(flags);
692
693 // don't waste time updating state of tools in a hidden toolbar
694 if ( !IsShown() )
695 return;
696
697 // There is no sense in updating the toolbar UI
698 // if the parent window is about to get destroyed
699 wxWindow *tlw = wxGetTopLevelParent( this );
700 if (tlw && wxPendingDelete.Member( tlw ))
701 return;
702
703 wxEvtHandler* evtHandler = GetEventHandler() ;
704
705 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
706 node;
707 node = node->GetNext() )
708 {
709 wxToolBarToolBase * const tool = node->GetData();
710 if ( tool->IsSeparator() )
711 continue;
712
713 int id = tool->GetId();
714
715 wxUpdateUIEvent event(id);
716 event.SetEventObject(this);
717
718 if ( evtHandler->ProcessEvent(event) )
719 {
720 if ( event.GetSetEnabled() )
721 EnableTool(id, event.GetEnabled());
722 if ( event.GetSetChecked() )
723 ToggleTool(id, event.GetChecked());
724 #if 0
725 if ( event.GetSetText() )
726 // Set tooltip?
727 #endif // 0
728 }
729 }
730 }
731
732 bool wxToolBarBase::SetDropdownMenu(int toolid, wxMenu* menu)
733 {
734 wxToolBarToolBase * const tool = FindById(toolid);
735 wxCHECK_MSG( tool, false, wxT("invalid tool id") );
736
737 wxCHECK_MSG( tool->GetKind() == wxITEM_DROPDOWN, false,
738 wxT("menu can be only associated with drop down tools") );
739
740 tool->SetDropdownMenu(menu);
741
742 return true;
743 }
744
745 #if WXWIN_COMPATIBILITY_2_8
746
747 bool wxCreateGreyedImage(const wxImage& in, wxImage& out)
748 {
749 #if wxUSE_IMAGE
750 out = in.ConvertToGreyscale();
751 if ( out.Ok() )
752 return true;
753 #endif // wxUSE_IMAGE
754 return false;
755 }
756
757 #endif // WXWIN_COMPATIBILITY_2_8
758
759 #endif // wxUSE_TOOLBAR