]> git.saurik.com Git - wxWidgets.git/blob - src/univ/toolbar.cpp
Avoid do-nothing virtuals in WinCE.
[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)
108 : wxToolBarToolBase(tbar, control)
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 *wxToolBar::CreateTool(wxControl *control)
332 {
333 return new wxToolBarTool(this, control);
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, _T("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 SetInitialSize(wxDefaultSize);
427
428 return true;
429 }
430
431 void wxToolBar::SetWindowStyleFlag( long style )
432 {
433 wxToolBarBase::SetWindowStyleFlag(style);
434
435 m_needsLayout = true;
436
437 Refresh();
438 }
439
440 void wxToolBar::DoLayout()
441 {
442 wxASSERT_MSG( m_needsLayout, _T("why are we called?") );
443
444 m_needsLayout = false;
445
446 wxCoord x = m_xMargin,
447 y = m_yMargin;
448
449 wxCoord widthTool = 0, maxWidthTool = 0;
450 wxCoord heightTool = 0, maxHeightTool = 0;
451 wxCoord margin = IsVertical() ? m_xMargin : m_yMargin;
452 wxCoord *pCur = IsVertical() ? &y : &x;
453
454 // calculate the positions of all elements
455 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
456 node;
457 node = node->GetNext() )
458 {
459 wxToolBarTool *tool = (wxToolBarTool *) node->GetData();
460
461 tool->m_x = x;
462 tool->m_y = y;
463
464 // TODO ugly number fiddling
465 if (tool->IsButton())
466 {
467 if (IsVertical())
468 {
469 widthTool = m_defaultHeight;
470 heightTool = m_defaultWidth;
471 if(HasFlag(wxTB_TEXT))
472 heightTool += GetFont().GetPointSize() * tool->GetLabel().length();
473 }
474 else
475 {
476 widthTool = m_defaultWidth;
477 if(HasFlag(wxTB_TEXT))
478 widthTool += GetFont().GetPointSize() * tool->GetLabel().length();
479
480 heightTool = m_defaultHeight;
481 }
482
483 if(widthTool > maxWidthTool) // Record max width of tool
484 {
485 maxWidthTool = widthTool;
486 }
487
488 if(heightTool > maxHeightTool) // Record max width of tool
489 {
490 maxHeightTool = heightTool;
491 }
492
493 *pCur += widthTool;
494 }
495 else if (tool->IsSeparator())
496 {
497 *pCur += m_widthSeparator;
498 }
499 else if (!IsVertical()) // horizontal control
500 {
501 wxControl *control = tool->GetControl();
502 wxSize size = control->GetSize();
503 tool->m_y += (m_defaultHeight - size.y)/2;
504 tool->m_width = size.x;
505 tool->m_height = size.y;
506
507 *pCur += tool->m_width;
508 }
509 *pCur += margin;
510 }
511
512 // calculate the total toolbar size
513 wxCoord xMin, yMin;
514
515 if(!HasFlag(wxTB_TEXT))
516 {
517 xMin = m_defaultWidth + 2*m_xMargin;
518 yMin = m_defaultHeight + 2*m_yMargin;
519 }
520 else
521 {
522 if (IsVertical())
523 {
524 xMin = heightTool + 2*m_xMargin;
525 yMin = widthTool + 2*m_xMargin;
526 }
527 else
528 {
529 xMin = maxWidthTool + 2*m_xMargin;
530 yMin = heightTool + 2*m_xMargin;
531 }
532 }
533
534 m_maxWidth = x < xMin ? xMin : x;
535 m_maxHeight = y < yMin ? yMin : y;
536 }
537
538 wxSize wxToolBar::DoGetBestClientSize() const
539 {
540 return wxSize(m_maxWidth, m_maxHeight);
541 }
542
543 void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
544 {
545 int old_width, old_height;
546 GetSize(&old_width, &old_height);
547
548 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
549
550 // Correct width and height if needed.
551 if ( width == wxDefaultCoord || height == wxDefaultCoord )
552 {
553 int tmp_width, tmp_height;
554 GetSize(&tmp_width, &tmp_height);
555
556 if ( width == wxDefaultCoord )
557 width = tmp_width;
558 if ( height == wxDefaultCoord )
559 height = tmp_height;
560 }
561
562 // We must refresh the frame size when the toolbar changes size
563 // otherwise the toolbar can be shown incorrectly
564 if ( old_width != width || old_height != height )
565 {
566 // But before we send the size event check it
567 // we have a frame that is not being deleted.
568 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
569 if ( frame && !frame->IsBeingDeleted() )
570 {
571 frame->SendSizeEvent();
572 }
573 }
574 }
575
576 // ----------------------------------------------------------------------------
577 // wxToolBar drawing
578 // ----------------------------------------------------------------------------
579
580 void wxToolBar::RefreshTool(wxToolBarToolBase *tool)
581 {
582 RefreshRect(GetToolRect(tool));
583 }
584
585 void wxToolBar::GetRectLimits(const wxRect& rect,
586 wxCoord *start,
587 wxCoord *end) const
588 {
589 wxCHECK_RET( start && end, _T("NULL pointer in GetRectLimits") );
590
591 if ( IsVertical() )
592 {
593 *start = rect.GetTop();
594 *end = rect.GetBottom();
595 }
596 else // horizontal
597 {
598 *start = rect.GetLeft();
599 *end = rect.GetRight();
600 }
601 }
602
603 void wxToolBar::DoDraw(wxControlRenderer *renderer)
604 {
605 // prepare the variables used below
606 wxDC& dc = renderer->GetDC();
607 wxRenderer *rend = renderer->GetRenderer();
608 dc.SetFont(GetFont());
609
610 // draw the border separating us from the menubar (if there is no menubar
611 // we probably shouldn't draw it?)
612 if ( !IsVertical() )
613 {
614 rend->DrawHorizontalLine(dc, 0, 0, GetClientSize().x);
615 }
616
617 // get the update rect and its limits depending on the orientation
618 wxRect rectUpdate = GetUpdateClientRect();
619 wxCoord start, end;
620 GetRectLimits(rectUpdate, &start, &end);
621
622 // and redraw all the tools intersecting it
623 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
624 node;
625 node = node->GetNext() )
626 {
627 wxToolBarTool *tool = (wxToolBarTool*) node->GetData();
628 wxRect rectTool = GetToolRect(tool);
629 wxCoord startTool, endTool;
630 GetRectLimits(rectTool, &startTool, &endTool);
631
632 if ( endTool < start )
633 {
634 // we're still to the left of the area to redraw
635 continue;
636 }
637
638 if ( startTool > end )
639 {
640 // we're beyond the area to redraw, nothing left to do
641 break;
642 }
643
644 if (tool->IsSeparator() && !HasFlag(wxTB_FLAT))
645 {
646 // Draw separators only in flat mode
647 continue;
648 }
649
650 // deal with the flags
651 int flags = 0;
652
653 if ( tool->IsEnabled() )
654 {
655 // The toolbars without wxTB_FLAT don't react to the mouse hovering
656 if ( !HasFlag(wxTB_FLAT) || tool->IsUnderMouse() )
657 flags |= wxCONTROL_CURRENT;
658 }
659 else // disabled tool
660 {
661 flags |= wxCONTROL_DISABLED;
662 }
663
664 //if ( tool == m_toolCaptured )
665 // flags |= wxCONTROL_FOCUSED;
666
667 if ( tool->IsPressed() )
668 flags = wxCONTROL_PRESSED;
669
670 wxString label;
671 wxBitmap bitmap;
672 if ( !tool->IsSeparator() )
673 {
674 label = tool->GetLabel();
675 bitmap = tool->GetBitmap();
676 }
677 //else: leave both the label and the bitmap invalid to draw a separator
678
679 if ( !tool->IsControl() )
680 {
681 int tbStyle = 0;
682 if(HasFlag(wxTB_TEXT))
683 {
684 tbStyle |= wxTB_TEXT;
685 }
686
687 if(HasFlag(wxTB_VERTICAL))
688 {
689 tbStyle |= wxTB_VERTICAL;
690 }
691 else
692 {
693 tbStyle |= wxTB_HORIZONTAL;
694 }
695 rend->DrawToolBarButton(dc, label, bitmap, rectTool, flags, tool->GetStyle(), tbStyle);
696 }
697 else // control
698 {
699 wxControl *control = tool->GetControl();
700 control->Move(tool->m_x, tool->m_y);
701 }
702 }
703 }
704
705 // ----------------------------------------------------------------------------
706 // wxToolBar actions
707 // ----------------------------------------------------------------------------
708
709 bool wxToolBar::PerformAction(const wxControlAction& action,
710 long numArg,
711 const wxString& strArg)
712 {
713 wxToolBarTool *tool = (wxToolBarTool*) FindById(numArg);
714 if (!tool)
715 return false;
716
717 if ( action == wxACTION_TOOLBAR_TOGGLE )
718 {
719 PerformAction( wxACTION_BUTTON_RELEASE, numArg );
720
721 PerformAction( wxACTION_BUTTON_CLICK, numArg );
722
723 // Write by Danny Raynor to change state again.
724 // Check button still pressed or not
725 if ( tool->CanBeToggled() && tool->IsToggled() )
726 {
727 tool->Toggle(false);
728 }
729
730 if( tool->IsInverted() )
731 {
732 PerformAction( wxACTION_TOOLBAR_RELEASE, numArg );
733 }
734
735 // Set mouse leave toolbar button range (If still in the range,
736 // toolbar button would get focus again
737 PerformAction( wxACTION_TOOLBAR_LEAVE, numArg );
738 }
739 else if ( action == wxACTION_TOOLBAR_PRESS )
740 {
741 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool->GetShortHelp().c_str());
742
743 tool->Invert();
744
745 RefreshTool( tool );
746 }
747 else if ( action == wxACTION_TOOLBAR_RELEASE )
748 {
749 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool->GetShortHelp().c_str());
750
751 wxASSERT_MSG( tool->IsInverted(), _T("release unpressed button?") );
752
753 tool->Invert();
754
755 RefreshTool( tool );
756 }
757 else if ( action == wxACTION_TOOLBAR_CLICK )
758 {
759 bool isToggled;
760 if ( tool->CanBeToggled() )
761 {
762 tool->Toggle();
763
764 RefreshTool( tool );
765
766 isToggled = tool->IsToggled();
767 }
768 else // simple non-checkable tool
769 {
770 isToggled = false;
771 }
772 OnLeftClick( tool->GetId(), isToggled );
773 }
774 else if ( action == wxACTION_TOOLBAR_ENTER )
775 {
776 wxCHECK_MSG( tool, false, _T("no tool to enter?") );
777
778 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
779 {
780 tool->SetUnderMouse( true );
781
782 if ( !tool->IsToggled() )
783 RefreshTool( tool );
784 }
785 }
786 else if ( action == wxACTION_TOOLBAR_LEAVE )
787 {
788 wxCHECK_MSG( tool, false, _T("no tool to leave?") );
789
790 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
791 {
792 tool->SetUnderMouse( false );
793
794 if ( !tool->IsToggled() )
795 RefreshTool( tool );
796 }
797 }
798 else
799 return wxControl::PerformAction(action, numArg, strArg);
800
801 return true;
802 }
803
804 /* static */
805 wxInputHandler *wxToolBar::GetStdInputHandler(wxInputHandler *handlerDef)
806 {
807 static wxStdToolbarInputHandler s_handler(handlerDef);
808
809 return &s_handler;
810 }
811
812 // ============================================================================
813 // wxStdToolbarInputHandler implementation
814 // ============================================================================
815
816 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler *handler)
817 : wxStdInputHandler(handler)
818 {
819 m_winCapture = NULL;
820 m_toolCapture = NULL;
821 m_toolLast = NULL;
822 }
823
824 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer *consumer,
825 const wxKeyEvent& event,
826 bool pressed)
827 {
828 // TODO: when we have a current button we should allow the arrow
829 // keys to move it
830 return wxStdInputHandler::HandleKey(consumer, event, pressed);
831 }
832
833 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer *consumer,
834 const wxMouseEvent& event)
835 {
836 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
837 wxToolBarToolBase *tool = tbar->FindToolForPosition(event.GetX(), event.GetY());
838
839 if ( event.Button(1) )
840 {
841
842 if ( event.LeftDown() || event.LeftDClick() )
843 {
844 if ( !tool || !tool->IsEnabled() )
845 return true;
846
847 m_winCapture = tbar;
848 m_winCapture->CaptureMouse();
849
850 m_toolCapture = tool;
851
852 consumer->PerformAction( wxACTION_BUTTON_PRESS, tool->GetId() );
853
854 return true;
855 }
856 else if ( event.LeftUp() )
857 {
858 if ( m_winCapture )
859 {
860 m_winCapture->ReleaseMouse();
861 m_winCapture = NULL;
862 }
863
864 if (m_toolCapture)
865 {
866 if ( tool == m_toolCapture )
867 consumer->PerformAction( wxACTION_BUTTON_TOGGLE, m_toolCapture->GetId() );
868 else
869 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
870 }
871
872 m_toolCapture = NULL;
873
874 return true;
875 }
876 //else: don't do anything special about the double click
877 }
878
879 return wxStdInputHandler::HandleMouse(consumer, event);
880 }
881
882 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer *consumer,
883 const wxMouseEvent& event)
884 {
885 if ( !wxStdInputHandler::HandleMouseMove(consumer, event) )
886 {
887 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
888
889 wxToolBarTool *tool;
890 if ( event.Leaving() )
891 {
892 // We cannot possibly be over a tool when
893 // leaving the toolbar
894 tool = NULL;
895 }
896 else
897 {
898 tool = (wxToolBarTool*) tbar->FindToolForPosition( event.GetX(), event.GetY() );
899 }
900
901 if (m_toolCapture)
902 {
903 // During capture we only care of the captured tool
904 if (tool && (tool != m_toolCapture))
905 tool = NULL;
906
907 if (tool == m_toolLast)
908 return true;
909
910 if (tool)
911 consumer->PerformAction( wxACTION_BUTTON_PRESS, m_toolCapture->GetId() );
912 else
913 consumer->PerformAction( wxACTION_BUTTON_RELEASE, m_toolCapture->GetId() );
914
915 m_toolLast = tool;
916 }
917 else
918 {
919 if (tool == m_toolLast)
920 return true;
921
922 if (m_toolLast)
923 {
924 // Leave old tool if any
925 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolLast->GetId() );
926 }
927
928 if (tool)
929 {
930 // Enter new tool if any
931 consumer->PerformAction( wxACTION_TOOLBAR_ENTER, tool->GetId() );
932 }
933
934 m_toolLast = tool;
935 }
936
937 return true;
938 }
939
940 return false;
941 }
942
943 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer *consumer,
944 const wxFocusEvent& WXUNUSED(event))
945 {
946 if ( m_toolCapture )
947 {
948 // We shouldn't be left with a highlighted button
949 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
950 }
951
952 return true;
953 }
954
955 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer *consumer,
956 bool activated)
957 {
958 if (m_toolCapture && !activated)
959 {
960 // We shouldn't be left with a highlighted button
961 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
962 }
963
964 return true;
965 }
966
967 #endif // wxUSE_TOOLBAR