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