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