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