]> git.saurik.com Git - wxWidgets.git/blame - src/univ/toolbar.cpp
ConvertToImage: Check !Ok() (we may not have any ref data at all) and if
[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
RR
647 wxToolBarTool *tool = (wxToolBarTool*) FindById(numArg);
648
649 if ( action == wxACTION_TOOLBAR_TOGGLE )
650 {
651 PerformAction( wxACTION_BUTTON_RELEASE, numArg );
bb312b54 652
34d26f42
RR
653 PerformAction( wxACTION_BUTTON_CLICK, numArg );
654 }
655 else if ( action == wxACTION_TOOLBAR_PRESS )
656 {
657 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool->GetShortHelp().c_str());
658
659 tool->Invert();
3216dbf5 660
34d26f42
RR
661 RefreshTool( tool );
662 }
663 else if ( action == wxACTION_TOOLBAR_RELEASE )
5a73d082 664 {
34d26f42 665 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool->GetShortHelp().c_str());
5a73d082 666
34d26f42
RR
667 wxASSERT_MSG( tool->IsInverted(), _T("release unpressed button?") );
668
669 tool->Invert();
5a73d082 670
34d26f42 671 RefreshTool( tool );
5a73d082 672 }
34d26f42 673 else if ( action == wxACTION_TOOLBAR_CLICK )
5a73d082 674 {
34d26f42
RR
675 bool isToggled;
676 if ( tool->CanBeToggled() )
677 {
678 tool->Toggle();
5a73d082 679
34d26f42 680 RefreshTool( tool );
bb312b54 681
34d26f42
RR
682 isToggled = tool->IsToggled();
683 }
684 else // simple non-checkable tool
685 {
686 isToggled = FALSE;
687 }
688 OnLeftClick( tool->GetId(), isToggled );
689 }
3216dbf5 690 else if ( action == wxACTION_TOOLBAR_ENTER )
c08a4f00 691 {
34d26f42
RR
692 wxCHECK_MSG( tool, FALSE, _T("no tool to enter?") );
693
694 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
c08a4f00 695 {
34d26f42
RR
696 tool->SetUnderMouse( TRUE );
697
698 if ( !tool->IsToggled() )
699 RefreshTool( tool );
c08a4f00
RR
700 }
701 }
3216dbf5 702 else if ( action == wxACTION_TOOLBAR_LEAVE )
bb312b54 703 {
34d26f42
RR
704 wxCHECK_MSG( tool, FALSE, _T("no tool to leave?") );
705
706 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
bb312b54 707 {
34d26f42
RR
708 tool->SetUnderMouse( FALSE );
709
710 if ( !tool->IsToggled() )
711 RefreshTool( tool );
bb312b54 712 }
c08a4f00 713 }
3216dbf5
VZ
714 else
715 return wxControl::PerformAction(action, numArg, strArg);
716
717 return TRUE;
718}
719
720// ============================================================================
721// wxStdToolbarInputHandler implementation
722// ============================================================================
723
724wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler *handler)
34d26f42 725 : wxStdInputHandler(handler)
3216dbf5 726{
34d26f42
RR
727 m_winCapture = NULL;
728 m_toolCapture = NULL;
729 m_toolLast = NULL;
3216dbf5
VZ
730}
731
732bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer *consumer,
733 const wxKeyEvent& event,
734 bool pressed)
735{
736 // TODO: when we have a current button we should allow the arrow
737 // keys to move it
738 return wxStdInputHandler::HandleKey(consumer, event, pressed);
739}
740
4e89ceb1
VZ
741bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer *consumer,
742 const wxMouseEvent& event)
743{
4e89ceb1
VZ
744 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
745 wxToolBarToolBase *tool = tbar->FindToolForPosition(event.GetX(), event.GetY());
746
34d26f42
RR
747 if ( event.Button(1) )
748 {
34d26f42
RR
749
750 if ( event.LeftDown() || event.LeftDClick() )
751 {
72726d27
RR
752 if ( !tool || !tool->IsEnabled() )
753 return TRUE;
754
34d26f42
RR
755 m_winCapture = tbar;
756 m_winCapture->CaptureMouse();
757
758 m_toolCapture = tool;
759
760 consumer->PerformAction( wxACTION_BUTTON_PRESS, tool->GetId() );
761
762 return TRUE;
763 }
764 else if ( event.LeftUp() )
765 {
766 if ( m_winCapture )
767 {
768 m_winCapture->ReleaseMouse();
769 m_winCapture = NULL;
770 }
771
2b5f62a0
VZ
772 if (m_toolCapture)
773 {
774 if ( tool == m_toolCapture )
775 consumer->PerformAction( wxACTION_BUTTON_TOGGLE, m_toolCapture->GetId() );
776 else
777 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
778 }
34d26f42
RR
779
780 m_toolCapture = NULL;
72726d27
RR
781
782 return TRUE;
34d26f42
RR
783 }
784 //else: don't do anything special about the double click
785 }
786
787 return wxStdInputHandler::HandleMouse(consumer, event);
4e89ceb1
VZ
788}
789
3216dbf5
VZ
790bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer *consumer,
791 const wxMouseEvent& event)
792{
34d26f42 793 if ( !wxStdInputHandler::HandleMouseMove(consumer, event) )
c08a4f00 794 {
34d26f42
RR
795 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
796
797 wxToolBarTool *tool;
3216dbf5 798 if ( event.Leaving() )
c08a4f00 799 {
34d26f42
RR
800 // We cannot possibly be over a tool when
801 // leaving the toolbar
3216dbf5 802 tool = NULL;
c08a4f00 803 }
3216dbf5 804 else
c08a4f00 805 {
34d26f42 806 tool = (wxToolBarTool*) tbar->FindToolForPosition( event.GetX(), event.GetY() );
c08a4f00 807 }
34d26f42 808
72726d27 809 if (m_toolCapture)
34d26f42 810 {
72726d27
RR
811 // During capture we only care of the captured tool
812 if (tool && (tool != m_toolCapture))
813 tool = NULL;
814
815 if (tool == m_toolLast)
816 return TRUE;
817
818 if (tool)
819 consumer->PerformAction( wxACTION_BUTTON_PRESS, m_toolCapture->GetId() );
820 else
821 consumer->PerformAction( wxACTION_BUTTON_RELEASE, m_toolCapture->GetId() );
822
823 m_toolLast = tool;
34d26f42 824 }
72726d27 825 else
34d26f42 826 {
72726d27
RR
827 if (tool == m_toolLast)
828 return TRUE;
829
830 if (m_toolLast)
831 {
832 // Leave old tool if any
833 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolLast->GetId() );
834 }
835
836 if (tool)
837 {
838 // Enter new tool if any
839 consumer->PerformAction( wxACTION_TOOLBAR_ENTER, tool->GetId() );
840 }
34d26f42 841
34d26f42 842 m_toolLast = tool;
34d26f42
RR
843 }
844
3216dbf5 845 return TRUE;
bb312b54 846 }
3216dbf5
VZ
847
848 return FALSE;
849}
850
851bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer *consumer,
61fef19b 852 const wxFocusEvent& WXUNUSED(event))
3216dbf5 853{
61fef19b 854 if ( m_toolCapture )
34d26f42
RR
855 {
856 // We shouldn't be left with a highlighted button
857 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
858 }
3216dbf5
VZ
859
860 return TRUE;
861}
862
863bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer *consumer,
864 bool activated)
865{
34d26f42
RR
866 if (m_toolCapture && !activated)
867 {
868 // We shouldn't be left with a highlighted button
869 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
870 }
3216dbf5
VZ
871
872 return TRUE;
c08a4f00
RR
873}
874
6a317e61
VZ
875#endif // wxUSE_TOOLBAR
876