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