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