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