]> git.saurik.com Git - wxWidgets.git/blame - src/univ/toolbar.cpp
clear m_oldStatusText after restoring it, otherwise we could restore it again the...
[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
170acdc9 427 SetInitialSize(wxDefaultSize);
3216dbf5 428
a290fa5a 429 return true;
3216dbf5
VZ
430}
431
370efbe7
WS
432void wxToolBar::SetWindowStyleFlag( long style )
433{
434 wxToolBarBase::SetWindowStyleFlag(style);
435
436 m_needsLayout = true;
437
438 Refresh();
439}
440
3216dbf5
VZ
441void wxToolBar::DoLayout()
442{
443 wxASSERT_MSG( m_needsLayout, _T("why are we called?") );
444
a290fa5a 445 m_needsLayout = false;
3216dbf5
VZ
446
447 wxCoord x = m_xMargin,
448 y = m_yMargin;
449
370efbe7
WS
450 wxCoord widthTool = 0, maxWidthTool = 0;
451 wxCoord heightTool = 0, maxHeightTool = 0;
32b13913
WS
452 wxCoord margin = IsVertical() ? m_xMargin : m_yMargin;
453 wxCoord *pCur = IsVertical() ? &y : &x;
3216dbf5
VZ
454
455 // calculate the positions of all elements
ac32ba44 456 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
c08a4f00
RR
457 node;
458 node = node->GetNext() )
459 {
3216dbf5
VZ
460 wxToolBarTool *tool = (wxToolBarTool *) node->GetData();
461
462 tool->m_x = x;
463 tool->m_y = y;
464
72726d27 465 // TODO ugly number fiddling
a8f4cabe
JS
466 if (tool->IsButton())
467 {
370efbe7
WS
468 if (IsVertical())
469 {
470 widthTool = m_defaultHeight;
471 heightTool = m_defaultWidth;
472 if(HasFlag(wxTB_TEXT))
473 heightTool += GetFont().GetPointSize() * tool->GetLabel().length();
474 }
475 else
476 {
477 widthTool = m_defaultWidth;
478 if(HasFlag(wxTB_TEXT))
479 widthTool += GetFont().GetPointSize() * tool->GetLabel().length();
480
481 heightTool = m_defaultHeight;
482 }
483
484 if(widthTool > maxWidthTool) // Record max width of tool
485 {
486 maxWidthTool = widthTool;
487 }
488
489 if(heightTool > maxHeightTool) // Record max width of tool
490 {
491 maxHeightTool = heightTool;
492 }
493
a8f4cabe
JS
494 *pCur += widthTool;
495 }
496 else if (tool->IsSeparator())
497 {
498 *pCur += m_widthSeparator;
499 }
500 else if (!IsVertical()) // horizontal control
501 {
502 wxControl *control = tool->GetControl();
503 wxSize size = control->GetSize();
504 tool->m_y += (m_defaultHeight - size.y)/2;
505 tool->m_width = size.x;
506 tool->m_height = size.y;
507
508 *pCur += tool->m_width;
509 }
510 *pCur += margin;
c08a4f00 511 }
3216dbf5
VZ
512
513 // calculate the total toolbar size
370efbe7
WS
514 wxCoord xMin, yMin;
515
516 if(!HasFlag(wxTB_TEXT))
517 {
518 xMin = m_defaultWidth + 2*m_xMargin;
519 yMin = m_defaultHeight + 2*m_yMargin;
520 }
521 else
522 {
523 if (IsVertical())
524 {
525 xMin = heightTool + 2*m_xMargin;
526 yMin = widthTool + 2*m_xMargin;
527 }
528 else
529 {
530 xMin = maxWidthTool + 2*m_xMargin;
531 yMin = heightTool + 2*m_xMargin;
532 }
533 }
3216dbf5
VZ
534
535 m_maxWidth = x < xMin ? xMin : x;
536 m_maxHeight = y < yMin ? yMin : y;
c08a4f00
RR
537}
538
3216dbf5 539wxSize wxToolBar::DoGetBestClientSize() const
c08a4f00 540{
3216dbf5
VZ
541 return wxSize(m_maxWidth, m_maxHeight);
542}
543
dda4f6c0
JS
544void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
545{
546 int old_width, old_height;
547 GetSize(&old_width, &old_height);
548
549 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
32b13913 550
dda4f6c0 551 // Correct width and height if needed.
a290fa5a 552 if ( width == wxDefaultCoord || height == wxDefaultCoord )
dda4f6c0
JS
553 {
554 int tmp_width, tmp_height;
555 GetSize(&tmp_width, &tmp_height);
556
a290fa5a 557 if ( width == wxDefaultCoord )
dda4f6c0 558 width = tmp_width;
a290fa5a 559 if ( height == wxDefaultCoord )
dda4f6c0
JS
560 height = tmp_height;
561 }
32b13913 562
dda4f6c0
JS
563 // We must refresh the frame size when the toolbar changes size
564 // otherwise the toolbar can be shown incorrectly
565 if ( old_width != width || old_height != height )
566 {
32b13913 567 // But before we send the size event check it
dda4f6c0
JS
568 // we have a frame that is not being deleted.
569 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
570 if ( frame && !frame->IsBeingDeleted() )
571 {
572 frame->SendSizeEvent();
573 }
574 }
575}
576
3216dbf5
VZ
577// ----------------------------------------------------------------------------
578// wxToolBar drawing
579// ----------------------------------------------------------------------------
c08a4f00 580
3216dbf5
VZ
581void wxToolBar::RefreshTool(wxToolBarToolBase *tool)
582{
583 RefreshRect(GetToolRect(tool));
584}
585
586void wxToolBar::GetRectLimits(const wxRect& rect,
587 wxCoord *start,
588 wxCoord *end) const
589{
590 wxCHECK_RET( start && end, _T("NULL pointer in GetRectLimits") );
591
592 if ( IsVertical() )
16c9a425 593 {
3216dbf5
VZ
594 *start = rect.GetTop();
595 *end = rect.GetBottom();
16c9a425 596 }
3216dbf5 597 else // horizontal
16c9a425 598 {
3216dbf5
VZ
599 *start = rect.GetLeft();
600 *end = rect.GetRight();
16c9a425 601 }
3216dbf5 602}
c08a4f00 603
3216dbf5
VZ
604void wxToolBar::DoDraw(wxControlRenderer *renderer)
605{
606 // prepare the variables used below
607 wxDC& dc = renderer->GetDC();
608 wxRenderer *rend = renderer->GetRenderer();
370efbe7 609 dc.SetFont(GetFont());
3216dbf5
VZ
610
611 // draw the border separating us from the menubar (if there is no menubar
612 // we probably shouldn't draw it?)
613 if ( !IsVertical() )
614 {
615 rend->DrawHorizontalLine(dc, 0, 0, GetClientSize().x);
616 }
617
618 // get the update rect and its limits depending on the orientation
619 wxRect rectUpdate = GetUpdateClientRect();
620 wxCoord start, end;
621 GetRectLimits(rectUpdate, &start, &end);
622
623 // and redraw all the tools intersecting it
ac32ba44 624 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
c08a4f00
RR
625 node;
626 node = node->GetNext() )
627 {
34d26f42 628 wxToolBarTool *tool = (wxToolBarTool*) node->GetData();
3216dbf5
VZ
629 wxRect rectTool = GetToolRect(tool);
630 wxCoord startTool, endTool;
631 GetRectLimits(rectTool, &startTool, &endTool);
632
633 if ( endTool < start )
c08a4f00 634 {
3216dbf5
VZ
635 // we're still to the left of the area to redraw
636 continue;
16c9a425 637 }
3216dbf5
VZ
638
639 if ( startTool > end )
16c9a425 640 {
3216dbf5
VZ
641 // we're beyond the area to redraw, nothing left to do
642 break;
643 }
32b13913 644
72726d27
RR
645 if (tool->IsSeparator() && !HasFlag(wxTB_FLAT))
646 {
d6922577 647 // Draw separators only in flat mode
72726d27
RR
648 continue;
649 }
32b13913 650
3216dbf5
VZ
651 // deal with the flags
652 int flags = 0;
653
654 if ( tool->IsEnabled() )
655 {
34d26f42
RR
656 // The toolbars without wxTB_FLAT don't react to the mouse hovering
657 if ( !HasFlag(wxTB_FLAT) || tool->IsUnderMouse() )
3216dbf5
VZ
658 flags |= wxCONTROL_CURRENT;
659 }
660 else // disabled tool
661 {
662 flags |= wxCONTROL_DISABLED;
c08a4f00 663 }
3216dbf5 664
34d26f42
RR
665 //if ( tool == m_toolCaptured )
666 // flags |= wxCONTROL_FOCUSED;
5a73d082 667
34d26f42
RR
668 if ( tool->IsPressed() )
669 flags = wxCONTROL_PRESSED;
3216dbf5
VZ
670
671 wxString label;
672 wxBitmap bitmap;
673 if ( !tool->IsSeparator() )
674 {
370efbe7 675 label = tool->GetLabel();
3216dbf5
VZ
676 bitmap = tool->GetBitmap();
677 }
678 //else: leave both the label and the bitmap invalid to draw a separator
679
a8f4cabe
JS
680 if ( !tool->IsControl() )
681 {
370efbe7
WS
682 int tbStyle = 0;
683 if(HasFlag(wxTB_TEXT))
684 {
685 tbStyle |= wxTB_TEXT;
686 }
687
688 if(HasFlag(wxTB_VERTICAL))
689 {
690 tbStyle |= wxTB_VERTICAL;
691 }
692 else
693 {
694 tbStyle |= wxTB_HORIZONTAL;
695 }
696 rend->DrawToolBarButton(dc, label, bitmap, rectTool, flags, tool->GetStyle(), tbStyle);
a8f4cabe
JS
697 }
698 else // control
699 {
700 wxControl *control = tool->GetControl();
701 control->Move(tool->m_x, tool->m_y);
702 }
16c9a425 703 }
3216dbf5 704}
16c9a425 705
3216dbf5
VZ
706// ----------------------------------------------------------------------------
707// wxToolBar actions
708// ----------------------------------------------------------------------------
709
34d26f42
RR
710bool wxToolBar::PerformAction(const wxControlAction& action,
711 long numArg,
712 const wxString& strArg)
bb312b54 713{
34d26f42 714 wxToolBarTool *tool = (wxToolBarTool*) FindById(numArg);
23219626
JS
715 if (!tool)
716 return false;
32b13913 717
34d26f42
RR
718 if ( action == wxACTION_TOOLBAR_TOGGLE )
719 {
720 PerformAction( wxACTION_BUTTON_RELEASE, numArg );
bb312b54 721
34d26f42 722 PerformAction( wxACTION_BUTTON_CLICK, numArg );
e4db172a
WS
723
724 // Write by Danny Raynor to change state again.
c4709ea5 725 // Check button still pressed or not
cc92fd84
WS
726 if ( tool->CanBeToggled() && tool->IsToggled() )
727 {
728 tool->Toggle(false);
729 }
730
c4709ea5 731 if( tool->IsInverted() )
e4db172a
WS
732 {
733 PerformAction( wxACTION_TOOLBAR_RELEASE, numArg );
c4709ea5 734 }
e4db172a 735
c4709ea5
JS
736 // Set mouse leave toolbar button range (If still in the range,
737 // toolbar button would get focus again
738 PerformAction( wxACTION_TOOLBAR_LEAVE, numArg );
34d26f42
RR
739 }
740 else if ( action == wxACTION_TOOLBAR_PRESS )
741 {
742 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool->GetShortHelp().c_str());
32b13913 743
34d26f42 744 tool->Invert();
3216dbf5 745
34d26f42
RR
746 RefreshTool( tool );
747 }
748 else if ( action == wxACTION_TOOLBAR_RELEASE )
5a73d082 749 {
34d26f42 750 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool->GetShortHelp().c_str());
5a73d082 751
34d26f42 752 wxASSERT_MSG( tool->IsInverted(), _T("release unpressed button?") );
32b13913 753
34d26f42 754 tool->Invert();
5a73d082 755
34d26f42 756 RefreshTool( tool );
5a73d082 757 }
34d26f42 758 else if ( action == wxACTION_TOOLBAR_CLICK )
5a73d082 759 {
34d26f42
RR
760 bool isToggled;
761 if ( tool->CanBeToggled() )
762 {
763 tool->Toggle();
5a73d082 764
34d26f42 765 RefreshTool( tool );
bb312b54 766
34d26f42
RR
767 isToggled = tool->IsToggled();
768 }
769 else // simple non-checkable tool
770 {
a290fa5a 771 isToggled = false;
34d26f42
RR
772 }
773 OnLeftClick( tool->GetId(), isToggled );
774 }
3216dbf5 775 else if ( action == wxACTION_TOOLBAR_ENTER )
c08a4f00 776 {
a290fa5a 777 wxCHECK_MSG( tool, false, _T("no tool to enter?") );
32b13913 778
34d26f42 779 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
c08a4f00 780 {
a290fa5a 781 tool->SetUnderMouse( true );
32b13913 782
34d26f42
RR
783 if ( !tool->IsToggled() )
784 RefreshTool( tool );
c08a4f00
RR
785 }
786 }
3216dbf5 787 else if ( action == wxACTION_TOOLBAR_LEAVE )
bb312b54 788 {
a290fa5a 789 wxCHECK_MSG( tool, false, _T("no tool to leave?") );
32b13913 790
34d26f42 791 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
bb312b54 792 {
a290fa5a 793 tool->SetUnderMouse( false );
32b13913 794
34d26f42
RR
795 if ( !tool->IsToggled() )
796 RefreshTool( tool );
bb312b54 797 }
c08a4f00 798 }
3216dbf5
VZ
799 else
800 return wxControl::PerformAction(action, numArg, strArg);
801
a290fa5a 802 return true;
3216dbf5
VZ
803}
804
9467bdb7
VZ
805/* static */
806wxInputHandler *wxToolBar::GetStdInputHandler(wxInputHandler *handlerDef)
807{
808 static wxStdToolbarInputHandler s_handler(handlerDef);
809
810 return &s_handler;
811}
812
3216dbf5
VZ
813// ============================================================================
814// wxStdToolbarInputHandler implementation
815// ============================================================================
816
817wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler *handler)
34d26f42 818 : wxStdInputHandler(handler)
3216dbf5 819{
34d26f42
RR
820 m_winCapture = NULL;
821 m_toolCapture = NULL;
822 m_toolLast = NULL;
3216dbf5
VZ
823}
824
825bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer *consumer,
826 const wxKeyEvent& event,
827 bool pressed)
828{
829 // TODO: when we have a current button we should allow the arrow
830 // keys to move it
831 return wxStdInputHandler::HandleKey(consumer, event, pressed);
832}
833
4e89ceb1
VZ
834bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer *consumer,
835 const wxMouseEvent& event)
836{
4e89ceb1
VZ
837 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
838 wxToolBarToolBase *tool = tbar->FindToolForPosition(event.GetX(), event.GetY());
839
34d26f42
RR
840 if ( event.Button(1) )
841 {
34d26f42
RR
842
843 if ( event.LeftDown() || event.LeftDClick() )
844 {
72726d27 845 if ( !tool || !tool->IsEnabled() )
a290fa5a 846 return true;
32b13913 847
34d26f42
RR
848 m_winCapture = tbar;
849 m_winCapture->CaptureMouse();
32b13913 850
34d26f42
RR
851 m_toolCapture = tool;
852
853 consumer->PerformAction( wxACTION_BUTTON_PRESS, tool->GetId() );
854
a290fa5a 855 return true;
34d26f42
RR
856 }
857 else if ( event.LeftUp() )
858 {
859 if ( m_winCapture )
860 {
861 m_winCapture->ReleaseMouse();
862 m_winCapture = NULL;
863 }
864
2b5f62a0
VZ
865 if (m_toolCapture)
866 {
867 if ( tool == m_toolCapture )
868 consumer->PerformAction( wxACTION_BUTTON_TOGGLE, m_toolCapture->GetId() );
869 else
870 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
871 }
32b13913 872
34d26f42 873 m_toolCapture = NULL;
32b13913 874
a290fa5a 875 return true;
34d26f42
RR
876 }
877 //else: don't do anything special about the double click
878 }
879
880 return wxStdInputHandler::HandleMouse(consumer, event);
4e89ceb1
VZ
881}
882
3216dbf5
VZ
883bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer *consumer,
884 const wxMouseEvent& event)
885{
34d26f42 886 if ( !wxStdInputHandler::HandleMouseMove(consumer, event) )
c08a4f00 887 {
34d26f42 888 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
32b13913 889
34d26f42 890 wxToolBarTool *tool;
3216dbf5 891 if ( event.Leaving() )
c08a4f00 892 {
34d26f42
RR
893 // We cannot possibly be over a tool when
894 // leaving the toolbar
3216dbf5 895 tool = NULL;
c08a4f00 896 }
3216dbf5 897 else
c08a4f00 898 {
34d26f42 899 tool = (wxToolBarTool*) tbar->FindToolForPosition( event.GetX(), event.GetY() );
c08a4f00 900 }
32b13913 901
72726d27 902 if (m_toolCapture)
34d26f42 903 {
72726d27
RR
904 // During capture we only care of the captured tool
905 if (tool && (tool != m_toolCapture))
906 tool = NULL;
32b13913 907
72726d27 908 if (tool == m_toolLast)
a290fa5a 909 return true;
32b13913 910
72726d27
RR
911 if (tool)
912 consumer->PerformAction( wxACTION_BUTTON_PRESS, m_toolCapture->GetId() );
913 else
914 consumer->PerformAction( wxACTION_BUTTON_RELEASE, m_toolCapture->GetId() );
32b13913 915
72726d27 916 m_toolLast = tool;
34d26f42 917 }
72726d27 918 else
34d26f42 919 {
72726d27 920 if (tool == m_toolLast)
a290fa5a 921 return true;
32b13913 922
72726d27
RR
923 if (m_toolLast)
924 {
925 // Leave old tool if any
926 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolLast->GetId() );
927 }
32b13913 928
72726d27
RR
929 if (tool)
930 {
931 // Enter new tool if any
932 consumer->PerformAction( wxACTION_TOOLBAR_ENTER, tool->GetId() );
933 }
32b13913 934
34d26f42 935 m_toolLast = tool;
34d26f42 936 }
32b13913 937
a290fa5a 938 return true;
bb312b54 939 }
3216dbf5 940
a290fa5a 941 return false;
3216dbf5
VZ
942}
943
944bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer *consumer,
61fef19b 945 const wxFocusEvent& WXUNUSED(event))
3216dbf5 946{
32b13913 947 if ( m_toolCapture )
34d26f42
RR
948 {
949 // We shouldn't be left with a highlighted button
950 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
951 }
3216dbf5 952
a290fa5a 953 return true;
3216dbf5
VZ
954}
955
956bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer *consumer,
957 bool activated)
958{
34d26f42
RR
959 if (m_toolCapture && !activated)
960 {
961 // We shouldn't be left with a highlighted button
962 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
963 }
3216dbf5 964
a290fa5a 965 return true;
c08a4f00
RR
966}
967
6a317e61 968#endif // wxUSE_TOOLBAR