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