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