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