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