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