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