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