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