]> git.saurik.com Git - wxWidgets.git/blame - src/univ/toolbar.cpp
Committing in .
[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"
c08a4f00
RR
38#endif
39
a9b33d6b
VZ
40#include "wx/univ/renderer.h"
41
c229e50d 42#include "wx/image.h"
c08a4f00 43
3216dbf5
VZ
44// ----------------------------------------------------------------------------
45// constants
46// ----------------------------------------------------------------------------
47
48// value meaning that m_widthSeparator is not initialized
a290fa5a 49static const wxCoord INVALID_WIDTH = wxDefaultCoord;
3216dbf5
VZ
50
51// ----------------------------------------------------------------------------
52// wxToolBarTool: our implementation of wxToolBarToolBase
53// ----------------------------------------------------------------------------
54
55class WXDLLEXPORT wxToolBarTool : public wxToolBarToolBase
16c9a425 56{
3216dbf5 57public:
d448aec3
VZ
58 wxToolBarTool(wxToolBar *tbar,
59 int id,
60 const wxString& label,
61 const wxBitmap& bmpNormal,
62 const wxBitmap& bmpDisabled,
63 wxItemKind kind,
64 wxObject *clientData,
65 const wxString& shortHelp,
66 const wxString& longHelp)
67 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
68 clientData, shortHelp, longHelp)
3216dbf5
VZ
69 {
70 // no position yet
71 m_x =
a290fa5a 72 m_y = wxDefaultCoord;
a8f4cabe
JS
73 m_width =
74 m_height = 0;
75
76 // not pressed yet
a290fa5a 77 m_isInverted = false;
32b13913 78
a8f4cabe 79 // mouse not here yet
a290fa5a 80 m_underMouse = false;
a8f4cabe
JS
81 }
82
83 wxToolBarTool(wxToolBar *tbar, wxControl *control)
84 : wxToolBarToolBase(tbar, control)
85 {
86 // no position yet
87 m_x =
a290fa5a 88 m_y = wxDefaultCoord;
a8f4cabe
JS
89 m_width =
90 m_height = 0;
5a73d082
VZ
91
92 // not pressed yet
a290fa5a 93 m_isInverted = false;
32b13913 94
34d26f42 95 // mouse not here yet
a290fa5a 96 m_underMouse = false;
3216dbf5
VZ
97 }
98
5a73d082
VZ
99 // is this tool pressed, even temporarily? (this is different from being
100 // permanently toggled which is what IsToggled() returns)
101 bool IsPressed() const
102 { return CanBeToggled() ? IsToggled() != m_isInverted : m_isInverted; }
103
104 // are we temporarily pressed/unpressed?
105 bool IsInverted() const { return m_isInverted; }
106
107 // press the tool temporarily by inverting its toggle state
108 void Invert() { m_isInverted = !m_isInverted; }
32b13913 109
34d26f42 110 // Set underMouse
a290fa5a 111 void SetUnderMouse( bool under = true ) { m_underMouse = under; }
34d26f42 112 bool IsUnderMouse() { return m_underMouse; }
5a73d082 113
3216dbf5 114public:
a8f4cabe
JS
115 // the tool position (for controls)
116 wxCoord m_x;
117 wxCoord m_y;
118 wxCoord m_width;
119 wxCoord m_height;
5a73d082
VZ
120
121private:
a290fa5a 122 // true if the tool is pressed
5a73d082 123 bool m_isInverted;
32b13913 124
a290fa5a 125 // true if the tool is under the mouse
34d26f42 126 bool m_underMouse;
3216dbf5
VZ
127};
128
129// ============================================================================
130// wxToolBar implementation
131// ============================================================================
132
412e0d47 133IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
3216dbf5
VZ
134
135// ----------------------------------------------------------------------------
136// wxToolBar creation
137// ----------------------------------------------------------------------------
c08a4f00
RR
138
139void wxToolBar::Init()
140{
3216dbf5 141 // no tools yet
a290fa5a 142 m_needsLayout = false;
3216dbf5
VZ
143
144 // unknown widths for the tools and separators
145 m_widthSeparator = INVALID_WIDTH;
146
147 m_maxWidth =
16c9a425
JS
148 m_maxHeight = 0;
149
3216dbf5
VZ
150 wxRenderer *renderer = GetRenderer();
151
152 SetToolBitmapSize(renderer->GetToolBarButtonSize(&m_widthSeparator));
153 SetMargins(renderer->GetToolBarMargin());
c08a4f00
RR
154}
155
3216dbf5
VZ
156bool wxToolBar::Create(wxWindow *parent,
157 wxWindowID id,
158 const wxPoint& pos,
159 const wxSize& size,
160 long style,
161 const wxString& name)
162{
163 if ( !wxToolBarBase::Create(parent, id, pos, size, style,
164 wxDefaultValidator, name) )
165 {
a290fa5a 166 return false;
3216dbf5
VZ
167 }
168
169 CreateInputHandler(wxINP_HANDLER_TOOLBAR);
170
171 SetBestSize(size);
172
a290fa5a 173 return true;
3216dbf5
VZ
174}
175
176wxToolBar::~wxToolBar()
177{
dda4f6c0
JS
178 // Make sure the toolbar is removed from the parent.
179 SetSize(0,0);
3216dbf5
VZ
180}
181
34d26f42
RR
182void wxToolBar::SetMargins(int x, int y)
183{
184 // This required for similar visual effects under
185 // native platforms and wxUniv.
72726d27 186 wxToolBarBase::SetMargins( x + 3, y + 3 );
34d26f42
RR
187}
188
3216dbf5
VZ
189// ----------------------------------------------------------------------------
190// wxToolBar tool-related methods
191// ----------------------------------------------------------------------------
192
c08a4f00
RR
193wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
194{
3216dbf5
VZ
195 // check the "other" direction first: it must be inside the toolbar or we
196 // don't risk finding anything
197 if ( IsVertical() )
198 {
199 if ( x < 0 || x > m_maxWidth )
200 return NULL;
201
202 // we always use x, even for a vertical toolbar, this makes the code
203 // below simpler
204 x = y;
205 }
206 else // horizontal
207 {
208 if ( y < 0 || y > m_maxHeight )
209 return NULL;
210 }
211
ac32ba44 212 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
3216dbf5
VZ
213 node;
214 node = node->GetNext() )
215 {
216 wxToolBarToolBase *tool = node->GetData();
217 wxRect rectTool = GetToolRect(tool);
218
219 wxCoord startTool, endTool;
220 GetRectLimits(rectTool, &startTool, &endTool);
221
222 if ( x >= startTool && x <= endTool )
223 {
224 // don't return the separators from here, they don't accept any
225 // input anyhow
226 return tool->IsSeparator() ? NULL : tool;
227 }
228 }
229
c08a4f00
RR
230 return NULL;
231}
232
3216dbf5 233void wxToolBar::SetToolShortHelp(int id, const wxString& help)
c08a4f00 234{
3216dbf5
VZ
235 wxToolBarToolBase *tool = FindById(id);
236
237 wxCHECK_RET( tool, _T("SetToolShortHelp: no such tool") );
238
239 tool->SetShortHelp(help);
c08a4f00
RR
240}
241
3216dbf5
VZ
242bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
243 wxToolBarToolBase * WXUNUSED(tool))
c08a4f00 244{
3216dbf5 245 // recalculate the toolbar geometry before redrawing it the next time
a290fa5a 246 m_needsLayout = true;
3216dbf5
VZ
247
248 // and ensure that we indeed are going to redraw
249 Refresh();
250
a290fa5a 251 return true;
c08a4f00
RR
252}
253
3216dbf5
VZ
254bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos),
255 wxToolBarToolBase * WXUNUSED(tool))
c08a4f00 256{
3216dbf5 257 // as above
a290fa5a 258 m_needsLayout = true;
3216dbf5
VZ
259
260 Refresh();
261
a290fa5a 262 return true;
c08a4f00
RR
263}
264
265void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
266{
3216dbf5
VZ
267 // created disabled-state bitmap on demand
268 if ( !enable && !tool->GetDisabledBitmap().Ok() )
c229e50d 269 {
d448aec3 270 wxImage image( tool->GetNormalBitmap().ConvertToImage() );
c229e50d 271
3216dbf5 272 // TODO: don't hardcode 180
bb312b54
RR
273 unsigned char bg_red = 180;
274 unsigned char bg_green = 180;
275 unsigned char bg_blue = 180;
3216dbf5 276
bb312b54
RR
277 unsigned char mask_red = image.GetMaskRed();
278 unsigned char mask_green = image.GetMaskGreen();
279 unsigned char mask_blue = image.GetMaskBlue();
3216dbf5 280
bb312b54 281 bool has_mask = image.HasMask();
3216dbf5 282
bb312b54
RR
283 int x,y;
284 for (y = 0; y < image.GetHeight(); y++)
285 {
3216dbf5 286 for (x = 0; x < image.GetWidth(); x++)
bb312b54
RR
287 {
288 unsigned char red = image.GetRed(x,y);
289 unsigned char green = image.GetGreen(x,y);
290 unsigned char blue = image.GetBlue(x,y);
291 if (!has_mask || red != mask_red || green != mask_green || blue != mask_blue)
292 {
32b13913
WS
293 red = (unsigned char)((((wxInt32) red - bg_red) >> 1) + bg_red);
294 green = (unsigned char)((((wxInt32) green - bg_green) >> 1) + bg_green);
295 blue = (unsigned char)((((wxInt32) blue - bg_blue) >> 1) + bg_blue);
3216dbf5 296 image.SetRGB( x, y, red, green, blue );
bb312b54
RR
297 }
298 }
299 }
c229e50d 300
bb312b54
RR
301 for (y = 0; y < image.GetHeight(); y++)
302 {
3216dbf5 303 for (x = y % 2; x < image.GetWidth(); x += 2)
bb312b54
RR
304 {
305 unsigned char red = image.GetRed(x,y);
306 unsigned char green = image.GetGreen(x,y);
307 unsigned char blue = image.GetBlue(x,y);
308 if (!has_mask || red != mask_red || green != mask_green || blue != mask_blue)
309 {
32b13913
WS
310 red = (unsigned char)((((wxInt32) red - bg_red) >> 1) + bg_red);
311 green = (unsigned char)((((wxInt32) green - bg_green) >> 1) + bg_green);
312 blue = (unsigned char)((((wxInt32) blue - bg_blue) >> 1) + bg_blue);
bb312b54
RR
313 image.SetRGB( x, y, red, green, blue );
314 }
315 }
316 }
317
d448aec3 318 tool->SetDisabledBitmap(image);
c229e50d 319 }
3216dbf5
VZ
320
321 RefreshTool(tool);
c08a4f00
RR
322}
323
3216dbf5 324void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool WXUNUSED(toggle))
c08a4f00 325{
3216dbf5
VZ
326 // note that if we're called the tool did change state (the base class
327 // checks for it), so it's not necessary to check for this again here
328 RefreshTool(tool);
c08a4f00
RR
329}
330
3216dbf5 331void wxToolBar::DoSetToggle(wxToolBarToolBase *tool, bool WXUNUSED(toggle))
c08a4f00 332{
3216dbf5 333 RefreshTool(tool);
c08a4f00
RR
334}
335
336wxToolBarToolBase *wxToolBar::CreateTool(int id,
d448aec3
VZ
337 const wxString& label,
338 const wxBitmap& bmpNormal,
339 const wxBitmap& bmpDisabled,
340 wxItemKind kind,
3216dbf5 341 wxObject *clientData,
d448aec3
VZ
342 const wxString& shortHelp,
343 const wxString& longHelp)
c08a4f00 344{
d448aec3
VZ
345 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
346 clientData, shortHelp, longHelp);
c08a4f00 347}
3216dbf5 348
c08a4f00
RR
349wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
350{
a8f4cabe 351 return new wxToolBarTool(this, control);
c08a4f00
RR
352}
353
3216dbf5
VZ
354// ----------------------------------------------------------------------------
355// wxToolBar geometry
356// ----------------------------------------------------------------------------
c08a4f00 357
3216dbf5 358wxRect wxToolBar::GetToolRect(wxToolBarToolBase *toolBase) const
c08a4f00 359{
3216dbf5
VZ
360 const wxToolBarTool *tool = (wxToolBarTool *)toolBase;
361
362 wxRect rect;
363
364 wxCHECK_MSG( tool, rect, _T("GetToolRect: NULL tool") );
365
366 // ensure that we always have the valid tool position
367 if ( m_needsLayout )
bb312b54 368 {
3216dbf5 369 wxConstCast(this, wxToolBar)->DoLayout();
bb312b54 370 }
3216dbf5
VZ
371
372 rect.x = tool->m_x - m_xMargin;
373 rect.y = tool->m_y - m_yMargin;
374
375 if ( IsVertical() )
c08a4f00 376 {
a290fa5a 377 if (tool->IsButton())
a8f4cabe 378 {
370efbe7
WS
379 if(!HasFlag(wxTB_TEXT))
380 {
381 rect.width = m_defaultWidth;
382 rect.height = m_defaultHeight;
383 }
384 else
385 {
386 rect.width = m_defaultWidth +
387 GetFont().GetPointSize() * tool->GetLabel().length();
388 rect.height = m_defaultHeight;
389 }
a8f4cabe
JS
390 }
391 else if (tool->IsSeparator())
392 {
393 rect.width = m_defaultWidth;
394 rect.height = m_widthSeparator;
395 }
396 else // control
397 {
398 rect.width = tool->m_width;
399 rect.height = tool->m_height;
400 }
c08a4f00 401 }
3216dbf5 402 else // horizontal
c08a4f00 403 {
a8f4cabe
JS
404 if (tool->IsButton())
405 {
370efbe7
WS
406 if(!HasFlag(wxTB_TEXT))
407 {
408 rect.width = m_defaultWidth;
409 rect.height = m_defaultHeight;
410 }
411 else
412 {
413 rect.width = m_defaultWidth +
414 GetFont().GetPointSize() * tool->GetLabel().length();
415 rect.height = m_defaultHeight;
416 }
a8f4cabe
JS
417 }
418 else if (tool->IsSeparator())
419 {
420 rect.width = m_widthSeparator;
421 rect.height = m_defaultHeight;
422 }
423 else // control
424 {
425 rect.width = tool->m_width;
426 rect.height = tool->m_height;
427 }
c08a4f00 428 }
3216dbf5
VZ
429
430 rect.width += 2*m_xMargin;
431 rect.height += 2*m_yMargin;
432
433 return rect;
c08a4f00
RR
434}
435
3216dbf5 436bool wxToolBar::Realize()
c08a4f00 437{
3216dbf5 438 if ( !wxToolBarBase::Realize() )
a290fa5a 439 return false;
3216dbf5 440
a290fa5a 441 m_needsLayout = true;
3216dbf5
VZ
442 DoLayout();
443
444 SetBestSize(wxDefaultSize);
445
a290fa5a 446 return true;
3216dbf5
VZ
447}
448
370efbe7
WS
449void wxToolBar::SetWindowStyleFlag( long style )
450{
451 wxToolBarBase::SetWindowStyleFlag(style);
452
453 m_needsLayout = true;
454
455 Refresh();
456}
457
3216dbf5
VZ
458void wxToolBar::DoLayout()
459{
460 wxASSERT_MSG( m_needsLayout, _T("why are we called?") );
461
a290fa5a 462 m_needsLayout = false;
3216dbf5
VZ
463
464 wxCoord x = m_xMargin,
465 y = m_yMargin;
466
370efbe7
WS
467 wxCoord widthTool = 0, maxWidthTool = 0;
468 wxCoord heightTool = 0, maxHeightTool = 0;
32b13913
WS
469 wxCoord margin = IsVertical() ? m_xMargin : m_yMargin;
470 wxCoord *pCur = IsVertical() ? &y : &x;
3216dbf5
VZ
471
472 // calculate the positions of all elements
ac32ba44 473 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
c08a4f00
RR
474 node;
475 node = node->GetNext() )
476 {
3216dbf5
VZ
477 wxToolBarTool *tool = (wxToolBarTool *) node->GetData();
478
479 tool->m_x = x;
480 tool->m_y = y;
481
72726d27 482 // TODO ugly number fiddling
a8f4cabe
JS
483 if (tool->IsButton())
484 {
370efbe7
WS
485 if (IsVertical())
486 {
487 widthTool = m_defaultHeight;
488 heightTool = m_defaultWidth;
489 if(HasFlag(wxTB_TEXT))
490 heightTool += GetFont().GetPointSize() * tool->GetLabel().length();
491 }
492 else
493 {
494 widthTool = m_defaultWidth;
495 if(HasFlag(wxTB_TEXT))
496 widthTool += GetFont().GetPointSize() * tool->GetLabel().length();
497
498 heightTool = m_defaultHeight;
499 }
500
501 if(widthTool > maxWidthTool) // Record max width of tool
502 {
503 maxWidthTool = widthTool;
504 }
505
506 if(heightTool > maxHeightTool) // Record max width of tool
507 {
508 maxHeightTool = heightTool;
509 }
510
a8f4cabe
JS
511 *pCur += widthTool;
512 }
513 else if (tool->IsSeparator())
514 {
515 *pCur += m_widthSeparator;
516 }
517 else if (!IsVertical()) // horizontal control
518 {
519 wxControl *control = tool->GetControl();
520 wxSize size = control->GetSize();
521 tool->m_y += (m_defaultHeight - size.y)/2;
522 tool->m_width = size.x;
523 tool->m_height = size.y;
524
525 *pCur += tool->m_width;
526 }
527 *pCur += margin;
c08a4f00 528 }
3216dbf5
VZ
529
530 // calculate the total toolbar size
370efbe7
WS
531 wxCoord xMin, yMin;
532
533 if(!HasFlag(wxTB_TEXT))
534 {
535 xMin = m_defaultWidth + 2*m_xMargin;
536 yMin = m_defaultHeight + 2*m_yMargin;
537 }
538 else
539 {
540 if (IsVertical())
541 {
542 xMin = heightTool + 2*m_xMargin;
543 yMin = widthTool + 2*m_xMargin;
544 }
545 else
546 {
547 xMin = maxWidthTool + 2*m_xMargin;
548 yMin = heightTool + 2*m_xMargin;
549 }
550 }
3216dbf5
VZ
551
552 m_maxWidth = x < xMin ? xMin : x;
553 m_maxHeight = y < yMin ? yMin : y;
c08a4f00
RR
554}
555
3216dbf5 556wxSize wxToolBar::DoGetBestClientSize() const
c08a4f00 557{
3216dbf5
VZ
558 return wxSize(m_maxWidth, m_maxHeight);
559}
560
dda4f6c0
JS
561void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
562{
563 int old_width, old_height;
564 GetSize(&old_width, &old_height);
565
566 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
32b13913 567
dda4f6c0 568 // Correct width and height if needed.
a290fa5a 569 if ( width == wxDefaultCoord || height == wxDefaultCoord )
dda4f6c0
JS
570 {
571 int tmp_width, tmp_height;
572 GetSize(&tmp_width, &tmp_height);
573
a290fa5a 574 if ( width == wxDefaultCoord )
dda4f6c0 575 width = tmp_width;
a290fa5a 576 if ( height == wxDefaultCoord )
dda4f6c0
JS
577 height = tmp_height;
578 }
32b13913 579
dda4f6c0
JS
580 // We must refresh the frame size when the toolbar changes size
581 // otherwise the toolbar can be shown incorrectly
582 if ( old_width != width || old_height != height )
583 {
32b13913 584 // But before we send the size event check it
dda4f6c0
JS
585 // we have a frame that is not being deleted.
586 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
587 if ( frame && !frame->IsBeingDeleted() )
588 {
589 frame->SendSizeEvent();
590 }
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{
607 wxCHECK_RET( start && end, _T("NULL pointer in GetRectLimits") );
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
VZ
693 bitmap = tool->GetBitmap();
694 }
695 //else: leave both the label and the bitmap invalid to draw a separator
696
a8f4cabe
JS
697 if ( !tool->IsControl() )
698 {
370efbe7
WS
699 int tbStyle = 0;
700 if(HasFlag(wxTB_TEXT))
701 {
702 tbStyle |= wxTB_TEXT;
703 }
704
705 if(HasFlag(wxTB_VERTICAL))
706 {
707 tbStyle |= wxTB_VERTICAL;
708 }
709 else
710 {
711 tbStyle |= wxTB_HORIZONTAL;
712 }
713 rend->DrawToolBarButton(dc, label, bitmap, rectTool, flags, 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
WS
740
741 // Write by Danny Raynor to change state again.
c4709ea5 742 // Check button still pressed or not
cc92fd84
WS
743 if ( tool->CanBeToggled() && tool->IsToggled() )
744 {
745 tool->Toggle(false);
746 }
747
c4709ea5 748 if( tool->IsInverted() )
e4db172a
WS
749 {
750 PerformAction( wxACTION_TOOLBAR_RELEASE, numArg );
c4709ea5 751 }
e4db172a 752
c4709ea5
JS
753 // Set mouse leave toolbar button range (If still in the range,
754 // toolbar button would get focus again
755 PerformAction( wxACTION_TOOLBAR_LEAVE, numArg );
34d26f42
RR
756 }
757 else if ( action == wxACTION_TOOLBAR_PRESS )
758 {
759 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool->GetShortHelp().c_str());
32b13913 760
34d26f42 761 tool->Invert();
3216dbf5 762
34d26f42
RR
763 RefreshTool( tool );
764 }
765 else if ( action == wxACTION_TOOLBAR_RELEASE )
5a73d082 766 {
34d26f42 767 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool->GetShortHelp().c_str());
5a73d082 768
34d26f42 769 wxASSERT_MSG( tool->IsInverted(), _T("release unpressed button?") );
32b13913 770
34d26f42 771 tool->Invert();
5a73d082 772
34d26f42 773 RefreshTool( tool );
5a73d082 774 }
34d26f42 775 else if ( action == wxACTION_TOOLBAR_CLICK )
5a73d082 776 {
34d26f42
RR
777 bool isToggled;
778 if ( tool->CanBeToggled() )
779 {
780 tool->Toggle();
5a73d082 781
34d26f42 782 RefreshTool( tool );
bb312b54 783
34d26f42
RR
784 isToggled = tool->IsToggled();
785 }
786 else // simple non-checkable tool
787 {
a290fa5a 788 isToggled = false;
34d26f42
RR
789 }
790 OnLeftClick( tool->GetId(), isToggled );
791 }
3216dbf5 792 else if ( action == wxACTION_TOOLBAR_ENTER )
c08a4f00 793 {
a290fa5a 794 wxCHECK_MSG( tool, false, _T("no tool to enter?") );
32b13913 795
34d26f42 796 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
c08a4f00 797 {
a290fa5a 798 tool->SetUnderMouse( true );
32b13913 799
34d26f42
RR
800 if ( !tool->IsToggled() )
801 RefreshTool( tool );
c08a4f00
RR
802 }
803 }
3216dbf5 804 else if ( action == wxACTION_TOOLBAR_LEAVE )
bb312b54 805 {
a290fa5a 806 wxCHECK_MSG( tool, false, _T("no tool to leave?") );
32b13913 807
34d26f42 808 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
bb312b54 809 {
a290fa5a 810 tool->SetUnderMouse( false );
32b13913 811
34d26f42
RR
812 if ( !tool->IsToggled() )
813 RefreshTool( tool );
bb312b54 814 }
c08a4f00 815 }
3216dbf5
VZ
816 else
817 return wxControl::PerformAction(action, numArg, strArg);
818
a290fa5a 819 return true;
3216dbf5
VZ
820}
821
822// ============================================================================
823// wxStdToolbarInputHandler implementation
824// ============================================================================
825
826wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler *handler)
34d26f42 827 : wxStdInputHandler(handler)
3216dbf5 828{
34d26f42
RR
829 m_winCapture = NULL;
830 m_toolCapture = NULL;
831 m_toolLast = NULL;
3216dbf5
VZ
832}
833
834bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer *consumer,
835 const wxKeyEvent& event,
836 bool pressed)
837{
838 // TODO: when we have a current button we should allow the arrow
839 // keys to move it
840 return wxStdInputHandler::HandleKey(consumer, event, pressed);
841}
842
4e89ceb1
VZ
843bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer *consumer,
844 const wxMouseEvent& event)
845{
4e89ceb1
VZ
846 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
847 wxToolBarToolBase *tool = tbar->FindToolForPosition(event.GetX(), event.GetY());
848
34d26f42
RR
849 if ( event.Button(1) )
850 {
34d26f42
RR
851
852 if ( event.LeftDown() || event.LeftDClick() )
853 {
72726d27 854 if ( !tool || !tool->IsEnabled() )
a290fa5a 855 return true;
32b13913 856
34d26f42
RR
857 m_winCapture = tbar;
858 m_winCapture->CaptureMouse();
32b13913 859
34d26f42
RR
860 m_toolCapture = tool;
861
862 consumer->PerformAction( wxACTION_BUTTON_PRESS, tool->GetId() );
863
a290fa5a 864 return true;
34d26f42
RR
865 }
866 else if ( event.LeftUp() )
867 {
868 if ( m_winCapture )
869 {
870 m_winCapture->ReleaseMouse();
871 m_winCapture = NULL;
872 }
873
2b5f62a0
VZ
874 if (m_toolCapture)
875 {
876 if ( tool == m_toolCapture )
877 consumer->PerformAction( wxACTION_BUTTON_TOGGLE, m_toolCapture->GetId() );
878 else
879 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
880 }
32b13913 881
34d26f42 882 m_toolCapture = NULL;
32b13913 883
a290fa5a 884 return true;
34d26f42
RR
885 }
886 //else: don't do anything special about the double click
887 }
888
889 return wxStdInputHandler::HandleMouse(consumer, event);
4e89ceb1
VZ
890}
891
3216dbf5
VZ
892bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer *consumer,
893 const wxMouseEvent& event)
894{
34d26f42 895 if ( !wxStdInputHandler::HandleMouseMove(consumer, event) )
c08a4f00 896 {
34d26f42 897 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
32b13913 898
34d26f42 899 wxToolBarTool *tool;
3216dbf5 900 if ( event.Leaving() )
c08a4f00 901 {
34d26f42
RR
902 // We cannot possibly be over a tool when
903 // leaving the toolbar
3216dbf5 904 tool = NULL;
c08a4f00 905 }
3216dbf5 906 else
c08a4f00 907 {
34d26f42 908 tool = (wxToolBarTool*) tbar->FindToolForPosition( event.GetX(), event.GetY() );
c08a4f00 909 }
32b13913 910
72726d27 911 if (m_toolCapture)
34d26f42 912 {
72726d27
RR
913 // During capture we only care of the captured tool
914 if (tool && (tool != m_toolCapture))
915 tool = NULL;
32b13913 916
72726d27 917 if (tool == m_toolLast)
a290fa5a 918 return true;
32b13913 919
72726d27
RR
920 if (tool)
921 consumer->PerformAction( wxACTION_BUTTON_PRESS, m_toolCapture->GetId() );
922 else
923 consumer->PerformAction( wxACTION_BUTTON_RELEASE, m_toolCapture->GetId() );
32b13913 924
72726d27 925 m_toolLast = tool;
34d26f42 926 }
72726d27 927 else
34d26f42 928 {
72726d27 929 if (tool == m_toolLast)
a290fa5a 930 return true;
32b13913 931
72726d27
RR
932 if (m_toolLast)
933 {
934 // Leave old tool if any
935 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolLast->GetId() );
936 }
32b13913 937
72726d27
RR
938 if (tool)
939 {
940 // Enter new tool if any
941 consumer->PerformAction( wxACTION_TOOLBAR_ENTER, tool->GetId() );
942 }
32b13913 943
34d26f42 944 m_toolLast = tool;
34d26f42 945 }
32b13913 946
a290fa5a 947 return true;
bb312b54 948 }
3216dbf5 949
a290fa5a 950 return false;
3216dbf5
VZ
951}
952
953bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer *consumer,
61fef19b 954 const wxFocusEvent& WXUNUSED(event))
3216dbf5 955{
32b13913 956 if ( m_toolCapture )
34d26f42
RR
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;
3216dbf5
VZ
963}
964
965bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer *consumer,
966 bool activated)
967{
34d26f42
RR
968 if (m_toolCapture && !activated)
969 {
970 // We shouldn't be left with a highlighted button
971 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
972 }
3216dbf5 973
a290fa5a 974 return true;
c08a4f00
RR
975}
976
6a317e61 977#endif // wxUSE_TOOLBAR