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