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