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