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