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