]> git.saurik.com Git - wxWidgets.git/blob - src/univ/toolbar.cpp
Actually check if the menu should be shown before popping it up
[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 + 3, y + 3 );
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 // TODO ugly number fiddling
409 *pCur += ( tool->IsSeparator() ? m_widthSeparator : (widthTool+2) ) + margin;
410 }
411
412 // calculate the total toolbar size
413 wxCoord xMin = m_defaultWidth + 2*m_xMargin,
414 yMin = m_defaultHeight + 2*m_yMargin;
415
416 m_maxWidth = x < xMin ? xMin : x;
417 m_maxHeight = y < yMin ? yMin : y;
418 }
419
420 wxSize wxToolBar::DoGetBestClientSize() const
421 {
422 return wxSize(m_maxWidth, m_maxHeight);
423 }
424
425 // ----------------------------------------------------------------------------
426 // wxToolBar drawing
427 // ----------------------------------------------------------------------------
428
429 void wxToolBar::RefreshTool(wxToolBarToolBase *tool)
430 {
431 RefreshRect(GetToolRect(tool));
432 }
433
434 void wxToolBar::GetRectLimits(const wxRect& rect,
435 wxCoord *start,
436 wxCoord *end) const
437 {
438 wxCHECK_RET( start && end, _T("NULL pointer in GetRectLimits") );
439
440 if ( IsVertical() )
441 {
442 *start = rect.GetTop();
443 *end = rect.GetBottom();
444 }
445 else // horizontal
446 {
447 *start = rect.GetLeft();
448 *end = rect.GetRight();
449 }
450 }
451
452 void wxToolBar::DoDraw(wxControlRenderer *renderer)
453 {
454 // prepare the variables used below
455 wxDC& dc = renderer->GetDC();
456 wxRenderer *rend = renderer->GetRenderer();
457 // dc.SetFont(GetFont()); -- uncomment when we support labels
458
459 // draw the border separating us from the menubar (if there is no menubar
460 // we probably shouldn't draw it?)
461 if ( !IsVertical() )
462 {
463 rend->DrawHorizontalLine(dc, 0, 0, GetClientSize().x);
464 }
465
466 // get the update rect and its limits depending on the orientation
467 wxRect rectUpdate = GetUpdateClientRect();
468 wxCoord start, end;
469 GetRectLimits(rectUpdate, &start, &end);
470
471 // and redraw all the tools intersecting it
472 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
473 node;
474 node = node->GetNext() )
475 {
476 wxToolBarTool *tool = (wxToolBarTool*) node->GetData();
477 wxRect rectTool = GetToolRect(tool);
478 wxCoord startTool, endTool;
479 GetRectLimits(rectTool, &startTool, &endTool);
480
481 if ( endTool < start )
482 {
483 // we're still to the left of the area to redraw
484 continue;
485 }
486
487 if ( startTool > end )
488 {
489 // we're beyond the area to redraw, nothing left to do
490 break;
491 }
492
493 if (tool->IsSeparator() && !HasFlag(wxTB_FLAT))
494 {
495 // Draw seperators only in flat mode
496 continue;
497 }
498
499 // deal with the flags
500 int flags = 0;
501
502 if ( tool->IsEnabled() )
503 {
504 // The toolbars without wxTB_FLAT don't react to the mouse hovering
505 if ( !HasFlag(wxTB_FLAT) || tool->IsUnderMouse() )
506 flags |= wxCONTROL_CURRENT;
507 }
508 else // disabled tool
509 {
510 flags |= wxCONTROL_DISABLED;
511 }
512
513 //if ( tool == m_toolCaptured )
514 // flags |= wxCONTROL_FOCUSED;
515
516 if ( tool->IsPressed() )
517 flags = wxCONTROL_PRESSED;
518
519 wxString label;
520 wxBitmap bitmap;
521 if ( !tool->IsSeparator() )
522 {
523 // label = tool->GetLabel();
524 bitmap = tool->GetBitmap();
525 }
526 //else: leave both the label and the bitmap invalid to draw a separator
527
528 rend->DrawToolBarButton(dc, label, bitmap, rectTool, flags);
529 }
530 }
531
532 // ----------------------------------------------------------------------------
533 // wxToolBar actions
534 // ----------------------------------------------------------------------------
535
536 bool wxToolBar::PerformAction(const wxControlAction& action,
537 long numArg,
538 const wxString& strArg)
539 {
540 wxToolBarTool *tool = (wxToolBarTool*) FindById(numArg);
541
542 if ( action == wxACTION_TOOLBAR_TOGGLE )
543 {
544 PerformAction( wxACTION_BUTTON_RELEASE, numArg );
545
546 PerformAction( wxACTION_BUTTON_CLICK, numArg );
547 }
548 else if ( action == wxACTION_TOOLBAR_PRESS )
549 {
550 wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool->GetShortHelp().c_str());
551
552 tool->Invert();
553
554 RefreshTool( tool );
555 }
556 else if ( action == wxACTION_TOOLBAR_RELEASE )
557 {
558 wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool->GetShortHelp().c_str());
559
560 wxASSERT_MSG( tool->IsInverted(), _T("release unpressed button?") );
561
562 tool->Invert();
563
564 RefreshTool( tool );
565 }
566 else if ( action == wxACTION_TOOLBAR_CLICK )
567 {
568 bool isToggled;
569 if ( tool->CanBeToggled() )
570 {
571 tool->Toggle();
572
573 RefreshTool( tool );
574
575 isToggled = tool->IsToggled();
576 }
577 else // simple non-checkable tool
578 {
579 isToggled = FALSE;
580 }
581 OnLeftClick( tool->GetId(), isToggled );
582 }
583 else if ( action == wxACTION_TOOLBAR_ENTER )
584 {
585 wxCHECK_MSG( tool, FALSE, _T("no tool to enter?") );
586
587 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
588 {
589 tool->SetUnderMouse( TRUE );
590
591 if ( !tool->IsToggled() )
592 RefreshTool( tool );
593 }
594 }
595 else if ( action == wxACTION_TOOLBAR_LEAVE )
596 {
597 wxCHECK_MSG( tool, FALSE, _T("no tool to leave?") );
598
599 if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() )
600 {
601 tool->SetUnderMouse( FALSE );
602
603 if ( !tool->IsToggled() )
604 RefreshTool( tool );
605 }
606 }
607 else
608 return wxControl::PerformAction(action, numArg, strArg);
609
610 return TRUE;
611 }
612
613 // ============================================================================
614 // wxStdToolbarInputHandler implementation
615 // ============================================================================
616
617 wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler *handler)
618 : wxStdInputHandler(handler)
619 {
620 m_winCapture = NULL;
621 m_toolCapture = NULL;
622 m_toolLast = NULL;
623 }
624
625 bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer *consumer,
626 const wxKeyEvent& event,
627 bool pressed)
628 {
629 // TODO: when we have a current button we should allow the arrow
630 // keys to move it
631 return wxStdInputHandler::HandleKey(consumer, event, pressed);
632 }
633
634 bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer *consumer,
635 const wxMouseEvent& event)
636 {
637 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
638 wxToolBarToolBase *tool = tbar->FindToolForPosition(event.GetX(), event.GetY());
639
640 if ( event.Button(1) )
641 {
642
643 if ( event.LeftDown() || event.LeftDClick() )
644 {
645 if ( !tool || !tool->IsEnabled() )
646 return TRUE;
647
648 m_winCapture = tbar;
649 m_winCapture->CaptureMouse();
650
651 m_toolCapture = tool;
652
653 consumer->PerformAction( wxACTION_BUTTON_PRESS, tool->GetId() );
654
655 return TRUE;
656 }
657 else if ( event.LeftUp() )
658 {
659 if ( m_winCapture )
660 {
661 m_winCapture->ReleaseMouse();
662 m_winCapture = NULL;
663 }
664
665 if ( tool == m_toolCapture )
666 consumer->PerformAction( wxACTION_BUTTON_TOGGLE, m_toolCapture->GetId() );
667 else
668 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
669
670 m_toolCapture = NULL;
671
672 return TRUE;
673 }
674 //else: don't do anything special about the double click
675 }
676
677 return wxStdInputHandler::HandleMouse(consumer, event);
678 }
679
680 bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer *consumer,
681 const wxMouseEvent& event)
682 {
683 if ( !wxStdInputHandler::HandleMouseMove(consumer, event) )
684 {
685 wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar);
686
687 wxToolBarTool *tool;
688 if ( event.Leaving() )
689 {
690 // We cannot possibly be over a tool when
691 // leaving the toolbar
692 tool = NULL;
693 }
694 else
695 {
696 tool = (wxToolBarTool*) tbar->FindToolForPosition( event.GetX(), event.GetY() );
697 }
698
699 if (m_toolCapture)
700 {
701 // During capture we only care of the captured tool
702 if (tool && (tool != m_toolCapture))
703 tool = NULL;
704
705 if (tool == m_toolLast)
706 return TRUE;
707
708 if (tool)
709 consumer->PerformAction( wxACTION_BUTTON_PRESS, m_toolCapture->GetId() );
710 else
711 consumer->PerformAction( wxACTION_BUTTON_RELEASE, m_toolCapture->GetId() );
712
713 m_toolLast = tool;
714 }
715 else
716 {
717 if (tool == m_toolLast)
718 return TRUE;
719
720 if (m_toolLast)
721 {
722 // Leave old tool if any
723 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolLast->GetId() );
724 }
725
726 if (tool)
727 {
728 // Enter new tool if any
729 consumer->PerformAction( wxACTION_TOOLBAR_ENTER, tool->GetId() );
730 }
731
732 m_toolLast = tool;
733 }
734
735 return TRUE;
736 }
737
738 return FALSE;
739 }
740
741 bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer *consumer,
742 const wxFocusEvent& event)
743 {
744 if (m_toolCapture)
745 {
746 // We shouldn't be left with a highlighted button
747 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
748 }
749
750 return TRUE;
751 }
752
753 bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer *consumer,
754 bool activated)
755 {
756 if (m_toolCapture && !activated)
757 {
758 // We shouldn't be left with a highlighted button
759 consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() );
760 }
761
762 return TRUE;
763 }
764
765 #endif // wxUSE_TOOLBAR
766