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