]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_BUTTONBAR && wxUSE_TOOLBAR
25 #include "wx/toolbar.h"
27 #include "wx/os2/private.h"
31 #define TBSTYLE_LIST 0x1000
32 #define TBSTYLE_FLAT 0x0800
33 #define TBSTYLE_TRANSPARENT 0x8000
35 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
39 #define TB_GETSTYLE (WM_USER + 57)
40 #define TB_SETSTYLE (WM_USER + 56)
43 /* Hint from a newsgroup for custom flatbar drawing:
44 Set the TBSTYLE_CUSTOMERASE style, then handle the
45 NM_CUSTOMDRAW message and do your custom drawing.
48 #define DEFAULTBITMAPX 16
49 #define DEFAULTBITMAPY 15
50 #define DEFAULTBUTTONX 24
51 #define DEFAULTBUTTONY 24
52 #define DEFAULTBARHEIGHT 27
54 #if !USE_SHARED_LIBRARY
55 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
58 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
59 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent
)
60 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged
)
63 static void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
);
65 wxToolBar::wxToolBar()
70 m_defaultWidth
= DEFAULTBITMAPX
;
71 m_defaultHeight
= DEFAULTBITMAPY
;
74 bool wxToolBar::Create(wxWindow
*parent
,
82 m_backgroundColour
= *wxWHITE
; //TODO: wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
83 // GetGValue(GetSysColor(COLOR_BTNFACE)),
84 // GetBValue(GetSysColor(COLOR_BTNFACE)));
85 m_foregroundColour
= *wxBLACK
;
87 wxASSERT_MSG( (style
& wxTB_VERTICAL
) == 0,
88 wxT("Sorry, wxToolBar under Windows 95 only "
89 "supports horizontal orientation.") );
96 m_defaultWidth
= DEFAULTBITMAPX
;
97 m_defaultHeight
= DEFAULTBITMAPY
;
100 m_windowStyle
= style
;
102 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
119 m_windowId
= (id
< 0 ? NewControlId() : id
);
123 if (style & wxBORDER)
124 msflags |= WS_BORDER;
125 msflags |= WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS;
127 if (style & wxTB_FLAT)
129 if (wxTheApp->GetComCtl32Version() > 400)
130 msflags |= TBSTYLE_FLAT;
134 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
136 // Even with extended styles, need to combine with WS_BORDER
137 // for them to look right.
138 if ( want3D || wxStyleHasBorder(m_windowStyle) )
139 msflags |= WS_BORDER;
141 // Create the toolbar control.
142 HWND hWndToolbar = CreateWindowEx
144 exStyle, // Extended styles.
145 TOOLBARCLASSNAME, // Class name for the toolbar.
146 wxT(""), // No default text.
148 x, y, width, height, // Standard toolbar size and position.
149 (HWND) parent->GetHWND(), // Parent window of the toolbar.
150 (HMENU)m_windowId, // Toolbar ID.
151 wxGetInstance(), // Current instance.
152 NULL // No class data.
155 wxCHECK_MSG( hWndToolbar, FALSE, wxT("Toolbar creation failed") );
157 // Toolbar-specific initialisation
158 ::SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE,
159 (WPARAM)sizeof(TBBUTTON), (LPARAM)0);
160 m_hWnd = (WXHWND) hWndToolbar;
163 parent->AddChild(this);
165 SubclassWin((WXHWND)hWndToolbar);
172 wxToolBar::~wxToolBar()
178 // ::DeleteObject((HBITMAP) m_hBitmap);
183 bool wxToolBar::CreateTools()
185 if (m_tools
.Number() == 0)
188 HBITMAP oldToolBarBitmap
= (HBITMAP
) m_hBitmap
;
190 int totalBitmapWidth
= (int)(m_defaultWidth
* m_tools
.Number());
191 int totalBitmapHeight
= (int)m_defaultHeight
;
195 // Create a bitmap for all the tool bitmaps
196 HDC dc = ::GetDC(NULL);
197 m_hBitmap = (WXHBITMAP) ::CreateCompatibleBitmap(dc, totalBitmapWidth, totalBitmapHeight);
198 ::ReleaseDC(NULL, dc);
200 // Now blit all the tools onto this bitmap
201 HDC memoryDC = ::CreateCompatibleDC(NULL);
202 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(memoryDC, (HBITMAP) m_hBitmap);
204 HDC memoryDC2 = ::CreateCompatibleDC(NULL);
206 wxNode *node = m_tools.First();
210 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
211 if ((tool->m_toolStyle != wxTOOL_STYLE_SEPARATOR) && tool->m_bitmap1.Ok() && tool->m_bitmap1.GetHBITMAP())
213 // wxPalette *palette = tool->m_bitmap1->GetPalette();
215 HBITMAP oldBitmap2 = (HBITMAP) ::SelectObject(memoryDC2, (HBITMAP) tool->m_bitmap1.GetHBITMAP());
217 BitBlt(memoryDC, x, 0, (int) m_defaultWidth, (int) m_defaultHeight, memoryDC2,
219 ::SelectObject(memoryDC2, oldBitmap2);
220 x += (int)m_defaultWidth;
225 ::SelectObject(memoryDC, oldBitmap);
226 ::DeleteDC(memoryDC);
227 ::DeleteDC(memoryDC2);
229 // Map to system colours
230 wxMapBitmap((HBITMAP) m_hBitmap, totalBitmapWidth, totalBitmapHeight);
232 if ( oldToolBarBitmap )
234 TBREPLACEBITMAP replaceBitmap;
235 replaceBitmap.hInstOld = NULL;
236 replaceBitmap.hInstNew = NULL;
237 replaceBitmap.nIDOld = (UINT) oldToolBarBitmap;
238 replaceBitmap.nIDNew = (UINT) (HBITMAP) m_hBitmap;
239 replaceBitmap.nButtons = noButtons;
240 if (::SendMessage((HWND) GetHWND(), TB_REPLACEBITMAP, (WPARAM) 0, (LPARAM) &replaceBitmap) == -1)
242 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
245 ::DeleteObject((HBITMAP) oldToolBarBitmap);
247 // Now delete all the buttons
251 // TODO: What about separators???? They don't have an id!
252 if ( ! ::SendMessage( (HWND) GetHWND(), TB_DELETEBUTTON, i, 0 ) )
258 TBADDBITMAP addBitmap;
260 addBitmap.nID = (UINT)m_hBitmap;
261 if (::SendMessage((HWND) GetHWND(), TB_ADDBITMAP, (WPARAM) noButtons, (LPARAM) &addBitmap) == -1)
263 wxFAIL_MSG(wxT("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, wxT("failed to add buttons to the toolbar") );
311 (void)::SendMessage((HWND) GetHWND(), TB_AUTOSIZE, (WPARAM)0, (LPARAM) 0);
318 bool wxToolBar::OS2Command(WXUINT cmd
, WXWORD id
)
320 wxNode
*node
= m_tools
.Find((long)id
);
323 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
326 if (tool->m_isToggle)
327 tool->m_toggleState = (1 == (1 & (int)::SendMessage((HWND) GetHWND(), TB_GETSTATE, (WPARAM) id, (LPARAM) 0)));
329 BOOL ret = OnLeftClick((int)id, tool->m_toggleState);
330 if (ret == FALSE && tool->m_isToggle)
332 tool->m_toggleState = !tool->m_toggleState;
333 ::SendMessage((HWND) GetHWND(), TB_CHECKBUTTON, (WPARAM)id, (LPARAM)MAKELONG(tool->m_toggleState, 0));
340 bool wxToolBar::OS2OnNotify(int WXUNUSED(idCtrl
),
346 // First check if this applies to us
347 NMHDR *hdr = (NMHDR *)lParam;
349 // the tooltips control created by the toolbar is sometimes Unicode, even in
350 // an ANSI application
351 int code = (int)hdr->code;
352 if ( (code != TTN_NEEDTEXTA) && (code != TTN_NEEDTEXTW) )
355 HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0);
356 if ( toolTipWnd != hdr->hwndFrom )
359 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
360 int id = (int)ttText->hdr.idFrom;
361 wxNode *node = m_tools.Find((long)id);
365 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
367 const wxString& help = tool->m_shortHelpString;
369 if ( !help.IsEmpty() )
371 if ( code == TTN_NEEDTEXTA )
373 ttText->lpszText = (wxChar *)help.c_str();
375 #if (_WIN32_IE >= 0x0300)
378 // FIXME this is a temp hack only until I understand better what
379 // must be done in both ANSI and Unicode builds
381 size_t lenAnsi = help.Len();
383 // MetroWerks doesn't like calling mbstowcs with NULL argument
384 size_t lenUnicode = 2*lenAnsi;
386 size_t lenUnicode = mbstowcs(NULL, help, lenAnsi);
389 // using the pointer of right type avoids us doing all sorts of
390 // pointer arithmetics ourselves
391 wchar_t *dst = (wchar_t *)ttText->szText,
392 *pwz = new wchar_t[lenUnicode + 1];
393 mbstowcs(pwz, help, lenAnsi + 1);
394 memcpy(dst, pwz, lenUnicode*sizeof(wchar_t));
396 // put the terminating _wide_ NUL
401 #endif // _WIN32_IE >= 0x0300
404 // For backward compatibility...
405 OnMouseEnter(tool->m_index);
412 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
414 m_defaultWidth
= size
.x
;
415 m_defaultHeight
= size
.y
;
416 // ::SendMessage((HWND) GetHWND(), TB_SETBITMAPSIZE, 0, (LPARAM) MAKELONG ((int)size.x, (int)size.y));
419 void wxToolBar::SetRows(int nRows
)
424 ::SendMessage((HWND) GetHWND(), TB_SETROWS, MAKEWPARAM(nRows, TRUE), (LPARAM) & rect);
425 m_maxWidth = (rect.right - rect.left + 2);
426 m_maxHeight = (rect.bottom - rect.top + 2);
430 wxSize
wxToolBar::GetMaxSize() const
434 if ((m_maxWidth == -1) || (m_maxHeight == -1))
437 ::SendMessage((HWND) GetHWND(), TB_SETROWS, MAKEWPARAM(m_maxRows, TRUE), (LPARAM) & rect);
438 ((wxToolBar *)this)->m_maxWidth = (rect.right - rect.left + 2); // ???
439 ((wxToolBar *)this)->m_maxHeight = (rect.bottom - rect.top + 2); // ???
442 return wxSize(m_maxWidth
, m_maxHeight
);
445 // The button size is bigger than the bitmap size
446 wxSize
wxToolBar::GetToolSize() const
448 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
451 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
453 wxNode
*node
= m_tools
.Find((long)toolIndex
);
456 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
457 tool
->m_enabled
= enable
;
458 // ::SendMessage((HWND) GetHWND(), TB_ENABLEBUTTON, (WPARAM)toolIndex, (LPARAM)MAKELONG(enable, 0));
462 void wxToolBar::ToggleTool(int toolIndex
, bool toggle
)
464 wxNode
*node
= m_tools
.Find((long)toolIndex
);
467 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
468 if (tool
->m_isToggle
)
470 tool
->m_toggleState
= toggle
;
471 // ::SendMessage((HWND) GetHWND(), TB_CHECKBUTTON, (WPARAM)toolIndex, (LPARAM)MAKELONG(toggle, 0));
476 bool wxToolBar::GetToolState(int toolIndex
) const
478 // return (::SendMessage((HWND) GetHWND(), TB_ISBUTTONCHECKED, (WPARAM)toolIndex, (LPARAM)0) != 0);
482 void wxToolBar::ClearTools()
484 // TODO: Don't know how to reset the toolbar bitmap, as yet.
485 // But adding tools and calling CreateTools should probably
486 // recreate a buttonbar OK.
487 wxToolBarBase::ClearTools();
490 // If pushedBitmap is NULL, a reversed version of bitmap is
491 // created and used as the pushed/toggled image.
492 // If toggle is TRUE, the button toggles between the two states.
493 wxToolBarTool
*wxToolBar::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
494 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
, const wxString
& helpString1
, const wxString
& helpString2
)
496 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, wxNullBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
497 tool
->m_clientData
= clientData
;
502 tool
->m_x
= m_xMargin
;
507 tool
->m_y
= m_yMargin
;
509 tool
->SetSize(GetToolSize().x
, GetToolSize().y
);
511 m_tools
.Append((long)index
, tool
);
515 // Responds to colour changes, and passes event on to children.
516 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent
& event
)
520 m_backgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
521 GetGValue(GetSysColor(COLOR_BTNFACE)), GetBValue(GetSysColor(COLOR_BTNFACE)));
528 // Propagate the event to the non-top-level children
529 wxWindow::OnSysColourChanged(event
);
532 void wxToolBar::OnMouseEvent(wxMouseEvent
& event
)
534 if (event
.RightDown())
536 // For now, we don't have an id. Later we could
537 // try finding the tool.
538 OnRightClick((int)-1, event
.GetX(), event
.GetY());
546 // These are the default colors used to map the bitmap colors
547 // to the current system colors
549 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
550 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
551 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
552 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
553 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
554 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
556 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
560 COLORMAP ColorMap[] = {
561 {BGR_BUTTONTEXT, COLOR_BTNTEXT}, // black
562 {BGR_BUTTONSHADOW, COLOR_BTNSHADOW}, // dark grey
563 {BGR_BUTTONFACE, COLOR_BTNFACE}, // bright grey
564 {BGR_BUTTONHILIGHT, COLOR_BTNHIGHLIGHT},// white
565 {BGR_BACKGROUNDSEL, COLOR_HIGHLIGHT}, // blue
566 {BGR_BACKGROUND, COLOR_WINDOW} // magenta
569 int NUM_MAPS = (sizeof(ColorMap)/sizeof(COLORMAP));
571 for ( n = 0; n < NUM_MAPS; n++)
573 ColorMap[n].to = ::GetSysColor(ColorMap[n].to);
577 HDC hdcMem = CreateCompatibleDC(NULL);
581 hbmOld = (HBITMAP) SelectObject(hdcMem, hBitmap);
584 for ( i = 0; i < width; i++)
586 for ( j = 0; j < height; j++)
588 COLORREF pixel = ::GetPixel(hdcMem, i, j);
589 BYTE red = GetRValue(pixel);
590 BYTE green = GetGValue(pixel);
591 BYTE blue = GetBValue(pixel);
593 for ( k = 0; k < NUM_MAPS; k ++)
595 if ( ColorMap[k].from == pixel )
597 COLORREF actualPixel = ::SetPixel(hdcMem, i, j, ColorMap[k].to);
604 SelectObject(hdcMem, hbmOld);
605 DeleteObject(hdcMem);
610 // Some experiments...
612 // What we want to do is create another bitmap which has a depth of 4,
613 // and set the bits. So probably we want to convert this HBITMAP into a
614 // DIB, then call SetDIBits.
615 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
616 // the screen), then SetDIBits fails.
617 HBITMAP newBitmap
= ::CreateBitmap(totalBitmapWidth
, totalBitmapHeight
, 1, 4, NULL
);
618 HANDLE newDIB
= ::BitmapToDIB((HBITMAP
) m_hBitmap
, NULL
);
619 LPBITMAPINFOHEADER lpbmi
= (LPBITMAPINFOHEADER
) GlobalLock(newDIB
);
622 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
624 int result
= ::SetDIBits(dc
, newBitmap
, 0, lpbmi
->biHeight
, FindDIBBits((LPSTR
)lpbmi
), (LPBITMAPINFO
)lpbmi
,
626 DWORD err
= GetLastError();
628 ::ReleaseDC(NULL
, dc
);
631 GlobalUnlock (newDIB
);
634 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
635 // Substitute our new bitmap for the old one
636 ::DeleteObject((HBITMAP
) m_hBitmap
);
637 m_hBitmap
= (WXHBITMAP
) newBitmap
;