]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tbar95.cpp
Avoid core dumps when SetImageList is used.
[wxWidgets.git] / src / msw / tbar95.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
1c383dba 2// Name: msw/tbar95.cpp
8a0681f9 3// Purpose: wxToolBar
2bda0e17
KB
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
1c383dba
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
8a0681f9 32 #include "wx/frame.h"
1c383dba
VZ
33 #include "wx/log.h"
34 #include "wx/intl.h"
35 #include "wx/dynarray.h"
4e15f6c5 36 #include "wx/settings.h"
381dd4bf 37 #include "wx/bitmap.h"
f6bcfd97 38 #include "wx/dcmemory.h"
f538710f 39 #include "wx/control.h"
2bda0e17
KB
40#endif
41
a96b4743 42#if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && (!defined(_WIN32_WCE) || (_WIN32_WCE >= 400 && !wxUSE_POCKETPC_UI))
8a0681f9
VZ
43
44#include "wx/toolbar.h"
dfa53609 45#include "wx/sysopt.h"
2bda0e17 46
1c383dba 47#include "wx/msw/private.h"
2bda0e17 48
0d236fd0
VZ
49// include <commctrl.h> "properly"
50#include "wx/msw/wrapcctl.h"
e5898961 51
1c383dba
VZ
52#include "wx/app.h" // for GetComCtl32Version
53
f6bcfd97
BP
54// ----------------------------------------------------------------------------
55// conditional compilation
56// ----------------------------------------------------------------------------
57
58// wxWindows previously always considered that toolbar buttons have light grey
59// (0xc0c0c0) background and so ignored any bitmap masks - however, this
60// doesn't work with XPMs which then appear to have black background. To make
61// this work, we must respect the bitmap masks - which we do now. This should
62// be ok in any case, but to restore 100% compatible with the old version
63// behaviour, you can set this to 0.
64#define USE_BITMAP_MASKS 1
65
1c383dba
VZ
66// ----------------------------------------------------------------------------
67// constants
68// ----------------------------------------------------------------------------
69
70// these standard constants are not always defined in compilers headers
2bda0e17 71
6a23cbce 72// Styles
bb6290e3 73#ifndef TBSTYLE_FLAT
1c383dba
VZ
74 #define TBSTYLE_LIST 0x1000
75 #define TBSTYLE_FLAT 0x0800
dd177170
RL
76#endif
77
78#ifndef TBSTYLE_TRANSPARENT
1c383dba 79 #define TBSTYLE_TRANSPARENT 0x8000
bb6290e3 80#endif
bb6290e3 81
a3399e6c
VZ
82#ifndef TBSTYLE_TOOLTIPS
83 #define TBSTYLE_TOOLTIPS 0x0100
84#endif
85
6a23cbce
JS
86// Messages
87#ifndef TB_GETSTYLE
1c383dba 88 #define TB_SETSTYLE (WM_USER + 56)
8a0681f9
VZ
89 #define TB_GETSTYLE (WM_USER + 57)
90#endif
91
92#ifndef TB_HITTEST
93 #define TB_HITTEST (WM_USER + 69)
6a23cbce
JS
94#endif
95
1c383dba 96// these values correspond to those used by comctl32.dll
81d66cf3
JS
97#define DEFAULTBITMAPX 16
98#define DEFAULTBITMAPY 15
99#define DEFAULTBUTTONX 24
100#define DEFAULTBUTTONY 24
101#define DEFAULTBARHEIGHT 27
102
1c383dba
VZ
103// ----------------------------------------------------------------------------
104// wxWin macros
105// ----------------------------------------------------------------------------
106
2eb10e2a 107IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
2bda0e17 108
066f1b7a
SC
109/*
110 TOOLBAR PROPERTIES
111 tool
112 bitmap
113 bitmap2
114 tooltip
115 longhelp
116 radio (bool)
117 toggle (bool)
118 separator
119 style ( wxNO_BORDER | wxTB_HORIZONTAL)
120 bitmapsize
121 margins
122 packing
123 separation
124
125 dontattachtoframe
126*/
127
8a0681f9
VZ
128BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
129 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent)
130 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged)
2bda0e17 131END_EVENT_TABLE()
2bda0e17 132
8a0681f9
VZ
133// ----------------------------------------------------------------------------
134// private classes
135// ----------------------------------------------------------------------------
136
137class wxToolBarTool : public wxToolBarToolBase
138{
139public:
140 wxToolBarTool(wxToolBar *tbar,
141 int id,
a3399e6c
VZ
142 const wxString& label,
143 const wxBitmap& bmpNormal,
144 const wxBitmap& bmpDisabled,
145 wxItemKind kind,
8a0681f9 146 wxObject *clientData,
a3399e6c
VZ
147 const wxString& shortHelp,
148 const wxString& longHelp)
149 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
150 clientData, shortHelp, longHelp)
8a0681f9
VZ
151 {
152 m_nSepCount = 0;
153 }
154
155 wxToolBarTool(wxToolBar *tbar, wxControl *control)
156 : wxToolBarToolBase(tbar, control)
157 {
158 m_nSepCount = 1;
159 }
160
c631abda
VZ
161 virtual void SetLabel(const wxString& label)
162 {
163 if ( label == m_label )
164 return;
165
166 wxToolBarToolBase::SetLabel(label);
167
168 // we need to update the label shown in the toolbar because it has a
169 // pointer to the internal buffer of the old label
170 //
171 // TODO: use TB_SETBUTTONINFO
172 }
173
8a0681f9
VZ
174 // set/get the number of separators which we use to cover the space used by
175 // a control in the toolbar
176 void SetSeparatorsCount(size_t count) { m_nSepCount = count; }
177 size_t GetSeparatorsCount() const { return m_nSepCount; }
178
179private:
180 size_t m_nSepCount;
2eb10e2a
VZ
181
182 DECLARE_NO_COPY_CLASS(wxToolBarTool)
8a0681f9
VZ
183};
184
185
1c383dba
VZ
186// ============================================================================
187// implementation
188// ============================================================================
2bda0e17 189
1c383dba 190// ----------------------------------------------------------------------------
8a0681f9 191// wxToolBarTool
1c383dba
VZ
192// ----------------------------------------------------------------------------
193
8a0681f9 194wxToolBarToolBase *wxToolBar::CreateTool(int id,
a3399e6c
VZ
195 const wxString& label,
196 const wxBitmap& bmpNormal,
197 const wxBitmap& bmpDisabled,
198 wxItemKind kind,
8a0681f9 199 wxObject *clientData,
a3399e6c
VZ
200 const wxString& shortHelp,
201 const wxString& longHelp)
8a0681f9 202{
a3399e6c
VZ
203 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
204 clientData, shortHelp, longHelp);
8a0681f9
VZ
205}
206
207wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
208{
209 return new wxToolBarTool(this, control);
210}
211
212// ----------------------------------------------------------------------------
213// wxToolBar construction
214// ----------------------------------------------------------------------------
215
216void wxToolBar::Init()
2bda0e17 217{
1c383dba 218 m_hBitmap = 0;
8a0681f9
VZ
219
220 m_nButtons = 0;
221
1c383dba
VZ
222 m_defaultWidth = DEFAULTBITMAPX;
223 m_defaultHeight = DEFAULTBITMAPY;
37d0bdff
MB
224
225 m_pInTool = 0;
2bda0e17
KB
226}
227
8a0681f9
VZ
228bool wxToolBar::Create(wxWindow *parent,
229 wxWindowID id,
230 const wxPoint& pos,
231 const wxSize& size,
232 long style,
233 const wxString& name)
2bda0e17 234{
1c383dba 235 // common initialisation
11b6a93b 236 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
1c383dba 237 return FALSE;
89b892a2 238
24998710 239 // MSW-specific initialisation
278c927d 240 if ( !MSWCreateToolbar(pos, size) )
24998710 241 return FALSE;
b0766406 242
9a63feff
VZ
243 wxSetCCUnicodeFormat(GetHwnd());
244
24998710
VZ
245 // set up the colors and fonts
246 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR));
247 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
7d6d3bf3 248
24998710
VZ
249 return TRUE;
250}
2bda0e17 251
278c927d 252bool wxToolBar::MSWCreateToolbar(const wxPoint& pos, const wxSize& size)
24998710 253{
fda7962d 254 if ( !MSWCreateControl(TOOLBARCLASSNAME, wxEmptyString, pos, size) )
1c383dba 255 return FALSE;
2bda0e17 256
c8f1f088 257 // toolbar-specific post initialisation
1c383dba 258 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
89b892a2 259
24998710
VZ
260 return TRUE;
261}
c8f1f088 262
24998710
VZ
263void wxToolBar::Recreate()
264{
265 const HWND hwndOld = GetHwnd();
266 if ( !hwndOld )
267 {
268 // we haven't been created yet, no need to recreate
269 return;
270 }
2bda0e17 271
24998710
VZ
272 // get the position and size before unsubclassing the old toolbar
273 const wxPoint pos = GetPosition();
274 const wxSize size = GetSize();
bb6290e3 275
24998710 276 UnsubclassWin();
2bda0e17 277
278c927d 278 if ( !MSWCreateToolbar(pos, size) )
24998710
VZ
279 {
280 // what can we do?
281 wxFAIL_MSG( _T("recreating the toolbar failed") );
282
283 return;
284 }
285
286 // reparent all our children under the new toolbar
222ed1d6 287 for ( wxWindowList::compatibility_iterator node = m_children.GetFirst();
24998710
VZ
288 node;
289 node = node->GetNext() )
290 {
291 wxWindow *win = node->GetData();
292 if ( !win->IsTopLevel() )
293 ::SetParent(GetHwndOf(win), GetHwnd());
294 }
295
296 // only destroy the old toolbar now -- after all the children had been
297 // reparented
298 ::DestroyWindow(hwndOld);
299
300 // it is for the old bitmap control and can't be used with the new one
301 if ( m_hBitmap )
302 {
303 ::DeleteObject((HBITMAP) m_hBitmap);
304 m_hBitmap = 0;
305 }
306
307 Realize();
308 UpdateSize();
2bda0e17
KB
309}
310
8a0681f9 311wxToolBar::~wxToolBar()
2bda0e17 312{
f6bcfd97
BP
313 // we must refresh the frame size when the toolbar is deleted but the frame
314 // is not - otherwise toolbar leaves a hole in the place it used to occupy
f6bcfd97 315 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
c2fd78b1 316 if ( frame && !frame->IsBeingDeleted() )
f6bcfd97
BP
317 {
318 frame->SendSizeEvent();
319 }
320
321 if ( m_hBitmap )
1c383dba
VZ
322 {
323 ::DeleteObject((HBITMAP) m_hBitmap);
324 }
325}
326
24998710
VZ
327wxSize wxToolBar::DoGetBestSize() const
328{
329 wxSize sizeBest = GetToolSize();
330 sizeBest.x *= GetToolsCount();
331
332 // reverse horz and vertical components if necessary
333 return HasFlag(wxTB_VERTICAL) ? wxSize(sizeBest.y, sizeBest.x) : sizeBest;
334}
335
336WXDWORD wxToolBar::MSWGetStyle(long style, WXDWORD *exstyle) const
337{
338 // toolbars never have border, giving one to them results in broken
339 // appearance
340 WXDWORD msStyle = wxControl::MSWGetStyle
341 (
342 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
343 );
344
345 // always include this one, it never hurts and setting it later only if we
346 // do have tooltips wouldn't work
347 msStyle |= TBSTYLE_TOOLTIPS;
348
f9dae779 349 if ( style & (wxTB_FLAT | wxTB_HORZ_LAYOUT) )
24998710
VZ
350 {
351 // static as it doesn't change during the program lifetime
352 static int s_verComCtl = wxTheApp->GetComCtl32Version();
353
354 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
355 // style with 6.00 (part of Windows XP) leads to the toolbar with
356 // incorrect background colour - and not using it still results in the
357 // correct (flat) toolbar, so don't use it there
358 if ( s_verComCtl > 400 && s_verComCtl < 600 )
359 {
360 msStyle |= TBSTYLE_FLAT | TBSTYLE_TRANSPARENT;
361 }
f9dae779
VZ
362
363 if ( s_verComCtl >= 470 && style & wxTB_HORZ_LAYOUT )
364 {
365 msStyle |= TBSTYLE_LIST;
366 }
24998710
VZ
367 }
368
369 if ( style & wxTB_NODIVIDER )
370 msStyle |= CCS_NODIVIDER;
371
372 if ( style & wxTB_NOALIGN )
373 msStyle |= CCS_NOPARENTALIGN;
374
3bb63e5c
JS
375 if ( style & wxTB_VERTICAL )
376 msStyle |= CCS_VERT;
377
24998710
VZ
378 return msStyle;
379}
380
bdc72a22 381// ----------------------------------------------------------------------------
8a0681f9 382// adding/removing tools
bdc72a22
VZ
383// ----------------------------------------------------------------------------
384
24998710 385bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
1c383dba 386{
8a0681f9
VZ
387 // nothing special to do here - we really create the toolbar buttons in
388 // Realize() later
389 tool->Attach(this);
390
391 return TRUE;
1c383dba
VZ
392}
393
8a0681f9 394bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
bdc72a22 395{
f6bcfd97
BP
396 // the main difficulty we have here is with the controls in the toolbars:
397 // as we (sometimes) use several separators to cover up the space used by
398 // them, the indices are not the same for us and the toolbar
399
400 // first determine the position of the first button to delete: it may be
401 // different from pos if we use several separators to cover the space used
402 // by a control
222ed1d6 403 wxToolBarToolsList::compatibility_iterator node;
f6bcfd97
BP
404 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
405 {
406 wxToolBarToolBase *tool2 = node->GetData();
407 if ( tool2 == tool )
408 {
409 // let node point to the next node in the list
410 node = node->GetNext();
411
412 break;
413 }
414
415 if ( tool2->IsControl() )
416 {
56bd6aac 417 pos += ((wxToolBarTool *)tool2)->GetSeparatorsCount() - 1;
f6bcfd97
BP
418 }
419 }
420
421 // now determine the number of buttons to delete and the area taken by them
8a0681f9 422 size_t nButtonsToDelete = 1;
bdc72a22 423
8a0681f9
VZ
424 // get the size of the button we're going to delete
425 RECT r;
426 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, pos, (LPARAM)&r) )
bdc72a22 427 {
8a0681f9 428 wxLogLastError(_T("TB_GETITEMRECT"));
bdc72a22
VZ
429 }
430
8a0681f9 431 int width = r.right - r.left;
bdc72a22 432
8a0681f9
VZ
433 if ( tool->IsControl() )
434 {
435 nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount();
bdc72a22 436
8a0681f9
VZ
437 width *= nButtonsToDelete;
438 }
bdc72a22 439
f6bcfd97
BP
440 // do delete all buttons
441 m_nButtons -= nButtonsToDelete;
8a0681f9
VZ
442 while ( nButtonsToDelete-- > 0 )
443 {
444 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) )
445 {
f6bcfd97 446 wxLogLastError(wxT("TB_DELETEBUTTON"));
1c383dba 447
8a0681f9
VZ
448 return FALSE;
449 }
450 }
1c383dba 451
8a0681f9 452 tool->Detach();
1c383dba 453
f6bcfd97
BP
454 // and finally reposition all the controls after this button (the toolbar
455 // takes care of all normal items)
456 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
8a0681f9
VZ
457 {
458 wxToolBarToolBase *tool2 = node->GetData();
459 if ( tool2->IsControl() )
460 {
461 int x;
462 wxControl *control = tool2->GetControl();
463 control->GetPosition(&x, NULL);
464 control->Move(x - width, -1);
465 }
466 }
1c383dba
VZ
467
468 return TRUE;
469}
470
8a0681f9 471bool wxToolBar::Realize()
1c383dba 472{
24998710 473 const size_t nTools = GetToolsCount();
8a0681f9
VZ
474 if ( nTools == 0 )
475 {
476 // nothing to do
477 return TRUE;
478 }
1c383dba 479
24998710 480 const bool isVertical = HasFlag(wxTB_VERTICAL);
2bda0e17 481
2b5f62a0
VZ
482 // delete all old buttons, if any
483 for ( size_t pos = 0; pos < m_nButtons; pos++ )
484 {
485 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) )
486 {
487 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
488 }
489 }
490
8a0681f9
VZ
491 // First, add the bitmap: we use one bitmap for all toolbar buttons
492 // ----------------------------------------------------------------
2bda0e17 493
222ed1d6 494 wxToolBarToolsList::compatibility_iterator node;
24998710
VZ
495 int bitmapId = 0;
496
497 wxSize sizeBmp;
498 if ( HasFlag(wxTB_NOICONS) )
499 {
500 // no icons, don't leave space for them
501 sizeBmp.x =
502 sizeBmp.y = 0;
503 }
504 else // do show icons
505 {
506 // if we already have a bitmap, we'll replace the existing one --
507 // otherwise we'll install a new one
508 HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap;
509
510 sizeBmp.x = m_defaultWidth;
511 sizeBmp.y = m_defaultHeight;
89b892a2 512
24998710
VZ
513 const wxCoord totalBitmapWidth = m_defaultWidth * nTools,
514 totalBitmapHeight = m_defaultHeight;
2bda0e17 515
24998710 516 // Create a bitmap and copy all the tool bitmaps to it
f6bcfd97 517#if USE_BITMAP_MASKS
24998710
VZ
518 wxMemoryDC dcAllButtons;
519 wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight);
520 dcAllButtons.SelectObject(bitmap);
521 dcAllButtons.SetBackground(*wxLIGHT_GREY_BRUSH);
522 dcAllButtons.Clear();
523
524 m_hBitmap = bitmap.GetHBITMAP();
525 HBITMAP hBitmap = (HBITMAP)m_hBitmap;
f6bcfd97 526#else // !USE_BITMAP_MASKS
24998710
VZ
527 HBITMAP hBitmap = ::CreateCompatibleBitmap(ScreenHDC(),
528 totalBitmapWidth,
529 totalBitmapHeight);
530 if ( !hBitmap )
531 {
532 wxLogLastError(_T("CreateCompatibleBitmap"));
8a0681f9 533
24998710
VZ
534 return FALSE;
535 }
8a0681f9 536
24998710 537 m_hBitmap = (WXHBITMAP)hBitmap;
89b892a2 538
24998710
VZ
539 MemoryHDC memoryDC;
540 SelectInHDC hdcSelector(memoryDC, hBitmap);
2bda0e17 541
24998710 542 MemoryHDC memoryDC2;
f6bcfd97 543#endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
2bda0e17 544
c36f1fc7 545 if (wxSystemOptions::HasOption(wxT("msw.remap")) && wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 0)
dfa53609
JS
546 {
547#if USE_BITMAP_MASKS
548 dcAllButtons.SelectObject(wxNullBitmap);
549#endif
550
551 // Even if we're not remapping the bitmap
552 // content, we still have to remap the background.
553 hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
554 totalBitmapWidth, totalBitmapHeight);
555
556#if USE_BITMAP_MASKS
557 dcAllButtons.SelectObject(bitmap);
558#endif
559 }
560
24998710
VZ
561 // the button position
562 wxCoord x = 0;
2bda0e17 563
24998710
VZ
564 // the number of buttons (not separators)
565 int nButtons = 0;
1c383dba 566
4769a562 567 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1c383dba 568 {
24998710
VZ
569 wxToolBarToolBase *tool = node->GetData();
570 if ( tool->IsButton() )
1c383dba 571 {
24998710
VZ
572 const wxBitmap& bmp = tool->GetNormalBitmap();
573 if ( bmp.Ok() )
574 {
dfa53609
JS
575 int xOffset = wxMax(0, (m_defaultWidth - bmp.GetWidth())/2);
576 int yOffset = wxMax(0, (m_defaultHeight - bmp.GetHeight())/2);
f6bcfd97 577#if USE_BITMAP_MASKS
24998710 578 // notice the last parameter: do use mask
dfa53609 579 dcAllButtons.DrawBitmap(bmp, x+xOffset, yOffset, TRUE);
f6bcfd97 580#else // !USE_BITMAP_MASKS
24998710
VZ
581 SelectInHDC hdcSelector2(memoryDC2, GetHbitmapOf(bmp));
582 if ( !BitBlt(memoryDC,
dfa53609 583 x+xOffset, yOffset, m_defaultWidth, m_defaultHeight,
24998710
VZ
584 memoryDC2,
585 0, 0, SRCCOPY) )
586 {
587 wxLogLastError(wxT("BitBlt"));
588 }
589#endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
590 }
591 else
1c383dba 592 {
24998710 593 wxFAIL_MSG( _T("invalid tool button bitmap") );
1c383dba
VZ
594 }
595
24998710
VZ
596 // still inc width and number of buttons because otherwise the
597 // subsequent buttons will all be shifted which is rather confusing
598 // (and like this you'd see immediately which bitmap was bad)
599 x += m_defaultWidth;
600 nButtons++;
1c383dba
VZ
601 }
602 }
8a0681f9 603
f6bcfd97 604#if USE_BITMAP_MASKS
24998710 605 dcAllButtons.SelectObject(wxNullBitmap);
f6bcfd97 606
24998710
VZ
607 // don't delete this HBITMAP!
608 bitmap.SetHBITMAP(0);
f6bcfd97 609#endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
2bda0e17 610
c36f1fc7 611 if (!wxSystemOptions::HasOption(wxT("msw.remap")) || wxSystemOptions::GetOptionInt(wxT("msw.remap")) == 1)
dfa53609
JS
612 {
613 // Map to system colours
614 hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
615 totalBitmapWidth, totalBitmapHeight);
616 }
1c383dba 617
24998710 618 bool addBitmap = TRUE;
9f83044f 619
24998710 620 if ( oldToolBarBitmap )
1c383dba 621 {
24998710
VZ
622#ifdef TB_REPLACEBITMAP
623 if ( wxTheApp->GetComCtl32Version() >= 400 )
9f83044f 624 {
24998710
VZ
625 TBREPLACEBITMAP replaceBitmap;
626 replaceBitmap.hInstOld = NULL;
627 replaceBitmap.hInstNew = NULL;
628 replaceBitmap.nIDOld = (UINT) oldToolBarBitmap;
629 replaceBitmap.nIDNew = (UINT) hBitmap;
630 replaceBitmap.nButtons = nButtons;
631 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP,
632 0, (LPARAM) &replaceBitmap) )
633 {
634 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
635 }
9f83044f 636
24998710 637 ::DeleteObject(oldToolBarBitmap);
9f83044f 638
24998710
VZ
639 // already done
640 addBitmap = FALSE;
641 }
642 else
9f83044f 643#endif // TB_REPLACEBITMAP
24998710
VZ
644 {
645 // we can't replace the old bitmap, so we will add another one
646 // (awfully inefficient, but what else to do?) and shift the bitmap
647 // indices accordingly
648 addBitmap = TRUE;
1c383dba 649
24998710
VZ
650 bitmapId = m_nButtons;
651 }
9f83044f 652 }
1c383dba 653
24998710 654 if ( addBitmap ) // no old bitmap or we can't replace it
1c383dba 655 {
24998710
VZ
656 TBADDBITMAP addBitmap;
657 addBitmap.hInst = 0;
658 addBitmap.nID = (UINT) hBitmap;
659 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP,
660 (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 )
8a0681f9 661 {
24998710 662 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
8a0681f9 663 }
1c383dba 664 }
2bda0e17 665 }
9f83044f 666
24998710
VZ
667 // don't call SetToolBitmapSize() as we don't want to change the values of
668 // m_defaultWidth/Height
669 if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0,
670 MAKELONG(sizeBmp.x, sizeBmp.y)) )
051205e6 671 {
24998710 672 wxLogLastError(_T("TB_SETBITMAPSIZE"));
051205e6 673 }
2bda0e17 674
8a0681f9
VZ
675 // Next add the buttons and separators
676 // -----------------------------------
677
1c383dba 678 TBBUTTON *buttons = new TBBUTTON[nTools];
2bda0e17 679
8a0681f9 680 // this array will hold the indices of all controls in the toolbar
1c383dba
VZ
681 wxArrayInt controlIds;
682
c631abda 683 bool lastWasRadio = FALSE;
1c383dba 684 int i = 0;
4769a562 685 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
2bda0e17 686 {
8a0681f9
VZ
687 wxToolBarToolBase *tool = node->GetData();
688
28603488
VZ
689 // don't add separators to the vertical toolbar with old comctl32.dll
690 // versions as they didn't handle this properly
691 if ( isVertical && tool->IsSeparator() &&
692 wxTheApp->GetComCtl32Version() <= 472 )
693 {
694 continue;
695 }
696
8a0681f9 697
1c383dba
VZ
698 TBBUTTON& button = buttons[i];
699
700 wxZeroMemory(button);
701
c631abda 702 bool isRadio = FALSE;
8a0681f9 703 switch ( tool->GetStyle() )
1c383dba
VZ
704 {
705 case wxTOOL_STYLE_CONTROL:
8a0681f9 706 button.idCommand = tool->GetId();
1c383dba
VZ
707 // fall through: create just a separator too
708
709 case wxTOOL_STYLE_SEPARATOR:
710 button.fsState = TBSTATE_ENABLED;
711 button.fsStyle = TBSTYLE_SEP;
712 break;
713
714 case wxTOOL_STYLE_BUTTON:
24998710
VZ
715 if ( !HasFlag(wxTB_NOICONS) )
716 button.iBitmap = bitmapId;
c631abda 717
24998710 718 if ( HasFlag(wxTB_TEXT) )
c631abda 719 {
24998710
VZ
720 const wxString& label = tool->GetLabel();
721 if ( !label.empty() )
722 {
723 button.iString = (int)label.c_str();
724 }
c631abda
VZ
725 }
726
8a0681f9 727 button.idCommand = tool->GetId();
1c383dba 728
8a0681f9 729 if ( tool->IsEnabled() )
1c383dba 730 button.fsState |= TBSTATE_ENABLED;
8a0681f9 731 if ( tool->IsToggled() )
1c383dba 732 button.fsState |= TBSTATE_CHECKED;
8a0681f9 733
c631abda
VZ
734 switch ( tool->GetKind() )
735 {
736 case wxITEM_RADIO:
737 button.fsStyle = TBSTYLE_CHECKGROUP;
738
739 if ( !lastWasRadio )
740 {
741 // the first item in the radio group is checked by
742 // default to be consistent with wxGTK and the menu
743 // radio items
744 button.fsState |= TBSTATE_CHECKED;
f3ba93c1
VZ
745
746 tool->Toggle(TRUE);
c631abda
VZ
747 }
748
749 isRadio = TRUE;
750 break;
751
752 case wxITEM_CHECK:
753 button.fsStyle = TBSTYLE_CHECK;
754 break;
755
756 default:
757 wxFAIL_MSG( _T("unexpected toolbar button kind") );
758 // fall through
759
760 case wxITEM_NORMAL:
761 button.fsStyle = TBSTYLE_BUTTON;
762 }
1c383dba
VZ
763
764 bitmapId++;
765 break;
766 }
2bda0e17 767
c631abda
VZ
768 lastWasRadio = isRadio;
769
1c383dba 770 i++;
2bda0e17 771 }
1c383dba 772
a3399e6c 773 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, (WPARAM)i, (LPARAM)buttons) )
2bda0e17 774 {
f6bcfd97 775 wxLogLastError(wxT("TB_ADDBUTTONS"));
2bda0e17 776 }
89b892a2 777
1c383dba 778 delete [] buttons;
2bda0e17 779
8a0681f9
VZ
780 // Deal with the controls finally
781 // ------------------------------
782
1c383dba 783 // adjust the controls size to fit nicely in the toolbar
34e5028f 784 int y = 0;
8a0681f9
VZ
785 size_t index = 0;
786 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ )
1c383dba 787 {
8a0681f9 788 wxToolBarToolBase *tool = node->GetData();
1c383dba 789
34e5028f
VZ
790 // we calculate the running y coord for vertical toolbars so we need to
791 // get the items size for all items but for the horizontal ones we
792 // don't need to deal with the non controls
793 bool isControl = tool->IsControl();
794 if ( !isControl && !isVertical )
795 continue;
bdc72a22 796
8a0681f9
VZ
797 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
798 // latter only appeared in v4.70 of comctl32.dll
799 RECT r;
800 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT,
801 index, (LPARAM)(LPRECT)&r) )
802 {
f6bcfd97 803 wxLogLastError(wxT("TB_GETITEMRECT"));
8a0681f9
VZ
804 }
805
34e5028f
VZ
806 if ( !isControl )
807 {
808 // can only be control if isVertical
809 y += r.bottom - r.top;
810
811 continue;
812 }
813
814 wxControl *control = tool->GetControl();
815
816 wxSize size = control->GetSize();
817
818 // the position of the leftmost controls corner
819 int left = -1;
820
bdc72a22 821 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
5b59df83 822#ifdef TB_SETBUTTONINFO
34e5028f
VZ
823 // available in headers, now check whether it is available now
824 // (during run-time)
825 if ( wxTheApp->GetComCtl32Version() >= 471 )
826 {
827 // set the (underlying) separators width to be that of the
828 // control
829 TBBUTTONINFO tbbi;
830 tbbi.cbSize = sizeof(tbbi);
831 tbbi.dwMask = TBIF_SIZE;
832 tbbi.cx = size.x;
833 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO,
834 tool->GetId(), (LPARAM)&tbbi) )
bdc72a22 835 {
34e5028f
VZ
836 // the id is probably invalid?
837 wxLogLastError(wxT("TB_SETBUTTONINFO"));
bdc72a22 838 }
34e5028f
VZ
839 }
840 else
841#endif // comctl32.dll 4.71
842 // TB_SETBUTTONINFO unavailable
843 {
844 // try adding several separators to fit the controls width
845 int widthSep = r.right - r.left;
846 left = r.left;
847
848 TBBUTTON tbb;
849 wxZeroMemory(tbb);
850 tbb.idCommand = 0;
851 tbb.fsState = TBSTATE_ENABLED;
852 tbb.fsStyle = TBSTYLE_SEP;
853
854 size_t nSeparators = size.x / widthSep;
855 for ( size_t nSep = 0; nSep < nSeparators; nSep++ )
bdc72a22 856 {
34e5028f
VZ
857 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON,
858 index, (LPARAM)&tbb) )
bdc72a22 859 {
34e5028f 860 wxLogLastError(wxT("TB_INSERTBUTTON"));
bdc72a22
VZ
861 }
862
34e5028f 863 index++;
bdc72a22 864 }
1c383dba 865
34e5028f
VZ
866 // remember the number of separators we used - we'd have to
867 // delete all of them later
868 ((wxToolBarTool *)tool)->SetSeparatorsCount(nSeparators);
869
870 // adjust the controls width to exactly cover the separators
871 control->SetSize((nSeparators + 1)*widthSep, -1);
872 }
873
874 // position the control itself correctly vertically
1c383dba
VZ
875 int height = r.bottom - r.top;
876 int diff = height - size.y;
877 if ( diff < 0 )
878 {
879 // the control is too high, resize to fit
880 control->SetSize(-1, height - 2);
881
882 diff = 2;
883 }
2bda0e17 884
34e5028f
VZ
885 int top;
886 if ( isVertical )
887 {
888 left = 0;
889 top = y;
890
891 y += height + 2*GetMargins().y;
892 }
893 else // horizontal toolbar
894 {
895 if ( left == -1 )
896 left = r.left;
897
898 top = r.top;
899 }
900
901 control->Move(left, top + (diff + 1) / 2);
1c383dba 902 }
89b892a2 903
8a0681f9
VZ
904 // the max index is the "real" number of buttons - i.e. counting even the
905 // separators which we added just for aligning the controls
906 m_nButtons = index;
89b892a2 907
8a0681f9
VZ
908 if ( !isVertical )
909 {
910 if ( m_maxRows == 0 )
911 {
912 // if not set yet, only one row
913 SetRows(1);
914 }
915 }
916 else if ( m_nButtons > 0 ) // vertical non empty toolbar
917 {
918 if ( m_maxRows == 0 )
919 {
920 // if not set yet, have one column
921 SetRows(m_nButtons);
922 }
923 }
2bda0e17 924
1c383dba 925 return TRUE;
2bda0e17
KB
926}
927
1c383dba
VZ
928// ----------------------------------------------------------------------------
929// message handlers
930// ----------------------------------------------------------------------------
931
33ac7e6f 932bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id)
2bda0e17 933{
8a0681f9
VZ
934 wxToolBarToolBase *tool = FindById((int)id);
935 if ( !tool )
1c383dba
VZ
936 return FALSE;
937
1bba1946 938 bool toggled = false; // just to suppress warnings
6bb7cee4 939
8a0681f9 940 if ( tool->CanBeToggled() )
1c383dba
VZ
941 {
942 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
6bb7cee4
VZ
943 toggled = (state & TBSTATE_CHECKED) != 0;
944
945 // ignore the event when a radio button is released, as this doesn't seem to
946 // happen at all, and is handled otherwise
947 if ( tool->GetKind() == wxITEM_RADIO && !toggled )
948 return TRUE;
1c383dba 949
6bb7cee4
VZ
950 tool->Toggle(toggled);
951 UnToggleRadioGroup(tool);
952 }
8a0681f9 953
6bb7cee4
VZ
954 // OnLeftClick() can veto the button state change - for buttons which
955 // may be toggled only, of couse
956 if ( !OnLeftClick((int)id, toggled) && tool->CanBeToggled() )
f3ba93c1 957 {
6bb7cee4
VZ
958 // revert back
959 tool->Toggle(!toggled);
8a0681f9 960
6bb7cee4 961 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(toggled, 0));
1c383dba
VZ
962 }
963
964 return TRUE;
2bda0e17
KB
965}
966
8a0681f9 967bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl),
fd3f686c 968 WXLPARAM lParam,
33ac7e6f 969 WXLPARAM *WXUNUSED(result))
2bda0e17 970{
3bce6687 971#if wxUSE_TOOLTIPS
89b892a2 972 // First check if this applies to us
2bda0e17 973 NMHDR *hdr = (NMHDR *)lParam;
2bda0e17 974
1c383dba
VZ
975 // the tooltips control created by the toolbar is sometimes Unicode, even
976 // in an ANSI application - this seems to be a bug in comctl32.dll v5
bd9cd534 977 UINT code = hdr->code;
9b601c24 978 if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) )
89b892a2 979 return FALSE;
2bda0e17 980
89b892a2
VZ
981 HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0);
982 if ( toolTipWnd != hdr->hwndFrom )
983 return FALSE;
2bda0e17 984
89b892a2
VZ
985 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
986 int id = (int)ttText->hdr.idFrom;
89b892a2 987
8a0681f9
VZ
988 wxToolBarToolBase *tool = FindById(id);
989 if ( !tool )
990 return FALSE;
2bda0e17 991
bd9cd534 992 return HandleTooltipNotify(code, lParam, tool->GetShortHelp());
3bce6687
JS
993#else
994 return FALSE;
995#endif
2bda0e17
KB
996}
997
1c383dba 998// ----------------------------------------------------------------------------
8a0681f9 999// toolbar geometry
1c383dba
VZ
1000// ----------------------------------------------------------------------------
1001
8a0681f9 1002void wxToolBar::SetToolBitmapSize(const wxSize& size)
2bda0e17 1003{
1c383dba
VZ
1004 wxToolBarBase::SetToolBitmapSize(size);
1005
1006 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
2bda0e17
KB
1007}
1008
8a0681f9 1009void wxToolBar::SetRows(int nRows)
2bda0e17 1010{
8a0681f9
VZ
1011 if ( nRows == m_maxRows )
1012 {
1013 // avoid resizing the frame uselessly
1014 return;
1015 }
1016
1017 // TRUE in wParam means to create at least as many rows, FALSE -
1018 // at most as many
1c383dba
VZ
1019 RECT rect;
1020 ::SendMessage(GetHwnd(), TB_SETROWS,
8a0681f9
VZ
1021 MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)),
1022 (LPARAM) &rect);
1c383dba
VZ
1023
1024 m_maxRows = nRows;
8a0681f9
VZ
1025
1026 UpdateSize();
1027}
1028
1029// The button size is bigger than the bitmap size
1030wxSize wxToolBar::GetToolSize() const
1031{
1032 // TB_GETBUTTONSIZE is supported from version 4.70
5438a566 1033#if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
50165591
CE
1034 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1035 && !defined (__DIGITALMARS__)
8a0681f9
VZ
1036 if ( wxTheApp->GetComCtl32Version() >= 470 )
1037 {
1038 DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0);
1039
1040 return wxSize(LOWORD(dw), HIWORD(dw));
1041 }
1042 else
1043#endif // comctl32.dll 4.70+
1044 {
1045 // defaults
1046 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
1047 }
2bda0e17
KB
1048}
1049
82c9f85c
VZ
1050static
1051wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools,
1052 size_t index )
a8945eef 1053{
222ed1d6 1054 wxToolBarToolsList::compatibility_iterator current = tools.GetFirst();
a8945eef 1055
82c9f85c 1056 for ( ; current != 0; current = current->GetNext() )
a8945eef 1057 {
82c9f85c 1058 if ( index == 0 )
a8945eef 1059 return current->GetData();
82c9f85c
VZ
1060
1061 wxToolBarTool *tool = (wxToolBarTool *)current->GetData();
1062 size_t separators = tool->GetSeparatorsCount();
1063
1064 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1065 // otherwise, skip as many items as the separator count, plus the
1066 // control itself
1067 index -= separators ? separators + 1 : 1;
a8945eef
MB
1068 }
1069
1070 return 0;
1071}
1072
8a0681f9 1073wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
2bda0e17 1074{
8a0681f9
VZ
1075 POINT pt;
1076 pt.x = x;
1077 pt.y = y;
1078 int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt);
37d0bdff
MB
1079 // MBN: when the point ( x, y ) is close to the toolbar border
1080 // TB_HITTEST returns m_nButtons ( not -1 )
1081 if ( index < 0 || (size_t)index >= m_nButtons )
1c383dba 1082 {
8a0681f9
VZ
1083 // it's a separator or there is no tool at all there
1084 return (wxToolBarToolBase *)NULL;
1c383dba
VZ
1085 }
1086
82c9f85c 1087 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
a8945eef
MB
1088#if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1089 if ( wxTheApp->GetComCtl32Version() >= 471 )
1090 {
1091 return m_tools.Item((size_t)index)->GetData();
1092 }
1093 else
82c9f85c 1094#endif
a8945eef
MB
1095 {
1096 return GetItemSkippingDummySpacers( m_tools, (size_t) index );
1097 }
2bda0e17
KB
1098}
1099
8a0681f9 1100void wxToolBar::UpdateSize()
2bda0e17 1101{
0d7ea902
VZ
1102 // the toolbar size changed
1103 SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0);
1104
1105 // we must also refresh the frame after the toolbar size (possibly) changed
8a0681f9
VZ
1106 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
1107 if ( frame )
1108 {
f6bcfd97 1109 frame->SendSizeEvent();
8a0681f9 1110 }
2bda0e17
KB
1111}
1112
c631abda
VZ
1113// ----------------------------------------------------------------------------
1114// toolbar styles
1115// ---------------------------------------------------------------------------
1116
1117void wxToolBar::SetWindowStyleFlag(long style)
1118{
24998710
VZ
1119 // the style bits whose changes force us to recreate the toolbar
1120 static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS;
c631abda 1121
24998710 1122 const long styleOld = GetWindowStyle();
c631abda 1123
24998710 1124 wxToolBarBase::SetWindowStyleFlag(style);
c631abda 1125
24998710
VZ
1126 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1127 // also fatal as we'd then try to recreate the toolbar when it's just being
1128 // created
1129 if ( GetToolsCount() &&
1130 (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) )
1131 {
1132 // to remove the text labels, simply re-realizing the toolbar is enough
1133 // but I don't know of any way to add the text to an existing toolbar
1134 // other than by recreating it entirely
1135 Recreate();
c631abda 1136 }
c631abda
VZ
1137}
1138
1c383dba
VZ
1139// ----------------------------------------------------------------------------
1140// tool state
1141// ----------------------------------------------------------------------------
1142
8a0681f9 1143void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
2bda0e17 1144{
8a0681f9
VZ
1145 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
1146 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0));
2bda0e17
KB
1147}
1148
8a0681f9 1149void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
2bda0e17 1150{
8a0681f9
VZ
1151 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
1152 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0));
2bda0e17
KB
1153}
1154
33ac7e6f 1155void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
088a95f5 1156{
8a0681f9
VZ
1157 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1158 // without, so we really need to delete the button and recreate it here
1159 wxFAIL_MSG( _T("not implemented") );
2bda0e17
KB
1160}
1161
1c383dba
VZ
1162// ----------------------------------------------------------------------------
1163// event handlers
1164// ----------------------------------------------------------------------------
2bda0e17
KB
1165
1166// Responds to colour changes, and passes event on to children.
8a0681f9 1167void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event)
2bda0e17 1168{
84c5b38d 1169 wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE));
2bda0e17
KB
1170
1171 // Remap the buttons
8a0681f9 1172 Realize();
2bda0e17 1173
56bd6aac
VZ
1174 // Relayout the toolbar
1175 int nrows = m_maxRows;
1176 m_maxRows = 0; // otherwise SetRows() wouldn't do anything
1177 SetRows(nrows);
1178
2bda0e17
KB
1179 Refresh();
1180
56bd6aac
VZ
1181 // let the event propagate further
1182 event.Skip();
2bda0e17
KB
1183}
1184
8a0681f9 1185void wxToolBar::OnMouseEvent(wxMouseEvent& event)
e6460682 1186{
fe87983b
MB
1187 if (event.Leaving() && m_pInTool)
1188 {
1189 OnMouseEnter( -1 );
1190 event.Skip();
1191 return;
1192 }
1193
e6460682
JS
1194 if (event.RightDown())
1195 {
1196 // For now, we don't have an id. Later we could
1197 // try finding the tool.
1198 OnRightClick((int)-1, event.GetX(), event.GetY());
1199 }
1200 else
1201 {
42e69d6b 1202 event.Skip();
e6460682
JS
1203 }
1204}
1205
2eb10e2a 1206bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
bdc72a22 1207{
3bb63e5c 1208 // calculate our minor dimension ourselves - we're confusing the standard
7509fa8c
VZ
1209 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1210 RECT r;
1211 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT, 0, (LPARAM)&r) )
bdc72a22 1212 {
7509fa8c
VZ
1213 int w, h;
1214
1215 if ( GetWindowStyle() & wxTB_VERTICAL )
1216 {
1217 w = r.right - r.left;
1218 if ( m_maxRows )
1219 {
1220 w *= (m_nButtons + m_maxRows - 1)/m_maxRows;
1221 }
1222 h = HIWORD(lParam);
1223 }
1224 else
1225 {
1226 w = LOWORD(lParam);
98b96436
RR
1227 if (HasFlag( wxTB_FLAT ))
1228 h = r.bottom - r.top - 3;
1229 else
1230 h = r.bottom - r.top;
7509fa8c
VZ
1231 if ( m_maxRows )
1232 {
28603488
VZ
1233 // FIXME: hardcoded separator line height...
1234 h += HasFlag(wxTB_NODIVIDER) ? 4 : 6;
7509fa8c
VZ
1235 h *= m_maxRows;
1236 }
1237 }
1238
1239 if ( MAKELPARAM(w, h) != lParam )
8a0681f9 1240 {
7509fa8c
VZ
1241 // size really changed
1242 SetSize(w, h);
1243 }
1244
1245 // message processed
1246 return TRUE;
1247 }
1248
1249 return FALSE;
1250}
1251
1252bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam)
1253{
1254 // erase any dummy separators which we used for aligning the controls if
1255 // any here
1256
1257 // first of all, do we have any controls at all?
222ed1d6 1258 wxToolBarToolsList::compatibility_iterator node;
7509fa8c
VZ
1259 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1260 {
1261 if ( node->GetData()->IsControl() )
1262 break;
1263 }
1264
1265 if ( !node )
1266 {
1267 // no controls, nothing to erase
1268 return FALSE;
1269 }
1270
1271 // prepare the DC on which we'll be drawing
1272 wxClientDC dc(this);
1273 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
1274 dc.SetPen(*wxTRANSPARENT_PEN);
1275
1276 RECT r;
1277 if ( !GetUpdateRect(GetHwnd(), &r, FALSE) )
1278 {
1279 // nothing to redraw anyhow
1280 return FALSE;
1281 }
1282
1283 wxRect rectUpdate;
1284 wxCopyRECTToRect(r, rectUpdate);
1285
1286 dc.SetClippingRegion(rectUpdate);
8a0681f9 1287
7509fa8c
VZ
1288 // draw the toolbar tools, separators &c normally
1289 wxControl::MSWWindowProc(WM_PAINT, wParam, lParam);
1290
1291 // for each control in the toolbar find all the separators intersecting it
1292 // and erase them
1293 //
1294 // NB: this is really the only way to do it as we don't know if a separator
1295 // corresponds to a control (i.e. is a dummy one) or a real one
1296 // otherwise
1297 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1298 {
1299 wxToolBarToolBase *tool = node->GetData();
1300 if ( tool->IsControl() )
1301 {
1302 // get the control rect in our client coords
1303 wxControl *control = tool->GetControl();
1304 wxRect rectCtrl = control->GetRect();
7509fa8c
VZ
1305
1306 // iterate over all buttons
1307 TBBUTTON tbb;
1308 int count = ::SendMessage(GetHwnd(), TB_BUTTONCOUNT, 0, 0);
1309 for ( int n = 0; n < count; n++ )
8a0681f9 1310 {
7509fa8c
VZ
1311 // is it a separator?
1312 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON,
1313 n, (LPARAM)&tbb) )
8a0681f9 1314 {
7509fa8c
VZ
1315 wxLogDebug(_T("TB_GETBUTTON failed?"));
1316
1317 continue;
8a0681f9 1318 }
7509fa8c
VZ
1319
1320 if ( tbb.fsStyle != TBSTYLE_SEP )
1321 continue;
1322
1323 // get the bounding rect of the separator
1324 RECT r;
1325 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT,
1326 n, (LPARAM)&r) )
8a0681f9 1327 {
7509fa8c
VZ
1328 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1329
1330 continue;
8a0681f9 1331 }
8a0681f9 1332
7509fa8c
VZ
1333 // does it intersect the control?
1334 wxRect rectItem;
1335 wxCopyRECTToRect(r, rectItem);
1336 if ( rectCtrl.Intersects(rectItem) )
1337 {
1338 // yes, do erase it!
1339 dc.DrawRectangle(rectItem);
229de929
JS
1340
1341 // Necessary in case we use a no-paint-on-size
1342 // style in the parent: the controls can disappear
1343 control->Refresh(FALSE);
7509fa8c 1344 }
8a0681f9 1345 }
8a0681f9 1346 }
bdc72a22 1347 }
7509fa8c
VZ
1348
1349 return TRUE;
1350}
1351
2eb10e2a 1352void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
7509fa8c
VZ
1353{
1354 wxCoord x = GET_X_LPARAM(lParam),
1355 y = GET_Y_LPARAM(lParam);
1356 wxToolBarToolBase* tool = FindToolForPosition( x, y );
1357
1358 // cursor left current tool
1359 if( tool != m_pInTool && !tool )
a8945eef 1360 {
7509fa8c
VZ
1361 m_pInTool = 0;
1362 OnMouseEnter( -1 );
1363 }
a8945eef 1364
7509fa8c
VZ
1365 // cursor entered a tool
1366 if( tool != m_pInTool && tool )
1367 {
1368 m_pInTool = tool;
1369 OnMouseEnter( tool->GetId() );
1370 }
1371}
a8945eef 1372
c140b7e7 1373WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
7509fa8c
VZ
1374{
1375 switch ( nMsg )
1376 {
1377 case WM_SIZE:
1378 if ( HandleSize(wParam, lParam) )
1379 return 0;
1380 break;
1381
1382 case WM_MOUSEMOVE:
1383 // we don't handle mouse moves, so always pass the message to
1384 // wxControl::MSWWindowProc
1385 HandleMouseMove(wParam, lParam);
1386 break;
a8945eef 1387
7509fa8c
VZ
1388 case WM_PAINT:
1389 if ( HandlePaint(wParam, lParam) )
1390 return 0;
a8945eef 1391 }
bdc72a22 1392
8a0681f9 1393 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
bdc72a22
VZ
1394}
1395
1c383dba
VZ
1396// ----------------------------------------------------------------------------
1397// private functions
1398// ----------------------------------------------------------------------------
1399
566fb299 1400WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height)
2bda0e17 1401{
566fb299 1402 MemoryHDC hdcMem;
56bd6aac 1403
566fb299 1404 if ( !hdcMem )
5e68f8f3 1405 {
566fb299
VZ
1406 wxLogLastError(_T("CreateCompatibleDC"));
1407
1408 return bitmap;
5e68f8f3 1409 }
56bd6aac 1410
566fb299 1411 SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap);
56bd6aac 1412
566fb299 1413 if ( !bmpInHDC )
5e68f8f3 1414 {
566fb299
VZ
1415 wxLogLastError(_T("SelectObject"));
1416
1417 return bitmap;
1418 }
56bd6aac 1419
90c1530a
VZ
1420 wxCOLORMAP *cmap = wxGetStdColourMap();
1421
684c68fd
VZ
1422 for ( int i = 0; i < width; i++ )
1423 {
1424 for ( int j = 0; j < height; j++ )
1425 {
1426 COLORREF pixel = ::GetPixel(hdcMem, i, j);
1427
90c1530a 1428 for ( size_t k = 0; k < wxSTD_COL_MAX; k++ )
684c68fd 1429 {
90c1530a 1430 COLORREF col = cmap[k].from;
684c68fd
VZ
1431 if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 &&
1432 abs(GetGValue(pixel) - GetGValue(col)) < 10 &&
1433 abs(GetBValue(pixel) - GetBValue(col)) < 10 )
1434 {
90c1530a 1435 ::SetPixel(hdcMem, i, j, cmap[k].to);
684c68fd
VZ
1436 break;
1437 }
1438 }
1439 }
1440 }
3a3898f8
VZ
1441
1442 return bitmap;
1443
566fb299
VZ
1444 // VZ: I leave here my attempts to map the bitmap to the system colours
1445 // faster by using BitBlt() even though it's broken currently - but
1446 // maybe someone else can finish it? It should be faster than iterating
1447 // over all pixels...
3a3898f8 1448#if 0
566fb299
VZ
1449 MemoryHDC hdcMask, hdcDst;
1450 if ( !hdcMask || !hdcDst )
1451 {
1452 wxLogLastError(_T("CreateCompatibleDC"));
56bd6aac 1453
566fb299 1454 return bitmap;
2bda0e17 1455 }
2bda0e17 1456
566fb299
VZ
1457 // create the target bitmap
1458 HBITMAP hbmpDst = ::CreateCompatibleBitmap(hdcDst, width, height);
1459 if ( !hbmpDst )
1460 {
1461 wxLogLastError(_T("CreateCompatibleBitmap"));
1462
1463 return bitmap;
1464 }
1465
1466 // create the monochrome mask bitmap
1467 HBITMAP hbmpMask = ::CreateBitmap(width, height, 1, 1, 0);
1468 if ( !hbmpMask )
1469 {
1470 wxLogLastError(_T("CreateBitmap(mono)"));
1471
1472 ::DeleteObject(hbmpDst);
1473
1474 return bitmap;
1475 }
1476
1477 SelectInHDC bmpInDst(hdcDst, hbmpDst),
1478 bmpInMask(hdcMask, hbmpMask);
1479
1480 // for each colour:
1481 for ( n = 0; n < NUM_OF_MAPPED_COLOURS; n++ )
1482 {
1483 // create the mask for this colour
1484 ::SetBkColor(hdcMem, ColorMap[n].from);
1485 ::BitBlt(hdcMask, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY);
1486
1487 // replace this colour with the target one in the dst bitmap
1488 HBRUSH hbr = ::CreateSolidBrush(ColorMap[n].to);
1489 HGDIOBJ hbrOld = ::SelectObject(hdcDst, hbr);
2bda0e17 1490
566fb299
VZ
1491 ::MaskBlt(hdcDst, 0, 0, width, height,
1492 hdcMem, 0, 0,
1493 hbmpMask, 0, 0,
1494 MAKEROP4(PATCOPY, SRCCOPY));
1495
1496 (void)::SelectObject(hdcDst, hbrOld);
1497 ::DeleteObject(hbr);
1498 }
1499
1500 ::DeleteObject((HBITMAP)bitmap);
1501
1502 return (WXHBITMAP)hbmpDst;
3a3898f8 1503#endif // 0
566fb299 1504}
2bda0e17 1505
8a0681f9 1506#endif // wxUSE_TOOLBAR && Win95
33ac7e6f 1507