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"
48 #if !USE_SHARED_LIBRARY
49 IMPLEMENT_DYNAMIC_CLASS(wxToolBar95
, wxToolBarBase
)
51 BEGIN_EVENT_TABLE(wxToolBar95
, wxToolBarBase
)
52 EVT_SIZE(wxToolBar95::OnSize
)
53 EVT_PAINT(wxToolBar95::OnPaint
)
54 EVT_KILL_FOCUS(wxToolBar95::OnKillFocus
)
55 EVT_MOUSE_EVENTS(wxToolBar95::OnMouseEvent
)
56 EVT_SYS_COLOUR_CHANGED(wxToolBar95::OnSysColourChanged
)
60 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
);
62 wxToolBar95::wxToolBar95(void)
64 m_tilingDirection
= wxVERTICAL
;
66 m_currentRowsOrColumns
= 0;
70 m_defaultWidth
= DEFAULTBITMAPX
;
71 m_defaultHeight
= DEFAULTBITMAPY
;
74 bool wxToolBar95::Create(wxWindow
*parent
, const wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
75 const long style
, const int orientation
,
76 const int RowsOrColumns
, const wxString
& name
)
78 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
79 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
80 m_foregroundColour
= *wxBLACK
;
82 m_defaultForegroundColour
= *wxBLACK
;
83 m_defaultBackgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
84 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
86 m_tilingDirection
= orientation
;
87 if (m_tilingDirection
== wxHORIZONTAL
)
88 wxMessageBox("Sorry, wxToolBar95 under Windows 95 only supports vertical tiling.\nPass the number of rows.", "wxToolBar95 usage", wxOK
);
89 m_rowsOrColumns
= RowsOrColumns
;
90 m_currentRowsOrColumns
= 0;
96 m_defaultWidth
= DEFAULTBITMAPX
;
97 m_defaultHeight
= DEFAULTBITMAPY
;
105 m_windowStyle
= style
;
107 SetFont(wxTheFontList
->FindOrCreateFont(11, wxSWISS
, wxNORMAL
, wxNORMAL
));
112 if (style
& wxBORDER
)
113 msflags
|= WS_BORDER
;
114 msflags
|= WS_CHILD
| WS_VISIBLE
;
125 m_windowId
= (id
< 0 ? NewControlId() : id
);
127 // Create the toolbar control.
128 HWND hWndToolbar
= CreateWindowEx(0L, // No extended styles.
129 TOOLBARCLASSNAME
, // Class name for the toolbar.
130 "", // No default text.
131 WS_CHILD
| WS_BORDER
| WS_VISIBLE
| TBSTYLE_TOOLTIPS
, // Styles and defaults.
132 x
, y
, width
, height
, // Standard toolbar size and position.
133 (HWND
) parent
->GetHWND(), // Parent window of the toolbar.
134 (HMENU
)m_windowId
, // Toolbar ID.
135 wxGetInstance(), // Current instance.
136 NULL
); // No class data.
138 // Toolbar-specific initialisation
139 ::SendMessage(hWndToolbar
, TB_BUTTONSTRUCTSIZE
, (WPARAM
)sizeof(TBBUTTON
), (LPARAM
)0);
141 m_hWnd
= (WXHWND
) hWndToolbar
;
142 if (parent
) parent
->AddChild(this);
144 SubclassWin((WXHWND
) hWndToolbar
);
149 wxToolBar95::~wxToolBar95(void)
155 ::DeleteObject((HBITMAP
) m_hBitmap
);
160 bool wxToolBar95::CreateTools(void)
162 if (m_tools
.Number() == 0)
165 HBITMAP oldToolBarBitmap
= (HBITMAP
) m_hBitmap
;
167 int totalBitmapWidth
= (int)(m_defaultWidth
* m_tools
.Number());
168 int totalBitmapHeight
= (int)m_defaultHeight
;
170 // Create a bitmap for all the tool bitmaps
171 HDC dc
= ::GetDC(NULL
);
172 m_hBitmap
= (WXHBITMAP
) ::CreateCompatibleBitmap(dc
, totalBitmapWidth
, totalBitmapHeight
);
173 ::ReleaseDC(NULL
, dc
);
175 // Now blit all the tools onto this bitmap
176 HDC memoryDC
= ::CreateCompatibleDC(NULL
);
177 HBITMAP oldBitmap
= ::SelectObject(memoryDC
, (HBITMAP
) m_hBitmap
);
179 HDC memoryDC2
= ::CreateCompatibleDC(NULL
);
181 wxNode
*node
= m_tools
.First();
185 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
186 if ((tool
->m_toolStyle
!= wxTOOL_STYLE_SEPARATOR
) && tool
->m_bitmap1
.Ok() && tool
->m_bitmap1
.GetHBITMAP())
188 // wxPalette *palette = tool->m_bitmap1->GetPalette();
190 HBITMAP oldBitmap2
= ::SelectObject(memoryDC2
, (HBITMAP
) tool
->m_bitmap1
.GetHBITMAP());
191 /* int bltResult = */
192 BitBlt(memoryDC
, x
, 0, (int) m_defaultWidth
, (int) m_defaultHeight
, memoryDC2
,
194 ::SelectObject(memoryDC2
, oldBitmap2
);
195 x
+= (int)m_defaultWidth
;
200 ::SelectObject(memoryDC
, oldBitmap
);
201 ::DeleteDC(memoryDC
);
202 ::DeleteDC(memoryDC2
);
204 // Map to system colours
205 wxMapBitmap((HBITMAP
) m_hBitmap
, totalBitmapWidth
, totalBitmapHeight
);
207 if ( oldToolBarBitmap
)
209 TBREPLACEBITMAP replaceBitmap
;
210 replaceBitmap
.hInstOld
= NULL
;
211 replaceBitmap
.hInstNew
= NULL
;
212 replaceBitmap
.nIDOld
= (UINT
) oldToolBarBitmap
;
213 replaceBitmap
.nIDNew
= (UINT
) (HBITMAP
) m_hBitmap
;
214 replaceBitmap
.nButtons
= noButtons
;
215 if (::SendMessage((HWND
) GetHWND(), TB_REPLACEBITMAP
, (WPARAM
) 0, (LPARAM
) &replaceBitmap
) == -1)
216 wxMessageBox("Could not add bitmap to toolbar");
218 ::DeleteObject((HBITMAP
) oldToolBarBitmap
);
220 // Now delete all the buttons
224 // TODO: What about separators???? They don't have an id!
225 if ( ! ::SendMessage( (HWND
) GetHWND(), TB_DELETEBUTTON
, i
, 0 ) )
231 TBADDBITMAP addBitmap
;
233 addBitmap
.nID
= (UINT
)m_hBitmap
;
234 if (::SendMessage((HWND
) GetHWND(), TB_ADDBITMAP
, (WPARAM
) noButtons
, (LPARAM
) &addBitmap
) == -1)
235 wxMessageBox("Could not add bitmap to toolbar");
238 // Now add the buttons.
239 TBBUTTON buttons
[50];
241 node
= m_tools
.First();
246 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
247 if (tool
->m_toolStyle
== wxTOOL_STYLE_SEPARATOR
)
249 buttons
[i
].iBitmap
= 0;
250 buttons
[i
].idCommand
= 0;
252 buttons
[i
].fsState
= TBSTATE_ENABLED
;
253 buttons
[i
].fsStyle
= TBSTYLE_SEP
;
254 buttons
[i
].dwData
= 0L;
255 buttons
[i
].iString
= 0;
259 buttons
[i
].iBitmap
= bitmapId
;
260 buttons
[i
].idCommand
= tool
->m_index
;
262 buttons
[i
].fsState
= 0;
264 buttons
[i
].fsState
|= TBSTATE_ENABLED
;
265 if (tool
->m_toggleState
)
266 buttons
[i
].fsState
|= TBSTATE_CHECKED
;
267 buttons
[i
].fsStyle
= tool
->m_isToggle
? TBSTYLE_CHECK
: TBSTYLE_BUTTON
;
268 buttons
[i
].dwData
= 0L;
269 buttons
[i
].iString
= 0;
278 int ans
= (int)::SendMessage((HWND
) GetHWND(), TB_ADDBUTTONS
, (WPARAM
)i
, (LPARAM
)& buttons
);
279 ans
= (int)::SendMessage((HWND
) GetHWND(), TB_AUTOSIZE
, (WPARAM
)0, (LPARAM
) 0);
282 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(m_rowsOrColumns
, TRUE
), (LPARAM
) & rect
);
283 m_maxWidth
= (rect
.right
- rect
.left
+ 2);
284 m_maxHeight
= (rect
.bottom
- rect
.top
+ 2);
289 bool wxToolBar95::MSWCommand(const WXUINT cmd
, const WXWORD id
)
291 wxNode
*node
= m_tools
.Find((long)id
);
294 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
295 if (tool
->m_isToggle
)
296 tool
->m_toggleState
= (1 == (1 & (int)::SendMessage((HWND
) GetHWND(), TB_GETSTATE
, (WPARAM
) id
, (LPARAM
) 0)));
298 BOOL ret
= OnLeftClick((int)id
, tool
->m_toggleState
);
299 if (ret
== FALSE
&& tool
->m_isToggle
)
301 tool
->m_toggleState
= !tool
->m_toggleState
;
302 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)id
, (LPARAM
)MAKELONG(tool
->m_toggleState
, 0));
307 bool wxToolBar95::MSWNotify(const WXWPARAM
WXUNUSED(wParam
), const WXLPARAM lParam
)
309 // First check if this applies to us
310 NMHDR
*hdr
= (NMHDR
*)lParam
;
311 if (hdr
->code
!= TTN_NEEDTEXT
)
314 HWND toolTipWnd
= (HWND
) ::SendMessage((HWND
) GetHWND(), TB_GETTOOLTIPS
, 0, 0);
315 if ( toolTipWnd
!= hdr
->hwndFrom
)
318 LPTOOLTIPTEXT ttText
= (LPTOOLTIPTEXT
) lParam
;
319 int id
= (int)ttText
->hdr
.idFrom
;
320 wxNode
*node
= m_tools
.Find((long)id
);
324 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
326 switch (ttText
->hdr
.code
)
330 if (tool
->m_shortHelpString
!= "")
331 ttText
->lpszText
= (char *) (const char *)tool
->m_shortHelpString
;
333 // For backward compatibility...
334 OnMouseEnter(tool
->m_index
);
345 void wxToolBar95::SetDefaultSize(const wxSize
& size
)
347 m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
;
348 ::SendMessage((HWND
) GetHWND(), TB_SETBITMAPSIZE
, 0, (LPARAM
) MAKELONG ((int)size
.x
, (int)size
.y
));
351 void wxToolBar95::SetRows(const int nRows
)
354 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(nRows
, TRUE
), (LPARAM
) & rect
);
355 m_maxWidth
= (rect
.right
- rect
.left
+ 2);
356 m_maxHeight
= (rect
.bottom
- rect
.top
+ 2);
359 wxSize
wxToolBar95::GetMaxSize(void) const
361 if (m_maxWidth
== -1 | m_maxHeight
== -1)
364 ::SendMessage((HWND
) GetHWND(), TB_SETROWS
, MAKEWPARAM(m_rowsOrColumns
, TRUE
), (LPARAM
) & rect
);
365 ((wxToolBar95
*)this)->m_maxWidth
= (rect
.right
- rect
.left
+ 2); // ???
366 ((wxToolBar95
*)this)->m_maxHeight
= (rect
.bottom
- rect
.top
+ 2); // ???
368 return wxSize(m_maxWidth
, m_maxHeight
);
371 void wxToolBar95::GetSize(int *w
, int *h
) const
373 wxWindow::GetSize(w
, h
);
374 // For some reason, the returned height is several pixels bigger than that
379 // The button size is bigger than the bitmap size
380 wxSize
wxToolBar95::GetDefaultButtonSize(void) const
382 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
385 void wxToolBar95::EnableTool(const int toolIndex
, const bool enable
)
387 wxNode
*node
= m_tools
.Find((long)toolIndex
);
390 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
391 tool
->m_enabled
= enable
;
392 ::SendMessage((HWND
) GetHWND(), TB_ENABLEBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(enable
, 0));
396 void wxToolBar95::ToggleTool(const int toolIndex
, const bool toggle
)
398 wxNode
*node
= m_tools
.Find((long)toolIndex
);
401 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
402 if (tool
->m_isToggle
)
404 tool
->m_toggleState
= toggle
;
405 ::SendMessage((HWND
) GetHWND(), TB_CHECKBUTTON
, (WPARAM
)toolIndex
, (LPARAM
)MAKELONG(toggle
, 0));
410 void wxToolBar95::ClearTools(void)
412 // TODO: Don't know how to reset the toolbar bitmap, as yet.
413 // But adding tools and calling CreateTools should probably
414 // recreate a buttonbar OK.
415 wxToolBarBase::ClearTools();
418 // If pushedBitmap is NULL, a reversed version of bitmap is
419 // created and used as the pushed/toggled image.
420 // If toggle is TRUE, the button toggles between the two states.
421 wxToolBarTool
*wxToolBar95::AddTool(const int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
422 const bool toggle
, const long xPos
, const long yPos
, wxObject
*clientData
, const wxString
& helpString1
, const wxString
& helpString2
)
424 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, (wxBitmap
*)NULL
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
425 tool
->m_clientData
= clientData
;
430 tool
->m_x
= m_xMargin
;
435 tool
->m_y
= m_yMargin
;
437 tool
->SetSize(GetDefaultButtonWidth(), GetDefaultButtonHeight());
439 m_tools
.Append((long)index
, tool
);
443 // Responds to colour changes, and passes event on to children.
444 void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent
& event
)
446 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
447 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
448 m_defaultBackgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
449 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
458 // Propagate the event to the non-top-level children
459 wxWindow::OnSysColourChanged(event
);
462 // These are the default colors used to map the bitmap colors
463 // to the current system colors
465 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
466 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
467 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
468 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
469 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
470 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
472 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
474 COLORMAP ColorMap
[] = {
475 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
476 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
477 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
478 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
479 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
480 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
483 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
485 for ( n
= 0; n
< NUM_MAPS
; n
++)
487 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
491 HDC hdcMem
= CreateCompatibleDC(NULL
);
495 hbmOld
= SelectObject(hdcMem
, hBitmap
);
498 for ( i
= 0; i
< width
; i
++)
500 for ( j
= 0; j
< height
; j
++)
502 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
504 BYTE red = GetRValue(pixel);
505 BYTE green = GetGValue(pixel);
506 BYTE blue = GetBValue(pixel);
509 for ( k
= 0; k
< NUM_MAPS
; k
++)
511 if ( ColorMap
[k
].from
== pixel
)
513 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
521 SelectObject(hdcMem
, hbmOld
);
522 DeleteObject(hdcMem
);
527 // Some experiments...
529 // What we want to do is create another bitmap which has a depth of 4,
530 // and set the bits. So probably we want to convert this HBITMAP into a
531 // DIB, then call SetDIBits.
532 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
533 // the screen), then SetDIBits fails.
534 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
535 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
536 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
539 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
541 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
543 DWORD err
= GetLastError();
545 ::ReleaseDC(NULL
, dc
);
548 GlobalUnlock (newDIB
);
551 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
552 // Substitute our new bitmap for the old one
553 ::DeleteObject((HBITMAP
) m_hBitmap
);
554 m_hBitmap
= (WXHBITMAP
) newBitmap
;