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