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