Many changes for wxInputHandler creation mainly related to:
[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 CreateInputHandler(wxINP_HANDLER_TOOLBAR);
194
195 SetBestSize(size);
196
197 return true;
198 }
199
200 wxToolBar::~wxToolBar()
201 {
202 // Make sure the toolbar is removed from the parent.
203 SetSize(0,0);
204 }
205
206 void wxToolBar::SetMargins(int x, int y)
207 {
208 // This required for similar visual effects under
209 // native platforms and wxUniv.
210 wxToolBarBase::SetMargins( x + 3, y + 3 );
211 }
212
213 // ----------------------------------------------------------------------------
214 // wxToolBar tool-related methods
215 // ----------------------------------------------------------------------------
216
217 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
218 {
219 // check the "other" direction first: it must be inside the toolbar or we
220 // don't risk finding anything
221 if ( IsVertical() )
222 {
223 if ( x < 0 || x > m_maxWidth )
224 return NULL;
225
226 // we always use x, even for a vertical toolbar, this makes the code
227 // below simpler
228 x = y;
229 }
230 else // horizontal
231 {
232 if ( y < 0 || y > m_maxHeight )
233 return NULL;
234 }
235
236 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
237 node;
238 node = node->GetNext() )
239 {
240 wxToolBarToolBase *tool = node->GetData();
241 wxRect rectTool = GetToolRect(tool);
242
243 wxCoord startTool, endTool;
244 GetRectLimits(rectTool, &startTool, &endTool);
245
246 if ( x >= startTool && x <= endTool )
247 {
248 // don't return the separators from here, they don't accept any
249 // input anyhow
250 return tool->IsSeparator() ? NULL : tool;
251 }
252 }
253
254 return NULL;
255 }
256
257 void wxToolBar::SetToolShortHelp(int id, const wxString& help)
258 {
259 wxToolBarToolBase *tool = FindById(id);
260
261 wxCHECK_RET( tool, _T("SetToolShortHelp: no such tool") );
262
263 tool->SetShortHelp(help);
264 }
265
266 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
267 wxToolBarToolBase * WXUNUSED(tool))
268 {
269 // recalculate the toolbar geometry before redrawing it the next time
270 m_needsLayout = true;
271
272 // and ensure that we indeed are going to redraw
273 Refresh();
274
275 return true;
276 }
277
278 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos),
279 wxToolBarToolBase * WXUNUSED(tool))
280 {
281 // as above
282 m_needsLayout = true;
283
284 Refresh();
285
286 return true;
287 }
288
289 void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
290 {
291 // created disabled-state bitmap on demand
292 if ( !enable && !tool->GetDisabledBitmap().Ok() )
293 {
294 wxImage image( tool->GetNormalBitmap().ConvertToImage() );
295
296 // TODO: don't hardcode 180
297 unsigned char bg_red = 180;
298 unsigned char bg_green = 180;
299 unsigned char bg_blue = 180;
300
301 unsigned char mask_red = image.GetMaskRed();
302 unsigned char mask_green = image.GetMaskGreen();
303 unsigned char mask_blue = image.GetMaskBlue();
304
305 bool has_mask = image.HasMask();
306
307 int x,y;
308 for (y = 0; y < image.GetHeight(); y++)
309 {
310 for (x = 0; x < image.GetWidth(); x++)
311 {
312 unsigned char red = image.GetRed(x,y);
313 unsigned char green = image.GetGreen(x,y);
314 unsigned char blue = image.GetBlue(x,y);
315 if (!has_mask || red != mask_red || green != mask_green || blue != mask_blue)
316 {
317 red = (unsigned char)((((wxInt32) red - bg_red) >> 1) + bg_red);
318 green = (unsigned char)((((wxInt32) green - bg_green) >> 1) + bg_green);
319 blue = (unsigned char)((((wxInt32) blue - bg_blue) >> 1) + bg_blue);
320 image.SetRGB( x, y, red, green, blue );
321 }
322 }
323 }
324
325 for (y = 0; y < image.GetHeight(); y++)
326 {
327 for (x = y % 2; x < image.GetWidth(); x += 2)
328 {
329 unsigned char red = image.GetRed(x,y);
330 unsigned char green = image.GetGreen(x,y);
331 unsigned char blue = image.GetBlue(x,y);
332 if (!has_mask || red != mask_red || green != mask_green || blue != mask_blue)
333 {
334 red = (unsigned char)((((wxInt32) red - bg_red) >> 1) + bg_red);
335 green = (unsigned char)((((wxInt32) green - bg_green) >> 1) + bg_green);
336 blue = (unsigned char)((((wxInt32) blue - bg_blue) >> 1) + bg_blue);
337 image.SetRGB( x, y, red, green, blue );
338 }
339 }
340 }
341
342 tool->SetDisabledBitmap(image);
343 }
344
345 RefreshTool(tool);
346 }
347
348 void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool WXUNUSED(toggle))
349 {
350 // note that if we're called the tool did change state (the base class
351 // checks for it), so it's not necessary to check for this again here
352 RefreshTool(tool);
353 }
354
355 void wxToolBar::DoSetToggle(wxToolBarToolBase *tool, bool WXUNUSED(toggle))
356 {
357 RefreshTool(tool);
358 }
359
360 wxToolBarToolBase *wxToolBar::CreateTool(int id,
361 const wxString& label,
362 const wxBitmap& bmpNormal,
363 const wxBitmap& bmpDisabled,
364 wxItemKind kind,
365 wxObject *clientData,
366 const wxString& shortHelp,
367 const wxString& longHelp)
368 {
369 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
370 clientData, shortHelp, longHelp);
371 }
372
373 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
374 {
375 return new wxToolBarTool(this, control);
376 }
377
378 // ----------------------------------------------------------------------------
379 // wxToolBar geometry
380 // ----------------------------------------------------------------------------
381
382 wxRect wxToolBar::GetToolRect(wxToolBarToolBase *toolBase) const
383 {
384 const wxToolBarTool *tool = (wxToolBarTool *)toolBase;
385
386 wxRect rect;
387
388 wxCHECK_MSG( tool, rect, _T("GetToolRect: NULL tool") );
389
390 // ensure that we always have the valid tool position
391 if ( m_needsLayout )
392 {
393 wxConstCast(this, wxToolBar)->DoLayout();
394 }
395
396 rect.x = tool->m_x - m_xMargin;
397 rect.y = tool->m_y - m_yMargin;
398
399 if ( IsVertical() )
400 {
401 if (tool->IsButton())
402 {
403 if(!HasFlag(wxTB_TEXT))
404 {
405 rect.width = m_defaultWidth;
406 rect.height = m_defaultHeight;
407 }
408 else
409 {
410 rect.width = m_defaultWidth +
411 GetFont().GetPointSize() * tool->GetLabel().length();
412 rect.height = m_defaultHeight;
413 }
414 }
415 else if (tool->IsSeparator())
416 {
417 rect.width = m_defaultWidth;
418 rect.height = m_widthSeparator;
419 }
420 else // control
421 {
422 rect.width = tool->m_width;
423 rect.height = tool->m_height;
424 }
425 }
426 else // horizontal
427 {
428 if (tool->IsButton())
429 {
430 if(!HasFlag(wxTB_TEXT))
431 {
432 rect.width = m_defaultWidth;
433 rect.height = m_defaultHeight;
434 }
435 else
436 {
437 rect.width = m_defaultWidth +
438 GetFont().GetPointSize() * tool->GetLabel().length();
439 rect.height = m_defaultHeight;
440 }
441 }
442 else if (tool->IsSeparator())
443 {
444 rect.width = m_widthSeparator;
445 rect.height = m_defaultHeight;
446 }
447 else // control
448 {
449 rect.width = tool->m_width;
450 rect.height = tool->m_height;
451 }
452 }
453
454 rect.width += 2*m_xMargin;
455 rect.height += 2*m_yMargin;
456
457 return rect;
458 }
459
460 bool wxToolBar::Realize()
461 {
462 if ( !wxToolBarBase::Realize() )
463 return false;
464
465 m_needsLayout = true;
466 DoLayout();
467
468 SetBestSize(wxDefaultSize);
469
470 return true;
471 }
472
473 void wxToolBar::SetWindowStyleFlag( long style )
474 {
475 wxToolBarBase::SetWindowStyleFlag(style);
476
477 m_needsLayout = true;
478
479 Refresh();
480 }
481
482 void wxToolBar::DoLayout()
483 {
484 wxASSERT_MSG( m_needsLayout, _T("why are we called?") );
485
486 m_needsLayout = false;
487
488 wxCoord x = m_xMargin,
489 y = m_yMargin;
490
491 wxCoord widthTool = 0, maxWidthTool = 0;
492 wxCoord heightTool = 0, maxHeightTool = 0;
493 wxCoord margin = IsVertical() ? m_xMargin : m_yMargin;
494 wxCoord *pCur = IsVertical() ? &y : &x;
495
496 // calculate the positions of all elements
497 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
498 node;
499 node = node->GetNext() )
500 {
501 wxToolBarTool *tool = (wxToolBarTool *) node->GetData();
502
503 tool->m_x = x;
504 tool->m_y = y;
505
506 // TODO ugly number fiddling
507 if (tool->IsButton())
508 {
509 if (IsVertical())
510 {
511 widthTool = m_defaultHeight;
512 heightTool = m_defaultWidth;
513 if(HasFlag(wxTB_TEXT))
514 heightTool += GetFont().GetPointSize() * tool->GetLabel().length();
515 }
516 else
517 {
518 widthTool = m_defaultWidth;
519 if(HasFlag(wxTB_TEXT))
520 widthTool += GetFont().GetPointSize() * tool->GetLabel().length();
521
522 heightTool = m_defaultHeight;
523 }
524
525 if(widthTool > maxWidthTool) // Record max width of tool
526 {
527 maxWidthTool = widthTool;
528 }
529
530 if(heightTool > maxHeightTool) // Record max width of tool
531 {
532 maxHeightTool = heightTool;
533 }
534
535 *pCur += widthTool;
536 }
537 else if (tool->IsSeparator())
538 {
539 *pCur += m_widthSeparator;
540 }
541 else if (!IsVertical()) // horizontal control
542 {
543 wxControl *control = tool->GetControl();
544 wxSize size = control->GetSize();
545 tool->m_y += (m_defaultHeight - size.y)/2;
546 tool->m_width = size.x;
547 tool->m_height = size.y;
548
549 *pCur += tool->m_width;
550 }
551 *pCur += margin;
552 }
553
554 // calculate the total toolbar size
555 wxCoord xMin, yMin;
556
557 if(!HasFlag(wxTB_TEXT))
558 {
559 xMin = m_defaultWidth + 2*m_xMargin;
560 yMin = m_defaultHeight + 2*m_yMargin;
561 }
562 else
563 {
564 if (IsVertical())
565 {
566 xMin = heightTool + 2*m_xMargin;
567 yMin = widthTool + 2*m_xMargin;
568 }
569 else
570 {
571 xMin = maxWidthTool + 2*m_xMargin;
572 yMin = heightTool + 2*m_xMargin;
573 }
574 }
575
576 m_maxWidth = x < xMin ? xMin : x;
577 m_maxHeight = y < yMin ? yMin : y;
578 }
579
580 wxSize wxToolBar::DoGetBestClientSize() const
581 {
582 return wxSize(m_maxWidth, m_maxHeight);
583 }
584
585 void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
586 {
587 int old_width, old_height;
588 GetSize(&old_width, &old_height);
589
590 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
591
592 // Correct width and height if needed.
593 if ( width == wxDefaultCoord || height == wxDefaultCoord )
594 {
595 int tmp_width, tmp_height;
596 GetSize(&tmp_width, &tmp_height);
597
598 if ( width == wxDefaultCoord )
599 width = tmp_width;
600 if ( height == wxDefaultCoord )
601 height = tmp_height;
602 }
603
604 // We must refresh the frame size when the toolbar changes size
605 // otherwise the toolbar can be shown incorrectly
606 if ( old_width != width || old_height != height )
607 {
608 // But before we send the size event check it
609 // we have a frame that is not being deleted.
610 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
611 if ( frame && !frame->IsBeingDeleted() )
612 {
613 frame->SendSizeEvent();
614 }
615 }
616 }
617
618 // ----------------------------------------------------------------------------
619 // wxToolBar drawing
620 // ----------------------------------------------------------------------------
621
622 void wxToolBar::RefreshTool(wxToolBarToolBase *tool)
623 {
624 RefreshRect(GetToolRect(tool));
625 }
626
627 void wxToolBar::GetRectLimits(const wxRect& rect,
628 wxCoord *start,
629 wxCoord *end) const
630 {
631 wxCHECK_RET( start && end, _T("NULL pointer in GetRectLimits") );
632
633 if ( IsVertical() )
634 {
635 *start = rect.GetTop();
636 *end = rect.GetBottom();
637 }
638 else // horizontal
639 {
640 *start = rect.GetLeft();
641 *end = rect.GetRight();
642 }
643 }
644
645 void wxToolBar::DoDraw(wxControlRenderer *renderer)
646 {
647 // prepare the variables used below
648 wxDC& dc = renderer->GetDC();
649 wxRenderer *rend = renderer->GetRenderer();
650 dc.SetFont(GetFont());
651
652 // draw the border separating us from the menubar (if there is no menubar
653 // we probably shouldn't draw it?)
654 if ( !IsVertical() )
655 {
656 rend->DrawHorizontalLine(dc, 0, 0, GetClientSize().x);
657 }
658
659 // get the update rect and its limits depending on the orientation
660 wxRect rectUpdate = GetUpdateClientRect();
661 wxCoord start, end;
662 GetRectLimits(rectUpdate, &start, &end);
663
664 // and redraw all the tools intersecting it
665 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
666 node;
667 node = node->GetNext() )
668 {
669 wxToolBarTool *tool = (wxToolBarTool*) node->GetData();
670 wxRect rectTool = GetToolRect(tool);
671 wxCoord startTool, endTool;
672 GetRectLimits(rectTool, &startTool, &endTool);
673
674 if ( endTool < start )
675 {
676 // we're still to the left of the area to redraw
677 continue;
678 }
679
680 if ( startTool > end )
681 {
682 // we're beyond the area to redraw, nothing left to do
683 break;
684 }
685
686 if (tool->IsSeparator() && !HasFlag(wxTB_FLAT))
687 {
688 // Draw separators only in flat mode
689 continue;
690 }
691
692 // deal with the flags
693 int flags = 0;
694
695 if ( tool->IsEnabled() )
696 {
697 // The toolbars without wxTB_FLAT don't react to the mouse hovering
698 if ( !HasFlag(wxTB_FLAT) || tool->IsUnderMouse() )
699 flags |= wxCONTROL_CURRENT;
700 }
701 else // disabled tool
702 {
703 flags |= wxCONTROL_DISABLED;
704 }
705
706 //if ( tool == m_toolCaptured )
707 // flags |= wxCONTROL_FOCUSED;
708
709 if ( tool->IsPressed() )
710 flags = wxCONTROL_PRESSED;
711
712 wxString label;
713 wxBitmap bitmap;
714 if ( !tool->IsSeparator() )
715 {
716 label = tool->GetLabel();
717 bitmap = tool->GetBitmap();
718 }
719 //else: leave both the label and the bitmap invalid to draw a separator
720
721 if ( !tool->IsControl() )
722 {
723 int tbStyle = 0;
724 if(HasFlag(wxTB_TEXT))
725 {
726 tbStyle |= wxTB_TEXT;
727 }
728
729 if(HasFlag(wxTB_VERTICAL))
730 {
731 tbStyle |= wxTB_VERTICAL;
732 }
733 else
734 {
735 tbStyle |= wxTB_HORIZONTAL;
736 }
737 rend->DrawToolBarButton(dc, label, bitmap, rectTool, flags, tool->GetStyle(), tbStyle);
738 }
739 else // control
740 {
741 wxControl *control = tool->GetControl();
742 control->Move(tool->m_x, tool->m_y);
743 }
744 }
745 }
746
747 // ----------------------------------------------------------------------------
748 // wxToolBar actions
749 // ----------------------------------------------------------------------------
750
751 bool wxToolBar::PerformAction(const wxControlAction& action,
752 long numArg,
753 const wxString& strArg)
754 {
755 wxToolBarTool *tool = (wxToolBarTool*) FindById(numArg);
756 if (!tool)
757 return false;
758
759 if ( action == wxACTION_TOOLBAR_TOGGLE )
760 {
761 PerformAction( wxACTION_BUTTON_RELEASE, numArg );
762
763 PerformAction( wxACTION_BUTTON_CLICK, numArg );
764
765 // Write by Danny Raynor to change state again.
766 // Check button still pressed or not
767 if ( tool->CanBeToggled() && tool->IsToggled() )
768 {
769 tool->Toggle(false);
770 }
771
772 if( tool->IsInverted() )
773 {
774 PerformAction( wxACTION_TOOLBAR_RELEASE, numArg );
775 }
776
777 // Set mouse leave toolbar button range (If still in the range,
778 // toolbar button would get focus again
779 PerformAction( wxACTION_TOOLBAR_LEAVE, numArg );
780 }
781 else if ( action == wxACTION_TOOLBAR_PRESS )
782 {
783 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool->GetShortHelp().c_str());
784
785 tool->Invert();
786
787 RefreshTool( tool );
788 }
789 else if ( action == wxACTION_TOOLBAR_RELEASE )
790 {
791 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool->GetShortHelp().c_str());
792
793 wxASSERT_MSG( tool->IsInverted(), _T("release unpressed button?") );
794
795 tool->Invert();
796
797 RefreshTool( tool );
798 }
799 else if ( action == wxACTION_TOOLBAR_CLICK )
800 {
801 bool isToggled;
802 if ( tool->CanBeToggled() )
803 {
804 tool->Toggle();
805
806 RefreshTool( tool );
807
808 isToggled = tool->IsToggled();
809 }
810 else // simple non-checkable tool
811 {
812 isToggled = false;
813 }
814 OnLeftClick( tool->GetId(), isToggled );
815 }
816 else if ( action == wxACTION_TOOLBAR_ENTER )
817 {
818 wxCHECK_MSG( tool, false, _T("no tool to enter?") );
819
820 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
821 {
822 tool->SetUnderMouse( true );
823
824 if ( !tool->IsToggled() )
825 RefreshTool( tool );
826 }
827 }
828 else if ( action == wxACTION_TOOLBAR_LEAVE )
829 {
830 wxCHECK_MSG( tool, false, _T("no tool to leave?") );
831
832 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
833 {
834 tool->SetUnderMouse( false );
835
836 if ( !tool->IsToggled() )
837 RefreshTool( tool );
838 }
839 }
840 else
841 return wxControl::PerformAction(action, numArg, strArg);
842
843 return true;
844 }
845
846 /* static */
847 wxInputHandler *wxToolBar::GetStdInputHandler(wxInputHandler *handlerDef)
848 {
849 static wxStdToolbarInputHandler s_handler(handlerDef);
850
851 return &s_handler;
852 }
853
854 // ============================================================================
855 // wxStdToolbarInputHandler implementation
856 // ============================================================================
857
858 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler *handler)
859 : wxStdInputHandler(handler)
860 {
861 m_winCapture = NULL;
862 m_toolCapture = NULL;
863 m_toolLast = NULL;
864 }
865
866 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer *consumer,
867 const wxKeyEvent& event,
868 bool pressed)
869 {
870 // TODO: when we have a current button we should allow the arrow
871 // keys to move it
872 return wxStdInputHandler::HandleKey(consumer, event, pressed);
873 }
874
875 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer *consumer,
876 const wxMouseEvent& event)
877 {
878 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
879 wxToolBarToolBase *tool = tbar->FindToolForPosition(event.GetX(), event.GetY());
880
881 if ( event.Button(1) )
882 {
883
884 if ( event.LeftDown() || event.LeftDClick() )
885 {
886 if ( !tool || !tool->IsEnabled() )
887 return true;
888
889 m_winCapture = tbar;
890 m_winCapture->CaptureMouse();
891
892 m_toolCapture = tool;
893
894 consumer->PerformAction( wxACTION_BUTTON_PRESS, tool->GetId() );
895
896 return true;
897 }
898 else if ( event.LeftUp() )
899 {
900 if ( m_winCapture )
901 {
902 m_winCapture->ReleaseMouse();
903 m_winCapture = NULL;
904 }
905
906 if (m_toolCapture)
907 {
908 if ( tool == m_toolCapture )
909 consumer->PerformAction( wxACTION_BUTTON_TOGGLE, m_toolCapture->GetId() );
910 else
911 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
912 }
913
914 m_toolCapture = NULL;
915
916 return true;
917 }
918 //else: don't do anything special about the double click
919 }
920
921 return wxStdInputHandler::HandleMouse(consumer, event);
922 }
923
924 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer *consumer,
925 const wxMouseEvent& event)
926 {
927 if ( !wxStdInputHandler::HandleMouseMove(consumer, event) )
928 {
929 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
930
931 wxToolBarTool *tool;
932 if ( event.Leaving() )
933 {
934 // We cannot possibly be over a tool when
935 // leaving the toolbar
936 tool = NULL;
937 }
938 else
939 {
940 tool = (wxToolBarTool*) tbar->FindToolForPosition( event.GetX(), event.GetY() );
941 }
942
943 if (m_toolCapture)
944 {
945 // During capture we only care of the captured tool
946 if (tool && (tool != m_toolCapture))
947 tool = NULL;
948
949 if (tool == m_toolLast)
950 return true;
951
952 if (tool)
953 consumer->PerformAction( wxACTION_BUTTON_PRESS, m_toolCapture->GetId() );
954 else
955 consumer->PerformAction( wxACTION_BUTTON_RELEASE, m_toolCapture->GetId() );
956
957 m_toolLast = tool;
958 }
959 else
960 {
961 if (tool == m_toolLast)
962 return true;
963
964 if (m_toolLast)
965 {
966 // Leave old tool if any
967 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolLast->GetId() );
968 }
969
970 if (tool)
971 {
972 // Enter new tool if any
973 consumer->PerformAction( wxACTION_TOOLBAR_ENTER, tool->GetId() );
974 }
975
976 m_toolLast = tool;
977 }
978
979 return true;
980 }
981
982 return false;
983 }
984
985 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer *consumer,
986 const wxFocusEvent& WXUNUSED(event))
987 {
988 if ( m_toolCapture )
989 {
990 // We shouldn't be left with a highlighted button
991 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
992 }
993
994 return true;
995 }
996
997 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer *consumer,
998 bool activated)
999 {
1000 if (m_toolCapture && !activated)
1001 {
1002 // We shouldn't be left with a highlighted button
1003 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
1004 }
1005
1006 return true;
1007 }
1008
1009 #endif // wxUSE_TOOLBAR