]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tbar95.cpp
Now allow SetMenuBar to be called several times.
[wxWidgets.git] / src / msw / tbar95.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
1c383dba 2// Name: msw/tbar95.cpp
2bda0e17
KB
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
89b892a2 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
1c383dba
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
1c383dba 21 #pragma implementation "tbar95.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
1c383dba 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
1c383dba
VZ
32 #include "wx/log.h"
33 #include "wx/intl.h"
34 #include "wx/dynarray.h"
4e15f6c5 35 #include "wx/settings.h"
381dd4bf 36 #include "wx/bitmap.h"
2bda0e17
KB
37#endif
38
47d67540 39#if wxUSE_BUTTONBAR && wxUSE_TOOLBAR && defined(__WIN95__)
2bda0e17 40
ce3ed50d 41#if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
1c383dba 42 #include "malloc.h"
2bda0e17
KB
43#endif
44
1c383dba 45#include "wx/msw/private.h"
2bda0e17 46
57c208c5 47#ifndef __TWIN32__
1c383dba
VZ
48
49#ifdef __GNUWIN32_OLD__
50 #include "wx/msw/gnuwin32/extra.h"
51#else
52 #include <commctrl.h>
65fd5cb0 53#endif
2bda0e17 54
1c383dba
VZ
55#endif // __TWIN32__
56
2bda0e17
KB
57#include "wx/msw/dib.h"
58#include "wx/tbar95.h"
1c383dba
VZ
59#include "wx/app.h" // for GetComCtl32Version
60
61// ----------------------------------------------------------------------------
62// constants
63// ----------------------------------------------------------------------------
64
65// these standard constants are not always defined in compilers headers
2bda0e17 66
6a23cbce 67// Styles
bb6290e3 68#ifndef TBSTYLE_FLAT
1c383dba
VZ
69 #define TBSTYLE_LIST 0x1000
70 #define TBSTYLE_FLAT 0x0800
71 #define TBSTYLE_TRANSPARENT 0x8000
bb6290e3
JS
72#endif
73 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
74
6a23cbce
JS
75// Messages
76#ifndef TB_GETSTYLE
1c383dba
VZ
77 #define TB_GETSTYLE (WM_USER + 57)
78 #define TB_SETSTYLE (WM_USER + 56)
6a23cbce
JS
79#endif
80
1c383dba 81// these values correspond to those used by comctl32.dll
81d66cf3
JS
82#define DEFAULTBITMAPX 16
83#define DEFAULTBITMAPY 15
84#define DEFAULTBUTTONX 24
85#define DEFAULTBUTTONY 24
86#define DEFAULTBARHEIGHT 27
87
1c383dba
VZ
88// ----------------------------------------------------------------------------
89// function prototypes
90// ----------------------------------------------------------------------------
91
92static void wxMapBitmap(HBITMAP hBitmap, int width, int height);
93
94// ----------------------------------------------------------------------------
95// wxWin macros
96// ----------------------------------------------------------------------------
97
2bda0e17 98#if !USE_SHARED_LIBRARY
7c0ea335 99 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
89b892a2 100#endif
2bda0e17
KB
101
102BEGIN_EVENT_TABLE(wxToolBar95, wxToolBarBase)
89b892a2 103 EVT_MOUSE_EVENTS(wxToolBar95::OnMouseEvent)
2bda0e17
KB
104 EVT_SYS_COLOUR_CHANGED(wxToolBar95::OnSysColourChanged)
105END_EVENT_TABLE()
2bda0e17 106
1c383dba
VZ
107// ============================================================================
108// implementation
109// ============================================================================
2bda0e17 110
1c383dba
VZ
111// ----------------------------------------------------------------------------
112// wxToolBar95 construction
113// ----------------------------------------------------------------------------
114
115void wxToolBar95::Init()
2bda0e17 116{
1c383dba
VZ
117 m_maxWidth = -1;
118 m_maxHeight = -1;
119 m_hBitmap = 0;
120 m_defaultWidth = DEFAULTBITMAPX;
121 m_defaultHeight = DEFAULTBITMAPY;
2bda0e17
KB
122}
123
89b892a2
VZ
124bool wxToolBar95::Create(wxWindow *parent,
125 wxWindowID id,
126 const wxPoint& pos,
127 const wxSize& size,
128 long style,
129 const wxString& name)
2bda0e17 130{
1c383dba
VZ
131 wxASSERT_MSG( (style & wxTB_VERTICAL) == 0,
132 wxT("Sorry, wxToolBar95 under Windows 95 only "
133 "supports horizontal orientation.") );
edccf428 134
1c383dba
VZ
135 // common initialisation
136 if ( !CreateControl(parent, id, pos, size, style, name) )
137 return FALSE;
89b892a2 138
1c383dba
VZ
139 // prepare flags
140 DWORD msflags = 0; // WS_VISIBLE | WS_CHILD always included
141 if (style & wxBORDER)
142 msflags |= WS_BORDER;
143 msflags |= TBSTYLE_TOOLTIPS;
2bda0e17 144
1c383dba
VZ
145 if (style & wxTB_FLAT)
146 {
147 if (wxTheApp->GetComCtl32Version() > 400)
c8f1f088 148 msflags |= TBSTYLE_FLAT;
1c383dba 149 }
2bda0e17 150
1c383dba
VZ
151 // MSW-specific initialisation
152 if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME, msflags) )
153 return FALSE;
2bda0e17 154
c8f1f088 155 // toolbar-specific post initialisation
1c383dba 156 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
89b892a2 157
c8f1f088
VZ
158 // set up the colors and fonts
159 wxRGBToColour(m_backgroundColour, GetSysColor(COLOR_BTNFACE));
160 m_foregroundColour = *wxBLACK;
161
162 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
163
1c383dba
VZ
164 // position it
165 int x = pos.x;
166 int y = pos.y;
167 int width = size.x;
168 int height = size.y;
2bda0e17 169
1c383dba
VZ
170 if (width <= 0)
171 width = 100;
172 if (height <= 0)
173 height = m_defaultHeight;
174 if (x < 0)
175 x = 0;
176 if (y < 0)
177 y = 0;
bb6290e3 178
1c383dba 179 SetSize(x, y, width, height);
2bda0e17 180
1c383dba 181 return TRUE;
2bda0e17
KB
182}
183
89b892a2 184wxToolBar95::~wxToolBar95()
2bda0e17 185{
1c383dba 186 UnsubclassWin();
2bda0e17 187
1c383dba
VZ
188 if (m_hBitmap)
189 {
190 ::DeleteObject((HBITMAP) m_hBitmap);
191 }
192}
193
194void wxToolBar95::ClearTools()
195{
196 // TODO: Don't know how to reset the toolbar bitmap, as yet.
197 // But adding tools and calling CreateTools should probably
198 // recreate a buttonbar OK.
199 wxToolBarBase::ClearTools();
200}
201
202bool wxToolBar95::AddControl(wxControl *control)
203{
204 wxCHECK_MSG( control, FALSE, _T("toolbar: can't insert NULL control") );
205
206 wxCHECK_MSG( control->GetParent() == this, FALSE,
207 _T("control must have toolbar as parent") );
208
209 wxToolBarTool *tool = new wxToolBarTool(control);
210
211 m_tools.Append(control->GetId(), tool);
212
213 return TRUE;
214}
215
216wxToolBarTool *wxToolBar95::AddTool(int index,
217 const wxBitmap& bitmap,
218 const wxBitmap& pushedBitmap,
219 bool toggle,
220 long xPos, long yPos,
221 wxObject *clientData,
222 const wxString& helpString1,
223 const wxString& helpString2)
224{
225 wxToolBarTool *tool = new wxToolBarTool(index, bitmap, wxNullBitmap,
226 toggle, xPos, yPos,
227 helpString1, helpString2);
228 tool->m_clientData = clientData;
229
230 if (xPos > -1)
231 tool->m_x = xPos;
232 else
233 tool->m_x = m_xMargin;
234
235 if (yPos > -1)
236 tool->m_y = yPos;
237 else
238 tool->m_y = m_yMargin;
239
240 tool->SetSize(GetToolSize().x, GetToolSize().y);
241
242 m_tools.Append((long)index, tool);
243
244 return tool;
2bda0e17
KB
245}
246
89b892a2 247bool wxToolBar95::CreateTools()
2bda0e17 248{
1c383dba
VZ
249 size_t nTools = m_tools.GetCount();
250 if ( nTools == 0 )
251 return FALSE;
2bda0e17 252
1c383dba 253 HBITMAP oldToolBarBitmap = (HBITMAP) m_hBitmap;
89b892a2 254
1c383dba
VZ
255 int totalBitmapWidth = (int)(m_defaultWidth * nTools);
256 int totalBitmapHeight = (int)m_defaultHeight;
2bda0e17 257
1c383dba
VZ
258 // Create a bitmap for all the tool bitmaps
259 HDC dc = ::GetDC(NULL);
260 m_hBitmap = (WXHBITMAP) ::CreateCompatibleBitmap(dc,
261 totalBitmapWidth,
262 totalBitmapHeight);
263 ::ReleaseDC(NULL, dc);
89b892a2 264
1c383dba
VZ
265 // Now blit all the tools onto this bitmap
266 HDC memoryDC = ::CreateCompatibleDC(NULL);
267 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(memoryDC, (HBITMAP)m_hBitmap);
2bda0e17 268
1c383dba 269 HDC memoryDC2 = ::CreateCompatibleDC(NULL);
2bda0e17 270
1c383dba
VZ
271 // the button position
272 wxCoord x = 0;
2bda0e17 273
1c383dba
VZ
274 // the number of buttons (not separators)
275 int noButtons = 0;
276
277 wxNode *node = m_tools.First();
278 while (node)
051205e6 279 {
1c383dba
VZ
280 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
281 if ( tool->m_toolStyle == wxTOOL_STYLE_BUTTON && tool->m_bitmap1.Ok() )
282 {
283 HBITMAP hbmp = GetHbitmapOf(tool->m_bitmap1);
284 if ( hbmp )
285 {
286 HBITMAP oldBitmap2 = (HBITMAP)::SelectObject(memoryDC2, hbmp);
287 if ( !BitBlt(memoryDC, x, 0, m_defaultWidth, m_defaultHeight,
288 memoryDC2, 0, 0, SRCCOPY) )
289 {
290 wxLogLastError("BitBlt");
291 }
292
293 ::SelectObject(memoryDC2, oldBitmap2);
294
295 x += m_defaultWidth;
296 noButtons++;
297 }
298 }
299 node = node->Next();
051205e6 300 }
2bda0e17 301
1c383dba
VZ
302 ::SelectObject(memoryDC, oldBitmap);
303 ::DeleteDC(memoryDC);
304 ::DeleteDC(memoryDC2);
2bda0e17 305
1c383dba
VZ
306 // Map to system colours
307 wxMapBitmap((HBITMAP) m_hBitmap, totalBitmapWidth, totalBitmapHeight);
308
309 if ( oldToolBarBitmap )
2bda0e17 310 {
1c383dba
VZ
311 TBREPLACEBITMAP replaceBitmap;
312 replaceBitmap.hInstOld = NULL;
313 replaceBitmap.hInstNew = NULL;
314 replaceBitmap.nIDOld = (UINT) oldToolBarBitmap;
315 replaceBitmap.nIDNew = (UINT) (HBITMAP) m_hBitmap;
316 replaceBitmap.nButtons = noButtons;
317 if ( ::SendMessage(GetHwnd(), TB_REPLACEBITMAP,
318 0, (LPARAM) &replaceBitmap) == -1 )
319 {
320 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
321 }
322
323 ::DeleteObject((HBITMAP) oldToolBarBitmap);
324
325 // Now delete all the buttons
326 int i = 0;
327 while ( TRUE )
328 {
329 // TODO: What about separators???? They don't have an id!
330 if ( ! ::SendMessage( GetHwnd(), TB_DELETEBUTTON, i, 0 ) )
331 break;
332 }
2bda0e17 333 }
1c383dba 334 else
051205e6 335 {
1c383dba
VZ
336 TBADDBITMAP addBitmap;
337 addBitmap.hInst = 0;
338 addBitmap.nID = (UINT)m_hBitmap;
339 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP,
340 (WPARAM) noButtons, (LPARAM)&addBitmap) == -1 )
341 {
342 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
343 }
051205e6 344 }
2bda0e17 345
1c383dba
VZ
346 // Now add the buttons.
347 TBBUTTON *buttons = new TBBUTTON[nTools];
2bda0e17 348
1c383dba
VZ
349 // this array will holds the indices of all controls in the toolbar
350 wxArrayInt controlIds;
351
352 int i = 0;
353 int bitmapId = 0;
354
355 node = m_tools.First();
356 while (node)
2bda0e17 357 {
1c383dba
VZ
358 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
359 TBBUTTON& button = buttons[i];
360
361 wxZeroMemory(button);
362
363 switch ( tool->m_toolStyle )
364 {
365 case wxTOOL_STYLE_CONTROL:
366 controlIds.Add(i);
367 button.idCommand = tool->m_index;
368 // fall through: create just a separator too
369
370 case wxTOOL_STYLE_SEPARATOR:
371 button.fsState = TBSTATE_ENABLED;
372 button.fsStyle = TBSTYLE_SEP;
373 break;
374
375 case wxTOOL_STYLE_BUTTON:
376 button.iBitmap = bitmapId;
377 button.idCommand = tool->m_index;
378
379 if (tool->m_enabled)
380 button.fsState |= TBSTATE_ENABLED;
381 if (tool->m_toggleState)
382 button.fsState |= TBSTATE_CHECKED;
383 button.fsStyle = tool->m_isToggle ? TBSTYLE_CHECK
384 : TBSTYLE_BUTTON;
385
386 bitmapId++;
387 break;
388 }
2bda0e17 389
1c383dba
VZ
390 i++;
391 node = node->Next();
2bda0e17 392 }
1c383dba
VZ
393
394 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS,
395 (WPARAM)i, (LPARAM)buttons) )
2bda0e17 396 {
1c383dba 397 wxLogLastError("TB_ADDBUTTONS");
2bda0e17 398 }
89b892a2 399
1c383dba 400 delete [] buttons;
2bda0e17 401
e42a6c18
VZ
402 // TBBUTTONINFO struct declaration is new (comctl32.dll 4.70+)
403#if !defined(__GNUWIN32__) && !defined(__WATCOMC__) && !defined(__BORLANDC__)
1c383dba
VZ
404 // adjust the controls size to fit nicely in the toolbar
405 size_t nControls = controlIds.GetCount();
406 for ( size_t nCtrl = 0; nCtrl < nControls; nCtrl++ )
407 {
408 wxToolBarTool *tool = (wxToolBarTool *)
409 m_tools.Nth(controlIds[nCtrl])->Data();
410 wxControl *control = tool->GetControl();
411
412 wxSize size = control->GetSize();
413
414 // set the (underlying) separators width to be that of the control
415 TBBUTTONINFO tbbi;
416 tbbi.cbSize = sizeof(tbbi);
417 tbbi.dwMask = TBIF_SIZE;
418 tbbi.cx = size.x;
419 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO,
420 tool->m_index, (LPARAM)&tbbi) )
421 {
422 // the index is probably invalid
423 wxLogLastError("TB_SETBUTTONINFO");
424 }
425
426 // and position the control itself correctly vertically
427 RECT r;
c8f1f088
VZ
428 if ( !SendMessage(GetHwnd(), TB_GETRECT,
429 tool->m_index, (LPARAM)(LPRECT)&r) )
430 {
431 wxLogLastError("TB_GETRECT");
432 }
1c383dba
VZ
433
434 int height = r.bottom - r.top;
435 int diff = height - size.y;
436 if ( diff < 0 )
437 {
438 // the control is too high, resize to fit
439 control->SetSize(-1, height - 2);
440
441 diff = 2;
442 }
2bda0e17 443
1c383dba
VZ
444 control->Move(r.left, r.top + diff / 2);
445 }
4e15f6c5 446#endif // __GNUWIN32__
89b892a2 447
1c383dba 448 (void)::SendMessage(GetHwnd(), TB_AUTOSIZE, (WPARAM)0, (LPARAM) 0);
89b892a2 449
1c383dba 450 SetRows(m_maxRows);
2bda0e17 451
1c383dba 452 return TRUE;
2bda0e17
KB
453}
454
1c383dba
VZ
455// ----------------------------------------------------------------------------
456// message handlers
457// ----------------------------------------------------------------------------
458
debe6624 459bool wxToolBar95::MSWCommand(WXUINT cmd, WXWORD id)
2bda0e17 460{
1c383dba
VZ
461 wxNode *node = m_tools.Find((long)id);
462 if (!node)
463 return FALSE;
464
465 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
466 if (tool->m_isToggle)
467 {
468 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
469 tool->m_toggleState = state & TBSTATE_CHECKED;
470 }
471
472 BOOL ret = OnLeftClick((int)id, tool->m_toggleState);
473 if ( ret == FALSE && tool->m_isToggle )
474 {
475 tool->m_toggleState = !tool->m_toggleState;
476 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
477 (WPARAM)id, (LPARAM)MAKELONG(tool->m_toggleState, 0));
478 }
479
480 return TRUE;
2bda0e17
KB
481}
482
a23fd0e1 483bool wxToolBar95::MSWOnNotify(int WXUNUSED(idCtrl),
fd3f686c
VZ
484 WXLPARAM lParam,
485 WXLPARAM *result)
2bda0e17 486{
89b892a2 487 // First check if this applies to us
2bda0e17 488 NMHDR *hdr = (NMHDR *)lParam;
2bda0e17 489
1c383dba
VZ
490 // the tooltips control created by the toolbar is sometimes Unicode, even
491 // in an ANSI application - this seems to be a bug in comctl32.dll v5
a17e237f
VZ
492 int code = (int)hdr->code;
493 if ( (code != TTN_NEEDTEXTA) && (code != TTN_NEEDTEXTW) )
89b892a2 494 return FALSE;
2bda0e17 495
89b892a2
VZ
496 HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0);
497 if ( toolTipWnd != hdr->hwndFrom )
498 return FALSE;
2bda0e17 499
89b892a2
VZ
500 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
501 int id = (int)ttText->hdr.idFrom;
502 wxNode *node = m_tools.Find((long)id);
503 if (!node)
504 return FALSE;
505
506 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
2bda0e17 507
8df13671
VZ
508 const wxString& help = tool->m_shortHelpString;
509
510 if ( !help.IsEmpty() )
89b892a2 511 {
a17e237f 512 if ( code == TTN_NEEDTEXTA )
89b892a2 513 {
837e5743 514 ttText->lpszText = (wxChar *)help.c_str();
89b892a2
VZ
515 }
516#if (_WIN32_IE >= 0x0300)
517 else
518 {
519 // FIXME this is a temp hack only until I understand better what
520 // must be done in both ANSI and Unicode builds
8df13671
VZ
521
522 size_t lenAnsi = help.Len();
b2cce0c4 523 #ifdef __MWERKS__
8df13671
VZ
524 // MetroWerks doesn't like calling mbstowcs with NULL argument
525 size_t lenUnicode = 2*lenAnsi;
b2cce0c4 526 #else
8df13671 527 size_t lenUnicode = mbstowcs(NULL, help, lenAnsi);
b2cce0c4 528 #endif
8df13671
VZ
529
530 // using the pointer of right type avoids us doing all sorts of
531 // pointer arithmetics ourselves
532 wchar_t *dst = (wchar_t *)ttText->szText,
533 *pwz = new wchar_t[lenUnicode + 1];
534 mbstowcs(pwz, help, lenAnsi + 1);
535 memcpy(dst, pwz, lenUnicode*sizeof(wchar_t));
536
537 // put the terminating _wide_ NUL
538 dst[lenUnicode] = 0;
89b892a2
VZ
539
540 delete [] pwz;
541 }
542#endif // _WIN32_IE >= 0x0300
2bda0e17 543 }
89b892a2
VZ
544
545 // For backward compatibility...
546 OnMouseEnter(tool->m_index);
547
548 return TRUE;
2bda0e17
KB
549}
550
1c383dba
VZ
551// ----------------------------------------------------------------------------
552// sizing stuff
553// ----------------------------------------------------------------------------
554
81d66cf3 555void wxToolBar95::SetToolBitmapSize(const wxSize& size)
2bda0e17 556{
1c383dba
VZ
557 wxToolBarBase::SetToolBitmapSize(size);
558
559 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
2bda0e17
KB
560}
561
debe6624 562void wxToolBar95::SetRows(int nRows)
2bda0e17 563{
1c383dba
VZ
564 // TRUE in wParam means to create at least as many rows
565 RECT rect;
566 ::SendMessage(GetHwnd(), TB_SETROWS,
567 MAKEWPARAM(nRows, TRUE), (LPARAM) &rect);
568
569 m_maxWidth = (rect.right - rect.left + 2);
570 m_maxHeight = (rect.bottom - rect.top + 2);
571
572 m_maxRows = nRows;
2bda0e17
KB
573}
574
89b892a2 575wxSize wxToolBar95::GetMaxSize() const
2bda0e17 576{
1c383dba
VZ
577 if ( (m_maxWidth == -1) || (m_maxHeight == -1) )
578 {
579 // it has a side effect of filling m_maxWidth/Height variables
580 ((wxToolBar95 *)this)->SetRows(m_maxRows); // const_cast
581 }
582
583 return wxSize(m_maxWidth, m_maxHeight);
2bda0e17
KB
584}
585
2bda0e17 586// The button size is bigger than the bitmap size
89b892a2 587wxSize wxToolBar95::GetToolSize() const
2bda0e17 588{
1c383dba
VZ
589 // FIXME: this is completely bogus (VZ)
590 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
2bda0e17
KB
591}
592
1c383dba
VZ
593// ----------------------------------------------------------------------------
594// tool state
595// ----------------------------------------------------------------------------
596
debe6624 597void wxToolBar95::EnableTool(int toolIndex, bool enable)
2bda0e17 598{
1c383dba
VZ
599 wxNode *node = m_tools.Find((long)toolIndex);
600 if (node)
601 {
602 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
603 tool->m_enabled = enable;
604 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
605 (WPARAM)toolIndex, (LPARAM)MAKELONG(enable, 0));
606 }
2bda0e17
KB
607}
608
debe6624 609void wxToolBar95::ToggleTool(int toolIndex, bool toggle)
2bda0e17 610{
1c383dba
VZ
611 wxNode *node = m_tools.Find((long)toolIndex);
612 if (node)
2bda0e17 613 {
1c383dba
VZ
614 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
615 if (tool->m_isToggle)
616 {
617 tool->m_toggleState = toggle;
618 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
619 (WPARAM)toolIndex, (LPARAM)MAKELONG(toggle, 0));
620 }
2bda0e17 621 }
2bda0e17
KB
622}
623
088a95f5
JS
624bool wxToolBar95::GetToolState(int toolIndex) const
625{
1c383dba 626 return (::SendMessage(GetHwnd(), TB_ISBUTTONCHECKED, (WPARAM)toolIndex, (LPARAM)0) != 0);
2bda0e17
KB
627}
628
1c383dba
VZ
629// ----------------------------------------------------------------------------
630// event handlers
631// ----------------------------------------------------------------------------
2bda0e17
KB
632
633// Responds to colour changes, and passes event on to children.
634void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent& event)
635{
636 m_backgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
89b892a2 637 GetGValue(GetSysColor(COLOR_BTNFACE)), GetBValue(GetSysColor(COLOR_BTNFACE)));
2bda0e17
KB
638
639 // Remap the buttons
640 CreateTools();
641
2bda0e17
KB
642 Refresh();
643
644 // Propagate the event to the non-top-level children
645 wxWindow::OnSysColourChanged(event);
646}
647
e6460682
JS
648void wxToolBar95::OnMouseEvent(wxMouseEvent& event)
649{
650 if (event.RightDown())
651 {
652 // For now, we don't have an id. Later we could
653 // try finding the tool.
654 OnRightClick((int)-1, event.GetX(), event.GetY());
655 }
656 else
657 {
42e69d6b 658 event.Skip();
e6460682
JS
659 }
660}
661
1c383dba
VZ
662// ----------------------------------------------------------------------------
663// private functions
664// ----------------------------------------------------------------------------
665
2bda0e17
KB
666// These are the default colors used to map the bitmap colors
667// to the current system colors
668
1c383dba
VZ
669// VZ: why are they BGR and not RGB? just to confuse the people or is there a
670// deeper reason?
2bda0e17
KB
671#define BGR_BUTTONTEXT (RGB(000,000,000)) // black
672#define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
673#define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
674#define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
675#define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
676#define BGR_BACKGROUND (RGB(255,000,255)) // magenta
677
678void wxMapBitmap(HBITMAP hBitmap, int width, int height)
679{
1c383dba
VZ
680 COLORMAP ColorMap[] =
681 {
2bda0e17
KB
682 {BGR_BUTTONTEXT, COLOR_BTNTEXT}, // black
683 {BGR_BUTTONSHADOW, COLOR_BTNSHADOW}, // dark grey
684 {BGR_BUTTONFACE, COLOR_BTNFACE}, // bright grey
685 {BGR_BUTTONHILIGHT, COLOR_BTNHIGHLIGHT},// white
686 {BGR_BACKGROUNDSEL, COLOR_HIGHLIGHT}, // blue
687 {BGR_BACKGROUND, COLOR_WINDOW} // magenta
688 };
689
690 int NUM_MAPS = (sizeof(ColorMap)/sizeof(COLORMAP));
691 int n;
692 for ( n = 0; n < NUM_MAPS; n++)
693 {
694 ColorMap[n].to = ::GetSysColor(ColorMap[n].to);
695 }
696
697 HBITMAP hbmOld;
698 HDC hdcMem = CreateCompatibleDC(NULL);
699
700 if (hdcMem)
701 {
c4e7c2aa 702 hbmOld = (HBITMAP) SelectObject(hdcMem, hBitmap);
2bda0e17
KB
703
704 int i, j, k;
705 for ( i = 0; i < width; i++)
706 {
707 for ( j = 0; j < height; j++)
708 {
709 COLORREF pixel = ::GetPixel(hdcMem, i, j);
710/*
711 BYTE red = GetRValue(pixel);
712 BYTE green = GetGValue(pixel);
713 BYTE blue = GetBValue(pixel);
714*/
715
716 for ( k = 0; k < NUM_MAPS; k ++)
717 {
718 if ( ColorMap[k].from == pixel )
719 {
720 /* COLORREF actualPixel = */ ::SetPixel(hdcMem, i, j, ColorMap[k].to);
721 break;
722 }
723 }
724 }
725 }
726
727
728 SelectObject(hdcMem, hbmOld);
729 DeleteObject(hdcMem);
730 }
731
732}
733
734// Some experiments...
735#if 0
736 // What we want to do is create another bitmap which has a depth of 4,
737 // and set the bits. So probably we want to convert this HBITMAP into a
738 // DIB, then call SetDIBits.
739 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
740 // the screen), then SetDIBits fails.
741 HBITMAP newBitmap = ::CreateBitmap(totalBitmapWidth, totalBitmapHeight, 1, 4, NULL);
742 HANDLE newDIB = ::BitmapToDIB((HBITMAP) m_hBitmap, NULL);
743 LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) GlobalLock(newDIB);
744
745 dc = ::GetDC(NULL);
746// LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
747
748 int result = ::SetDIBits(dc, newBitmap, 0, lpbmi->biHeight, FindDIBBits((LPSTR)lpbmi), (LPBITMAPINFO)lpbmi,
749 DIB_PAL_COLORS);
750 DWORD err = GetLastError();
751
752 ::ReleaseDC(NULL, dc);
753
754 // Delete the DIB
755 GlobalUnlock (newDIB);
756 GlobalFree (newDIB);
757
758// WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
759 // Substitute our new bitmap for the old one
760 ::DeleteObject((HBITMAP) m_hBitmap);
761 m_hBitmap = (WXHBITMAP) newBitmap;
762#endif
763
764
1c383dba 765#endif // !(wxUSE_TOOLBAR && Win95)