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__)
40 #include "wx/msw/gnuwin32/extra.h"
43 #include "wx/msw/dib.h"
44 #include "wx/tbar95.h"
46 #include "wx/msw/private.h"
50 #define TBSTYLE_LIST 0x1000
51 #define TBSTYLE_FLAT 0x0800
52 #define TBSTYLE_TRANSPARENT 0x8000
54 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
58 #define TB_GETSTYLE (WM_USER + 57)
59 #define TB_SETSTYLE (WM_USER + 56)
62 /* Hint from a newsgroup for custom flatbar drawing:
63 Set the TBSTYLE_CUSTOMERASE style, then handle the
64 NM_CUSTOMDRAW message and do your custom drawing.
67 #define DEFAULTBITMAPX 16
68 #define DEFAULTBITMAPY 15
69 #define DEFAULTBUTTONX 24
70 #define DEFAULTBUTTONY 24
71 #define DEFAULTBARHEIGHT 27
73 #if !USE_SHARED_LIBRARY
74 IMPLEMENT_DYNAMIC_CLASS(wxToolBar95
, wxToolBarBase
)
77 BEGIN_EVENT_TABLE(wxToolBar95
, wxToolBarBase
)
78 EVT_MOUSE_EVENTS(wxToolBar95::OnMouseEvent
)
79 EVT_SYS_COLOUR_CHANGED(wxToolBar95::OnSysColourChanged
)
82 static void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
);
84 wxToolBar95::wxToolBar95()
89 m_defaultWidth
= DEFAULTBITMAPX
;
90 m_defaultHeight
= DEFAULTBITMAPY
;
93 bool wxToolBar95::Create(wxWindow
*parent
,
100 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
101 GetGValue(GetSysColor(COLOR_BTNFACE
)),
102 GetBValue(GetSysColor(COLOR_BTNFACE
)));
103 m_foregroundColour
= *wxBLACK
;
105 wxASSERT_MSG( (style
& wxTB_VERTICAL
) == 0,
106 "Sorry, wxToolBar95 under Windows 95 only "
107 "supports horizontal orientation." );
114 m_defaultWidth
= DEFAULTBITMAPX
;
115 m_defaultHeight
= DEFAULTBITMAPY
;
118 m_windowStyle
= style
;
120 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
137 m_windowId
= (id
< 0 ? NewControlId() : id
);
139 if (style
& wxBORDER
)
140 msflags
|= WS_BORDER
;
141 msflags
|= WS_CHILD
| WS_VISIBLE
| TBSTYLE_TOOLTIPS
;
143 if (style
& wxTB_FLAT
)
145 if (wxTheApp
->GetComCtl32Version() > 400)
146 msflags
|= TBSTYLE_FLAT
;
149 // Create the toolbar control.
150 HWND hWndToolbar
= CreateWindowEx
152 0L, // No extended styles.
153 TOOLBARCLASSNAME
, // Class name for the toolbar.
154 "", // No default text.
156 x
, y
, width
, height
, // Standard toolbar size and position.
157 (HWND
) parent
->GetHWND(), // Parent window of the toolbar.
158 (HMENU
)m_windowId
, // Toolbar ID.
159 wxGetInstance(), // Current instance.
160 NULL
// No class data.
163 wxCHECK_MSG( hWndToolbar
, FALSE
, "Toolbar creation failed" );
165 // Toolbar-specific initialisation
166 ::SendMessage(hWndToolbar
, TB_BUTTONSTRUCTSIZE
,
167 (WPARAM
)sizeof(TBBUTTON
), (LPARAM
)0);
169 m_hWnd
= (WXHWND
) hWndToolbar
;
171 parent
->AddChild(this);
173 SubclassWin((WXHWND
)hWndToolbar
);
178 wxToolBar95::~wxToolBar95()
184 ::DeleteObject((HBITMAP
) m_hBitmap
);
189 bool wxToolBar95::CreateTools()
191 if (m_tools
.Number() == 0)
194 HBITMAP oldToolBarBitmap
= (HBITMAP
) m_hBitmap
;
196 int totalBitmapWidth
= (int)(m_defaultWidth
* m_tools
.Number());
197 int totalBitmapHeight
= (int)m_defaultHeight
;
199 // Create a bitmap for all the tool bitmaps
200 HDC dc
= ::GetDC(NULL
);
201 m_hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap(dc
, totalBitmapWidth
, totalBitmapHeight
);
202 ::ReleaseDC(NULL
, dc
);
204 // Now blit all the tools onto this bitmap
205 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
206 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, (HBITMAP
) m_hBitmap
);
208 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
210 wxNode
*node
= m_tools
.First();
214 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
215 if ((tool
->m_toolStyle
!= wxTOOL_STYLE_SEPARATOR
) && tool
->m_bitmap1
.Ok() && tool
->m_bitmap1
.GetHBITMAP())
217 // wxPalette *palette = tool->m_bitmap1->GetPalette();
219 HBITMAP oldBitmap2
= (HBITMAP
) ::SelectObject(memoryDC2
, (HBITMAP
) tool
->m_bitmap1
.GetHBITMAP());
220 /* int bltResult = */
221 BitBlt(memoryDC
, x
, 0, (int) m_defaultWidth
, (int) m_defaultHeight
, memoryDC2
,
223 ::SelectObject(memoryDC2
, oldBitmap2
);
224 x
+= (int)m_defaultWidth
;
229 ::SelectObject(memoryDC
, oldBitmap
);
230 ::DeleteDC(memoryDC
);
231 ::DeleteDC(memoryDC2
);
233 // Map to system colours
234 wxMapBitmap((HBITMAP
) m_hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
236 if ( oldToolBarBitmap
)
238 TBREPLACEBITMAP replaceBitmap
;
239 replaceBitmap
.hInstOld
= NULL
;
240 replaceBitmap
.hInstNew
= NULL
;
241 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
242 replaceBitmap
.nIDNew
= (UINT
) (HBITMAP
) m_hBitmap
;
243 replaceBitmap
.nButtons
= noButtons
;
244 if (::SendMessage((HWND
) GetHWND(), TB_REPLACEBITMAP
, (WPARAM
) 0, (LPARAM
) &replaceBitmap
) == -1)
245 wxMessageBox("Could not add bitmap to toolbar");
247 ::DeleteObject((HBITMAP
) oldToolBarBitmap
);
249 // Now delete all the buttons
253 // TODO: What about separators???? They don't have an id!
254 if ( ! ::SendMessage( (HWND
) GetHWND(), TB_DELETEBUTTON
, i
, 0 ) )
260 TBADDBITMAP addBitmap
;
262 addBitmap
.nID
= (UINT
)m_hBitmap
;
263 if (::SendMessage((HWND
) GetHWND(), TB_ADDBITMAP
, (WPARAM
) noButtons
, (LPARAM
) &addBitmap
) == -1)
264 wxMessageBox("Could not add bitmap to toolbar");
267 // Now add the buttons.
268 TBBUTTON buttons
[50];
270 node
= m_tools
.First();
275 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
276 if (tool
->m_toolStyle
== wxTOOL_STYLE_SEPARATOR
)
278 buttons
[i
].iBitmap
= 0;
279 buttons
[i
].idCommand
= 0;
281 buttons
[i
].fsState
= TBSTATE_ENABLED
;
282 buttons
[i
].fsStyle
= TBSTYLE_SEP
;
283 buttons
[i
].dwData
= 0L;
284 buttons
[i
].iString
= 0;
288 buttons
[i
].iBitmap
= bitmapId
;
289 buttons
[i
].idCommand
= tool
->m_index
;
291 buttons
[i
].fsState
= 0;
293 buttons
[i
].fsState
|= TBSTATE_ENABLED
;
294 if (tool
->m_toggleState
)
295 buttons
[i
].fsState
|= TBSTATE_CHECKED
;
296 buttons
[i
].fsStyle
= tool
->m_isToggle
? TBSTYLE_CHECK
: TBSTYLE_BUTTON
;
297 buttons
[i
].dwData
= 0L;
298 buttons
[i
].iString
= 0;
307 long rc
= ::SendMessage((HWND
) GetHWND(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)& buttons
);
309 wxCHECK_MSG( rc
, FALSE
, "failed to add buttons to the toolbar" );
311 (void)::SendMessage((HWND
) GetHWND(), TB_AUTOSIZE
, (WPARAM
)0, (LPARAM
) 0);
318 bool wxToolBar95::MSWCommand(WXUINT cmd
, WXWORD id
)
320 wxNode
*node
= m_tools
.Find((long)id
);
323 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
324 if (tool
->m_isToggle
)
325 tool
->m_toggleState
= (1 == (1 & (int)::SendMessage((HWND
) GetHWND(), TB_GETSTATE
, (WPARAM
) id
, (LPARAM
) 0)));
327 BOOL ret
= OnLeftClick((int)id
, tool
->m_toggleState
);
328 if (ret
== FALSE
&& tool
->m_isToggle
)
330 tool
->m_toggleState
= !tool
->m_toggleState
;
331 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)id
, (LPARAM
)MAKELONG(tool
->m_toggleState
, 0));
336 bool wxToolBar95::MSWNotify(WXWPARAM
WXUNUSED(wParam
),
340 // First check if this applies to us
341 NMHDR
*hdr
= (NMHDR
*)lParam
;
343 // the tooltips control created by the toolbar is sometimes Unicode, even in
344 // an ANSI application
345 if ( (hdr
->code
!= TTN_NEEDTEXTA
) && (hdr
->code
!= TTN_NEEDTEXTW
) )
348 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
349 if ( toolTipWnd
!= hdr
->hwndFrom
)
352 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
353 int id
= (int)ttText
->hdr
.idFrom
;
354 wxNode
*node
= m_tools
.Find((long)id
);
358 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
360 if ( tool
->m_shortHelpString
!= "" )
362 if ( hdr
->code
== TTN_NEEDTEXTA
)
364 ttText
->lpszText
= (char *)(const char *)tool
->m_shortHelpString
;
366 #if (_WIN32_IE >= 0x0300)
369 // FIXME this is a temp hack only until I understand better what
370 // must be done in both ANSI and Unicode builds
371 size_t lenAnsi
= tool
->m_shortHelpString
.Len();
372 size_t lenUnicode
= mbstowcs(NULL
, tool
->m_shortHelpString
, lenAnsi
);
373 wchar_t *pwz
= new wchar_t[lenUnicode
+ 1];
374 mbstowcs(pwz
, tool
->m_shortHelpString
, lenAnsi
+ 1);
375 memcpy(ttText
->szText
, pwz
,
376 (sizeof(ttText
->szText
) - 1)/sizeof(ttText
->szText
[0]));
377 ttText
->szText
[WXSIZEOF(ttText
->szText
)] = 0;
381 #endif // _WIN32_IE >= 0x0300
384 // For backward compatibility...
385 OnMouseEnter(tool
->m_index
);
390 void wxToolBar95::SetToolBitmapSize(const wxSize
& size
)
392 m_defaultWidth
= size
.x
;
393 m_defaultHeight
= size
.y
;
394 ::SendMessage((HWND
) GetHWND(), TB_SETBITMAPSIZE
, 0, (LPARAM
) MAKELONG ((int)size
.x
, (int)size
.y
));
397 void wxToolBar95::SetRows(int nRows
)
400 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(nRows
, TRUE
), (LPARAM
) & rect
);
401 m_maxWidth
= (rect
.right
- rect
.left
+ 2);
402 m_maxHeight
= (rect
.bottom
- rect
.top
+ 2);
405 wxSize
wxToolBar95::GetMaxSize() const
407 if ((m_maxWidth
== -1) || (m_maxHeight
== -1))
410 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(m_maxRows
, TRUE
), (LPARAM
) & rect
);
411 ((wxToolBar95
*)this)->m_maxWidth
= (rect
.right
- rect
.left
+ 2); // ???
412 ((wxToolBar95
*)this)->m_maxHeight
= (rect
.bottom
- rect
.top
+ 2); // ???
414 return wxSize(m_maxWidth
, m_maxHeight
);
417 void wxToolBar95::GetSize(int *w
, int *h
) const
419 wxWindow::GetSize(w
, h
);
420 // For some reason, the returned height is several pixels bigger than that
425 // The button size is bigger than the bitmap size
426 wxSize
wxToolBar95::GetToolSize() const
428 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
431 void wxToolBar95::EnableTool(int toolIndex
, bool enable
)
433 wxNode
*node
= m_tools
.Find((long)toolIndex
);
436 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
437 tool
->m_enabled
= enable
;
438 ::SendMessage((HWND
) GetHWND(), TB_ENABLEBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(enable
, 0));
442 void wxToolBar95::ToggleTool(int toolIndex
, bool toggle
)
444 wxNode
*node
= m_tools
.Find((long)toolIndex
);
447 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
448 if (tool
->m_isToggle
)
450 tool
->m_toggleState
= toggle
;
451 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(toggle
, 0));
456 bool wxToolBar95::GetToolState(int toolIndex
) const
458 return (::SendMessage((HWND
) GetHWND(), TB_ISBUTTONCHECKED
, (WPARAM
)toolIndex
, (LPARAM
)0) != 0);
461 void wxToolBar95::ClearTools()
463 // TODO: Don't know how to reset the toolbar bitmap, as yet.
464 // But adding tools and calling CreateTools should probably
465 // recreate a buttonbar OK.
466 wxToolBarBase::ClearTools();
469 // If pushedBitmap is NULL, a reversed version of bitmap is
470 // created and used as the pushed/toggled image.
471 // If toggle is TRUE, the button toggles between the two states.
472 wxToolBarTool
*wxToolBar95::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
473 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
, const wxString
& helpString1
, const wxString
& helpString2
)
475 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, wxNullBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
476 tool
->m_clientData
= clientData
;
481 tool
->m_x
= m_xMargin
;
486 tool
->m_y
= m_yMargin
;
488 tool
->SetSize(GetDefaultButtonWidth(), GetDefaultButtonHeight());
490 m_tools
.Append((long)index
, tool
);
494 // Responds to colour changes, and passes event on to children.
495 void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent
& event
)
497 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
498 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
507 // Propagate the event to the non-top-level children
508 wxWindow::OnSysColourChanged(event
);
511 void wxToolBar95::OnMouseEvent(wxMouseEvent
& event
)
513 if (event
.RightDown())
515 // For now, we don't have an id. Later we could
516 // try finding the tool.
517 OnRightClick((int)-1, event
.GetX(), event
.GetY());
525 // These are the default colors used to map the bitmap colors
526 // to the current system colors
528 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
529 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
530 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
531 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
532 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
533 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
535 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
537 COLORMAP ColorMap
[] = {
538 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
539 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
540 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
541 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
542 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
543 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
546 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
548 for ( n
= 0; n
< NUM_MAPS
; n
++)
550 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
554 HDC hdcMem
= CreateCompatibleDC(NULL
);
558 hbmOld
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
561 for ( i
= 0; i
< width
; i
++)
563 for ( j
= 0; j
< height
; j
++)
565 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
567 BYTE red = GetRValue(pixel);
568 BYTE green = GetGValue(pixel);
569 BYTE blue = GetBValue(pixel);
572 for ( k
= 0; k
< NUM_MAPS
; k
++)
574 if ( ColorMap
[k
].from
== pixel
)
576 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
584 SelectObject(hdcMem
, hbmOld
);
585 DeleteObject(hdcMem
);
590 // Some experiments...
592 // What we want to do is create another bitmap which has a depth of 4,
593 // and set the bits. So probably we want to convert this HBITMAP into a
594 // DIB, then call SetDIBits.
595 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
596 // the screen), then SetDIBits fails.
597 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
598 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
599 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
602 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
604 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
606 DWORD err
= GetLastError();
608 ::ReleaseDC(NULL
, dc
);
611 GlobalUnlock (newDIB
);
614 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
615 // Substitute our new bitmap for the old one
616 ::DeleteObject((HBITMAP
) m_hBitmap
);
617 m_hBitmap
= (WXHBITMAP
) newBitmap
;