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