]> git.saurik.com Git - wxWidgets.git/blob - src/univ/toolbar.cpp
follow up parent chain to properly support modal dialog parents, see #15383
[wxWidgets.git] / src / univ / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/toolbar.cpp
3 // Purpose: implementation of wxToolBar for wxUniversal
4 // Author: Robert Roebling, Vadim Zeitlin (universalization)
5 // Modified by:
6 // Created: 20.02.02
7 // Copyright: (c) 2001 Robert Roebling,
8 // (c) 2002 SciTech Software, Inc. (www.scitechsoft.com)
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/utils.h"
33 #include "wx/app.h"
34 #include "wx/log.h"
35 #include "wx/frame.h"
36 #include "wx/dc.h"
37 #include "wx/image.h"
38 #endif
39
40 #include "wx/univ/renderer.h"
41
42 // ----------------------------------------------------------------------------
43 // wxStdToolbarInputHandler: translates SPACE and ENTER keys and the left mouse
44 // click into button press/release actions
45 // ----------------------------------------------------------------------------
46
47 class WXDLLEXPORT wxStdToolbarInputHandler : public wxStdInputHandler
48 {
49 public:
50 wxStdToolbarInputHandler(wxInputHandler *inphand);
51
52 virtual bool HandleKey(wxInputConsumer *consumer,
53 const wxKeyEvent& event,
54 bool pressed);
55 virtual bool HandleMouse(wxInputConsumer *consumer,
56 const wxMouseEvent& event);
57 virtual bool HandleMouseMove(wxInputConsumer *consumer, const wxMouseEvent& event);
58 virtual bool HandleFocus(wxInputConsumer *consumer, const wxFocusEvent& event);
59 virtual bool HandleActivation(wxInputConsumer *consumer, bool activated);
60
61 private:
62 wxWindow *m_winCapture;
63 wxToolBarToolBase *m_toolCapture;
64 wxToolBarToolBase *m_toolLast;
65 };
66
67 // ----------------------------------------------------------------------------
68 // constants
69 // ----------------------------------------------------------------------------
70
71 // value meaning that m_widthSeparator is not initialized
72 static const wxCoord INVALID_WIDTH = wxDefaultCoord;
73
74 // ----------------------------------------------------------------------------
75 // wxToolBarTool: our implementation of wxToolBarToolBase
76 // ----------------------------------------------------------------------------
77
78 class WXDLLEXPORT wxToolBarTool : public wxToolBarToolBase
79 {
80 public:
81 wxToolBarTool(wxToolBar *tbar,
82 int id,
83 const wxString& label,
84 const wxBitmap& bmpNormal,
85 const wxBitmap& bmpDisabled,
86 wxItemKind kind,
87 wxObject *clientData,
88 const wxString& shortHelp,
89 const wxString& longHelp)
90 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
91 clientData, shortHelp, longHelp)
92 {
93 // no position yet
94 m_x =
95 m_y = wxDefaultCoord;
96 m_width =
97 m_height = 0;
98
99 // not pressed yet
100 m_isInverted = false;
101
102 // mouse not here yet
103 m_underMouse = false;
104 }
105
106 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
107 : wxToolBarToolBase(tbar, control, label)
108 {
109 // no position yet
110 m_x =
111 m_y = wxDefaultCoord;
112 m_width =
113 m_height = 0;
114
115 // not pressed yet
116 m_isInverted = false;
117
118 // mouse not here yet
119 m_underMouse = false;
120 }
121
122 // is this tool pressed, even temporarily? (this is different from being
123 // permanently toggled which is what IsToggled() returns)
124 bool IsPressed() const
125 { return CanBeToggled() ? IsToggled() != m_isInverted : m_isInverted; }
126
127 // are we temporarily pressed/unpressed?
128 bool IsInverted() const { return m_isInverted; }
129
130 // press the tool temporarily by inverting its toggle state
131 void Invert() { m_isInverted = !m_isInverted; }
132
133 // Set underMouse
134 void SetUnderMouse( bool under = true ) { m_underMouse = under; }
135 bool IsUnderMouse() { return m_underMouse; }
136
137 public:
138 // the tool position (for controls)
139 wxCoord m_x;
140 wxCoord m_y;
141 wxCoord m_width;
142 wxCoord m_height;
143
144 private:
145 // true if the tool is pressed
146 bool m_isInverted;
147
148 // true if the tool is under the mouse
149 bool m_underMouse;
150 };
151
152 // ============================================================================
153 // wxToolBar implementation
154 // ============================================================================
155
156 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
157
158 // ----------------------------------------------------------------------------
159 // wxToolBar creation
160 // ----------------------------------------------------------------------------
161
162 void wxToolBar::Init()
163 {
164 // no tools yet
165 m_needsLayout = false;
166
167 // unknown widths for the tools and separators
168 m_widthSeparator = INVALID_WIDTH;
169
170 m_maxWidth =
171 m_maxHeight = 0;
172
173 wxRenderer *renderer = GetRenderer();
174
175 SetToolBitmapSize(renderer->GetToolBarButtonSize(&m_widthSeparator));
176 SetMargins(renderer->GetToolBarMargin());
177 }
178
179 bool wxToolBar::Create(wxWindow *parent,
180 wxWindowID id,
181 const wxPoint& pos,
182 const wxSize& size,
183 long style,
184 const wxString& name)
185 {
186 if ( !wxToolBarBase::Create(parent, id, pos, size, style,
187 wxDefaultValidator, name) )
188 {
189 return false;
190 }
191
192 FixupStyle();
193
194 CreateInputHandler(wxINP_HANDLER_TOOLBAR);
195
196 SetInitialSize(size);
197
198 return true;
199 }
200
201 wxToolBar::~wxToolBar()
202 {
203 // Make sure the toolbar is removed from the parent.
204 SetSize(0,0);
205 }
206
207 void wxToolBar::SetMargins(int x, int y)
208 {
209 // This required for similar visual effects under
210 // native platforms and wxUniv.
211 wxToolBarBase::SetMargins( x + 3, y + 3 );
212 }
213
214 // ----------------------------------------------------------------------------
215 // wxToolBar tool-related methods
216 // ----------------------------------------------------------------------------
217
218 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
219 {
220 // check the "other" direction first: it must be inside the toolbar or we
221 // don't risk finding anything
222 if ( IsVertical() )
223 {
224 if ( x < 0 || x > m_maxWidth )
225 return NULL;
226
227 // we always use x, even for a vertical toolbar, this makes the code
228 // below simpler
229 x = y;
230 }
231 else // horizontal
232 {
233 if ( y < 0 || y > m_maxHeight )
234 return NULL;
235 }
236
237 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
238 node;
239 node = node->GetNext() )
240 {
241 wxToolBarToolBase *tool = node->GetData();
242 wxRect rectTool = GetToolRect(tool);
243
244 wxCoord startTool, endTool;
245 GetRectLimits(rectTool, &startTool, &endTool);
246
247 if ( x >= startTool && x <= endTool )
248 {
249 // don't return the separators from here, they don't accept any
250 // input anyhow
251 return tool->IsSeparator() ? NULL : tool;
252 }
253 }
254
255 return NULL;
256 }
257
258 void wxToolBar::SetToolShortHelp(int id, const wxString& help)
259 {
260 wxToolBarToolBase *tool = FindById(id);
261
262 wxCHECK_RET( tool, wxT("SetToolShortHelp: no such tool") );
263
264 tool->SetShortHelp(help);
265 }
266
267 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
268 wxToolBarToolBase * WXUNUSED(tool))
269 {
270 // recalculate the toolbar geometry before redrawing it the next time
271 m_needsLayout = true;
272
273 // and ensure that we indeed are going to redraw
274 Refresh();
275
276 return true;
277 }
278
279 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos),
280 wxToolBarToolBase * WXUNUSED(tool))
281 {
282 // as above
283 m_needsLayout = true;
284
285 Refresh();
286
287 return true;
288 }
289
290 void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
291 {
292 #if wxUSE_IMAGE
293 // created disabled-state bitmap on demand
294 if ( !enable && !tool->GetDisabledBitmap().IsOk() )
295 {
296 wxImage image(tool->GetNormalBitmap().ConvertToImage());
297
298 tool->SetDisabledBitmap(image.ConvertToGreyscale());
299 }
300 #endif // wxUSE_IMAGE
301
302 RefreshTool(tool);
303 }
304
305 void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool WXUNUSED(toggle))
306 {
307 // note that if we're called the tool did change state (the base class
308 // checks for it), so it's not necessary to check for this again here
309 RefreshTool(tool);
310 }
311
312 void wxToolBar::DoSetToggle(wxToolBarToolBase *tool, bool WXUNUSED(toggle))
313 {
314 RefreshTool(tool);
315 }
316
317 wxToolBarToolBase *wxToolBar::CreateTool(int id,
318 const wxString& label,
319 const wxBitmap& bmpNormal,
320 const wxBitmap& bmpDisabled,
321 wxItemKind kind,
322 wxObject *clientData,
323 const wxString& shortHelp,
324 const wxString& longHelp)
325 {
326 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
327 clientData, shortHelp, longHelp);
328 }
329
330 wxToolBarToolBase *
331 wxToolBar::CreateTool(wxControl *control, const wxString& label)
332 {
333 return new wxToolBarTool(this, control, label);
334 }
335
336 // ----------------------------------------------------------------------------
337 // wxToolBar geometry
338 // ----------------------------------------------------------------------------
339
340 wxRect wxToolBar::GetToolRect(wxToolBarToolBase *toolBase) const
341 {
342 const wxToolBarTool *tool = (wxToolBarTool *)toolBase;
343
344 wxRect rect;
345
346 wxCHECK_MSG( tool, rect, wxT("GetToolRect: NULL tool") );
347
348 // ensure that we always have the valid tool position
349 if ( m_needsLayout )
350 {
351 wxConstCast(this, wxToolBar)->DoLayout();
352 }
353
354 rect.x = tool->m_x - m_xMargin;
355 rect.y = tool->m_y - m_yMargin;
356
357 if ( IsVertical() )
358 {
359 if (tool->IsButton())
360 {
361 if(!HasFlag(wxTB_TEXT))
362 {
363 rect.width = m_defaultWidth;
364 rect.height = m_defaultHeight;
365 }
366 else
367 {
368 rect.width = m_defaultWidth +
369 GetFont().GetPointSize() * tool->GetLabel().length();
370 rect.height = m_defaultHeight;
371 }
372 }
373 else if (tool->IsSeparator())
374 {
375 rect.width = m_defaultWidth;
376 rect.height = m_widthSeparator;
377 }
378 else // control
379 {
380 rect.width = tool->m_width;
381 rect.height = tool->m_height;
382 }
383 }
384 else // horizontal
385 {
386 if (tool->IsButton())
387 {
388 if(!HasFlag(wxTB_TEXT))
389 {
390 rect.width = m_defaultWidth;
391 rect.height = m_defaultHeight;
392 }
393 else
394 {
395 rect.width = m_defaultWidth +
396 GetFont().GetPointSize() * tool->GetLabel().length();
397 rect.height = m_defaultHeight;
398 }
399 }
400 else if (tool->IsSeparator())
401 {
402 rect.width = m_widthSeparator;
403 rect.height = m_defaultHeight;
404 }
405 else // control
406 {
407 rect.width = tool->m_width;
408 rect.height = tool->m_height;
409 }
410 }
411
412 rect.width += 2*m_xMargin;
413 rect.height += 2*m_yMargin;
414
415 return rect;
416 }
417
418 bool wxToolBar::Realize()
419 {
420 if ( !wxToolBarBase::Realize() )
421 return false;
422
423 m_needsLayout = true;
424 DoLayout();
425
426 // the first item in the radio group is checked by default to be consistent
427 // with wxGTK and the menu radio items
428 int radioGroupCount = 0;
429
430 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
431 node;
432 node = node->GetNext() )
433 {
434 wxToolBarTool *tool = (wxToolBarTool*) node->GetData();
435
436 if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO )
437 {
438 radioGroupCount = 0;
439 continue;
440 }
441
442 bool toggle = !radioGroupCount++;
443 if ( tool->Toggle(toggle) )
444 {
445 DoToggleTool(tool, toggle);
446 }
447 }
448
449 SetInitialSize(wxDefaultSize);
450
451 return true;
452 }
453
454 void wxToolBar::SetWindowStyleFlag( long style )
455 {
456 wxToolBarBase::SetWindowStyleFlag(style);
457
458 m_needsLayout = true;
459
460 Refresh();
461 }
462
463 void wxToolBar::DoLayout()
464 {
465 wxASSERT_MSG( m_needsLayout, wxT("why are we called?") );
466
467 m_needsLayout = false;
468
469 wxCoord x = m_xMargin,
470 y = m_yMargin;
471
472 wxCoord widthTool = 0, maxWidthTool = 0;
473 wxCoord heightTool = 0, maxHeightTool = 0;
474 wxCoord margin = IsVertical() ? m_xMargin : m_yMargin;
475 wxCoord *pCur = IsVertical() ? &y : &x;
476
477 // calculate the positions of all elements
478 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
479 node;
480 node = node->GetNext() )
481 {
482 wxToolBarTool *tool = (wxToolBarTool *) node->GetData();
483
484 tool->m_x = x;
485 tool->m_y = y;
486
487 // TODO ugly number fiddling
488 if (tool->IsButton())
489 {
490 if (IsVertical())
491 {
492 widthTool = m_defaultHeight;
493 heightTool = m_defaultWidth;
494 if(HasFlag(wxTB_TEXT))
495 heightTool += GetFont().GetPointSize() * tool->GetLabel().length();
496 }
497 else
498 {
499 widthTool = m_defaultWidth;
500 if(HasFlag(wxTB_TEXT))
501 widthTool += GetFont().GetPointSize() * tool->GetLabel().length();
502
503 heightTool = m_defaultHeight;
504 }
505
506 if(widthTool > maxWidthTool) // Record max width of tool
507 {
508 maxWidthTool = widthTool;
509 }
510
511 if(heightTool > maxHeightTool) // Record max width of tool
512 {
513 maxHeightTool = heightTool;
514 }
515
516 *pCur += widthTool;
517 }
518 else if (tool->IsSeparator())
519 {
520 *pCur += m_widthSeparator;
521 }
522 else if (!IsVertical()) // horizontal control
523 {
524 wxControl *control = tool->GetControl();
525 wxSize size = control->GetSize();
526 tool->m_y += (m_defaultHeight - size.y)/2;
527 tool->m_width = size.x;
528 tool->m_height = size.y;
529
530 *pCur += tool->m_width;
531 }
532 *pCur += margin;
533 }
534
535 // calculate the total toolbar size
536 wxCoord xMin, yMin;
537
538 if(!HasFlag(wxTB_TEXT))
539 {
540 xMin = m_defaultWidth + 2*m_xMargin;
541 yMin = m_defaultHeight + 2*m_yMargin;
542 }
543 else
544 {
545 if (IsVertical())
546 {
547 xMin = heightTool + 2*m_xMargin;
548 yMin = widthTool + 2*m_xMargin;
549 }
550 else
551 {
552 xMin = maxWidthTool + 2*m_xMargin;
553 yMin = heightTool + 2*m_xMargin;
554 }
555 }
556
557 m_maxWidth = x < xMin ? xMin : x;
558 m_maxHeight = y < yMin ? yMin : y;
559 }
560
561 wxSize wxToolBar::DoGetBestClientSize() const
562 {
563 return wxSize(m_maxWidth, m_maxHeight);
564 }
565
566 void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
567 {
568 int old_width, old_height;
569 GetSize(&old_width, &old_height);
570
571 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
572
573 // Correct width and height if needed.
574 if ( width == wxDefaultCoord || height == wxDefaultCoord )
575 {
576 int tmp_width, tmp_height;
577 GetSize(&tmp_width, &tmp_height);
578
579 if ( width == wxDefaultCoord )
580 width = tmp_width;
581 if ( height == wxDefaultCoord )
582 height = tmp_height;
583 }
584
585 // We must refresh the frame size when the toolbar changes size
586 // otherwise the toolbar can be shown incorrectly
587 if ( old_width != width || old_height != height )
588 {
589 SendSizeEventToParent();
590 }
591 }
592
593 // ----------------------------------------------------------------------------
594 // wxToolBar drawing
595 // ----------------------------------------------------------------------------
596
597 void wxToolBar::RefreshTool(wxToolBarToolBase *tool)
598 {
599 RefreshRect(GetToolRect(tool));
600 }
601
602 void wxToolBar::GetRectLimits(const wxRect& rect,
603 wxCoord *start,
604 wxCoord *end) const
605 {
606 wxCHECK_RET( start && end, wxT("NULL pointer in GetRectLimits") );
607
608 if ( IsVertical() )
609 {
610 *start = rect.GetTop();
611 *end = rect.GetBottom();
612 }
613 else // horizontal
614 {
615 *start = rect.GetLeft();
616 *end = rect.GetRight();
617 }
618 }
619
620 void wxToolBar::DoDraw(wxControlRenderer *renderer)
621 {
622 // prepare the variables used below
623 wxDC& dc = renderer->GetDC();
624 wxRenderer *rend = renderer->GetRenderer();
625 dc.SetFont(GetFont());
626
627 // draw the border separating us from the menubar (if there is no menubar
628 // we probably shouldn't draw it?)
629 if ( !IsVertical() )
630 {
631 rend->DrawHorizontalLine(dc, 0, 0, GetClientSize().x);
632 }
633
634 // get the update rect and its limits depending on the orientation
635 wxRect rectUpdate = GetUpdateClientRect();
636 wxCoord start, end;
637 GetRectLimits(rectUpdate, &start, &end);
638
639 // and redraw all the tools intersecting it
640 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
641 node;
642 node = node->GetNext() )
643 {
644 wxToolBarTool *tool = (wxToolBarTool*) node->GetData();
645 wxRect rectTool = GetToolRect(tool);
646 wxCoord startTool, endTool;
647 GetRectLimits(rectTool, &startTool, &endTool);
648
649 if ( endTool < start )
650 {
651 // we're still to the left of the area to redraw
652 continue;
653 }
654
655 if ( startTool > end )
656 {
657 // we're beyond the area to redraw, nothing left to do
658 break;
659 }
660
661 if (tool->IsSeparator() && !HasFlag(wxTB_FLAT))
662 {
663 // Draw separators only in flat mode
664 continue;
665 }
666
667 // deal with the flags
668 int flags = 0;
669
670 if ( tool->IsEnabled() )
671 {
672 // The toolbars without wxTB_FLAT don't react to the mouse hovering
673 if ( !HasFlag(wxTB_FLAT) || tool->IsUnderMouse() )
674 flags |= wxCONTROL_CURRENT;
675 }
676 else // disabled tool
677 {
678 flags |= wxCONTROL_DISABLED;
679 }
680
681 //if ( tool == m_toolCaptured )
682 // flags |= wxCONTROL_FOCUSED;
683
684 if ( tool->IsPressed() )
685 flags = wxCONTROL_PRESSED;
686
687 wxString label;
688 wxBitmap bitmap;
689 if ( !tool->IsSeparator() )
690 {
691 label = tool->GetLabel();
692 bitmap = tool->GetBitmap();
693
694 if ( !bitmap.IsOk() )
695 {
696 // it's better not to draw anything than to assert inside
697 // drawing code as this results in an almost guaranteed crash
698 // as we're likely to be called by a paint event handler and so
699 // the assert is going to be triggered again and again and ...
700 continue;
701 }
702 }
703 //else: leave both the label and the bitmap invalid to draw a separator
704
705 if ( !tool->IsControl() )
706 {
707 int tbStyle = HasFlag(wxTB_VERTICAL) ? wxTB_VERTICAL : wxTB_HORIZONTAL;
708 if ( HasFlag(wxTB_TEXT) )
709 tbStyle |= wxTB_TEXT;
710
711 rend->DrawToolBarButton(dc, label, bitmap, rectTool, flags,
712 tool->GetStyle(), tbStyle);
713 }
714 else // control
715 {
716 wxControl *control = tool->GetControl();
717 control->Move(tool->m_x, tool->m_y);
718 }
719 }
720 }
721
722 // ----------------------------------------------------------------------------
723 // wxToolBar actions
724 // ----------------------------------------------------------------------------
725
726 bool wxToolBar::PerformAction(const wxControlAction& action,
727 long numArg,
728 const wxString& strArg)
729 {
730 wxToolBarTool *tool = (wxToolBarTool*) FindById(numArg);
731 if (!tool)
732 return false;
733
734 if ( action == wxACTION_TOOLBAR_TOGGLE )
735 {
736 PerformAction( wxACTION_BUTTON_RELEASE, numArg );
737
738 PerformAction( wxACTION_BUTTON_CLICK, numArg );
739
740 // Set mouse leave toolbar button range (If still in the range,
741 // toolbar button would get focus again
742 PerformAction( wxACTION_TOOLBAR_LEAVE, numArg );
743 }
744 else if ( action == wxACTION_TOOLBAR_PRESS )
745 {
746 wxLogTrace(wxT("toolbar"), wxT("Button '%s' pressed."), tool->GetShortHelp().c_str());
747
748 tool->Invert();
749
750 RefreshTool( tool );
751 }
752 else if ( action == wxACTION_TOOLBAR_RELEASE )
753 {
754 wxLogTrace(wxT("toolbar"), wxT("Button '%s' released."), tool->GetShortHelp().c_str());
755
756 wxASSERT_MSG( tool->IsInverted(), wxT("release unpressed button?") );
757
758 if(tool->IsInverted())
759 {
760 tool->Invert();
761 }
762
763 RefreshTool( tool );
764 }
765 else if ( action == wxACTION_TOOLBAR_CLICK )
766 {
767 bool isToggled;
768 if ( tool->CanBeToggled() )
769 {
770 if ( tool->IsButton() && tool->GetKind() == wxITEM_RADIO )
771 {
772 UnToggleRadioGroup(tool);
773 tool->Toggle(true);
774 }
775 else
776 {
777 tool->Toggle();
778 }
779
780 RefreshTool( tool );
781
782 isToggled = tool->IsToggled();
783 }
784 else // simple non-checkable tool
785 {
786 isToggled = false;
787 }
788 OnLeftClick( tool->GetId(), isToggled );
789 }
790 else if ( action == wxACTION_TOOLBAR_ENTER )
791 {
792 wxCHECK_MSG( tool, false, wxT("no tool to enter?") );
793
794 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
795 {
796 tool->SetUnderMouse( true );
797
798 if ( !tool->IsToggled() )
799 RefreshTool( tool );
800 }
801 }
802 else if ( action == wxACTION_TOOLBAR_LEAVE )
803 {
804 wxCHECK_MSG( tool, false, wxT("no tool to leave?") );
805
806 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
807 {
808 tool->SetUnderMouse( false );
809
810 if ( !tool->IsToggled() )
811 RefreshTool( tool );
812 }
813 }
814 else
815 return wxControl::PerformAction(action, numArg, strArg);
816
817 return true;
818 }
819
820 /* static */
821 wxInputHandler *wxToolBar::GetStdInputHandler(wxInputHandler *handlerDef)
822 {
823 static wxStdToolbarInputHandler s_handler(handlerDef);
824
825 return &s_handler;
826 }
827
828 // ============================================================================
829 // wxStdToolbarInputHandler implementation
830 // ============================================================================
831
832 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler *handler)
833 : wxStdInputHandler(handler)
834 {
835 m_winCapture = NULL;
836 m_toolCapture = NULL;
837 m_toolLast = NULL;
838 }
839
840 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer *consumer,
841 const wxKeyEvent& event,
842 bool pressed)
843 {
844 // TODO: when we have a current button we should allow the arrow
845 // keys to move it
846 return wxStdInputHandler::HandleKey(consumer, event, pressed);
847 }
848
849 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer *consumer,
850 const wxMouseEvent& event)
851 {
852 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
853 wxToolBarToolBase *tool = tbar->FindToolForPosition(event.GetX(), event.GetY());
854
855 if ( event.Button(1) )
856 {
857
858 if ( event.LeftDown() || event.LeftDClick() )
859 {
860 if ( !tool || !tool->IsEnabled() )
861 return true;
862
863 m_winCapture = tbar;
864 m_winCapture->CaptureMouse();
865
866 m_toolCapture = tool;
867
868 consumer->PerformAction( wxACTION_BUTTON_PRESS, tool->GetId() );
869
870 return true;
871 }
872 else if ( event.LeftUp() )
873 {
874 if ( m_winCapture )
875 {
876 m_winCapture->ReleaseMouse();
877 m_winCapture = NULL;
878 }
879
880 if (m_toolCapture)
881 {
882 if ( tool == m_toolCapture )
883 consumer->PerformAction( wxACTION_BUTTON_TOGGLE, m_toolCapture->GetId() );
884 else
885 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
886 }
887
888 m_toolCapture = NULL;
889
890 return true;
891 }
892 //else: don't do anything special about the double click
893 }
894
895 return wxStdInputHandler::HandleMouse(consumer, event);
896 }
897
898 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer *consumer,
899 const wxMouseEvent& event)
900 {
901 if ( !wxStdInputHandler::HandleMouseMove(consumer, event) )
902 {
903 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
904
905 wxToolBarTool *tool;
906 if ( event.Leaving() )
907 {
908 // We cannot possibly be over a tool when
909 // leaving the toolbar
910 tool = NULL;
911 }
912 else
913 {
914 tool = (wxToolBarTool*) tbar->FindToolForPosition( event.GetX(), event.GetY() );
915 }
916
917 if (m_toolCapture)
918 {
919 // During capture we only care of the captured tool
920 if (tool && (tool != m_toolCapture))
921 tool = NULL;
922
923 if (tool == m_toolLast)
924 return true;
925
926 if (tool)
927 consumer->PerformAction( wxACTION_BUTTON_PRESS, m_toolCapture->GetId() );
928 else
929 consumer->PerformAction( wxACTION_BUTTON_RELEASE, m_toolCapture->GetId() );
930
931 m_toolLast = tool;
932 }
933 else
934 {
935 if (tool == m_toolLast)
936 return true;
937
938 if (m_toolLast)
939 {
940 // Leave old tool if any
941 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolLast->GetId() );
942 }
943
944 if (tool)
945 {
946 // Enter new tool if any
947 consumer->PerformAction( wxACTION_TOOLBAR_ENTER, tool->GetId() );
948 }
949
950 m_toolLast = tool;
951 }
952
953 return true;
954 }
955
956 return false;
957 }
958
959 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer *consumer,
960 const wxFocusEvent& WXUNUSED(event))
961 {
962 if ( m_toolCapture )
963 {
964 // We shouldn't be left with a highlighted button
965 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
966 }
967
968 return true;
969 }
970
971 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer *consumer,
972 bool activated)
973 {
974 if (m_toolCapture && !activated)
975 {
976 // We shouldn't be left with a highlighted button
977 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
978 }
979
980 return true;
981 }
982
983 #endif // wxUSE_TOOLBAR