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