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 USE_BUTTONBAR && USE_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"
49 #define TBSTYLE_LIST 0x1000
50 #define TBSTYLE_FLAT 0x0800
51 #define TBSTYLE_TRANSPARENT 0x8000
53 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
55 #if !USE_SHARED_LIBRARY
56 IMPLEMENT_DYNAMIC_CLASS(wxToolBar95
, wxToolBarBase
)
58 BEGIN_EVENT_TABLE(wxToolBar95
, wxToolBarBase
)
59 EVT_SIZE(wxToolBar95::OnSize
)
60 EVT_PAINT(wxToolBar95::OnPaint
)
61 EVT_KILL_FOCUS(wxToolBar95::OnKillFocus
)
62 EVT_MOUSE_EVENTS(wxToolBar95::OnMouseEvent
)
63 EVT_SYS_COLOUR_CHANGED(wxToolBar95::OnSysColourChanged
)
67 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
);
69 wxToolBar95::wxToolBar95(void)
71 m_tilingDirection
= wxVERTICAL
;
73 m_currentRowsOrColumns
= 0;
77 m_defaultWidth
= DEFAULTBITMAPX
;
78 m_defaultHeight
= DEFAULTBITMAPY
;
81 bool wxToolBar95::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
82 long style
, int orientation
,
83 int RowsOrColumns
, const wxString
& name
)
85 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
86 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
87 m_foregroundColour
= *wxBLACK
;
89 m_defaultForegroundColour
= *wxBLACK
;
90 m_defaultBackgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
91 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
93 m_tilingDirection
= orientation
;
94 if (m_tilingDirection
== wxHORIZONTAL
)
95 wxMessageBox("Sorry, wxToolBar95 under Windows 95 only supports vertical tiling.\nPass the number of rows.", "wxToolBar95 usage", wxOK
);
96 m_rowsOrColumns
= RowsOrColumns
;
97 m_currentRowsOrColumns
= 0;
103 m_defaultWidth
= DEFAULTBITMAPX
;
104 m_defaultHeight
= DEFAULTBITMAPY
;
112 m_windowStyle
= style
;
114 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
119 if (style
& wxBORDER
)
120 msflags
|= WS_BORDER
;
121 msflags
|= WS_CHILD
| WS_VISIBLE
;
132 m_windowId
= (id
< 0 ? NewControlId() : id
);
133 DWORD msStyle
= WS_CHILD
| WS_BORDER
| WS_VISIBLE
| TBSTYLE_TOOLTIPS
;
135 if (style
& wxTB_FLAT
)
137 if (wxTheApp
->GetComCtl32Version() > 400)
138 msStyle
|= TBSTYLE_FLAT
;
141 // Create the toolbar control.
142 HWND hWndToolbar
= CreateWindowEx(0L, // No extended styles.
143 TOOLBARCLASSNAME
, // Class name for the toolbar.
144 "", // No default text.
145 msStyle
, // Styles and defaults.
146 x
, y
, width
, height
, // Standard toolbar size and position.
147 (HWND
) parent
->GetHWND(), // Parent window of the toolbar.
148 (HMENU
)m_windowId
, // Toolbar ID.
149 wxGetInstance(), // Current instance.
150 NULL
); // No class data.
152 // Toolbar-specific initialisation
153 ::SendMessage(hWndToolbar
, TB_BUTTONSTRUCTSIZE
, (WPARAM
)sizeof(TBBUTTON
), (LPARAM
)0);
155 m_hWnd
= (WXHWND
) hWndToolbar
;
156 if (parent
) parent
->AddChild(this);
158 SubclassWin((WXHWND
) hWndToolbar
);
163 wxToolBar95::~wxToolBar95(void)
169 ::DeleteObject((HBITMAP
) m_hBitmap
);
174 bool wxToolBar95::CreateTools(void)
176 if (m_tools
.Number() == 0)
179 HBITMAP oldToolBarBitmap
= (HBITMAP
) m_hBitmap
;
181 int totalBitmapWidth
= (int)(m_defaultWidth
* m_tools
.Number());
182 int totalBitmapHeight
= (int)m_defaultHeight
;
184 // Create a bitmap for all the tool bitmaps
185 HDC dc
= ::GetDC(NULL
);
186 m_hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap(dc
, totalBitmapWidth
, totalBitmapHeight
);
187 ::ReleaseDC(NULL
, dc
);
189 // Now blit all the tools onto this bitmap
190 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
191 HBITMAP oldBitmap
= ::SelectObject(memoryDC
, (HBITMAP
) m_hBitmap
);
193 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
195 wxNode
*node
= m_tools
.First();
199 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
200 if ((tool
->m_toolStyle
!= wxTOOL_STYLE_SEPARATOR
) && tool
->m_bitmap1
.Ok() && tool
->m_bitmap1
.GetHBITMAP())
202 // wxPalette *palette = tool->m_bitmap1->GetPalette();
204 HBITMAP oldBitmap2
= ::SelectObject(memoryDC2
, (HBITMAP
) tool
->m_bitmap1
.GetHBITMAP());
205 /* int bltResult = */
206 BitBlt(memoryDC
, x
, 0, (int) m_defaultWidth
, (int) m_defaultHeight
, memoryDC2
,
208 ::SelectObject(memoryDC2
, oldBitmap2
);
209 x
+= (int)m_defaultWidth
;
214 ::SelectObject(memoryDC
, oldBitmap
);
215 ::DeleteDC(memoryDC
);
216 ::DeleteDC(memoryDC2
);
218 // Map to system colours
219 wxMapBitmap((HBITMAP
) m_hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
221 if ( oldToolBarBitmap
)
223 TBREPLACEBITMAP replaceBitmap
;
224 replaceBitmap
.hInstOld
= NULL
;
225 replaceBitmap
.hInstNew
= NULL
;
226 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
227 replaceBitmap
.nIDNew
= (UINT
) (HBITMAP
) m_hBitmap
;
228 replaceBitmap
.nButtons
= noButtons
;
229 if (::SendMessage((HWND
) GetHWND(), TB_REPLACEBITMAP
, (WPARAM
) 0, (LPARAM
) &replaceBitmap
) == -1)
230 wxMessageBox("Could not add bitmap to toolbar");
232 ::DeleteObject((HBITMAP
) oldToolBarBitmap
);
234 // Now delete all the buttons
238 // TODO: What about separators???? They don't have an id!
239 if ( ! ::SendMessage( (HWND
) GetHWND(), TB_DELETEBUTTON
, i
, 0 ) )
245 TBADDBITMAP addBitmap
;
247 addBitmap
.nID
= (UINT
)m_hBitmap
;
248 if (::SendMessage((HWND
) GetHWND(), TB_ADDBITMAP
, (WPARAM
) noButtons
, (LPARAM
) &addBitmap
) == -1)
249 wxMessageBox("Could not add bitmap to toolbar");
252 // Now add the buttons.
253 TBBUTTON buttons
[50];
255 node
= m_tools
.First();
260 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
261 if (tool
->m_toolStyle
== wxTOOL_STYLE_SEPARATOR
)
263 buttons
[i
].iBitmap
= 0;
264 buttons
[i
].idCommand
= 0;
266 buttons
[i
].fsState
= TBSTATE_ENABLED
;
267 buttons
[i
].fsStyle
= TBSTYLE_SEP
;
268 buttons
[i
].dwData
= 0L;
269 buttons
[i
].iString
= 0;
273 buttons
[i
].iBitmap
= bitmapId
;
274 buttons
[i
].idCommand
= tool
->m_index
;
276 buttons
[i
].fsState
= 0;
278 buttons
[i
].fsState
|= TBSTATE_ENABLED
;
279 if (tool
->m_toggleState
)
280 buttons
[i
].fsState
|= TBSTATE_CHECKED
;
281 buttons
[i
].fsStyle
= tool
->m_isToggle
? TBSTYLE_CHECK
: TBSTYLE_BUTTON
;
282 buttons
[i
].dwData
= 0L;
283 buttons
[i
].iString
= 0;
292 int ans
= (int)::SendMessage((HWND
) GetHWND(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)& buttons
);
293 ans
= (int)::SendMessage((HWND
) GetHWND(), TB_AUTOSIZE
, (WPARAM
)0, (LPARAM
) 0);
296 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(m_rowsOrColumns
, TRUE
), (LPARAM
) & rect
);
297 m_maxWidth
= (rect
.right
- rect
.left
+ 2);
298 m_maxHeight
= (rect
.bottom
- rect
.top
+ 2);
303 bool wxToolBar95::MSWCommand(WXUINT cmd
, WXWORD id
)
305 wxNode
*node
= m_tools
.Find((long)id
);
308 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
309 if (tool
->m_isToggle
)
310 tool
->m_toggleState
= (1 == (1 & (int)::SendMessage((HWND
) GetHWND(), TB_GETSTATE
, (WPARAM
) id
, (LPARAM
) 0)));
312 BOOL ret
= OnLeftClick((int)id
, tool
->m_toggleState
);
313 if (ret
== FALSE
&& tool
->m_isToggle
)
315 tool
->m_toggleState
= !tool
->m_toggleState
;
316 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)id
, (LPARAM
)MAKELONG(tool
->m_toggleState
, 0));
321 bool wxToolBar95::MSWNotify(WXWPARAM
WXUNUSED(wParam
), WXLPARAM lParam
)
323 // First check if this applies to us
324 NMHDR
*hdr
= (NMHDR
*)lParam
;
325 if (hdr
->code
!= TTN_NEEDTEXT
)
328 HWND toolTipWnd
= (HWND
) ::SendMessage((HWND
) GetHWND(), TB_GETTOOLTIPS
, 0, 0);
329 if ( toolTipWnd
!= hdr
->hwndFrom
)
332 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
) lParam
;
333 int id
= (int)ttText
->hdr
.idFrom
;
334 wxNode
*node
= m_tools
.Find((long)id
);
338 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
340 switch (ttText
->hdr
.code
)
344 if (tool
->m_shortHelpString
!= "")
345 ttText
->lpszText
= (char *) (const char *)tool
->m_shortHelpString
;
347 // For backward compatibility...
348 OnMouseEnter(tool
->m_index
);
359 void wxToolBar95::SetDefaultSize(const wxSize
& size
)
361 m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
;
362 ::SendMessage((HWND
) GetHWND(), TB_SETBITMAPSIZE
, 0, (LPARAM
) MAKELONG ((int)size
.x
, (int)size
.y
));
365 void wxToolBar95::SetRows(int nRows
)
368 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(nRows
, TRUE
), (LPARAM
) & rect
);
369 m_maxWidth
= (rect
.right
- rect
.left
+ 2);
370 m_maxHeight
= (rect
.bottom
- rect
.top
+ 2);
373 wxSize
wxToolBar95::GetMaxSize(void) const
375 if (m_maxWidth
== -1 | m_maxHeight
== -1)
378 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(m_rowsOrColumns
, TRUE
), (LPARAM
) & rect
);
379 ((wxToolBar95
*)this)->m_maxWidth
= (rect
.right
- rect
.left
+ 2); // ???
380 ((wxToolBar95
*)this)->m_maxHeight
= (rect
.bottom
- rect
.top
+ 2); // ???
382 return wxSize(m_maxWidth
, m_maxHeight
);
385 void wxToolBar95::GetSize(int *w
, int *h
) const
387 wxWindow::GetSize(w
, h
);
388 // For some reason, the returned height is several pixels bigger than that
393 // The button size is bigger than the bitmap size
394 wxSize
wxToolBar95::GetDefaultButtonSize(void) const
396 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
399 void wxToolBar95::EnableTool(int toolIndex
, bool enable
)
401 wxNode
*node
= m_tools
.Find((long)toolIndex
);
404 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
405 tool
->m_enabled
= enable
;
406 ::SendMessage((HWND
) GetHWND(), TB_ENABLEBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(enable
, 0));
410 void wxToolBar95::ToggleTool(int toolIndex
, bool toggle
)
412 wxNode
*node
= m_tools
.Find((long)toolIndex
);
415 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
416 if (tool
->m_isToggle
)
418 tool
->m_toggleState
= toggle
;
419 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(toggle
, 0));
424 void wxToolBar95::ClearTools(void)
426 // TODO: Don't know how to reset the toolbar bitmap, as yet.
427 // But adding tools and calling CreateTools should probably
428 // recreate a buttonbar OK.
429 wxToolBarBase::ClearTools();
432 // If pushedBitmap is NULL, a reversed version of bitmap is
433 // created and used as the pushed/toggled image.
434 // If toggle is TRUE, the button toggles between the two states.
435 wxToolBarTool
*wxToolBar95::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
436 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
, const wxString
& helpString1
, const wxString
& helpString2
)
438 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, (wxBitmap
*)NULL
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
439 tool
->m_clientData
= clientData
;
444 tool
->m_x
= m_xMargin
;
449 tool
->m_y
= m_yMargin
;
451 tool
->SetSize(GetDefaultButtonWidth(), GetDefaultButtonHeight());
453 m_tools
.Append((long)index
, tool
);
457 // Responds to colour changes, and passes event on to children.
458 void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent
& event
)
460 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
461 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
462 m_defaultBackgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
463 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
472 // Propagate the event to the non-top-level children
473 wxWindow::OnSysColourChanged(event
);
476 // These are the default colors used to map the bitmap colors
477 // to the current system colors
479 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
480 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
481 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
482 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
483 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
484 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
486 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
488 COLORMAP ColorMap
[] = {
489 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
490 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
491 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
492 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
493 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
494 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
497 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
499 for ( n
= 0; n
< NUM_MAPS
; n
++)
501 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
505 HDC hdcMem
= CreateCompatibleDC(NULL
);
509 hbmOld
= SelectObject(hdcMem
, hBitmap
);
512 for ( i
= 0; i
< width
; i
++)
514 for ( j
= 0; j
< height
; j
++)
516 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
518 BYTE red = GetRValue(pixel);
519 BYTE green = GetGValue(pixel);
520 BYTE blue = GetBValue(pixel);
523 for ( k
= 0; k
< NUM_MAPS
; k
++)
525 if ( ColorMap
[k
].from
== pixel
)
527 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
535 SelectObject(hdcMem
, hbmOld
);
536 DeleteObject(hdcMem
);
541 // Some experiments...
543 // What we want to do is create another bitmap which has a depth of 4,
544 // and set the bits. So probably we want to convert this HBITMAP into a
545 // DIB, then call SetDIBits.
546 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
547 // the screen), then SetDIBits fails.
548 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
549 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
550 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
553 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
555 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
557 DWORD err
= GetLastError();
559 ::ReleaseDC(NULL
, dc
);
562 GlobalUnlock (newDIB
);
565 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
566 // Substitute our new bitmap for the old one
567 ::DeleteObject((HBITMAP
) m_hBitmap
);
568 m_hBitmap
= (WXHBITMAP
) newBitmap
;