]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tbar95.cpp
bitmap and image updates
[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
bdc72a22
VZ
194// ----------------------------------------------------------------------------
195// adding/removing buttons
196// ----------------------------------------------------------------------------
197
1c383dba
VZ
198void wxToolBar95::ClearTools()
199{
200 // TODO: Don't know how to reset the toolbar bitmap, as yet.
201 // But adding tools and calling CreateTools should probably
202 // recreate a buttonbar OK.
203 wxToolBarBase::ClearTools();
204}
205
bdc72a22
VZ
206bool wxToolBar95::DeleteTool(int id)
207{
208 int index = GetIndexFromId(id);
209 wxASSERT_MSG( index != wxNOT_FOUND, _T("invalid toolbar button id") );
210
211 if ( !SendMessage(GetHwnd(), TB_DELETEBUTTON, index, 0) )
212 {
213 wxLogLastError("TB_DELETEBUTTON");
214
215 return FALSE;
216 }
217
218 wxNode *node = m_tools.Nth(index);
219 delete (wxToolBarTool *)node->Data();
220 m_tools.DeleteNode(node);
221
222 m_ids.RemoveAt(index);
223
224 return TRUE;
225}
226
1c383dba
VZ
227bool wxToolBar95::AddControl(wxControl *control)
228{
229 wxCHECK_MSG( control, FALSE, _T("toolbar: can't insert NULL control") );
230
231 wxCHECK_MSG( control->GetParent() == this, FALSE,
232 _T("control must have toolbar as parent") );
233
234 wxToolBarTool *tool = new wxToolBarTool(control);
235
236 m_tools.Append(control->GetId(), tool);
bdc72a22 237 m_ids.Add(control->GetId());
1c383dba
VZ
238
239 return TRUE;
240}
241
242wxToolBarTool *wxToolBar95::AddTool(int index,
243 const wxBitmap& bitmap,
244 const wxBitmap& pushedBitmap,
245 bool toggle,
246 long xPos, long yPos,
247 wxObject *clientData,
248 const wxString& helpString1,
249 const wxString& helpString2)
250{
251 wxToolBarTool *tool = new wxToolBarTool(index, bitmap, wxNullBitmap,
252 toggle, xPos, yPos,
253 helpString1, helpString2);
254 tool->m_clientData = clientData;
255
256 if (xPos > -1)
257 tool->m_x = xPos;
258 else
259 tool->m_x = m_xMargin;
260
261 if (yPos > -1)
262 tool->m_y = yPos;
263 else
264 tool->m_y = m_yMargin;
265
266 tool->SetSize(GetToolSize().x, GetToolSize().y);
267
268 m_tools.Append((long)index, tool);
bdc72a22 269 m_ids.Add(index);
1c383dba
VZ
270
271 return tool;
2bda0e17
KB
272}
273
89b892a2 274bool wxToolBar95::CreateTools()
2bda0e17 275{
1c383dba
VZ
276 size_t nTools = m_tools.GetCount();
277 if ( nTools == 0 )
278 return FALSE;
2bda0e17 279
1c383dba 280 HBITMAP oldToolBarBitmap = (HBITMAP) m_hBitmap;
89b892a2 281
1c383dba
VZ
282 int totalBitmapWidth = (int)(m_defaultWidth * nTools);
283 int totalBitmapHeight = (int)m_defaultHeight;
2bda0e17 284
1c383dba
VZ
285 // Create a bitmap for all the tool bitmaps
286 HDC dc = ::GetDC(NULL);
287 m_hBitmap = (WXHBITMAP) ::CreateCompatibleBitmap(dc,
288 totalBitmapWidth,
289 totalBitmapHeight);
290 ::ReleaseDC(NULL, dc);
89b892a2 291
1c383dba
VZ
292 // Now blit all the tools onto this bitmap
293 HDC memoryDC = ::CreateCompatibleDC(NULL);
294 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(memoryDC, (HBITMAP)m_hBitmap);
2bda0e17 295
1c383dba 296 HDC memoryDC2 = ::CreateCompatibleDC(NULL);
2bda0e17 297
1c383dba
VZ
298 // the button position
299 wxCoord x = 0;
2bda0e17 300
1c383dba
VZ
301 // the number of buttons (not separators)
302 int noButtons = 0;
303
304 wxNode *node = m_tools.First();
305 while (node)
051205e6 306 {
1c383dba
VZ
307 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
308 if ( tool->m_toolStyle == wxTOOL_STYLE_BUTTON && tool->m_bitmap1.Ok() )
309 {
310 HBITMAP hbmp = GetHbitmapOf(tool->m_bitmap1);
311 if ( hbmp )
312 {
313 HBITMAP oldBitmap2 = (HBITMAP)::SelectObject(memoryDC2, hbmp);
314 if ( !BitBlt(memoryDC, x, 0, m_defaultWidth, m_defaultHeight,
315 memoryDC2, 0, 0, SRCCOPY) )
316 {
317 wxLogLastError("BitBlt");
318 }
319
320 ::SelectObject(memoryDC2, oldBitmap2);
321
322 x += m_defaultWidth;
323 noButtons++;
324 }
325 }
326 node = node->Next();
051205e6 327 }
2bda0e17 328
1c383dba
VZ
329 ::SelectObject(memoryDC, oldBitmap);
330 ::DeleteDC(memoryDC);
331 ::DeleteDC(memoryDC2);
2bda0e17 332
1c383dba
VZ
333 // Map to system colours
334 wxMapBitmap((HBITMAP) m_hBitmap, totalBitmapWidth, totalBitmapHeight);
335
336 if ( oldToolBarBitmap )
2bda0e17 337 {
1c383dba
VZ
338 TBREPLACEBITMAP replaceBitmap;
339 replaceBitmap.hInstOld = NULL;
340 replaceBitmap.hInstNew = NULL;
341 replaceBitmap.nIDOld = (UINT) oldToolBarBitmap;
342 replaceBitmap.nIDNew = (UINT) (HBITMAP) m_hBitmap;
343 replaceBitmap.nButtons = noButtons;
344 if ( ::SendMessage(GetHwnd(), TB_REPLACEBITMAP,
345 0, (LPARAM) &replaceBitmap) == -1 )
346 {
347 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
348 }
349
350 ::DeleteObject((HBITMAP) oldToolBarBitmap);
351
352 // Now delete all the buttons
353 int i = 0;
354 while ( TRUE )
355 {
356 // TODO: What about separators???? They don't have an id!
357 if ( ! ::SendMessage( GetHwnd(), TB_DELETEBUTTON, i, 0 ) )
358 break;
359 }
2bda0e17 360 }
1c383dba 361 else
051205e6 362 {
1c383dba
VZ
363 TBADDBITMAP addBitmap;
364 addBitmap.hInst = 0;
365 addBitmap.nID = (UINT)m_hBitmap;
366 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP,
367 (WPARAM) noButtons, (LPARAM)&addBitmap) == -1 )
368 {
369 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
370 }
051205e6 371 }
2bda0e17 372
1c383dba
VZ
373 // Now add the buttons.
374 TBBUTTON *buttons = new TBBUTTON[nTools];
2bda0e17 375
1c383dba
VZ
376 // this array will holds the indices of all controls in the toolbar
377 wxArrayInt controlIds;
378
379 int i = 0;
380 int bitmapId = 0;
381
382 node = m_tools.First();
383 while (node)
2bda0e17 384 {
1c383dba
VZ
385 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
386 TBBUTTON& button = buttons[i];
387
388 wxZeroMemory(button);
389
390 switch ( tool->m_toolStyle )
391 {
392 case wxTOOL_STYLE_CONTROL:
393 controlIds.Add(i);
394 button.idCommand = tool->m_index;
395 // fall through: create just a separator too
396
397 case wxTOOL_STYLE_SEPARATOR:
398 button.fsState = TBSTATE_ENABLED;
399 button.fsStyle = TBSTYLE_SEP;
400 break;
401
402 case wxTOOL_STYLE_BUTTON:
403 button.iBitmap = bitmapId;
404 button.idCommand = tool->m_index;
405
406 if (tool->m_enabled)
407 button.fsState |= TBSTATE_ENABLED;
408 if (tool->m_toggleState)
409 button.fsState |= TBSTATE_CHECKED;
410 button.fsStyle = tool->m_isToggle ? TBSTYLE_CHECK
411 : TBSTYLE_BUTTON;
412
413 bitmapId++;
414 break;
415 }
2bda0e17 416
1c383dba
VZ
417 i++;
418 node = node->Next();
2bda0e17 419 }
1c383dba
VZ
420
421 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS,
422 (WPARAM)i, (LPARAM)buttons) )
2bda0e17 423 {
1c383dba 424 wxLogLastError("TB_ADDBUTTONS");
2bda0e17 425 }
89b892a2 426
1c383dba 427 delete [] buttons;
2bda0e17 428
1c383dba
VZ
429 // adjust the controls size to fit nicely in the toolbar
430 size_t nControls = controlIds.GetCount();
431 for ( size_t nCtrl = 0; nCtrl < nControls; nCtrl++ )
432 {
433 wxToolBarTool *tool = (wxToolBarTool *)
434 m_tools.Nth(controlIds[nCtrl])->Data();
435 wxControl *control = tool->GetControl();
436
437 wxSize size = control->GetSize();
438
bdc72a22
VZ
439 // the position of the leftmost controls corner
440 int left = -1;
441
442 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
443 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
444 // available in headers, now check whether it is available now
445 // (during run-time)
446 if ( wxTheApp->GetComCtl32Version() >= 471 )
447 {
448 // set the (underlying) separators width to be that of the
449 // control
450 TBBUTTONINFO tbbi;
451 tbbi.cbSize = sizeof(tbbi);
452 tbbi.dwMask = TBIF_SIZE;
453 tbbi.cx = size.x;
454 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO,
455 tool->m_index, (LPARAM)&tbbi) )
456 {
457 // the index is probably invalid
458 wxLogLastError("TB_SETBUTTONINFO");
459 }
460
461 }
462 else
463 #endif // comctl32.dll 4.71
464 // TB_SETBUTTONINFO unavailable
465 {
466 int index = GetIndexFromId(tool->m_index);
467 wxASSERT_MSG( index != wxNOT_FOUND,
468 _T("control wasn't added to the tbar?") );
469
470 // try adding several separators to fit the controls width
471 RECT r;
472 if ( !SendMessage(GetHwnd(), TB_GETRECT,
473 tool->m_index, (LPARAM)(LPRECT)&r) )
474 {
475 wxLogLastError("TB_GETITEMRECT");
476 }
477
478 int widthSep = r.right - r.left;
479 left = r.left;
480
481 TBBUTTON tbb;
482 wxZeroMemory(tbb);
483 tbb.idCommand = 0;
484 tbb.fsState = TBSTATE_ENABLED;
485 tbb.fsStyle = TBSTYLE_SEP;
486
487 size_t nSeparators = size.x / widthSep;
488 for ( size_t nSep = 0; nSep < nSeparators; nSep++ )
489 {
490 m_ids.Insert(0, (size_t)index);
491
492 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON,
493 index, (LPARAM)&tbb) )
494 {
495 wxLogLastError("TB_INSERTBUTTON");
496 }
497 }
498
499 // adjust the controls width to exactly cover the separators
500 control->SetSize((nSeparators + 1)*widthSep, -1);
501 }
1c383dba
VZ
502
503 // and position the control itself correctly vertically
504 RECT r;
c8f1f088
VZ
505 if ( !SendMessage(GetHwnd(), TB_GETRECT,
506 tool->m_index, (LPARAM)(LPRECT)&r) )
507 {
508 wxLogLastError("TB_GETRECT");
509 }
1c383dba
VZ
510
511 int height = r.bottom - r.top;
512 int diff = height - size.y;
513 if ( diff < 0 )
514 {
515 // the control is too high, resize to fit
516 control->SetSize(-1, height - 2);
517
518 diff = 2;
519 }
2bda0e17 520
bdc72a22 521 control->Move(left == -1 ? r.left : left, r.top + diff / 2);
1c383dba 522 }
89b892a2 523
1c383dba 524 (void)::SendMessage(GetHwnd(), TB_AUTOSIZE, (WPARAM)0, (LPARAM) 0);
89b892a2 525
1c383dba 526 SetRows(m_maxRows);
2bda0e17 527
1c383dba 528 return TRUE;
2bda0e17
KB
529}
530
1c383dba
VZ
531// ----------------------------------------------------------------------------
532// message handlers
533// ----------------------------------------------------------------------------
534
debe6624 535bool wxToolBar95::MSWCommand(WXUINT cmd, WXWORD id)
2bda0e17 536{
1c383dba
VZ
537 wxNode *node = m_tools.Find((long)id);
538 if (!node)
539 return FALSE;
540
541 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
542 if (tool->m_isToggle)
543 {
544 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
545 tool->m_toggleState = state & TBSTATE_CHECKED;
546 }
547
548 BOOL ret = OnLeftClick((int)id, tool->m_toggleState);
549 if ( ret == FALSE && tool->m_isToggle )
550 {
551 tool->m_toggleState = !tool->m_toggleState;
552 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
553 (WPARAM)id, (LPARAM)MAKELONG(tool->m_toggleState, 0));
554 }
555
556 return TRUE;
2bda0e17
KB
557}
558
a23fd0e1 559bool wxToolBar95::MSWOnNotify(int WXUNUSED(idCtrl),
fd3f686c
VZ
560 WXLPARAM lParam,
561 WXLPARAM *result)
2bda0e17 562{
89b892a2 563 // First check if this applies to us
2bda0e17 564 NMHDR *hdr = (NMHDR *)lParam;
2bda0e17 565
1c383dba
VZ
566 // the tooltips control created by the toolbar is sometimes Unicode, even
567 // in an ANSI application - this seems to be a bug in comctl32.dll v5
a17e237f
VZ
568 int code = (int)hdr->code;
569 if ( (code != TTN_NEEDTEXTA) && (code != TTN_NEEDTEXTW) )
89b892a2 570 return FALSE;
2bda0e17 571
89b892a2
VZ
572 HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0);
573 if ( toolTipWnd != hdr->hwndFrom )
574 return FALSE;
2bda0e17 575
89b892a2
VZ
576 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
577 int id = (int)ttText->hdr.idFrom;
578 wxNode *node = m_tools.Find((long)id);
579 if (!node)
580 return FALSE;
581
582 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
2bda0e17 583
8df13671
VZ
584 const wxString& help = tool->m_shortHelpString;
585
586 if ( !help.IsEmpty() )
89b892a2 587 {
a17e237f 588 if ( code == TTN_NEEDTEXTA )
89b892a2 589 {
837e5743 590 ttText->lpszText = (wxChar *)help.c_str();
89b892a2
VZ
591 }
592#if (_WIN32_IE >= 0x0300)
593 else
594 {
595 // FIXME this is a temp hack only until I understand better what
596 // must be done in both ANSI and Unicode builds
8df13671
VZ
597
598 size_t lenAnsi = help.Len();
b2cce0c4 599 #ifdef __MWERKS__
8df13671
VZ
600 // MetroWerks doesn't like calling mbstowcs with NULL argument
601 size_t lenUnicode = 2*lenAnsi;
b2cce0c4 602 #else
8df13671 603 size_t lenUnicode = mbstowcs(NULL, help, lenAnsi);
b2cce0c4 604 #endif
8df13671
VZ
605
606 // using the pointer of right type avoids us doing all sorts of
607 // pointer arithmetics ourselves
608 wchar_t *dst = (wchar_t *)ttText->szText,
609 *pwz = new wchar_t[lenUnicode + 1];
610 mbstowcs(pwz, help, lenAnsi + 1);
611 memcpy(dst, pwz, lenUnicode*sizeof(wchar_t));
612
613 // put the terminating _wide_ NUL
614 dst[lenUnicode] = 0;
89b892a2
VZ
615
616 delete [] pwz;
617 }
618#endif // _WIN32_IE >= 0x0300
2bda0e17 619 }
89b892a2
VZ
620
621 // For backward compatibility...
622 OnMouseEnter(tool->m_index);
623
624 return TRUE;
2bda0e17
KB
625}
626
1c383dba
VZ
627// ----------------------------------------------------------------------------
628// sizing stuff
629// ----------------------------------------------------------------------------
630
81d66cf3 631void wxToolBar95::SetToolBitmapSize(const wxSize& size)
2bda0e17 632{
1c383dba
VZ
633 wxToolBarBase::SetToolBitmapSize(size);
634
635 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
2bda0e17
KB
636}
637
debe6624 638void wxToolBar95::SetRows(int nRows)
2bda0e17 639{
1c383dba
VZ
640 // TRUE in wParam means to create at least as many rows
641 RECT rect;
642 ::SendMessage(GetHwnd(), TB_SETROWS,
643 MAKEWPARAM(nRows, TRUE), (LPARAM) &rect);
644
645 m_maxWidth = (rect.right - rect.left + 2);
646 m_maxHeight = (rect.bottom - rect.top + 2);
647
648 m_maxRows = nRows;
2bda0e17
KB
649}
650
89b892a2 651wxSize wxToolBar95::GetMaxSize() const
2bda0e17 652{
1c383dba
VZ
653 if ( (m_maxWidth == -1) || (m_maxHeight == -1) )
654 {
655 // it has a side effect of filling m_maxWidth/Height variables
656 ((wxToolBar95 *)this)->SetRows(m_maxRows); // const_cast
657 }
658
659 return wxSize(m_maxWidth, m_maxHeight);
2bda0e17
KB
660}
661
2bda0e17 662// The button size is bigger than the bitmap size
89b892a2 663wxSize wxToolBar95::GetToolSize() const
2bda0e17 664{
1c383dba
VZ
665 // FIXME: this is completely bogus (VZ)
666 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
2bda0e17
KB
667}
668
1c383dba
VZ
669// ----------------------------------------------------------------------------
670// tool state
671// ----------------------------------------------------------------------------
672
debe6624 673void wxToolBar95::EnableTool(int toolIndex, bool enable)
2bda0e17 674{
1c383dba
VZ
675 wxNode *node = m_tools.Find((long)toolIndex);
676 if (node)
677 {
678 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
679 tool->m_enabled = enable;
680 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
681 (WPARAM)toolIndex, (LPARAM)MAKELONG(enable, 0));
682 }
2bda0e17
KB
683}
684
debe6624 685void wxToolBar95::ToggleTool(int toolIndex, bool toggle)
2bda0e17 686{
1c383dba
VZ
687 wxNode *node = m_tools.Find((long)toolIndex);
688 if (node)
2bda0e17 689 {
1c383dba
VZ
690 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
691 if (tool->m_isToggle)
692 {
693 tool->m_toggleState = toggle;
694 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
695 (WPARAM)toolIndex, (LPARAM)MAKELONG(toggle, 0));
696 }
2bda0e17 697 }
2bda0e17
KB
698}
699
088a95f5
JS
700bool wxToolBar95::GetToolState(int toolIndex) const
701{
bdc72a22
VZ
702 wxASSERT_MSG( GetIndexFromId(toolIndex) != wxNOT_FOUND,
703 _T("invalid toolbar button id") );
704
705 return ::SendMessage(GetHwnd(), TB_ISBUTTONCHECKED,
706 (WPARAM)toolIndex, (LPARAM)0) != 0;
2bda0e17
KB
707}
708
1c383dba
VZ
709// ----------------------------------------------------------------------------
710// event handlers
711// ----------------------------------------------------------------------------
2bda0e17
KB
712
713// Responds to colour changes, and passes event on to children.
714void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent& event)
715{
716 m_backgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
89b892a2 717 GetGValue(GetSysColor(COLOR_BTNFACE)), GetBValue(GetSysColor(COLOR_BTNFACE)));
2bda0e17
KB
718
719 // Remap the buttons
720 CreateTools();
721
2bda0e17
KB
722 Refresh();
723
724 // Propagate the event to the non-top-level children
725 wxWindow::OnSysColourChanged(event);
726}
727
e6460682
JS
728void wxToolBar95::OnMouseEvent(wxMouseEvent& event)
729{
730 if (event.RightDown())
731 {
732 // For now, we don't have an id. Later we could
733 // try finding the tool.
734 OnRightClick((int)-1, event.GetX(), event.GetY());
735 }
736 else
737 {
42e69d6b 738 event.Skip();
e6460682
JS
739 }
740}
741
bdc72a22
VZ
742
743// ----------------------------------------------------------------------------
744// helpers
745// ----------------------------------------------------------------------------
746
747int wxToolBar95::GetIndexFromId(int id) const
748{
749 size_t count = m_ids.GetCount();
750 for ( size_t n = 0; n < count; n++ )
751 {
752 if ( m_ids[n] == id )
753 return n;
754 }
755
756 return wxNOT_FOUND;
757}
758
1c383dba
VZ
759// ----------------------------------------------------------------------------
760// private functions
761// ----------------------------------------------------------------------------
762
bdc72a22
VZ
763// These are the default colors used to map the bitmap colors to the current
764// system colors. Note that they are in BGR format because this is what Windows
765// wants (and not RGB)
2bda0e17
KB
766
767#define BGR_BUTTONTEXT (RGB(000,000,000)) // black
768#define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
769#define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
770#define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
771#define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
772#define BGR_BACKGROUND (RGB(255,000,255)) // magenta
773
774void wxMapBitmap(HBITMAP hBitmap, int width, int height)
775{
1c383dba
VZ
776 COLORMAP ColorMap[] =
777 {
2bda0e17
KB
778 {BGR_BUTTONTEXT, COLOR_BTNTEXT}, // black
779 {BGR_BUTTONSHADOW, COLOR_BTNSHADOW}, // dark grey
780 {BGR_BUTTONFACE, COLOR_BTNFACE}, // bright grey
781 {BGR_BUTTONHILIGHT, COLOR_BTNHIGHLIGHT},// white
782 {BGR_BACKGROUNDSEL, COLOR_HIGHLIGHT}, // blue
783 {BGR_BACKGROUND, COLOR_WINDOW} // magenta
784 };
785
786 int NUM_MAPS = (sizeof(ColorMap)/sizeof(COLORMAP));
787 int n;
788 for ( n = 0; n < NUM_MAPS; n++)
789 {
790 ColorMap[n].to = ::GetSysColor(ColorMap[n].to);
791 }
792
793 HBITMAP hbmOld;
794 HDC hdcMem = CreateCompatibleDC(NULL);
795
796 if (hdcMem)
797 {
c4e7c2aa 798 hbmOld = (HBITMAP) SelectObject(hdcMem, hBitmap);
2bda0e17
KB
799
800 int i, j, k;
801 for ( i = 0; i < width; i++)
802 {
803 for ( j = 0; j < height; j++)
804 {
805 COLORREF pixel = ::GetPixel(hdcMem, i, j);
806/*
807 BYTE red = GetRValue(pixel);
808 BYTE green = GetGValue(pixel);
809 BYTE blue = GetBValue(pixel);
810*/
811
812 for ( k = 0; k < NUM_MAPS; k ++)
813 {
814 if ( ColorMap[k].from == pixel )
815 {
816 /* COLORREF actualPixel = */ ::SetPixel(hdcMem, i, j, ColorMap[k].to);
817 break;
818 }
819 }
820 }
821 }
822
823
824 SelectObject(hdcMem, hbmOld);
825 DeleteObject(hdcMem);
826 }
827
828}
829
830// Some experiments...
831#if 0
832 // What we want to do is create another bitmap which has a depth of 4,
833 // and set the bits. So probably we want to convert this HBITMAP into a
834 // DIB, then call SetDIBits.
835 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
836 // the screen), then SetDIBits fails.
837 HBITMAP newBitmap = ::CreateBitmap(totalBitmapWidth, totalBitmapHeight, 1, 4, NULL);
838 HANDLE newDIB = ::BitmapToDIB((HBITMAP) m_hBitmap, NULL);
839 LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) GlobalLock(newDIB);
840
841 dc = ::GetDC(NULL);
842// LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
843
844 int result = ::SetDIBits(dc, newBitmap, 0, lpbmi->biHeight, FindDIBBits((LPSTR)lpbmi), (LPBITMAPINFO)lpbmi,
845 DIB_PAL_COLORS);
846 DWORD err = GetLastError();
847
848 ::ReleaseDC(NULL, dc);
849
850 // Delete the DIB
851 GlobalUnlock (newDIB);
852 GlobalFree (newDIB);
853
854// WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
855 // Substitute our new bitmap for the old one
856 ::DeleteObject((HBITMAP) m_hBitmap);
857 m_hBitmap = (WXHBITMAP) newBitmap;
858#endif
859
860
1c383dba 861#endif // !(wxUSE_TOOLBAR && Win95)