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