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