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