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