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