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