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