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