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