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