]>
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 | else if ( action == wxACTION_TOOLBAR_PRESS ) | |
654 | { | |
655 | wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool->GetShortHelp().c_str()); | |
656 | ||
657 | tool->Invert(); | |
658 | ||
659 | RefreshTool( tool ); | |
660 | } | |
661 | else if ( action == wxACTION_TOOLBAR_RELEASE ) | |
662 | { | |
663 | wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool->GetShortHelp().c_str()); | |
664 | ||
665 | wxASSERT_MSG( tool->IsInverted(), _T("release unpressed button?") ); | |
666 | ||
667 | tool->Invert(); | |
668 | ||
669 | RefreshTool( tool ); | |
670 | } | |
671 | else if ( action == wxACTION_TOOLBAR_CLICK ) | |
672 | { | |
673 | bool isToggled; | |
674 | if ( tool->CanBeToggled() ) | |
675 | { | |
676 | tool->Toggle(); | |
677 | ||
678 | RefreshTool( tool ); | |
679 | ||
680 | isToggled = tool->IsToggled(); | |
681 | } | |
682 | else // simple non-checkable tool | |
683 | { | |
684 | isToggled = false; | |
685 | } | |
686 | OnLeftClick( tool->GetId(), isToggled ); | |
687 | } | |
688 | else if ( action == wxACTION_TOOLBAR_ENTER ) | |
689 | { | |
690 | wxCHECK_MSG( tool, false, _T("no tool to enter?") ); | |
691 | ||
692 | if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() ) | |
693 | { | |
694 | tool->SetUnderMouse( true ); | |
695 | ||
696 | if ( !tool->IsToggled() ) | |
697 | RefreshTool( tool ); | |
698 | } | |
699 | } | |
700 | else if ( action == wxACTION_TOOLBAR_LEAVE ) | |
701 | { | |
702 | wxCHECK_MSG( tool, false, _T("no tool to leave?") ); | |
703 | ||
704 | if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() ) | |
705 | { | |
706 | tool->SetUnderMouse( false ); | |
707 | ||
708 | if ( !tool->IsToggled() ) | |
709 | RefreshTool( tool ); | |
710 | } | |
711 | } | |
712 | else | |
713 | return wxControl::PerformAction(action, numArg, strArg); | |
714 | ||
715 | return true; | |
716 | } | |
717 | ||
718 | // ============================================================================ | |
719 | // wxStdToolbarInputHandler implementation | |
720 | // ============================================================================ | |
721 | ||
722 | wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler *handler) | |
723 | : wxStdInputHandler(handler) | |
724 | { | |
725 | m_winCapture = NULL; | |
726 | m_toolCapture = NULL; | |
727 | m_toolLast = NULL; | |
728 | } | |
729 | ||
730 | bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer *consumer, | |
731 | const wxKeyEvent& event, | |
732 | bool pressed) | |
733 | { | |
734 | // TODO: when we have a current button we should allow the arrow | |
735 | // keys to move it | |
736 | return wxStdInputHandler::HandleKey(consumer, event, pressed); | |
737 | } | |
738 | ||
739 | bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer *consumer, | |
740 | const wxMouseEvent& event) | |
741 | { | |
742 | wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar); | |
743 | wxToolBarToolBase *tool = tbar->FindToolForPosition(event.GetX(), event.GetY()); | |
744 | ||
745 | if ( event.Button(1) ) | |
746 | { | |
747 | ||
748 | if ( event.LeftDown() || event.LeftDClick() ) | |
749 | { | |
750 | if ( !tool || !tool->IsEnabled() ) | |
751 | return true; | |
752 | ||
753 | m_winCapture = tbar; | |
754 | m_winCapture->CaptureMouse(); | |
755 | ||
756 | m_toolCapture = tool; | |
757 | ||
758 | consumer->PerformAction( wxACTION_BUTTON_PRESS, tool->GetId() ); | |
759 | ||
760 | return true; | |
761 | } | |
762 | else if ( event.LeftUp() ) | |
763 | { | |
764 | if ( m_winCapture ) | |
765 | { | |
766 | m_winCapture->ReleaseMouse(); | |
767 | m_winCapture = NULL; | |
768 | } | |
769 | ||
770 | if (m_toolCapture) | |
771 | { | |
772 | if ( tool == m_toolCapture ) | |
773 | consumer->PerformAction( wxACTION_BUTTON_TOGGLE, m_toolCapture->GetId() ); | |
774 | else | |
775 | consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() ); | |
776 | } | |
777 | ||
778 | m_toolCapture = NULL; | |
779 | ||
780 | return true; | |
781 | } | |
782 | //else: don't do anything special about the double click | |
783 | } | |
784 | ||
785 | return wxStdInputHandler::HandleMouse(consumer, event); | |
786 | } | |
787 | ||
788 | bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer *consumer, | |
789 | const wxMouseEvent& event) | |
790 | { | |
791 | if ( !wxStdInputHandler::HandleMouseMove(consumer, event) ) | |
792 | { | |
793 | wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar); | |
794 | ||
795 | wxToolBarTool *tool; | |
796 | if ( event.Leaving() ) | |
797 | { | |
798 | // We cannot possibly be over a tool when | |
799 | // leaving the toolbar | |
800 | tool = NULL; | |
801 | } | |
802 | else | |
803 | { | |
804 | tool = (wxToolBarTool*) tbar->FindToolForPosition( event.GetX(), event.GetY() ); | |
805 | } | |
806 | ||
807 | if (m_toolCapture) | |
808 | { | |
809 | // During capture we only care of the captured tool | |
810 | if (tool && (tool != m_toolCapture)) | |
811 | tool = NULL; | |
812 | ||
813 | if (tool == m_toolLast) | |
814 | return true; | |
815 | ||
816 | if (tool) | |
817 | consumer->PerformAction( wxACTION_BUTTON_PRESS, m_toolCapture->GetId() ); | |
818 | else | |
819 | consumer->PerformAction( wxACTION_BUTTON_RELEASE, m_toolCapture->GetId() ); | |
820 | ||
821 | m_toolLast = tool; | |
822 | } | |
823 | else | |
824 | { | |
825 | if (tool == m_toolLast) | |
826 | return true; | |
827 | ||
828 | if (m_toolLast) | |
829 | { | |
830 | // Leave old tool if any | |
831 | consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolLast->GetId() ); | |
832 | } | |
833 | ||
834 | if (tool) | |
835 | { | |
836 | // Enter new tool if any | |
837 | consumer->PerformAction( wxACTION_TOOLBAR_ENTER, tool->GetId() ); | |
838 | } | |
839 | ||
840 | m_toolLast = tool; | |
841 | } | |
842 | ||
843 | return true; | |
844 | } | |
845 | ||
846 | return false; | |
847 | } | |
848 | ||
849 | bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer *consumer, | |
850 | const wxFocusEvent& WXUNUSED(event)) | |
851 | { | |
852 | if ( m_toolCapture ) | |
853 | { | |
854 | // We shouldn't be left with a highlighted button | |
855 | consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() ); | |
856 | } | |
857 | ||
858 | return true; | |
859 | } | |
860 | ||
861 | bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer *consumer, | |
862 | bool activated) | |
863 | { | |
864 | if (m_toolCapture && !activated) | |
865 | { | |
866 | // We shouldn't be left with a highlighted button | |
867 | consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() ); | |
868 | } | |
869 | ||
870 | return true; | |
871 | } | |
872 | ||
873 | #endif // wxUSE_TOOLBAR | |
874 |