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
;
155 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
157 // Even with extended styles, need to combine with WS_BORDER
158 // for them to look right.
159 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
160 msflags
|= WS_BORDER
;
162 // Create the toolbar control.
163 HWND hWndToolbar
= CreateWindowEx
165 exStyle
, // Extended styles.
166 TOOLBARCLASSNAME
, // Class name for the toolbar.
167 "", // No default text.
169 x
, y
, width
, height
, // Standard toolbar size and position.
170 (HWND
) parent
->GetHWND(), // Parent window of the toolbar.
171 (HMENU
)m_windowId
, // Toolbar ID.
172 wxGetInstance(), // Current instance.
173 NULL
// No class data.
176 wxCHECK_MSG( hWndToolbar
, FALSE
, "Toolbar creation failed" );
178 // Toolbar-specific initialisation
179 ::SendMessage(hWndToolbar
, TB_BUTTONSTRUCTSIZE
,
180 (WPARAM
)sizeof(TBBUTTON
), (LPARAM
)0);
182 m_hWnd
= (WXHWND
) hWndToolbar
;
184 parent
->AddChild(this);
186 SubclassWin((WXHWND
)hWndToolbar
);
191 wxToolBar95::~wxToolBar95()
197 ::DeleteObject((HBITMAP
) m_hBitmap
);
202 bool wxToolBar95::CreateTools()
204 if (m_tools
.Number() == 0)
207 HBITMAP oldToolBarBitmap
= (HBITMAP
) m_hBitmap
;
209 int totalBitmapWidth
= (int)(m_defaultWidth
* m_tools
.Number());
210 int totalBitmapHeight
= (int)m_defaultHeight
;
212 // Create a bitmap for all the tool bitmaps
213 HDC dc
= ::GetDC(NULL
);
214 m_hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap(dc
, totalBitmapWidth
, totalBitmapHeight
);
215 ::ReleaseDC(NULL
, dc
);
217 // Now blit all the tools onto this bitmap
218 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
219 HBITMAP oldBitmap
= (HBITMAP
) ::SelectObject(memoryDC
, (HBITMAP
) m_hBitmap
);
221 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
223 wxNode
*node
= m_tools
.First();
227 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
228 if ((tool
->m_toolStyle
!= wxTOOL_STYLE_SEPARATOR
) && tool
->m_bitmap1
.Ok() && tool
->m_bitmap1
.GetHBITMAP())
230 // wxPalette *palette = tool->m_bitmap1->GetPalette();
232 HBITMAP oldBitmap2
= (HBITMAP
) ::SelectObject(memoryDC2
, (HBITMAP
) tool
->m_bitmap1
.GetHBITMAP());
233 /* int bltResult = */
234 BitBlt(memoryDC
, x
, 0, (int) m_defaultWidth
, (int) m_defaultHeight
, memoryDC2
,
236 ::SelectObject(memoryDC2
, oldBitmap2
);
237 x
+= (int)m_defaultWidth
;
242 ::SelectObject(memoryDC
, oldBitmap
);
243 ::DeleteDC(memoryDC
);
244 ::DeleteDC(memoryDC2
);
246 // Map to system colours
247 wxMapBitmap((HBITMAP
) m_hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
249 if ( oldToolBarBitmap
)
251 TBREPLACEBITMAP replaceBitmap
;
252 replaceBitmap
.hInstOld
= NULL
;
253 replaceBitmap
.hInstNew
= NULL
;
254 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
255 replaceBitmap
.nIDNew
= (UINT
) (HBITMAP
) m_hBitmap
;
256 replaceBitmap
.nButtons
= noButtons
;
257 if (::SendMessage((HWND
) GetHWND(), TB_REPLACEBITMAP
, (WPARAM
) 0, (LPARAM
) &replaceBitmap
) == -1)
258 wxMessageBox("Could not add bitmap to toolbar");
260 ::DeleteObject((HBITMAP
) oldToolBarBitmap
);
262 // Now delete all the buttons
266 // TODO: What about separators???? They don't have an id!
267 if ( ! ::SendMessage( (HWND
) GetHWND(), TB_DELETEBUTTON
, i
, 0 ) )
273 TBADDBITMAP addBitmap
;
275 addBitmap
.nID
= (UINT
)m_hBitmap
;
276 if (::SendMessage((HWND
) GetHWND(), TB_ADDBITMAP
, (WPARAM
) noButtons
, (LPARAM
) &addBitmap
) == -1)
277 wxMessageBox("Could not add bitmap to toolbar");
280 // Now add the buttons.
281 TBBUTTON buttons
[50];
283 node
= m_tools
.First();
288 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
289 if (tool
->m_toolStyle
== wxTOOL_STYLE_SEPARATOR
)
291 buttons
[i
].iBitmap
= 0;
292 buttons
[i
].idCommand
= 0;
294 buttons
[i
].fsState
= TBSTATE_ENABLED
;
295 buttons
[i
].fsStyle
= TBSTYLE_SEP
;
296 buttons
[i
].dwData
= 0L;
297 buttons
[i
].iString
= 0;
301 buttons
[i
].iBitmap
= bitmapId
;
302 buttons
[i
].idCommand
= tool
->m_index
;
304 buttons
[i
].fsState
= 0;
306 buttons
[i
].fsState
|= TBSTATE_ENABLED
;
307 if (tool
->m_toggleState
)
308 buttons
[i
].fsState
|= TBSTATE_CHECKED
;
309 buttons
[i
].fsStyle
= tool
->m_isToggle
? TBSTYLE_CHECK
: TBSTYLE_BUTTON
;
310 buttons
[i
].dwData
= 0L;
311 buttons
[i
].iString
= 0;
320 long rc
= ::SendMessage((HWND
) GetHWND(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)& buttons
);
322 wxCHECK_MSG( rc
, FALSE
, "failed to add buttons to the toolbar" );
324 (void)::SendMessage((HWND
) GetHWND(), TB_AUTOSIZE
, (WPARAM
)0, (LPARAM
) 0);
331 bool wxToolBar95::MSWCommand(WXUINT cmd
, WXWORD id
)
333 wxNode
*node
= m_tools
.Find((long)id
);
336 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
337 if (tool
->m_isToggle
)
338 tool
->m_toggleState
= (1 == (1 & (int)::SendMessage((HWND
) GetHWND(), TB_GETSTATE
, (WPARAM
) id
, (LPARAM
) 0)));
340 BOOL ret
= OnLeftClick((int)id
, tool
->m_toggleState
);
341 if (ret
== FALSE
&& tool
->m_isToggle
)
343 tool
->m_toggleState
= !tool
->m_toggleState
;
344 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)id
, (LPARAM
)MAKELONG(tool
->m_toggleState
, 0));
349 bool wxToolBar95::MSWNotify(WXWPARAM
WXUNUSED(wParam
),
353 // First check if this applies to us
354 NMHDR
*hdr
= (NMHDR
*)lParam
;
356 // the tooltips control created by the toolbar is sometimes Unicode, even in
357 // an ANSI application
358 if ( (hdr
->code
!= TTN_NEEDTEXTA
) && (hdr
->code
!= TTN_NEEDTEXTW
) )
361 HWND toolTipWnd
= (HWND
)::SendMessage((HWND
)GetHWND(), TB_GETTOOLTIPS
, 0, 0);
362 if ( toolTipWnd
!= hdr
->hwndFrom
)
365 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
)lParam
;
366 int id
= (int)ttText
->hdr
.idFrom
;
367 wxNode
*node
= m_tools
.Find((long)id
);
371 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
373 if ( tool
->m_shortHelpString
!= "" )
375 if ( hdr
->code
== TTN_NEEDTEXTA
)
377 ttText
->lpszText
= (char *)(const char *)tool
->m_shortHelpString
;
379 #if (_WIN32_IE >= 0x0300)
382 // FIXME this is a temp hack only until I understand better what
383 // must be done in both ANSI and Unicode builds
384 size_t lenAnsi
= tool
->m_shortHelpString
.Len();
386 wchar_t *pwz
= new wchar_t[lenAnsi
* 2 + 1];
388 size_t lenUnicode
= mbstowcs(NULL
, tool
->m_shortHelpString
, lenAnsi
);
389 wchar_t *pwz
= new wchar_t[lenUnicode
+ 1];
391 mbstowcs(pwz
, tool
->m_shortHelpString
, lenAnsi
+ 1);
392 memcpy(ttText
->szText
, pwz
,
393 (sizeof(ttText
->szText
) - 1)/sizeof(ttText
->szText
[0]));
394 ttText
->szText
[WXSIZEOF(ttText
->szText
)] = 0;
398 #endif // _WIN32_IE >= 0x0300
401 // For backward compatibility...
402 OnMouseEnter(tool
->m_index
);
407 void wxToolBar95::SetToolBitmapSize(const wxSize
& size
)
409 m_defaultWidth
= size
.x
;
410 m_defaultHeight
= size
.y
;
411 ::SendMessage((HWND
) GetHWND(), TB_SETBITMAPSIZE
, 0, (LPARAM
) MAKELONG ((int)size
.x
, (int)size
.y
));
414 void wxToolBar95::SetRows(int nRows
)
417 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(nRows
, TRUE
), (LPARAM
) & rect
);
418 m_maxWidth
= (rect
.right
- rect
.left
+ 2);
419 m_maxHeight
= (rect
.bottom
- rect
.top
+ 2);
422 wxSize
wxToolBar95::GetMaxSize() const
424 if ((m_maxWidth
== -1) || (m_maxHeight
== -1))
427 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(m_maxRows
, TRUE
), (LPARAM
) & rect
);
428 ((wxToolBar95
*)this)->m_maxWidth
= (rect
.right
- rect
.left
+ 2); // ???
429 ((wxToolBar95
*)this)->m_maxHeight
= (rect
.bottom
- rect
.top
+ 2); // ???
431 return wxSize(m_maxWidth
, m_maxHeight
);
434 void wxToolBar95::GetSize(int *w
, int *h
) const
436 wxWindow::GetSize(w
, h
);
437 // For some reason, the returned height is several pixels bigger than that
439 // Taking this fudge factor out now, it seems fine without it.
443 // The button size is bigger than the bitmap size
444 wxSize
wxToolBar95::GetToolSize() const
446 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
449 void wxToolBar95::EnableTool(int toolIndex
, bool enable
)
451 wxNode
*node
= m_tools
.Find((long)toolIndex
);
454 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
455 tool
->m_enabled
= enable
;
456 ::SendMessage((HWND
) GetHWND(), TB_ENABLEBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(enable
, 0));
460 void wxToolBar95::ToggleTool(int toolIndex
, bool toggle
)
462 wxNode
*node
= m_tools
.Find((long)toolIndex
);
465 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
466 if (tool
->m_isToggle
)
468 tool
->m_toggleState
= toggle
;
469 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(toggle
, 0));
474 bool wxToolBar95::GetToolState(int toolIndex
) const
476 return (::SendMessage((HWND
) GetHWND(), TB_ISBUTTONCHECKED
, (WPARAM
)toolIndex
, (LPARAM
)0) != 0);
479 void wxToolBar95::ClearTools()
481 // TODO: Don't know how to reset the toolbar bitmap, as yet.
482 // But adding tools and calling CreateTools should probably
483 // recreate a buttonbar OK.
484 wxToolBarBase::ClearTools();
487 // If pushedBitmap is NULL, a reversed version of bitmap is
488 // created and used as the pushed/toggled image.
489 // If toggle is TRUE, the button toggles between the two states.
490 wxToolBarTool
*wxToolBar95::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
491 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
, const wxString
& helpString1
, const wxString
& helpString2
)
493 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, wxNullBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
494 tool
->m_clientData
= clientData
;
499 tool
->m_x
= m_xMargin
;
504 tool
->m_y
= m_yMargin
;
506 tool
->SetSize(GetToolSize().x
, GetToolSize().y
);
508 m_tools
.Append((long)index
, tool
);
512 // Responds to colour changes, and passes event on to children.
513 void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent
& event
)
515 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
516 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
525 // Propagate the event to the non-top-level children
526 wxWindow::OnSysColourChanged(event
);
529 void wxToolBar95::OnMouseEvent(wxMouseEvent
& event
)
531 if (event
.RightDown())
533 // For now, we don't have an id. Later we could
534 // try finding the tool.
535 OnRightClick((int)-1, event
.GetX(), event
.GetY());
543 // These are the default colors used to map the bitmap colors
544 // to the current system colors
546 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
547 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
548 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
549 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
550 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
551 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
553 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
555 COLORMAP ColorMap
[] = {
556 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
557 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
558 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
559 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
560 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
561 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
564 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
566 for ( n
= 0; n
< NUM_MAPS
; n
++)
568 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
572 HDC hdcMem
= CreateCompatibleDC(NULL
);
576 hbmOld
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
579 for ( i
= 0; i
< width
; i
++)
581 for ( j
= 0; j
< height
; j
++)
583 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
585 BYTE red = GetRValue(pixel);
586 BYTE green = GetGValue(pixel);
587 BYTE blue = GetBValue(pixel);
590 for ( k
= 0; k
< NUM_MAPS
; k
++)
592 if ( ColorMap
[k
].from
== pixel
)
594 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
602 SelectObject(hdcMem
, hbmOld
);
603 DeleteObject(hdcMem
);
608 // Some experiments...
610 // What we want to do is create another bitmap which has a depth of 4,
611 // and set the bits. So probably we want to convert this HBITMAP into a
612 // DIB, then call SetDIBits.
613 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
614 // the screen), then SetDIBits fails.
615 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
616 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
617 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
620 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
622 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
624 DWORD err
= GetLastError();
626 ::ReleaseDC(NULL
, dc
);
629 GlobalUnlock (newDIB
);
632 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
633 // Substitute our new bitmap for the old one
634 ::DeleteObject((HBITMAP
) m_hBitmap
);
635 m_hBitmap
= (WXHBITMAP
) newBitmap
;