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