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