Rewrote wxToolBar another time.
[wxWidgets.git] / src / univ / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/toolbar.cpp
3 // Purpose: implementation of wxToolBar for wxUniversal
4 // Author: Robert Roebling, Vadim Zeitlin (universalization)
5 // Modified by:
6 // Created: 20.02.02
7 // Id: $Id$
8 // Copyright: (c) 2001 Robert Roebling,
9 // (c) 2002 SciTech Software, Inc. (www.scitechsoft.com)
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // ============================================================================
14 // declarations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 #ifdef __GNUG__
22 #pragma implementation "univtoolbar.h"
23 #endif
24
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 #if wxUSE_TOOLBAR
33
34 #ifndef WX_PRECOMP
35 #include "wx/utils.h"
36 #include "wx/app.h"
37 #endif
38
39 #include "wx/univ/renderer.h"
40
41 #include "wx/toolbar.h"
42 #include "wx/image.h"
43
44 // ----------------------------------------------------------------------------
45 // constants
46 // ----------------------------------------------------------------------------
47
48 // value meaning that m_widthSeparator is not initialized
49 static const wxCoord INVALID_WIDTH = -1;
50
51 // ----------------------------------------------------------------------------
52 // wxToolBarTool: our implementation of wxToolBarToolBase
53 // ----------------------------------------------------------------------------
54
55 class WXDLLEXPORT wxToolBarTool : public wxToolBarToolBase
56 {
57 public:
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)
69 {
70 // no position yet
71 m_x =
72 m_y = -1;
73
74 // not pressed yet
75 m_isInverted = FALSE;
76
77 // mouse not here yet
78 m_underMouse = FALSE;
79 }
80
81 // is this tool pressed, even temporarily? (this is different from being
82 // permanently toggled which is what IsToggled() returns)
83 bool IsPressed() const
84 { return CanBeToggled() ? IsToggled() != m_isInverted : m_isInverted; }
85
86 // are we temporarily pressed/unpressed?
87 bool IsInverted() const { return m_isInverted; }
88
89 // press the tool temporarily by inverting its toggle state
90 void Invert() { m_isInverted = !m_isInverted; }
91
92 // Set underMouse
93 void SetUnderMouse( bool under = TRUE ) { m_underMouse = under; }
94 bool IsUnderMouse() { return m_underMouse; }
95
96 public:
97 // the tool position (the size is known by the toolbar itself)
98 int m_x,
99 m_y;
100
101 private:
102 // TRUE if the tool is pressed
103 bool m_isInverted;
104
105 // TRUE if the tool is under the mouse
106 bool m_underMouse;
107 };
108
109 // ============================================================================
110 // wxToolBar implementation
111 // ============================================================================
112
113 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl);
114
115 // ----------------------------------------------------------------------------
116 // wxToolBar creation
117 // ----------------------------------------------------------------------------
118
119 void wxToolBar::Init()
120 {
121 // no tools yet
122 m_needsLayout = FALSE;
123
124 // unknown widths for the tools and separators
125 m_widthSeparator = INVALID_WIDTH;
126
127 m_maxWidth =
128 m_maxHeight = 0;
129
130 wxRenderer *renderer = GetRenderer();
131
132 SetToolBitmapSize(renderer->GetToolBarButtonSize(&m_widthSeparator));
133 SetMargins(renderer->GetToolBarMargin());
134 }
135
136 bool wxToolBar::Create(wxWindow *parent,
137 wxWindowID id,
138 const wxPoint& pos,
139 const wxSize& size,
140 long style,
141 const wxString& name)
142 {
143 if ( !wxToolBarBase::Create(parent, id, pos, size, style,
144 wxDefaultValidator, name) )
145 {
146 return FALSE;
147 }
148
149 CreateInputHandler(wxINP_HANDLER_TOOLBAR);
150
151 SetBestSize(size);
152
153 return TRUE;
154 }
155
156 wxToolBar::~wxToolBar()
157 {
158 }
159
160 void wxToolBar::SetMargins(int x, int y)
161 {
162 // This required for similar visual effects under
163 // native platforms and wxUniv.
164 wxToolBarBase::SetMargins( x + 2, y + 2 );
165 }
166
167 // ----------------------------------------------------------------------------
168 // wxToolBar tool-related methods
169 // ----------------------------------------------------------------------------
170
171 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
172 {
173 // check the "other" direction first: it must be inside the toolbar or we
174 // don't risk finding anything
175 if ( IsVertical() )
176 {
177 if ( x < 0 || x > m_maxWidth )
178 return NULL;
179
180 // we always use x, even for a vertical toolbar, this makes the code
181 // below simpler
182 x = y;
183 }
184 else // horizontal
185 {
186 if ( y < 0 || y > m_maxHeight )
187 return NULL;
188 }
189
190 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
191 node;
192 node = node->GetNext() )
193 {
194 wxToolBarToolBase *tool = node->GetData();
195 wxRect rectTool = GetToolRect(tool);
196
197 wxCoord startTool, endTool;
198 GetRectLimits(rectTool, &startTool, &endTool);
199
200 if ( x >= startTool && x <= endTool )
201 {
202 // don't return the separators from here, they don't accept any
203 // input anyhow
204 return tool->IsSeparator() ? NULL : tool;
205 }
206 }
207
208 return NULL;
209 }
210
211 void wxToolBar::SetToolShortHelp(int id, const wxString& help)
212 {
213 wxToolBarToolBase *tool = FindById(id);
214
215 wxCHECK_RET( tool, _T("SetToolShortHelp: no such tool") );
216
217 tool->SetShortHelp(help);
218 }
219
220 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
221 wxToolBarToolBase * WXUNUSED(tool))
222 {
223 // recalculate the toolbar geometry before redrawing it the next time
224 m_needsLayout = TRUE;
225
226 // and ensure that we indeed are going to redraw
227 Refresh();
228
229 return TRUE;
230 }
231
232 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos),
233 wxToolBarToolBase * WXUNUSED(tool))
234 {
235 // as above
236 m_needsLayout = TRUE;
237
238 Refresh();
239
240 return TRUE;
241 }
242
243 void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
244 {
245 // created disabled-state bitmap on demand
246 if ( !enable && !tool->GetDisabledBitmap().Ok() )
247 {
248 wxImage image( tool->GetNormalBitmap().ConvertToImage() );
249
250 // TODO: don't hardcode 180
251 unsigned char bg_red = 180;
252 unsigned char bg_green = 180;
253 unsigned char bg_blue = 180;
254
255 unsigned char mask_red = image.GetMaskRed();
256 unsigned char mask_green = image.GetMaskGreen();
257 unsigned char mask_blue = image.GetMaskBlue();
258
259 bool has_mask = image.HasMask();
260
261 int x,y;
262 for (y = 0; y < image.GetHeight(); y++)
263 {
264 for (x = 0; x < image.GetWidth(); x++)
265 {
266 unsigned char red = image.GetRed(x,y);
267 unsigned char green = image.GetGreen(x,y);
268 unsigned char blue = image.GetBlue(x,y);
269 if (!has_mask || red != mask_red || green != mask_green || blue != mask_blue)
270 {
271 red = (((wxInt32) red - bg_red) >> 1) + bg_red;
272 green = (((wxInt32) green - bg_green) >> 1) + bg_green;
273 blue = (((wxInt32) blue - bg_blue) >> 1) + bg_blue;
274 image.SetRGB( x, y, red, green, blue );
275 }
276 }
277 }
278
279 for (y = 0; y < image.GetHeight(); y++)
280 {
281 for (x = y % 2; x < image.GetWidth(); x += 2)
282 {
283 unsigned char red = image.GetRed(x,y);
284 unsigned char green = image.GetGreen(x,y);
285 unsigned char blue = image.GetBlue(x,y);
286 if (!has_mask || red != mask_red || green != mask_green || blue != mask_blue)
287 {
288 red = (((wxInt32) red - bg_red) >> 1) + bg_red;
289 green = (((wxInt32) green - bg_green) >> 1) + bg_green;
290 blue = (((wxInt32) blue - bg_blue) >> 1) + bg_blue;
291 image.SetRGB( x, y, red, green, blue );
292 }
293 }
294 }
295
296 tool->SetDisabledBitmap(image);
297 }
298
299 RefreshTool(tool);
300 }
301
302 void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool WXUNUSED(toggle))
303 {
304 // note that if we're called the tool did change state (the base class
305 // checks for it), so it's not necessary to check for this again here
306 RefreshTool(tool);
307 }
308
309 void wxToolBar::DoSetToggle(wxToolBarToolBase *tool, bool WXUNUSED(toggle))
310 {
311 RefreshTool(tool);
312 }
313
314 wxToolBarToolBase *wxToolBar::CreateTool(int id,
315 const wxString& label,
316 const wxBitmap& bmpNormal,
317 const wxBitmap& bmpDisabled,
318 wxItemKind kind,
319 wxObject *clientData,
320 const wxString& shortHelp,
321 const wxString& longHelp)
322 {
323 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
324 clientData, shortHelp, longHelp);
325 }
326
327 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
328 {
329 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet (TODO)") );
330
331 return NULL;
332 }
333
334 // ----------------------------------------------------------------------------
335 // wxToolBar geometry
336 // ----------------------------------------------------------------------------
337
338 wxRect wxToolBar::GetToolRect(wxToolBarToolBase *toolBase) const
339 {
340 const wxToolBarTool *tool = (wxToolBarTool *)toolBase;
341
342 wxRect rect;
343
344 wxCHECK_MSG( tool, rect, _T("GetToolRect: NULL tool") );
345
346 // ensure that we always have the valid tool position
347 if ( m_needsLayout )
348 {
349 wxConstCast(this, wxToolBar)->DoLayout();
350 }
351
352 rect.x = tool->m_x - m_xMargin;
353 rect.y = tool->m_y - m_yMargin;
354
355 if ( IsVertical() )
356 {
357 rect.width = m_defaultWidth;
358 rect.height = tool->IsSeparator() ? m_widthSeparator : m_defaultHeight;
359 }
360 else // horizontal
361 {
362 rect.width = tool->IsSeparator() ? m_widthSeparator : m_defaultWidth;
363 rect.height = m_defaultHeight;
364 }
365
366 rect.width += 2*m_xMargin;
367 rect.height += 2*m_yMargin;
368
369 return rect;
370 }
371
372 bool wxToolBar::Realize()
373 {
374 if ( !wxToolBarBase::Realize() )
375 return FALSE;
376
377 m_needsLayout = TRUE;
378 DoLayout();
379
380 SetBestSize(wxDefaultSize);
381
382 return TRUE;
383 }
384
385 void wxToolBar::DoLayout()
386 {
387 wxASSERT_MSG( m_needsLayout, _T("why are we called?") );
388
389 m_needsLayout = FALSE;
390
391 wxCoord x = m_xMargin,
392 y = m_yMargin;
393
394 const wxCoord widthTool = IsVertical() ? m_defaultHeight : m_defaultWidth;
395 wxCoord margin = IsVertical() ? m_xMargin : m_yMargin,
396 *pCur = IsVertical() ? &y : &x;
397
398 // calculate the positions of all elements
399 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
400 node;
401 node = node->GetNext() )
402 {
403 wxToolBarTool *tool = (wxToolBarTool *) node->GetData();
404
405 tool->m_x = x;
406 tool->m_y = y;
407
408 *pCur += (tool->IsSeparator() ? m_widthSeparator : widthTool) + margin;
409 }
410
411 // calculate the total toolbar size
412 wxCoord xMin = m_defaultWidth + 2*m_xMargin,
413 yMin = m_defaultHeight + 2*m_yMargin;
414
415 m_maxWidth = x < xMin ? xMin : x;
416 m_maxHeight = y < yMin ? yMin : y;
417 }
418
419 wxSize wxToolBar::DoGetBestClientSize() const
420 {
421 return wxSize(m_maxWidth, m_maxHeight);
422 }
423
424 // ----------------------------------------------------------------------------
425 // wxToolBar drawing
426 // ----------------------------------------------------------------------------
427
428 void wxToolBar::RefreshTool(wxToolBarToolBase *tool)
429 {
430 RefreshRect(GetToolRect(tool));
431 }
432
433 void wxToolBar::GetRectLimits(const wxRect& rect,
434 wxCoord *start,
435 wxCoord *end) const
436 {
437 wxCHECK_RET( start && end, _T("NULL pointer in GetRectLimits") );
438
439 if ( IsVertical() )
440 {
441 *start = rect.GetTop();
442 *end = rect.GetBottom();
443 }
444 else // horizontal
445 {
446 *start = rect.GetLeft();
447 *end = rect.GetRight();
448 }
449 }
450
451 void wxToolBar::DoDraw(wxControlRenderer *renderer)
452 {
453 // prepare the variables used below
454 wxDC& dc = renderer->GetDC();
455 wxRenderer *rend = renderer->GetRenderer();
456 // dc.SetFont(GetFont()); -- uncomment when we support labels
457
458 // draw the border separating us from the menubar (if there is no menubar
459 // we probably shouldn't draw it?)
460 if ( !IsVertical() )
461 {
462 rend->DrawHorizontalLine(dc, 0, 0, GetClientSize().x);
463 }
464
465 // get the update rect and its limits depending on the orientation
466 wxRect rectUpdate = GetUpdateClientRect();
467 wxCoord start, end;
468 GetRectLimits(rectUpdate, &start, &end);
469
470 // and redraw all the tools intersecting it
471 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
472 node;
473 node = node->GetNext() )
474 {
475 wxToolBarTool *tool = (wxToolBarTool*) node->GetData();
476 wxRect rectTool = GetToolRect(tool);
477 wxCoord startTool, endTool;
478 GetRectLimits(rectTool, &startTool, &endTool);
479
480 if ( endTool < start )
481 {
482 // we're still to the left of the area to redraw
483 continue;
484 }
485
486 if ( startTool > end )
487 {
488 // we're beyond the area to redraw, nothing left to do
489 break;
490 }
491
492 // deal with the flags
493 int flags = 0;
494
495 if ( tool->IsEnabled() )
496 {
497 // The toolbars without wxTB_FLAT don't react to the mouse hovering
498 if ( !HasFlag(wxTB_FLAT) || tool->IsUnderMouse() )
499 flags |= wxCONTROL_CURRENT;
500 }
501 else // disabled tool
502 {
503 flags |= wxCONTROL_DISABLED;
504 }
505
506 //if ( tool == m_toolCaptured )
507 // flags |= wxCONTROL_FOCUSED;
508
509 if ( tool->IsPressed() )
510 flags = wxCONTROL_PRESSED;
511
512 wxString label;
513 wxBitmap bitmap;
514 if ( !tool->IsSeparator() )
515 {
516 // label = tool->GetLabel();
517 bitmap = tool->GetBitmap();
518 }
519 //else: leave both the label and the bitmap invalid to draw a separator
520
521 rend->DrawToolBarButton(dc, label, bitmap, rectTool, flags);
522 }
523 }
524
525 // ----------------------------------------------------------------------------
526 // wxToolBar actions
527 // ----------------------------------------------------------------------------
528
529 bool wxToolBar::PerformAction(const wxControlAction& action,
530 long numArg,
531 const wxString& strArg)
532 {
533 wxToolBarTool *tool = (wxToolBarTool*) FindById(numArg);
534
535 if ( action == wxACTION_TOOLBAR_TOGGLE )
536 {
537 PerformAction( wxACTION_BUTTON_RELEASE, numArg );
538
539 PerformAction( wxACTION_BUTTON_CLICK, numArg );
540 }
541 else if ( action == wxACTION_TOOLBAR_PRESS )
542 {
543 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool->GetShortHelp().c_str());
544
545 tool->Invert();
546
547 RefreshTool( tool );
548 }
549 else if ( action == wxACTION_TOOLBAR_RELEASE )
550 {
551 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool->GetShortHelp().c_str());
552
553 wxASSERT_MSG( tool->IsInverted(), _T("release unpressed button?") );
554
555 tool->Invert();
556
557 RefreshTool( tool );
558 }
559 else if ( action == wxACTION_TOOLBAR_CLICK )
560 {
561 bool isToggled;
562 if ( tool->CanBeToggled() )
563 {
564 tool->Toggle();
565
566 RefreshTool( tool );
567
568 isToggled = tool->IsToggled();
569 }
570 else // simple non-checkable tool
571 {
572 isToggled = FALSE;
573 }
574 OnLeftClick( tool->GetId(), isToggled );
575 }
576 else if ( action == wxACTION_TOOLBAR_ENTER )
577 {
578 wxCHECK_MSG( tool, FALSE, _T("no tool to enter?") );
579
580 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
581 {
582 tool->SetUnderMouse( TRUE );
583
584 if ( !tool->IsToggled() )
585 RefreshTool( tool );
586 }
587 }
588 else if ( action == wxACTION_TOOLBAR_LEAVE )
589 {
590 wxCHECK_MSG( tool, FALSE, _T("no tool to leave?") );
591
592 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
593 {
594 tool->SetUnderMouse( FALSE );
595
596 if ( !tool->IsToggled() )
597 RefreshTool( tool );
598 }
599 }
600 else
601 return wxControl::PerformAction(action, numArg, strArg);
602
603 return TRUE;
604 }
605
606 // ============================================================================
607 // wxStdToolbarInputHandler implementation
608 // ============================================================================
609
610 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler *handler)
611 : wxStdInputHandler(handler)
612 {
613 m_winCapture = NULL;
614 m_toolCapture = NULL;
615 m_toolLast = NULL;
616 }
617
618 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer *consumer,
619 const wxKeyEvent& event,
620 bool pressed)
621 {
622 // TODO: when we have a current button we should allow the arrow
623 // keys to move it
624 return wxStdInputHandler::HandleKey(consumer, event, pressed);
625 }
626
627 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer *consumer,
628 const wxMouseEvent& event)
629 {
630 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
631 wxToolBarToolBase *tool = tbar->FindToolForPosition(event.GetX(), event.GetY());
632
633 if ( event.Button(1) )
634 {
635 if ( !tool || !tool->IsEnabled() )
636 return TRUE;
637
638 if ( event.LeftDown() || event.LeftDClick() )
639 {
640 m_winCapture = tbar;
641 m_winCapture->CaptureMouse();
642
643 m_toolCapture = tool;
644
645 consumer->PerformAction( wxACTION_BUTTON_PRESS, tool->GetId() );
646
647 return TRUE;
648 }
649 else if ( event.LeftUp() )
650 {
651 if ( m_winCapture )
652 {
653 m_winCapture->ReleaseMouse();
654 m_winCapture = NULL;
655 }
656
657 if ( tool == m_toolCapture )
658 {
659 // this will generate a click event
660 consumer->PerformAction( wxACTION_BUTTON_TOGGLE, tool->GetId() );
661
662 m_toolCapture = NULL;
663
664 return TRUE;
665 }
666 //else: the mouse was released outside the tool or in
667 // a different tool
668
669 m_toolCapture = NULL;
670
671 }
672 //else: don't do anything special about the double click
673 }
674
675 return wxStdInputHandler::HandleMouse(consumer, event);
676 }
677
678 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer *consumer,
679 const wxMouseEvent& event)
680 {
681 if ( !wxStdInputHandler::HandleMouseMove(consumer, event) )
682 {
683 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
684
685 wxToolBarTool *tool;
686 if ( event.Leaving() )
687 {
688 // We cannot possibly be over a tool when
689 // leaving the toolbar
690 tool = NULL;
691 }
692 else
693 {
694 tool = (wxToolBarTool*) tbar->FindToolForPosition( event.GetX(), event.GetY() );
695 }
696
697 if ((tool) && (tool == m_toolLast))
698 {
699 // Still over the same tool as last time
700 return TRUE;
701 }
702
703 if (m_toolLast)
704 {
705 // Leave old tool if any
706 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolLast->GetId() );
707 }
708
709 if (m_toolCapture && (m_toolCapture != tool))
710 m_toolLast = NULL;
711 else
712 m_toolLast = tool;
713
714 if (m_toolLast)
715 {
716 // Enter new tool if any
717 consumer->PerformAction( wxACTION_TOOLBAR_ENTER, m_toolLast->GetId() );
718 }
719
720 return TRUE;
721 }
722
723 return FALSE;
724 }
725
726 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer *consumer,
727 const wxFocusEvent& event)
728 {
729 if (m_toolCapture)
730 {
731 // We shouldn't be left with a highlighted button
732 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
733 }
734
735 return TRUE;
736 }
737
738 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer *consumer,
739 bool activated)
740 {
741 if (m_toolCapture && !activated)
742 {
743 // We shouldn't be left with a highlighted button
744 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
745 }
746
747 return TRUE;
748 }
749
750 #endif // wxUSE_TOOLBAR
751