]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
ad9835c9 | 2 | // Name: src/msw/tbar95.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 | ||
2bda0e17 | 29 | #ifndef WX_PRECOMP |
ad9835c9 | 30 | #include "wx/dynarray.h" |
8a0681f9 | 31 | #include "wx/frame.h" |
1c383dba VZ |
32 | #include "wx/log.h" |
33 | #include "wx/intl.h" | |
4e15f6c5 | 34 | #include "wx/settings.h" |
381dd4bf | 35 | #include "wx/bitmap.h" |
f6bcfd97 | 36 | #include "wx/dcmemory.h" |
f538710f | 37 | #include "wx/control.h" |
670f9935 | 38 | #include "wx/app.h" // for GetComCtl32Version |
2bda0e17 KB |
39 | #endif |
40 | ||
8a0681f9 | 41 | #include "wx/toolbar.h" |
dfa53609 | 42 | #include "wx/sysopt.h" |
8d0a7b56 | 43 | #include "wx/image.h" |
2bda0e17 | 44 | |
1c383dba | 45 | #include "wx/msw/private.h" |
2bda0e17 | 46 | |
d679df70 JS |
47 | #if wxUSE_UXTHEME |
48 | #include "wx/msw/uxtheme.h" | |
49 | #endif | |
50 | ||
0d236fd0 VZ |
51 | // include <commctrl.h> "properly" |
52 | #include "wx/msw/wrapcctl.h" | |
e5898961 | 53 | |
fb7e87ef VZ |
54 | // this define controls whether the code for button colours remapping (only |
55 | // useful for 16 or 256 colour images) is active at all, it's always turned off | |
56 | // for CE where it doesn't compile (and is probably not needed anyhow) and may | |
57 | // also be turned off for other systems if you always use 24bpp images and so | |
58 | // never need it | |
59 | #ifndef __WXWINCE__ | |
60 | #define wxREMAP_BUTTON_COLOURS | |
61 | #endif // !__WXWINCE__ | |
62 | ||
1c383dba VZ |
63 | // ---------------------------------------------------------------------------- |
64 | // constants | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
67 | // these standard constants are not always defined in compilers headers | |
2bda0e17 | 68 | |
6a23cbce | 69 | // Styles |
bb6290e3 | 70 | #ifndef TBSTYLE_FLAT |
1c383dba VZ |
71 | #define TBSTYLE_LIST 0x1000 |
72 | #define TBSTYLE_FLAT 0x0800 | |
dd177170 RL |
73 | #endif |
74 | ||
75 | #ifndef TBSTYLE_TRANSPARENT | |
1c383dba | 76 | #define TBSTYLE_TRANSPARENT 0x8000 |
bb6290e3 | 77 | #endif |
bb6290e3 | 78 | |
a3399e6c VZ |
79 | #ifndef TBSTYLE_TOOLTIPS |
80 | #define TBSTYLE_TOOLTIPS 0x0100 | |
81 | #endif | |
82 | ||
6a23cbce JS |
83 | // Messages |
84 | #ifndef TB_GETSTYLE | |
1c383dba | 85 | #define TB_SETSTYLE (WM_USER + 56) |
8a0681f9 VZ |
86 | #define TB_GETSTYLE (WM_USER + 57) |
87 | #endif | |
88 | ||
89 | #ifndef TB_HITTEST | |
90 | #define TB_HITTEST (WM_USER + 69) | |
6a23cbce JS |
91 | #endif |
92 | ||
308bb56f VZ |
93 | #ifndef TB_GETMAXSIZE |
94 | #define TB_GETMAXSIZE (WM_USER + 83) | |
95 | #endif | |
96 | ||
1c383dba | 97 | // these values correspond to those used by comctl32.dll |
81d66cf3 JS |
98 | #define DEFAULTBITMAPX 16 |
99 | #define DEFAULTBITMAPY 15 | |
81d66cf3 | 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; | |
152 | } | |
153 | ||
154 | wxToolBarTool(wxToolBar *tbar, wxControl *control) | |
155 | : wxToolBarToolBase(tbar, control) | |
156 | { | |
157 | m_nSepCount = 1; | |
158 | } | |
159 | ||
c631abda VZ |
160 | virtual void SetLabel(const wxString& label) |
161 | { | |
162 | if ( label == m_label ) | |
163 | return; | |
164 | ||
165 | wxToolBarToolBase::SetLabel(label); | |
166 | ||
167 | // we need to update the label shown in the toolbar because it has a | |
168 | // pointer to the internal buffer of the old label | |
169 | // | |
170 | // TODO: use TB_SETBUTTONINFO | |
171 | } | |
172 | ||
8a0681f9 VZ |
173 | // set/get the number of separators which we use to cover the space used by |
174 | // a control in the toolbar | |
175 | void SetSeparatorsCount(size_t count) { m_nSepCount = count; } | |
176 | size_t GetSeparatorsCount() const { return m_nSepCount; } | |
177 | ||
178 | private: | |
179 | size_t m_nSepCount; | |
2eb10e2a VZ |
180 | |
181 | DECLARE_NO_COPY_CLASS(wxToolBarTool) | |
8a0681f9 VZ |
182 | }; |
183 | ||
184 | ||
1c383dba VZ |
185 | // ============================================================================ |
186 | // implementation | |
187 | // ============================================================================ | |
2bda0e17 | 188 | |
1c383dba | 189 | // ---------------------------------------------------------------------------- |
8a0681f9 | 190 | // wxToolBarTool |
1c383dba VZ |
191 | // ---------------------------------------------------------------------------- |
192 | ||
8a0681f9 | 193 | wxToolBarToolBase *wxToolBar::CreateTool(int id, |
a3399e6c VZ |
194 | const wxString& label, |
195 | const wxBitmap& bmpNormal, | |
196 | const wxBitmap& bmpDisabled, | |
197 | wxItemKind kind, | |
8a0681f9 | 198 | wxObject *clientData, |
a3399e6c VZ |
199 | const wxString& shortHelp, |
200 | const wxString& longHelp) | |
8a0681f9 | 201 | { |
a3399e6c VZ |
202 | return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind, |
203 | clientData, shortHelp, longHelp); | |
8a0681f9 VZ |
204 | } |
205 | ||
206 | wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control) | |
207 | { | |
208 | return new wxToolBarTool(this, control); | |
209 | } | |
210 | ||
211 | // ---------------------------------------------------------------------------- | |
212 | // wxToolBar construction | |
213 | // ---------------------------------------------------------------------------- | |
214 | ||
215 | void wxToolBar::Init() | |
2bda0e17 | 216 | { |
1c383dba | 217 | m_hBitmap = 0; |
8d0a7b56 | 218 | m_disabledImgList = NULL; |
8a0681f9 VZ |
219 | |
220 | m_nButtons = 0; | |
221 | ||
1c383dba VZ |
222 | m_defaultWidth = DEFAULTBITMAPX; |
223 | m_defaultHeight = DEFAULTBITMAPY; | |
37d0bdff MB |
224 | |
225 | m_pInTool = 0; | |
2bda0e17 KB |
226 | } |
227 | ||
8a0681f9 VZ |
228 | bool wxToolBar::Create(wxWindow *parent, |
229 | wxWindowID id, | |
230 | const wxPoint& pos, | |
231 | const wxSize& size, | |
232 | long style, | |
233 | const wxString& name) | |
2bda0e17 | 234 | { |
1c383dba | 235 | // common initialisation |
11b6a93b | 236 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) |
bfbb0b4c | 237 | return false; |
89b892a2 | 238 | |
24998710 | 239 | // MSW-specific initialisation |
278c927d | 240 | if ( !MSWCreateToolbar(pos, size) ) |
bfbb0b4c | 241 | return false; |
b0766406 | 242 | |
9a63feff VZ |
243 | wxSetCCUnicodeFormat(GetHwnd()); |
244 | ||
f6becba5 | 245 | // workaround for flat toolbar on Windows XP classic style: we have to set |
a8cb1a49 | 246 | // the style after creating the control; doing it at creation time doesn't work |
d679df70 | 247 | #if wxUSE_UXTHEME |
6d473712 JS |
248 | if ( style & wxTB_FLAT ) |
249 | { | |
f6becba5 | 250 | LRESULT style = ::SendMessage(GetHwnd(), TB_GETSTYLE, 0, 0L); |
bfbb0b4c | 251 | |
f6becba5 | 252 | if ( !(style & TBSTYLE_FLAT) ) |
f6becba5 | 253 | ::SendMessage(GetHwnd(), TB_SETSTYLE, 0, style | TBSTYLE_FLAT); |
6d473712 | 254 | } |
f6becba5 | 255 | #endif // wxUSE_UXTHEME |
bfbb0b4c WS |
256 | |
257 | return true; | |
24998710 | 258 | } |
2bda0e17 | 259 | |
278c927d | 260 | bool wxToolBar::MSWCreateToolbar(const wxPoint& pos, const wxSize& size) |
24998710 | 261 | { |
fda7962d | 262 | if ( !MSWCreateControl(TOOLBARCLASSNAME, wxEmptyString, pos, size) ) |
bfbb0b4c | 263 | return false; |
2bda0e17 | 264 | |
c8f1f088 | 265 | // toolbar-specific post initialisation |
1c383dba | 266 | ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0); |
bfbb0b4c WS |
267 | |
268 | return true; | |
24998710 | 269 | } |
c8f1f088 | 270 | |
24998710 VZ |
271 | void wxToolBar::Recreate() |
272 | { | |
273 | const HWND hwndOld = GetHwnd(); | |
274 | if ( !hwndOld ) | |
275 | { | |
276 | // we haven't been created yet, no need to recreate | |
277 | return; | |
278 | } | |
2bda0e17 | 279 | |
24998710 VZ |
280 | // get the position and size before unsubclassing the old toolbar |
281 | const wxPoint pos = GetPosition(); | |
282 | const wxSize size = GetSize(); | |
bb6290e3 | 283 | |
24998710 | 284 | UnsubclassWin(); |
2bda0e17 | 285 | |
278c927d | 286 | if ( !MSWCreateToolbar(pos, size) ) |
24998710 VZ |
287 | { |
288 | // what can we do? | |
289 | wxFAIL_MSG( _T("recreating the toolbar failed") ); | |
290 | ||
291 | return; | |
292 | } | |
293 | ||
294 | // reparent all our children under the new toolbar | |
222ed1d6 | 295 | for ( wxWindowList::compatibility_iterator node = m_children.GetFirst(); |
24998710 VZ |
296 | node; |
297 | node = node->GetNext() ) | |
298 | { | |
299 | wxWindow *win = node->GetData(); | |
300 | if ( !win->IsTopLevel() ) | |
301 | ::SetParent(GetHwndOf(win), GetHwnd()); | |
302 | } | |
303 | ||
a8cb1a49 DS |
304 | // only destroy the old toolbar now -- |
305 | // after all the children had been reparented | |
24998710 VZ |
306 | ::DestroyWindow(hwndOld); |
307 | ||
308 | // it is for the old bitmap control and can't be used with the new one | |
309 | if ( m_hBitmap ) | |
310 | { | |
311 | ::DeleteObject((HBITMAP) m_hBitmap); | |
312 | m_hBitmap = 0; | |
313 | } | |
314 | ||
8d0a7b56 VZ |
315 | if ( m_disabledImgList ) |
316 | { | |
317 | delete m_disabledImgList; | |
318 | m_disabledImgList = NULL; | |
319 | } | |
320 | ||
24998710 | 321 | Realize(); |
2bda0e17 KB |
322 | } |
323 | ||
8a0681f9 | 324 | wxToolBar::~wxToolBar() |
2bda0e17 | 325 | { |
f6bcfd97 BP |
326 | // we must refresh the frame size when the toolbar is deleted but the frame |
327 | // is not - otherwise toolbar leaves a hole in the place it used to occupy | |
f6bcfd97 | 328 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); |
c2fd78b1 | 329 | if ( frame && !frame->IsBeingDeleted() ) |
f6bcfd97 | 330 | frame->SendSizeEvent(); |
f6bcfd97 BP |
331 | |
332 | if ( m_hBitmap ) | |
1c383dba | 333 | ::DeleteObject((HBITMAP) m_hBitmap); |
8d0a7b56 VZ |
334 | |
335 | delete m_disabledImgList; | |
1c383dba VZ |
336 | } |
337 | ||
24998710 VZ |
338 | wxSize wxToolBar::DoGetBestSize() const |
339 | { | |
308bb56f VZ |
340 | wxSize sizeBest; |
341 | ||
342 | SIZE size; | |
343 | if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE, 0, (LPARAM)&size) ) | |
344 | { | |
345 | // maybe an old (< 0x400) Windows version? try to approximate the | |
346 | // toolbar size ourselves | |
347 | sizeBest = GetToolSize(); | |
4f6b4292 | 348 | sizeBest.y += 2 * ::GetSystemMetrics(SM_CYBORDER); // Add borders |
308bb56f VZ |
349 | sizeBest.x *= GetToolsCount(); |
350 | ||
351 | // reverse horz and vertical components if necessary | |
352 | if ( HasFlag(wxTB_VERTICAL) ) | |
353 | { | |
354 | int t = sizeBest.x; | |
355 | sizeBest.x = sizeBest.y; | |
356 | sizeBest.y = t; | |
357 | } | |
358 | } | |
359 | else | |
360 | { | |
361 | sizeBest.x = size.cx; | |
362 | sizeBest.y = size.cy; | |
363 | } | |
24998710 | 364 | |
31582e4e | 365 | CacheBestSize(sizeBest); |
97071cdb | 366 | |
308bb56f | 367 | return sizeBest; |
24998710 VZ |
368 | } |
369 | ||
370 | WXDWORD wxToolBar::MSWGetStyle(long style, WXDWORD *exstyle) const | |
371 | { | |
372 | // toolbars never have border, giving one to them results in broken | |
373 | // appearance | |
374 | WXDWORD msStyle = wxControl::MSWGetStyle | |
375 | ( | |
376 | (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle | |
377 | ); | |
378 | ||
97071cdb DS |
379 | // always include this one, it never hurts and setting it later |
380 | // only if we do have tooltips wouldn't work | |
24998710 VZ |
381 | msStyle |= TBSTYLE_TOOLTIPS; |
382 | ||
f9dae779 | 383 | if ( style & (wxTB_FLAT | wxTB_HORZ_LAYOUT) ) |
24998710 VZ |
384 | { |
385 | // static as it doesn't change during the program lifetime | |
2a1f999f | 386 | static int s_verComCtl = wxApp::GetComCtl32Version(); |
24998710 VZ |
387 | |
388 | // comctl32.dll 4.00 doesn't support the flat toolbars and using this | |
389 | // style with 6.00 (part of Windows XP) leads to the toolbar with | |
390 | // incorrect background colour - and not using it still results in the | |
391 | // correct (flat) toolbar, so don't use it there | |
392 | if ( s_verComCtl > 400 && s_verComCtl < 600 ) | |
24998710 | 393 | msStyle |= TBSTYLE_FLAT | TBSTYLE_TRANSPARENT; |
f9dae779 VZ |
394 | |
395 | if ( s_verComCtl >= 470 && style & wxTB_HORZ_LAYOUT ) | |
f9dae779 | 396 | msStyle |= TBSTYLE_LIST; |
24998710 VZ |
397 | } |
398 | ||
399 | if ( style & wxTB_NODIVIDER ) | |
400 | msStyle |= CCS_NODIVIDER; | |
401 | ||
402 | if ( style & wxTB_NOALIGN ) | |
403 | msStyle |= CCS_NOPARENTALIGN; | |
404 | ||
3bb63e5c JS |
405 | if ( style & wxTB_VERTICAL ) |
406 | msStyle |= CCS_VERT; | |
407 | ||
24998710 VZ |
408 | return msStyle; |
409 | } | |
410 | ||
bdc72a22 | 411 | // ---------------------------------------------------------------------------- |
8a0681f9 | 412 | // adding/removing tools |
bdc72a22 VZ |
413 | // ---------------------------------------------------------------------------- |
414 | ||
24998710 | 415 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool) |
1c383dba | 416 | { |
8a0681f9 VZ |
417 | // nothing special to do here - we really create the toolbar buttons in |
418 | // Realize() later | |
419 | tool->Attach(this); | |
420 | ||
9f884528 | 421 | InvalidateBestSize(); |
bfbb0b4c | 422 | return true; |
1c383dba VZ |
423 | } |
424 | ||
8a0681f9 | 425 | bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool) |
bdc72a22 | 426 | { |
f6bcfd97 BP |
427 | // the main difficulty we have here is with the controls in the toolbars: |
428 | // as we (sometimes) use several separators to cover up the space used by | |
429 | // them, the indices are not the same for us and the toolbar | |
430 | ||
431 | // first determine the position of the first button to delete: it may be | |
432 | // different from pos if we use several separators to cover the space used | |
433 | // by a control | |
222ed1d6 | 434 | wxToolBarToolsList::compatibility_iterator node; |
f6bcfd97 BP |
435 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) |
436 | { | |
437 | wxToolBarToolBase *tool2 = node->GetData(); | |
438 | if ( tool2 == tool ) | |
439 | { | |
440 | // let node point to the next node in the list | |
441 | node = node->GetNext(); | |
442 | ||
443 | break; | |
444 | } | |
445 | ||
446 | if ( tool2->IsControl() ) | |
56bd6aac | 447 | pos += ((wxToolBarTool *)tool2)->GetSeparatorsCount() - 1; |
f6bcfd97 BP |
448 | } |
449 | ||
450 | // now determine the number of buttons to delete and the area taken by them | |
8a0681f9 | 451 | size_t nButtonsToDelete = 1; |
bdc72a22 | 452 | |
8a0681f9 VZ |
453 | // get the size of the button we're going to delete |
454 | RECT r; | |
455 | if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, pos, (LPARAM)&r) ) | |
bdc72a22 | 456 | { |
8a0681f9 | 457 | wxLogLastError(_T("TB_GETITEMRECT")); |
bdc72a22 VZ |
458 | } |
459 | ||
8a0681f9 | 460 | int width = r.right - r.left; |
bdc72a22 | 461 | |
8a0681f9 VZ |
462 | if ( tool->IsControl() ) |
463 | { | |
464 | nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount(); | |
8a0681f9 | 465 | width *= nButtonsToDelete; |
a3e9caa2 | 466 | tool->GetControl()->Destroy(); |
8a0681f9 | 467 | } |
bdc72a22 | 468 | |
f6bcfd97 BP |
469 | // do delete all buttons |
470 | m_nButtons -= nButtonsToDelete; | |
8a0681f9 VZ |
471 | while ( nButtonsToDelete-- > 0 ) |
472 | { | |
473 | if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) ) | |
474 | { | |
f6bcfd97 | 475 | wxLogLastError(wxT("TB_DELETEBUTTON")); |
1c383dba | 476 | |
bfbb0b4c | 477 | return false; |
8a0681f9 VZ |
478 | } |
479 | } | |
1c383dba | 480 | |
8a0681f9 | 481 | tool->Detach(); |
1c383dba | 482 | |
f6bcfd97 BP |
483 | // and finally reposition all the controls after this button (the toolbar |
484 | // takes care of all normal items) | |
485 | for ( /* node -> first after deleted */ ; node; node = node->GetNext() ) | |
8a0681f9 VZ |
486 | { |
487 | wxToolBarToolBase *tool2 = node->GetData(); | |
488 | if ( tool2->IsControl() ) | |
489 | { | |
490 | int x; | |
491 | wxControl *control = tool2->GetControl(); | |
492 | control->GetPosition(&x, NULL); | |
bfbb0b4c | 493 | control->Move(x - width, wxDefaultCoord); |
8a0681f9 VZ |
494 | } |
495 | } | |
1c383dba | 496 | |
9f884528 | 497 | InvalidateBestSize(); |
97071cdb | 498 | |
bfbb0b4c | 499 | return true; |
1c383dba VZ |
500 | } |
501 | ||
8d0a7b56 VZ |
502 | void wxToolBar::CreateDisabledImageList() |
503 | { | |
2c622593 DS |
504 | if (m_disabledImgList != NULL) |
505 | { | |
506 | delete m_disabledImgList; | |
507 | m_disabledImgList = NULL; | |
508 | } | |
509 | ||
8d0a7b56 VZ |
510 | // as we can't use disabled image list with older versions of comctl32.dll, |
511 | // don't even bother creating it | |
5625d0d4 | 512 | if ( wxApp::GetComCtl32Version() >= 470 ) |
8d0a7b56 VZ |
513 | { |
514 | // search for the first disabled button img in the toolbar, if any | |
515 | for ( wxToolBarToolsList::compatibility_iterator | |
516 | node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
517 | { | |
518 | wxToolBarToolBase *tool = node->GetData(); | |
519 | wxBitmap bmpDisabled = tool->GetDisabledBitmap(); | |
520 | if ( bmpDisabled.Ok() ) | |
521 | { | |
522 | m_disabledImgList = new wxImageList | |
523 | ( | |
524 | m_defaultWidth, | |
525 | m_defaultHeight, | |
526 | bmpDisabled.GetMask() != NULL, | |
527 | GetToolsCount() | |
528 | ); | |
2c622593 | 529 | break; |
8d0a7b56 VZ |
530 | } |
531 | } | |
532 | ||
533 | // we don't have any disabled bitmaps | |
534 | } | |
8d0a7b56 VZ |
535 | } |
536 | ||
8a0681f9 | 537 | bool wxToolBar::Realize() |
1c383dba | 538 | { |
24998710 | 539 | const size_t nTools = GetToolsCount(); |
8a0681f9 | 540 | if ( nTools == 0 ) |
8a0681f9 | 541 | // nothing to do |
bfbb0b4c | 542 | return true; |
1c383dba | 543 | |
24998710 | 544 | const bool isVertical = HasFlag(wxTB_VERTICAL); |
2bda0e17 | 545 | |
fb7e87ef VZ |
546 | #ifdef wxREMAP_BUTTON_COLOURS |
547 | // don't change the values of these constants, they can be set from the | |
548 | // user code via wxSystemOptions | |
549 | enum | |
92661f97 | 550 | { |
fb7e87ef VZ |
551 | Remap_None = -1, |
552 | Remap_Bg, | |
553 | Remap_Buttons, | |
554 | Remap_TransparentBg | |
555 | }; | |
556 | ||
557 | // the user-specified option overrides anything, but if it wasn't set, only | |
558 | // remap the buttons on 8bpp displays as otherwise the bitmaps usually look | |
559 | // much worse after remapping | |
560 | static const wxChar *remapOption = wxT("msw.remap"); | |
561 | const int remapValue = wxSystemOptions::HasOption(remapOption) | |
562 | ? wxSystemOptions::GetOptionInt(remapOption) | |
563 | : wxDisplayDepth() <= 8 ? Remap_Buttons | |
564 | : Remap_None; | |
565 | ||
566 | #endif // wxREMAP_BUTTON_COLOURS | |
92661f97 | 567 | |
2b5f62a0 VZ |
568 | // delete all old buttons, if any |
569 | for ( size_t pos = 0; pos < m_nButtons; pos++ ) | |
570 | { | |
571 | if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) ) | |
572 | { | |
573 | wxLogDebug(wxT("TB_DELETEBUTTON failed")); | |
574 | } | |
575 | } | |
576 | ||
8a0681f9 VZ |
577 | // First, add the bitmap: we use one bitmap for all toolbar buttons |
578 | // ---------------------------------------------------------------- | |
2bda0e17 | 579 | |
222ed1d6 | 580 | wxToolBarToolsList::compatibility_iterator node; |
24998710 VZ |
581 | int bitmapId = 0; |
582 | ||
583 | wxSize sizeBmp; | |
584 | if ( HasFlag(wxTB_NOICONS) ) | |
585 | { | |
586 | // no icons, don't leave space for them | |
587 | sizeBmp.x = | |
588 | sizeBmp.y = 0; | |
589 | } | |
590 | else // do show icons | |
591 | { | |
592 | // if we already have a bitmap, we'll replace the existing one -- | |
593 | // otherwise we'll install a new one | |
594 | HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap; | |
595 | ||
596 | sizeBmp.x = m_defaultWidth; | |
597 | sizeBmp.y = m_defaultHeight; | |
89b892a2 | 598 | |
4a10ea8b MW |
599 | const wxCoord totalBitmapWidth = m_defaultWidth * |
600 | wx_truncate_cast(wxCoord, nTools), | |
24998710 | 601 | totalBitmapHeight = m_defaultHeight; |
2bda0e17 | 602 | |
17cfea01 | 603 | // Create a bitmap and copy all the tool bitmaps into it |
24998710 VZ |
604 | wxMemoryDC dcAllButtons; |
605 | wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight); | |
606 | dcAllButtons.SelectObject(bitmap); | |
bb86baea | 607 | |
fb7e87ef VZ |
608 | #ifdef wxREMAP_BUTTON_COLOURS |
609 | if ( remapValue != Remap_TransparentBg ) | |
610 | #endif // wxREMAP_BUTTON_COLOURS | |
611 | { | |
612 | // VZ: why do we hardcode grey colour for CE? | |
613 | dcAllButtons.SetBackground(wxBrush( | |
614 | #ifdef __WXWINCE__ | |
615 | wxColour(0xc0, 0xc0, 0xc0) | |
616 | #else // !__WXWINCE__ | |
617 | GetBackgroundColour() | |
618 | #endif // __WXWINCE__/!__WXWINCE__ | |
619 | )); | |
620 | dcAllButtons.Clear(); | |
621 | } | |
24998710 VZ |
622 | |
623 | m_hBitmap = bitmap.GetHBITMAP(); | |
624 | HBITMAP hBitmap = (HBITMAP)m_hBitmap; | |
2bda0e17 | 625 | |
fb7e87ef VZ |
626 | #ifdef wxREMAP_BUTTON_COLOURS |
627 | if ( remapValue == Remap_Bg ) | |
dfa53609 | 628 | { |
dfa53609 | 629 | dcAllButtons.SelectObject(wxNullBitmap); |
dfa53609 JS |
630 | |
631 | // Even if we're not remapping the bitmap | |
632 | // content, we still have to remap the background. | |
633 | hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap, | |
634 | totalBitmapWidth, totalBitmapHeight); | |
635 | ||
dfa53609 | 636 | dcAllButtons.SelectObject(bitmap); |
dfa53609 | 637 | } |
fb7e87ef | 638 | #endif // wxREMAP_BUTTON_COLOURS |
dfa53609 | 639 | |
24998710 VZ |
640 | // the button position |
641 | wxCoord x = 0; | |
2bda0e17 | 642 | |
24998710 VZ |
643 | // the number of buttons (not separators) |
644 | int nButtons = 0; | |
1c383dba | 645 | |
8d0a7b56 | 646 | CreateDisabledImageList(); |
4769a562 | 647 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) |
1c383dba | 648 | { |
24998710 VZ |
649 | wxToolBarToolBase *tool = node->GetData(); |
650 | if ( tool->IsButton() ) | |
1c383dba | 651 | { |
24998710 | 652 | const wxBitmap& bmp = tool->GetNormalBitmap(); |
ba700b5c VZ |
653 | |
654 | const int w = bmp.GetWidth(); | |
655 | const int h = bmp.GetHeight(); | |
656 | ||
24998710 VZ |
657 | if ( bmp.Ok() ) |
658 | { | |
ba700b5c VZ |
659 | int xOffset = wxMax(0, (m_defaultWidth - w)/2); |
660 | int yOffset = wxMax(0, (m_defaultHeight - h)/2); | |
661 | ||
24998710 | 662 | // notice the last parameter: do use mask |
ba700b5c | 663 | dcAllButtons.DrawBitmap(bmp, x + xOffset, yOffset, true); |
24998710 VZ |
664 | } |
665 | else | |
1c383dba | 666 | { |
24998710 | 667 | wxFAIL_MSG( _T("invalid tool button bitmap") ); |
1c383dba VZ |
668 | } |
669 | ||
8d0a7b56 VZ |
670 | // also deal with disabled bitmap if we want to use them |
671 | if ( m_disabledImgList ) | |
672 | { | |
673 | wxBitmap bmpDisabled = tool->GetDisabledBitmap(); | |
64c288fa | 674 | #if wxUSE_IMAGE && wxUSE_WXDIB |
8d0a7b56 VZ |
675 | if ( !bmpDisabled.Ok() ) |
676 | { | |
8d0a7b56 VZ |
677 | // no disabled bitmap specified but we still need to |
678 | // fill the space in the image list with something, so | |
679 | // we grey out the normal bitmap | |
8d0a7b56 | 680 | wxImage imgGreyed; |
ba700b5c VZ |
681 | wxCreateGreyedImage(bmp.ConvertToImage(), imgGreyed); |
682 | ||
fb7e87ef VZ |
683 | #ifdef wxREMAP_BUTTON_COLOURS |
684 | if ( remapValue == Remap_Buttons ) | |
ba700b5c | 685 | { |
0f99ce3d DS |
686 | // we need to have light grey background colour for |
687 | // MapBitmap() to work correctly | |
688 | for ( int y = 0; y < h; y++ ) | |
ba700b5c | 689 | { |
0f99ce3d DS |
690 | for ( int x = 0; x < w; x++ ) |
691 | { | |
692 | if ( imgGreyed.IsTransparent(x, y) ) | |
693 | imgGreyed.SetRGB(x, y, | |
694 | wxLIGHT_GREY->Red(), | |
695 | wxLIGHT_GREY->Green(), | |
696 | wxLIGHT_GREY->Blue()); | |
697 | } | |
ba700b5c VZ |
698 | } |
699 | } | |
fb7e87ef | 700 | #endif // wxREMAP_BUTTON_COLOURS |
8d0a7b56 VZ |
701 | |
702 | bmpDisabled = wxBitmap(imgGreyed); | |
8d0a7b56 | 703 | } |
ba700b5c VZ |
704 | #endif // wxUSE_IMAGE |
705 | ||
fb7e87ef VZ |
706 | #ifdef wxREMAP_BUTTON_COLOURS |
707 | if ( remapValue == Remap_Buttons ) | |
0f99ce3d | 708 | MapBitmap(bmpDisabled.GetHBITMAP(), w, h); |
fb7e87ef | 709 | #endif // wxREMAP_BUTTON_COLOURS |
8d0a7b56 VZ |
710 | |
711 | m_disabledImgList->Add(bmpDisabled); | |
712 | } | |
713 | ||
24998710 VZ |
714 | // still inc width and number of buttons because otherwise the |
715 | // subsequent buttons will all be shifted which is rather confusing | |
716 | // (and like this you'd see immediately which bitmap was bad) | |
717 | x += m_defaultWidth; | |
718 | nButtons++; | |
1c383dba VZ |
719 | } |
720 | } | |
8a0681f9 | 721 | |
24998710 | 722 | dcAllButtons.SelectObject(wxNullBitmap); |
f6bcfd97 | 723 | |
24998710 VZ |
724 | // don't delete this HBITMAP! |
725 | bitmap.SetHBITMAP(0); | |
2bda0e17 | 726 | |
fb7e87ef VZ |
727 | #ifdef wxREMAP_BUTTON_COLOURS |
728 | if ( remapValue == Remap_Buttons ) | |
dfa53609 JS |
729 | { |
730 | // Map to system colours | |
731 | hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap, | |
fb7e87ef | 732 | totalBitmapWidth, totalBitmapHeight); |
dfa53609 | 733 | } |
fb7e87ef | 734 | #endif // wxREMAP_BUTTON_COLOURS |
1c383dba | 735 | |
bfbb0b4c | 736 | bool addBitmap = true; |
9f83044f | 737 | |
24998710 | 738 | if ( oldToolBarBitmap ) |
1c383dba | 739 | { |
24998710 | 740 | #ifdef TB_REPLACEBITMAP |
2a1f999f | 741 | if ( wxApp::GetComCtl32Version() >= 400 ) |
9f83044f | 742 | { |
24998710 VZ |
743 | TBREPLACEBITMAP replaceBitmap; |
744 | replaceBitmap.hInstOld = NULL; | |
745 | replaceBitmap.hInstNew = NULL; | |
746 | replaceBitmap.nIDOld = (UINT) oldToolBarBitmap; | |
747 | replaceBitmap.nIDNew = (UINT) hBitmap; | |
748 | replaceBitmap.nButtons = nButtons; | |
749 | if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP, | |
750 | 0, (LPARAM) &replaceBitmap) ) | |
751 | { | |
752 | wxFAIL_MSG(wxT("Could not replace the old bitmap")); | |
753 | } | |
9f83044f | 754 | |
24998710 | 755 | ::DeleteObject(oldToolBarBitmap); |
9f83044f | 756 | |
24998710 | 757 | // already done |
bfbb0b4c | 758 | addBitmap = false; |
24998710 VZ |
759 | } |
760 | else | |
9f83044f | 761 | #endif // TB_REPLACEBITMAP |
24998710 VZ |
762 | { |
763 | // we can't replace the old bitmap, so we will add another one | |
764 | // (awfully inefficient, but what else to do?) and shift the bitmap | |
765 | // indices accordingly | |
bfbb0b4c | 766 | addBitmap = true; |
1c383dba | 767 | |
24998710 VZ |
768 | bitmapId = m_nButtons; |
769 | } | |
9f83044f | 770 | } |
1c383dba | 771 | |
24998710 | 772 | if ( addBitmap ) // no old bitmap or we can't replace it |
1c383dba | 773 | { |
24998710 VZ |
774 | TBADDBITMAP addBitmap; |
775 | addBitmap.hInst = 0; | |
776 | addBitmap.nID = (UINT) hBitmap; | |
777 | if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP, | |
778 | (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 ) | |
8a0681f9 | 779 | { |
24998710 | 780 | wxFAIL_MSG(wxT("Could not add bitmap to toolbar")); |
8a0681f9 | 781 | } |
1c383dba | 782 | } |
8d0a7b56 VZ |
783 | |
784 | if ( m_disabledImgList ) | |
785 | { | |
786 | HIMAGELIST oldImageList = (HIMAGELIST) | |
787 | ::SendMessage(GetHwnd(), | |
788 | TB_SETDISABLEDIMAGELIST, | |
789 | 0, | |
790 | (LPARAM)GetHimagelistOf(m_disabledImgList)); | |
791 | ||
792 | // delete previous image list if any | |
793 | if ( oldImageList ) | |
794 | ::DeleteObject( oldImageList ); | |
795 | } | |
2bda0e17 | 796 | } |
9f83044f | 797 | |
24998710 VZ |
798 | // don't call SetToolBitmapSize() as we don't want to change the values of |
799 | // m_defaultWidth/Height | |
800 | if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, | |
801 | MAKELONG(sizeBmp.x, sizeBmp.y)) ) | |
051205e6 | 802 | { |
24998710 | 803 | wxLogLastError(_T("TB_SETBITMAPSIZE")); |
051205e6 | 804 | } |
2bda0e17 | 805 | |
8a0681f9 VZ |
806 | // Next add the buttons and separators |
807 | // ----------------------------------- | |
808 | ||
1c383dba | 809 | TBBUTTON *buttons = new TBBUTTON[nTools]; |
2bda0e17 | 810 | |
8a0681f9 | 811 | // this array will hold the indices of all controls in the toolbar |
1c383dba VZ |
812 | wxArrayInt controlIds; |
813 | ||
bfbb0b4c | 814 | bool lastWasRadio = false; |
1c383dba | 815 | int i = 0; |
4769a562 | 816 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) |
2bda0e17 | 817 | { |
8a0681f9 VZ |
818 | wxToolBarToolBase *tool = node->GetData(); |
819 | ||
28603488 VZ |
820 | // don't add separators to the vertical toolbar with old comctl32.dll |
821 | // versions as they didn't handle this properly | |
822 | if ( isVertical && tool->IsSeparator() && | |
2a1f999f | 823 | wxApp::GetComCtl32Version() <= 472 ) |
28603488 VZ |
824 | { |
825 | continue; | |
826 | } | |
827 | ||
1c383dba VZ |
828 | TBBUTTON& button = buttons[i]; |
829 | ||
830 | wxZeroMemory(button); | |
831 | ||
bfbb0b4c | 832 | bool isRadio = false; |
8a0681f9 | 833 | switch ( tool->GetStyle() ) |
1c383dba VZ |
834 | { |
835 | case wxTOOL_STYLE_CONTROL: | |
8a0681f9 | 836 | button.idCommand = tool->GetId(); |
1c383dba VZ |
837 | // fall through: create just a separator too |
838 | ||
839 | case wxTOOL_STYLE_SEPARATOR: | |
840 | button.fsState = TBSTATE_ENABLED; | |
841 | button.fsStyle = TBSTYLE_SEP; | |
842 | break; | |
843 | ||
844 | case wxTOOL_STYLE_BUTTON: | |
24998710 VZ |
845 | if ( !HasFlag(wxTB_NOICONS) ) |
846 | button.iBitmap = bitmapId; | |
c631abda | 847 | |
24998710 | 848 | if ( HasFlag(wxTB_TEXT) ) |
c631abda | 849 | { |
24998710 VZ |
850 | const wxString& label = tool->GetLabel(); |
851 | if ( !label.empty() ) | |
24998710 | 852 | button.iString = (int)label.c_str(); |
c631abda VZ |
853 | } |
854 | ||
8a0681f9 | 855 | button.idCommand = tool->GetId(); |
1c383dba | 856 | |
8a0681f9 | 857 | if ( tool->IsEnabled() ) |
1c383dba | 858 | button.fsState |= TBSTATE_ENABLED; |
8a0681f9 | 859 | if ( tool->IsToggled() ) |
1c383dba | 860 | button.fsState |= TBSTATE_CHECKED; |
8a0681f9 | 861 | |
c631abda VZ |
862 | switch ( tool->GetKind() ) |
863 | { | |
864 | case wxITEM_RADIO: | |
865 | button.fsStyle = TBSTYLE_CHECKGROUP; | |
866 | ||
867 | if ( !lastWasRadio ) | |
868 | { | |
869 | // the first item in the radio group is checked by | |
870 | // default to be consistent with wxGTK and the menu | |
871 | // radio items | |
872 | button.fsState |= TBSTATE_CHECKED; | |
f3ba93c1 | 873 | |
730d3366 RD |
874 | if (tool->Toggle(true)) |
875 | { | |
876 | DoToggleTool(tool, true); | |
877 | } | |
878 | } | |
879 | else if (tool->IsToggled()) | |
880 | { | |
881 | wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious(); | |
882 | int prevIndex = i - 1; | |
883 | while ( nodePrev ) | |
884 | { | |
885 | TBBUTTON& prevButton = buttons[prevIndex]; | |
886 | wxToolBarToolBase *tool = nodePrev->GetData(); | |
887 | if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO ) | |
888 | break; | |
ed7fdb86 | 889 | |
730d3366 | 890 | if ( tool->Toggle(false) ) |
730d3366 | 891 | DoToggleTool(tool, false); |
a8cb1a49 | 892 | |
730d3366 RD |
893 | prevButton.fsState = TBSTATE_ENABLED; |
894 | nodePrev = nodePrev->GetPrevious(); | |
895 | prevIndex--; | |
ed7fdb86 | 896 | } |
c631abda VZ |
897 | } |
898 | ||
bfbb0b4c | 899 | isRadio = true; |
c631abda VZ |
900 | break; |
901 | ||
902 | case wxITEM_CHECK: | |
903 | button.fsStyle = TBSTYLE_CHECK; | |
904 | break; | |
905 | ||
97071cdb DS |
906 | case wxITEM_NORMAL: |
907 | button.fsStyle = TBSTYLE_BUTTON; | |
908 | break; | |
909 | ||
c631abda VZ |
910 | default: |
911 | wxFAIL_MSG( _T("unexpected toolbar button kind") ); | |
c631abda | 912 | button.fsStyle = TBSTYLE_BUTTON; |
97071cdb | 913 | break; |
c631abda | 914 | } |
1c383dba VZ |
915 | |
916 | bitmapId++; | |
917 | break; | |
918 | } | |
2bda0e17 | 919 | |
c631abda VZ |
920 | lastWasRadio = isRadio; |
921 | ||
1c383dba | 922 | i++; |
2bda0e17 | 923 | } |
1c383dba | 924 | |
a3399e6c | 925 | if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, (WPARAM)i, (LPARAM)buttons) ) |
2bda0e17 | 926 | { |
f6bcfd97 | 927 | wxLogLastError(wxT("TB_ADDBUTTONS")); |
2bda0e17 | 928 | } |
89b892a2 | 929 | |
1c383dba | 930 | delete [] buttons; |
2bda0e17 | 931 | |
8a0681f9 VZ |
932 | // Deal with the controls finally |
933 | // ------------------------------ | |
934 | ||
1c383dba | 935 | // adjust the controls size to fit nicely in the toolbar |
34e5028f | 936 | int y = 0; |
8a0681f9 VZ |
937 | size_t index = 0; |
938 | for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ ) | |
1c383dba | 939 | { |
8a0681f9 | 940 | wxToolBarToolBase *tool = node->GetData(); |
1c383dba | 941 | |
34e5028f VZ |
942 | // we calculate the running y coord for vertical toolbars so we need to |
943 | // get the items size for all items but for the horizontal ones we | |
944 | // don't need to deal with the non controls | |
945 | bool isControl = tool->IsControl(); | |
946 | if ( !isControl && !isVertical ) | |
947 | continue; | |
bdc72a22 | 948 | |
8a0681f9 VZ |
949 | // note that we use TB_GETITEMRECT and not TB_GETRECT because the |
950 | // latter only appeared in v4.70 of comctl32.dll | |
951 | RECT r; | |
bfbb0b4c WS |
952 | if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, |
953 | index, (LPARAM)(LPRECT)&r) ) | |
8a0681f9 | 954 | { |
f6bcfd97 | 955 | wxLogLastError(wxT("TB_GETITEMRECT")); |
8a0681f9 VZ |
956 | } |
957 | ||
34e5028f VZ |
958 | if ( !isControl ) |
959 | { | |
960 | // can only be control if isVertical | |
961 | y += r.bottom - r.top; | |
962 | ||
963 | continue; | |
964 | } | |
965 | ||
966 | wxControl *control = tool->GetControl(); | |
34e5028f VZ |
967 | wxSize size = control->GetSize(); |
968 | ||
969 | // the position of the leftmost controls corner | |
bfbb0b4c | 970 | int left = wxDefaultCoord; |
34e5028f | 971 | |
bdc72a22 | 972 | // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+ |
5b59df83 | 973 | #ifdef TB_SETBUTTONINFO |
34e5028f VZ |
974 | // available in headers, now check whether it is available now |
975 | // (during run-time) | |
2a1f999f | 976 | if ( wxApp::GetComCtl32Version() >= 471 ) |
34e5028f VZ |
977 | { |
978 | // set the (underlying) separators width to be that of the | |
979 | // control | |
980 | TBBUTTONINFO tbbi; | |
981 | tbbi.cbSize = sizeof(tbbi); | |
982 | tbbi.dwMask = TBIF_SIZE; | |
5c519b6c | 983 | tbbi.cx = (WORD)size.x; |
bfbb0b4c WS |
984 | if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO, |
985 | tool->GetId(), (LPARAM)&tbbi) ) | |
bdc72a22 | 986 | { |
34e5028f VZ |
987 | // the id is probably invalid? |
988 | wxLogLastError(wxT("TB_SETBUTTONINFO")); | |
bdc72a22 | 989 | } |
34e5028f VZ |
990 | } |
991 | else | |
992 | #endif // comctl32.dll 4.71 | |
993 | // TB_SETBUTTONINFO unavailable | |
994 | { | |
995 | // try adding several separators to fit the controls width | |
996 | int widthSep = r.right - r.left; | |
997 | left = r.left; | |
998 | ||
999 | TBBUTTON tbb; | |
1000 | wxZeroMemory(tbb); | |
1001 | tbb.idCommand = 0; | |
1002 | tbb.fsState = TBSTATE_ENABLED; | |
1003 | tbb.fsStyle = TBSTYLE_SEP; | |
1004 | ||
1005 | size_t nSeparators = size.x / widthSep; | |
1006 | for ( size_t nSep = 0; nSep < nSeparators; nSep++ ) | |
bdc72a22 | 1007 | { |
bfbb0b4c WS |
1008 | if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON, |
1009 | index, (LPARAM)&tbb) ) | |
bdc72a22 | 1010 | { |
34e5028f | 1011 | wxLogLastError(wxT("TB_INSERTBUTTON")); |
bdc72a22 VZ |
1012 | } |
1013 | ||
34e5028f | 1014 | index++; |
bdc72a22 | 1015 | } |
1c383dba | 1016 | |
34e5028f VZ |
1017 | // remember the number of separators we used - we'd have to |
1018 | // delete all of them later | |
1019 | ((wxToolBarTool *)tool)->SetSeparatorsCount(nSeparators); | |
1020 | ||
1021 | // adjust the controls width to exactly cover the separators | |
bfbb0b4c | 1022 | control->SetSize((nSeparators + 1)*widthSep, wxDefaultCoord); |
34e5028f VZ |
1023 | } |
1024 | ||
1025 | // position the control itself correctly vertically | |
1c383dba VZ |
1026 | int height = r.bottom - r.top; |
1027 | int diff = height - size.y; | |
1028 | if ( diff < 0 ) | |
1029 | { | |
1030 | // the control is too high, resize to fit | |
bfbb0b4c | 1031 | control->SetSize(wxDefaultCoord, height - 2); |
1c383dba VZ |
1032 | |
1033 | diff = 2; | |
1034 | } | |
2bda0e17 | 1035 | |
34e5028f VZ |
1036 | int top; |
1037 | if ( isVertical ) | |
1038 | { | |
1039 | left = 0; | |
1040 | top = y; | |
1041 | ||
97071cdb | 1042 | y += height + 2 * GetMargins().y; |
34e5028f VZ |
1043 | } |
1044 | else // horizontal toolbar | |
1045 | { | |
bfbb0b4c | 1046 | if ( left == wxDefaultCoord ) |
34e5028f VZ |
1047 | left = r.left; |
1048 | ||
1049 | top = r.top; | |
1050 | } | |
1051 | ||
1052 | control->Move(left, top + (diff + 1) / 2); | |
1c383dba | 1053 | } |
89b892a2 | 1054 | |
8a0681f9 VZ |
1055 | // the max index is the "real" number of buttons - i.e. counting even the |
1056 | // separators which we added just for aligning the controls | |
1057 | m_nButtons = index; | |
89b892a2 | 1058 | |
8a0681f9 VZ |
1059 | if ( !isVertical ) |
1060 | { | |
1061 | if ( m_maxRows == 0 ) | |
8a0681f9 VZ |
1062 | // if not set yet, only one row |
1063 | SetRows(1); | |
8a0681f9 VZ |
1064 | } |
1065 | else if ( m_nButtons > 0 ) // vertical non empty toolbar | |
1066 | { | |
1067 | if ( m_maxRows == 0 ) | |
8a0681f9 VZ |
1068 | // if not set yet, have one column |
1069 | SetRows(m_nButtons); | |
8a0681f9 | 1070 | } |
2bda0e17 | 1071 | |
9f884528 | 1072 | InvalidateBestSize(); |
2091f5e7 | 1073 | UpdateSize(); |
c6dcd973 | 1074 | |
bfbb0b4c | 1075 | return true; |
2bda0e17 KB |
1076 | } |
1077 | ||
1c383dba VZ |
1078 | // ---------------------------------------------------------------------------- |
1079 | // message handlers | |
1080 | // ---------------------------------------------------------------------------- | |
1081 | ||
33ac7e6f | 1082 | bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id) |
2bda0e17 | 1083 | { |
8a0681f9 VZ |
1084 | wxToolBarToolBase *tool = FindById((int)id); |
1085 | if ( !tool ) | |
bfbb0b4c | 1086 | return false; |
1c383dba | 1087 | |
1bba1946 | 1088 | bool toggled = false; // just to suppress warnings |
6bb7cee4 | 1089 | |
8a0681f9 | 1090 | if ( tool->CanBeToggled() ) |
1c383dba VZ |
1091 | { |
1092 | LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0); | |
6bb7cee4 VZ |
1093 | toggled = (state & TBSTATE_CHECKED) != 0; |
1094 | ||
0356c259 VZ |
1095 | // ignore the event when a radio button is released, as this doesn't |
1096 | // seem to happen at all, and is handled otherwise | |
6bb7cee4 | 1097 | if ( tool->GetKind() == wxITEM_RADIO && !toggled ) |
bfbb0b4c | 1098 | return true; |
1c383dba | 1099 | |
6bb7cee4 VZ |
1100 | tool->Toggle(toggled); |
1101 | UnToggleRadioGroup(tool); | |
1102 | } | |
8a0681f9 | 1103 | |
6bb7cee4 VZ |
1104 | // OnLeftClick() can veto the button state change - for buttons which |
1105 | // may be toggled only, of couse | |
1106 | if ( !OnLeftClick((int)id, toggled) && tool->CanBeToggled() ) | |
f3ba93c1 | 1107 | { |
6bb7cee4 VZ |
1108 | // revert back |
1109 | tool->Toggle(!toggled); | |
8a0681f9 | 1110 | |
0356c259 | 1111 | ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(!toggled, 0)); |
1c383dba VZ |
1112 | } |
1113 | ||
bfbb0b4c | 1114 | return true; |
2bda0e17 KB |
1115 | } |
1116 | ||
8a0681f9 | 1117 | bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl), |
fd3f686c | 1118 | WXLPARAM lParam, |
33ac7e6f | 1119 | WXLPARAM *WXUNUSED(result)) |
2bda0e17 | 1120 | { |
3bce6687 | 1121 | #if wxUSE_TOOLTIPS |
89b892a2 | 1122 | // First check if this applies to us |
2bda0e17 | 1123 | NMHDR *hdr = (NMHDR *)lParam; |
2bda0e17 | 1124 | |
1c383dba VZ |
1125 | // the tooltips control created by the toolbar is sometimes Unicode, even |
1126 | // in an ANSI application - this seems to be a bug in comctl32.dll v5 | |
bd9cd534 | 1127 | UINT code = hdr->code; |
9b601c24 | 1128 | if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) ) |
bfbb0b4c | 1129 | return false; |
2bda0e17 | 1130 | |
89b892a2 VZ |
1131 | HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0); |
1132 | if ( toolTipWnd != hdr->hwndFrom ) | |
bfbb0b4c | 1133 | return false; |
2bda0e17 | 1134 | |
89b892a2 VZ |
1135 | LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam; |
1136 | int id = (int)ttText->hdr.idFrom; | |
89b892a2 | 1137 | |
8a0681f9 VZ |
1138 | wxToolBarToolBase *tool = FindById(id); |
1139 | if ( !tool ) | |
bfbb0b4c | 1140 | return false; |
2bda0e17 | 1141 | |
bd9cd534 | 1142 | return HandleTooltipNotify(code, lParam, tool->GetShortHelp()); |
3bce6687 | 1143 | #else |
abf912c5 VZ |
1144 | wxUnusedVar(lParam); |
1145 | ||
bfbb0b4c | 1146 | return false; |
3bce6687 | 1147 | #endif |
2bda0e17 KB |
1148 | } |
1149 | ||
1c383dba | 1150 | // ---------------------------------------------------------------------------- |
8a0681f9 | 1151 | // toolbar geometry |
1c383dba VZ |
1152 | // ---------------------------------------------------------------------------- |
1153 | ||
8a0681f9 | 1154 | void wxToolBar::SetToolBitmapSize(const wxSize& size) |
2bda0e17 | 1155 | { |
1c383dba VZ |
1156 | wxToolBarBase::SetToolBitmapSize(size); |
1157 | ||
1158 | ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y)); | |
2bda0e17 KB |
1159 | } |
1160 | ||
8a0681f9 | 1161 | void wxToolBar::SetRows(int nRows) |
2bda0e17 | 1162 | { |
8a0681f9 VZ |
1163 | if ( nRows == m_maxRows ) |
1164 | { | |
1165 | // avoid resizing the frame uselessly | |
1166 | return; | |
1167 | } | |
1168 | ||
1169 | // TRUE in wParam means to create at least as many rows, FALSE - | |
1170 | // at most as many | |
1c383dba VZ |
1171 | RECT rect; |
1172 | ::SendMessage(GetHwnd(), TB_SETROWS, | |
8a0681f9 VZ |
1173 | MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)), |
1174 | (LPARAM) &rect); | |
1c383dba VZ |
1175 | |
1176 | m_maxRows = nRows; | |
8a0681f9 VZ |
1177 | |
1178 | UpdateSize(); | |
1179 | } | |
1180 | ||
1181 | // The button size is bigger than the bitmap size | |
1182 | wxSize wxToolBar::GetToolSize() const | |
1183 | { | |
1184 | // TB_GETBUTTONSIZE is supported from version 4.70 | |
5438a566 | 1185 | #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \ |
50165591 CE |
1186 | && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \ |
1187 | && !defined (__DIGITALMARS__) | |
2a1f999f | 1188 | if ( wxApp::GetComCtl32Version() >= 470 ) |
8a0681f9 VZ |
1189 | { |
1190 | DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0); | |
1191 | ||
1192 | return wxSize(LOWORD(dw), HIWORD(dw)); | |
1193 | } | |
1194 | else | |
1195 | #endif // comctl32.dll 4.70+ | |
1196 | { | |
1197 | // defaults | |
1198 | return wxSize(m_defaultWidth + 8, m_defaultHeight + 7); | |
1199 | } | |
2bda0e17 KB |
1200 | } |
1201 | ||
82c9f85c VZ |
1202 | static |
1203 | wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools, | |
1204 | size_t index ) | |
a8945eef | 1205 | { |
222ed1d6 | 1206 | wxToolBarToolsList::compatibility_iterator current = tools.GetFirst(); |
a8945eef | 1207 | |
489f6cf7 | 1208 | for ( ; current ; current = current->GetNext() ) |
a8945eef | 1209 | { |
82c9f85c | 1210 | if ( index == 0 ) |
a8945eef | 1211 | return current->GetData(); |
82c9f85c VZ |
1212 | |
1213 | wxToolBarTool *tool = (wxToolBarTool *)current->GetData(); | |
1214 | size_t separators = tool->GetSeparatorsCount(); | |
1215 | ||
1216 | // if it is a normal button, sepcount == 0, so skip 1 item (the button) | |
1217 | // otherwise, skip as many items as the separator count, plus the | |
1218 | // control itself | |
1219 | index -= separators ? separators + 1 : 1; | |
a8945eef MB |
1220 | } |
1221 | ||
1222 | return 0; | |
1223 | } | |
1224 | ||
8a0681f9 | 1225 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const |
2bda0e17 | 1226 | { |
8a0681f9 VZ |
1227 | POINT pt; |
1228 | pt.x = x; | |
1229 | pt.y = y; | |
1230 | int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt); | |
97071cdb | 1231 | |
37d0bdff MB |
1232 | // MBN: when the point ( x, y ) is close to the toolbar border |
1233 | // TB_HITTEST returns m_nButtons ( not -1 ) | |
1234 | if ( index < 0 || (size_t)index >= m_nButtons ) | |
8a0681f9 VZ |
1235 | // it's a separator or there is no tool at all there |
1236 | return (wxToolBarToolBase *)NULL; | |
1c383dba | 1237 | |
a84f66ef VZ |
1238 | // when TB_SETBUTTONINFO is available (both during compile- and run-time), |
1239 | // we don't use the dummy separators hack | |
1240 | #ifdef TB_SETBUTTONINFO | |
2a1f999f | 1241 | if ( wxApp::GetComCtl32Version() >= 471 ) |
a8945eef MB |
1242 | { |
1243 | return m_tools.Item((size_t)index)->GetData(); | |
1244 | } | |
1245 | else | |
a84f66ef | 1246 | #endif // TB_SETBUTTONINFO |
a8945eef MB |
1247 | { |
1248 | return GetItemSkippingDummySpacers( m_tools, (size_t) index ); | |
1249 | } | |
2bda0e17 KB |
1250 | } |
1251 | ||
8a0681f9 | 1252 | void wxToolBar::UpdateSize() |
2bda0e17 | 1253 | { |
48a102f9 | 1254 | wxPoint pos = GetPosition(); |
93489fc8 | 1255 | ::SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0); |
48a102f9 JS |
1256 | if (pos != GetPosition()) |
1257 | Move(pos); | |
93489fc8 | 1258 | |
b93328d8 RD |
1259 | // In case Realize is called after the initial display (IOW the programmer |
1260 | // may have rebuilt the toolbar) give the frame the option of resizing the | |
1261 | // toolbar to full width again, but only if the parent is a frame and the | |
1262 | // toolbar is managed by the frame. Otherwise assume that some other | |
1263 | // layout mechanism is controlling the toolbar size and leave it alone. | |
8a0681f9 | 1264 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); |
b93328d8 RD |
1265 | if ( frame && frame->GetToolBar() == this ) |
1266 | { | |
f6bcfd97 | 1267 | frame->SendSizeEvent(); |
b93328d8 | 1268 | } |
2bda0e17 KB |
1269 | } |
1270 | ||
c631abda VZ |
1271 | // ---------------------------------------------------------------------------- |
1272 | // toolbar styles | |
1273 | // --------------------------------------------------------------------------- | |
1274 | ||
1275 | void wxToolBar::SetWindowStyleFlag(long style) | |
1276 | { | |
24998710 VZ |
1277 | // the style bits whose changes force us to recreate the toolbar |
1278 | static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS; | |
c631abda | 1279 | |
24998710 | 1280 | const long styleOld = GetWindowStyle(); |
c631abda | 1281 | |
24998710 | 1282 | wxToolBarBase::SetWindowStyleFlag(style); |
c631abda | 1283 | |
24998710 VZ |
1284 | // don't recreate an empty toolbar: not only this is unnecessary, but it is |
1285 | // also fatal as we'd then try to recreate the toolbar when it's just being | |
1286 | // created | |
1287 | if ( GetToolsCount() && | |
1288 | (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) ) | |
1289 | { | |
1290 | // to remove the text labels, simply re-realizing the toolbar is enough | |
1291 | // but I don't know of any way to add the text to an existing toolbar | |
1292 | // other than by recreating it entirely | |
1293 | Recreate(); | |
c631abda | 1294 | } |
c631abda VZ |
1295 | } |
1296 | ||
1c383dba VZ |
1297 | // ---------------------------------------------------------------------------- |
1298 | // tool state | |
1299 | // ---------------------------------------------------------------------------- | |
1300 | ||
8a0681f9 | 1301 | void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable) |
2bda0e17 | 1302 | { |
8a0681f9 VZ |
1303 | ::SendMessage(GetHwnd(), TB_ENABLEBUTTON, |
1304 | (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0)); | |
2bda0e17 KB |
1305 | } |
1306 | ||
8a0681f9 | 1307 | void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle) |
2bda0e17 | 1308 | { |
8a0681f9 VZ |
1309 | ::SendMessage(GetHwnd(), TB_CHECKBUTTON, |
1310 | (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0)); | |
2bda0e17 KB |
1311 | } |
1312 | ||
33ac7e6f | 1313 | void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)) |
088a95f5 | 1314 | { |
8a0681f9 VZ |
1315 | // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or |
1316 | // without, so we really need to delete the button and recreate it here | |
1317 | wxFAIL_MSG( _T("not implemented") ); | |
2bda0e17 KB |
1318 | } |
1319 | ||
1c383dba VZ |
1320 | // ---------------------------------------------------------------------------- |
1321 | // event handlers | |
1322 | // ---------------------------------------------------------------------------- | |
2bda0e17 KB |
1323 | |
1324 | // Responds to colour changes, and passes event on to children. | |
8a0681f9 | 1325 | void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event) |
2bda0e17 | 1326 | { |
84c5b38d | 1327 | wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE)); |
2bda0e17 KB |
1328 | |
1329 | // Remap the buttons | |
8a0681f9 | 1330 | Realize(); |
2bda0e17 | 1331 | |
56bd6aac VZ |
1332 | // Relayout the toolbar |
1333 | int nrows = m_maxRows; | |
1334 | m_maxRows = 0; // otherwise SetRows() wouldn't do anything | |
1335 | SetRows(nrows); | |
1336 | ||
2bda0e17 KB |
1337 | Refresh(); |
1338 | ||
56bd6aac VZ |
1339 | // let the event propagate further |
1340 | event.Skip(); | |
2bda0e17 KB |
1341 | } |
1342 | ||
8a0681f9 | 1343 | void wxToolBar::OnMouseEvent(wxMouseEvent& event) |
e6460682 | 1344 | { |
fe87983b MB |
1345 | if (event.Leaving() && m_pInTool) |
1346 | { | |
1347 | OnMouseEnter( -1 ); | |
1348 | event.Skip(); | |
1349 | return; | |
1350 | } | |
1351 | ||
2334fef6 | 1352 | if ( event.RightDown() ) |
e6460682 | 1353 | { |
2334fef6 | 1354 | // find the tool under the mouse |
8d7eaf91 | 1355 | wxCoord x = 0, y = 0; |
2334fef6 VZ |
1356 | event.GetPosition(&x, &y); |
1357 | ||
1358 | wxToolBarToolBase *tool = FindToolForPosition(x, y); | |
1359 | OnRightClick(tool ? tool->GetId() : -1, x, y); | |
e6460682 JS |
1360 | } |
1361 | else | |
1362 | { | |
42e69d6b | 1363 | event.Skip(); |
e6460682 JS |
1364 | } |
1365 | } | |
1366 | ||
0090a153 JS |
1367 | // This handler is required to allow the toolbar to be set to a non-default |
1368 | // colour: for example, when it must blend in with a notebook page. | |
1369 | void wxToolBar::OnEraseBackground(wxEraseEvent& event) | |
1370 | { | |
848c37c3 | 1371 | RECT rect = wxGetClientRect(GetHwnd()); |
053889ad | 1372 | HDC hdc = GetHdcOf((*event.GetDC())); |
0090a153 | 1373 | |
053889ad VZ |
1374 | if ( UseBgCol() ) |
1375 | { | |
1376 | // do draw our background | |
1377 | // | |
1378 | // notice that this 'dumb' implementation may cause flicker for some of | |
1379 | // the controls in which case they should intercept wxEraseEvent and | |
1380 | // process it themselves somehow | |
1381 | AutoHBRUSH hBrush(wxColourToRGB(GetBackgroundColour())); | |
0090a153 | 1382 | |
053889ad VZ |
1383 | wxCHANGE_HDC_MAP_MODE(hdc, MM_TEXT); |
1384 | ::FillRect(hdc, &rect, hBrush); | |
1385 | } | |
1386 | else // we have no non default background colour | |
1387 | { | |
1388 | #if wxUSE_UXTHEME | |
1389 | // we may need to draw themed colour so that we appear correctly on | |
1390 | // e.g. notebook page under XP with themes but only do it if the parent | |
1391 | // draws themed background itself | |
1392 | if ( !GetParent()->UseBgCol() ) | |
1393 | { | |
1394 | wxUxThemeEngine *theme = wxUxThemeEngine::GetIfActive(); | |
1395 | if ( theme ) | |
1396 | { | |
1397 | HRESULT | |
1398 | hr = theme->DrawThemeParentBackground(GetHwnd(), hdc, &rect); | |
3d412f80 | 1399 | if ( hr == S_OK ) |
053889ad | 1400 | return; |
0090a153 | 1401 | |
3d412f80 VZ |
1402 | // it can also return S_FALSE which seems to simply say that it |
1403 | // didn't draw anything but no error really occurred | |
1404 | if ( FAILED(hr) ) | |
1405 | wxLogApiError(_T("DrawThemeParentBackground(toolbar)"), hr); | |
053889ad VZ |
1406 | } |
1407 | } | |
1408 | #endif // wxUSE_UXTHEME | |
0090a153 | 1409 | |
053889ad VZ |
1410 | event.Skip(); |
1411 | return; | |
1412 | } | |
0090a153 JS |
1413 | } |
1414 | ||
2eb10e2a | 1415 | bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam) |
bdc72a22 | 1416 | { |
3bb63e5c | 1417 | // calculate our minor dimension ourselves - we're confusing the standard |
7509fa8c VZ |
1418 | // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks |
1419 | RECT r; | |
1420 | if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT, 0, (LPARAM)&r) ) | |
bdc72a22 | 1421 | { |
7509fa8c VZ |
1422 | int w, h; |
1423 | ||
1424 | if ( GetWindowStyle() & wxTB_VERTICAL ) | |
1425 | { | |
1426 | w = r.right - r.left; | |
1427 | if ( m_maxRows ) | |
1428 | { | |
1429 | w *= (m_nButtons + m_maxRows - 1)/m_maxRows; | |
1430 | } | |
1431 | h = HIWORD(lParam); | |
1432 | } | |
1433 | else | |
1434 | { | |
1435 | w = LOWORD(lParam); | |
98b96436 RR |
1436 | if (HasFlag( wxTB_FLAT )) |
1437 | h = r.bottom - r.top - 3; | |
1438 | else | |
1439 | h = r.bottom - r.top; | |
7509fa8c VZ |
1440 | if ( m_maxRows ) |
1441 | { | |
28603488 VZ |
1442 | // FIXME: hardcoded separator line height... |
1443 | h += HasFlag(wxTB_NODIVIDER) ? 4 : 6; | |
7509fa8c VZ |
1444 | h *= m_maxRows; |
1445 | } | |
1446 | } | |
1447 | ||
1448 | if ( MAKELPARAM(w, h) != lParam ) | |
8a0681f9 | 1449 | { |
7509fa8c VZ |
1450 | // size really changed |
1451 | SetSize(w, h); | |
1452 | } | |
1453 | ||
1454 | // message processed | |
bfbb0b4c | 1455 | return true; |
7509fa8c VZ |
1456 | } |
1457 | ||
bfbb0b4c | 1458 | return false; |
7509fa8c VZ |
1459 | } |
1460 | ||
1461 | bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam) | |
1462 | { | |
97071cdb DS |
1463 | // erase any dummy separators which were used |
1464 | // for aligning the controls if any here | |
7509fa8c | 1465 | |
97071cdb | 1466 | // first of all, are there any controls at all? |
222ed1d6 | 1467 | wxToolBarToolsList::compatibility_iterator node; |
7509fa8c VZ |
1468 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) |
1469 | { | |
1470 | if ( node->GetData()->IsControl() ) | |
1471 | break; | |
1472 | } | |
1473 | ||
1474 | if ( !node ) | |
7509fa8c | 1475 | // no controls, nothing to erase |
bfbb0b4c | 1476 | return false; |
7509fa8c VZ |
1477 | |
1478 | // prepare the DC on which we'll be drawing | |
1479 | wxClientDC dc(this); | |
1480 | dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID)); | |
1481 | dc.SetPen(*wxTRANSPARENT_PEN); | |
1482 | ||
1483 | RECT r; | |
bfbb0b4c | 1484 | if ( !::GetUpdateRect(GetHwnd(), &r, FALSE) ) |
7509fa8c | 1485 | // nothing to redraw anyhow |
bfbb0b4c | 1486 | return false; |
7509fa8c VZ |
1487 | |
1488 | wxRect rectUpdate; | |
1489 | wxCopyRECTToRect(r, rectUpdate); | |
1490 | ||
1491 | dc.SetClippingRegion(rectUpdate); | |
8a0681f9 | 1492 | |
7509fa8c VZ |
1493 | // draw the toolbar tools, separators &c normally |
1494 | wxControl::MSWWindowProc(WM_PAINT, wParam, lParam); | |
1495 | ||
1496 | // for each control in the toolbar find all the separators intersecting it | |
1497 | // and erase them | |
1498 | // | |
1499 | // NB: this is really the only way to do it as we don't know if a separator | |
1500 | // corresponds to a control (i.e. is a dummy one) or a real one | |
1501 | // otherwise | |
1502 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
1503 | { | |
1504 | wxToolBarToolBase *tool = node->GetData(); | |
1505 | if ( tool->IsControl() ) | |
1506 | { | |
1507 | // get the control rect in our client coords | |
1508 | wxControl *control = tool->GetControl(); | |
1509 | wxRect rectCtrl = control->GetRect(); | |
7509fa8c VZ |
1510 | |
1511 | // iterate over all buttons | |
1512 | TBBUTTON tbb; | |
1513 | int count = ::SendMessage(GetHwnd(), TB_BUTTONCOUNT, 0, 0); | |
1514 | for ( int n = 0; n < count; n++ ) | |
8a0681f9 | 1515 | { |
7509fa8c VZ |
1516 | // is it a separator? |
1517 | if ( !::SendMessage(GetHwnd(), TB_GETBUTTON, | |
1518 | n, (LPARAM)&tbb) ) | |
8a0681f9 | 1519 | { |
7509fa8c VZ |
1520 | wxLogDebug(_T("TB_GETBUTTON failed?")); |
1521 | ||
1522 | continue; | |
8a0681f9 | 1523 | } |
7509fa8c VZ |
1524 | |
1525 | if ( tbb.fsStyle != TBSTYLE_SEP ) | |
1526 | continue; | |
1527 | ||
1528 | // get the bounding rect of the separator | |
1529 | RECT r; | |
1530 | if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, | |
1531 | n, (LPARAM)&r) ) | |
8a0681f9 | 1532 | { |
7509fa8c VZ |
1533 | wxLogDebug(_T("TB_GETITEMRECT failed?")); |
1534 | ||
1535 | continue; | |
8a0681f9 | 1536 | } |
8a0681f9 | 1537 | |
7509fa8c VZ |
1538 | // does it intersect the control? |
1539 | wxRect rectItem; | |
1540 | wxCopyRECTToRect(r, rectItem); | |
1541 | if ( rectCtrl.Intersects(rectItem) ) | |
1542 | { | |
1543 | // yes, do erase it! | |
1544 | dc.DrawRectangle(rectItem); | |
bfbb0b4c | 1545 | |
229de929 JS |
1546 | // Necessary in case we use a no-paint-on-size |
1547 | // style in the parent: the controls can disappear | |
bfbb0b4c | 1548 | control->Refresh(false); |
7509fa8c | 1549 | } |
8a0681f9 | 1550 | } |
8a0681f9 | 1551 | } |
bdc72a22 | 1552 | } |
7509fa8c | 1553 | |
bfbb0b4c | 1554 | return true; |
7509fa8c VZ |
1555 | } |
1556 | ||
2eb10e2a | 1557 | void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam) |
7509fa8c VZ |
1558 | { |
1559 | wxCoord x = GET_X_LPARAM(lParam), | |
1560 | y = GET_Y_LPARAM(lParam); | |
1561 | wxToolBarToolBase* tool = FindToolForPosition( x, y ); | |
1562 | ||
1563 | // cursor left current tool | |
97071cdb | 1564 | if ( tool != m_pInTool && !tool ) |
a8945eef | 1565 | { |
7509fa8c VZ |
1566 | m_pInTool = 0; |
1567 | OnMouseEnter( -1 ); | |
1568 | } | |
a8945eef | 1569 | |
7509fa8c | 1570 | // cursor entered a tool |
97071cdb | 1571 | if ( tool != m_pInTool && tool ) |
7509fa8c VZ |
1572 | { |
1573 | m_pInTool = tool; | |
1574 | OnMouseEnter( tool->GetId() ); | |
1575 | } | |
1576 | } | |
a8945eef | 1577 | |
c140b7e7 | 1578 | WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
7509fa8c VZ |
1579 | { |
1580 | switch ( nMsg ) | |
1581 | { | |
7509fa8c VZ |
1582 | case WM_MOUSEMOVE: |
1583 | // we don't handle mouse moves, so always pass the message to | |
4012b958 | 1584 | // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter) |
7509fa8c VZ |
1585 | HandleMouseMove(wParam, lParam); |
1586 | break; | |
a8945eef | 1587 | |
4012b958 JS |
1588 | case WM_SIZE: |
1589 | if ( HandleSize(wParam, lParam) ) | |
1590 | return 0; | |
1591 | break; | |
1592 | ||
1593 | #ifndef __WXWINCE__ | |
7509fa8c VZ |
1594 | case WM_PAINT: |
1595 | if ( HandlePaint(wParam, lParam) ) | |
1596 | return 0; | |
4012b958 | 1597 | #endif |
97071cdb DS |
1598 | |
1599 | default: | |
1600 | break; | |
a8945eef | 1601 | } |
bdc72a22 | 1602 | |
8a0681f9 | 1603 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); |
bdc72a22 VZ |
1604 | } |
1605 | ||
1c383dba VZ |
1606 | // ---------------------------------------------------------------------------- |
1607 | // private functions | |
1608 | // ---------------------------------------------------------------------------- | |
1609 | ||
fb7e87ef VZ |
1610 | #ifdef wxREMAP_BUTTON_COLOURS |
1611 | ||
566fb299 | 1612 | WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height) |
2bda0e17 | 1613 | { |
566fb299 | 1614 | MemoryHDC hdcMem; |
56bd6aac | 1615 | |
566fb299 | 1616 | if ( !hdcMem ) |
5e68f8f3 | 1617 | { |
566fb299 VZ |
1618 | wxLogLastError(_T("CreateCompatibleDC")); |
1619 | ||
1620 | return bitmap; | |
5e68f8f3 | 1621 | } |
56bd6aac | 1622 | |
566fb299 | 1623 | SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap); |
56bd6aac | 1624 | |
566fb299 | 1625 | if ( !bmpInHDC ) |
5e68f8f3 | 1626 | { |
566fb299 VZ |
1627 | wxLogLastError(_T("SelectObject")); |
1628 | ||
1629 | return bitmap; | |
1630 | } | |
56bd6aac | 1631 | |
90c1530a VZ |
1632 | wxCOLORMAP *cmap = wxGetStdColourMap(); |
1633 | ||
684c68fd VZ |
1634 | for ( int i = 0; i < width; i++ ) |
1635 | { | |
1636 | for ( int j = 0; j < height; j++ ) | |
1637 | { | |
1638 | COLORREF pixel = ::GetPixel(hdcMem, i, j); | |
1639 | ||
90c1530a | 1640 | for ( size_t k = 0; k < wxSTD_COL_MAX; k++ ) |
684c68fd | 1641 | { |
90c1530a | 1642 | COLORREF col = cmap[k].from; |
684c68fd VZ |
1643 | if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 && |
1644 | abs(GetGValue(pixel) - GetGValue(col)) < 10 && | |
1645 | abs(GetBValue(pixel) - GetBValue(col)) < 10 ) | |
1646 | { | |
fb7e87ef VZ |
1647 | if ( cmap[k].to != pixel ) |
1648 | ::SetPixel(hdcMem, i, j, cmap[k].to); | |
684c68fd VZ |
1649 | break; |
1650 | } | |
1651 | } | |
1652 | } | |
1653 | } | |
3a3898f8 VZ |
1654 | |
1655 | return bitmap; | |
566fb299 | 1656 | } |
2bda0e17 | 1657 | |
fb7e87ef VZ |
1658 | #endif // wxREMAP_BUTTON_COLOURS |
1659 | ||
97071cdb | 1660 | #endif // wxUSE_TOOLBAR |