]> git.saurik.com Git - wxWidgets.git/blame - src/univ/toolbar.cpp
Trigger wxLog auto-creation when getting old logger in wxLogChain ctor.
[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
9a83f860 263 wxCHECK_RET( tool, wxT("SetToolShortHelp: no such tool") );
3216dbf5
VZ
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 294 // created disabled-state bitmap on demand
a1b806b9 295 if ( !enable && !tool->GetDisabledBitmap().IsOk() )
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
9a83f860 347 wxCHECK_MSG( tool, rect, wxT("GetToolRect: NULL tool") );
3216dbf5
VZ
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{
9a83f860 466 wxASSERT_MSG( m_needsLayout, wxT("why are we called?") );
3216dbf5 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 {
0dba08dd 590 SendSizeEventToParent();
dda4f6c0
JS
591 }
592}
593
3216dbf5
VZ
594// ----------------------------------------------------------------------------
595// wxToolBar drawing
596// ----------------------------------------------------------------------------
c08a4f00 597
3216dbf5
VZ
598void wxToolBar::RefreshTool(wxToolBarToolBase *tool)
599{
600 RefreshRect(GetToolRect(tool));
601}
602
603void wxToolBar::GetRectLimits(const wxRect& rect,
604 wxCoord *start,
605 wxCoord *end) const
606{
9a83f860 607 wxCHECK_RET( start && end, wxT("NULL pointer in GetRectLimits") );
3216dbf5
VZ
608
609 if ( IsVertical() )
16c9a425 610 {
3216dbf5
VZ
611 *start = rect.GetTop();
612 *end = rect.GetBottom();
16c9a425 613 }
3216dbf5 614 else // horizontal
16c9a425 615 {
3216dbf5
VZ
616 *start = rect.GetLeft();
617 *end = rect.GetRight();
16c9a425 618 }
3216dbf5 619}
c08a4f00 620
3216dbf5
VZ
621void wxToolBar::DoDraw(wxControlRenderer *renderer)
622{
623 // prepare the variables used below
624 wxDC& dc = renderer->GetDC();
625 wxRenderer *rend = renderer->GetRenderer();
370efbe7 626 dc.SetFont(GetFont());
3216dbf5
VZ
627
628 // draw the border separating us from the menubar (if there is no menubar
629 // we probably shouldn't draw it?)
630 if ( !IsVertical() )
631 {
632 rend->DrawHorizontalLine(dc, 0, 0, GetClientSize().x);
633 }
634
635 // get the update rect and its limits depending on the orientation
636 wxRect rectUpdate = GetUpdateClientRect();
637 wxCoord start, end;
638 GetRectLimits(rectUpdate, &start, &end);
639
640 // and redraw all the tools intersecting it
ac32ba44 641 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
c08a4f00
RR
642 node;
643 node = node->GetNext() )
644 {
34d26f42 645 wxToolBarTool *tool = (wxToolBarTool*) node->GetData();
3216dbf5
VZ
646 wxRect rectTool = GetToolRect(tool);
647 wxCoord startTool, endTool;
648 GetRectLimits(rectTool, &startTool, &endTool);
649
650 if ( endTool < start )
c08a4f00 651 {
3216dbf5
VZ
652 // we're still to the left of the area to redraw
653 continue;
16c9a425 654 }
3216dbf5
VZ
655
656 if ( startTool > end )
16c9a425 657 {
3216dbf5
VZ
658 // we're beyond the area to redraw, nothing left to do
659 break;
660 }
32b13913 661
72726d27
RR
662 if (tool->IsSeparator() && !HasFlag(wxTB_FLAT))
663 {
d6922577 664 // Draw separators only in flat mode
72726d27
RR
665 continue;
666 }
32b13913 667
3216dbf5
VZ
668 // deal with the flags
669 int flags = 0;
670
671 if ( tool->IsEnabled() )
672 {
34d26f42
RR
673 // The toolbars without wxTB_FLAT don't react to the mouse hovering
674 if ( !HasFlag(wxTB_FLAT) || tool->IsUnderMouse() )
3216dbf5
VZ
675 flags |= wxCONTROL_CURRENT;
676 }
677 else // disabled tool
678 {
679 flags |= wxCONTROL_DISABLED;
c08a4f00 680 }
3216dbf5 681
34d26f42
RR
682 //if ( tool == m_toolCaptured )
683 // flags |= wxCONTROL_FOCUSED;
5a73d082 684
34d26f42
RR
685 if ( tool->IsPressed() )
686 flags = wxCONTROL_PRESSED;
3216dbf5
VZ
687
688 wxString label;
689 wxBitmap bitmap;
690 if ( !tool->IsSeparator() )
691 {
370efbe7 692 label = tool->GetLabel();
3216dbf5 693 bitmap = tool->GetBitmap();
57c6f0fe
VZ
694
695 if ( !bitmap.IsOk() )
696 {
697 // it's better not to draw anything than to assert inside
698 // drawing code as this results in an almost guaranteed crash
699 // as we're likely to be called by a paint event handler and so
700 // the assert is going to be triggered again and again and ...
701 continue;
702 }
3216dbf5
VZ
703 }
704 //else: leave both the label and the bitmap invalid to draw a separator
705
a8f4cabe
JS
706 if ( !tool->IsControl() )
707 {
57c6f0fe
VZ
708 int tbStyle = HasFlag(wxTB_VERTICAL) ? wxTB_VERTICAL : wxTB_HORIZONTAL;
709 if ( HasFlag(wxTB_TEXT) )
370efbe7 710 tbStyle |= wxTB_TEXT;
370efbe7 711
57c6f0fe
VZ
712 rend->DrawToolBarButton(dc, label, bitmap, rectTool, flags,
713 tool->GetStyle(), tbStyle);
a8f4cabe
JS
714 }
715 else // control
716 {
717 wxControl *control = tool->GetControl();
718 control->Move(tool->m_x, tool->m_y);
719 }
16c9a425 720 }
3216dbf5 721}
16c9a425 722
3216dbf5
VZ
723// ----------------------------------------------------------------------------
724// wxToolBar actions
725// ----------------------------------------------------------------------------
726
34d26f42
RR
727bool wxToolBar::PerformAction(const wxControlAction& action,
728 long numArg,
729 const wxString& strArg)
bb312b54 730{
34d26f42 731 wxToolBarTool *tool = (wxToolBarTool*) FindById(numArg);
23219626
JS
732 if (!tool)
733 return false;
32b13913 734
34d26f42
RR
735 if ( action == wxACTION_TOOLBAR_TOGGLE )
736 {
737 PerformAction( wxACTION_BUTTON_RELEASE, numArg );
bb312b54 738
34d26f42 739 PerformAction( wxACTION_BUTTON_CLICK, numArg );
e4db172a 740
c4709ea5
JS
741 // Set mouse leave toolbar button range (If still in the range,
742 // toolbar button would get focus again
743 PerformAction( wxACTION_TOOLBAR_LEAVE, numArg );
34d26f42
RR
744 }
745 else if ( action == wxACTION_TOOLBAR_PRESS )
746 {
9a83f860 747 wxLogTrace(wxT("toolbar"), wxT("Button '%s' pressed."), tool->GetShortHelp().c_str());
32b13913 748
34d26f42 749 tool->Invert();
3216dbf5 750
34d26f42
RR
751 RefreshTool( tool );
752 }
753 else if ( action == wxACTION_TOOLBAR_RELEASE )
5a73d082 754 {
9a83f860 755 wxLogTrace(wxT("toolbar"), wxT("Button '%s' released."), tool->GetShortHelp().c_str());
5a73d082 756
9a83f860 757 wxASSERT_MSG( tool->IsInverted(), wxT("release unpressed button?") );
32b13913 758
5c680882
VZ
759 if(tool->IsInverted())
760 {
761 tool->Invert();
762 }
5a73d082 763
34d26f42 764 RefreshTool( tool );
5a73d082 765 }
34d26f42 766 else if ( action == wxACTION_TOOLBAR_CLICK )
5a73d082 767 {
34d26f42
RR
768 bool isToggled;
769 if ( tool->CanBeToggled() )
770 {
5c680882
VZ
771 if ( tool->IsButton() && tool->GetKind() == wxITEM_RADIO )
772 {
773 UnToggleRadioGroup(tool);
774 tool->Toggle(true);
775 }
776 else
777 {
778 tool->Toggle();
779 }
5a73d082 780
34d26f42 781 RefreshTool( tool );
bb312b54 782
34d26f42
RR
783 isToggled = tool->IsToggled();
784 }
785 else // simple non-checkable tool
786 {
a290fa5a 787 isToggled = false;
34d26f42
RR
788 }
789 OnLeftClick( tool->GetId(), isToggled );
790 }
3216dbf5 791 else if ( action == wxACTION_TOOLBAR_ENTER )
c08a4f00 792 {
9a83f860 793 wxCHECK_MSG( tool, false, wxT("no tool to enter?") );
32b13913 794
34d26f42 795 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
c08a4f00 796 {
a290fa5a 797 tool->SetUnderMouse( true );
32b13913 798
34d26f42
RR
799 if ( !tool->IsToggled() )
800 RefreshTool( tool );
c08a4f00
RR
801 }
802 }
3216dbf5 803 else if ( action == wxACTION_TOOLBAR_LEAVE )
bb312b54 804 {
9a83f860 805 wxCHECK_MSG( tool, false, wxT("no tool to leave?") );
32b13913 806
34d26f42 807 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
bb312b54 808 {
a290fa5a 809 tool->SetUnderMouse( false );
32b13913 810
34d26f42
RR
811 if ( !tool->IsToggled() )
812 RefreshTool( tool );
bb312b54 813 }
c08a4f00 814 }
3216dbf5
VZ
815 else
816 return wxControl::PerformAction(action, numArg, strArg);
817
a290fa5a 818 return true;
3216dbf5
VZ
819}
820
9467bdb7
VZ
821/* static */
822wxInputHandler *wxToolBar::GetStdInputHandler(wxInputHandler *handlerDef)
823{
824 static wxStdToolbarInputHandler s_handler(handlerDef);
825
826 return &s_handler;
827}
828
3216dbf5
VZ
829// ============================================================================
830// wxStdToolbarInputHandler implementation
831// ============================================================================
832
833wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler *handler)
34d26f42 834 : wxStdInputHandler(handler)
3216dbf5 835{
34d26f42
RR
836 m_winCapture = NULL;
837 m_toolCapture = NULL;
838 m_toolLast = NULL;
3216dbf5
VZ
839}
840
841bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer *consumer,
842 const wxKeyEvent& event,
843 bool pressed)
844{
845 // TODO: when we have a current button we should allow the arrow
846 // keys to move it
847 return wxStdInputHandler::HandleKey(consumer, event, pressed);
848}
849
4e89ceb1
VZ
850bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer *consumer,
851 const wxMouseEvent& event)
852{
4e89ceb1
VZ
853 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
854 wxToolBarToolBase *tool = tbar->FindToolForPosition(event.GetX(), event.GetY());
855
34d26f42
RR
856 if ( event.Button(1) )
857 {
34d26f42
RR
858
859 if ( event.LeftDown() || event.LeftDClick() )
860 {
72726d27 861 if ( !tool || !tool->IsEnabled() )
a290fa5a 862 return true;
32b13913 863
34d26f42
RR
864 m_winCapture = tbar;
865 m_winCapture->CaptureMouse();
32b13913 866
34d26f42
RR
867 m_toolCapture = tool;
868
869 consumer->PerformAction( wxACTION_BUTTON_PRESS, tool->GetId() );
870
a290fa5a 871 return true;
34d26f42
RR
872 }
873 else if ( event.LeftUp() )
874 {
875 if ( m_winCapture )
876 {
877 m_winCapture->ReleaseMouse();
878 m_winCapture = NULL;
879 }
880
2b5f62a0
VZ
881 if (m_toolCapture)
882 {
883 if ( tool == m_toolCapture )
884 consumer->PerformAction( wxACTION_BUTTON_TOGGLE, m_toolCapture->GetId() );
885 else
886 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
887 }
32b13913 888
34d26f42 889 m_toolCapture = NULL;
32b13913 890
a290fa5a 891 return true;
34d26f42
RR
892 }
893 //else: don't do anything special about the double click
894 }
895
896 return wxStdInputHandler::HandleMouse(consumer, event);
4e89ceb1
VZ
897}
898
3216dbf5
VZ
899bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer *consumer,
900 const wxMouseEvent& event)
901{
34d26f42 902 if ( !wxStdInputHandler::HandleMouseMove(consumer, event) )
c08a4f00 903 {
34d26f42 904 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
32b13913 905
34d26f42 906 wxToolBarTool *tool;
3216dbf5 907 if ( event.Leaving() )
c08a4f00 908 {
34d26f42
RR
909 // We cannot possibly be over a tool when
910 // leaving the toolbar
3216dbf5 911 tool = NULL;
c08a4f00 912 }
3216dbf5 913 else
c08a4f00 914 {
34d26f42 915 tool = (wxToolBarTool*) tbar->FindToolForPosition( event.GetX(), event.GetY() );
c08a4f00 916 }
32b13913 917
72726d27 918 if (m_toolCapture)
34d26f42 919 {
72726d27
RR
920 // During capture we only care of the captured tool
921 if (tool && (tool != m_toolCapture))
922 tool = NULL;
32b13913 923
72726d27 924 if (tool == m_toolLast)
a290fa5a 925 return true;
32b13913 926
72726d27
RR
927 if (tool)
928 consumer->PerformAction( wxACTION_BUTTON_PRESS, m_toolCapture->GetId() );
929 else
930 consumer->PerformAction( wxACTION_BUTTON_RELEASE, m_toolCapture->GetId() );
32b13913 931
72726d27 932 m_toolLast = tool;
34d26f42 933 }
72726d27 934 else
34d26f42 935 {
72726d27 936 if (tool == m_toolLast)
a290fa5a 937 return true;
32b13913 938
72726d27
RR
939 if (m_toolLast)
940 {
941 // Leave old tool if any
942 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolLast->GetId() );
943 }
32b13913 944
72726d27
RR
945 if (tool)
946 {
947 // Enter new tool if any
948 consumer->PerformAction( wxACTION_TOOLBAR_ENTER, tool->GetId() );
949 }
32b13913 950
34d26f42 951 m_toolLast = tool;
34d26f42 952 }
32b13913 953
a290fa5a 954 return true;
bb312b54 955 }
3216dbf5 956
a290fa5a 957 return false;
3216dbf5
VZ
958}
959
960bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer *consumer,
61fef19b 961 const wxFocusEvent& WXUNUSED(event))
3216dbf5 962{
32b13913 963 if ( m_toolCapture )
34d26f42
RR
964 {
965 // We shouldn't be left with a highlighted button
966 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
967 }
3216dbf5 968
a290fa5a 969 return true;
3216dbf5
VZ
970}
971
972bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer *consumer,
973 bool activated)
974{
34d26f42
RR
975 if (m_toolCapture && !activated)
976 {
977 // We shouldn't be left with a highlighted button
978 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
979 }
3216dbf5 980
a290fa5a 981 return true;
c08a4f00
RR
982}
983
6a317e61 984#endif // wxUSE_TOOLBAR