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