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