]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tbar95.cpp
somehow log target wasn't being created automatically anymore. Restored.
[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 USE_BUTTONBAR && USE_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
76 BEGIN_EVENT_TABLE(wxToolBar95, wxToolBarBase)
77 EVT_SIZE(wxToolBar95::OnSize)
78 EVT_PAINT(wxToolBar95::OnPaint)
79 EVT_KILL_FOCUS(wxToolBar95::OnKillFocus)
80 EVT_MOUSE_EVENTS(wxToolBar95::OnMouseEvent)
81 EVT_SYS_COLOUR_CHANGED(wxToolBar95::OnSysColourChanged)
82 END_EVENT_TABLE()
83 #endif
84
85 void wxMapBitmap(HBITMAP hBitmap, int width, int height);
86
87 wxToolBar95::wxToolBar95(void)
88 {
89 m_maxWidth = -1;
90 m_maxHeight = -1;
91 m_hBitmap = 0;
92 m_defaultWidth = DEFAULTBITMAPX;
93 m_defaultHeight = DEFAULTBITMAPY;
94 }
95
96 bool wxToolBar95::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
97 long style, const wxString& name)
98 {
99 m_backgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
100 GetGValue(GetSysColor(COLOR_BTNFACE)), GetBValue(GetSysColor(COLOR_BTNFACE)));
101 m_foregroundColour = *wxBLACK ;
102
103 m_defaultForegroundColour = *wxBLACK ;
104 m_defaultBackgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
105 GetGValue(GetSysColor(COLOR_BTNFACE)), GetBValue(GetSysColor(COLOR_BTNFACE)));
106
107 if (style & wxTB_VERTICAL)
108 wxMessageBox("Sorry, wxToolBar95 under Windows 95 only supports horizontal orientation.", "wxToolBar95 usage", wxOK);
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 int x = pos.x;
119 int y = pos.y;
120 int width = size.x;
121 int height = size.y;
122
123 m_windowStyle = style;
124
125 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
126
127 SetParent(parent);
128
129 DWORD msflags = 0;
130 if (style & wxBORDER)
131 msflags |= WS_BORDER;
132 msflags |= WS_CHILD | WS_VISIBLE;
133
134 if (width <= 0)
135 width = 100;
136 if (height <= 0)
137 height = 30;
138 if (x < 0)
139 x = 0;
140 if (y < 0)
141 y = 0;
142
143 m_windowId = (id < 0 ? NewControlId() : id);
144 DWORD msStyle = WS_CHILD | WS_BORDER | WS_VISIBLE | TBSTYLE_TOOLTIPS;
145
146 if (style & wxTB_FLAT)
147 {
148 if (wxTheApp->GetComCtl32Version() > 400)
149 msStyle |= TBSTYLE_FLAT;
150 }
151
152 // Create the toolbar control.
153 HWND hWndToolbar = CreateWindowEx(0L, // No extended styles.
154 TOOLBARCLASSNAME, // Class name for the toolbar.
155 "", // No default text.
156 msStyle, // Styles and defaults.
157 x, y, width, height, // Standard toolbar size and position.
158 (HWND) parent->GetHWND(), // Parent window of the toolbar.
159 (HMENU)m_windowId, // Toolbar ID.
160 wxGetInstance(), // Current instance.
161 NULL ); // No class data.
162
163 // Toolbar-specific initialisation
164 ::SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), (LPARAM)0);
165
166 m_hWnd = (WXHWND) hWndToolbar;
167 if (parent) parent->AddChild(this);
168
169 SubclassWin((WXHWND) hWndToolbar);
170
171 return TRUE;
172 }
173
174 wxToolBar95::~wxToolBar95(void)
175 {
176 UnsubclassWin();
177
178 if (m_hBitmap)
179 {
180 ::DeleteObject((HBITMAP) m_hBitmap);
181 m_hBitmap = 0;
182 }
183 }
184
185 bool wxToolBar95::CreateTools(void)
186 {
187 if (m_tools.Number() == 0)
188 return FALSE;
189
190 HBITMAP oldToolBarBitmap = (HBITMAP) m_hBitmap;
191
192 int totalBitmapWidth = (int)(m_defaultWidth * m_tools.Number());
193 int totalBitmapHeight = (int)m_defaultHeight;
194
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);
199
200 // Now blit all the tools onto this bitmap
201 HDC memoryDC = ::CreateCompatibleDC(NULL);
202 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(memoryDC, (HBITMAP) m_hBitmap);
203
204 HDC memoryDC2 = ::CreateCompatibleDC(NULL);
205 int x = 0;
206 wxNode *node = m_tools.First();
207 int noButtons = 0;
208 while (node)
209 {
210 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
211 if ((tool->m_toolStyle != wxTOOL_STYLE_SEPARATOR) && tool->m_bitmap1.Ok() && tool->m_bitmap1.GetHBITMAP())
212 {
213 // wxPalette *palette = tool->m_bitmap1->GetPalette();
214
215 HBITMAP oldBitmap2 = (HBITMAP) ::SelectObject(memoryDC2, (HBITMAP) tool->m_bitmap1.GetHBITMAP());
216 /* int bltResult = */
217 BitBlt(memoryDC, x, 0, (int) m_defaultWidth, (int) m_defaultHeight, memoryDC2,
218 0, 0, SRCCOPY);
219 ::SelectObject(memoryDC2, oldBitmap2);
220 x += (int)m_defaultWidth;
221 noButtons ++;
222 }
223 node = node->Next();
224 }
225 ::SelectObject(memoryDC, oldBitmap);
226 ::DeleteDC(memoryDC);
227 ::DeleteDC(memoryDC2);
228
229 // Map to system colours
230 wxMapBitmap((HBITMAP) m_hBitmap, totalBitmapWidth, totalBitmapHeight);
231
232 if ( oldToolBarBitmap )
233 {
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)
241 wxMessageBox("Could not add bitmap to toolbar");
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 wxMessageBox("Could not add bitmap to toolbar");
261 }
262
263 // Now add the buttons.
264 TBBUTTON buttons[50];
265
266 node = m_tools.First();
267 int i = 0;
268 int bitmapId = 0;
269 while (node)
270 {
271 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
272 if (tool->m_toolStyle == wxTOOL_STYLE_SEPARATOR)
273 {
274 buttons[i].iBitmap = 0;
275 buttons[i].idCommand = 0;
276
277 buttons[i].fsState = TBSTATE_ENABLED;
278 buttons[i].fsStyle = TBSTYLE_SEP;
279 buttons[i].dwData = 0L;
280 buttons[i].iString = 0;
281 }
282 else
283 {
284 buttons[i].iBitmap = bitmapId;
285 buttons[i].idCommand = tool->m_index;
286
287 buttons[i].fsState = 0;
288 if (tool->m_enabled)
289 buttons[i].fsState |= TBSTATE_ENABLED;
290 if (tool->m_toggleState)
291 buttons[i].fsState |= TBSTATE_CHECKED;
292 buttons[i].fsStyle = tool->m_isToggle ? TBSTYLE_CHECK : TBSTYLE_BUTTON;
293 buttons[i].dwData = 0L;
294 buttons[i].iString = 0;
295
296 bitmapId ++;
297 }
298
299 i ++;
300 node = node->Next();
301 }
302
303 int ans = (int)::SendMessage((HWND) GetHWND(), TB_ADDBUTTONS, (WPARAM)i, (LPARAM)& buttons);
304 ans = (int)::SendMessage((HWND) GetHWND(), TB_AUTOSIZE, (WPARAM)0, (LPARAM) 0);
305
306 RECT rect;
307 ::SendMessage((HWND) GetHWND(), TB_SETROWS, MAKEWPARAM(m_maxRows, TRUE), (LPARAM) & rect);
308 m_maxWidth = (rect.right - rect.left + 2);
309 m_maxHeight = (rect.bottom - rect.top + 2);
310
311 return TRUE;
312 }
313
314 bool wxToolBar95::MSWCommand(WXUINT cmd, WXWORD id)
315 {
316 wxNode *node = m_tools.Find((long)id);
317 if (!node)
318 return FALSE;
319 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
320 if (tool->m_isToggle)
321 tool->m_toggleState = (1 == (1 & (int)::SendMessage((HWND) GetHWND(), TB_GETSTATE, (WPARAM) id, (LPARAM) 0)));
322
323 BOOL ret = OnLeftClick((int)id, tool->m_toggleState);
324 if (ret == FALSE && tool->m_isToggle)
325 {
326 tool->m_toggleState = !tool->m_toggleState;
327 ::SendMessage((HWND) GetHWND(), TB_CHECKBUTTON, (WPARAM)id, (LPARAM)MAKELONG(tool->m_toggleState, 0));
328 }
329 return TRUE;
330 }
331
332 bool wxToolBar95::MSWNotify(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
333 {
334 // First check if this applies to us
335 NMHDR *hdr = (NMHDR *)lParam;
336 if (hdr->code != TTN_NEEDTEXT)
337 return FALSE;
338
339 HWND toolTipWnd = (HWND) ::SendMessage((HWND) GetHWND(), TB_GETTOOLTIPS, 0, 0);
340 if ( toolTipWnd != hdr->hwndFrom )
341 return FALSE;
342
343 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT) lParam;
344 int id = (int)ttText->hdr.idFrom;
345 wxNode *node = m_tools.Find((long)id);
346 if (!node)
347 return FALSE;
348
349 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
350
351 switch (ttText->hdr.code)
352 {
353 case TTN_NEEDTEXT:
354 {
355 if (tool->m_shortHelpString != "")
356 ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
357
358 // For backward compatibility...
359 OnMouseEnter(tool->m_index);
360 break;
361 }
362 default:
363 return FALSE;
364 break;
365 }
366
367 return TRUE;
368 }
369
370 void wxToolBar95::SetToolBitmapSize(const wxSize& size)
371 {
372 m_defaultWidth = size.x; m_defaultHeight = size.y;
373 ::SendMessage((HWND) GetHWND(), TB_SETBITMAPSIZE, 0, (LPARAM) MAKELONG ((int)size.x, (int)size.y));
374 }
375
376 void wxToolBar95::SetRows(int nRows)
377 {
378 RECT rect;
379 ::SendMessage((HWND) GetHWND(), TB_SETROWS, MAKEWPARAM(nRows, TRUE), (LPARAM) & rect);
380 m_maxWidth = (rect.right - rect.left + 2);
381 m_maxHeight = (rect.bottom - rect.top + 2);
382 }
383
384 wxSize wxToolBar95::GetMaxSize(void) const
385 {
386 if (m_maxWidth == -1 | m_maxHeight == -1)
387 {
388 RECT rect;
389 ::SendMessage((HWND) GetHWND(), TB_SETROWS, MAKEWPARAM(m_maxRows, TRUE), (LPARAM) & rect);
390 ((wxToolBar95 *)this)->m_maxWidth = (rect.right - rect.left + 2); // ???
391 ((wxToolBar95 *)this)->m_maxHeight = (rect.bottom - rect.top + 2); // ???
392 }
393 return wxSize(m_maxWidth, m_maxHeight);
394 }
395
396 void wxToolBar95::GetSize(int *w, int *h) const
397 {
398 wxWindow::GetSize(w, h);
399 // For some reason, the returned height is several pixels bigger than that
400 // displayed!
401 *h -= 2;
402 }
403
404 // The button size is bigger than the bitmap size
405 wxSize wxToolBar95::GetToolSize(void) const
406 {
407 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
408 }
409
410 void wxToolBar95::EnableTool(int toolIndex, bool enable)
411 {
412 wxNode *node = m_tools.Find((long)toolIndex);
413 if (node)
414 {
415 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
416 tool->m_enabled = enable;
417 ::SendMessage((HWND) GetHWND(), TB_ENABLEBUTTON, (WPARAM)toolIndex, (LPARAM)MAKELONG(enable, 0));
418 }
419 }
420
421 void wxToolBar95::ToggleTool(int toolIndex, bool toggle)
422 {
423 wxNode *node = m_tools.Find((long)toolIndex);
424 if (node)
425 {
426 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
427 if (tool->m_isToggle)
428 {
429 tool->m_toggleState = toggle;
430 ::SendMessage((HWND) GetHWND(), TB_CHECKBUTTON, (WPARAM)toolIndex, (LPARAM)MAKELONG(toggle, 0));
431 }
432 }
433 }
434
435 bool wxToolBar95::GetToolState(int toolIndex) const
436 {
437 return (::SendMessage((HWND) GetHWND(), TB_ISBUTTONCHECKED, (WPARAM)toolIndex, (LPARAM)0) != 0);
438 }
439
440 void wxToolBar95::ClearTools(void)
441 {
442 // TODO: Don't know how to reset the toolbar bitmap, as yet.
443 // But adding tools and calling CreateTools should probably
444 // recreate a buttonbar OK.
445 wxToolBarBase::ClearTools();
446 }
447
448 // If pushedBitmap is NULL, a reversed version of bitmap is
449 // created and used as the pushed/toggled image.
450 // If toggle is TRUE, the button toggles between the two states.
451 wxToolBarTool *wxToolBar95::AddTool(int index, const wxBitmap& bitmap, const wxBitmap& pushedBitmap,
452 bool toggle, long xPos, long yPos, wxObject *clientData, const wxString& helpString1, const wxString& helpString2)
453 {
454 wxToolBarTool *tool = new wxToolBarTool(index, bitmap, (wxBitmap *)NULL, toggle, xPos, yPos, helpString1, helpString2);
455 tool->m_clientData = clientData;
456
457 if (xPos > -1)
458 tool->m_x = xPos;
459 else
460 tool->m_x = m_xMargin;
461
462 if (yPos > -1)
463 tool->m_y = yPos;
464 else
465 tool->m_y = m_yMargin;
466
467 tool->SetSize(GetDefaultButtonWidth(), GetDefaultButtonHeight());
468
469 m_tools.Append((long)index, tool);
470 return tool;
471 }
472
473 // Responds to colour changes, and passes event on to children.
474 void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent& event)
475 {
476 m_backgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
477 GetGValue(GetSysColor(COLOR_BTNFACE)), GetBValue(GetSysColor(COLOR_BTNFACE)));
478 m_defaultBackgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
479 GetGValue(GetSysColor(COLOR_BTNFACE)), GetBValue(GetSysColor(COLOR_BTNFACE)));
480
481 // Remap the buttons
482 CreateTools();
483
484 Default();
485
486 Refresh();
487
488 // Propagate the event to the non-top-level children
489 wxWindow::OnSysColourChanged(event);
490 }
491
492 // These are the default colors used to map the bitmap colors
493 // to the current system colors
494
495 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
496 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
497 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
498 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
499 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
500 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
501
502 void wxMapBitmap(HBITMAP hBitmap, int width, int height)
503 {
504 COLORMAP ColorMap[] = {
505 {BGR_BUTTONTEXT, COLOR_BTNTEXT}, // black
506 {BGR_BUTTONSHADOW, COLOR_BTNSHADOW}, // dark grey
507 {BGR_BUTTONFACE, COLOR_BTNFACE}, // bright grey
508 {BGR_BUTTONHILIGHT, COLOR_BTNHIGHLIGHT},// white
509 {BGR_BACKGROUNDSEL, COLOR_HIGHLIGHT}, // blue
510 {BGR_BACKGROUND, COLOR_WINDOW} // magenta
511 };
512
513 int NUM_MAPS = (sizeof(ColorMap)/sizeof(COLORMAP));
514 int n;
515 for ( n = 0; n < NUM_MAPS; n++)
516 {
517 ColorMap[n].to = ::GetSysColor(ColorMap[n].to);
518 }
519
520 HBITMAP hbmOld;
521 HDC hdcMem = CreateCompatibleDC(NULL);
522
523 if (hdcMem)
524 {
525 hbmOld = (HBITMAP) SelectObject(hdcMem, hBitmap);
526
527 int i, j, k;
528 for ( i = 0; i < width; i++)
529 {
530 for ( j = 0; j < height; j++)
531 {
532 COLORREF pixel = ::GetPixel(hdcMem, i, j);
533 /*
534 BYTE red = GetRValue(pixel);
535 BYTE green = GetGValue(pixel);
536 BYTE blue = GetBValue(pixel);
537 */
538
539 for ( k = 0; k < NUM_MAPS; k ++)
540 {
541 if ( ColorMap[k].from == pixel )
542 {
543 /* COLORREF actualPixel = */ ::SetPixel(hdcMem, i, j, ColorMap[k].to);
544 break;
545 }
546 }
547 }
548 }
549
550
551 SelectObject(hdcMem, hbmOld);
552 DeleteObject(hdcMem);
553 }
554
555 }
556
557 // Some experiments...
558 #if 0
559 // What we want to do is create another bitmap which has a depth of 4,
560 // and set the bits. So probably we want to convert this HBITMAP into a
561 // DIB, then call SetDIBits.
562 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
563 // the screen), then SetDIBits fails.
564 HBITMAP newBitmap = ::CreateBitmap(totalBitmapWidth, totalBitmapHeight, 1, 4, NULL);
565 HANDLE newDIB = ::BitmapToDIB((HBITMAP) m_hBitmap, NULL);
566 LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) GlobalLock(newDIB);
567
568 dc = ::GetDC(NULL);
569 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
570
571 int result = ::SetDIBits(dc, newBitmap, 0, lpbmi->biHeight, FindDIBBits((LPSTR)lpbmi), (LPBITMAPINFO)lpbmi,
572 DIB_PAL_COLORS);
573 DWORD err = GetLastError();
574
575 ::ReleaseDC(NULL, dc);
576
577 // Delete the DIB
578 GlobalUnlock (newDIB);
579 GlobalFree (newDIB);
580
581 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
582 // Substitute our new bitmap for the old one
583 ::DeleteObject((HBITMAP) m_hBitmap);
584 m_hBitmap = (WXHBITMAP) newBitmap;
585 #endif
586
587
588 #endif