]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/toolbar.cpp | |
3 | // Purpose: wxToolBar | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // Copyright: (c) Julian Smart | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // For compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && !defined(__SMARTPHONE__) | |
27 | ||
28 | #include "wx/toolbar.h" | |
29 | ||
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" | |
32 | #include "wx/dynarray.h" | |
33 | #include "wx/frame.h" | |
34 | #include "wx/log.h" | |
35 | #include "wx/intl.h" | |
36 | #include "wx/settings.h" | |
37 | #include "wx/bitmap.h" | |
38 | #include "wx/region.h" | |
39 | #include "wx/dcmemory.h" | |
40 | #include "wx/control.h" | |
41 | #include "wx/app.h" // for GetComCtl32Version | |
42 | #include "wx/image.h" | |
43 | #include "wx/stattext.h" | |
44 | #endif | |
45 | ||
46 | #include "wx/artprov.h" | |
47 | #include "wx/sysopt.h" | |
48 | #include "wx/dcclient.h" | |
49 | #include "wx/scopedarray.h" | |
50 | ||
51 | #include "wx/msw/private.h" | |
52 | #include "wx/msw/dc.h" | |
53 | ||
54 | #if wxUSE_UXTHEME | |
55 | #include "wx/msw/uxtheme.h" | |
56 | #endif | |
57 | ||
58 | // this define controls whether the code for button colours remapping (only | |
59 | // useful for 16 or 256 colour images) is active at all, it's always turned off | |
60 | // for CE where it doesn't compile (and is probably not needed anyhow) and may | |
61 | // also be turned off for other systems if you always use 24bpp images and so | |
62 | // never need it | |
63 | #ifndef __WXWINCE__ | |
64 | #define wxREMAP_BUTTON_COLOURS | |
65 | #endif // !__WXWINCE__ | |
66 | ||
67 | // ---------------------------------------------------------------------------- | |
68 | // constants | |
69 | // ---------------------------------------------------------------------------- | |
70 | ||
71 | // these standard constants are not always defined in compilers headers | |
72 | ||
73 | // Styles | |
74 | #ifndef TBSTYLE_FLAT | |
75 | #define TBSTYLE_LIST 0x1000 | |
76 | #define TBSTYLE_FLAT 0x0800 | |
77 | #endif | |
78 | ||
79 | #ifndef TBSTYLE_TRANSPARENT | |
80 | #define TBSTYLE_TRANSPARENT 0x8000 | |
81 | #endif | |
82 | ||
83 | #ifndef TBSTYLE_TOOLTIPS | |
84 | #define TBSTYLE_TOOLTIPS 0x0100 | |
85 | #endif | |
86 | ||
87 | // Messages | |
88 | #ifndef TB_GETSTYLE | |
89 | #define TB_SETSTYLE (WM_USER + 56) | |
90 | #define TB_GETSTYLE (WM_USER + 57) | |
91 | #endif | |
92 | ||
93 | #ifndef TB_HITTEST | |
94 | #define TB_HITTEST (WM_USER + 69) | |
95 | #endif | |
96 | ||
97 | #ifndef TB_GETMAXSIZE | |
98 | #define TB_GETMAXSIZE (WM_USER + 83) | |
99 | #endif | |
100 | ||
101 | // ---------------------------------------------------------------------------- | |
102 | // wxWin macros | |
103 | // ---------------------------------------------------------------------------- | |
104 | ||
105 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl) | |
106 | ||
107 | /* | |
108 | TOOLBAR PROPERTIES | |
109 | tool | |
110 | bitmap | |
111 | bitmap2 | |
112 | tooltip | |
113 | longhelp | |
114 | radio (bool) | |
115 | toggle (bool) | |
116 | separator | |
117 | style ( wxNO_BORDER | wxTB_HORIZONTAL) | |
118 | bitmapsize | |
119 | margins | |
120 | packing | |
121 | separation | |
122 | ||
123 | dontattachtoframe | |
124 | */ | |
125 | ||
126 | BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase) | |
127 | EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent) | |
128 | EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged) | |
129 | EVT_ERASE_BACKGROUND(wxToolBar::OnEraseBackground) | |
130 | END_EVENT_TABLE() | |
131 | ||
132 | // ---------------------------------------------------------------------------- | |
133 | // private classes | |
134 | // ---------------------------------------------------------------------------- | |
135 | ||
136 | class wxToolBarTool : public wxToolBarToolBase | |
137 | { | |
138 | public: | |
139 | wxToolBarTool(wxToolBar *tbar, | |
140 | int id, | |
141 | const wxString& label, | |
142 | const wxBitmap& bmpNormal, | |
143 | const wxBitmap& bmpDisabled, | |
144 | wxItemKind kind, | |
145 | wxObject *clientData, | |
146 | const wxString& shortHelp, | |
147 | const wxString& longHelp) | |
148 | : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind, | |
149 | clientData, shortHelp, longHelp) | |
150 | { | |
151 | m_nSepCount = 0; | |
152 | m_staticText = NULL; | |
153 | } | |
154 | ||
155 | wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label) | |
156 | : wxToolBarToolBase(tbar, control, label) | |
157 | { | |
158 | if ( IsControl() && !m_label.empty() ) | |
159 | { | |
160 | // create a control to render the control's label | |
161 | m_staticText = new wxStaticText | |
162 | ( | |
163 | m_tbar, | |
164 | wxID_ANY, | |
165 | m_label, | |
166 | wxDefaultPosition, | |
167 | wxDefaultSize, | |
168 | wxALIGN_CENTRE | wxST_NO_AUTORESIZE | |
169 | ); | |
170 | } | |
171 | else // no label | |
172 | { | |
173 | m_staticText = NULL; | |
174 | } | |
175 | ||
176 | m_nSepCount = 1; | |
177 | } | |
178 | ||
179 | virtual ~wxToolBarTool() | |
180 | { | |
181 | delete m_staticText; | |
182 | } | |
183 | ||
184 | virtual void SetLabel(const wxString& label) | |
185 | { | |
186 | if ( label == m_label ) | |
187 | return; | |
188 | ||
189 | wxToolBarToolBase::SetLabel(label); | |
190 | ||
191 | if ( m_staticText ) | |
192 | m_staticText->SetLabel(label); | |
193 | ||
194 | // we need to update the label shown in the toolbar because it has a | |
195 | // pointer to the internal buffer of the old label | |
196 | // | |
197 | // TODO: use TB_SETBUTTONINFO | |
198 | } | |
199 | ||
200 | wxStaticText* GetStaticText() | |
201 | { | |
202 | wxASSERT_MSG( IsControl(), | |
203 | wxT("only makes sense for embedded control tools") ); | |
204 | ||
205 | return m_staticText; | |
206 | } | |
207 | ||
208 | // set/get the number of separators which we use to cover the space used by | |
209 | // a control in the toolbar | |
210 | void SetSeparatorsCount(size_t count) { m_nSepCount = count; } | |
211 | size_t GetSeparatorsCount() const { return m_nSepCount; } | |
212 | ||
213 | // we need ids for the spacers which we want to modify later on, this | |
214 | // function will allocate a valid/unique id for a spacer if not done yet | |
215 | void AllocSpacerId() | |
216 | { | |
217 | if ( m_id == wxID_SEPARATOR ) | |
218 | m_id = wxWindow::NewControlId(); | |
219 | } | |
220 | ||
221 | // this method is used for controls only and offsets the control by the | |
222 | // given amount (in pixels) in horizontal direction | |
223 | void MoveBy(int offset) | |
224 | { | |
225 | wxControl * const control = GetControl(); | |
226 | ||
227 | control->Move(control->GetPosition().x + offset, wxDefaultCoord); | |
228 | ||
229 | if ( m_staticText ) | |
230 | { | |
231 | m_staticText->Move(m_staticText->GetPosition().x + offset, | |
232 | wxDefaultCoord); | |
233 | } | |
234 | } | |
235 | ||
236 | private: | |
237 | size_t m_nSepCount; | |
238 | wxStaticText *m_staticText; | |
239 | ||
240 | wxDECLARE_NO_COPY_CLASS(wxToolBarTool); | |
241 | }; | |
242 | ||
243 | // ---------------------------------------------------------------------------- | |
244 | // helper functions | |
245 | // ---------------------------------------------------------------------------- | |
246 | ||
247 | // return the rectangle of the item at the given index | |
248 | // | |
249 | // returns an empty (0, 0, 0, 0) rectangle if fails so the caller may compare | |
250 | // r.right or r.bottom with 0 to check for this | |
251 | static RECT wxGetTBItemRect(HWND hwnd, int index) | |
252 | { | |
253 | RECT r; | |
254 | ||
255 | // note that we use TB_GETITEMRECT and not TB_GETRECT because the latter | |
256 | // only appeared in v4.70 of comctl32.dll | |
257 | if ( !::SendMessage(hwnd, TB_GETITEMRECT, index, (LPARAM)&r) ) | |
258 | { | |
259 | wxLogLastError(wxT("TB_GETITEMRECT")); | |
260 | ||
261 | r.top = | |
262 | r.left = | |
263 | r.right = | |
264 | r.bottom = 0; | |
265 | } | |
266 | ||
267 | return r; | |
268 | } | |
269 | ||
270 | // ============================================================================ | |
271 | // implementation | |
272 | // ============================================================================ | |
273 | ||
274 | // ---------------------------------------------------------------------------- | |
275 | // wxToolBarTool | |
276 | // ---------------------------------------------------------------------------- | |
277 | ||
278 | wxToolBarToolBase *wxToolBar::CreateTool(int id, | |
279 | const wxString& label, | |
280 | const wxBitmap& bmpNormal, | |
281 | const wxBitmap& bmpDisabled, | |
282 | wxItemKind kind, | |
283 | wxObject *clientData, | |
284 | const wxString& shortHelp, | |
285 | const wxString& longHelp) | |
286 | { | |
287 | return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind, | |
288 | clientData, shortHelp, longHelp); | |
289 | } | |
290 | ||
291 | wxToolBarToolBase * | |
292 | wxToolBar::CreateTool(wxControl *control, const wxString& label) | |
293 | { | |
294 | return new wxToolBarTool(this, control, label); | |
295 | } | |
296 | ||
297 | // ---------------------------------------------------------------------------- | |
298 | // wxToolBar construction | |
299 | // ---------------------------------------------------------------------------- | |
300 | ||
301 | void wxToolBar::Init() | |
302 | { | |
303 | m_hBitmap = 0; | |
304 | m_disabledImgList = NULL; | |
305 | ||
306 | m_nButtons = 0; | |
307 | m_totalFixedSize = 0; | |
308 | ||
309 | // even though modern Windows applications typically use 24*24 (or even | |
310 | // 32*32) size for their bitmaps, the native control itself still uses the | |
311 | // old 16*15 default size (see TB_SETBITMAPSIZE documentation in MSDN), so | |
312 | // default to it so that we don't call SetToolBitmapSize() unnecessarily in | |
313 | // wxToolBarBase::AdjustToolBitmapSize() | |
314 | m_defaultWidth = 16; | |
315 | m_defaultHeight = 15; | |
316 | ||
317 | m_pInTool = NULL; | |
318 | } | |
319 | ||
320 | bool wxToolBar::Create(wxWindow *parent, | |
321 | wxWindowID id, | |
322 | const wxPoint& pos, | |
323 | const wxSize& size, | |
324 | long style, | |
325 | const wxString& name) | |
326 | { | |
327 | // common initialisation | |
328 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) | |
329 | return false; | |
330 | ||
331 | FixupStyle(); | |
332 | ||
333 | // MSW-specific initialisation | |
334 | if ( !MSWCreateToolbar(pos, size) ) | |
335 | return false; | |
336 | ||
337 | wxSetCCUnicodeFormat(GetHwnd()); | |
338 | ||
339 | // we always erase our background on WM_PAINT so there is no need to do it | |
340 | // in WM_ERASEBKGND too (by default this won't be done but if the toolbar | |
341 | // has a non default background colour, then it would be used in both | |
342 | // places resulting in flicker) | |
343 | if (wxApp::GetComCtl32Version() >= 600) | |
344 | { | |
345 | SetBackgroundStyle(wxBG_STYLE_PAINT); | |
346 | } | |
347 | ||
348 | return true; | |
349 | } | |
350 | ||
351 | bool wxToolBar::MSWCreateToolbar(const wxPoint& pos, const wxSize& size) | |
352 | { | |
353 | if ( !MSWCreateControl(TOOLBARCLASSNAME, wxEmptyString, pos, size) ) | |
354 | return false; | |
355 | ||
356 | // toolbar-specific post initialisation | |
357 | ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0); | |
358 | ||
359 | #ifdef TB_SETEXTENDEDSTYLE | |
360 | if ( wxApp::GetComCtl32Version() >= 471 ) | |
361 | ::SendMessage(GetHwnd(), TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_DRAWDDARROWS); | |
362 | #endif | |
363 | ||
364 | return true; | |
365 | } | |
366 | ||
367 | void wxToolBar::Recreate() | |
368 | { | |
369 | const HWND hwndOld = GetHwnd(); | |
370 | if ( !hwndOld ) | |
371 | { | |
372 | // we haven't been created yet, no need to recreate | |
373 | return; | |
374 | } | |
375 | ||
376 | // get the position and size before unsubclassing the old toolbar | |
377 | const wxPoint pos = GetPosition(); | |
378 | const wxSize size = GetSize(); | |
379 | ||
380 | UnsubclassWin(); | |
381 | ||
382 | if ( !MSWCreateToolbar(pos, size) ) | |
383 | { | |
384 | // what can we do? | |
385 | wxFAIL_MSG( wxT("recreating the toolbar failed") ); | |
386 | ||
387 | return; | |
388 | } | |
389 | ||
390 | // reparent all our children under the new toolbar | |
391 | for ( wxWindowList::compatibility_iterator node = m_children.GetFirst(); | |
392 | node; | |
393 | node = node->GetNext() ) | |
394 | { | |
395 | wxWindow *win = node->GetData(); | |
396 | if ( !win->IsTopLevel() ) | |
397 | ::SetParent(GetHwndOf(win), GetHwnd()); | |
398 | } | |
399 | ||
400 | // only destroy the old toolbar now -- | |
401 | // after all the children had been reparented | |
402 | ::DestroyWindow(hwndOld); | |
403 | ||
404 | // it is for the old bitmap control and can't be used with the new one | |
405 | if ( m_hBitmap ) | |
406 | { | |
407 | ::DeleteObject((HBITMAP) m_hBitmap); | |
408 | m_hBitmap = 0; | |
409 | } | |
410 | ||
411 | wxDELETE(m_disabledImgList); | |
412 | ||
413 | Realize(); | |
414 | } | |
415 | ||
416 | wxToolBar::~wxToolBar() | |
417 | { | |
418 | // we must refresh the frame size when the toolbar is deleted but the frame | |
419 | // is not - otherwise toolbar leaves a hole in the place it used to occupy | |
420 | SendSizeEventToParent(); | |
421 | ||
422 | if ( m_hBitmap ) | |
423 | ::DeleteObject((HBITMAP) m_hBitmap); | |
424 | ||
425 | delete m_disabledImgList; | |
426 | } | |
427 | ||
428 | wxSize wxToolBar::DoGetBestSize() const | |
429 | { | |
430 | wxSize sizeBest; | |
431 | ||
432 | SIZE size; | |
433 | if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE, 0, (LPARAM)&size) ) | |
434 | { | |
435 | // maybe an old (< 0x400) Windows version? try to approximate the | |
436 | // toolbar size ourselves | |
437 | sizeBest = GetToolSize(); | |
438 | sizeBest.y += 2 * ::GetSystemMetrics(SM_CYBORDER); // Add borders | |
439 | sizeBest.x *= GetToolsCount(); | |
440 | ||
441 | // reverse horz and vertical components if necessary | |
442 | if ( IsVertical() ) | |
443 | { | |
444 | int t = sizeBest.x; | |
445 | sizeBest.x = sizeBest.y; | |
446 | sizeBest.y = t; | |
447 | } | |
448 | } | |
449 | else // TB_GETMAXSIZE succeeded | |
450 | { | |
451 | // but it could still return an incorrect result due to what appears to | |
452 | // be a bug in old comctl32.dll versions which don't handle controls in | |
453 | // the toolbar correctly, so work around it (see SF patch 1902358) | |
454 | if ( !IsVertical() && wxApp::GetComCtl32Version() < 600 ) | |
455 | { | |
456 | // calculate the toolbar width in alternative way | |
457 | const RECT rcFirst = wxGetTBItemRect(GetHwnd(), 0); | |
458 | const RECT rcLast = wxGetTBItemRect(GetHwnd(), GetToolsCount() - 1); | |
459 | ||
460 | const int widthAlt = rcLast.right - rcFirst.left; | |
461 | if ( widthAlt > size.cx ) | |
462 | size.cx = widthAlt; | |
463 | } | |
464 | ||
465 | sizeBest.x = size.cx; | |
466 | sizeBest.y = size.cy; | |
467 | } | |
468 | ||
469 | if ( !IsVertical() ) | |
470 | { | |
471 | // Without the extra height, DoGetBestSize can report a size that's | |
472 | // smaller than the actual window, causing windows to overlap slightly | |
473 | // in some circumstances, leading to missing borders (especially noticeable | |
474 | // in AUI layouts). | |
475 | if (!(GetWindowStyle() & wxTB_NODIVIDER)) | |
476 | sizeBest.y += 2; | |
477 | sizeBest.y ++; | |
478 | } | |
479 | ||
480 | CacheBestSize(sizeBest); | |
481 | ||
482 | return sizeBest; | |
483 | } | |
484 | ||
485 | WXDWORD wxToolBar::MSWGetStyle(long style, WXDWORD *exstyle) const | |
486 | { | |
487 | // toolbars never have border, giving one to them results in broken | |
488 | // appearance | |
489 | WXDWORD msStyle = wxControl::MSWGetStyle | |
490 | ( | |
491 | (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle | |
492 | ); | |
493 | ||
494 | if ( !(style & wxTB_NO_TOOLTIPS) ) | |
495 | msStyle |= TBSTYLE_TOOLTIPS; | |
496 | ||
497 | if ( style & wxTB_FLAT && wxApp::GetComCtl32Version() > 400 ) | |
498 | msStyle |= TBSTYLE_FLAT; | |
499 | ||
500 | if ( style & wxTB_HORZ_LAYOUT && wxApp::GetComCtl32Version() >= 470 ) | |
501 | msStyle |= TBSTYLE_LIST; | |
502 | ||
503 | if ( style & wxTB_NODIVIDER ) | |
504 | msStyle |= CCS_NODIVIDER; | |
505 | ||
506 | if ( style & wxTB_NOALIGN ) | |
507 | msStyle |= CCS_NOPARENTALIGN; | |
508 | ||
509 | if ( style & wxTB_VERTICAL ) | |
510 | msStyle |= CCS_VERT; | |
511 | ||
512 | if( style & wxTB_BOTTOM ) | |
513 | msStyle |= CCS_BOTTOM; | |
514 | ||
515 | if ( style & wxTB_RIGHT ) | |
516 | msStyle |= CCS_RIGHT; | |
517 | ||
518 | // always use TBSTYLE_TRANSPARENT because the background is not drawn | |
519 | // correctly without it in all themes and, for whatever reason, the control | |
520 | // also flickers horribly when it is resized if this style is not used | |
521 | // | |
522 | // note that this is implicitly enabled by the native toolbar itself when | |
523 | // TBSTYLE_FLAT is used (i.e. it's impossible to use TBSTYLE_FLAT without | |
524 | // TBSTYLE_TRANSPARENT) but turn it on explicitly in any case | |
525 | msStyle |= TBSTYLE_TRANSPARENT; | |
526 | ||
527 | return msStyle; | |
528 | } | |
529 | ||
530 | // ---------------------------------------------------------------------------- | |
531 | // adding/removing tools | |
532 | // ---------------------------------------------------------------------------- | |
533 | ||
534 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), | |
535 | wxToolBarToolBase * WXUNUSED(tool)) | |
536 | { | |
537 | // nothing special to do here - we really create the toolbar buttons in | |
538 | // Realize() later | |
539 | InvalidateBestSize(); | |
540 | return true; | |
541 | } | |
542 | ||
543 | bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool) | |
544 | { | |
545 | // the main difficulty we have here is with the controls in the toolbars: | |
546 | // as we (sometimes) use several separators to cover up the space used by | |
547 | // them, the indices are not the same for us and the toolbar | |
548 | ||
549 | // first determine the position of the first button to delete: it may be | |
550 | // different from pos if we use several separators to cover the space used | |
551 | // by a control | |
552 | wxToolBarToolsList::compatibility_iterator node; | |
553 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
554 | { | |
555 | wxToolBarToolBase *tool2 = node->GetData(); | |
556 | if ( tool2 == tool ) | |
557 | { | |
558 | // let node point to the next node in the list | |
559 | node = node->GetNext(); | |
560 | ||
561 | break; | |
562 | } | |
563 | ||
564 | if ( tool2->IsControl() ) | |
565 | pos += ((wxToolBarTool *)tool2)->GetSeparatorsCount() - 1; | |
566 | } | |
567 | ||
568 | // now determine the number of buttons to delete and the area taken by them | |
569 | size_t nButtonsToDelete = 1; | |
570 | ||
571 | // get the size of the button we're going to delete | |
572 | const RECT r = wxGetTBItemRect(GetHwnd(), pos); | |
573 | ||
574 | int delta = IsVertical() ? r.bottom - r.top : r.right - r.left; | |
575 | ||
576 | if ( tool->IsControl() ) | |
577 | { | |
578 | nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount(); | |
579 | ||
580 | if ( !IsVertical() ) | |
581 | delta *= nButtonsToDelete; | |
582 | } | |
583 | ||
584 | m_totalFixedSize -= delta; | |
585 | ||
586 | // do delete all buttons | |
587 | m_nButtons -= nButtonsToDelete; | |
588 | while ( nButtonsToDelete-- > 0 ) | |
589 | { | |
590 | if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) ) | |
591 | { | |
592 | wxLogLastError(wxT("TB_DELETEBUTTON")); | |
593 | ||
594 | return false; | |
595 | } | |
596 | } | |
597 | ||
598 | // and finally rearrange the tools | |
599 | ||
600 | // search for any stretch spacers before the removed tool | |
601 | bool hasPrecedingStrechables = false; | |
602 | for ( wxToolBarToolsList::compatibility_iterator nodeStch = m_tools.GetFirst(); | |
603 | nodeStch != node; nodeStch = nodeStch->GetNext() ) | |
604 | { | |
605 | if ( ((wxToolBarTool*)nodeStch->GetData())->IsStretchable() ) | |
606 | { | |
607 | hasPrecedingStrechables = true; | |
608 | break; | |
609 | } | |
610 | } | |
611 | ||
612 | if ( hasPrecedingStrechables ) | |
613 | { | |
614 | // if the removed tool is preceded by stretch spacers | |
615 | // just redistribute the space | |
616 | UpdateStretchableSpacersSize(); | |
617 | } | |
618 | else | |
619 | { | |
620 | // reposition all the controls after this button but before any | |
621 | // stretch spacer (the toolbar takes care of all normal items) | |
622 | for ( /* node -> first after deleted */ ; node; node = node->GetNext() ) | |
623 | { | |
624 | wxToolBarTool *tool2 = (wxToolBarTool*)node->GetData(); | |
625 | ||
626 | if ( tool2->IsControl() ) | |
627 | { | |
628 | tool2->MoveBy(-delta); | |
629 | } | |
630 | ||
631 | // if a stretch spacer is found just redistribute the available space | |
632 | else if ( tool2->IsStretchable() ) | |
633 | { | |
634 | UpdateStretchableSpacersSize(); | |
635 | break; | |
636 | } | |
637 | } | |
638 | } | |
639 | ||
640 | InvalidateBestSize(); | |
641 | ||
642 | return true; | |
643 | } | |
644 | ||
645 | void wxToolBar::CreateDisabledImageList() | |
646 | { | |
647 | wxDELETE(m_disabledImgList); | |
648 | ||
649 | // as we can't use disabled image list with older versions of comctl32.dll, | |
650 | // don't even bother creating it | |
651 | if ( wxApp::GetComCtl32Version() >= 470 ) | |
652 | { | |
653 | // search for the first disabled button img in the toolbar, if any | |
654 | for ( wxToolBarToolsList::compatibility_iterator | |
655 | node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
656 | { | |
657 | wxToolBarToolBase *tool = node->GetData(); | |
658 | wxBitmap bmpDisabled = tool->GetDisabledBitmap(); | |
659 | if ( bmpDisabled.IsOk() ) | |
660 | { | |
661 | const wxSize sizeBitmap = bmpDisabled.GetSize(); | |
662 | m_disabledImgList = new wxImageList | |
663 | ( | |
664 | sizeBitmap.x, | |
665 | sizeBitmap.y, | |
666 | bmpDisabled.GetMask() != NULL, | |
667 | GetToolsCount() | |
668 | ); | |
669 | break; | |
670 | } | |
671 | } | |
672 | ||
673 | // we don't have any disabled bitmaps | |
674 | } | |
675 | } | |
676 | ||
677 | bool wxToolBar::Realize() | |
678 | { | |
679 | if ( !wxToolBarBase::Realize() ) | |
680 | return false; | |
681 | ||
682 | const size_t nTools = GetToolsCount(); | |
683 | ||
684 | #ifdef wxREMAP_BUTTON_COLOURS | |
685 | // don't change the values of these constants, they can be set from the | |
686 | // user code via wxSystemOptions | |
687 | enum | |
688 | { | |
689 | Remap_None = -1, | |
690 | Remap_Bg, | |
691 | Remap_Buttons, | |
692 | Remap_TransparentBg | |
693 | }; | |
694 | ||
695 | // the user-specified option overrides anything, but if it wasn't set, only | |
696 | // remap the buttons on 8bpp displays as otherwise the bitmaps usually look | |
697 | // much worse after remapping | |
698 | static const wxChar *remapOption = wxT("msw.remap"); | |
699 | const int remapValue = wxSystemOptions::HasOption(remapOption) | |
700 | ? wxSystemOptions::GetOptionInt(remapOption) | |
701 | : wxDisplayDepth() <= 8 ? Remap_Buttons | |
702 | : Remap_None; | |
703 | ||
704 | #endif // wxREMAP_BUTTON_COLOURS | |
705 | ||
706 | // delete all old buttons, if any | |
707 | for ( size_t pos = 0; pos < m_nButtons; pos++ ) | |
708 | { | |
709 | if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) ) | |
710 | { | |
711 | wxLogDebug(wxT("TB_DELETEBUTTON failed")); | |
712 | } | |
713 | } | |
714 | ||
715 | // First, add the bitmap: we use one bitmap for all toolbar buttons | |
716 | // ---------------------------------------------------------------- | |
717 | ||
718 | wxToolBarToolsList::compatibility_iterator node; | |
719 | int bitmapId = 0; | |
720 | ||
721 | if ( !HasFlag(wxTB_NOICONS) ) | |
722 | { | |
723 | // if we already have a bitmap, we'll replace the existing one -- | |
724 | // otherwise we'll install a new one | |
725 | HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap; | |
726 | ||
727 | const wxCoord totalBitmapWidth = m_defaultWidth * | |
728 | wx_truncate_cast(wxCoord, nTools), | |
729 | totalBitmapHeight = m_defaultHeight; | |
730 | ||
731 | // Create a bitmap and copy all the tool bitmaps into it | |
732 | wxMemoryDC dcAllButtons; | |
733 | wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight); | |
734 | dcAllButtons.SelectObject(bitmap); | |
735 | ||
736 | #ifdef wxREMAP_BUTTON_COLOURS | |
737 | if ( remapValue != Remap_TransparentBg ) | |
738 | #endif // wxREMAP_BUTTON_COLOURS | |
739 | { | |
740 | // VZ: why do we hardcode grey colour for CE? | |
741 | dcAllButtons.SetBackground(wxBrush( | |
742 | #ifdef __WXWINCE__ | |
743 | wxColour(0xc0, 0xc0, 0xc0) | |
744 | #else // !__WXWINCE__ | |
745 | GetBackgroundColour() | |
746 | #endif // __WXWINCE__/!__WXWINCE__ | |
747 | )); | |
748 | dcAllButtons.Clear(); | |
749 | } | |
750 | ||
751 | m_hBitmap = bitmap.GetHBITMAP(); | |
752 | HBITMAP hBitmap = (HBITMAP)m_hBitmap; | |
753 | ||
754 | #ifdef wxREMAP_BUTTON_COLOURS | |
755 | if ( remapValue == Remap_Bg ) | |
756 | { | |
757 | dcAllButtons.SelectObject(wxNullBitmap); | |
758 | ||
759 | // Even if we're not remapping the bitmap | |
760 | // content, we still have to remap the background. | |
761 | hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap, | |
762 | totalBitmapWidth, totalBitmapHeight); | |
763 | ||
764 | dcAllButtons.SelectObject(bitmap); | |
765 | } | |
766 | #endif // wxREMAP_BUTTON_COLOURS | |
767 | ||
768 | // the button position | |
769 | wxCoord x = 0; | |
770 | ||
771 | // the number of buttons (not separators) | |
772 | int nButtons = 0; | |
773 | ||
774 | CreateDisabledImageList(); | |
775 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
776 | { | |
777 | wxToolBarToolBase *tool = node->GetData(); | |
778 | if ( tool->IsButton() ) | |
779 | { | |
780 | const wxBitmap& bmp = tool->GetNormalBitmap(); | |
781 | ||
782 | const int w = bmp.GetWidth(); | |
783 | const int h = bmp.GetHeight(); | |
784 | ||
785 | if ( bmp.IsOk() ) | |
786 | { | |
787 | int xOffset = wxMax(0, (m_defaultWidth - w)/2); | |
788 | int yOffset = wxMax(0, (m_defaultHeight - h)/2); | |
789 | ||
790 | // notice the last parameter: do use mask | |
791 | dcAllButtons.DrawBitmap(bmp, x + xOffset, yOffset, true); | |
792 | } | |
793 | else | |
794 | { | |
795 | wxFAIL_MSG( wxT("invalid tool button bitmap") ); | |
796 | } | |
797 | ||
798 | // also deal with disabled bitmap if we want to use them | |
799 | if ( m_disabledImgList ) | |
800 | { | |
801 | wxBitmap bmpDisabled = tool->GetDisabledBitmap(); | |
802 | #if wxUSE_IMAGE && wxUSE_WXDIB | |
803 | if ( !bmpDisabled.IsOk() ) | |
804 | { | |
805 | // no disabled bitmap specified but we still need to | |
806 | // fill the space in the image list with something, so | |
807 | // we grey out the normal bitmap | |
808 | wxImage | |
809 | imgGreyed = bmp.ConvertToImage().ConvertToGreyscale(); | |
810 | ||
811 | #ifdef wxREMAP_BUTTON_COLOURS | |
812 | if ( remapValue == Remap_Buttons ) | |
813 | { | |
814 | // we need to have light grey background colour for | |
815 | // MapBitmap() to work correctly | |
816 | for ( int y = 0; y < h; y++ ) | |
817 | { | |
818 | for ( int x = 0; x < w; x++ ) | |
819 | { | |
820 | if ( imgGreyed.IsTransparent(x, y) ) | |
821 | imgGreyed.SetRGB(x, y, | |
822 | wxLIGHT_GREY->Red(), | |
823 | wxLIGHT_GREY->Green(), | |
824 | wxLIGHT_GREY->Blue()); | |
825 | } | |
826 | } | |
827 | } | |
828 | #endif // wxREMAP_BUTTON_COLOURS | |
829 | ||
830 | bmpDisabled = wxBitmap(imgGreyed); | |
831 | } | |
832 | #endif // wxUSE_IMAGE | |
833 | ||
834 | #ifdef wxREMAP_BUTTON_COLOURS | |
835 | if ( remapValue == Remap_Buttons ) | |
836 | MapBitmap(bmpDisabled.GetHBITMAP(), w, h); | |
837 | #endif // wxREMAP_BUTTON_COLOURS | |
838 | ||
839 | m_disabledImgList->Add(bmpDisabled); | |
840 | } | |
841 | ||
842 | // still inc width and number of buttons because otherwise the | |
843 | // subsequent buttons will all be shifted which is rather confusing | |
844 | // (and like this you'd see immediately which bitmap was bad) | |
845 | x += m_defaultWidth; | |
846 | nButtons++; | |
847 | } | |
848 | } | |
849 | ||
850 | dcAllButtons.SelectObject(wxNullBitmap); | |
851 | ||
852 | // don't delete this HBITMAP! | |
853 | bitmap.SetHBITMAP(0); | |
854 | ||
855 | #ifdef wxREMAP_BUTTON_COLOURS | |
856 | if ( remapValue == Remap_Buttons ) | |
857 | { | |
858 | // Map to system colours | |
859 | hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap, | |
860 | totalBitmapWidth, totalBitmapHeight); | |
861 | } | |
862 | #endif // wxREMAP_BUTTON_COLOURS | |
863 | ||
864 | bool addBitmap = true; | |
865 | ||
866 | if ( oldToolBarBitmap ) | |
867 | { | |
868 | #ifdef TB_REPLACEBITMAP | |
869 | if ( wxApp::GetComCtl32Version() >= 400 ) | |
870 | { | |
871 | TBREPLACEBITMAP replaceBitmap; | |
872 | replaceBitmap.hInstOld = NULL; | |
873 | replaceBitmap.hInstNew = NULL; | |
874 | replaceBitmap.nIDOld = (UINT_PTR)oldToolBarBitmap; | |
875 | replaceBitmap.nIDNew = (UINT_PTR)hBitmap; | |
876 | replaceBitmap.nButtons = nButtons; | |
877 | if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP, | |
878 | 0, (LPARAM) &replaceBitmap) ) | |
879 | { | |
880 | wxFAIL_MSG(wxT("Could not replace the old bitmap")); | |
881 | } | |
882 | ||
883 | ::DeleteObject(oldToolBarBitmap); | |
884 | ||
885 | // already done | |
886 | addBitmap = false; | |
887 | } | |
888 | else | |
889 | #endif // TB_REPLACEBITMAP | |
890 | { | |
891 | // we can't replace the old bitmap, so we will add another one | |
892 | // (awfully inefficient, but what else to do?) and shift the bitmap | |
893 | // indices accordingly | |
894 | addBitmap = true; | |
895 | ||
896 | bitmapId = m_nButtons; | |
897 | } | |
898 | } | |
899 | ||
900 | if ( addBitmap ) // no old bitmap or we can't replace it | |
901 | { | |
902 | TBADDBITMAP addBitmap; | |
903 | addBitmap.hInst = 0; | |
904 | addBitmap.nID = (UINT_PTR)hBitmap; | |
905 | if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP, | |
906 | (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 ) | |
907 | { | |
908 | wxFAIL_MSG(wxT("Could not add bitmap to toolbar")); | |
909 | } | |
910 | } | |
911 | ||
912 | // disable image lists are only supported in comctl32.dll 4.70+ | |
913 | if ( wxApp::GetComCtl32Version() >= 470 ) | |
914 | { | |
915 | HIMAGELIST hil = m_disabledImgList | |
916 | ? GetHimagelistOf(m_disabledImgList) | |
917 | : 0; | |
918 | ||
919 | // notice that we set the image list even if don't have one right | |
920 | // now as we could have it before and need to reset it in this case | |
921 | HIMAGELIST oldImageList = (HIMAGELIST) | |
922 | ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST, 0, (LPARAM)hil); | |
923 | ||
924 | // delete previous image list if any | |
925 | if ( oldImageList ) | |
926 | ::DeleteObject(oldImageList); | |
927 | } | |
928 | } | |
929 | ||
930 | ||
931 | // Next add the buttons and separators | |
932 | // ----------------------------------- | |
933 | ||
934 | wxScopedArray<TBBUTTON> buttons(new TBBUTTON[nTools]); | |
935 | ||
936 | // this array will hold the indices of all controls in the toolbar | |
937 | wxArrayInt controlIds; | |
938 | ||
939 | bool lastWasRadio = false; | |
940 | int i = 0; | |
941 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
942 | { | |
943 | wxToolBarTool *tool = static_cast<wxToolBarTool *>(node->GetData()); | |
944 | ||
945 | // don't add separators to the vertical toolbar with old comctl32.dll | |
946 | // versions as they didn't handle this properly | |
947 | if ( IsVertical() && tool->IsSeparator() && | |
948 | wxApp::GetComCtl32Version() <= 472 ) | |
949 | { | |
950 | continue; | |
951 | } | |
952 | ||
953 | TBBUTTON& button = buttons[i]; | |
954 | ||
955 | wxZeroMemory(button); | |
956 | ||
957 | bool isRadio = false; | |
958 | switch ( tool->GetStyle() ) | |
959 | { | |
960 | case wxTOOL_STYLE_CONTROL: | |
961 | case wxTOOL_STYLE_SEPARATOR: | |
962 | if ( tool->IsStretchableSpace() ) | |
963 | { | |
964 | // we're going to modify the size of this button later and | |
965 | // so we need a valid id for it and not wxID_SEPARATOR | |
966 | // which is used by spacers by default | |
967 | tool->AllocSpacerId(); | |
968 | ||
969 | // also set the number of separators so that the logic in | |
970 | // HandlePaint() works correctly | |
971 | tool->SetSeparatorsCount(1); | |
972 | } | |
973 | ||
974 | button.idCommand = tool->GetId(); | |
975 | button.fsState = TBSTATE_ENABLED; | |
976 | button.fsStyle = TBSTYLE_SEP; | |
977 | break; | |
978 | ||
979 | case wxTOOL_STYLE_BUTTON: | |
980 | if ( !HasFlag(wxTB_NOICONS) ) | |
981 | button.iBitmap = bitmapId; | |
982 | ||
983 | if ( HasFlag(wxTB_TEXT) ) | |
984 | { | |
985 | const wxString& label = tool->GetLabel(); | |
986 | if ( !label.empty() ) | |
987 | button.iString = (INT_PTR) wxMSW_CONV_LPCTSTR(label); | |
988 | } | |
989 | ||
990 | button.idCommand = tool->GetId(); | |
991 | ||
992 | if ( tool->IsEnabled() ) | |
993 | button.fsState |= TBSTATE_ENABLED; | |
994 | if ( tool->IsToggled() ) | |
995 | button.fsState |= TBSTATE_CHECKED; | |
996 | ||
997 | switch ( tool->GetKind() ) | |
998 | { | |
999 | case wxITEM_RADIO: | |
1000 | button.fsStyle = TBSTYLE_CHECKGROUP; | |
1001 | ||
1002 | if ( !lastWasRadio ) | |
1003 | { | |
1004 | // the first item in the radio group is checked by | |
1005 | // default to be consistent with wxGTK and the menu | |
1006 | // radio items | |
1007 | button.fsState |= TBSTATE_CHECKED; | |
1008 | ||
1009 | if (tool->Toggle(true)) | |
1010 | { | |
1011 | DoToggleTool(tool, true); | |
1012 | } | |
1013 | } | |
1014 | else if ( tool->IsToggled() ) | |
1015 | { | |
1016 | wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious(); | |
1017 | int prevIndex = i - 1; | |
1018 | while ( nodePrev ) | |
1019 | { | |
1020 | TBBUTTON& prevButton = buttons[prevIndex]; | |
1021 | wxToolBarToolBase *tool = nodePrev->GetData(); | |
1022 | if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO ) | |
1023 | break; | |
1024 | ||
1025 | if ( tool->Toggle(false) ) | |
1026 | DoToggleTool(tool, false); | |
1027 | ||
1028 | prevButton.fsState &= ~TBSTATE_CHECKED; | |
1029 | nodePrev = nodePrev->GetPrevious(); | |
1030 | prevIndex--; | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | isRadio = true; | |
1035 | break; | |
1036 | ||
1037 | case wxITEM_CHECK: | |
1038 | button.fsStyle = TBSTYLE_CHECK; | |
1039 | break; | |
1040 | ||
1041 | case wxITEM_NORMAL: | |
1042 | button.fsStyle = TBSTYLE_BUTTON; | |
1043 | break; | |
1044 | ||
1045 | case wxITEM_DROPDOWN: | |
1046 | button.fsStyle = TBSTYLE_DROPDOWN; | |
1047 | break; | |
1048 | ||
1049 | default: | |
1050 | wxFAIL_MSG( wxT("unexpected toolbar button kind") ); | |
1051 | button.fsStyle = TBSTYLE_BUTTON; | |
1052 | break; | |
1053 | } | |
1054 | ||
1055 | // Instead of using fixed widths for all buttons, size them | |
1056 | // automatically according to the size of their bitmap and text | |
1057 | // label, if present. This particularly matters for toolbars | |
1058 | // with the wxTB_HORZ_LAYOUT style: they look hideously ugly | |
1059 | // without autosizing when the labels have even slightly | |
1060 | // different lengths. | |
1061 | button.fsStyle |= TBSTYLE_AUTOSIZE; | |
1062 | ||
1063 | bitmapId++; | |
1064 | break; | |
1065 | } | |
1066 | ||
1067 | lastWasRadio = isRadio; | |
1068 | ||
1069 | i++; | |
1070 | } | |
1071 | ||
1072 | if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, i, (LPARAM)buttons.get()) ) | |
1073 | { | |
1074 | wxLogLastError(wxT("TB_ADDBUTTONS")); | |
1075 | } | |
1076 | ||
1077 | ||
1078 | // Adjust controls and stretchable spaces | |
1079 | // -------------------------------------- | |
1080 | ||
1081 | // adjust the controls size to fit nicely in the toolbar and compute its | |
1082 | // total size while doing it | |
1083 | m_totalFixedSize = 0; | |
1084 | int toolIndex = 0; | |
1085 | for ( node = m_tools.GetFirst(); node; node = node->GetNext(), toolIndex++ ) | |
1086 | { | |
1087 | wxToolBarTool * const tool = (wxToolBarTool*)node->GetData(); | |
1088 | ||
1089 | const RECT r = wxGetTBItemRect(GetHwnd(), toolIndex); | |
1090 | ||
1091 | if ( !tool->IsControl() ) | |
1092 | { | |
1093 | if ( IsVertical() ) | |
1094 | m_totalFixedSize += r.bottom - r.top; | |
1095 | else | |
1096 | m_totalFixedSize += r.right - r.left; | |
1097 | ||
1098 | continue; | |
1099 | } | |
1100 | ||
1101 | if ( IsVertical() ) | |
1102 | { | |
1103 | // don't embed controls in the vertical toolbar, this doesn't look | |
1104 | // good and wxGTK doesn't do it neither (and the code below can't | |
1105 | // deal with this case) | |
1106 | continue; | |
1107 | } | |
1108 | ||
1109 | wxControl * const control = tool->GetControl(); | |
1110 | wxStaticText * const staticText = tool->GetStaticText(); | |
1111 | ||
1112 | wxSize size = control->GetSize(); | |
1113 | wxSize staticTextSize; | |
1114 | if ( staticText ) | |
1115 | { | |
1116 | staticTextSize = staticText->GetSize(); | |
1117 | staticTextSize.y += 3; // margin between control and its label | |
1118 | } | |
1119 | ||
1120 | // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+ | |
1121 | #ifdef TB_SETBUTTONINFO | |
1122 | // available in headers, now check whether it is available now | |
1123 | // (during run-time) | |
1124 | if ( wxApp::GetComCtl32Version() >= 471 ) | |
1125 | { | |
1126 | // set the (underlying) separators width to be that of the | |
1127 | // control | |
1128 | TBBUTTONINFO tbbi; | |
1129 | tbbi.cbSize = sizeof(tbbi); | |
1130 | tbbi.dwMask = TBIF_SIZE; | |
1131 | tbbi.cx = (WORD)size.x; | |
1132 | if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO, | |
1133 | tool->GetId(), (LPARAM)&tbbi) ) | |
1134 | { | |
1135 | // the id is probably invalid? | |
1136 | wxLogLastError(wxT("TB_SETBUTTONINFO")); | |
1137 | } | |
1138 | } | |
1139 | else | |
1140 | #endif // comctl32.dll 4.71 | |
1141 | // TB_SETBUTTONINFO unavailable | |
1142 | { | |
1143 | // try adding several separators to fit the controls width | |
1144 | int widthSep = r.right - r.left; | |
1145 | ||
1146 | TBBUTTON tbb; | |
1147 | wxZeroMemory(tbb); | |
1148 | tbb.idCommand = 0; | |
1149 | tbb.fsState = TBSTATE_ENABLED; | |
1150 | tbb.fsStyle = TBSTYLE_SEP; | |
1151 | ||
1152 | size_t nSeparators = size.x / widthSep; | |
1153 | for ( size_t nSep = 0; nSep < nSeparators; nSep++ ) | |
1154 | { | |
1155 | if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON, | |
1156 | toolIndex, (LPARAM)&tbb) ) | |
1157 | { | |
1158 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
1159 | } | |
1160 | ||
1161 | toolIndex++; | |
1162 | } | |
1163 | ||
1164 | // remember the number of separators we used - we'd have to | |
1165 | // delete all of them later | |
1166 | tool->SetSeparatorsCount(nSeparators); | |
1167 | ||
1168 | // adjust the controls width to exactly cover the separators | |
1169 | size.x = (nSeparators + 1)*widthSep; | |
1170 | control->SetSize(size.x, wxDefaultCoord); | |
1171 | } | |
1172 | ||
1173 | // position the control itself correctly vertically centering it on the | |
1174 | // icon area of the toolbar | |
1175 | int height = r.bottom - r.top - staticTextSize.y; | |
1176 | ||
1177 | int diff = height - size.y; | |
1178 | if ( diff < 0 || !HasFlag(wxTB_TEXT) ) | |
1179 | { | |
1180 | // not enough room for the static text | |
1181 | if ( staticText ) | |
1182 | staticText->Hide(); | |
1183 | ||
1184 | // recalculate height & diff without the staticText control | |
1185 | height = r.bottom - r.top; | |
1186 | diff = height - size.y; | |
1187 | if ( diff < 0 ) | |
1188 | { | |
1189 | // the control is too high, resize to fit | |
1190 | control->SetSize(wxDefaultCoord, height - 2); | |
1191 | ||
1192 | diff = 2; | |
1193 | } | |
1194 | } | |
1195 | else // enough space for both the control and the label | |
1196 | { | |
1197 | if ( staticText ) | |
1198 | staticText->Show(); | |
1199 | } | |
1200 | ||
1201 | control->Move(r.left, r.top + (diff + 1) / 2); | |
1202 | if ( staticText ) | |
1203 | { | |
1204 | staticText->Move(r.left + (size.x - staticTextSize.x)/2, | |
1205 | r.bottom - staticTextSize.y); | |
1206 | } | |
1207 | ||
1208 | m_totalFixedSize += size.x; | |
1209 | } | |
1210 | ||
1211 | // the max index is the "real" number of buttons - i.e. counting even the | |
1212 | // separators which we added just for aligning the controls | |
1213 | m_nButtons = toolIndex; | |
1214 | ||
1215 | if ( !IsVertical() ) | |
1216 | { | |
1217 | if ( m_maxRows == 0 ) | |
1218 | // if not set yet, only one row | |
1219 | SetRows(1); | |
1220 | } | |
1221 | else if ( m_nButtons > 0 ) // vertical non empty toolbar | |
1222 | { | |
1223 | // if not set yet, have one column | |
1224 | m_maxRows = 1; | |
1225 | SetRows(m_nButtons); | |
1226 | } | |
1227 | ||
1228 | InvalidateBestSize(); | |
1229 | UpdateSize(); | |
1230 | ||
1231 | return true; | |
1232 | } | |
1233 | ||
1234 | void wxToolBar::UpdateStretchableSpacersSize() | |
1235 | { | |
1236 | #ifdef TB_SETBUTTONINFO | |
1237 | // we can't resize the spacers if TB_SETBUTTONINFO is not supported (we | |
1238 | // could try to do it with multiple separators as for the controls but this | |
1239 | // is too painful and it just doesn't seem to be worth doing for the | |
1240 | // ancient systems) | |
1241 | if ( wxApp::GetComCtl32Version() < 471 ) | |
1242 | return; | |
1243 | ||
1244 | // check if we have any stretchable spacers in the first place | |
1245 | unsigned numSpaces = 0; | |
1246 | wxToolBarToolsList::compatibility_iterator node; | |
1247 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
1248 | { | |
1249 | wxToolBarTool * const tool = (wxToolBarTool*)node->GetData(); | |
1250 | if ( tool->IsStretchableSpace() ) | |
1251 | numSpaces++; | |
1252 | } | |
1253 | ||
1254 | if ( !numSpaces ) | |
1255 | return; | |
1256 | ||
1257 | // we do, adjust their size: either distribute the extra size among them or | |
1258 | // reduce their size if there is not enough place for all tools | |
1259 | const int totalSize = IsVertical() ? GetClientSize().y : GetClientSize().x; | |
1260 | const int extraSize = totalSize - m_totalFixedSize; | |
1261 | const int sizeSpacer = extraSize > 0 ? extraSize / numSpaces : 1; | |
1262 | ||
1263 | // the last spacer should consume all remaining space if we have too much | |
1264 | // of it (which can be greater than sizeSpacer because of the rounding) | |
1265 | const int sizeLastSpacer = extraSize > 0 | |
1266 | ? extraSize - (numSpaces - 1)*sizeSpacer | |
1267 | : 1; | |
1268 | ||
1269 | // cumulated offset by which we need to move all the following controls to | |
1270 | // the right: while the toolbar takes care of the normal items, we must | |
1271 | // move the controls manually ourselves to ensure they remain at the | |
1272 | // correct place | |
1273 | int offset = 0; | |
1274 | int toolIndex = 0; | |
1275 | for ( node = m_tools.GetFirst(); node; node = node->GetNext(), toolIndex++ ) | |
1276 | { | |
1277 | wxToolBarTool * const tool = (wxToolBarTool*)node->GetData(); | |
1278 | ||
1279 | if ( tool->IsControl() && offset ) | |
1280 | { | |
1281 | tool->MoveBy(offset); | |
1282 | ||
1283 | continue; | |
1284 | } | |
1285 | ||
1286 | if ( !tool->IsStretchableSpace() ) | |
1287 | continue; | |
1288 | ||
1289 | const RECT rcOld = wxGetTBItemRect(GetHwnd(), toolIndex); | |
1290 | ||
1291 | WinStruct<TBBUTTONINFO> tbbi; | |
1292 | tbbi.dwMask = TBIF_SIZE; | |
1293 | tbbi.cx = --numSpaces ? sizeSpacer : sizeLastSpacer; | |
1294 | ||
1295 | if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO, | |
1296 | tool->GetId(), (LPARAM)&tbbi) ) | |
1297 | { | |
1298 | wxLogLastError(wxT("TB_SETBUTTONINFO")); | |
1299 | } | |
1300 | else | |
1301 | { | |
1302 | // we successfully resized this one, move all the controls after it | |
1303 | // by the corresponding amount (may be positive or negative) | |
1304 | offset += tbbi.cx - (rcOld.right - rcOld.left); | |
1305 | } | |
1306 | } | |
1307 | #endif // TB_SETBUTTONINFO | |
1308 | } | |
1309 | ||
1310 | // ---------------------------------------------------------------------------- | |
1311 | // message handlers | |
1312 | // ---------------------------------------------------------------------------- | |
1313 | ||
1314 | bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id_) | |
1315 | { | |
1316 | // cast to signed is important as we compare this id with (signed) ints in | |
1317 | // FindById() and without the cast we'd get a positive int from a | |
1318 | // "negative" (i.e. > 32767) WORD | |
1319 | const int id = (signed short)id_; | |
1320 | ||
1321 | wxToolBarToolBase *tool = FindById(id); | |
1322 | if ( !tool ) | |
1323 | return false; | |
1324 | ||
1325 | bool toggled = false; // just to suppress warnings | |
1326 | ||
1327 | LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0); | |
1328 | ||
1329 | if ( tool->CanBeToggled() ) | |
1330 | { | |
1331 | toggled = (state & TBSTATE_CHECKED) != 0; | |
1332 | ||
1333 | // ignore the event when a radio button is released, as this doesn't | |
1334 | // seem to happen at all, and is handled otherwise | |
1335 | if ( tool->GetKind() == wxITEM_RADIO && !toggled ) | |
1336 | return true; | |
1337 | ||
1338 | tool->Toggle(toggled); | |
1339 | UnToggleRadioGroup(tool); | |
1340 | } | |
1341 | ||
1342 | // Without the two lines of code below, if the toolbar was repainted during | |
1343 | // OnLeftClick(), then it could end up without the tool bitmap temporarily | |
1344 | // (see http://lists.nongnu.org/archive/html/lmi/2008-10/msg00014.html). | |
1345 | // The Update() call below ensures that this won't happen, by repainting | |
1346 | // invalidated areas of the toolbar immediately. | |
1347 | // | |
1348 | // To complicate matters, the tool would be drawn in depressed state (this | |
1349 | // code is called when mouse button is released, not pressed). That's not | |
1350 | // ideal, having the tool pressed for the duration of OnLeftClick() | |
1351 | // provides the user with useful visual clue that the app is busy reacting | |
1352 | // to the event. So we manually put the tool into pressed state, handle the | |
1353 | // event and then finally restore tool's original state. | |
1354 | ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state | TBSTATE_PRESSED, 0)); | |
1355 | Update(); | |
1356 | ||
1357 | bool allowLeftClick = OnLeftClick(id, toggled); | |
1358 | ||
1359 | // Restore the unpressed state. Enabled/toggled state might have been | |
1360 | // changed since so take care of it. | |
1361 | if (tool->IsEnabled()) | |
1362 | state |= TBSTATE_ENABLED; | |
1363 | else | |
1364 | state &= ~TBSTATE_ENABLED; | |
1365 | if (tool->IsToggled()) | |
1366 | state |= TBSTATE_CHECKED; | |
1367 | else | |
1368 | state &= ~TBSTATE_CHECKED; | |
1369 | ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state, 0)); | |
1370 | ||
1371 | // OnLeftClick() can veto the button state change - for buttons which | |
1372 | // may be toggled only, of course. | |
1373 | if ( !allowLeftClick && tool->CanBeToggled() ) | |
1374 | { | |
1375 | // revert back | |
1376 | tool->Toggle(!toggled); | |
1377 | ||
1378 | ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(!toggled, 0)); | |
1379 | } | |
1380 | ||
1381 | return true; | |
1382 | } | |
1383 | ||
1384 | bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl), | |
1385 | WXLPARAM lParam, | |
1386 | WXLPARAM *WXUNUSED(result)) | |
1387 | { | |
1388 | LPNMHDR hdr = (LPNMHDR)lParam; | |
1389 | if ( hdr->code == TBN_DROPDOWN ) | |
1390 | { | |
1391 | LPNMTOOLBAR tbhdr = (LPNMTOOLBAR)lParam; | |
1392 | ||
1393 | wxCommandEvent evt(wxEVT_TOOL_DROPDOWN, tbhdr->iItem); | |
1394 | if ( HandleWindowEvent(evt) ) | |
1395 | { | |
1396 | // Event got handled, don't display default popup menu | |
1397 | return false; | |
1398 | } | |
1399 | ||
1400 | const wxToolBarToolBase * const tool = FindById(tbhdr->iItem); | |
1401 | wxCHECK_MSG( tool, false, wxT("drop down message for unknown tool") ); | |
1402 | ||
1403 | wxMenu * const menu = tool->GetDropdownMenu(); | |
1404 | if ( !menu ) | |
1405 | return false; | |
1406 | ||
1407 | // Display popup menu below button | |
1408 | const RECT r = wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr->iItem)); | |
1409 | if ( r.right ) | |
1410 | PopupMenu(menu, r.left, r.bottom); | |
1411 | ||
1412 | return true; | |
1413 | } | |
1414 | ||
1415 | ||
1416 | if( !HasFlag(wxTB_NO_TOOLTIPS) ) | |
1417 | { | |
1418 | #if wxUSE_TOOLTIPS | |
1419 | // First check if this applies to us | |
1420 | ||
1421 | // the tooltips control created by the toolbar is sometimes Unicode, even | |
1422 | // in an ANSI application - this seems to be a bug in comctl32.dll v5 | |
1423 | UINT code = hdr->code; | |
1424 | if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) ) | |
1425 | return false; | |
1426 | ||
1427 | HWND toolTipWnd = (HWND)::SendMessage(GetHwnd(), TB_GETTOOLTIPS, 0, 0); | |
1428 | if ( toolTipWnd != hdr->hwndFrom ) | |
1429 | return false; | |
1430 | ||
1431 | LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam; | |
1432 | int id = (int)ttText->hdr.idFrom; | |
1433 | ||
1434 | wxToolBarToolBase *tool = FindById(id); | |
1435 | if ( tool ) | |
1436 | return HandleTooltipNotify(code, lParam, tool->GetShortHelp()); | |
1437 | #else | |
1438 | wxUnusedVar(lParam); | |
1439 | #endif | |
1440 | } | |
1441 | ||
1442 | return false; | |
1443 | } | |
1444 | ||
1445 | // ---------------------------------------------------------------------------- | |
1446 | // toolbar geometry | |
1447 | // ---------------------------------------------------------------------------- | |
1448 | ||
1449 | void wxToolBar::SetToolBitmapSize(const wxSize& size) | |
1450 | { | |
1451 | wxToolBarBase::SetToolBitmapSize(size); | |
1452 | ||
1453 | ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y)); | |
1454 | } | |
1455 | ||
1456 | void wxToolBar::SetRows(int nRows) | |
1457 | { | |
1458 | if ( nRows == m_maxRows ) | |
1459 | { | |
1460 | // avoid resizing the frame uselessly | |
1461 | return; | |
1462 | } | |
1463 | ||
1464 | // TRUE in wParam means to create at least as many rows, FALSE - | |
1465 | // at most as many | |
1466 | RECT rect; | |
1467 | ::SendMessage(GetHwnd(), TB_SETROWS, | |
1468 | MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)), | |
1469 | (LPARAM) &rect); | |
1470 | ||
1471 | m_maxRows = nRows; | |
1472 | ||
1473 | UpdateSize(); | |
1474 | } | |
1475 | ||
1476 | // The button size is bigger than the bitmap size | |
1477 | wxSize wxToolBar::GetToolSize() const | |
1478 | { | |
1479 | // TB_GETBUTTONSIZE is supported from version 4.70 | |
1480 | #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \ | |
1481 | && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \ | |
1482 | && !defined (__DIGITALMARS__) | |
1483 | if ( wxApp::GetComCtl32Version() >= 470 ) | |
1484 | { | |
1485 | DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0); | |
1486 | ||
1487 | return wxSize(LOWORD(dw), HIWORD(dw)); | |
1488 | } | |
1489 | else | |
1490 | #endif // comctl32.dll 4.70+ | |
1491 | { | |
1492 | // defaults | |
1493 | return wxSize(m_defaultWidth + 8, m_defaultHeight + 7); | |
1494 | } | |
1495 | } | |
1496 | ||
1497 | static | |
1498 | wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools, | |
1499 | size_t index ) | |
1500 | { | |
1501 | wxToolBarToolsList::compatibility_iterator current = tools.GetFirst(); | |
1502 | ||
1503 | for ( ; current ; current = current->GetNext() ) | |
1504 | { | |
1505 | if ( index == 0 ) | |
1506 | return current->GetData(); | |
1507 | ||
1508 | wxToolBarTool *tool = (wxToolBarTool *)current->GetData(); | |
1509 | size_t separators = tool->GetSeparatorsCount(); | |
1510 | ||
1511 | // if it is a normal button, sepcount == 0, so skip 1 item (the button) | |
1512 | // otherwise, skip as many items as the separator count, plus the | |
1513 | // control itself | |
1514 | index -= separators ? separators + 1 : 1; | |
1515 | } | |
1516 | ||
1517 | return 0; | |
1518 | } | |
1519 | ||
1520 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const | |
1521 | { | |
1522 | POINT pt; | |
1523 | pt.x = x; | |
1524 | pt.y = y; | |
1525 | int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt); | |
1526 | ||
1527 | // MBN: when the point ( x, y ) is close to the toolbar border | |
1528 | // TB_HITTEST returns m_nButtons ( not -1 ) | |
1529 | if ( index < 0 || (size_t)index >= m_nButtons ) | |
1530 | // it's a separator or there is no tool at all there | |
1531 | return NULL; | |
1532 | ||
1533 | // when TB_SETBUTTONINFO is available (both during compile- and run-time), | |
1534 | // we don't use the dummy separators hack | |
1535 | #ifdef TB_SETBUTTONINFO | |
1536 | if ( wxApp::GetComCtl32Version() >= 471 ) | |
1537 | { | |
1538 | return m_tools.Item((size_t)index)->GetData(); | |
1539 | } | |
1540 | else | |
1541 | #endif // TB_SETBUTTONINFO | |
1542 | { | |
1543 | return GetItemSkippingDummySpacers( m_tools, (size_t) index ); | |
1544 | } | |
1545 | } | |
1546 | ||
1547 | void wxToolBar::UpdateSize() | |
1548 | { | |
1549 | wxPoint pos = GetPosition(); | |
1550 | ::SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0); | |
1551 | if (pos != GetPosition()) | |
1552 | Move(pos); | |
1553 | ||
1554 | // In case Realize is called after the initial display (IOW the programmer | |
1555 | // may have rebuilt the toolbar) give the frame the option of resizing the | |
1556 | // toolbar to full width again, but only if the parent is a frame and the | |
1557 | // toolbar is managed by the frame. Otherwise assume that some other | |
1558 | // layout mechanism is controlling the toolbar size and leave it alone. | |
1559 | SendSizeEventToParent(); | |
1560 | } | |
1561 | ||
1562 | // ---------------------------------------------------------------------------- | |
1563 | // toolbar styles | |
1564 | // --------------------------------------------------------------------------- | |
1565 | ||
1566 | // get the TBSTYLE of the given toolbar window | |
1567 | long wxToolBar::GetMSWToolbarStyle() const | |
1568 | { | |
1569 | return ::SendMessage(GetHwnd(), TB_GETSTYLE, 0, 0L); | |
1570 | } | |
1571 | ||
1572 | void wxToolBar::SetWindowStyleFlag(long style) | |
1573 | { | |
1574 | // the style bits whose changes force us to recreate the toolbar | |
1575 | static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS; | |
1576 | ||
1577 | const long styleOld = GetWindowStyle(); | |
1578 | ||
1579 | wxToolBarBase::SetWindowStyleFlag(style); | |
1580 | ||
1581 | // don't recreate an empty toolbar: not only this is unnecessary, but it is | |
1582 | // also fatal as we'd then try to recreate the toolbar when it's just being | |
1583 | // created | |
1584 | if ( GetToolsCount() && | |
1585 | (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) ) | |
1586 | { | |
1587 | // to remove the text labels, simply re-realizing the toolbar is enough | |
1588 | // but I don't know of any way to add the text to an existing toolbar | |
1589 | // other than by recreating it entirely | |
1590 | Recreate(); | |
1591 | } | |
1592 | } | |
1593 | ||
1594 | // ---------------------------------------------------------------------------- | |
1595 | // tool state | |
1596 | // ---------------------------------------------------------------------------- | |
1597 | ||
1598 | void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable) | |
1599 | { | |
1600 | ::SendMessage(GetHwnd(), TB_ENABLEBUTTON, | |
1601 | (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0)); | |
1602 | } | |
1603 | ||
1604 | void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle) | |
1605 | { | |
1606 | ::SendMessage(GetHwnd(), TB_CHECKBUTTON, | |
1607 | (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0)); | |
1608 | } | |
1609 | ||
1610 | void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)) | |
1611 | { | |
1612 | // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or | |
1613 | // without, so we really need to delete the button and recreate it here | |
1614 | wxFAIL_MSG( wxT("not implemented") ); | |
1615 | } | |
1616 | ||
1617 | void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap ) | |
1618 | { | |
1619 | wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id)); | |
1620 | if ( tool ) | |
1621 | { | |
1622 | wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); | |
1623 | ||
1624 | tool->SetNormalBitmap(bitmap); | |
1625 | Realize(); | |
1626 | } | |
1627 | } | |
1628 | ||
1629 | void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap ) | |
1630 | { | |
1631 | wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id)); | |
1632 | if ( tool ) | |
1633 | { | |
1634 | wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); | |
1635 | ||
1636 | tool->SetDisabledBitmap(bitmap); | |
1637 | Realize(); | |
1638 | } | |
1639 | } | |
1640 | ||
1641 | // ---------------------------------------------------------------------------- | |
1642 | // event handlers | |
1643 | // ---------------------------------------------------------------------------- | |
1644 | ||
1645 | // Responds to colour changes, and passes event on to children. | |
1646 | void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event) | |
1647 | { | |
1648 | wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE)); | |
1649 | ||
1650 | // Remap the buttons | |
1651 | Realize(); | |
1652 | ||
1653 | // Relayout the toolbar | |
1654 | int nrows = m_maxRows; | |
1655 | m_maxRows = 0; // otherwise SetRows() wouldn't do anything | |
1656 | SetRows(nrows); | |
1657 | ||
1658 | Refresh(); | |
1659 | ||
1660 | // let the event propagate further | |
1661 | event.Skip(); | |
1662 | } | |
1663 | ||
1664 | void wxToolBar::OnMouseEvent(wxMouseEvent& event) | |
1665 | { | |
1666 | if ( event.Leaving() ) | |
1667 | { | |
1668 | if ( m_pInTool ) | |
1669 | { | |
1670 | OnMouseEnter(wxID_ANY); | |
1671 | m_pInTool = NULL; | |
1672 | } | |
1673 | ||
1674 | event.Skip(); | |
1675 | return; | |
1676 | } | |
1677 | ||
1678 | if ( event.RightDown() ) | |
1679 | { | |
1680 | // find the tool under the mouse | |
1681 | wxCoord x = 0, y = 0; | |
1682 | event.GetPosition(&x, &y); | |
1683 | ||
1684 | wxToolBarToolBase *tool = FindToolForPosition(x, y); | |
1685 | OnRightClick(tool ? tool->GetId() : -1, x, y); | |
1686 | } | |
1687 | else | |
1688 | { | |
1689 | event.Skip(); | |
1690 | } | |
1691 | } | |
1692 | ||
1693 | // This handler is needed to fix problems with painting the background of | |
1694 | // toolbar icons with comctl32.dll < 6.0. | |
1695 | void wxToolBar::OnEraseBackground(wxEraseEvent& event) | |
1696 | { | |
1697 | #ifdef wxHAS_MSW_BACKGROUND_ERASE_HOOK | |
1698 | MSWDoEraseBackground(event.GetDC()->GetHDC()); | |
1699 | #endif // wxHAS_MSW_BACKGROUND_ERASE_HOOK | |
1700 | } | |
1701 | ||
1702 | bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam) | |
1703 | { | |
1704 | // wait until we have some tools | |
1705 | if ( !GetToolsCount() ) | |
1706 | return false; | |
1707 | ||
1708 | // calculate our minor dimension ourselves - we're confusing the standard | |
1709 | // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks | |
1710 | const RECT r = wxGetTBItemRect(GetHwnd(), 0); | |
1711 | if ( !r.right ) | |
1712 | return false; | |
1713 | ||
1714 | int w, h; | |
1715 | ||
1716 | if ( IsVertical() ) | |
1717 | { | |
1718 | w = r.right - r.left; | |
1719 | if ( m_maxRows ) | |
1720 | { | |
1721 | w *= (m_nButtons + m_maxRows - 1)/m_maxRows; | |
1722 | } | |
1723 | h = HIWORD(lParam); | |
1724 | } | |
1725 | else | |
1726 | { | |
1727 | w = LOWORD(lParam); | |
1728 | if (HasFlag( wxTB_FLAT )) | |
1729 | h = r.bottom - r.top - 3; | |
1730 | else | |
1731 | h = r.bottom - r.top; | |
1732 | if ( m_maxRows ) | |
1733 | { | |
1734 | // FIXME: hardcoded separator line height... | |
1735 | h += HasFlag(wxTB_NODIVIDER) ? 4 : 6; | |
1736 | h *= m_maxRows; | |
1737 | } | |
1738 | } | |
1739 | ||
1740 | if ( MAKELPARAM(w, h) != lParam ) | |
1741 | { | |
1742 | // size really changed | |
1743 | SetSize(w, h); | |
1744 | } | |
1745 | ||
1746 | UpdateStretchableSpacersSize(); | |
1747 | ||
1748 | // message processed | |
1749 | return true; | |
1750 | } | |
1751 | ||
1752 | #ifdef wxHAS_MSW_BACKGROUND_ERASE_HOOK | |
1753 | ||
1754 | bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam) | |
1755 | { | |
1756 | // we must prevent the dummy separators corresponding to controls or | |
1757 | // stretchable spaces from being seen: we used to do it by painting over | |
1758 | // them but this, unsurprisingly, resulted in a lot of flicker so now we | |
1759 | // prevent the toolbar from painting them at all | |
1760 | ||
1761 | // compute the region containing all dummy separators which we don't want | |
1762 | // to be seen | |
1763 | wxRegion rgnDummySeps; | |
1764 | const wxRect rectTotal = GetClientRect(); | |
1765 | int toolIndex = 0; | |
1766 | for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
1767 | node; | |
1768 | node = node->GetNext() ) | |
1769 | { | |
1770 | wxToolBarTool * const | |
1771 | tool = static_cast<wxToolBarTool *>(node->GetData()); | |
1772 | ||
1773 | if ( tool->IsControl() || tool->IsStretchableSpace() ) | |
1774 | { | |
1775 | const size_t numSeps = tool->GetSeparatorsCount(); | |
1776 | for ( size_t n = 0; n < numSeps; n++, toolIndex++ ) | |
1777 | { | |
1778 | // for some reason TB_GETITEMRECT returns a rectangle 1 pixel | |
1779 | // shorter than the full window size (at least under Windows 7) | |
1780 | // but we need to erase the full width/height below | |
1781 | RECT rcItem = wxGetTBItemRect(GetHwnd(), toolIndex); | |
1782 | if ( IsVertical() ) | |
1783 | { | |
1784 | rcItem.left = 0; | |
1785 | rcItem.right = rectTotal.width; | |
1786 | } | |
1787 | else | |
1788 | { | |
1789 | rcItem.top = 0; | |
1790 | rcItem.bottom = rectTotal.height; | |
1791 | } | |
1792 | ||
1793 | rgnDummySeps.Union(wxRectFromRECT(rcItem)); | |
1794 | } | |
1795 | } | |
1796 | else | |
1797 | { | |
1798 | // normal tools never correspond to more than one native button | |
1799 | toolIndex++; | |
1800 | } | |
1801 | } | |
1802 | ||
1803 | if ( rgnDummySeps.IsOk() ) | |
1804 | { | |
1805 | // exclude the area occupied by the controls and stretchable spaces | |
1806 | // from the update region to prevent the toolbar from drawing | |
1807 | // separators in it | |
1808 | if ( !::ValidateRgn(GetHwnd(), GetHrgnOf(rgnDummySeps)) ) | |
1809 | { | |
1810 | wxLogLastError(wxT("ValidateRgn()")); | |
1811 | } | |
1812 | } | |
1813 | ||
1814 | // still let the native control draw everything else normally but set up a | |
1815 | // hook to be able to process the next WM_ERASEBKGND sent to our parent | |
1816 | // because toolbar will ask it to erase its background from its WM_PAINT | |
1817 | // handler (when using TBSTYLE_TRANSPARENT which we do always use) | |
1818 | // | |
1819 | // installing hook is not completely trivial as all kinds of strange | |
1820 | // situations are possible: sometimes we can be called recursively from | |
1821 | // inside the native toolbar WM_PAINT handler so the hook might already be | |
1822 | // installed and sometimes the native toolbar might not send WM_ERASEBKGND | |
1823 | // to the parent at all for whatever reason, so deal with all these cases | |
1824 | wxWindow * const parent = GetParent(); | |
1825 | const bool hadHook = parent->MSWHasEraseBgHook(); | |
1826 | if ( !hadHook ) | |
1827 | GetParent()->MSWSetEraseBgHook(this); | |
1828 | ||
1829 | MSWDefWindowProc(WM_PAINT, wParam, lParam); | |
1830 | ||
1831 | if ( !hadHook ) | |
1832 | GetParent()->MSWSetEraseBgHook(NULL); | |
1833 | ||
1834 | ||
1835 | if ( rgnDummySeps.IsOk() ) | |
1836 | { | |
1837 | // erase the dummy separators region ourselves now as nobody painted | |
1838 | // over them | |
1839 | WindowHDC hdc(GetHwnd()); | |
1840 | ::SelectClipRgn(hdc, GetHrgnOf(rgnDummySeps)); | |
1841 | MSWDoEraseBackground(hdc); | |
1842 | } | |
1843 | ||
1844 | return true; | |
1845 | } | |
1846 | ||
1847 | WXHBRUSH wxToolBar::MSWGetToolbarBgBrush() | |
1848 | { | |
1849 | // we conservatively use a solid brush here but we could also use a themed | |
1850 | // brush by using DrawThemeBackground() to create a bitmap brush (it'd need | |
1851 | // to be invalidated whenever the toolbar is resized and, also, correctly | |
1852 | // aligned using SetBrushOrgEx() before each use -- there is code for doing | |
1853 | // this in wxNotebook already so it'd need to be refactored into wxWindow) | |
1854 | // | |
1855 | // however inasmuch as there is a default background for the toolbar at all | |
1856 | // (and this is not a trivial question as different applications use very | |
1857 | // different colours), it seems to be a solid one and using REBAR | |
1858 | // background brush as we used to do before doesn't look good at all under | |
1859 | // Windows 7 (and probably Vista too), so for now we just keep it simple | |
1860 | wxColour const | |
1861 | colBg = m_hasBgCol ? GetBackgroundColour() | |
1862 | : wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE); | |
1863 | wxBrush * const | |
1864 | brush = wxTheBrushList->FindOrCreateBrush(colBg); | |
1865 | ||
1866 | return brush ? static_cast<WXHBRUSH>(brush->GetResourceHandle()) : 0; | |
1867 | } | |
1868 | ||
1869 | WXHBRUSH wxToolBar::MSWGetBgBrushForChild(WXHDC hDC, wxWindowMSW *child) | |
1870 | { | |
1871 | WXHBRUSH hbr = wxToolBarBase::MSWGetBgBrushForChild(hDC, child); | |
1872 | if ( hbr ) | |
1873 | return hbr; | |
1874 | ||
1875 | // the base class version only returns a brush for erasing children | |
1876 | // background if we have a non-default background colour but as the toolbar | |
1877 | // doesn't erase its own background by default, we need to always do it for | |
1878 | // (semi-)transparent children | |
1879 | if ( child->GetParent() == this && child->HasTransparentBackground() ) | |
1880 | return MSWGetToolbarBgBrush(); | |
1881 | ||
1882 | return 0; | |
1883 | } | |
1884 | ||
1885 | void wxToolBar::MSWDoEraseBackground(WXHDC hDC) | |
1886 | { | |
1887 | wxFillRect(GetHwnd(), (HDC)hDC, (HBRUSH)MSWGetToolbarBgBrush()); | |
1888 | } | |
1889 | ||
1890 | bool wxToolBar::MSWEraseBgHook(WXHDC hDC) | |
1891 | { | |
1892 | // toolbar WM_PAINT handler offsets the DC origin before sending | |
1893 | // WM_ERASEBKGND to the parent but as we handle it in the toolbar itself, | |
1894 | // we need to reset it back | |
1895 | HDC hdc = (HDC)hDC; | |
1896 | POINT ptOldOrg; | |
1897 | if ( !::SetWindowOrgEx(hdc, 0, 0, &ptOldOrg) ) | |
1898 | { | |
1899 | wxLogLastError(wxT("SetWindowOrgEx(tbar-bg-hdc)")); | |
1900 | return false; | |
1901 | } | |
1902 | ||
1903 | MSWDoEraseBackground(hDC); | |
1904 | ||
1905 | ::SetWindowOrgEx(hdc, ptOldOrg.x, ptOldOrg.y, NULL); | |
1906 | ||
1907 | return true; | |
1908 | } | |
1909 | ||
1910 | #endif // wxHAS_MSW_BACKGROUND_ERASE_HOOK | |
1911 | ||
1912 | void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam) | |
1913 | { | |
1914 | wxCoord x = GET_X_LPARAM(lParam), | |
1915 | y = GET_Y_LPARAM(lParam); | |
1916 | wxToolBarToolBase* tool = FindToolForPosition( x, y ); | |
1917 | ||
1918 | // has the current tool changed? | |
1919 | if ( tool != m_pInTool ) | |
1920 | { | |
1921 | m_pInTool = tool; | |
1922 | OnMouseEnter(tool ? tool->GetId() : wxID_ANY); | |
1923 | } | |
1924 | } | |
1925 | ||
1926 | WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
1927 | { | |
1928 | switch ( nMsg ) | |
1929 | { | |
1930 | case WM_MOUSEMOVE: | |
1931 | // we don't handle mouse moves, so always pass the message to | |
1932 | // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter) | |
1933 | HandleMouseMove(wParam, lParam); | |
1934 | break; | |
1935 | ||
1936 | case WM_SIZE: | |
1937 | if ( HandleSize(wParam, lParam) ) | |
1938 | return 0; | |
1939 | break; | |
1940 | ||
1941 | #ifdef wxHAS_MSW_BACKGROUND_ERASE_HOOK | |
1942 | case WM_PAINT: | |
1943 | // refreshing the controls in the toolbar inside a composite window | |
1944 | // results in an endless stream of WM_PAINT messages -- and seems | |
1945 | // to be unnecessary anyhow as everything works just fine without | |
1946 | // any special workarounds in this case | |
1947 | if ( !IsDoubleBuffered() && HandlePaint(wParam, lParam) ) | |
1948 | return 0; | |
1949 | break; | |
1950 | #endif // wxHAS_MSW_BACKGROUND_ERASE_HOOK | |
1951 | ||
1952 | case WM_PRINTCLIENT: | |
1953 | wxFillRect(GetHwnd(), (HDC)wParam, MSWGetToolbarBgBrush()); | |
1954 | return 1; | |
1955 | } | |
1956 | ||
1957 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
1958 | } | |
1959 | ||
1960 | // ---------------------------------------------------------------------------- | |
1961 | // private functions | |
1962 | // ---------------------------------------------------------------------------- | |
1963 | ||
1964 | #ifdef wxREMAP_BUTTON_COLOURS | |
1965 | ||
1966 | WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height) | |
1967 | { | |
1968 | MemoryHDC hdcMem; | |
1969 | ||
1970 | if ( !hdcMem ) | |
1971 | { | |
1972 | wxLogLastError(wxT("CreateCompatibleDC")); | |
1973 | ||
1974 | return bitmap; | |
1975 | } | |
1976 | ||
1977 | SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap); | |
1978 | ||
1979 | if ( !bmpInHDC ) | |
1980 | { | |
1981 | wxLogLastError(wxT("SelectObject")); | |
1982 | ||
1983 | return bitmap; | |
1984 | } | |
1985 | ||
1986 | wxCOLORMAP *cmap = wxGetStdColourMap(); | |
1987 | ||
1988 | for ( int i = 0; i < width; i++ ) | |
1989 | { | |
1990 | for ( int j = 0; j < height; j++ ) | |
1991 | { | |
1992 | COLORREF pixel = ::GetPixel(hdcMem, i, j); | |
1993 | ||
1994 | for ( size_t k = 0; k < wxSTD_COL_MAX; k++ ) | |
1995 | { | |
1996 | COLORREF col = cmap[k].from; | |
1997 | if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 && | |
1998 | abs(GetGValue(pixel) - GetGValue(col)) < 10 && | |
1999 | abs(GetBValue(pixel) - GetBValue(col)) < 10 ) | |
2000 | { | |
2001 | if ( cmap[k].to != pixel ) | |
2002 | ::SetPixel(hdcMem, i, j, cmap[k].to); | |
2003 | break; | |
2004 | } | |
2005 | } | |
2006 | } | |
2007 | } | |
2008 | ||
2009 | return bitmap; | |
2010 | } | |
2011 | ||
2012 | #endif // wxREMAP_BUTTON_COLOURS | |
2013 | ||
2014 | #endif // wxUSE_TOOLBAR |