]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
1c383dba | 2 | // Name: 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 JS |
8 | // Copyright: (c) Julian Smart |
9 | // Licence: wxWindows licence | |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
1c383dba VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
1c383dba | 21 | #pragma implementation "tbar95.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
1c383dba | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
8a0681f9 | 32 | #include "wx/frame.h" |
1c383dba VZ |
33 | #include "wx/log.h" |
34 | #include "wx/intl.h" | |
35 | #include "wx/dynarray.h" | |
4e15f6c5 | 36 | #include "wx/settings.h" |
381dd4bf | 37 | #include "wx/bitmap.h" |
f6bcfd97 | 38 | #include "wx/dcmemory.h" |
f538710f | 39 | #include "wx/control.h" |
2bda0e17 KB |
40 | #endif |
41 | ||
a96b4743 | 42 | #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && (!defined(_WIN32_WCE) || (_WIN32_WCE >= 400 && !wxUSE_POCKETPC_UI)) |
8a0681f9 VZ |
43 | |
44 | #include "wx/toolbar.h" | |
2bda0e17 | 45 | |
1c383dba | 46 | #include "wx/msw/private.h" |
2bda0e17 | 47 | |
0d236fd0 VZ |
48 | // include <commctrl.h> "properly" |
49 | #include "wx/msw/wrapcctl.h" | |
e5898961 | 50 | |
1c383dba VZ |
51 | #include "wx/app.h" // for GetComCtl32Version |
52 | ||
f6bcfd97 BP |
53 | // ---------------------------------------------------------------------------- |
54 | // conditional compilation | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
57 | // wxWindows previously always considered that toolbar buttons have light grey | |
58 | // (0xc0c0c0) background and so ignored any bitmap masks - however, this | |
59 | // doesn't work with XPMs which then appear to have black background. To make | |
60 | // this work, we must respect the bitmap masks - which we do now. This should | |
61 | // be ok in any case, but to restore 100% compatible with the old version | |
62 | // behaviour, you can set this to 0. | |
63 | #define USE_BITMAP_MASKS 1 | |
64 | ||
1c383dba VZ |
65 | // ---------------------------------------------------------------------------- |
66 | // constants | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | // these standard constants are not always defined in compilers headers | |
2bda0e17 | 70 | |
6a23cbce | 71 | // Styles |
bb6290e3 | 72 | #ifndef TBSTYLE_FLAT |
1c383dba VZ |
73 | #define TBSTYLE_LIST 0x1000 |
74 | #define TBSTYLE_FLAT 0x0800 | |
dd177170 RL |
75 | #endif |
76 | ||
77 | #ifndef TBSTYLE_TRANSPARENT | |
1c383dba | 78 | #define TBSTYLE_TRANSPARENT 0x8000 |
bb6290e3 | 79 | #endif |
bb6290e3 | 80 | |
a3399e6c VZ |
81 | #ifndef TBSTYLE_TOOLTIPS |
82 | #define TBSTYLE_TOOLTIPS 0x0100 | |
83 | #endif | |
84 | ||
6a23cbce JS |
85 | // Messages |
86 | #ifndef TB_GETSTYLE | |
1c383dba | 87 | #define TB_SETSTYLE (WM_USER + 56) |
8a0681f9 VZ |
88 | #define TB_GETSTYLE (WM_USER + 57) |
89 | #endif | |
90 | ||
91 | #ifndef TB_HITTEST | |
92 | #define TB_HITTEST (WM_USER + 69) | |
6a23cbce JS |
93 | #endif |
94 | ||
1c383dba | 95 | // these values correspond to those used by comctl32.dll |
81d66cf3 JS |
96 | #define DEFAULTBITMAPX 16 |
97 | #define DEFAULTBITMAPY 15 | |
98 | #define DEFAULTBUTTONX 24 | |
99 | #define DEFAULTBUTTONY 24 | |
100 | #define DEFAULTBARHEIGHT 27 | |
101 | ||
1c383dba VZ |
102 | // ---------------------------------------------------------------------------- |
103 | // wxWin macros | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
2eb10e2a | 106 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl) |
2bda0e17 | 107 | |
066f1b7a SC |
108 | /* |
109 | TOOLBAR PROPERTIES | |
110 | tool | |
111 | bitmap | |
112 | bitmap2 | |
113 | tooltip | |
114 | longhelp | |
115 | radio (bool) | |
116 | toggle (bool) | |
117 | separator | |
118 | style ( wxNO_BORDER | wxTB_HORIZONTAL) | |
119 | bitmapsize | |
120 | margins | |
121 | packing | |
122 | separation | |
123 | ||
124 | dontattachtoframe | |
125 | */ | |
126 | ||
8a0681f9 VZ |
127 | BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase) |
128 | EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent) | |
129 | EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged) | |
2bda0e17 | 130 | END_EVENT_TABLE() |
2bda0e17 | 131 | |
8a0681f9 VZ |
132 | // ---------------------------------------------------------------------------- |
133 | // private classes | |
134 | // ---------------------------------------------------------------------------- | |
135 | ||
136 | class wxToolBarTool : public wxToolBarToolBase | |
137 | { | |
138 | public: | |
139 | wxToolBarTool(wxToolBar *tbar, | |
140 | int id, | |
a3399e6c VZ |
141 | const wxString& label, |
142 | const wxBitmap& bmpNormal, | |
143 | const wxBitmap& bmpDisabled, | |
144 | wxItemKind kind, | |
8a0681f9 | 145 | wxObject *clientData, |
a3399e6c VZ |
146 | const wxString& shortHelp, |
147 | const wxString& longHelp) | |
148 | : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind, | |
149 | clientData, shortHelp, longHelp) | |
8a0681f9 VZ |
150 | { |
151 | m_nSepCount = 0; | |
152 | } | |
153 | ||
154 | wxToolBarTool(wxToolBar *tbar, wxControl *control) | |
155 | : wxToolBarToolBase(tbar, control) | |
156 | { | |
157 | m_nSepCount = 1; | |
158 | } | |
159 | ||
c631abda VZ |
160 | virtual void SetLabel(const wxString& label) |
161 | { | |
162 | if ( label == m_label ) | |
163 | return; | |
164 | ||
165 | wxToolBarToolBase::SetLabel(label); | |
166 | ||
167 | // we need to update the label shown in the toolbar because it has a | |
168 | // pointer to the internal buffer of the old label | |
169 | // | |
170 | // TODO: use TB_SETBUTTONINFO | |
171 | } | |
172 | ||
8a0681f9 VZ |
173 | // set/get the number of separators which we use to cover the space used by |
174 | // a control in the toolbar | |
175 | void SetSeparatorsCount(size_t count) { m_nSepCount = count; } | |
176 | size_t GetSeparatorsCount() const { return m_nSepCount; } | |
177 | ||
178 | private: | |
179 | size_t m_nSepCount; | |
2eb10e2a VZ |
180 | |
181 | DECLARE_NO_COPY_CLASS(wxToolBarTool) | |
8a0681f9 VZ |
182 | }; |
183 | ||
184 | ||
1c383dba VZ |
185 | // ============================================================================ |
186 | // implementation | |
187 | // ============================================================================ | |
2bda0e17 | 188 | |
1c383dba | 189 | // ---------------------------------------------------------------------------- |
8a0681f9 | 190 | // wxToolBarTool |
1c383dba VZ |
191 | // ---------------------------------------------------------------------------- |
192 | ||
8a0681f9 | 193 | wxToolBarToolBase *wxToolBar::CreateTool(int id, |
a3399e6c VZ |
194 | const wxString& label, |
195 | const wxBitmap& bmpNormal, | |
196 | const wxBitmap& bmpDisabled, | |
197 | wxItemKind kind, | |
8a0681f9 | 198 | wxObject *clientData, |
a3399e6c VZ |
199 | const wxString& shortHelp, |
200 | const wxString& longHelp) | |
8a0681f9 | 201 | { |
a3399e6c VZ |
202 | return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind, |
203 | clientData, shortHelp, longHelp); | |
8a0681f9 VZ |
204 | } |
205 | ||
206 | wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control) | |
207 | { | |
208 | return new wxToolBarTool(this, control); | |
209 | } | |
210 | ||
211 | // ---------------------------------------------------------------------------- | |
212 | // wxToolBar construction | |
213 | // ---------------------------------------------------------------------------- | |
214 | ||
215 | void wxToolBar::Init() | |
2bda0e17 | 216 | { |
1c383dba | 217 | m_hBitmap = 0; |
8a0681f9 VZ |
218 | |
219 | m_nButtons = 0; | |
220 | ||
1c383dba VZ |
221 | m_defaultWidth = DEFAULTBITMAPX; |
222 | m_defaultHeight = DEFAULTBITMAPY; | |
37d0bdff MB |
223 | |
224 | m_pInTool = 0; | |
2bda0e17 KB |
225 | } |
226 | ||
8a0681f9 VZ |
227 | bool wxToolBar::Create(wxWindow *parent, |
228 | wxWindowID id, | |
229 | const wxPoint& pos, | |
230 | const wxSize& size, | |
231 | long style, | |
232 | const wxString& name) | |
2bda0e17 | 233 | { |
1c383dba | 234 | // common initialisation |
11b6a93b | 235 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) |
1c383dba | 236 | return FALSE; |
89b892a2 | 237 | |
24998710 | 238 | // MSW-specific initialisation |
278c927d | 239 | if ( !MSWCreateToolbar(pos, size) ) |
24998710 | 240 | return FALSE; |
b0766406 | 241 | |
24998710 VZ |
242 | // set up the colors and fonts |
243 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR)); | |
244 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
7d6d3bf3 | 245 | |
24998710 VZ |
246 | return TRUE; |
247 | } | |
2bda0e17 | 248 | |
278c927d | 249 | bool wxToolBar::MSWCreateToolbar(const wxPoint& pos, const wxSize& size) |
24998710 | 250 | { |
fda7962d | 251 | if ( !MSWCreateControl(TOOLBARCLASSNAME, wxEmptyString, pos, size) ) |
1c383dba | 252 | return FALSE; |
2bda0e17 | 253 | |
c8f1f088 | 254 | // toolbar-specific post initialisation |
1c383dba | 255 | ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0); |
89b892a2 | 256 | |
24998710 VZ |
257 | return TRUE; |
258 | } | |
c8f1f088 | 259 | |
24998710 VZ |
260 | void wxToolBar::Recreate() |
261 | { | |
262 | const HWND hwndOld = GetHwnd(); | |
263 | if ( !hwndOld ) | |
264 | { | |
265 | // we haven't been created yet, no need to recreate | |
266 | return; | |
267 | } | |
2bda0e17 | 268 | |
24998710 VZ |
269 | // get the position and size before unsubclassing the old toolbar |
270 | const wxPoint pos = GetPosition(); | |
271 | const wxSize size = GetSize(); | |
bb6290e3 | 272 | |
24998710 | 273 | UnsubclassWin(); |
2bda0e17 | 274 | |
278c927d | 275 | if ( !MSWCreateToolbar(pos, size) ) |
24998710 VZ |
276 | { |
277 | // what can we do? | |
278 | wxFAIL_MSG( _T("recreating the toolbar failed") ); | |
279 | ||
280 | return; | |
281 | } | |
282 | ||
283 | // reparent all our children under the new toolbar | |
222ed1d6 | 284 | for ( wxWindowList::compatibility_iterator node = m_children.GetFirst(); |
24998710 VZ |
285 | node; |
286 | node = node->GetNext() ) | |
287 | { | |
288 | wxWindow *win = node->GetData(); | |
289 | if ( !win->IsTopLevel() ) | |
290 | ::SetParent(GetHwndOf(win), GetHwnd()); | |
291 | } | |
292 | ||
293 | // only destroy the old toolbar now -- after all the children had been | |
294 | // reparented | |
295 | ::DestroyWindow(hwndOld); | |
296 | ||
297 | // it is for the old bitmap control and can't be used with the new one | |
298 | if ( m_hBitmap ) | |
299 | { | |
300 | ::DeleteObject((HBITMAP) m_hBitmap); | |
301 | m_hBitmap = 0; | |
302 | } | |
303 | ||
304 | Realize(); | |
305 | UpdateSize(); | |
2bda0e17 KB |
306 | } |
307 | ||
8a0681f9 | 308 | wxToolBar::~wxToolBar() |
2bda0e17 | 309 | { |
f6bcfd97 BP |
310 | // we must refresh the frame size when the toolbar is deleted but the frame |
311 | // is not - otherwise toolbar leaves a hole in the place it used to occupy | |
f6bcfd97 | 312 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); |
c2fd78b1 | 313 | if ( frame && !frame->IsBeingDeleted() ) |
f6bcfd97 BP |
314 | { |
315 | frame->SendSizeEvent(); | |
316 | } | |
317 | ||
318 | if ( m_hBitmap ) | |
1c383dba VZ |
319 | { |
320 | ::DeleteObject((HBITMAP) m_hBitmap); | |
321 | } | |
322 | } | |
323 | ||
24998710 VZ |
324 | wxSize wxToolBar::DoGetBestSize() const |
325 | { | |
326 | wxSize sizeBest = GetToolSize(); | |
327 | sizeBest.x *= GetToolsCount(); | |
328 | ||
329 | // reverse horz and vertical components if necessary | |
330 | return HasFlag(wxTB_VERTICAL) ? wxSize(sizeBest.y, sizeBest.x) : sizeBest; | |
331 | } | |
332 | ||
333 | WXDWORD wxToolBar::MSWGetStyle(long style, WXDWORD *exstyle) const | |
334 | { | |
335 | // toolbars never have border, giving one to them results in broken | |
336 | // appearance | |
337 | WXDWORD msStyle = wxControl::MSWGetStyle | |
338 | ( | |
339 | (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle | |
340 | ); | |
341 | ||
342 | // always include this one, it never hurts and setting it later only if we | |
343 | // do have tooltips wouldn't work | |
344 | msStyle |= TBSTYLE_TOOLTIPS; | |
345 | ||
f9dae779 | 346 | if ( style & (wxTB_FLAT | wxTB_HORZ_LAYOUT) ) |
24998710 VZ |
347 | { |
348 | // static as it doesn't change during the program lifetime | |
349 | static int s_verComCtl = wxTheApp->GetComCtl32Version(); | |
350 | ||
351 | // comctl32.dll 4.00 doesn't support the flat toolbars and using this | |
352 | // style with 6.00 (part of Windows XP) leads to the toolbar with | |
353 | // incorrect background colour - and not using it still results in the | |
354 | // correct (flat) toolbar, so don't use it there | |
355 | if ( s_verComCtl > 400 && s_verComCtl < 600 ) | |
356 | { | |
357 | msStyle |= TBSTYLE_FLAT | TBSTYLE_TRANSPARENT; | |
358 | } | |
f9dae779 VZ |
359 | |
360 | if ( s_verComCtl >= 470 && style & wxTB_HORZ_LAYOUT ) | |
361 | { | |
362 | msStyle |= TBSTYLE_LIST; | |
363 | } | |
24998710 VZ |
364 | } |
365 | ||
366 | if ( style & wxTB_NODIVIDER ) | |
367 | msStyle |= CCS_NODIVIDER; | |
368 | ||
369 | if ( style & wxTB_NOALIGN ) | |
370 | msStyle |= CCS_NOPARENTALIGN; | |
371 | ||
3bb63e5c JS |
372 | if ( style & wxTB_VERTICAL ) |
373 | msStyle |= CCS_VERT; | |
374 | ||
24998710 VZ |
375 | return msStyle; |
376 | } | |
377 | ||
bdc72a22 | 378 | // ---------------------------------------------------------------------------- |
8a0681f9 | 379 | // adding/removing tools |
bdc72a22 VZ |
380 | // ---------------------------------------------------------------------------- |
381 | ||
24998710 | 382 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool) |
1c383dba | 383 | { |
8a0681f9 VZ |
384 | // nothing special to do here - we really create the toolbar buttons in |
385 | // Realize() later | |
386 | tool->Attach(this); | |
387 | ||
388 | return TRUE; | |
1c383dba VZ |
389 | } |
390 | ||
8a0681f9 | 391 | bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool) |
bdc72a22 | 392 | { |
f6bcfd97 BP |
393 | // the main difficulty we have here is with the controls in the toolbars: |
394 | // as we (sometimes) use several separators to cover up the space used by | |
395 | // them, the indices are not the same for us and the toolbar | |
396 | ||
397 | // first determine the position of the first button to delete: it may be | |
398 | // different from pos if we use several separators to cover the space used | |
399 | // by a control | |
222ed1d6 | 400 | wxToolBarToolsList::compatibility_iterator node; |
f6bcfd97 BP |
401 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) |
402 | { | |
403 | wxToolBarToolBase *tool2 = node->GetData(); | |
404 | if ( tool2 == tool ) | |
405 | { | |
406 | // let node point to the next node in the list | |
407 | node = node->GetNext(); | |
408 | ||
409 | break; | |
410 | } | |
411 | ||
412 | if ( tool2->IsControl() ) | |
413 | { | |
56bd6aac | 414 | pos += ((wxToolBarTool *)tool2)->GetSeparatorsCount() - 1; |
f6bcfd97 BP |
415 | } |
416 | } | |
417 | ||
418 | // now determine the number of buttons to delete and the area taken by them | |
8a0681f9 | 419 | size_t nButtonsToDelete = 1; |
bdc72a22 | 420 | |
8a0681f9 VZ |
421 | // get the size of the button we're going to delete |
422 | RECT r; | |
423 | if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, pos, (LPARAM)&r) ) | |
bdc72a22 | 424 | { |
8a0681f9 | 425 | wxLogLastError(_T("TB_GETITEMRECT")); |
bdc72a22 VZ |
426 | } |
427 | ||
8a0681f9 | 428 | int width = r.right - r.left; |
bdc72a22 | 429 | |
8a0681f9 VZ |
430 | if ( tool->IsControl() ) |
431 | { | |
432 | nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount(); | |
bdc72a22 | 433 | |
8a0681f9 VZ |
434 | width *= nButtonsToDelete; |
435 | } | |
bdc72a22 | 436 | |
f6bcfd97 BP |
437 | // do delete all buttons |
438 | m_nButtons -= nButtonsToDelete; | |
8a0681f9 VZ |
439 | while ( nButtonsToDelete-- > 0 ) |
440 | { | |
441 | if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) ) | |
442 | { | |
f6bcfd97 | 443 | wxLogLastError(wxT("TB_DELETEBUTTON")); |
1c383dba | 444 | |
8a0681f9 VZ |
445 | return FALSE; |
446 | } | |
447 | } | |
1c383dba | 448 | |
8a0681f9 | 449 | tool->Detach(); |
1c383dba | 450 | |
f6bcfd97 BP |
451 | // and finally reposition all the controls after this button (the toolbar |
452 | // takes care of all normal items) | |
453 | for ( /* node -> first after deleted */ ; node; node = node->GetNext() ) | |
8a0681f9 VZ |
454 | { |
455 | wxToolBarToolBase *tool2 = node->GetData(); | |
456 | if ( tool2->IsControl() ) | |
457 | { | |
458 | int x; | |
459 | wxControl *control = tool2->GetControl(); | |
460 | control->GetPosition(&x, NULL); | |
461 | control->Move(x - width, -1); | |
462 | } | |
463 | } | |
1c383dba VZ |
464 | |
465 | return TRUE; | |
466 | } | |
467 | ||
8a0681f9 | 468 | bool wxToolBar::Realize() |
1c383dba | 469 | { |
24998710 | 470 | const size_t nTools = GetToolsCount(); |
8a0681f9 VZ |
471 | if ( nTools == 0 ) |
472 | { | |
473 | // nothing to do | |
474 | return TRUE; | |
475 | } | |
1c383dba | 476 | |
24998710 | 477 | const bool isVertical = HasFlag(wxTB_VERTICAL); |
2bda0e17 | 478 | |
2b5f62a0 VZ |
479 | // delete all old buttons, if any |
480 | for ( size_t pos = 0; pos < m_nButtons; pos++ ) | |
481 | { | |
482 | if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) ) | |
483 | { | |
484 | wxLogDebug(wxT("TB_DELETEBUTTON failed")); | |
485 | } | |
486 | } | |
487 | ||
8a0681f9 VZ |
488 | // First, add the bitmap: we use one bitmap for all toolbar buttons |
489 | // ---------------------------------------------------------------- | |
2bda0e17 | 490 | |
222ed1d6 | 491 | wxToolBarToolsList::compatibility_iterator node; |
24998710 VZ |
492 | int bitmapId = 0; |
493 | ||
494 | wxSize sizeBmp; | |
495 | if ( HasFlag(wxTB_NOICONS) ) | |
496 | { | |
497 | // no icons, don't leave space for them | |
498 | sizeBmp.x = | |
499 | sizeBmp.y = 0; | |
500 | } | |
501 | else // do show icons | |
502 | { | |
503 | // if we already have a bitmap, we'll replace the existing one -- | |
504 | // otherwise we'll install a new one | |
505 | HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap; | |
506 | ||
507 | sizeBmp.x = m_defaultWidth; | |
508 | sizeBmp.y = m_defaultHeight; | |
89b892a2 | 509 | |
24998710 VZ |
510 | const wxCoord totalBitmapWidth = m_defaultWidth * nTools, |
511 | totalBitmapHeight = m_defaultHeight; | |
2bda0e17 | 512 | |
24998710 | 513 | // Create a bitmap and copy all the tool bitmaps to it |
f6bcfd97 | 514 | #if USE_BITMAP_MASKS |
24998710 VZ |
515 | wxMemoryDC dcAllButtons; |
516 | wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight); | |
517 | dcAllButtons.SelectObject(bitmap); | |
518 | dcAllButtons.SetBackground(*wxLIGHT_GREY_BRUSH); | |
519 | dcAllButtons.Clear(); | |
520 | ||
521 | m_hBitmap = bitmap.GetHBITMAP(); | |
522 | HBITMAP hBitmap = (HBITMAP)m_hBitmap; | |
f6bcfd97 | 523 | #else // !USE_BITMAP_MASKS |
24998710 VZ |
524 | HBITMAP hBitmap = ::CreateCompatibleBitmap(ScreenHDC(), |
525 | totalBitmapWidth, | |
526 | totalBitmapHeight); | |
527 | if ( !hBitmap ) | |
528 | { | |
529 | wxLogLastError(_T("CreateCompatibleBitmap")); | |
8a0681f9 | 530 | |
24998710 VZ |
531 | return FALSE; |
532 | } | |
8a0681f9 | 533 | |
24998710 | 534 | m_hBitmap = (WXHBITMAP)hBitmap; |
89b892a2 | 535 | |
24998710 VZ |
536 | MemoryHDC memoryDC; |
537 | SelectInHDC hdcSelector(memoryDC, hBitmap); | |
2bda0e17 | 538 | |
24998710 | 539 | MemoryHDC memoryDC2; |
f6bcfd97 | 540 | #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS |
2bda0e17 | 541 | |
24998710 VZ |
542 | // the button position |
543 | wxCoord x = 0; | |
2bda0e17 | 544 | |
24998710 VZ |
545 | // the number of buttons (not separators) |
546 | int nButtons = 0; | |
1c383dba | 547 | |
4769a562 | 548 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) |
1c383dba | 549 | { |
24998710 VZ |
550 | wxToolBarToolBase *tool = node->GetData(); |
551 | if ( tool->IsButton() ) | |
1c383dba | 552 | { |
24998710 VZ |
553 | const wxBitmap& bmp = tool->GetNormalBitmap(); |
554 | if ( bmp.Ok() ) | |
555 | { | |
f6bcfd97 | 556 | #if USE_BITMAP_MASKS |
24998710 VZ |
557 | // notice the last parameter: do use mask |
558 | dcAllButtons.DrawBitmap(bmp, x, 0, TRUE); | |
f6bcfd97 | 559 | #else // !USE_BITMAP_MASKS |
24998710 VZ |
560 | SelectInHDC hdcSelector2(memoryDC2, GetHbitmapOf(bmp)); |
561 | if ( !BitBlt(memoryDC, | |
562 | x, 0, m_defaultWidth, m_defaultHeight, | |
563 | memoryDC2, | |
564 | 0, 0, SRCCOPY) ) | |
565 | { | |
566 | wxLogLastError(wxT("BitBlt")); | |
567 | } | |
568 | #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS | |
569 | } | |
570 | else | |
1c383dba | 571 | { |
24998710 | 572 | wxFAIL_MSG( _T("invalid tool button bitmap") ); |
1c383dba VZ |
573 | } |
574 | ||
24998710 VZ |
575 | // still inc width and number of buttons because otherwise the |
576 | // subsequent buttons will all be shifted which is rather confusing | |
577 | // (and like this you'd see immediately which bitmap was bad) | |
578 | x += m_defaultWidth; | |
579 | nButtons++; | |
1c383dba VZ |
580 | } |
581 | } | |
8a0681f9 | 582 | |
f6bcfd97 | 583 | #if USE_BITMAP_MASKS |
24998710 | 584 | dcAllButtons.SelectObject(wxNullBitmap); |
f6bcfd97 | 585 | |
24998710 VZ |
586 | // don't delete this HBITMAP! |
587 | bitmap.SetHBITMAP(0); | |
f6bcfd97 | 588 | #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS |
2bda0e17 | 589 | |
24998710 VZ |
590 | // Map to system colours |
591 | hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap, | |
592 | totalBitmapWidth, totalBitmapHeight); | |
1c383dba | 593 | |
24998710 | 594 | bool addBitmap = TRUE; |
9f83044f | 595 | |
24998710 | 596 | if ( oldToolBarBitmap ) |
1c383dba | 597 | { |
24998710 VZ |
598 | #ifdef TB_REPLACEBITMAP |
599 | if ( wxTheApp->GetComCtl32Version() >= 400 ) | |
9f83044f | 600 | { |
24998710 VZ |
601 | TBREPLACEBITMAP replaceBitmap; |
602 | replaceBitmap.hInstOld = NULL; | |
603 | replaceBitmap.hInstNew = NULL; | |
604 | replaceBitmap.nIDOld = (UINT) oldToolBarBitmap; | |
605 | replaceBitmap.nIDNew = (UINT) hBitmap; | |
606 | replaceBitmap.nButtons = nButtons; | |
607 | if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP, | |
608 | 0, (LPARAM) &replaceBitmap) ) | |
609 | { | |
610 | wxFAIL_MSG(wxT("Could not replace the old bitmap")); | |
611 | } | |
9f83044f | 612 | |
24998710 | 613 | ::DeleteObject(oldToolBarBitmap); |
9f83044f | 614 | |
24998710 VZ |
615 | // already done |
616 | addBitmap = FALSE; | |
617 | } | |
618 | else | |
9f83044f | 619 | #endif // TB_REPLACEBITMAP |
24998710 VZ |
620 | { |
621 | // we can't replace the old bitmap, so we will add another one | |
622 | // (awfully inefficient, but what else to do?) and shift the bitmap | |
623 | // indices accordingly | |
624 | addBitmap = TRUE; | |
1c383dba | 625 | |
24998710 VZ |
626 | bitmapId = m_nButtons; |
627 | } | |
9f83044f | 628 | } |
1c383dba | 629 | |
24998710 | 630 | if ( addBitmap ) // no old bitmap or we can't replace it |
1c383dba | 631 | { |
24998710 VZ |
632 | TBADDBITMAP addBitmap; |
633 | addBitmap.hInst = 0; | |
634 | addBitmap.nID = (UINT) hBitmap; | |
635 | if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP, | |
636 | (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 ) | |
8a0681f9 | 637 | { |
24998710 | 638 | wxFAIL_MSG(wxT("Could not add bitmap to toolbar")); |
8a0681f9 | 639 | } |
1c383dba | 640 | } |
2bda0e17 | 641 | } |
9f83044f | 642 | |
24998710 VZ |
643 | // don't call SetToolBitmapSize() as we don't want to change the values of |
644 | // m_defaultWidth/Height | |
645 | if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, | |
646 | MAKELONG(sizeBmp.x, sizeBmp.y)) ) | |
051205e6 | 647 | { |
24998710 | 648 | wxLogLastError(_T("TB_SETBITMAPSIZE")); |
051205e6 | 649 | } |
2bda0e17 | 650 | |
8a0681f9 VZ |
651 | // Next add the buttons and separators |
652 | // ----------------------------------- | |
653 | ||
1c383dba | 654 | TBBUTTON *buttons = new TBBUTTON[nTools]; |
2bda0e17 | 655 | |
8a0681f9 | 656 | // this array will hold the indices of all controls in the toolbar |
1c383dba VZ |
657 | wxArrayInt controlIds; |
658 | ||
c631abda | 659 | bool lastWasRadio = FALSE; |
1c383dba | 660 | int i = 0; |
4769a562 | 661 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) |
2bda0e17 | 662 | { |
8a0681f9 VZ |
663 | wxToolBarToolBase *tool = node->GetData(); |
664 | ||
28603488 VZ |
665 | // don't add separators to the vertical toolbar with old comctl32.dll |
666 | // versions as they didn't handle this properly | |
667 | if ( isVertical && tool->IsSeparator() && | |
668 | wxTheApp->GetComCtl32Version() <= 472 ) | |
669 | { | |
670 | continue; | |
671 | } | |
672 | ||
8a0681f9 | 673 | |
1c383dba VZ |
674 | TBBUTTON& button = buttons[i]; |
675 | ||
676 | wxZeroMemory(button); | |
677 | ||
c631abda | 678 | bool isRadio = FALSE; |
8a0681f9 | 679 | switch ( tool->GetStyle() ) |
1c383dba VZ |
680 | { |
681 | case wxTOOL_STYLE_CONTROL: | |
8a0681f9 | 682 | button.idCommand = tool->GetId(); |
1c383dba VZ |
683 | // fall through: create just a separator too |
684 | ||
685 | case wxTOOL_STYLE_SEPARATOR: | |
686 | button.fsState = TBSTATE_ENABLED; | |
687 | button.fsStyle = TBSTYLE_SEP; | |
688 | break; | |
689 | ||
690 | case wxTOOL_STYLE_BUTTON: | |
24998710 VZ |
691 | if ( !HasFlag(wxTB_NOICONS) ) |
692 | button.iBitmap = bitmapId; | |
c631abda | 693 | |
24998710 | 694 | if ( HasFlag(wxTB_TEXT) ) |
c631abda | 695 | { |
24998710 VZ |
696 | const wxString& label = tool->GetLabel(); |
697 | if ( !label.empty() ) | |
698 | { | |
699 | button.iString = (int)label.c_str(); | |
700 | } | |
c631abda VZ |
701 | } |
702 | ||
8a0681f9 | 703 | button.idCommand = tool->GetId(); |
1c383dba | 704 | |
8a0681f9 | 705 | if ( tool->IsEnabled() ) |
1c383dba | 706 | button.fsState |= TBSTATE_ENABLED; |
8a0681f9 | 707 | if ( tool->IsToggled() ) |
1c383dba | 708 | button.fsState |= TBSTATE_CHECKED; |
8a0681f9 | 709 | |
c631abda VZ |
710 | switch ( tool->GetKind() ) |
711 | { | |
712 | case wxITEM_RADIO: | |
713 | button.fsStyle = TBSTYLE_CHECKGROUP; | |
714 | ||
715 | if ( !lastWasRadio ) | |
716 | { | |
717 | // the first item in the radio group is checked by | |
718 | // default to be consistent with wxGTK and the menu | |
719 | // radio items | |
720 | button.fsState |= TBSTATE_CHECKED; | |
f3ba93c1 VZ |
721 | |
722 | tool->Toggle(TRUE); | |
c631abda VZ |
723 | } |
724 | ||
725 | isRadio = TRUE; | |
726 | break; | |
727 | ||
728 | case wxITEM_CHECK: | |
729 | button.fsStyle = TBSTYLE_CHECK; | |
730 | break; | |
731 | ||
732 | default: | |
733 | wxFAIL_MSG( _T("unexpected toolbar button kind") ); | |
734 | // fall through | |
735 | ||
736 | case wxITEM_NORMAL: | |
737 | button.fsStyle = TBSTYLE_BUTTON; | |
738 | } | |
1c383dba VZ |
739 | |
740 | bitmapId++; | |
741 | break; | |
742 | } | |
2bda0e17 | 743 | |
c631abda VZ |
744 | lastWasRadio = isRadio; |
745 | ||
1c383dba | 746 | i++; |
2bda0e17 | 747 | } |
1c383dba | 748 | |
a3399e6c | 749 | if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, (WPARAM)i, (LPARAM)buttons) ) |
2bda0e17 | 750 | { |
f6bcfd97 | 751 | wxLogLastError(wxT("TB_ADDBUTTONS")); |
2bda0e17 | 752 | } |
89b892a2 | 753 | |
1c383dba | 754 | delete [] buttons; |
2bda0e17 | 755 | |
8a0681f9 VZ |
756 | // Deal with the controls finally |
757 | // ------------------------------ | |
758 | ||
1c383dba | 759 | // adjust the controls size to fit nicely in the toolbar |
34e5028f | 760 | int y = 0; |
8a0681f9 VZ |
761 | size_t index = 0; |
762 | for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ ) | |
1c383dba | 763 | { |
8a0681f9 | 764 | wxToolBarToolBase *tool = node->GetData(); |
1c383dba | 765 | |
34e5028f VZ |
766 | // we calculate the running y coord for vertical toolbars so we need to |
767 | // get the items size for all items but for the horizontal ones we | |
768 | // don't need to deal with the non controls | |
769 | bool isControl = tool->IsControl(); | |
770 | if ( !isControl && !isVertical ) | |
771 | continue; | |
bdc72a22 | 772 | |
8a0681f9 VZ |
773 | // note that we use TB_GETITEMRECT and not TB_GETRECT because the |
774 | // latter only appeared in v4.70 of comctl32.dll | |
775 | RECT r; | |
776 | if ( !SendMessage(GetHwnd(), TB_GETITEMRECT, | |
777 | index, (LPARAM)(LPRECT)&r) ) | |
778 | { | |
f6bcfd97 | 779 | wxLogLastError(wxT("TB_GETITEMRECT")); |
8a0681f9 VZ |
780 | } |
781 | ||
34e5028f VZ |
782 | if ( !isControl ) |
783 | { | |
784 | // can only be control if isVertical | |
785 | y += r.bottom - r.top; | |
786 | ||
787 | continue; | |
788 | } | |
789 | ||
790 | wxControl *control = tool->GetControl(); | |
791 | ||
792 | wxSize size = control->GetSize(); | |
793 | ||
794 | // the position of the leftmost controls corner | |
795 | int left = -1; | |
796 | ||
bdc72a22 | 797 | // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+ |
5b59df83 | 798 | #ifdef TB_SETBUTTONINFO |
34e5028f VZ |
799 | // available in headers, now check whether it is available now |
800 | // (during run-time) | |
801 | if ( wxTheApp->GetComCtl32Version() >= 471 ) | |
802 | { | |
803 | // set the (underlying) separators width to be that of the | |
804 | // control | |
805 | TBBUTTONINFO tbbi; | |
806 | tbbi.cbSize = sizeof(tbbi); | |
807 | tbbi.dwMask = TBIF_SIZE; | |
808 | tbbi.cx = size.x; | |
809 | if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO, | |
810 | tool->GetId(), (LPARAM)&tbbi) ) | |
bdc72a22 | 811 | { |
34e5028f VZ |
812 | // the id is probably invalid? |
813 | wxLogLastError(wxT("TB_SETBUTTONINFO")); | |
bdc72a22 | 814 | } |
34e5028f VZ |
815 | } |
816 | else | |
817 | #endif // comctl32.dll 4.71 | |
818 | // TB_SETBUTTONINFO unavailable | |
819 | { | |
820 | // try adding several separators to fit the controls width | |
821 | int widthSep = r.right - r.left; | |
822 | left = r.left; | |
823 | ||
824 | TBBUTTON tbb; | |
825 | wxZeroMemory(tbb); | |
826 | tbb.idCommand = 0; | |
827 | tbb.fsState = TBSTATE_ENABLED; | |
828 | tbb.fsStyle = TBSTYLE_SEP; | |
829 | ||
830 | size_t nSeparators = size.x / widthSep; | |
831 | for ( size_t nSep = 0; nSep < nSeparators; nSep++ ) | |
bdc72a22 | 832 | { |
34e5028f VZ |
833 | if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON, |
834 | index, (LPARAM)&tbb) ) | |
bdc72a22 | 835 | { |
34e5028f | 836 | wxLogLastError(wxT("TB_INSERTBUTTON")); |
bdc72a22 VZ |
837 | } |
838 | ||
34e5028f | 839 | index++; |
bdc72a22 | 840 | } |
1c383dba | 841 | |
34e5028f VZ |
842 | // remember the number of separators we used - we'd have to |
843 | // delete all of them later | |
844 | ((wxToolBarTool *)tool)->SetSeparatorsCount(nSeparators); | |
845 | ||
846 | // adjust the controls width to exactly cover the separators | |
847 | control->SetSize((nSeparators + 1)*widthSep, -1); | |
848 | } | |
849 | ||
850 | // position the control itself correctly vertically | |
1c383dba VZ |
851 | int height = r.bottom - r.top; |
852 | int diff = height - size.y; | |
853 | if ( diff < 0 ) | |
854 | { | |
855 | // the control is too high, resize to fit | |
856 | control->SetSize(-1, height - 2); | |
857 | ||
858 | diff = 2; | |
859 | } | |
2bda0e17 | 860 | |
34e5028f VZ |
861 | int top; |
862 | if ( isVertical ) | |
863 | { | |
864 | left = 0; | |
865 | top = y; | |
866 | ||
867 | y += height + 2*GetMargins().y; | |
868 | } | |
869 | else // horizontal toolbar | |
870 | { | |
871 | if ( left == -1 ) | |
872 | left = r.left; | |
873 | ||
874 | top = r.top; | |
875 | } | |
876 | ||
877 | control->Move(left, top + (diff + 1) / 2); | |
1c383dba | 878 | } |
89b892a2 | 879 | |
8a0681f9 VZ |
880 | // the max index is the "real" number of buttons - i.e. counting even the |
881 | // separators which we added just for aligning the controls | |
882 | m_nButtons = index; | |
89b892a2 | 883 | |
8a0681f9 VZ |
884 | if ( !isVertical ) |
885 | { | |
886 | if ( m_maxRows == 0 ) | |
887 | { | |
888 | // if not set yet, only one row | |
889 | SetRows(1); | |
890 | } | |
891 | } | |
892 | else if ( m_nButtons > 0 ) // vertical non empty toolbar | |
893 | { | |
894 | if ( m_maxRows == 0 ) | |
895 | { | |
896 | // if not set yet, have one column | |
897 | SetRows(m_nButtons); | |
898 | } | |
899 | } | |
2bda0e17 | 900 | |
1c383dba | 901 | return TRUE; |
2bda0e17 KB |
902 | } |
903 | ||
1c383dba VZ |
904 | // ---------------------------------------------------------------------------- |
905 | // message handlers | |
906 | // ---------------------------------------------------------------------------- | |
907 | ||
33ac7e6f | 908 | bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id) |
2bda0e17 | 909 | { |
8a0681f9 VZ |
910 | wxToolBarToolBase *tool = FindById((int)id); |
911 | if ( !tool ) | |
1c383dba VZ |
912 | return FALSE; |
913 | ||
1bba1946 | 914 | bool toggled = false; // just to suppress warnings |
6bb7cee4 | 915 | |
8a0681f9 | 916 | if ( tool->CanBeToggled() ) |
1c383dba VZ |
917 | { |
918 | LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0); | |
6bb7cee4 VZ |
919 | toggled = (state & TBSTATE_CHECKED) != 0; |
920 | ||
921 | // ignore the event when a radio button is released, as this doesn't seem to | |
922 | // happen at all, and is handled otherwise | |
923 | if ( tool->GetKind() == wxITEM_RADIO && !toggled ) | |
924 | return TRUE; | |
1c383dba | 925 | |
6bb7cee4 VZ |
926 | tool->Toggle(toggled); |
927 | UnToggleRadioGroup(tool); | |
928 | } | |
8a0681f9 | 929 | |
6bb7cee4 VZ |
930 | // OnLeftClick() can veto the button state change - for buttons which |
931 | // may be toggled only, of couse | |
932 | if ( !OnLeftClick((int)id, toggled) && tool->CanBeToggled() ) | |
f3ba93c1 | 933 | { |
6bb7cee4 VZ |
934 | // revert back |
935 | tool->Toggle(!toggled); | |
8a0681f9 | 936 | |
6bb7cee4 | 937 | ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(toggled, 0)); |
1c383dba VZ |
938 | } |
939 | ||
940 | return TRUE; | |
2bda0e17 KB |
941 | } |
942 | ||
8a0681f9 | 943 | bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl), |
fd3f686c | 944 | WXLPARAM lParam, |
33ac7e6f | 945 | WXLPARAM *WXUNUSED(result)) |
2bda0e17 | 946 | { |
3bce6687 | 947 | #if wxUSE_TOOLTIPS |
89b892a2 | 948 | // First check if this applies to us |
2bda0e17 | 949 | NMHDR *hdr = (NMHDR *)lParam; |
2bda0e17 | 950 | |
1c383dba VZ |
951 | // the tooltips control created by the toolbar is sometimes Unicode, even |
952 | // in an ANSI application - this seems to be a bug in comctl32.dll v5 | |
bd9cd534 | 953 | UINT code = hdr->code; |
9b601c24 | 954 | if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) ) |
89b892a2 | 955 | return FALSE; |
2bda0e17 | 956 | |
89b892a2 VZ |
957 | HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0); |
958 | if ( toolTipWnd != hdr->hwndFrom ) | |
959 | return FALSE; | |
2bda0e17 | 960 | |
89b892a2 VZ |
961 | LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam; |
962 | int id = (int)ttText->hdr.idFrom; | |
89b892a2 | 963 | |
8a0681f9 VZ |
964 | wxToolBarToolBase *tool = FindById(id); |
965 | if ( !tool ) | |
966 | return FALSE; | |
2bda0e17 | 967 | |
bd9cd534 | 968 | return HandleTooltipNotify(code, lParam, tool->GetShortHelp()); |
3bce6687 JS |
969 | #else |
970 | return FALSE; | |
971 | #endif | |
2bda0e17 KB |
972 | } |
973 | ||
1c383dba | 974 | // ---------------------------------------------------------------------------- |
8a0681f9 | 975 | // toolbar geometry |
1c383dba VZ |
976 | // ---------------------------------------------------------------------------- |
977 | ||
8a0681f9 | 978 | void wxToolBar::SetToolBitmapSize(const wxSize& size) |
2bda0e17 | 979 | { |
1c383dba VZ |
980 | wxToolBarBase::SetToolBitmapSize(size); |
981 | ||
982 | ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y)); | |
2bda0e17 KB |
983 | } |
984 | ||
8a0681f9 | 985 | void wxToolBar::SetRows(int nRows) |
2bda0e17 | 986 | { |
8a0681f9 VZ |
987 | if ( nRows == m_maxRows ) |
988 | { | |
989 | // avoid resizing the frame uselessly | |
990 | return; | |
991 | } | |
992 | ||
993 | // TRUE in wParam means to create at least as many rows, FALSE - | |
994 | // at most as many | |
1c383dba VZ |
995 | RECT rect; |
996 | ::SendMessage(GetHwnd(), TB_SETROWS, | |
8a0681f9 VZ |
997 | MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)), |
998 | (LPARAM) &rect); | |
1c383dba VZ |
999 | |
1000 | m_maxRows = nRows; | |
8a0681f9 VZ |
1001 | |
1002 | UpdateSize(); | |
1003 | } | |
1004 | ||
1005 | // The button size is bigger than the bitmap size | |
1006 | wxSize wxToolBar::GetToolSize() const | |
1007 | { | |
1008 | // TB_GETBUTTONSIZE is supported from version 4.70 | |
5438a566 | 1009 | #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \ |
50165591 CE |
1010 | && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \ |
1011 | && !defined (__DIGITALMARS__) | |
8a0681f9 VZ |
1012 | if ( wxTheApp->GetComCtl32Version() >= 470 ) |
1013 | { | |
1014 | DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0); | |
1015 | ||
1016 | return wxSize(LOWORD(dw), HIWORD(dw)); | |
1017 | } | |
1018 | else | |
1019 | #endif // comctl32.dll 4.70+ | |
1020 | { | |
1021 | // defaults | |
1022 | return wxSize(m_defaultWidth + 8, m_defaultHeight + 7); | |
1023 | } | |
2bda0e17 KB |
1024 | } |
1025 | ||
82c9f85c VZ |
1026 | static |
1027 | wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools, | |
1028 | size_t index ) | |
a8945eef | 1029 | { |
222ed1d6 | 1030 | wxToolBarToolsList::compatibility_iterator current = tools.GetFirst(); |
a8945eef | 1031 | |
82c9f85c | 1032 | for ( ; current != 0; current = current->GetNext() ) |
a8945eef | 1033 | { |
82c9f85c | 1034 | if ( index == 0 ) |
a8945eef | 1035 | return current->GetData(); |
82c9f85c VZ |
1036 | |
1037 | wxToolBarTool *tool = (wxToolBarTool *)current->GetData(); | |
1038 | size_t separators = tool->GetSeparatorsCount(); | |
1039 | ||
1040 | // if it is a normal button, sepcount == 0, so skip 1 item (the button) | |
1041 | // otherwise, skip as many items as the separator count, plus the | |
1042 | // control itself | |
1043 | index -= separators ? separators + 1 : 1; | |
a8945eef MB |
1044 | } |
1045 | ||
1046 | return 0; | |
1047 | } | |
1048 | ||
8a0681f9 | 1049 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const |
2bda0e17 | 1050 | { |
8a0681f9 VZ |
1051 | POINT pt; |
1052 | pt.x = x; | |
1053 | pt.y = y; | |
1054 | int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt); | |
37d0bdff MB |
1055 | // MBN: when the point ( x, y ) is close to the toolbar border |
1056 | // TB_HITTEST returns m_nButtons ( not -1 ) | |
1057 | if ( index < 0 || (size_t)index >= m_nButtons ) | |
1c383dba | 1058 | { |
8a0681f9 VZ |
1059 | // it's a separator or there is no tool at all there |
1060 | return (wxToolBarToolBase *)NULL; | |
1c383dba VZ |
1061 | } |
1062 | ||
82c9f85c | 1063 | // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers |
a8945eef MB |
1064 | #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 ) |
1065 | if ( wxTheApp->GetComCtl32Version() >= 471 ) | |
1066 | { | |
1067 | return m_tools.Item((size_t)index)->GetData(); | |
1068 | } | |
1069 | else | |
82c9f85c | 1070 | #endif |
a8945eef MB |
1071 | { |
1072 | return GetItemSkippingDummySpacers( m_tools, (size_t) index ); | |
1073 | } | |
2bda0e17 KB |
1074 | } |
1075 | ||
8a0681f9 | 1076 | void wxToolBar::UpdateSize() |
2bda0e17 | 1077 | { |
0d7ea902 VZ |
1078 | // the toolbar size changed |
1079 | SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0); | |
1080 | ||
1081 | // we must also refresh the frame after the toolbar size (possibly) changed | |
8a0681f9 VZ |
1082 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); |
1083 | if ( frame ) | |
1084 | { | |
f6bcfd97 | 1085 | frame->SendSizeEvent(); |
8a0681f9 | 1086 | } |
2bda0e17 KB |
1087 | } |
1088 | ||
c631abda VZ |
1089 | // ---------------------------------------------------------------------------- |
1090 | // toolbar styles | |
1091 | // --------------------------------------------------------------------------- | |
1092 | ||
1093 | void wxToolBar::SetWindowStyleFlag(long style) | |
1094 | { | |
24998710 VZ |
1095 | // the style bits whose changes force us to recreate the toolbar |
1096 | static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS; | |
c631abda | 1097 | |
24998710 | 1098 | const long styleOld = GetWindowStyle(); |
c631abda | 1099 | |
24998710 | 1100 | wxToolBarBase::SetWindowStyleFlag(style); |
c631abda | 1101 | |
24998710 VZ |
1102 | // don't recreate an empty toolbar: not only this is unnecessary, but it is |
1103 | // also fatal as we'd then try to recreate the toolbar when it's just being | |
1104 | // created | |
1105 | if ( GetToolsCount() && | |
1106 | (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) ) | |
1107 | { | |
1108 | // to remove the text labels, simply re-realizing the toolbar is enough | |
1109 | // but I don't know of any way to add the text to an existing toolbar | |
1110 | // other than by recreating it entirely | |
1111 | Recreate(); | |
c631abda | 1112 | } |
c631abda VZ |
1113 | } |
1114 | ||
1c383dba VZ |
1115 | // ---------------------------------------------------------------------------- |
1116 | // tool state | |
1117 | // ---------------------------------------------------------------------------- | |
1118 | ||
8a0681f9 | 1119 | void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable) |
2bda0e17 | 1120 | { |
8a0681f9 VZ |
1121 | ::SendMessage(GetHwnd(), TB_ENABLEBUTTON, |
1122 | (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0)); | |
2bda0e17 KB |
1123 | } |
1124 | ||
8a0681f9 | 1125 | void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle) |
2bda0e17 | 1126 | { |
8a0681f9 VZ |
1127 | ::SendMessage(GetHwnd(), TB_CHECKBUTTON, |
1128 | (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0)); | |
2bda0e17 KB |
1129 | } |
1130 | ||
33ac7e6f | 1131 | void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)) |
088a95f5 | 1132 | { |
8a0681f9 VZ |
1133 | // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or |
1134 | // without, so we really need to delete the button and recreate it here | |
1135 | wxFAIL_MSG( _T("not implemented") ); | |
2bda0e17 KB |
1136 | } |
1137 | ||
1c383dba VZ |
1138 | // ---------------------------------------------------------------------------- |
1139 | // event handlers | |
1140 | // ---------------------------------------------------------------------------- | |
2bda0e17 KB |
1141 | |
1142 | // Responds to colour changes, and passes event on to children. | |
8a0681f9 | 1143 | void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event) |
2bda0e17 | 1144 | { |
84c5b38d | 1145 | wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE)); |
2bda0e17 KB |
1146 | |
1147 | // Remap the buttons | |
8a0681f9 | 1148 | Realize(); |
2bda0e17 | 1149 | |
56bd6aac VZ |
1150 | // Relayout the toolbar |
1151 | int nrows = m_maxRows; | |
1152 | m_maxRows = 0; // otherwise SetRows() wouldn't do anything | |
1153 | SetRows(nrows); | |
1154 | ||
2bda0e17 KB |
1155 | Refresh(); |
1156 | ||
56bd6aac VZ |
1157 | // let the event propagate further |
1158 | event.Skip(); | |
2bda0e17 KB |
1159 | } |
1160 | ||
8a0681f9 | 1161 | void wxToolBar::OnMouseEvent(wxMouseEvent& event) |
e6460682 | 1162 | { |
fe87983b MB |
1163 | if (event.Leaving() && m_pInTool) |
1164 | { | |
1165 | OnMouseEnter( -1 ); | |
1166 | event.Skip(); | |
1167 | return; | |
1168 | } | |
1169 | ||
e6460682 JS |
1170 | if (event.RightDown()) |
1171 | { | |
1172 | // For now, we don't have an id. Later we could | |
1173 | // try finding the tool. | |
1174 | OnRightClick((int)-1, event.GetX(), event.GetY()); | |
1175 | } | |
1176 | else | |
1177 | { | |
42e69d6b | 1178 | event.Skip(); |
e6460682 JS |
1179 | } |
1180 | } | |
1181 | ||
2eb10e2a | 1182 | bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam) |
bdc72a22 | 1183 | { |
3bb63e5c | 1184 | // calculate our minor dimension ourselves - we're confusing the standard |
7509fa8c VZ |
1185 | // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks |
1186 | RECT r; | |
1187 | if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT, 0, (LPARAM)&r) ) | |
bdc72a22 | 1188 | { |
7509fa8c VZ |
1189 | int w, h; |
1190 | ||
1191 | if ( GetWindowStyle() & wxTB_VERTICAL ) | |
1192 | { | |
1193 | w = r.right - r.left; | |
1194 | if ( m_maxRows ) | |
1195 | { | |
1196 | w *= (m_nButtons + m_maxRows - 1)/m_maxRows; | |
1197 | } | |
1198 | h = HIWORD(lParam); | |
1199 | } | |
1200 | else | |
1201 | { | |
1202 | w = LOWORD(lParam); | |
98b96436 RR |
1203 | if (HasFlag( wxTB_FLAT )) |
1204 | h = r.bottom - r.top - 3; | |
1205 | else | |
1206 | h = r.bottom - r.top; | |
7509fa8c VZ |
1207 | if ( m_maxRows ) |
1208 | { | |
28603488 VZ |
1209 | // FIXME: hardcoded separator line height... |
1210 | h += HasFlag(wxTB_NODIVIDER) ? 4 : 6; | |
7509fa8c VZ |
1211 | h *= m_maxRows; |
1212 | } | |
1213 | } | |
1214 | ||
1215 | if ( MAKELPARAM(w, h) != lParam ) | |
8a0681f9 | 1216 | { |
7509fa8c VZ |
1217 | // size really changed |
1218 | SetSize(w, h); | |
1219 | } | |
1220 | ||
1221 | // message processed | |
1222 | return TRUE; | |
1223 | } | |
1224 | ||
1225 | return FALSE; | |
1226 | } | |
1227 | ||
1228 | bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam) | |
1229 | { | |
1230 | // erase any dummy separators which we used for aligning the controls if | |
1231 | // any here | |
1232 | ||
1233 | // first of all, do we have any controls at all? | |
222ed1d6 | 1234 | wxToolBarToolsList::compatibility_iterator node; |
7509fa8c VZ |
1235 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) |
1236 | { | |
1237 | if ( node->GetData()->IsControl() ) | |
1238 | break; | |
1239 | } | |
1240 | ||
1241 | if ( !node ) | |
1242 | { | |
1243 | // no controls, nothing to erase | |
1244 | return FALSE; | |
1245 | } | |
1246 | ||
1247 | // prepare the DC on which we'll be drawing | |
1248 | wxClientDC dc(this); | |
1249 | dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID)); | |
1250 | dc.SetPen(*wxTRANSPARENT_PEN); | |
1251 | ||
1252 | RECT r; | |
1253 | if ( !GetUpdateRect(GetHwnd(), &r, FALSE) ) | |
1254 | { | |
1255 | // nothing to redraw anyhow | |
1256 | return FALSE; | |
1257 | } | |
1258 | ||
1259 | wxRect rectUpdate; | |
1260 | wxCopyRECTToRect(r, rectUpdate); | |
1261 | ||
1262 | dc.SetClippingRegion(rectUpdate); | |
8a0681f9 | 1263 | |
7509fa8c VZ |
1264 | // draw the toolbar tools, separators &c normally |
1265 | wxControl::MSWWindowProc(WM_PAINT, wParam, lParam); | |
1266 | ||
1267 | // for each control in the toolbar find all the separators intersecting it | |
1268 | // and erase them | |
1269 | // | |
1270 | // NB: this is really the only way to do it as we don't know if a separator | |
1271 | // corresponds to a control (i.e. is a dummy one) or a real one | |
1272 | // otherwise | |
1273 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
1274 | { | |
1275 | wxToolBarToolBase *tool = node->GetData(); | |
1276 | if ( tool->IsControl() ) | |
1277 | { | |
1278 | // get the control rect in our client coords | |
1279 | wxControl *control = tool->GetControl(); | |
1280 | wxRect rectCtrl = control->GetRect(); | |
7509fa8c VZ |
1281 | |
1282 | // iterate over all buttons | |
1283 | TBBUTTON tbb; | |
1284 | int count = ::SendMessage(GetHwnd(), TB_BUTTONCOUNT, 0, 0); | |
1285 | for ( int n = 0; n < count; n++ ) | |
8a0681f9 | 1286 | { |
7509fa8c VZ |
1287 | // is it a separator? |
1288 | if ( !::SendMessage(GetHwnd(), TB_GETBUTTON, | |
1289 | n, (LPARAM)&tbb) ) | |
8a0681f9 | 1290 | { |
7509fa8c VZ |
1291 | wxLogDebug(_T("TB_GETBUTTON failed?")); |
1292 | ||
1293 | continue; | |
8a0681f9 | 1294 | } |
7509fa8c VZ |
1295 | |
1296 | if ( tbb.fsStyle != TBSTYLE_SEP ) | |
1297 | continue; | |
1298 | ||
1299 | // get the bounding rect of the separator | |
1300 | RECT r; | |
1301 | if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, | |
1302 | n, (LPARAM)&r) ) | |
8a0681f9 | 1303 | { |
7509fa8c VZ |
1304 | wxLogDebug(_T("TB_GETITEMRECT failed?")); |
1305 | ||
1306 | continue; | |
8a0681f9 | 1307 | } |
8a0681f9 | 1308 | |
7509fa8c VZ |
1309 | // does it intersect the control? |
1310 | wxRect rectItem; | |
1311 | wxCopyRECTToRect(r, rectItem); | |
1312 | if ( rectCtrl.Intersects(rectItem) ) | |
1313 | { | |
1314 | // yes, do erase it! | |
1315 | dc.DrawRectangle(rectItem); | |
229de929 JS |
1316 | |
1317 | // Necessary in case we use a no-paint-on-size | |
1318 | // style in the parent: the controls can disappear | |
1319 | control->Refresh(FALSE); | |
7509fa8c | 1320 | } |
8a0681f9 | 1321 | } |
8a0681f9 | 1322 | } |
bdc72a22 | 1323 | } |
7509fa8c VZ |
1324 | |
1325 | return TRUE; | |
1326 | } | |
1327 | ||
2eb10e2a | 1328 | void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam) |
7509fa8c VZ |
1329 | { |
1330 | wxCoord x = GET_X_LPARAM(lParam), | |
1331 | y = GET_Y_LPARAM(lParam); | |
1332 | wxToolBarToolBase* tool = FindToolForPosition( x, y ); | |
1333 | ||
1334 | // cursor left current tool | |
1335 | if( tool != m_pInTool && !tool ) | |
a8945eef | 1336 | { |
7509fa8c VZ |
1337 | m_pInTool = 0; |
1338 | OnMouseEnter( -1 ); | |
1339 | } | |
a8945eef | 1340 | |
7509fa8c VZ |
1341 | // cursor entered a tool |
1342 | if( tool != m_pInTool && tool ) | |
1343 | { | |
1344 | m_pInTool = tool; | |
1345 | OnMouseEnter( tool->GetId() ); | |
1346 | } | |
1347 | } | |
a8945eef | 1348 | |
7509fa8c VZ |
1349 | long wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
1350 | { | |
1351 | switch ( nMsg ) | |
1352 | { | |
1353 | case WM_SIZE: | |
1354 | if ( HandleSize(wParam, lParam) ) | |
1355 | return 0; | |
1356 | break; | |
1357 | ||
1358 | case WM_MOUSEMOVE: | |
1359 | // we don't handle mouse moves, so always pass the message to | |
1360 | // wxControl::MSWWindowProc | |
1361 | HandleMouseMove(wParam, lParam); | |
1362 | break; | |
a8945eef | 1363 | |
7509fa8c VZ |
1364 | case WM_PAINT: |
1365 | if ( HandlePaint(wParam, lParam) ) | |
1366 | return 0; | |
a8945eef | 1367 | } |
bdc72a22 | 1368 | |
8a0681f9 | 1369 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); |
bdc72a22 VZ |
1370 | } |
1371 | ||
1c383dba VZ |
1372 | // ---------------------------------------------------------------------------- |
1373 | // private functions | |
1374 | // ---------------------------------------------------------------------------- | |
1375 | ||
566fb299 | 1376 | WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height) |
2bda0e17 | 1377 | { |
566fb299 | 1378 | MemoryHDC hdcMem; |
56bd6aac | 1379 | |
566fb299 | 1380 | if ( !hdcMem ) |
5e68f8f3 | 1381 | { |
566fb299 VZ |
1382 | wxLogLastError(_T("CreateCompatibleDC")); |
1383 | ||
1384 | return bitmap; | |
5e68f8f3 | 1385 | } |
56bd6aac | 1386 | |
566fb299 | 1387 | SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap); |
56bd6aac | 1388 | |
566fb299 | 1389 | if ( !bmpInHDC ) |
5e68f8f3 | 1390 | { |
566fb299 VZ |
1391 | wxLogLastError(_T("SelectObject")); |
1392 | ||
1393 | return bitmap; | |
1394 | } | |
56bd6aac | 1395 | |
90c1530a VZ |
1396 | wxCOLORMAP *cmap = wxGetStdColourMap(); |
1397 | ||
684c68fd VZ |
1398 | for ( int i = 0; i < width; i++ ) |
1399 | { | |
1400 | for ( int j = 0; j < height; j++ ) | |
1401 | { | |
1402 | COLORREF pixel = ::GetPixel(hdcMem, i, j); | |
1403 | ||
90c1530a | 1404 | for ( size_t k = 0; k < wxSTD_COL_MAX; k++ ) |
684c68fd | 1405 | { |
90c1530a | 1406 | COLORREF col = cmap[k].from; |
684c68fd VZ |
1407 | if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 && |
1408 | abs(GetGValue(pixel) - GetGValue(col)) < 10 && | |
1409 | abs(GetBValue(pixel) - GetBValue(col)) < 10 ) | |
1410 | { | |
90c1530a | 1411 | ::SetPixel(hdcMem, i, j, cmap[k].to); |
684c68fd VZ |
1412 | break; |
1413 | } | |
1414 | } | |
1415 | } | |
1416 | } | |
3a3898f8 VZ |
1417 | |
1418 | return bitmap; | |
1419 | ||
566fb299 VZ |
1420 | // VZ: I leave here my attempts to map the bitmap to the system colours |
1421 | // faster by using BitBlt() even though it's broken currently - but | |
1422 | // maybe someone else can finish it? It should be faster than iterating | |
1423 | // over all pixels... | |
3a3898f8 | 1424 | #if 0 |
566fb299 VZ |
1425 | MemoryHDC hdcMask, hdcDst; |
1426 | if ( !hdcMask || !hdcDst ) | |
1427 | { | |
1428 | wxLogLastError(_T("CreateCompatibleDC")); | |
56bd6aac | 1429 | |
566fb299 | 1430 | return bitmap; |
2bda0e17 | 1431 | } |
2bda0e17 | 1432 | |
566fb299 VZ |
1433 | // create the target bitmap |
1434 | HBITMAP hbmpDst = ::CreateCompatibleBitmap(hdcDst, width, height); | |
1435 | if ( !hbmpDst ) | |
1436 | { | |
1437 | wxLogLastError(_T("CreateCompatibleBitmap")); | |
1438 | ||
1439 | return bitmap; | |
1440 | } | |
1441 | ||
1442 | // create the monochrome mask bitmap | |
1443 | HBITMAP hbmpMask = ::CreateBitmap(width, height, 1, 1, 0); | |
1444 | if ( !hbmpMask ) | |
1445 | { | |
1446 | wxLogLastError(_T("CreateBitmap(mono)")); | |
1447 | ||
1448 | ::DeleteObject(hbmpDst); | |
1449 | ||
1450 | return bitmap; | |
1451 | } | |
1452 | ||
1453 | SelectInHDC bmpInDst(hdcDst, hbmpDst), | |
1454 | bmpInMask(hdcMask, hbmpMask); | |
1455 | ||
1456 | // for each colour: | |
1457 | for ( n = 0; n < NUM_OF_MAPPED_COLOURS; n++ ) | |
1458 | { | |
1459 | // create the mask for this colour | |
1460 | ::SetBkColor(hdcMem, ColorMap[n].from); | |
1461 | ::BitBlt(hdcMask, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY); | |
1462 | ||
1463 | // replace this colour with the target one in the dst bitmap | |
1464 | HBRUSH hbr = ::CreateSolidBrush(ColorMap[n].to); | |
1465 | HGDIOBJ hbrOld = ::SelectObject(hdcDst, hbr); | |
2bda0e17 | 1466 | |
566fb299 VZ |
1467 | ::MaskBlt(hdcDst, 0, 0, width, height, |
1468 | hdcMem, 0, 0, | |
1469 | hbmpMask, 0, 0, | |
1470 | MAKEROP4(PATCOPY, SRCCOPY)); | |
1471 | ||
1472 | (void)::SelectObject(hdcDst, hbrOld); | |
1473 | ::DeleteObject(hbr); | |
1474 | } | |
1475 | ||
1476 | ::DeleteObject((HBITMAP)bitmap); | |
1477 | ||
1478 | return (WXHBITMAP)hbmpDst; | |
3a3898f8 | 1479 | #endif // 0 |
566fb299 | 1480 | } |
2bda0e17 | 1481 | |
8a0681f9 | 1482 | #endif // wxUSE_TOOLBAR && Win95 |
33ac7e6f | 1483 |