]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/os2/toolbar.cpp
checks for time functions added
[wxWidgets.git] / src / os2 / toolbar.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: toolbar.cpp
3// Purpose: wxToolBar
4// Author: David Webster
5// Modified by:
6// Created: 10/17/99
7// RCS-ID: $Id$
8// Copyright: (c) David Webster
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifndef WX_PRECOMP
16#include "wx/wx.h"
17#endif
18
19#if wxUSE_BUTTONBAR && wxUSE_TOOLBAR && defined(__WIN95__)
20
21#include "malloc.h"
22#define INCL_PM
23#include <os2.h>
24
25#include "wx/toolbar.h"
26#include "wx/app.h"
27#include "wx/os2/private.h"
28
29// Styles
30#ifndef TBSTYLE_FLAT
31#define TBSTYLE_LIST 0x1000
32#define TBSTYLE_FLAT 0x0800
33#define TBSTYLE_TRANSPARENT 0x8000
34#endif
35 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
36
37// Messages
38#ifndef TB_GETSTYLE
39#define TB_GETSTYLE (WM_USER + 57)
40#define TB_SETSTYLE (WM_USER + 56)
41#endif
42
43/* Hint from a newsgroup for custom flatbar drawing:
44Set the TBSTYLE_CUSTOMERASE style, then handle the
45NM_CUSTOMDRAW message and do your custom drawing.
46*/
47
48#define DEFAULTBITMAPX 16
49#define DEFAULTBITMAPY 15
50#define DEFAULTBUTTONX 24
51#define DEFAULTBUTTONY 24
52#define DEFAULTBARHEIGHT 27
53
54#if !USE_SHARED_LIBRARY
55IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
56#endif
57
58BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
59 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent)
60 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged)
61END_EVENT_TABLE()
62
63static void wxMapBitmap(HBITMAP hBitmap, int width, int height);
64
65wxToolBar::wxToolBar()
66{
67 m_maxWidth = -1;
68 m_maxHeight = -1;
69 m_hBitmap = 0;
70 m_defaultWidth = DEFAULTBITMAPX;
71 m_defaultHeight = DEFAULTBITMAPY;
72}
73
74bool wxToolBar::Create(wxWindow *parent,
75 wxWindowID id,
76 const wxPoint& pos,
77 const wxSize& size,
78 long style,
79 const wxString& name)
80{
81 m_hWnd = 0;
82 m_backgroundColour = *wxWHITE; //TODO: wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
83// GetGValue(GetSysColor(COLOR_BTNFACE)),
84// GetBValue(GetSysColor(COLOR_BTNFACE)));
85 m_foregroundColour = *wxBLACK ;
86
87 wxASSERT_MSG( (style & wxTB_VERTICAL) == 0,
88 wxT("Sorry, wxToolBar under Windows 95 only "
89 "supports horizontal orientation.") );
90
91 m_maxWidth = -1;
92 m_maxHeight = -1;
93
94 m_hBitmap = 0;
95
96 m_defaultWidth = DEFAULTBITMAPX;
97 m_defaultHeight = DEFAULTBITMAPY;
98 SetName(name);
99
100 m_windowStyle = style;
101
102 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
103 SetParent(parent);
104
105 int x = pos.x;
106 int y = pos.y;
107 int width = size.x;
108 int height = size.y;
109
110 if (width <= 0)
111 width = 100;
112 if (height <= 0)
113 height = 30;
114 if (x < 0)
115 x = 0;
116 if (y < 0)
117 y = 0;
118
119 m_windowId = (id < 0 ? NewControlId() : id);
120 DWORD msflags = 0;
121// TODO:
122/*
123 if (style & wxBORDER)
124 msflags |= WS_BORDER;
125 msflags |= WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS;
126
127 if (style & wxTB_FLAT)
128 {
129 if (wxTheApp->GetComCtl32Version() > 400)
130 msflags |= TBSTYLE_FLAT;
131 }
132
133 bool want3D;
134 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
135
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;
140
141 // Create the toolbar control.
142 HWND hWndToolbar = CreateWindowEx
143 (
144 exStyle, // Extended styles.
145 TOOLBARCLASSNAME, // Class name for the toolbar.
146 wxT(""), // No default text.
147 msflags, // Styles
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.
153 );
154
155 wxCHECK_MSG( hWndToolbar, FALSE, wxT("Toolbar creation failed") );
156
157 // Toolbar-specific initialisation
158 ::SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE,
159 (WPARAM)sizeof(TBBUTTON), (LPARAM)0);
160*/
161 m_hWnd = (WXHWND) hWndToolbar;
162 if (parent)
163 parent->AddChild(this);
164
165 SubclassWin((WXHWND)hWndToolbar);
166
167 return TRUE;
168}
169
170wxToolBar::~wxToolBar()
171{
172 UnsubclassWin();
173
174 if (m_hBitmap)
175 {
176// ::DeleteObject((HBITMAP) m_hBitmap);
177 m_hBitmap = 0;
178 }
179}
180
181bool wxToolBar::CreateTools()
182{
183 if (m_tools.Number() == 0)
184 return FALSE;
185
186 HBITMAP oldToolBarBitmap = (HBITMAP) m_hBitmap;
187
188 int totalBitmapWidth = (int)(m_defaultWidth * m_tools.Number());
189 int totalBitmapHeight = (int)m_defaultHeight;
190
191// TODO:
192/*
193 // Create a bitmap for all the tool bitmaps
194 HDC dc = ::GetDC(NULL);
195 m_hBitmap = (WXHBITMAP) ::CreateCompatibleBitmap(dc, totalBitmapWidth, totalBitmapHeight);
196 ::ReleaseDC(NULL, dc);
197
198 // Now blit all the tools onto this bitmap
199 HDC memoryDC = ::CreateCompatibleDC(NULL);
200 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(memoryDC, (HBITMAP) m_hBitmap);
201
202 HDC memoryDC2 = ::CreateCompatibleDC(NULL);
203 int x = 0;
204 wxNode *node = m_tools.First();
205 int noButtons = 0;
206 while (node)
207 {
208 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
209 if ((tool->m_toolStyle != wxTOOL_STYLE_SEPARATOR) && tool->m_bitmap1.Ok() && tool->m_bitmap1.GetHBITMAP())
210 {
211// wxPalette *palette = tool->m_bitmap1->GetPalette();
212
213 HBITMAP oldBitmap2 = (HBITMAP) ::SelectObject(memoryDC2, (HBITMAP) tool->m_bitmap1.GetHBITMAP());
214 // int bltResult =
215 BitBlt(memoryDC, x, 0, (int) m_defaultWidth, (int) m_defaultHeight, memoryDC2,
216 0, 0, SRCCOPY);
217 ::SelectObject(memoryDC2, oldBitmap2);
218 x += (int)m_defaultWidth;
219 noButtons ++;
220 }
221 node = node->Next();
222 }
223 ::SelectObject(memoryDC, oldBitmap);
224 ::DeleteDC(memoryDC);
225 ::DeleteDC(memoryDC2);
226
227 // Map to system colours
228 wxMapBitmap((HBITMAP) m_hBitmap, totalBitmapWidth, totalBitmapHeight);
229
230 if ( oldToolBarBitmap )
231 {
232 TBREPLACEBITMAP replaceBitmap;
233 replaceBitmap.hInstOld = NULL;
234 replaceBitmap.hInstNew = NULL;
235 replaceBitmap.nIDOld = (UINT) oldToolBarBitmap;
236 replaceBitmap.nIDNew = (UINT) (HBITMAP) m_hBitmap;
237 replaceBitmap.nButtons = noButtons;
238 if (::SendMessage((HWND) GetHWND(), TB_REPLACEBITMAP, (WPARAM) 0, (LPARAM) &replaceBitmap) == -1)
239 {
240 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
241 }
242
243 ::DeleteObject((HBITMAP) oldToolBarBitmap);
244
245 // Now delete all the buttons
246 int i = 0;
247 while ( TRUE )
248 {
249 // TODO: What about separators???? They don't have an id!
250 if ( ! ::SendMessage( (HWND) GetHWND(), TB_DELETEBUTTON, i, 0 ) )
251 break;
252 }
253 }
254 else
255 {
256 TBADDBITMAP addBitmap;
257 addBitmap.hInst = 0;
258 addBitmap.nID = (UINT)m_hBitmap;
259 if (::SendMessage((HWND) GetHWND(), TB_ADDBITMAP, (WPARAM) noButtons, (LPARAM) &addBitmap) == -1)
260 {
261 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
262 }
263 }
264
265 // Now add the buttons.
266 TBBUTTON buttons[50];
267
268 node = m_tools.First();
269 int i = 0;
270 int bitmapId = 0;
271 while (node)
272 {
273 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
274 if (tool->m_toolStyle == wxTOOL_STYLE_SEPARATOR)
275 {
276 buttons[i].iBitmap = 0;
277 buttons[i].idCommand = 0;
278
279 buttons[i].fsState = TBSTATE_ENABLED;
280 buttons[i].fsStyle = TBSTYLE_SEP;
281 buttons[i].dwData = 0L;
282 buttons[i].iString = 0;
283 }
284 else
285 {
286 buttons[i].iBitmap = bitmapId;
287 buttons[i].idCommand = tool->m_index;
288
289 buttons[i].fsState = 0;
290 if (tool->m_enabled)
291 buttons[i].fsState |= TBSTATE_ENABLED;
292 if (tool->m_toggleState)
293 buttons[i].fsState |= TBSTATE_CHECKED;
294 buttons[i].fsStyle = tool->m_isToggle ? TBSTYLE_CHECK : TBSTYLE_BUTTON;
295 buttons[i].dwData = 0L;
296 buttons[i].iString = 0;
297
298 bitmapId ++;
299 }
300
301 i ++;
302 node = node->Next();
303 }
304
305 long rc = ::SendMessage((HWND) GetHWND(), TB_ADDBUTTONS, (WPARAM)i, (LPARAM)& buttons);
306
307 wxCHECK_MSG( rc, FALSE, wxT("failed to add buttons to the toolbar") );
308
309 (void)::SendMessage((HWND) GetHWND(), TB_AUTOSIZE, (WPARAM)0, (LPARAM) 0);
310
311 SetRows(m_maxRows);
312*/
313 return TRUE;
314}
315
316bool wxToolBar::OS2Command(WXUINT cmd, WXWORD id)
317{
318 wxNode *node = m_tools.Find((long)id);
319 if (!node)
320 return FALSE;
321 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
322// TODO:
323/*
324 if (tool->m_isToggle)
325 tool->m_toggleState = (1 == (1 & (int)::SendMessage((HWND) GetHWND(), TB_GETSTATE, (WPARAM) id, (LPARAM) 0)));
326
327 BOOL ret = OnLeftClick((int)id, tool->m_toggleState);
328 if (ret == FALSE && tool->m_isToggle)
329 {
330 tool->m_toggleState = !tool->m_toggleState;
331 ::SendMessage((HWND) GetHWND(), TB_CHECKBUTTON, (WPARAM)id, (LPARAM)MAKELONG(tool->m_toggleState, 0));
332 }
333 return TRUE;
334*/
335 return FALSE;
336}
337
338bool wxToolBar::OS2OnNotify(int WXUNUSED(idCtrl),
339 WXLPARAM lParam,
340 WXLPARAM *result)
341{
342// TODO:
343/*
344 // First check if this applies to us
345 NMHDR *hdr = (NMHDR *)lParam;
346
347 // the tooltips control created by the toolbar is sometimes Unicode, even in
348 // an ANSI application
349 int code = (int)hdr->code;
350 if ( (code != TTN_NEEDTEXTA) && (code != TTN_NEEDTEXTW) )
351 return FALSE;
352
353 HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0);
354 if ( toolTipWnd != hdr->hwndFrom )
355 return FALSE;
356
357 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
358 int id = (int)ttText->hdr.idFrom;
359 wxNode *node = m_tools.Find((long)id);
360 if (!node)
361 return FALSE;
362
363 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
364
365 const wxString& help = tool->m_shortHelpString;
366
367 if ( !help.IsEmpty() )
368 {
369 if ( code == TTN_NEEDTEXTA )
370 {
371 ttText->lpszText = (wxChar *)help.c_str();
372 }
373#if (_WIN32_IE >= 0x0300)
374 else
375 {
376 // FIXME this is a temp hack only until I understand better what
377 // must be done in both ANSI and Unicode builds
378
379 size_t lenAnsi = help.Len();
380 #ifdef __MWERKS__
381 // MetroWerks doesn't like calling mbstowcs with NULL argument
382 size_t lenUnicode = 2*lenAnsi;
383 #else
384 size_t lenUnicode = mbstowcs(NULL, help, lenAnsi);
385 #endif
386
387 // using the pointer of right type avoids us doing all sorts of
388 // pointer arithmetics ourselves
389 wchar_t *dst = (wchar_t *)ttText->szText,
390 *pwz = new wchar_t[lenUnicode + 1];
391 mbstowcs(pwz, help, lenAnsi + 1);
392 memcpy(dst, pwz, lenUnicode*sizeof(wchar_t));
393
394 // put the terminating _wide_ NUL
395 dst[lenUnicode] = 0;
396
397 delete [] pwz;
398 }
399#endif // _WIN32_IE >= 0x0300
400 }
401
402 // For backward compatibility...
403 OnMouseEnter(tool->m_index);
404
405 return TRUE;
406*/
407 return FALSE;
408}
409
410void wxToolBar::SetToolBitmapSize(const wxSize& size)
411{
412 m_defaultWidth = size.x;
413 m_defaultHeight = size.y;
414// ::SendMessage((HWND) GetHWND(), TB_SETBITMAPSIZE, 0, (LPARAM) MAKELONG ((int)size.x, (int)size.y));
415}
416
417void wxToolBar::SetRows(int nRows)
418{
419// TODO:
420/*
421 RECT rect;
422 ::SendMessage((HWND) GetHWND(), TB_SETROWS, MAKEWPARAM(nRows, TRUE), (LPARAM) & rect);
423 m_maxWidth = (rect.right - rect.left + 2);
424 m_maxHeight = (rect.bottom - rect.top + 2);
425*/
426}
427
428wxSize wxToolBar::GetMaxSize() const
429{
430// TODO:
431/*
432 if ((m_maxWidth == -1) || (m_maxHeight == -1))
433 {
434 RECT rect;
435 ::SendMessage((HWND) GetHWND(), TB_SETROWS, MAKEWPARAM(m_maxRows, TRUE), (LPARAM) & rect);
436 ((wxToolBar *)this)->m_maxWidth = (rect.right - rect.left + 2); // ???
437 ((wxToolBar *)this)->m_maxHeight = (rect.bottom - rect.top + 2); // ???
438 }
439*/
440 return wxSize(m_maxWidth, m_maxHeight);
441}
442
443// The button size is bigger than the bitmap size
444wxSize wxToolBar::GetToolSize() const
445{
446 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
447}
448
449void wxToolBar::EnableTool(int toolIndex, bool enable)
450{
451 wxNode *node = m_tools.Find((long)toolIndex);
452 if (node)
453 {
454 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
455 tool->m_enabled = enable;
456// ::SendMessage((HWND) GetHWND(), TB_ENABLEBUTTON, (WPARAM)toolIndex, (LPARAM)MAKELONG(enable, 0));
457 }
458}
459
460void wxToolBar::ToggleTool(int toolIndex, bool toggle)
461{
462 wxNode *node = m_tools.Find((long)toolIndex);
463 if (node)
464 {
465 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
466 if (tool->m_isToggle)
467 {
468 tool->m_toggleState = toggle;
469// ::SendMessage((HWND) GetHWND(), TB_CHECKBUTTON, (WPARAM)toolIndex, (LPARAM)MAKELONG(toggle, 0));
470 }
471 }
472}
473
474bool wxToolBar::GetToolState(int toolIndex) const
475{
476// return (::SendMessage((HWND) GetHWND(), TB_ISBUTTONCHECKED, (WPARAM)toolIndex, (LPARAM)0) != 0);
477 return FALSE;
478}
479
480void wxToolBar::ClearTools()
481{
482 // TODO: Don't know how to reset the toolbar bitmap, as yet.
483 // But adding tools and calling CreateTools should probably
484 // recreate a buttonbar OK.
485 wxToolBarBase::ClearTools();
486}
487
488// If pushedBitmap is NULL, a reversed version of bitmap is
489// created and used as the pushed/toggled image.
490// If toggle is TRUE, the button toggles between the two states.
491wxToolBarTool *wxToolBar::AddTool(int index, const wxBitmap& bitmap, const wxBitmap& pushedBitmap,
492 bool toggle, long xPos, long yPos, wxObject *clientData, const wxString& helpString1, const wxString& helpString2)
493{
494 wxToolBarTool *tool = new wxToolBarTool(index, bitmap, wxNullBitmap, toggle, xPos, yPos, helpString1, helpString2);
495 tool->m_clientData = clientData;
496
497 if (xPos > -1)
498 tool->m_x = xPos;
499 else
500 tool->m_x = m_xMargin;
501
502 if (yPos > -1)
503 tool->m_y = yPos;
504 else
505 tool->m_y = m_yMargin;
506
507 tool->SetSize(GetToolSize().x, GetToolSize().y);
508
509 m_tools.Append((long)index, tool);
510 return tool;
511}
512
513// Responds to colour changes, and passes event on to children.
514void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event)
515{
516// TODO:
517/*
518 m_backgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
519 GetGValue(GetSysColor(COLOR_BTNFACE)), GetBValue(GetSysColor(COLOR_BTNFACE)));
520*/
521 // Remap the buttons
522 CreateTools();
523
524 Refresh();
525
526 // Propagate the event to the non-top-level children
527 wxWindow::OnSysColourChanged(event);
528}
529
530void wxToolBar::OnMouseEvent(wxMouseEvent& event)
531{
532 if (event.RightDown())
533 {
534 // For now, we don't have an id. Later we could
535 // try finding the tool.
536 OnRightClick((int)-1, event.GetX(), event.GetY());
537 }
538 else
539 {
540 event.Skip();
541 }
542}
543
544// These are the default colors used to map the bitmap colors
545// to the current system colors
546
547#define BGR_BUTTONTEXT (RGB(000,000,000)) // black
548#define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
549#define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
550#define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
551#define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
552#define BGR_BACKGROUND (RGB(255,000,255)) // magenta
553
554void wxMapBitmap(HBITMAP hBitmap, int width, int height)
555{
556 COLORMAP ColorMap[] = {
557 {BGR_BUTTONTEXT, COLOR_BTNTEXT}, // black
558 {BGR_BUTTONSHADOW, COLOR_BTNSHADOW}, // dark grey
559 {BGR_BUTTONFACE, COLOR_BTNFACE}, // bright grey
560 {BGR_BUTTONHILIGHT, COLOR_BTNHIGHLIGHT},// white
561 {BGR_BACKGROUNDSEL, COLOR_HIGHLIGHT}, // blue
562 {BGR_BACKGROUND, COLOR_WINDOW} // magenta
563 };
564
565 int NUM_MAPS = (sizeof(ColorMap)/sizeof(COLORMAP));
566 int n;
567 for ( n = 0; n < NUM_MAPS; n++)
568 {
569 ColorMap[n].to = ::GetSysColor(ColorMap[n].to);
570 }
571
572 HBITMAP hbmOld;
573 HDC hdcMem = CreateCompatibleDC(NULL);
574
575 if (hdcMem)
576 {
577 hbmOld = (HBITMAP) SelectObject(hdcMem, hBitmap);
578
579 int i, j, k;
580 for ( i = 0; i < width; i++)
581 {
582 for ( j = 0; j < height; j++)
583 {
584// COLORREF pixel = ::GetPixel(hdcMem, i, j);
585/*
586 BYTE red = GetRValue(pixel);
587 BYTE green = GetGValue(pixel);
588 BYTE blue = GetBValue(pixel);
589*/
590
591 for ( k = 0; k < NUM_MAPS; k ++)
592 {
593 if ( ColorMap[k].from == pixel )
594 {
595// /* COLORREF actualPixel = */ ::SetPixel(hdcMem, i, j, ColorMap[k].to);
596 break;
597 }
598 }
599 }
600 }
601
602
603// SelectObject(hdcMem, hbmOld);
604// DeleteObject(hdcMem);
605 }
606
607}
608
609// Some experiments...
610#if 0
611 // What we want to do is create another bitmap which has a depth of 4,
612 // and set the bits. So probably we want to convert this HBITMAP into a
613 // DIB, then call SetDIBits.
614 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
615 // the screen), then SetDIBits fails.
616 HBITMAP newBitmap = ::CreateBitmap(totalBitmapWidth, totalBitmapHeight, 1, 4, NULL);
617 HANDLE newDIB = ::BitmapToDIB((HBITMAP) m_hBitmap, NULL);
618 LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) GlobalLock(newDIB);
619
620 dc = ::GetDC(NULL);
621// LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
622
623 int result = ::SetDIBits(dc, newBitmap, 0, lpbmi->biHeight, FindDIBBits((LPSTR)lpbmi), (LPBITMAPINFO)lpbmi,
624 DIB_PAL_COLORS);
625 DWORD err = GetLastError();
626
627 ::ReleaseDC(NULL, dc);
628
629 // Delete the DIB
630 GlobalUnlock (newDIB);
631 GlobalFree (newDIB);
632
633// WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
634 // Substitute our new bitmap for the old one
635 ::DeleteObject((HBITMAP) m_hBitmap);
636 m_hBitmap = (WXHBITMAP) newBitmap;
637#endif
638
639
640#endif