]> git.saurik.com Git - wxWidgets.git/blob - src/common/tbarbase.cpp
No changes, just slightly simplify the tool insertion code.
[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, CreateTool(wxID_SEPARATOR,
263 wxEmptyString,
264 wxNullBitmap, wxNullBitmap,
265 wxITEM_SEPARATOR, NULL,
266 wxEmptyString, wxEmptyString));
267 }
268
269 wxToolBarToolBase *wxToolBarBase::RemoveTool(int id)
270 {
271 size_t pos = 0;
272 wxToolBarToolsList::compatibility_iterator node;
273 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
274 {
275 if ( node->GetData()->GetId() == id )
276 break;
277
278 pos++;
279 }
280
281 if ( !node )
282 {
283 // don't give any error messages - sometimes we might call RemoveTool()
284 // without knowing whether the tool is or not in the toolbar
285 return NULL;
286 }
287
288 wxToolBarToolBase *tool = node->GetData();
289 wxCHECK_MSG( tool, NULL, "NULL tool in the tools list?" );
290
291 if ( !DoDeleteTool(pos, tool) )
292 return NULL;
293
294 m_tools.Erase(node);
295
296 tool->Detach();
297
298 return tool;
299 }
300
301 bool wxToolBarBase::DeleteToolByPos(size_t pos)
302 {
303 wxCHECK_MSG( pos < GetToolsCount(), false,
304 wxT("invalid position in wxToolBar::DeleteToolByPos()") );
305
306 wxToolBarToolsList::compatibility_iterator node = m_tools.Item(pos);
307
308 if ( !DoDeleteTool(pos, node->GetData()) )
309 {
310 return false;
311 }
312
313 delete node->GetData();
314 m_tools.Erase(node);
315
316 return true;
317 }
318
319 bool wxToolBarBase::DeleteTool(int id)
320 {
321 size_t pos = 0;
322 wxToolBarToolsList::compatibility_iterator node;
323 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
324 {
325 if ( node->GetData()->GetId() == id )
326 break;
327
328 pos++;
329 }
330
331 if ( !node || !DoDeleteTool(pos, node->GetData()) )
332 {
333 return false;
334 }
335
336 delete node->GetData();
337 m_tools.Erase(node);
338
339 return true;
340 }
341
342 wxToolBarToolBase *wxToolBarBase::FindById(int id) const
343 {
344 wxToolBarToolBase *tool = NULL;
345
346 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
347 node;
348 node = node->GetNext() )
349 {
350 tool = node->GetData();
351 if ( tool->GetId() == id )
352 {
353 // found
354 break;
355 }
356
357 tool = NULL;
358 }
359
360 return tool;
361 }
362
363 void wxToolBarBase::UnToggleRadioGroup(wxToolBarToolBase *tool)
364 {
365 wxCHECK_RET( tool, wxT("NULL tool in wxToolBarTool::UnToggleRadioGroup") );
366
367 if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO )
368 return;
369
370 wxToolBarToolsList::compatibility_iterator node = m_tools.Find(tool);
371 wxCHECK_RET( node, wxT("invalid tool in wxToolBarTool::UnToggleRadioGroup") );
372
373 wxToolBarToolsList::compatibility_iterator nodeNext = node->GetNext();
374 while ( nodeNext )
375 {
376 wxToolBarToolBase *toolNext = nodeNext->GetData();
377
378 if ( !toolNext->IsButton() || toolNext->GetKind() != wxITEM_RADIO )
379 break;
380
381 if ( toolNext->Toggle(false) )
382 {
383 DoToggleTool(toolNext, false);
384 }
385
386 nodeNext = nodeNext->GetNext();
387 }
388
389 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
390 while ( nodePrev )
391 {
392 wxToolBarToolBase *toolNext = nodePrev->GetData();
393
394 if ( !toolNext->IsButton() || toolNext->GetKind() != wxITEM_RADIO )
395 break;
396
397 if ( toolNext->Toggle(false) )
398 {
399 DoToggleTool(toolNext, false);
400 }
401
402 nodePrev = nodePrev->GetPrevious();
403 }
404 }
405
406 void wxToolBarBase::ClearTools()
407 {
408 while ( GetToolsCount() )
409 {
410 DeleteToolByPos(0);
411 }
412 }
413
414 void wxToolBarBase::AdjustToolBitmapSize()
415 {
416 const wxSize sizeOrig(m_defaultWidth, m_defaultHeight);
417
418 wxSize sizeActual(sizeOrig);
419
420 for ( wxToolBarToolsList::const_iterator i = m_tools.begin();
421 i != m_tools.end();
422 ++i )
423 {
424 const wxBitmap& bmp = (*i)->GetNormalBitmap();
425 if ( bmp.IsOk() )
426 sizeActual.IncTo(bmp.GetSize());
427 }
428
429 if ( sizeActual != sizeOrig )
430 SetToolBitmapSize(sizeActual);
431 }
432
433 bool wxToolBarBase::Realize()
434 {
435 // check if we have anything to do
436 if ( m_tools.empty() )
437 return false;
438
439 // make sure tool size is larger enough for all all bitmaps to fit in
440 // (this is consistent with what other ports do):
441 AdjustToolBitmapSize();
442
443 return true;
444 }
445
446 wxToolBarBase::~wxToolBarBase()
447 {
448 WX_CLEAR_LIST(wxToolBarToolsList, m_tools);
449
450 // notify the frame that it doesn't have a tool bar any longer to avoid
451 // dangling pointers
452 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
453 if ( frame && frame->GetToolBar() == this )
454 {
455 frame->SetToolBar(NULL);
456 }
457 }
458
459 // ----------------------------------------------------------------------------
460 // wxToolBarBase tools state
461 // ----------------------------------------------------------------------------
462
463 void wxToolBarBase::EnableTool(int id, bool enable)
464 {
465 wxToolBarToolBase *tool = FindById(id);
466 if ( tool )
467 {
468 if ( tool->Enable(enable) )
469 {
470 DoEnableTool(tool, enable);
471 }
472 }
473 }
474
475 void wxToolBarBase::ToggleTool(int id, bool toggle)
476 {
477 wxToolBarToolBase *tool = FindById(id);
478 if ( tool && tool->CanBeToggled() )
479 {
480 if ( tool->Toggle(toggle) )
481 {
482 UnToggleRadioGroup(tool);
483 DoToggleTool(tool, toggle);
484 }
485 }
486 }
487
488 void wxToolBarBase::SetToggle(int id, bool toggle)
489 {
490 wxToolBarToolBase *tool = FindById(id);
491 if ( tool )
492 {
493 if ( tool->SetToggle(toggle) )
494 {
495 DoSetToggle(tool, toggle);
496 }
497 }
498 }
499
500 void wxToolBarBase::SetToolShortHelp(int id, const wxString& help)
501 {
502 wxToolBarToolBase *tool = FindById(id);
503 if ( tool )
504 {
505 (void)tool->SetShortHelp(help);
506 }
507 }
508
509 void wxToolBarBase::SetToolLongHelp(int id, const wxString& help)
510 {
511 wxToolBarToolBase *tool = FindById(id);
512 if ( tool )
513 {
514 (void)tool->SetLongHelp(help);
515 }
516 }
517
518 wxObject *wxToolBarBase::GetToolClientData(int id) const
519 {
520 wxToolBarToolBase *tool = FindById(id);
521
522 return tool ? tool->GetClientData() : NULL;
523 }
524
525 void wxToolBarBase::SetToolClientData(int id, wxObject *clientData)
526 {
527 wxToolBarToolBase *tool = FindById(id);
528
529 wxCHECK_RET( tool, wxT("no such tool in wxToolBar::SetToolClientData") );
530
531 tool->SetClientData(clientData);
532 }
533
534 int wxToolBarBase::GetToolPos(int id) const
535 {
536 size_t pos = 0;
537 wxToolBarToolsList::compatibility_iterator node;
538
539 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
540 {
541 if ( node->GetData()->GetId() == id )
542 return pos;
543
544 pos++;
545 }
546
547 return wxNOT_FOUND;
548 }
549
550 bool wxToolBarBase::GetToolState(int id) const
551 {
552 wxToolBarToolBase *tool = FindById(id);
553 wxCHECK_MSG( tool, false, wxT("no such tool") );
554
555 return tool->IsToggled();
556 }
557
558 bool wxToolBarBase::GetToolEnabled(int id) const
559 {
560 wxToolBarToolBase *tool = FindById(id);
561 wxCHECK_MSG( tool, false, wxT("no such tool") );
562
563 return tool->IsEnabled();
564 }
565
566 wxString wxToolBarBase::GetToolShortHelp(int id) const
567 {
568 wxToolBarToolBase *tool = FindById(id);
569 wxCHECK_MSG( tool, wxEmptyString, wxT("no such tool") );
570
571 return tool->GetShortHelp();
572 }
573
574 wxString wxToolBarBase::GetToolLongHelp(int id) const
575 {
576 wxToolBarToolBase *tool = FindById(id);
577 wxCHECK_MSG( tool, wxEmptyString, wxT("no such tool") );
578
579 return tool->GetLongHelp();
580 }
581
582 // ----------------------------------------------------------------------------
583 // wxToolBarBase geometry
584 // ----------------------------------------------------------------------------
585
586 void wxToolBarBase::SetMargins(int x, int y)
587 {
588 m_xMargin = x;
589 m_yMargin = y;
590 }
591
592 void wxToolBarBase::SetRows(int WXUNUSED(nRows))
593 {
594 // nothing
595 }
596
597 bool wxToolBarBase::IsVertical() const
598 {
599 return HasFlag(wxTB_LEFT | wxTB_RIGHT);
600 }
601
602
603 // ----------------------------------------------------------------------------
604 // event processing
605 // ----------------------------------------------------------------------------
606
607 // Only allow toggle if returns true
608 bool wxToolBarBase::OnLeftClick(int id, bool toggleDown)
609 {
610 wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, id);
611 event.SetEventObject(this);
612
613 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
614 event.SetInt((int)toggleDown);
615
616 // and SetExtraLong() for backwards compatibility
617 event.SetExtraLong((long)toggleDown);
618
619 // Send events to this toolbar instead (and thence up the window hierarchy)
620 HandleWindowEvent(event);
621
622 return true;
623 }
624
625 // Call when right button down.
626 void wxToolBarBase::OnRightClick(int id,
627 long WXUNUSED(x),
628 long WXUNUSED(y))
629 {
630 wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, id);
631 event.SetEventObject(this);
632 event.SetInt(id);
633
634 GetEventHandler()->ProcessEvent(event);
635 }
636
637 // Called when the mouse cursor enters a tool bitmap (no button pressed).
638 // Argument is wxID_ANY if mouse is exiting the toolbar.
639 // Note that for this event, the id of the window is used,
640 // and the integer parameter of wxCommandEvent is used to retrieve
641 // the tool id.
642 void wxToolBarBase::OnMouseEnter(int id)
643 {
644 wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, GetId());
645 event.SetEventObject(this);
646 event.SetInt(id);
647
648 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
649 if ( frame )
650 {
651 wxString help;
652 if ( id != wxID_ANY )
653 {
654 const wxToolBarToolBase * const tool = FindById(id);
655 if ( tool )
656 help = tool->GetLongHelp();
657 }
658
659 // call DoGiveHelp() even if help string is empty to avoid showing the
660 // help for the previously selected tool when another one is selected
661 frame->DoGiveHelp(help, id != wxID_ANY);
662 }
663
664 (void)GetEventHandler()->ProcessEvent(event);
665 }
666
667 // ----------------------------------------------------------------------------
668 // UI updates
669 // ----------------------------------------------------------------------------
670
671 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
672 void wxToolBarBase::UpdateWindowUI(long flags)
673 {
674 wxWindowBase::UpdateWindowUI(flags);
675
676 // don't waste time updating state of tools in a hidden toolbar
677 if ( !IsShown() )
678 return;
679
680 // There is no sense in updating the toolbar UI
681 // if the parent window is about to get destroyed
682 wxWindow *tlw = wxGetTopLevelParent( this );
683 if (tlw && wxPendingDelete.Member( tlw ))
684 return;
685
686 wxEvtHandler* evtHandler = GetEventHandler() ;
687
688 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
689 node;
690 node = node->GetNext() )
691 {
692 wxToolBarToolBase * const tool = node->GetData();
693 if ( tool->IsSeparator() )
694 continue;
695
696 int id = tool->GetId();
697
698 wxUpdateUIEvent event(id);
699 event.SetEventObject(this);
700
701 if ( evtHandler->ProcessEvent(event) )
702 {
703 if ( event.GetSetEnabled() )
704 EnableTool(id, event.GetEnabled());
705 if ( event.GetSetChecked() )
706 ToggleTool(id, event.GetChecked());
707 #if 0
708 if ( event.GetSetText() )
709 // Set tooltip?
710 #endif // 0
711 }
712 }
713 }
714
715 bool wxToolBarBase::SetDropdownMenu(int toolid, wxMenu* menu)
716 {
717 wxToolBarToolBase * const tool = FindById(toolid);
718 wxCHECK_MSG( tool, false, wxT("invalid tool id") );
719
720 wxCHECK_MSG( tool->GetKind() == wxITEM_DROPDOWN, false,
721 wxT("menu can be only associated with drop down tools") );
722
723 tool->SetDropdownMenu(menu);
724
725 return true;
726 }
727
728 #if WXWIN_COMPATIBILITY_2_8
729
730 bool wxCreateGreyedImage(const wxImage& in, wxImage& out)
731 {
732 #if wxUSE_IMAGE
733 out = in.ConvertToGreyscale();
734 if ( out.Ok() )
735 return true;
736 #endif // wxUSE_IMAGE
737 return false;
738 }
739
740 #endif // WXWIN_COMPATIBILITY_2_8
741
742 #endif // wxUSE_TOOLBAR