1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/tbar95.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "tbar95.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/dynarray.h"
36 #include "wx/settings.h"
37 #include "wx/bitmap.h"
40 #if wxUSE_TOOLBAR && defined(__WIN95__) && wxUSE_TOOLBAR_NATIVE
42 #include "wx/toolbar.h"
44 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
48 #include "wx/msw/private.h"
52 // I don't know what _OLD_ refers to so I'm reinstating the old
54 // #ifdef __GNUWIN32_OLD__
55 #if defined(__GNUWIN32__) && !defined(__MINGW32__)
56 #include "wx/msw/gnuwin32/extra.h"
63 #include "wx/msw/dib.h"
64 #include "wx/app.h" // for GetComCtl32Version
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // these standard constants are not always defined in compilers headers
74 #define TBSTYLE_LIST 0x1000
75 #define TBSTYLE_FLAT 0x0800
76 #define TBSTYLE_TRANSPARENT 0x8000
78 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
82 #define TB_SETSTYLE (WM_USER + 56)
83 #define TB_GETSTYLE (WM_USER + 57)
87 #define TB_HITTEST (WM_USER + 69)
90 // these values correspond to those used by comctl32.dll
91 #define DEFAULTBITMAPX 16
92 #define DEFAULTBITMAPY 15
93 #define DEFAULTBUTTONX 24
94 #define DEFAULTBUTTONY 24
95 #define DEFAULTBARHEIGHT 27
97 // ----------------------------------------------------------------------------
98 // private function prototypes
99 // ----------------------------------------------------------------------------
101 static void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
);
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
109 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
110 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
111 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 class wxToolBarTool
: public wxToolBarToolBase
121 wxToolBarTool(wxToolBar
*tbar
,
123 const wxBitmap
& bitmap1
,
124 const wxBitmap
& bitmap2
,
126 wxObject
*clientData
,
127 const wxString
& shortHelpString
,
128 const wxString
& longHelpString
)
129 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
130 clientData
, shortHelpString
, longHelpString
)
135 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
136 : wxToolBarToolBase(tbar
, control
)
141 // set/get the number of separators which we use to cover the space used by
142 // a control in the toolbar
143 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
144 size_t GetSeparatorsCount() const { return m_nSepCount
; }
151 // ============================================================================
153 // ============================================================================
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
160 const wxBitmap
& bitmap1
,
161 const wxBitmap
& bitmap2
,
163 wxObject
*clientData
,
164 const wxString
& shortHelpString
,
165 const wxString
& longHelpString
)
167 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
168 clientData
, shortHelpString
, longHelpString
);
171 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
173 return new wxToolBarTool(this, control
);
176 // ----------------------------------------------------------------------------
177 // wxToolBar construction
178 // ----------------------------------------------------------------------------
180 void wxToolBar::Init()
186 m_defaultWidth
= DEFAULTBITMAPX
;
187 m_defaultHeight
= DEFAULTBITMAPY
;
190 bool wxToolBar::Create(wxWindow
*parent
,
195 const wxString
& name
)
197 // common initialisation
198 if ( !CreateControl(parent
, id
, pos
, size
, style
, name
) )
202 DWORD msflags
= 0; // WS_VISIBLE | WS_CHILD always included
203 if (style
& wxBORDER
)
204 msflags
|= WS_BORDER
;
206 #ifdef TBSTYLE_TOOLTIPS
207 msflags
|= TBSTYLE_TOOLTIPS
;
210 if (style
& wxTB_FLAT
)
212 if (wxTheApp
->GetComCtl32Version() > 400)
213 msflags
|= TBSTYLE_FLAT
;
216 // MSW-specific initialisation
217 if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME
, msflags
) )
220 // toolbar-specific post initialisation
221 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
223 // set up the colors and fonts
224 wxRGBToColour(m_backgroundColour
, GetSysColor(COLOR_BTNFACE
));
225 m_foregroundColour
= *wxBLACK
;
227 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
238 height
= m_defaultHeight
;
244 SetSize(x
, y
, width
, height
);
249 wxToolBar::~wxToolBar()
253 ::DeleteObject((HBITMAP
) m_hBitmap
);
257 // ----------------------------------------------------------------------------
258 // adding/removing tools
259 // ----------------------------------------------------------------------------
261 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
262 wxToolBarToolBase
*tool
)
264 // nothing special to do here - we really create the toolbar buttons in
271 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
273 // normally, we only delete one button, but we use several separators to
274 // cover the space used by one control sometimes (with old comctl32.dll)
275 size_t nButtonsToDelete
= 1;
277 // get the size of the button we're going to delete
279 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT
, pos
, (LPARAM
)&r
) )
281 wxLogLastError(_T("TB_GETITEMRECT"));
284 int width
= r
.right
- r
.left
;
286 if ( tool
->IsControl() )
288 nButtonsToDelete
= ((wxToolBarTool
*)tool
)->GetSeparatorsCount();
290 width
*= nButtonsToDelete
;
293 while ( nButtonsToDelete
-- > 0 )
295 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, pos
, 0) )
297 wxLogLastError("TB_DELETEBUTTON");
305 m_nButtons
-= nButtonsToDelete
;
307 // reposition all the controls after this button
308 wxToolBarToolsList::Node
*node
= m_tools
.Item(pos
);
309 for ( node
= node
->GetNext(); node
; node
= node
->GetNext() )
311 wxToolBarToolBase
*tool2
= node
->GetData();
312 if ( tool2
->IsControl() )
315 wxControl
*control
= tool2
->GetControl();
316 control
->GetPosition(&x
, NULL
);
317 control
->Move(x
- width
, -1);
324 bool wxToolBar::Realize()
326 size_t nTools
= GetToolsCount();
333 bool isVertical
= (GetWindowStyle() & wxTB_VERTICAL
) != 0;
335 // First, add the bitmap: we use one bitmap for all toolbar buttons
336 // ----------------------------------------------------------------
338 // if we already have a bitmap, we'll replace the existing one - otherwise
339 // we'll install a new one
340 HBITMAP oldToolBarBitmap
= (HBITMAP
)m_hBitmap
;
342 int totalBitmapWidth
= (int)(m_defaultWidth
* nTools
);
343 int totalBitmapHeight
= (int)m_defaultHeight
;
345 // Create a bitmap for all the tool bitmaps
346 HBITMAP hBitmap
= ::CreateCompatibleBitmap(ScreenHDC(),
351 wxLogLastError(_T("CreateCompatibleBitmap"));
356 m_hBitmap
= (WXHBITMAP
)hBitmap
;
358 // Now blit all the tools onto this bitmap
359 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
360 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, hBitmap
);
362 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
364 // the button position
367 // the number of buttons (not separators)
370 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
373 wxToolBarToolBase
*tool
= node
->GetData();
374 if ( tool
->IsButton() )
376 HBITMAP hbmp
= GetHbitmapOf(tool
->GetBitmap1());
379 HBITMAP oldBitmap2
= (HBITMAP
)::SelectObject(memoryDC2
, hbmp
);
380 if ( !BitBlt(memoryDC
, x
, 0, m_defaultWidth
, m_defaultHeight
,
381 memoryDC2
, 0, 0, SRCCOPY
) )
383 wxLogLastError("BitBlt");
386 ::SelectObject(memoryDC2
, oldBitmap2
);
390 wxFAIL_MSG( _T("invalid tool button bitmap") );
393 // still inc width and number of buttons because otherwise the
394 // subsequent buttons will all be shifted which is rather confusing
395 // (and like this you'd see immediately which bitmap was bad)
400 node
= node
->GetNext();
403 ::SelectObject(memoryDC
, oldBitmap
);
404 ::DeleteDC(memoryDC
);
405 ::DeleteDC(memoryDC2
);
407 // Map to system colours
408 wxMapBitmap(hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
410 if ( oldToolBarBitmap
)
412 TBREPLACEBITMAP replaceBitmap
;
413 replaceBitmap
.hInstOld
= NULL
;
414 replaceBitmap
.hInstNew
= NULL
;
415 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
416 replaceBitmap
.nIDNew
= (UINT
) hBitmap
;
417 replaceBitmap
.nButtons
= nButtons
;
418 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP
,
419 0, (LPARAM
) &replaceBitmap
) )
421 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
424 ::DeleteObject(oldToolBarBitmap
);
426 // Now delete all the buttons
427 for ( size_t pos
= 0; pos
< m_nButtons
; pos
++ )
429 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON
, 0, 0) )
431 wxLogLastError("TB_DELETEBUTTON");
435 else // no old bitmap
437 TBADDBITMAP addBitmap
;
439 addBitmap
.nID
= (UINT
) hBitmap
;
440 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP
,
441 (WPARAM
) nButtons
, (LPARAM
)&addBitmap
) == -1 )
443 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
447 // Next add the buttons and separators
448 // -----------------------------------
450 TBBUTTON
*buttons
= new TBBUTTON
[nTools
];
452 // this array will hold the indices of all controls in the toolbar
453 wxArrayInt controlIds
;
458 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext() )
460 wxToolBarToolBase
*tool
= node
->GetData();
462 // don't add separators to the vertical toolbar - looks ugly
463 if ( isVertical
&& tool
->IsSeparator() )
466 TBBUTTON
& button
= buttons
[i
];
468 wxZeroMemory(button
);
470 switch ( tool
->GetStyle() )
472 case wxTOOL_STYLE_CONTROL
:
473 button
.idCommand
= tool
->GetId();
474 // fall through: create just a separator too
476 case wxTOOL_STYLE_SEPARATOR
:
477 button
.fsState
= TBSTATE_ENABLED
;
478 button
.fsStyle
= TBSTYLE_SEP
;
481 case wxTOOL_STYLE_BUTTON
:
482 button
.iBitmap
= bitmapId
;
483 button
.idCommand
= tool
->GetId();
485 if ( tool
->IsEnabled() )
486 button
.fsState
|= TBSTATE_ENABLED
;
487 if ( tool
->IsToggled() )
488 button
.fsState
|= TBSTATE_CHECKED
;
490 button
.fsStyle
= tool
->CanBeToggled() ? TBSTYLE_CHECK
500 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS
,
501 (WPARAM
)i
, (LPARAM
)buttons
) )
503 wxLogLastError("TB_ADDBUTTONS");
508 // Deal with the controls finally
509 // ------------------------------
511 // adjust the controls size to fit nicely in the toolbar
513 for ( node
= m_tools
.GetFirst(); node
; node
= node
->GetNext(), index
++ )
515 wxToolBarToolBase
*tool
= node
->GetData();
516 if ( !tool
->IsControl() )
519 wxControl
*control
= tool
->GetControl();
521 wxSize size
= control
->GetSize();
523 // the position of the leftmost controls corner
526 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
527 // latter only appeared in v4.70 of comctl32.dll
529 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT
,
530 index
, (LPARAM
)(LPRECT
)&r
) )
532 wxLogLastError("TB_GETITEMRECT");
535 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
536 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
537 // available in headers, now check whether it is available now
539 if ( wxTheApp
->GetComCtl32Version() >= 471 )
541 // set the (underlying) separators width to be that of the
544 tbbi
.cbSize
= sizeof(tbbi
);
545 tbbi
.dwMask
= TBIF_SIZE
;
547 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO
,
548 tool
->GetId(), (LPARAM
)&tbbi
) )
550 // the id is probably invalid?
551 wxLogLastError("TB_SETBUTTONINFO");
556 #endif // comctl32.dll 4.71
557 // TB_SETBUTTONINFO unavailable
559 // try adding several separators to fit the controls width
560 int widthSep
= r
.right
- r
.left
;
566 tbb
.fsState
= TBSTATE_ENABLED
;
567 tbb
.fsStyle
= TBSTYLE_SEP
;
569 size_t nSeparators
= size
.x
/ widthSep
;
570 for ( size_t nSep
= 0; nSep
< nSeparators
; nSep
++ )
572 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON
,
573 index
, (LPARAM
)&tbb
) )
575 wxLogLastError("TB_INSERTBUTTON");
581 // remember the number of separators we used - we'd have to
582 // delete all of them later
583 ((wxToolBarTool
*)tool
)->SetSeparatorsCount(nSeparators
);
585 // adjust the controls width to exactly cover the separators
586 control
->SetSize((nSeparators
+ 1)*widthSep
, -1);
589 // and position the control itself correctly vertically
590 int height
= r
.bottom
- r
.top
;
591 int diff
= height
- size
.y
;
594 // the control is too high, resize to fit
595 control
->SetSize(-1, height
- 2);
600 control
->Move(left
== -1 ? r
.left
: left
, r
.top
+ (diff
+ 1) / 2);
603 // the max index is the "real" number of buttons - i.e. counting even the
604 // separators which we added just for aligning the controls
609 if ( m_maxRows
== 0 )
611 // if not set yet, only one row
615 else if ( m_nButtons
> 0 ) // vertical non empty toolbar
617 if ( m_maxRows
== 0 )
619 // if not set yet, have one column
627 // ----------------------------------------------------------------------------
629 // ----------------------------------------------------------------------------
631 bool wxToolBar::MSWCommand(WXUINT cmd
, WXWORD id
)
633 wxToolBarToolBase
*tool
= FindById((int)id
);
637 if ( tool
->CanBeToggled() )
639 LRESULT state
= ::SendMessage(GetHwnd(), TB_GETSTATE
, id
, 0);
640 tool
->SetToggle((state
& TBSTATE_CHECKED
) != 0);
643 bool toggled
= tool
->IsToggled();
645 // OnLeftClick() can veto the button state change - for buttons which may
646 // be toggled only, of couse
647 if ( !OnLeftClick((int)id
, toggled
) && tool
->CanBeToggled() )
651 tool
->SetToggle(toggled
);
653 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
, id
, MAKELONG(toggled
, 0));
659 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl
),
663 // First check if this applies to us
664 NMHDR
*hdr
= (NMHDR
*)lParam
;
666 // the tooltips control created by the toolbar is sometimes Unicode, even
667 // in an ANSI application - this seems to be a bug in comctl32.dll v5
668 int code
= (int)hdr
->code
;
669 if ( (code
!= TTN_NEEDTEXTA
) && (code
!= TTN_NEEDTEXTW
) )
672 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
673 if ( toolTipWnd
!= hdr
->hwndFrom
)
676 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
677 int id
= (int)ttText
->hdr
.idFrom
;
679 wxToolBarToolBase
*tool
= FindById(id
);
683 const wxString
& help
= tool
->GetShortHelp();
685 if ( !help
.IsEmpty() )
687 if ( code
== TTN_NEEDTEXTA
)
689 ttText
->lpszText
= (wxChar
*)help
.c_str();
691 #if (_WIN32_IE >= 0x0300)
694 // FIXME this is a temp hack only until I understand better what
695 // must be done in both ANSI and Unicode builds
697 size_t lenAnsi
= help
.Len();
699 // MetroWerks doesn't like calling mbstowcs with NULL argument
700 size_t lenUnicode
= 2*lenAnsi
;
702 size_t lenUnicode
= mbstowcs(NULL
, help
, lenAnsi
);
705 // using the pointer of right type avoids us doing all sorts of
706 // pointer arithmetics ourselves
707 wchar_t *dst
= (wchar_t *)ttText
->szText
,
708 *pwz
= new wchar_t[lenUnicode
+ 1];
709 mbstowcs(pwz
, help
, lenAnsi
+ 1);
710 memcpy(dst
, pwz
, lenUnicode
*sizeof(wchar_t));
712 // put the terminating _wide_ NUL
717 #endif // _WIN32_IE >= 0x0300
720 // For backward compatibility...
721 OnMouseEnter(tool
->GetId());
726 // ----------------------------------------------------------------------------
728 // ----------------------------------------------------------------------------
730 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
732 wxToolBarBase::SetToolBitmapSize(size
);
734 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE
, 0, MAKELONG(size
.x
, size
.y
));
737 void wxToolBar::SetRows(int nRows
)
739 if ( nRows
== m_maxRows
)
741 // avoid resizing the frame uselessly
745 // TRUE in wParam means to create at least as many rows, FALSE -
748 ::SendMessage(GetHwnd(), TB_SETROWS
,
749 MAKEWPARAM(nRows
, !(GetWindowStyle() & wxTB_VERTICAL
)),
757 // The button size is bigger than the bitmap size
758 wxSize
wxToolBar::GetToolSize() const
760 // TB_GETBUTTONSIZE is supported from version 4.70
761 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 )
762 if ( wxTheApp
->GetComCtl32Version() >= 470 )
764 DWORD dw
= ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE
, 0, 0);
766 return wxSize(LOWORD(dw
), HIWORD(dw
));
769 #endif // comctl32.dll 4.70+
772 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
776 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
781 int index
= (int)::SendMessage(GetHwnd(), TB_HITTEST
, 0, (LPARAM
)&pt
);
784 // it's a separator or there is no tool at all there
785 return (wxToolBarToolBase
*)NULL
;
788 return m_tools
.Item((size_t)index
)->GetData();
791 void wxToolBar::UpdateSize()
793 // we must refresh the frame after the toolbar size (possibly) changed
794 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
797 // don't change the size, we just need to generate a WM_SIZE
799 if ( !GetWindowRect(GetHwndOf(frame
), &r
) )
801 wxLogLastError(_T("GetWindowRect"));
804 (void)::SendMessage(GetHwndOf(frame
), WM_SIZE
, SIZE_RESTORED
,
805 MAKELPARAM(r
.right
- r
.left
, r
.bottom
- r
.top
));
810 // ----------------------------------------------------------------------------
812 // ----------------------------------------------------------------------------
814 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
816 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON
,
817 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(enable
, 0));
820 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
822 ::SendMessage(GetHwnd(), TB_CHECKBUTTON
,
823 (WPARAM
)tool
->GetId(), (LPARAM
)MAKELONG(toggle
, 0));
826 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
)
828 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
829 // without, so we really need to delete the button and recreate it here
830 wxFAIL_MSG( _T("not implemented") );
833 // ----------------------------------------------------------------------------
835 // ----------------------------------------------------------------------------
837 // Responds to colour changes, and passes event on to children.
838 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
840 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
841 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
848 // Propagate the event to the non-top-level children
849 wxWindow::OnSysColourChanged(event
);
852 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
854 if (event
.RightDown())
856 // For now, we don't have an id. Later we could
857 // try finding the tool.
858 OnRightClick((int)-1, event
.GetX(), event
.GetY());
866 long wxToolBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
868 if ( nMsg
== WM_SIZE
)
870 // calculate our minor dimenstion ourselves - we're confusing the
871 // standard logic (TB_AUTOSIZE) with our horizontal toolbars and other
874 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT
, 0, (LPARAM
)&r
) )
878 if ( GetWindowStyle() & wxTB_VERTICAL
)
880 w
= r
.right
- r
.left
;
883 w
*= (m_nButtons
+ m_maxRows
- 1)/m_maxRows
;
890 h
= r
.bottom
- r
.top
;
893 h
+= 6; // FIXME: this is the separator line height...
898 if ( MAKELPARAM(w
, h
) != lParam
)
900 // size really changed
909 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
912 // ----------------------------------------------------------------------------
914 // ----------------------------------------------------------------------------
916 // These are the default colors used to map the bitmap colors to the current
917 // system colors. Note that they are in BGR format because this is what Windows
918 // wants (and not RGB)
920 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
921 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
922 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
923 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
924 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
925 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
927 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
929 COLORMAP ColorMap
[] =
931 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
932 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
933 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
934 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
935 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
936 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
939 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
941 for ( n
= 0; n
< NUM_MAPS
; n
++)
943 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
947 HDC hdcMem
= CreateCompatibleDC(NULL
);
951 hbmOld
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
954 for ( i
= 0; i
< width
; i
++)
956 for ( j
= 0; j
< height
; j
++)
958 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
960 BYTE red = GetRValue(pixel);
961 BYTE green = GetGValue(pixel);
962 BYTE blue = GetBValue(pixel);
965 for ( k
= 0; k
< NUM_MAPS
; k
++)
967 if ( ColorMap
[k
].from
== pixel
)
969 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
977 SelectObject(hdcMem
, hbmOld
);
978 DeleteObject(hdcMem
);
983 // Some experiments...
985 // What we want to do is create another bitmap which has a depth of 4,
986 // and set the bits. So probably we want to convert this HBITMAP into a
987 // DIB, then call SetDIBits.
988 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
989 // the screen), then SetDIBits fails.
990 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
991 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
992 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
995 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
997 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
999 DWORD err
= GetLastError();
1001 ::ReleaseDC(NULL
, dc
);
1004 GlobalUnlock (newDIB
);
1005 GlobalFree (newDIB
);
1007 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
1008 // Substitute our new bitmap for the old one
1009 ::DeleteObject((HBITMAP
) m_hBitmap
);
1010 m_hBitmap
= (WXHBITMAP
) newBitmap
;
1014 #endif // wxUSE_TOOLBAR && Win95