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