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