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