1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxToolBar95
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "tbar95.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #if wxUSE_BUTTONBAR && wxUSE_TOOLBAR && defined(__WIN95__)
29 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
35 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
41 #include "wx/msw/gnuwin32/extra.h"
45 #include "wx/msw/dib.h"
46 #include "wx/tbar95.h"
48 #include "wx/msw/private.h"
52 #define TBSTYLE_LIST 0x1000
53 #define TBSTYLE_FLAT 0x0800
54 #define TBSTYLE_TRANSPARENT 0x8000
56 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
60 #define TB_GETSTYLE (WM_USER + 57)
61 #define TB_SETSTYLE (WM_USER + 56)
64 /* Hint from a newsgroup for custom flatbar drawing:
65 Set the TBSTYLE_CUSTOMERASE style, then handle the
66 NM_CUSTOMDRAW message and do your custom drawing.
69 #define DEFAULTBITMAPX 16
70 #define DEFAULTBITMAPY 15
71 #define DEFAULTBUTTONX 24
72 #define DEFAULTBUTTONY 24
73 #define DEFAULTBARHEIGHT 27
75 #if !USE_SHARED_LIBRARY
76 IMPLEMENT_DYNAMIC_CLASS(wxToolBar95
, wxToolBarBase
)
79 BEGIN_EVENT_TABLE(wxToolBar95
, wxToolBarBase
)
80 EVT_SIZE(wxToolBar95::OnSize
)
81 EVT_PAINT(wxToolBar95::OnPaint
)
82 EVT_MOUSE_EVENTS(wxToolBar95::OnMouseEvent
)
83 EVT_KILL_FOCUS(wxToolBar95::OnKillFocus
)
84 EVT_SYS_COLOUR_CHANGED(wxToolBar95::OnSysColourChanged
)
87 static void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
);
89 wxToolBar95::wxToolBar95()
94 m_defaultWidth
= DEFAULTBITMAPX
;
95 m_defaultHeight
= DEFAULTBITMAPY
;
98 bool wxToolBar95::Create(wxWindow
*parent
,
103 const wxString
& name
)
105 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
106 GetGValue(GetSysColor(COLOR_BTNFACE
)),
107 GetBValue(GetSysColor(COLOR_BTNFACE
)));
108 m_foregroundColour
= *wxBLACK
;
110 wxASSERT_MSG( (style
& wxTB_VERTICAL
) == 0,
111 "Sorry, wxToolBar95 under Windows 95 only "
112 "supports horizontal orientation." );
119 m_defaultWidth
= DEFAULTBITMAPX
;
120 m_defaultHeight
= DEFAULTBITMAPY
;
123 m_windowStyle
= style
;
125 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
142 m_windowId
= (id
< 0 ? NewControlId() : id
);
144 if (style
& wxBORDER
)
145 msflags
|= WS_BORDER
;
146 msflags
|= WS_CHILD
| WS_VISIBLE
| TBSTYLE_TOOLTIPS
;
148 if (style
& wxTB_FLAT
)
150 if (wxTheApp
->GetComCtl32Version() > 400)
151 msflags
|= TBSTYLE_FLAT
;
154 // Create the toolbar control.
155 HWND hWndToolbar
= CreateWindowEx
157 0L, // No extended styles.
158 TOOLBARCLASSNAME
, // Class name for the toolbar.
159 "", // No default text.
161 x
, y
, width
, height
, // Standard toolbar size and position.
162 (HWND
) parent
->GetHWND(), // Parent window of the toolbar.
163 (HMENU
)m_windowId
, // Toolbar ID.
164 wxGetInstance(), // Current instance.
165 NULL
// No class data.
168 wxCHECK_MSG( hWndToolbar
, FALSE
, "Toolbar creation failed" );
170 // Toolbar-specific initialisation
171 ::SendMessage(hWndToolbar
, TB_BUTTONSTRUCTSIZE
,
172 (WPARAM
)sizeof(TBBUTTON
), (LPARAM
)0);
174 m_hWnd
= (WXHWND
) hWndToolbar
;
176 parent
->AddChild(this);
178 SubclassWin((WXHWND
)hWndToolbar
);
183 wxToolBar95::~wxToolBar95()
189 ::DeleteObject((HBITMAP
) m_hBitmap
);
194 bool wxToolBar95::CreateTools()
196 if (m_tools
.Number() == 0)
199 HBITMAP oldToolBarBitmap
= (HBITMAP
) m_hBitmap
;
201 int totalBitmapWidth
= (int)(m_defaultWidth
* m_tools
.Number());
202 int totalBitmapHeight
= (int)m_defaultHeight
;
204 // Create a bitmap for all the tool bitmaps
205 HDC dc
= ::GetDC(NULL
);
206 m_hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap(dc
, totalBitmapWidth
, totalBitmapHeight
);
207 ::ReleaseDC(NULL
, dc
);
209 // Now blit all the tools onto this bitmap
210 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
211 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, (HBITMAP
) m_hBitmap
);
213 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
215 wxNode
*node
= m_tools
.First();
219 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
220 if ((tool
->m_toolStyle
!= wxTOOL_STYLE_SEPARATOR
) && tool
->m_bitmap1
.Ok() && tool
->m_bitmap1
.GetHBITMAP())
222 // wxPalette *palette = tool->m_bitmap1->GetPalette();
224 HBITMAP oldBitmap2
= (HBITMAP
) ::SelectObject(memoryDC2
, (HBITMAP
) tool
->m_bitmap1
.GetHBITMAP());
225 /* int bltResult = */
226 BitBlt(memoryDC
, x
, 0, (int) m_defaultWidth
, (int) m_defaultHeight
, memoryDC2
,
228 ::SelectObject(memoryDC2
, oldBitmap2
);
229 x
+= (int)m_defaultWidth
;
234 ::SelectObject(memoryDC
, oldBitmap
);
235 ::DeleteDC(memoryDC
);
236 ::DeleteDC(memoryDC2
);
238 // Map to system colours
239 wxMapBitmap((HBITMAP
) m_hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
241 if ( oldToolBarBitmap
)
243 TBREPLACEBITMAP replaceBitmap
;
244 replaceBitmap
.hInstOld
= NULL
;
245 replaceBitmap
.hInstNew
= NULL
;
246 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
247 replaceBitmap
.nIDNew
= (UINT
) (HBITMAP
) m_hBitmap
;
248 replaceBitmap
.nButtons
= noButtons
;
249 if (::SendMessage((HWND
) GetHWND(), TB_REPLACEBITMAP
, (WPARAM
) 0, (LPARAM
) &replaceBitmap
) == -1)
250 wxMessageBox("Could not add bitmap to toolbar");
252 ::DeleteObject((HBITMAP
) oldToolBarBitmap
);
254 // Now delete all the buttons
258 // TODO: What about separators???? They don't have an id!
259 if ( ! ::SendMessage( (HWND
) GetHWND(), TB_DELETEBUTTON
, i
, 0 ) )
265 TBADDBITMAP addBitmap
;
267 addBitmap
.nID
= (UINT
)m_hBitmap
;
268 if (::SendMessage((HWND
) GetHWND(), TB_ADDBITMAP
, (WPARAM
) noButtons
, (LPARAM
) &addBitmap
) == -1)
269 wxMessageBox("Could not add bitmap to toolbar");
272 // Now add the buttons.
273 TBBUTTON buttons
[50];
275 node
= m_tools
.First();
280 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
281 if (tool
->m_toolStyle
== wxTOOL_STYLE_SEPARATOR
)
283 buttons
[i
].iBitmap
= 0;
284 buttons
[i
].idCommand
= 0;
286 buttons
[i
].fsState
= TBSTATE_ENABLED
;
287 buttons
[i
].fsStyle
= TBSTYLE_SEP
;
288 buttons
[i
].dwData
= 0L;
289 buttons
[i
].iString
= 0;
293 buttons
[i
].iBitmap
= bitmapId
;
294 buttons
[i
].idCommand
= tool
->m_index
;
296 buttons
[i
].fsState
= 0;
298 buttons
[i
].fsState
|= TBSTATE_ENABLED
;
299 if (tool
->m_toggleState
)
300 buttons
[i
].fsState
|= TBSTATE_CHECKED
;
301 buttons
[i
].fsStyle
= tool
->m_isToggle
? TBSTYLE_CHECK
: TBSTYLE_BUTTON
;
302 buttons
[i
].dwData
= 0L;
303 buttons
[i
].iString
= 0;
312 long rc
= ::SendMessage((HWND
) GetHWND(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)& buttons
);
314 wxCHECK_MSG( rc
, FALSE
, "failed to add buttons to the toolbar" );
316 (void)::SendMessage((HWND
) GetHWND(), TB_AUTOSIZE
, (WPARAM
)0, (LPARAM
) 0);
323 bool wxToolBar95::MSWCommand(WXUINT cmd
, WXWORD id
)
325 wxNode
*node
= m_tools
.Find((long)id
);
328 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
329 if (tool
->m_isToggle
)
330 tool
->m_toggleState
= (1 == (1 & (int)::SendMessage((HWND
) GetHWND(), TB_GETSTATE
, (WPARAM
) id
, (LPARAM
) 0)));
332 BOOL ret
= OnLeftClick((int)id
, tool
->m_toggleState
);
333 if (ret
== FALSE
&& tool
->m_isToggle
)
335 tool
->m_toggleState
= !tool
->m_toggleState
;
336 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)id
, (LPARAM
)MAKELONG(tool
->m_toggleState
, 0));
341 bool wxToolBar95::MSWNotify(WXWPARAM
WXUNUSED(wParam
),
345 // First check if this applies to us
346 NMHDR
*hdr
= (NMHDR
*)lParam
;
348 // the tooltips control created by the toolbar is sometimes Unicode, even in
349 // an ANSI application
350 if ( (hdr
->code
!= TTN_NEEDTEXTA
) && (hdr
->code
!= TTN_NEEDTEXTW
) )
353 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
354 if ( toolTipWnd
!= hdr
->hwndFrom
)
357 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
358 int id
= (int)ttText
->hdr
.idFrom
;
359 wxNode
*node
= m_tools
.Find((long)id
);
363 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
365 if ( tool
->m_shortHelpString
!= "" )
367 if ( hdr
->code
== TTN_NEEDTEXTA
)
369 ttText
->lpszText
= (char *)(const char *)tool
->m_shortHelpString
;
371 #if (_WIN32_IE >= 0x0300)
374 // FIXME this is a temp hack only until I understand better what
375 // must be done in both ANSI and Unicode builds
376 size_t lenAnsi
= tool
->m_shortHelpString
.Len();
377 size_t lenUnicode
= mbstowcs(NULL
, tool
->m_shortHelpString
, lenAnsi
);
378 wchar_t *pwz
= new wchar_t[lenUnicode
+ 1];
379 mbstowcs(pwz
, tool
->m_shortHelpString
, lenAnsi
+ 1);
380 memcpy(ttText
->szText
, pwz
,
381 (sizeof(ttText
->szText
) - 1)/sizeof(ttText
->szText
[0]));
382 ttText
->szText
[WXSIZEOF(ttText
->szText
)] = 0;
386 #endif // _WIN32_IE >= 0x0300
389 // For backward compatibility...
390 OnMouseEnter(tool
->m_index
);
395 void wxToolBar95::SetToolBitmapSize(const wxSize
& size
)
397 m_defaultWidth
= size
.x
;
398 m_defaultHeight
= size
.y
;
399 ::SendMessage((HWND
) GetHWND(), TB_SETBITMAPSIZE
, 0, (LPARAM
) MAKELONG ((int)size
.x
, (int)size
.y
));
402 void wxToolBar95::SetRows(int nRows
)
405 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(nRows
, TRUE
), (LPARAM
) & rect
);
406 m_maxWidth
= (rect
.right
- rect
.left
+ 2);
407 m_maxHeight
= (rect
.bottom
- rect
.top
+ 2);
410 wxSize
wxToolBar95::GetMaxSize() const
412 if ((m_maxWidth
== -1) || (m_maxHeight
== -1))
415 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(m_maxRows
, TRUE
), (LPARAM
) & rect
);
416 ((wxToolBar95
*)this)->m_maxWidth
= (rect
.right
- rect
.left
+ 2); // ???
417 ((wxToolBar95
*)this)->m_maxHeight
= (rect
.bottom
- rect
.top
+ 2); // ???
419 return wxSize(m_maxWidth
, m_maxHeight
);
422 void wxToolBar95::GetSize(int *w
, int *h
) const
424 wxWindow::GetSize(w
, h
);
425 // For some reason, the returned height is several pixels bigger than that
430 // The button size is bigger than the bitmap size
431 wxSize
wxToolBar95::GetToolSize() const
433 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
436 void wxToolBar95::EnableTool(int toolIndex
, bool enable
)
438 wxNode
*node
= m_tools
.Find((long)toolIndex
);
441 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
442 tool
->m_enabled
= enable
;
443 ::SendMessage((HWND
) GetHWND(), TB_ENABLEBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(enable
, 0));
447 void wxToolBar95::ToggleTool(int toolIndex
, bool toggle
)
449 wxNode
*node
= m_tools
.Find((long)toolIndex
);
452 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
453 if (tool
->m_isToggle
)
455 tool
->m_toggleState
= toggle
;
456 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(toggle
, 0));
461 bool wxToolBar95::GetToolState(int toolIndex
) const
463 return (::SendMessage((HWND
) GetHWND(), TB_ISBUTTONCHECKED
, (WPARAM
)toolIndex
, (LPARAM
)0) != 0);
466 void wxToolBar95::ClearTools()
468 // TODO: Don't know how to reset the toolbar bitmap, as yet.
469 // But adding tools and calling CreateTools should probably
470 // recreate a buttonbar OK.
471 wxToolBarBase::ClearTools();
474 // If pushedBitmap is NULL, a reversed version of bitmap is
475 // created and used as the pushed/toggled image.
476 // If toggle is TRUE, the button toggles between the two states.
477 wxToolBarTool
*wxToolBar95::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
478 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
, const wxString
& helpString1
, const wxString
& helpString2
)
480 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, wxNullBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
481 tool
->m_clientData
= clientData
;
486 tool
->m_x
= m_xMargin
;
491 tool
->m_y
= m_yMargin
;
493 tool
->SetSize(GetToolSize().x
, GetToolSize().y
);
495 m_tools
.Append((long)index
, tool
);
499 // Responds to colour changes, and passes event on to children.
500 void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent
& event
)
502 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
503 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
512 // Propagate the event to the non-top-level children
513 wxWindow::OnSysColourChanged(event
);
516 void wxToolBar95::OnMouseEvent(wxMouseEvent
& event
)
518 if (event
.RightDown())
520 // For now, we don't have an id. Later we could
521 // try finding the tool.
522 OnRightClick((int)-1, event
.GetX(), event
.GetY());
530 // These are the default colors used to map the bitmap colors
531 // to the current system colors
533 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
534 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
535 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
536 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
537 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
538 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
540 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
542 COLORMAP ColorMap
[] = {
543 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
544 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
545 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
546 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
547 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
548 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
551 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
553 for ( n
= 0; n
< NUM_MAPS
; n
++)
555 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
559 HDC hdcMem
= CreateCompatibleDC(NULL
);
563 hbmOld
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
566 for ( i
= 0; i
< width
; i
++)
568 for ( j
= 0; j
< height
; j
++)
570 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
572 BYTE red = GetRValue(pixel);
573 BYTE green = GetGValue(pixel);
574 BYTE blue = GetBValue(pixel);
577 for ( k
= 0; k
< NUM_MAPS
; k
++)
579 if ( ColorMap
[k
].from
== pixel
)
581 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
589 SelectObject(hdcMem
, hbmOld
);
590 DeleteObject(hdcMem
);
595 // Some experiments...
597 // What we want to do is create another bitmap which has a depth of 4,
598 // and set the bits. So probably we want to convert this HBITMAP into a
599 // DIB, then call SetDIBits.
600 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
601 // the screen), then SetDIBits fails.
602 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
603 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
604 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
607 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
609 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
611 DWORD err
= GetLastError();
613 ::ReleaseDC(NULL
, dc
);
616 GlobalUnlock (newDIB
);
619 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
620 // Substitute our new bitmap for the old one
621 ::DeleteObject((HBITMAP
) m_hBitmap
);
622 m_hBitmap
= (WXHBITMAP
) newBitmap
;