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