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