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