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