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