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