]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tbar95.cpp
WinCE fixes (in case a WinCE-friendly backend gets written one day)
[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();
c6dcd973 1052 SetBestFittingSize();
2091f5e7 1053 UpdateSize();
c6dcd973 1054
bfbb0b4c 1055 return true;
2bda0e17
KB
1056}
1057
1c383dba
VZ
1058// ----------------------------------------------------------------------------
1059// message handlers
1060// ----------------------------------------------------------------------------
1061
33ac7e6f 1062bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id)
2bda0e17 1063{
8a0681f9
VZ
1064 wxToolBarToolBase *tool = FindById((int)id);
1065 if ( !tool )
bfbb0b4c 1066 return false;
1c383dba 1067
1bba1946 1068 bool toggled = false; // just to suppress warnings
6bb7cee4 1069
8a0681f9 1070 if ( tool->CanBeToggled() )
1c383dba
VZ
1071 {
1072 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
6bb7cee4
VZ
1073 toggled = (state & TBSTATE_CHECKED) != 0;
1074
0356c259
VZ
1075 // ignore the event when a radio button is released, as this doesn't
1076 // seem to happen at all, and is handled otherwise
6bb7cee4 1077 if ( tool->GetKind() == wxITEM_RADIO && !toggled )
bfbb0b4c 1078 return true;
1c383dba 1079
6bb7cee4
VZ
1080 tool->Toggle(toggled);
1081 UnToggleRadioGroup(tool);
1082 }
8a0681f9 1083
6bb7cee4
VZ
1084 // OnLeftClick() can veto the button state change - for buttons which
1085 // may be toggled only, of couse
1086 if ( !OnLeftClick((int)id, toggled) && tool->CanBeToggled() )
f3ba93c1 1087 {
6bb7cee4
VZ
1088 // revert back
1089 tool->Toggle(!toggled);
8a0681f9 1090
0356c259 1091 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(!toggled, 0));
1c383dba
VZ
1092 }
1093
bfbb0b4c 1094 return true;
2bda0e17
KB
1095}
1096
8a0681f9 1097bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl),
fd3f686c 1098 WXLPARAM lParam,
33ac7e6f 1099 WXLPARAM *WXUNUSED(result))
2bda0e17 1100{
3bce6687 1101#if wxUSE_TOOLTIPS
89b892a2 1102 // First check if this applies to us
2bda0e17 1103 NMHDR *hdr = (NMHDR *)lParam;
2bda0e17 1104
1c383dba
VZ
1105 // the tooltips control created by the toolbar is sometimes Unicode, even
1106 // in an ANSI application - this seems to be a bug in comctl32.dll v5
bd9cd534 1107 UINT code = hdr->code;
9b601c24 1108 if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) )
bfbb0b4c 1109 return false;
2bda0e17 1110
89b892a2
VZ
1111 HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0);
1112 if ( toolTipWnd != hdr->hwndFrom )
bfbb0b4c 1113 return false;
2bda0e17 1114
89b892a2
VZ
1115 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
1116 int id = (int)ttText->hdr.idFrom;
89b892a2 1117
8a0681f9
VZ
1118 wxToolBarToolBase *tool = FindById(id);
1119 if ( !tool )
bfbb0b4c 1120 return false;
2bda0e17 1121
bd9cd534 1122 return HandleTooltipNotify(code, lParam, tool->GetShortHelp());
3bce6687 1123#else
abf912c5
VZ
1124 wxUnusedVar(lParam);
1125
bfbb0b4c 1126 return false;
3bce6687 1127#endif
2bda0e17
KB
1128}
1129
1c383dba 1130// ----------------------------------------------------------------------------
8a0681f9 1131// toolbar geometry
1c383dba
VZ
1132// ----------------------------------------------------------------------------
1133
8a0681f9 1134void wxToolBar::SetToolBitmapSize(const wxSize& size)
2bda0e17 1135{
1c383dba
VZ
1136 wxToolBarBase::SetToolBitmapSize(size);
1137
1138 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
2bda0e17
KB
1139}
1140
8a0681f9 1141void wxToolBar::SetRows(int nRows)
2bda0e17 1142{
8a0681f9
VZ
1143 if ( nRows == m_maxRows )
1144 {
1145 // avoid resizing the frame uselessly
1146 return;
1147 }
1148
1149 // TRUE in wParam means to create at least as many rows, FALSE -
1150 // at most as many
1c383dba
VZ
1151 RECT rect;
1152 ::SendMessage(GetHwnd(), TB_SETROWS,
8a0681f9
VZ
1153 MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)),
1154 (LPARAM) &rect);
1c383dba
VZ
1155
1156 m_maxRows = nRows;
8a0681f9
VZ
1157
1158 UpdateSize();
1159}
1160
1161// The button size is bigger than the bitmap size
1162wxSize wxToolBar::GetToolSize() const
1163{
1164 // TB_GETBUTTONSIZE is supported from version 4.70
5438a566 1165#if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
50165591
CE
1166 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1167 && !defined (__DIGITALMARS__)
2a1f999f 1168 if ( wxApp::GetComCtl32Version() >= 470 )
8a0681f9
VZ
1169 {
1170 DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0);
1171
1172 return wxSize(LOWORD(dw), HIWORD(dw));
1173 }
1174 else
1175#endif // comctl32.dll 4.70+
1176 {
1177 // defaults
1178 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
1179 }
2bda0e17
KB
1180}
1181
82c9f85c
VZ
1182static
1183wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools,
1184 size_t index )
a8945eef 1185{
222ed1d6 1186 wxToolBarToolsList::compatibility_iterator current = tools.GetFirst();
a8945eef 1187
489f6cf7 1188 for ( ; current ; current = current->GetNext() )
a8945eef 1189 {
82c9f85c 1190 if ( index == 0 )
a8945eef 1191 return current->GetData();
82c9f85c
VZ
1192
1193 wxToolBarTool *tool = (wxToolBarTool *)current->GetData();
1194 size_t separators = tool->GetSeparatorsCount();
1195
1196 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1197 // otherwise, skip as many items as the separator count, plus the
1198 // control itself
1199 index -= separators ? separators + 1 : 1;
a8945eef
MB
1200 }
1201
1202 return 0;
1203}
1204
8a0681f9 1205wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
2bda0e17 1206{
8a0681f9
VZ
1207 POINT pt;
1208 pt.x = x;
1209 pt.y = y;
1210 int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt);
97071cdb 1211
37d0bdff
MB
1212 // MBN: when the point ( x, y ) is close to the toolbar border
1213 // TB_HITTEST returns m_nButtons ( not -1 )
1214 if ( index < 0 || (size_t)index >= m_nButtons )
8a0681f9
VZ
1215 // it's a separator or there is no tool at all there
1216 return (wxToolBarToolBase *)NULL;
1c383dba 1217
a84f66ef
VZ
1218 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1219 // we don't use the dummy separators hack
1220#ifdef TB_SETBUTTONINFO
2a1f999f 1221 if ( wxApp::GetComCtl32Version() >= 471 )
a8945eef
MB
1222 {
1223 return m_tools.Item((size_t)index)->GetData();
1224 }
1225 else
a84f66ef 1226#endif // TB_SETBUTTONINFO
a8945eef
MB
1227 {
1228 return GetItemSkippingDummySpacers( m_tools, (size_t) index );
1229 }
2bda0e17
KB
1230}
1231
8a0681f9 1232void wxToolBar::UpdateSize()
2bda0e17 1233{
b93328d8
RD
1234 // In case Realize is called after the initial display (IOW the programmer
1235 // may have rebuilt the toolbar) give the frame the option of resizing the
1236 // toolbar to full width again, but only if the parent is a frame and the
1237 // toolbar is managed by the frame. Otherwise assume that some other
1238 // layout mechanism is controlling the toolbar size and leave it alone.
0d7ea902 1239
8a0681f9 1240 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
b93328d8
RD
1241 if ( frame && frame->GetToolBar() == this )
1242 {
1243 // the toolbar size changed
1244 ::SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0);
f6bcfd97 1245 frame->SendSizeEvent();
b93328d8 1246 }
2bda0e17
KB
1247}
1248
c631abda
VZ
1249// ----------------------------------------------------------------------------
1250// toolbar styles
1251// ---------------------------------------------------------------------------
1252
1253void wxToolBar::SetWindowStyleFlag(long style)
1254{
24998710
VZ
1255 // the style bits whose changes force us to recreate the toolbar
1256 static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS;
c631abda 1257
24998710 1258 const long styleOld = GetWindowStyle();
c631abda 1259
24998710 1260 wxToolBarBase::SetWindowStyleFlag(style);
c631abda 1261
24998710
VZ
1262 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1263 // also fatal as we'd then try to recreate the toolbar when it's just being
1264 // created
1265 if ( GetToolsCount() &&
1266 (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) )
1267 {
1268 // to remove the text labels, simply re-realizing the toolbar is enough
1269 // but I don't know of any way to add the text to an existing toolbar
1270 // other than by recreating it entirely
1271 Recreate();
c631abda 1272 }
c631abda
VZ
1273}
1274
1c383dba
VZ
1275// ----------------------------------------------------------------------------
1276// tool state
1277// ----------------------------------------------------------------------------
1278
8a0681f9 1279void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
2bda0e17 1280{
8a0681f9
VZ
1281 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
1282 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0));
2bda0e17
KB
1283}
1284
8a0681f9 1285void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
2bda0e17 1286{
8a0681f9
VZ
1287 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
1288 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0));
2bda0e17
KB
1289}
1290
33ac7e6f 1291void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
088a95f5 1292{
8a0681f9
VZ
1293 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1294 // without, so we really need to delete the button and recreate it here
1295 wxFAIL_MSG( _T("not implemented") );
2bda0e17
KB
1296}
1297
1c383dba
VZ
1298// ----------------------------------------------------------------------------
1299// event handlers
1300// ----------------------------------------------------------------------------
2bda0e17
KB
1301
1302// Responds to colour changes, and passes event on to children.
8a0681f9 1303void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event)
2bda0e17 1304{
84c5b38d 1305 wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE));
2bda0e17
KB
1306
1307 // Remap the buttons
8a0681f9 1308 Realize();
2bda0e17 1309
56bd6aac
VZ
1310 // Relayout the toolbar
1311 int nrows = m_maxRows;
1312 m_maxRows = 0; // otherwise SetRows() wouldn't do anything
1313 SetRows(nrows);
1314
2bda0e17
KB
1315 Refresh();
1316
56bd6aac
VZ
1317 // let the event propagate further
1318 event.Skip();
2bda0e17
KB
1319}
1320
8a0681f9 1321void wxToolBar::OnMouseEvent(wxMouseEvent& event)
e6460682 1322{
fe87983b
MB
1323 if (event.Leaving() && m_pInTool)
1324 {
1325 OnMouseEnter( -1 );
1326 event.Skip();
1327 return;
1328 }
1329
2334fef6 1330 if ( event.RightDown() )
e6460682 1331 {
2334fef6
VZ
1332 // find the tool under the mouse
1333 wxCoord x,y;
1334 event.GetPosition(&x, &y);
1335
1336 wxToolBarToolBase *tool = FindToolForPosition(x, y);
1337 OnRightClick(tool ? tool->GetId() : -1, x, y);
e6460682
JS
1338 }
1339 else
1340 {
42e69d6b 1341 event.Skip();
e6460682
JS
1342 }
1343}
1344
0090a153
JS
1345// This handler is required to allow the toolbar to be set to a non-default
1346// colour: for example, when it must blend in with a notebook page.
1347void wxToolBar::OnEraseBackground(wxEraseEvent& event)
1348{
1349 wxColour bgCol = GetBackgroundColour();
1350 if (!bgCol.Ok())
1351 {
1352 event.Skip();
1353 return;
1354 }
489f6cf7 1355
0090a153
JS
1356 // notice that this 'dumb' implementation may cause flicker for some of the
1357 // controls in which case they should intercept wxEraseEvent and process it
1358 // themselves somehow
1359
1360 RECT rect;
1361 ::GetClientRect(GetHwnd(), &rect);
1362
1363 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(bgCol));
1364
1365 HDC hdc = GetHdcOf((*event.GetDC()));
1366
1367#ifndef __WXWINCE__
1368 int mode = ::SetMapMode(hdc, MM_TEXT);
1369#endif
1370
1371 ::FillRect(hdc, &rect, hBrush);
1372 ::DeleteObject(hBrush);
1373
1374#ifndef __WXWINCE__
1375 ::SetMapMode(hdc, mode);
1376#endif
1377}
1378
2eb10e2a 1379bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
bdc72a22 1380{
3bb63e5c 1381 // calculate our minor dimension ourselves - we're confusing the standard
7509fa8c
VZ
1382 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
1383 RECT r;
1384 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT, 0, (LPARAM)&r) )
bdc72a22 1385 {
7509fa8c
VZ
1386 int w, h;
1387
1388 if ( GetWindowStyle() & wxTB_VERTICAL )
1389 {
1390 w = r.right - r.left;
1391 if ( m_maxRows )
1392 {
1393 w *= (m_nButtons + m_maxRows - 1)/m_maxRows;
1394 }
1395 h = HIWORD(lParam);
1396 }
1397 else
1398 {
1399 w = LOWORD(lParam);
98b96436
RR
1400 if (HasFlag( wxTB_FLAT ))
1401 h = r.bottom - r.top - 3;
1402 else
1403 h = r.bottom - r.top;
7509fa8c
VZ
1404 if ( m_maxRows )
1405 {
28603488
VZ
1406 // FIXME: hardcoded separator line height...
1407 h += HasFlag(wxTB_NODIVIDER) ? 4 : 6;
7509fa8c
VZ
1408 h *= m_maxRows;
1409 }
1410 }
1411
1412 if ( MAKELPARAM(w, h) != lParam )
8a0681f9 1413 {
7509fa8c
VZ
1414 // size really changed
1415 SetSize(w, h);
1416 }
1417
1418 // message processed
bfbb0b4c 1419 return true;
7509fa8c
VZ
1420 }
1421
bfbb0b4c 1422 return false;
7509fa8c
VZ
1423}
1424
1425bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam)
1426{
97071cdb
DS
1427 // erase any dummy separators which were used
1428 // for aligning the controls if any here
7509fa8c 1429
97071cdb 1430 // first of all, are there any controls at all?
222ed1d6 1431 wxToolBarToolsList::compatibility_iterator node;
7509fa8c
VZ
1432 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1433 {
1434 if ( node->GetData()->IsControl() )
1435 break;
1436 }
1437
1438 if ( !node )
7509fa8c 1439 // no controls, nothing to erase
bfbb0b4c 1440 return false;
7509fa8c
VZ
1441
1442 // prepare the DC on which we'll be drawing
1443 wxClientDC dc(this);
1444 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
1445 dc.SetPen(*wxTRANSPARENT_PEN);
1446
1447 RECT r;
bfbb0b4c 1448 if ( !::GetUpdateRect(GetHwnd(), &r, FALSE) )
7509fa8c 1449 // nothing to redraw anyhow
bfbb0b4c 1450 return false;
7509fa8c
VZ
1451
1452 wxRect rectUpdate;
1453 wxCopyRECTToRect(r, rectUpdate);
1454
1455 dc.SetClippingRegion(rectUpdate);
8a0681f9 1456
7509fa8c
VZ
1457 // draw the toolbar tools, separators &c normally
1458 wxControl::MSWWindowProc(WM_PAINT, wParam, lParam);
1459
1460 // for each control in the toolbar find all the separators intersecting it
1461 // and erase them
1462 //
1463 // NB: this is really the only way to do it as we don't know if a separator
1464 // corresponds to a control (i.e. is a dummy one) or a real one
1465 // otherwise
1466 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1467 {
1468 wxToolBarToolBase *tool = node->GetData();
1469 if ( tool->IsControl() )
1470 {
1471 // get the control rect in our client coords
1472 wxControl *control = tool->GetControl();
1473 wxRect rectCtrl = control->GetRect();
7509fa8c
VZ
1474
1475 // iterate over all buttons
1476 TBBUTTON tbb;
1477 int count = ::SendMessage(GetHwnd(), TB_BUTTONCOUNT, 0, 0);
1478 for ( int n = 0; n < count; n++ )
8a0681f9 1479 {
7509fa8c
VZ
1480 // is it a separator?
1481 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON,
1482 n, (LPARAM)&tbb) )
8a0681f9 1483 {
7509fa8c
VZ
1484 wxLogDebug(_T("TB_GETBUTTON failed?"));
1485
1486 continue;
8a0681f9 1487 }
7509fa8c
VZ
1488
1489 if ( tbb.fsStyle != TBSTYLE_SEP )
1490 continue;
1491
1492 // get the bounding rect of the separator
1493 RECT r;
1494 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT,
1495 n, (LPARAM)&r) )
8a0681f9 1496 {
7509fa8c
VZ
1497 wxLogDebug(_T("TB_GETITEMRECT failed?"));
1498
1499 continue;
8a0681f9 1500 }
8a0681f9 1501
7509fa8c
VZ
1502 // does it intersect the control?
1503 wxRect rectItem;
1504 wxCopyRECTToRect(r, rectItem);
1505 if ( rectCtrl.Intersects(rectItem) )
1506 {
1507 // yes, do erase it!
1508 dc.DrawRectangle(rectItem);
bfbb0b4c 1509
229de929
JS
1510 // Necessary in case we use a no-paint-on-size
1511 // style in the parent: the controls can disappear
bfbb0b4c 1512 control->Refresh(false);
7509fa8c 1513 }
8a0681f9 1514 }
8a0681f9 1515 }
bdc72a22 1516 }
7509fa8c 1517
bfbb0b4c 1518 return true;
7509fa8c
VZ
1519}
1520
2eb10e2a 1521void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
7509fa8c
VZ
1522{
1523 wxCoord x = GET_X_LPARAM(lParam),
1524 y = GET_Y_LPARAM(lParam);
1525 wxToolBarToolBase* tool = FindToolForPosition( x, y );
1526
1527 // cursor left current tool
97071cdb 1528 if ( tool != m_pInTool && !tool )
a8945eef 1529 {
7509fa8c
VZ
1530 m_pInTool = 0;
1531 OnMouseEnter( -1 );
1532 }
a8945eef 1533
7509fa8c 1534 // cursor entered a tool
97071cdb 1535 if ( tool != m_pInTool && tool )
7509fa8c
VZ
1536 {
1537 m_pInTool = tool;
1538 OnMouseEnter( tool->GetId() );
1539 }
1540}
a8945eef 1541
c140b7e7 1542WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
7509fa8c
VZ
1543{
1544 switch ( nMsg )
1545 {
7509fa8c
VZ
1546 case WM_MOUSEMOVE:
1547 // we don't handle mouse moves, so always pass the message to
4012b958 1548 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
7509fa8c
VZ
1549 HandleMouseMove(wParam, lParam);
1550 break;
a8945eef 1551
4012b958
JS
1552 case WM_SIZE:
1553 if ( HandleSize(wParam, lParam) )
1554 return 0;
1555 break;
1556
1557#ifndef __WXWINCE__
7509fa8c
VZ
1558 case WM_PAINT:
1559 if ( HandlePaint(wParam, lParam) )
1560 return 0;
4012b958 1561#endif
97071cdb
DS
1562
1563 default:
1564 break;
a8945eef 1565 }
bdc72a22 1566
8a0681f9 1567 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
bdc72a22
VZ
1568}
1569
1c383dba
VZ
1570// ----------------------------------------------------------------------------
1571// private functions
1572// ----------------------------------------------------------------------------
1573
566fb299 1574WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height)
2bda0e17 1575{
566fb299 1576 MemoryHDC hdcMem;
56bd6aac 1577
566fb299 1578 if ( !hdcMem )
5e68f8f3 1579 {
566fb299
VZ
1580 wxLogLastError(_T("CreateCompatibleDC"));
1581
1582 return bitmap;
5e68f8f3 1583 }
56bd6aac 1584
566fb299 1585 SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap);
56bd6aac 1586
566fb299 1587 if ( !bmpInHDC )
5e68f8f3 1588 {
566fb299
VZ
1589 wxLogLastError(_T("SelectObject"));
1590
1591 return bitmap;
1592 }
56bd6aac 1593
90c1530a
VZ
1594 wxCOLORMAP *cmap = wxGetStdColourMap();
1595
684c68fd
VZ
1596 for ( int i = 0; i < width; i++ )
1597 {
1598 for ( int j = 0; j < height; j++ )
1599 {
1600 COLORREF pixel = ::GetPixel(hdcMem, i, j);
1601
90c1530a 1602 for ( size_t k = 0; k < wxSTD_COL_MAX; k++ )
684c68fd 1603 {
90c1530a 1604 COLORREF col = cmap[k].from;
684c68fd
VZ
1605 if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 &&
1606 abs(GetGValue(pixel) - GetGValue(col)) < 10 &&
1607 abs(GetBValue(pixel) - GetBValue(col)) < 10 )
1608 {
90c1530a 1609 ::SetPixel(hdcMem, i, j, cmap[k].to);
684c68fd
VZ
1610 break;
1611 }
1612 }
1613 }
1614 }
3a3898f8
VZ
1615
1616 return bitmap;
1617
566fb299
VZ
1618 // VZ: I leave here my attempts to map the bitmap to the system colours
1619 // faster by using BitBlt() even though it's broken currently - but
1620 // maybe someone else can finish it? It should be faster than iterating
1621 // over all pixels...
3a3898f8 1622#if 0
566fb299
VZ
1623 MemoryHDC hdcMask, hdcDst;
1624 if ( !hdcMask || !hdcDst )
1625 {
1626 wxLogLastError(_T("CreateCompatibleDC"));
56bd6aac 1627
566fb299 1628 return bitmap;
2bda0e17 1629 }
2bda0e17 1630
566fb299
VZ
1631 // create the target bitmap
1632 HBITMAP hbmpDst = ::CreateCompatibleBitmap(hdcDst, width, height);
1633 if ( !hbmpDst )
1634 {
1635 wxLogLastError(_T("CreateCompatibleBitmap"));
1636
1637 return bitmap;
1638 }
1639
1640 // create the monochrome mask bitmap
1641 HBITMAP hbmpMask = ::CreateBitmap(width, height, 1, 1, 0);
1642 if ( !hbmpMask )
1643 {
1644 wxLogLastError(_T("CreateBitmap(mono)"));
1645
1646 ::DeleteObject(hbmpDst);
1647
1648 return bitmap;
1649 }
1650
1651 SelectInHDC bmpInDst(hdcDst, hbmpDst),
1652 bmpInMask(hdcMask, hbmpMask);
1653
1654 // for each colour:
1655 for ( n = 0; n < NUM_OF_MAPPED_COLOURS; n++ )
1656 {
1657 // create the mask for this colour
1658 ::SetBkColor(hdcMem, ColorMap[n].from);
1659 ::BitBlt(hdcMask, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY);
1660
1661 // replace this colour with the target one in the dst bitmap
1662 HBRUSH hbr = ::CreateSolidBrush(ColorMap[n].to);
1663 HGDIOBJ hbrOld = ::SelectObject(hdcDst, hbr);
2bda0e17 1664
566fb299
VZ
1665 ::MaskBlt(hdcDst, 0, 0, width, height,
1666 hdcMem, 0, 0,
1667 hbmpMask, 0, 0,
1668 MAKEROP4(PATCOPY, SRCCOPY));
1669
1670 (void)::SelectObject(hdcDst, hbrOld);
1671 ::DeleteObject(hbr);
1672 }
1673
1674 ::DeleteObject((HBITMAP)bitmap);
1675
1676 return (WXHBITMAP)hbmpDst;
3a3898f8 1677#endif // 0
566fb299 1678}
2bda0e17 1679
97071cdb 1680#endif // wxUSE_TOOLBAR
33ac7e6f 1681