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