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