]> git.saurik.com Git - wxWidgets.git/blame - src/msw/toolbar.cpp
Fix best size computation for buttons without image and with empty label.
[wxWidgets.git] / src / msw / toolbar.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
936f6353 2// Name: src/msw/toolbar.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
ad9835c9
WS
27#if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && !defined(__SMARTPHONE__)
28
4e3e485b
WS
29#include "wx/toolbar.h"
30
2bda0e17 31#ifndef WX_PRECOMP
57bd4c60 32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
ad9835c9 33 #include "wx/dynarray.h"
8a0681f9 34 #include "wx/frame.h"
1c383dba
VZ
35 #include "wx/log.h"
36 #include "wx/intl.h"
4e15f6c5 37 #include "wx/settings.h"
381dd4bf 38 #include "wx/bitmap.h"
f6bcfd97 39 #include "wx/dcmemory.h"
f538710f 40 #include "wx/control.h"
670f9935 41 #include "wx/app.h" // for GetComCtl32Version
155ecd4c 42 #include "wx/image.h"
0f767c96 43 #include "wx/stattext.h"
2bda0e17
KB
44#endif
45
2d8b540a 46#include "wx/artprov.h"
dfa53609 47#include "wx/sysopt.h"
888dde65 48#include "wx/dcclient.h"
664e1314 49#include "wx/scopedarray.h"
2bda0e17 50
1c383dba 51#include "wx/msw/private.h"
025f7d77 52#include "wx/msw/dc.h"
2bda0e17 53
d679df70
JS
54#if wxUSE_UXTHEME
55#include "wx/msw/uxtheme.h"
56#endif
57
fb7e87ef
VZ
58// this define controls whether the code for button colours remapping (only
59// useful for 16 or 256 colour images) is active at all, it's always turned off
60// for CE where it doesn't compile (and is probably not needed anyhow) and may
61// also be turned off for other systems if you always use 24bpp images and so
62// never need it
63#ifndef __WXWINCE__
64 #define wxREMAP_BUTTON_COLOURS
65#endif // !__WXWINCE__
66
1c383dba
VZ
67// ----------------------------------------------------------------------------
68// constants
69// ----------------------------------------------------------------------------
70
71// these standard constants are not always defined in compilers headers
2bda0e17 72
6a23cbce 73// Styles
bb6290e3 74#ifndef TBSTYLE_FLAT
1c383dba
VZ
75 #define TBSTYLE_LIST 0x1000
76 #define TBSTYLE_FLAT 0x0800
dd177170
RL
77#endif
78
79#ifndef TBSTYLE_TRANSPARENT
1c383dba 80 #define TBSTYLE_TRANSPARENT 0x8000
bb6290e3 81#endif
bb6290e3 82
a3399e6c
VZ
83#ifndef TBSTYLE_TOOLTIPS
84 #define TBSTYLE_TOOLTIPS 0x0100
85#endif
86
6a23cbce
JS
87// Messages
88#ifndef TB_GETSTYLE
1c383dba 89 #define TB_SETSTYLE (WM_USER + 56)
8a0681f9
VZ
90 #define TB_GETSTYLE (WM_USER + 57)
91#endif
92
93#ifndef TB_HITTEST
94 #define TB_HITTEST (WM_USER + 69)
6a23cbce
JS
95#endif
96
308bb56f
VZ
97#ifndef TB_GETMAXSIZE
98 #define TB_GETMAXSIZE (WM_USER + 83)
99#endif
100
1c383dba
VZ
101// ----------------------------------------------------------------------------
102// wxWin macros
103// ----------------------------------------------------------------------------
104
2eb10e2a 105IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
2bda0e17 106
066f1b7a 107/*
bfbb0b4c
WS
108 TOOLBAR PROPERTIES
109 tool
110 bitmap
111 bitmap2
112 tooltip
113 longhelp
114 radio (bool)
115 toggle (bool)
116 separator
117 style ( wxNO_BORDER | wxTB_HORIZONTAL)
118 bitmapsize
119 margins
120 packing
121 separation
122
123 dontattachtoframe
066f1b7a
SC
124*/
125
8a0681f9
VZ
126BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
127 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent)
128 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged)
2bda0e17 129END_EVENT_TABLE()
2bda0e17 130
8a0681f9
VZ
131// ----------------------------------------------------------------------------
132// private classes
133// ----------------------------------------------------------------------------
134
135class wxToolBarTool : public wxToolBarToolBase
136{
137public:
138 wxToolBarTool(wxToolBar *tbar,
139 int id,
a3399e6c
VZ
140 const wxString& label,
141 const wxBitmap& bmpNormal,
142 const wxBitmap& bmpDisabled,
143 wxItemKind kind,
8a0681f9 144 wxObject *clientData,
a3399e6c
VZ
145 const wxString& shortHelp,
146 const wxString& longHelp)
147 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
148 clientData, shortHelp, longHelp)
8a0681f9
VZ
149 {
150 m_nSepCount = 0;
cc260109 151 m_staticText = NULL;
8a0681f9
VZ
152 }
153
cdb11cb9
VZ
154 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
155 : wxToolBarToolBase(tbar, control, label)
8a0681f9 156 {
cdb11cb9
VZ
157 if ( IsControl() && !m_label.empty() )
158 {
159 // create a control to render the control's label
160 m_staticText = new wxStaticText
161 (
162 m_tbar,
163 wxID_ANY,
164 m_label,
165 wxDefaultPosition,
166 wxDefaultSize,
167 wxALIGN_CENTRE | wxST_NO_AUTORESIZE
168 );
169 }
170 else // no label
171 {
172 m_staticText = NULL;
173 }
174
8a0681f9
VZ
175 m_nSepCount = 1;
176 }
177
cdb11cb9
VZ
178 virtual ~wxToolBarTool()
179 {
180 delete m_staticText;
181 }
182
c631abda
VZ
183 virtual void SetLabel(const wxString& label)
184 {
185 if ( label == m_label )
186 return;
187
188 wxToolBarToolBase::SetLabel(label);
189
cdb11cb9
VZ
190 if ( m_staticText )
191 m_staticText->SetLabel(label);
192
c631abda
VZ
193 // we need to update the label shown in the toolbar because it has a
194 // pointer to the internal buffer of the old label
195 //
196 // TODO: use TB_SETBUTTONINFO
197 }
198
cdb11cb9
VZ
199 wxStaticText* GetStaticText()
200 {
201 wxASSERT_MSG( IsControl(),
9a83f860 202 wxT("only makes sense for embedded control tools") );
cdb11cb9
VZ
203
204 return m_staticText;
205 }
206
8a0681f9
VZ
207 // set/get the number of separators which we use to cover the space used by
208 // a control in the toolbar
209 void SetSeparatorsCount(size_t count) { m_nSepCount = count; }
210 size_t GetSeparatorsCount() const { return m_nSepCount; }
211
cc260109
VZ
212 // we need ids for the spacers which we want to modify later on, this
213 // function will allocate a valid/unique id for a spacer if not done yet
214 void AllocSpacerId()
215 {
216 if ( m_id == wxID_SEPARATOR )
217 m_id = wxWindow::NewControlId();
218 }
219
220 // this method is used for controls only and offsets the control by the
221 // given amount (in pixels) in horizontal direction
222 void MoveBy(int offset)
223 {
224 wxControl * const control = GetControl();
225
226 control->Move(control->GetPosition().x + offset, wxDefaultCoord);
227
228 if ( m_staticText )
229 {
230 m_staticText->Move(m_staticText->GetPosition().x + offset,
231 wxDefaultCoord);
232 }
233 }
234
8a0681f9
VZ
235private:
236 size_t m_nSepCount;
cdb11cb9 237 wxStaticText *m_staticText;
2eb10e2a 238
c0c133e1 239 wxDECLARE_NO_COPY_CLASS(wxToolBarTool);
8a0681f9
VZ
240};
241
3c30bbb9
VZ
242// ----------------------------------------------------------------------------
243// helper functions
244// ----------------------------------------------------------------------------
245
246// return the rectangle of the item at the given index
247//
248// returns an empty (0, 0, 0, 0) rectangle if fails so the caller may compare
249// r.right or r.bottom with 0 to check for this
250static RECT wxGetTBItemRect(HWND hwnd, int index)
251{
252 RECT r;
253
254 // note that we use TB_GETITEMRECT and not TB_GETRECT because the latter
255 // only appeared in v4.70 of comctl32.dll
256 if ( !::SendMessage(hwnd, TB_GETITEMRECT, index, (LPARAM)&r) )
257 {
258 wxLogLastError(wxT("TB_GETITEMRECT"));
259
260 r.top =
261 r.left =
262 r.right =
263 r.bottom = 0;
264 }
265
266 return r;
267}
268
1c383dba
VZ
269// ============================================================================
270// implementation
271// ============================================================================
2bda0e17 272
1c383dba 273// ----------------------------------------------------------------------------
8a0681f9 274// wxToolBarTool
1c383dba
VZ
275// ----------------------------------------------------------------------------
276
8a0681f9 277wxToolBarToolBase *wxToolBar::CreateTool(int id,
a3399e6c
VZ
278 const wxString& label,
279 const wxBitmap& bmpNormal,
280 const wxBitmap& bmpDisabled,
281 wxItemKind kind,
8a0681f9 282 wxObject *clientData,
a3399e6c
VZ
283 const wxString& shortHelp,
284 const wxString& longHelp)
8a0681f9 285{
a3399e6c
VZ
286 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
287 clientData, shortHelp, longHelp);
8a0681f9
VZ
288}
289
cdb11cb9
VZ
290wxToolBarToolBase *
291wxToolBar::CreateTool(wxControl *control, const wxString& label)
8a0681f9 292{
cdb11cb9 293 return new wxToolBarTool(this, control, label);
8a0681f9
VZ
294}
295
296// ----------------------------------------------------------------------------
297// wxToolBar construction
298// ----------------------------------------------------------------------------
299
300void wxToolBar::Init()
2bda0e17 301{
1c383dba 302 m_hBitmap = 0;
8d0a7b56 303 m_disabledImgList = NULL;
8a0681f9
VZ
304
305 m_nButtons = 0;
cc260109 306 m_totalFixedSize = 0;
8a0681f9 307
040d3c2e
VZ
308 // even though modern Windows applications typically use 24*24 (or even
309 // 32*32) size for their bitmaps, the native control itself still uses the
310 // old 16*15 default size (see TB_SETBITMAPSIZE documentation in MSDN), so
311 // default to it so that we don't call SetToolBitmapSize() unnecessarily in
bb2212e6 312 // wxToolBarBase::AdjustToolBitmapSize()
040d3c2e
VZ
313 m_defaultWidth = 16;
314 m_defaultHeight = 15;
37d0bdff 315
6ae2a4b7 316 m_pInTool = NULL;
2bda0e17
KB
317}
318
8a0681f9
VZ
319bool wxToolBar::Create(wxWindow *parent,
320 wxWindowID id,
321 const wxPoint& pos,
322 const wxSize& size,
323 long style,
324 const wxString& name)
2bda0e17 325{
1c383dba 326 // common initialisation
11b6a93b 327 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
bfbb0b4c 328 return false;
89b892a2 329
d408730c
VZ
330 FixupStyle();
331
24998710 332 // MSW-specific initialisation
278c927d 333 if ( !MSWCreateToolbar(pos, size) )
bfbb0b4c 334 return false;
b0766406 335
9a63feff
VZ
336 wxSetCCUnicodeFormat(GetHwnd());
337
f6becba5 338 // workaround for flat toolbar on Windows XP classic style: we have to set
a8cb1a49 339 // the style after creating the control; doing it at creation time doesn't work
d679df70 340#if wxUSE_UXTHEME
6d473712
JS
341 if ( style & wxTB_FLAT )
342 {
2588b9c4 343 LRESULT style = GetMSWToolbarStyle();
bfbb0b4c 344
f6becba5 345 if ( !(style & TBSTYLE_FLAT) )
f6becba5 346 ::SendMessage(GetHwnd(), TB_SETSTYLE, 0, style | TBSTYLE_FLAT);
6d473712 347 }
f6becba5 348#endif // wxUSE_UXTHEME
bfbb0b4c
WS
349
350 return true;
24998710 351}
2bda0e17 352
278c927d 353bool wxToolBar::MSWCreateToolbar(const wxPoint& pos, const wxSize& size)
24998710 354{
fda7962d 355 if ( !MSWCreateControl(TOOLBARCLASSNAME, wxEmptyString, pos, size) )
bfbb0b4c 356 return false;
2bda0e17 357
c8f1f088 358 // toolbar-specific post initialisation
1c383dba 359 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
bfbb0b4c 360
d6f2a891 361#ifdef TB_SETEXTENDEDSTYLE
a9a0ceca
VZ
362 if ( wxApp::GetComCtl32Version() >= 471 )
363 ::SendMessage(GetHwnd(), TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_DRAWDDARROWS);
d6f2a891 364#endif
a9a0ceca 365
bfbb0b4c 366 return true;
24998710 367}
c8f1f088 368
24998710
VZ
369void wxToolBar::Recreate()
370{
371 const HWND hwndOld = GetHwnd();
372 if ( !hwndOld )
373 {
374 // we haven't been created yet, no need to recreate
375 return;
376 }
2bda0e17 377
24998710
VZ
378 // get the position and size before unsubclassing the old toolbar
379 const wxPoint pos = GetPosition();
380 const wxSize size = GetSize();
bb6290e3 381
24998710 382 UnsubclassWin();
2bda0e17 383
278c927d 384 if ( !MSWCreateToolbar(pos, size) )
24998710
VZ
385 {
386 // what can we do?
9a83f860 387 wxFAIL_MSG( wxT("recreating the toolbar failed") );
24998710
VZ
388
389 return;
390 }
391
392 // reparent all our children under the new toolbar
222ed1d6 393 for ( wxWindowList::compatibility_iterator node = m_children.GetFirst();
24998710
VZ
394 node;
395 node = node->GetNext() )
396 {
397 wxWindow *win = node->GetData();
398 if ( !win->IsTopLevel() )
399 ::SetParent(GetHwndOf(win), GetHwnd());
400 }
401
a8cb1a49
DS
402 // only destroy the old toolbar now --
403 // after all the children had been reparented
24998710
VZ
404 ::DestroyWindow(hwndOld);
405
406 // it is for the old bitmap control and can't be used with the new one
407 if ( m_hBitmap )
408 {
409 ::DeleteObject((HBITMAP) m_hBitmap);
410 m_hBitmap = 0;
411 }
412
8d0a7b56
VZ
413 if ( m_disabledImgList )
414 {
415 delete m_disabledImgList;
416 m_disabledImgList = NULL;
417 }
418
24998710 419 Realize();
2bda0e17
KB
420}
421
8a0681f9 422wxToolBar::~wxToolBar()
2bda0e17 423{
f6bcfd97
BP
424 // we must refresh the frame size when the toolbar is deleted but the frame
425 // is not - otherwise toolbar leaves a hole in the place it used to occupy
0dba08dd 426 SendSizeEventToParent();
f6bcfd97
BP
427
428 if ( m_hBitmap )
1c383dba 429 ::DeleteObject((HBITMAP) m_hBitmap);
8d0a7b56
VZ
430
431 delete m_disabledImgList;
1c383dba
VZ
432}
433
24998710
VZ
434wxSize wxToolBar::DoGetBestSize() const
435{
308bb56f
VZ
436 wxSize sizeBest;
437
438 SIZE size;
439 if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE, 0, (LPARAM)&size) )
440 {
441 // maybe an old (< 0x400) Windows version? try to approximate the
442 // toolbar size ourselves
443 sizeBest = GetToolSize();
4f6b4292 444 sizeBest.y += 2 * ::GetSystemMetrics(SM_CYBORDER); // Add borders
308bb56f
VZ
445 sizeBest.x *= GetToolsCount();
446
447 // reverse horz and vertical components if necessary
7a976304 448 if ( IsVertical() )
308bb56f
VZ
449 {
450 int t = sizeBest.x;
451 sizeBest.x = sizeBest.y;
452 sizeBest.y = t;
453 }
454 }
3c30bbb9 455 else // TB_GETMAXSIZE succeeded
308bb56f 456 {
3c30bbb9
VZ
457 // but it could still return an incorrect result due to what appears to
458 // be a bug in old comctl32.dll versions which don't handle controls in
459 // the toolbar correctly, so work around it (see SF patch 1902358)
460 if ( !IsVertical() && wxApp::GetComCtl32Version() < 600 )
461 {
462 // calculate the toolbar width in alternative way
463 const RECT rcFirst = wxGetTBItemRect(GetHwnd(), 0);
464 const RECT rcLast = wxGetTBItemRect(GetHwnd(), GetToolsCount() - 1);
465
466 const int widthAlt = rcLast.right - rcFirst.left;
467 if ( widthAlt > size.cx )
468 size.cx = widthAlt;
469 }
470
308bb56f
VZ
471 sizeBest.x = size.cx;
472 sizeBest.y = size.cy;
473 }
24998710 474
3c30bbb9 475 if ( !IsVertical() )
c118d8b0
JS
476 {
477 // Without the extra height, DoGetBestSize can report a size that's
478 // smaller than the actual window, causing windows to overlap slightly
479 // in some circumstances, leading to missing borders (especially noticeable
480 // in AUI layouts).
481 if (!(GetWindowStyle() & wxTB_NODIVIDER))
482 sizeBest.y += 2;
483 sizeBest.y ++;
a14337d6 484 }
378c9833 485
31582e4e 486 CacheBestSize(sizeBest);
97071cdb 487
308bb56f 488 return sizeBest;
24998710
VZ
489}
490
491WXDWORD wxToolBar::MSWGetStyle(long style, WXDWORD *exstyle) const
492{
493 // toolbars never have border, giving one to them results in broken
494 // appearance
495 WXDWORD msStyle = wxControl::MSWGetStyle
496 (
497 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
498 );
499
6a1b3ead
VZ
500 if ( !(style & wxTB_NO_TOOLTIPS) )
501 msStyle |= TBSTYLE_TOOLTIPS;
24998710 502
f9dae779 503 if ( style & (wxTB_FLAT | wxTB_HORZ_LAYOUT) )
24998710
VZ
504 {
505 // static as it doesn't change during the program lifetime
60decce5 506 static const int s_verComCtl = wxApp::GetComCtl32Version();
24998710
VZ
507
508 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
509 // style with 6.00 (part of Windows XP) leads to the toolbar with
510 // incorrect background colour - and not using it still results in the
511 // correct (flat) toolbar, so don't use it there
512 if ( s_verComCtl > 400 && s_verComCtl < 600 )
24998710 513 msStyle |= TBSTYLE_FLAT | TBSTYLE_TRANSPARENT;
f9dae779
VZ
514
515 if ( s_verComCtl >= 470 && style & wxTB_HORZ_LAYOUT )
f9dae779 516 msStyle |= TBSTYLE_LIST;
24998710
VZ
517 }
518
519 if ( style & wxTB_NODIVIDER )
520 msStyle |= CCS_NODIVIDER;
521
522 if ( style & wxTB_NOALIGN )
523 msStyle |= CCS_NOPARENTALIGN;
524
3bb63e5c
JS
525 if ( style & wxTB_VERTICAL )
526 msStyle |= CCS_VERT;
527
5b2acc3a
RR
528 if( style & wxTB_BOTTOM )
529 msStyle |= CCS_BOTTOM;
530
7a976304
VZ
531 if ( style & wxTB_RIGHT )
532 msStyle |= CCS_RIGHT;
533
24998710
VZ
534 return msStyle;
535}
536
bdc72a22 537// ----------------------------------------------------------------------------
8a0681f9 538// adding/removing tools
bdc72a22
VZ
539// ----------------------------------------------------------------------------
540
9ccffa51
VZ
541bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
542 wxToolBarToolBase * WXUNUSED(tool))
1c383dba 543{
8a0681f9
VZ
544 // nothing special to do here - we really create the toolbar buttons in
545 // Realize() later
9f884528 546 InvalidateBestSize();
bfbb0b4c 547 return true;
1c383dba
VZ
548}
549
8a0681f9 550bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
bdc72a22 551{
f6bcfd97
BP
552 // the main difficulty we have here is with the controls in the toolbars:
553 // as we (sometimes) use several separators to cover up the space used by
554 // them, the indices are not the same for us and the toolbar
555
556 // first determine the position of the first button to delete: it may be
557 // different from pos if we use several separators to cover the space used
558 // by a control
222ed1d6 559 wxToolBarToolsList::compatibility_iterator node;
f6bcfd97
BP
560 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
561 {
562 wxToolBarToolBase *tool2 = node->GetData();
563 if ( tool2 == tool )
564 {
565 // let node point to the next node in the list
566 node = node->GetNext();
567
568 break;
569 }
570
571 if ( tool2->IsControl() )
56bd6aac 572 pos += ((wxToolBarTool *)tool2)->GetSeparatorsCount() - 1;
f6bcfd97
BP
573 }
574
575 // now determine the number of buttons to delete and the area taken by them
8a0681f9 576 size_t nButtonsToDelete = 1;
bdc72a22 577
8a0681f9 578 // get the size of the button we're going to delete
3c30bbb9 579 const RECT r = wxGetTBItemRect(GetHwnd(), pos);
bdc72a22 580
8a0681f9 581 int width = r.right - r.left;
bdc72a22 582
8a0681f9
VZ
583 if ( tool->IsControl() )
584 {
585 nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount();
8a0681f9
VZ
586 width *= nButtonsToDelete;
587 }
bdc72a22 588
f6bcfd97
BP
589 // do delete all buttons
590 m_nButtons -= nButtonsToDelete;
8a0681f9
VZ
591 while ( nButtonsToDelete-- > 0 )
592 {
593 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) )
594 {
f6bcfd97 595 wxLogLastError(wxT("TB_DELETEBUTTON"));
1c383dba 596
bfbb0b4c 597 return false;
8a0681f9
VZ
598 }
599 }
1c383dba 600
f6bcfd97
BP
601 // and finally reposition all the controls after this button (the toolbar
602 // takes care of all normal items)
603 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
8a0681f9 604 {
cdb11cb9 605 wxToolBarTool *tool2 = (wxToolBarTool*)node->GetData();
8a0681f9
VZ
606 if ( tool2->IsControl() )
607 {
cc260109 608 tool2->MoveBy(-width);
8a0681f9
VZ
609 }
610 }
1c383dba 611
9f884528 612 InvalidateBestSize();
97071cdb 613
bfbb0b4c 614 return true;
1c383dba
VZ
615}
616
8d0a7b56
VZ
617void wxToolBar::CreateDisabledImageList()
618{
2c622593
DS
619 if (m_disabledImgList != NULL)
620 {
621 delete m_disabledImgList;
622 m_disabledImgList = NULL;
623 }
624
8d0a7b56
VZ
625 // as we can't use disabled image list with older versions of comctl32.dll,
626 // don't even bother creating it
5625d0d4 627 if ( wxApp::GetComCtl32Version() >= 470 )
8d0a7b56
VZ
628 {
629 // search for the first disabled button img in the toolbar, if any
630 for ( wxToolBarToolsList::compatibility_iterator
631 node = m_tools.GetFirst(); node; node = node->GetNext() )
632 {
633 wxToolBarToolBase *tool = node->GetData();
634 wxBitmap bmpDisabled = tool->GetDisabledBitmap();
635 if ( bmpDisabled.Ok() )
636 {
637 m_disabledImgList = new wxImageList
638 (
639 m_defaultWidth,
640 m_defaultHeight,
641 bmpDisabled.GetMask() != NULL,
642 GetToolsCount()
643 );
2c622593 644 break;
8d0a7b56
VZ
645 }
646 }
647
648 // we don't have any disabled bitmaps
649 }
8d0a7b56
VZ
650}
651
8a0681f9 652bool wxToolBar::Realize()
1c383dba 653{
bb2212e6
VZ
654 if ( !wxToolBarBase::Realize() )
655 return false;
1c383dba 656
bb2212e6 657 const size_t nTools = GetToolsCount();
26dd7154 658
fb7e87ef
VZ
659#ifdef wxREMAP_BUTTON_COLOURS
660 // don't change the values of these constants, they can be set from the
661 // user code via wxSystemOptions
662 enum
92661f97 663 {
fb7e87ef
VZ
664 Remap_None = -1,
665 Remap_Bg,
666 Remap_Buttons,
667 Remap_TransparentBg
668 };
669
670 // the user-specified option overrides anything, but if it wasn't set, only
671 // remap the buttons on 8bpp displays as otherwise the bitmaps usually look
672 // much worse after remapping
673 static const wxChar *remapOption = wxT("msw.remap");
674 const int remapValue = wxSystemOptions::HasOption(remapOption)
675 ? wxSystemOptions::GetOptionInt(remapOption)
676 : wxDisplayDepth() <= 8 ? Remap_Buttons
677 : Remap_None;
678
679#endif // wxREMAP_BUTTON_COLOURS
92661f97 680
2b5f62a0
VZ
681 // delete all old buttons, if any
682 for ( size_t pos = 0; pos < m_nButtons; pos++ )
683 {
684 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) )
685 {
686 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
687 }
688 }
689
8a0681f9
VZ
690 // First, add the bitmap: we use one bitmap for all toolbar buttons
691 // ----------------------------------------------------------------
2bda0e17 692
222ed1d6 693 wxToolBarToolsList::compatibility_iterator node;
24998710
VZ
694 int bitmapId = 0;
695
0580ddb2 696 if ( !HasFlag(wxTB_NOICONS) )
24998710
VZ
697 {
698 // if we already have a bitmap, we'll replace the existing one --
699 // otherwise we'll install a new one
700 HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap;
701
4a10ea8b
MW
702 const wxCoord totalBitmapWidth = m_defaultWidth *
703 wx_truncate_cast(wxCoord, nTools),
24998710 704 totalBitmapHeight = m_defaultHeight;
2bda0e17 705
17cfea01 706 // Create a bitmap and copy all the tool bitmaps into it
24998710
VZ
707 wxMemoryDC dcAllButtons;
708 wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight);
709 dcAllButtons.SelectObject(bitmap);
bb86baea 710
fb7e87ef
VZ
711#ifdef wxREMAP_BUTTON_COLOURS
712 if ( remapValue != Remap_TransparentBg )
713#endif // wxREMAP_BUTTON_COLOURS
714 {
715 // VZ: why do we hardcode grey colour for CE?
716 dcAllButtons.SetBackground(wxBrush(
717#ifdef __WXWINCE__
718 wxColour(0xc0, 0xc0, 0xc0)
719#else // !__WXWINCE__
720 GetBackgroundColour()
721#endif // __WXWINCE__/!__WXWINCE__
722 ));
723 dcAllButtons.Clear();
724 }
24998710
VZ
725
726 m_hBitmap = bitmap.GetHBITMAP();
727 HBITMAP hBitmap = (HBITMAP)m_hBitmap;
2bda0e17 728
fb7e87ef
VZ
729#ifdef wxREMAP_BUTTON_COLOURS
730 if ( remapValue == Remap_Bg )
dfa53609 731 {
dfa53609 732 dcAllButtons.SelectObject(wxNullBitmap);
dfa53609
JS
733
734 // Even if we're not remapping the bitmap
735 // content, we still have to remap the background.
736 hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
737 totalBitmapWidth, totalBitmapHeight);
738
dfa53609 739 dcAllButtons.SelectObject(bitmap);
dfa53609 740 }
fb7e87ef 741#endif // wxREMAP_BUTTON_COLOURS
dfa53609 742
24998710
VZ
743 // the button position
744 wxCoord x = 0;
2bda0e17 745
24998710
VZ
746 // the number of buttons (not separators)
747 int nButtons = 0;
1c383dba 748
8d0a7b56 749 CreateDisabledImageList();
4769a562 750 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1c383dba 751 {
24998710
VZ
752 wxToolBarToolBase *tool = node->GetData();
753 if ( tool->IsButton() )
1c383dba 754 {
24998710 755 const wxBitmap& bmp = tool->GetNormalBitmap();
ba700b5c
VZ
756
757 const int w = bmp.GetWidth();
758 const int h = bmp.GetHeight();
759
24998710
VZ
760 if ( bmp.Ok() )
761 {
ba700b5c
VZ
762 int xOffset = wxMax(0, (m_defaultWidth - w)/2);
763 int yOffset = wxMax(0, (m_defaultHeight - h)/2);
764
24998710 765 // notice the last parameter: do use mask
ba700b5c 766 dcAllButtons.DrawBitmap(bmp, x + xOffset, yOffset, true);
24998710
VZ
767 }
768 else
1c383dba 769 {
9a83f860 770 wxFAIL_MSG( wxT("invalid tool button bitmap") );
1c383dba
VZ
771 }
772
8d0a7b56
VZ
773 // also deal with disabled bitmap if we want to use them
774 if ( m_disabledImgList )
775 {
776 wxBitmap bmpDisabled = tool->GetDisabledBitmap();
64c288fa 777#if wxUSE_IMAGE && wxUSE_WXDIB
8d0a7b56
VZ
778 if ( !bmpDisabled.Ok() )
779 {
8d0a7b56
VZ
780 // no disabled bitmap specified but we still need to
781 // fill the space in the image list with something, so
782 // we grey out the normal bitmap
9ac43913
VZ
783 wxImage
784 imgGreyed = bmp.ConvertToImage().ConvertToGreyscale();
ba700b5c 785
fb7e87ef
VZ
786#ifdef wxREMAP_BUTTON_COLOURS
787 if ( remapValue == Remap_Buttons )
ba700b5c 788 {
0f99ce3d
DS
789 // we need to have light grey background colour for
790 // MapBitmap() to work correctly
791 for ( int y = 0; y < h; y++ )
ba700b5c 792 {
0f99ce3d
DS
793 for ( int x = 0; x < w; x++ )
794 {
795 if ( imgGreyed.IsTransparent(x, y) )
796 imgGreyed.SetRGB(x, y,
797 wxLIGHT_GREY->Red(),
798 wxLIGHT_GREY->Green(),
799 wxLIGHT_GREY->Blue());
800 }
ba700b5c
VZ
801 }
802 }
fb7e87ef 803#endif // wxREMAP_BUTTON_COLOURS
8d0a7b56
VZ
804
805 bmpDisabled = wxBitmap(imgGreyed);
8d0a7b56 806 }
ba700b5c
VZ
807#endif // wxUSE_IMAGE
808
fb7e87ef
VZ
809#ifdef wxREMAP_BUTTON_COLOURS
810 if ( remapValue == Remap_Buttons )
0f99ce3d 811 MapBitmap(bmpDisabled.GetHBITMAP(), w, h);
fb7e87ef 812#endif // wxREMAP_BUTTON_COLOURS
8d0a7b56
VZ
813
814 m_disabledImgList->Add(bmpDisabled);
815 }
816
24998710
VZ
817 // still inc width and number of buttons because otherwise the
818 // subsequent buttons will all be shifted which is rather confusing
819 // (and like this you'd see immediately which bitmap was bad)
820 x += m_defaultWidth;
821 nButtons++;
1c383dba
VZ
822 }
823 }
8a0681f9 824
24998710 825 dcAllButtons.SelectObject(wxNullBitmap);
f6bcfd97 826
24998710
VZ
827 // don't delete this HBITMAP!
828 bitmap.SetHBITMAP(0);
2bda0e17 829
fb7e87ef
VZ
830#ifdef wxREMAP_BUTTON_COLOURS
831 if ( remapValue == Remap_Buttons )
dfa53609
JS
832 {
833 // Map to system colours
834 hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
fb7e87ef 835 totalBitmapWidth, totalBitmapHeight);
dfa53609 836 }
fb7e87ef 837#endif // wxREMAP_BUTTON_COLOURS
1c383dba 838
bfbb0b4c 839 bool addBitmap = true;
9f83044f 840
24998710 841 if ( oldToolBarBitmap )
1c383dba 842 {
24998710 843#ifdef TB_REPLACEBITMAP
2a1f999f 844 if ( wxApp::GetComCtl32Version() >= 400 )
9f83044f 845 {
24998710
VZ
846 TBREPLACEBITMAP replaceBitmap;
847 replaceBitmap.hInstOld = NULL;
848 replaceBitmap.hInstNew = NULL;
dca0f651
VZ
849 replaceBitmap.nIDOld = (UINT_PTR)oldToolBarBitmap;
850 replaceBitmap.nIDNew = (UINT_PTR)hBitmap;
24998710
VZ
851 replaceBitmap.nButtons = nButtons;
852 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP,
853 0, (LPARAM) &replaceBitmap) )
854 {
855 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
856 }
9f83044f 857
24998710 858 ::DeleteObject(oldToolBarBitmap);
9f83044f 859
24998710 860 // already done
bfbb0b4c 861 addBitmap = false;
24998710
VZ
862 }
863 else
9f83044f 864#endif // TB_REPLACEBITMAP
24998710
VZ
865 {
866 // we can't replace the old bitmap, so we will add another one
867 // (awfully inefficient, but what else to do?) and shift the bitmap
868 // indices accordingly
bfbb0b4c 869 addBitmap = true;
1c383dba 870
24998710
VZ
871 bitmapId = m_nButtons;
872 }
9f83044f 873 }
1c383dba 874
24998710 875 if ( addBitmap ) // no old bitmap or we can't replace it
1c383dba 876 {
24998710
VZ
877 TBADDBITMAP addBitmap;
878 addBitmap.hInst = 0;
dca0f651 879 addBitmap.nID = (UINT_PTR)hBitmap;
24998710
VZ
880 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP,
881 (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 )
8a0681f9 882 {
24998710 883 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
8a0681f9 884 }
1c383dba 885 }
8d0a7b56 886
24bc71f4
VZ
887 // disable image lists are only supported in comctl32.dll 4.70+
888 if ( wxApp::GetComCtl32Version() >= 470 )
8d0a7b56 889 {
24bc71f4
VZ
890 HIMAGELIST hil = m_disabledImgList
891 ? GetHimagelistOf(m_disabledImgList)
892 : 0;
893
894 // notice that we set the image list even if don't have one right
895 // now as we could have it before and need to reset it in this case
8d0a7b56 896 HIMAGELIST oldImageList = (HIMAGELIST)
24bc71f4 897 ::SendMessage(GetHwnd(), TB_SETDISABLEDIMAGELIST, 0, (LPARAM)hil);
8d0a7b56
VZ
898
899 // delete previous image list if any
900 if ( oldImageList )
24bc71f4 901 ::DeleteObject(oldImageList);
8d0a7b56 902 }
2bda0e17 903 }
9f83044f 904
2bda0e17 905
8a0681f9
VZ
906 // Next add the buttons and separators
907 // -----------------------------------
908
0580ddb2 909 wxScopedArray<TBBUTTON> buttons(new TBBUTTON[nTools]);
2bda0e17 910
8a0681f9 911 // this array will hold the indices of all controls in the toolbar
1c383dba
VZ
912 wxArrayInt controlIds;
913
bfbb0b4c 914 bool lastWasRadio = false;
1c383dba 915 int i = 0;
4769a562 916 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
2bda0e17 917 {
cc260109 918 wxToolBarTool *tool = static_cast<wxToolBarTool *>(node->GetData());
8a0681f9 919
28603488
VZ
920 // don't add separators to the vertical toolbar with old comctl32.dll
921 // versions as they didn't handle this properly
7a976304 922 if ( IsVertical() && tool->IsSeparator() &&
2a1f999f 923 wxApp::GetComCtl32Version() <= 472 )
28603488
VZ
924 {
925 continue;
926 }
927
1c383dba
VZ
928 TBBUTTON& button = buttons[i];
929
930 wxZeroMemory(button);
931
bfbb0b4c 932 bool isRadio = false;
8a0681f9 933 switch ( tool->GetStyle() )
1c383dba
VZ
934 {
935 case wxTOOL_STYLE_CONTROL:
1c383dba 936 case wxTOOL_STYLE_SEPARATOR:
cc260109
VZ
937 if ( tool->IsStretchableSpace() )
938 {
939 // we're going to modify the size of this button later and
940 // so we need a valid id for it and not wxID_SEPARATOR
941 // which is used by spacers by default
942 tool->AllocSpacerId();
8c118ca4
VZ
943
944 // also set the number of separators so that the logic in
945 // HandlePaint() works correctly
946 tool->SetSeparatorsCount(1);
cc260109
VZ
947 }
948
949 button.idCommand = tool->GetId();
1c383dba
VZ
950 button.fsState = TBSTATE_ENABLED;
951 button.fsStyle = TBSTYLE_SEP;
952 break;
953
954 case wxTOOL_STYLE_BUTTON:
24998710
VZ
955 if ( !HasFlag(wxTB_NOICONS) )
956 button.iBitmap = bitmapId;
c631abda 957
24998710 958 if ( HasFlag(wxTB_TEXT) )
c631abda 959 {
24998710
VZ
960 const wxString& label = tool->GetLabel();
961 if ( !label.empty() )
dca0f651 962 button.iString = (INT_PTR)label.wx_str();
c631abda
VZ
963 }
964
8a0681f9 965 button.idCommand = tool->GetId();
1c383dba 966
8a0681f9 967 if ( tool->IsEnabled() )
1c383dba 968 button.fsState |= TBSTATE_ENABLED;
8a0681f9 969 if ( tool->IsToggled() )
1c383dba 970 button.fsState |= TBSTATE_CHECKED;
8a0681f9 971
c631abda
VZ
972 switch ( tool->GetKind() )
973 {
974 case wxITEM_RADIO:
975 button.fsStyle = TBSTYLE_CHECKGROUP;
976
977 if ( !lastWasRadio )
978 {
979 // the first item in the radio group is checked by
980 // default to be consistent with wxGTK and the menu
981 // radio items
982 button.fsState |= TBSTATE_CHECKED;
f3ba93c1 983
730d3366
RD
984 if (tool->Toggle(true))
985 {
986 DoToggleTool(tool, true);
987 }
988 }
0b62d387 989 else if ( tool->IsToggled() )
730d3366
RD
990 {
991 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
992 int prevIndex = i - 1;
993 while ( nodePrev )
994 {
995 TBBUTTON& prevButton = buttons[prevIndex];
996 wxToolBarToolBase *tool = nodePrev->GetData();
997 if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO )
998 break;
ed7fdb86 999
730d3366 1000 if ( tool->Toggle(false) )
730d3366 1001 DoToggleTool(tool, false);
a8cb1a49 1002
a2dd2ea7 1003 prevButton.fsState &= ~TBSTATE_CHECKED;
730d3366
RD
1004 nodePrev = nodePrev->GetPrevious();
1005 prevIndex--;
ed7fdb86 1006 }
c631abda
VZ
1007 }
1008
bfbb0b4c 1009 isRadio = true;
c631abda
VZ
1010 break;
1011
1012 case wxITEM_CHECK:
1013 button.fsStyle = TBSTYLE_CHECK;
1014 break;
1015
97071cdb
DS
1016 case wxITEM_NORMAL:
1017 button.fsStyle = TBSTYLE_BUTTON;
1018 break;
1019
a9a0ceca
VZ
1020 case wxITEM_DROPDOWN:
1021 button.fsStyle = TBSTYLE_DROPDOWN;
1022 break;
1023
c631abda 1024 default:
9a83f860 1025 wxFAIL_MSG( wxT("unexpected toolbar button kind") );
c631abda 1026 button.fsStyle = TBSTYLE_BUTTON;
97071cdb 1027 break;
c631abda 1028 }
1c383dba
VZ
1029
1030 bitmapId++;
1031 break;
1032 }
2bda0e17 1033
c631abda
VZ
1034 lastWasRadio = isRadio;
1035
1c383dba 1036 i++;
2bda0e17 1037 }
1c383dba 1038
0580ddb2 1039 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, i, (LPARAM)buttons.get()) )
2bda0e17 1040 {
f6bcfd97 1041 wxLogLastError(wxT("TB_ADDBUTTONS"));
2bda0e17 1042 }
89b892a2 1043
2bda0e17 1044
cc260109
VZ
1045 // Adjust controls and stretchable spaces
1046 // --------------------------------------
8a0681f9 1047
cc260109
VZ
1048 // adjust the controls size to fit nicely in the toolbar and compute its
1049 // total size while doing it
1050 m_totalFixedSize = 0;
1051 int toolIndex = 0;
1052 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), toolIndex++ )
1c383dba 1053 {
cc260109 1054 wxToolBarTool * const tool = (wxToolBarTool*)node->GetData();
1c383dba 1055
cc260109 1056 const RECT r = wxGetTBItemRect(GetHwnd(), toolIndex);
bdc72a22 1057
cc260109 1058 if ( !tool->IsControl() )
34e5028f 1059 {
cc260109
VZ
1060 if ( IsVertical() )
1061 m_totalFixedSize += r.bottom - r.top;
1062 else
1063 m_totalFixedSize += r.right - r.left;
1064
1065 continue;
1066 }
34e5028f 1067
cc260109
VZ
1068 if ( IsVertical() )
1069 {
1070 // don't embed controls in the vertical toolbar, this doesn't look
1071 // good and wxGTK doesn't do it neither (and the code below can't
1072 // deal with this case)
34e5028f
VZ
1073 continue;
1074 }
1075
cc260109 1076 wxControl * const control = tool->GetControl();
cdb11cb9
VZ
1077 wxStaticText * const staticText = tool->GetStaticText();
1078
34e5028f 1079 wxSize size = control->GetSize();
cdb11cb9
VZ
1080 wxSize staticTextSize;
1081 if ( staticText )
1082 {
1083 staticTextSize = staticText->GetSize();
1084 staticTextSize.y += 3; // margin between control and its label
1085 }
34e5028f 1086
bdc72a22 1087 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
5b59df83 1088#ifdef TB_SETBUTTONINFO
34e5028f
VZ
1089 // available in headers, now check whether it is available now
1090 // (during run-time)
2a1f999f 1091 if ( wxApp::GetComCtl32Version() >= 471 )
34e5028f
VZ
1092 {
1093 // set the (underlying) separators width to be that of the
1094 // control
1095 TBBUTTONINFO tbbi;
1096 tbbi.cbSize = sizeof(tbbi);
1097 tbbi.dwMask = TBIF_SIZE;
5c519b6c 1098 tbbi.cx = (WORD)size.x;
bfbb0b4c
WS
1099 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO,
1100 tool->GetId(), (LPARAM)&tbbi) )
bdc72a22 1101 {
34e5028f
VZ
1102 // the id is probably invalid?
1103 wxLogLastError(wxT("TB_SETBUTTONINFO"));
bdc72a22 1104 }
34e5028f
VZ
1105 }
1106 else
1107#endif // comctl32.dll 4.71
1108 // TB_SETBUTTONINFO unavailable
1109 {
1110 // try adding several separators to fit the controls width
1111 int widthSep = r.right - r.left;
34e5028f
VZ
1112
1113 TBBUTTON tbb;
1114 wxZeroMemory(tbb);
1115 tbb.idCommand = 0;
1116 tbb.fsState = TBSTATE_ENABLED;
1117 tbb.fsStyle = TBSTYLE_SEP;
1118
1119 size_t nSeparators = size.x / widthSep;
1120 for ( size_t nSep = 0; nSep < nSeparators; nSep++ )
bdc72a22 1121 {
bfbb0b4c 1122 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON,
cc260109 1123 toolIndex, (LPARAM)&tbb) )
bdc72a22 1124 {
34e5028f 1125 wxLogLastError(wxT("TB_INSERTBUTTON"));
bdc72a22
VZ
1126 }
1127
cc260109 1128 toolIndex++;
bdc72a22 1129 }
1c383dba 1130
34e5028f
VZ
1131 // remember the number of separators we used - we'd have to
1132 // delete all of them later
cc260109 1133 tool->SetSeparatorsCount(nSeparators);
34e5028f
VZ
1134
1135 // adjust the controls width to exactly cover the separators
cdb11cb9
VZ
1136 size.x = (nSeparators + 1)*widthSep;
1137 control->SetSize(size.x, wxDefaultCoord);
34e5028f
VZ
1138 }
1139
cdb11cb9
VZ
1140 // position the control itself correctly vertically centering it on the
1141 // icon area of the toolbar
1142 int height = r.bottom - r.top - staticTextSize.y;
1143
1c383dba 1144 int diff = height - size.y;
cdb11cb9 1145 if ( diff < 0 || !HasFlag(wxTB_TEXT) )
1c383dba 1146 {
cdb11cb9
VZ
1147 // not enough room for the static text
1148 if ( staticText )
1149 staticText->Hide();
1150
1151 // recalculate height & diff without the staticText control
1152 height = r.bottom - r.top;
1153 diff = height - size.y;
1154 if ( diff < 0 )
1155 {
1156 // the control is too high, resize to fit
1157 control->SetSize(wxDefaultCoord, height - 2);
1c383dba 1158
cdb11cb9
VZ
1159 diff = 2;
1160 }
1161 }
1162 else // enough space for both the control and the label
1163 {
1164 if ( staticText )
1165 staticText->Show();
1c383dba 1166 }
2bda0e17 1167
cc260109 1168 control->Move(r.left, r.top + (diff + 1) / 2);
cdb11cb9
VZ
1169 if ( staticText )
1170 {
cc260109 1171 staticText->Move(r.left + (size.x - staticTextSize.x)/2,
cdb11cb9
VZ
1172 r.bottom - staticTextSize.y);
1173 }
cc260109
VZ
1174
1175 m_totalFixedSize += size.x;
1c383dba 1176 }
89b892a2 1177
8a0681f9
VZ
1178 // the max index is the "real" number of buttons - i.e. counting even the
1179 // separators which we added just for aligning the controls
cc260109 1180 m_nButtons = toolIndex;
89b892a2 1181
7a976304 1182 if ( !IsVertical() )
8a0681f9
VZ
1183 {
1184 if ( m_maxRows == 0 )
8a0681f9
VZ
1185 // if not set yet, only one row
1186 SetRows(1);
8a0681f9
VZ
1187 }
1188 else if ( m_nButtons > 0 ) // vertical non empty toolbar
1189 {
e49426a2
RD
1190 // if not set yet, have one column
1191 m_maxRows = 1;
f4322df6 1192 SetRows(m_nButtons);
8a0681f9 1193 }
2bda0e17 1194
9f884528 1195 InvalidateBestSize();
2091f5e7 1196 UpdateSize();
c6dcd973 1197
bfbb0b4c 1198 return true;
2bda0e17
KB
1199}
1200
cc260109
VZ
1201void wxToolBar::UpdateStretchableSpacersSize()
1202{
8c118ca4
VZ
1203#ifdef TB_SETBUTTONINFO
1204 // we can't resize the spacers if TB_SETBUTTONINFO is not supported (we
1205 // could try to do it with multiple separators as for the controls but this
1206 // is too painful and it just doesn't seem to be worth doing for the
1207 // ancient systems)
cc260109
VZ
1208 if ( wxApp::GetComCtl32Version() < 471 )
1209 return;
1210
1211 // check if we have any stretchable spacers in the first place
1212 unsigned numSpaces = 0;
1213 wxToolBarToolsList::compatibility_iterator node;
1214 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1215 {
1216 wxToolBarTool * const tool = (wxToolBarTool*)node->GetData();
1217 if ( tool->IsStretchableSpace() )
1218 numSpaces++;
1219 }
1220
1221 if ( !numSpaces )
1222 return;
1223
1224 // we do, adjust their size: either distribute the extra size among them or
1225 // reduce their size if there is not enough place for all tools
1226 const int totalSize = IsVertical() ? GetClientSize().y : GetClientSize().x;
1227 const int extraSize = totalSize - m_totalFixedSize;
1228 const int sizeSpacer = extraSize > 0 ? extraSize / numSpaces : 0;
1229
1230 // the last spacer should consume all remaining space if we have too much
1231 // of it (which can be greater than sizeSpacer because of the rounding)
1232 const int sizeLastSpacer = extraSize > 0
1233 ? extraSize - (numSpaces - 1)*sizeSpacer
1234 : 0;
1235
1236 // cumulated offset by which we need to move all the following controls to
1237 // the right: while the toolbar takes care of the normal items, we must
1238 // move the controls manually ourselves to ensure they remain at the
1239 // correct place
1240 int offset = 0;
1241 int toolIndex = 0;
1242 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), toolIndex++ )
1243 {
1244 wxToolBarTool * const tool = (wxToolBarTool*)node->GetData();
1245
1246 if ( tool->IsControl() && offset )
1247 {
1248 tool->MoveBy(offset);
1249
1250 continue;
1251 }
1252
1253 if ( !tool->IsStretchableSpace() )
1254 continue;
1255
1256 const RECT rcOld = wxGetTBItemRect(GetHwnd(), toolIndex);
1257
1258 WinStruct<TBBUTTONINFO> tbbi;
1259 tbbi.dwMask = TBIF_SIZE;
1260 tbbi.cx = --numSpaces ? sizeSpacer : sizeLastSpacer;
1261
1262 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO,
1263 tool->GetId(), (LPARAM)&tbbi) )
1264 {
1265 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1266 }
1267 else
1268 {
1269 // we successfully resized this one, move all the controls after it
1270 // by the corresponding amount (may be positive or negative)
1271 offset += tbbi.cx - (rcOld.right - rcOld.left);
1272 }
1273 }
8c118ca4 1274#endif // TB_SETBUTTONINFO
cc260109
VZ
1275}
1276
1c383dba
VZ
1277// ----------------------------------------------------------------------------
1278// message handlers
1279// ----------------------------------------------------------------------------
1280
0edeeb6d 1281bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id_)
2bda0e17 1282{
0edeeb6d
VZ
1283 // cast to signed is important as we compare this id with (signed) ints in
1284 // FindById() and without the cast we'd get a positive int from a
1285 // "negative" (i.e. > 32767) WORD
1286 const int id = (signed short)id_;
1287
1288 wxToolBarToolBase *tool = FindById(id);
8a0681f9 1289 if ( !tool )
bfbb0b4c 1290 return false;
1c383dba 1291
1bba1946 1292 bool toggled = false; // just to suppress warnings
6bb7cee4 1293
ed7701bd
VS
1294 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
1295
8a0681f9 1296 if ( tool->CanBeToggled() )
1c383dba 1297 {
6bb7cee4
VZ
1298 toggled = (state & TBSTATE_CHECKED) != 0;
1299
0356c259
VZ
1300 // ignore the event when a radio button is released, as this doesn't
1301 // seem to happen at all, and is handled otherwise
6bb7cee4 1302 if ( tool->GetKind() == wxITEM_RADIO && !toggled )
bfbb0b4c 1303 return true;
1c383dba 1304
6bb7cee4
VZ
1305 tool->Toggle(toggled);
1306 UnToggleRadioGroup(tool);
1307 }
8a0681f9 1308
ed7701bd
VS
1309 // Without the two lines of code below, if the toolbar was repainted during
1310 // OnLeftClick(), then it could end up without the tool bitmap temporarily
1311 // (see http://lists.nongnu.org/archive/html/lmi/2008-10/msg00014.html).
1312 // The Update() call bellow ensures that this won't happen, by repainting
1313 // invalidated areas of the toolbar immediately.
1314 //
1315 // To complicate matters, the tool would be drawn in depressed state (this
1316 // code is called when mouse button is released, not pressed). That's not
1317 // ideal, having the tool pressed for the duration of OnLeftClick()
1318 // provides the user with useful visual clue that the app is busy reacting
1319 // to the event. So we manually put the tool into pressed state, handle the
1320 // event and then finally restore tool's original state.
1321 ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state | TBSTATE_PRESSED, 0));
1322 Update();
1323
1324 bool allowLeftClick = OnLeftClick(id, toggled);
1325
5f4250ed
VS
1326 // Restore the unpressed state. Enabled/toggled state might have been
1327 // changed since so take care of it.
1328 if (tool->IsEnabled())
1329 state |= TBSTATE_ENABLED;
1330 else
1331 state &= ~TBSTATE_ENABLED;
1332 if (tool->IsToggled())
1333 state |= TBSTATE_CHECKED;
1334 else
1335 state &= ~TBSTATE_CHECKED;
ed7701bd
VS
1336 ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state, 0));
1337
6bb7cee4
VZ
1338 // OnLeftClick() can veto the button state change - for buttons which
1339 // may be toggled only, of couse
ed7701bd 1340 if ( !allowLeftClick && tool->CanBeToggled() )
f3ba93c1 1341 {
6bb7cee4
VZ
1342 // revert back
1343 tool->Toggle(!toggled);
8a0681f9 1344
0356c259 1345 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(!toggled, 0));
1c383dba
VZ
1346 }
1347
bfbb0b4c 1348 return true;
2bda0e17
KB
1349}
1350
8a0681f9 1351bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl),
fd3f686c 1352 WXLPARAM lParam,
33ac7e6f 1353 WXLPARAM *WXUNUSED(result))
2bda0e17 1354{
a9a0ceca
VZ
1355 LPNMHDR hdr = (LPNMHDR)lParam;
1356 if ( hdr->code == TBN_DROPDOWN )
1357 {
1358 LPNMTOOLBAR tbhdr = (LPNMTOOLBAR)lParam;
1359
1360 wxCommandEvent evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED, tbhdr->iItem);
937013e0 1361 if ( HandleWindowEvent(evt) )
a9a0ceca
VZ
1362 {
1363 // Event got handled, don't display default popup menu
1364 return false;
1365 }
1366
1367 const wxToolBarToolBase * const tool = FindById(tbhdr->iItem);
9a83f860 1368 wxCHECK_MSG( tool, false, wxT("drop down message for unknown tool") );
a9a0ceca
VZ
1369
1370 wxMenu * const menu = tool->GetDropdownMenu();
1371 if ( !menu )
1372 return false;
1373
1374 // Display popup menu below button
3c30bbb9
VZ
1375 const RECT r = wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr->iItem));
1376 if ( r.right )
a9a0ceca
VZ
1377 PopupMenu(menu, r.left, r.bottom);
1378
1379 return true;
1380 }
1381
1382
6a1b3ead
VZ
1383 if( !HasFlag(wxTB_NO_TOOLTIPS) )
1384 {
3bce6687 1385#if wxUSE_TOOLTIPS
6a1b3ead 1386 // First check if this applies to us
2bda0e17 1387
6a1b3ead
VZ
1388 // the tooltips control created by the toolbar is sometimes Unicode, even
1389 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1390 UINT code = hdr->code;
1391 if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) )
1392 return false;
2bda0e17 1393
6a1b3ead
VZ
1394 HWND toolTipWnd = (HWND)::SendMessage(GetHwnd(), TB_GETTOOLTIPS, 0, 0);
1395 if ( toolTipWnd != hdr->hwndFrom )
1396 return false;
2bda0e17 1397
6a1b3ead
VZ
1398 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
1399 int id = (int)ttText->hdr.idFrom;
89b892a2 1400
6a1b3ead
VZ
1401 wxToolBarToolBase *tool = FindById(id);
1402 if ( tool )
1403 return HandleTooltipNotify(code, lParam, tool->GetShortHelp());
3bce6687 1404#else
6a1b3ead
VZ
1405 wxUnusedVar(lParam);
1406#endif
1407 }
abf912c5 1408
bfbb0b4c 1409 return false;
2bda0e17
KB
1410}
1411
1c383dba 1412// ----------------------------------------------------------------------------
8a0681f9 1413// toolbar geometry
1c383dba
VZ
1414// ----------------------------------------------------------------------------
1415
8a0681f9 1416void wxToolBar::SetToolBitmapSize(const wxSize& size)
2bda0e17 1417{
1c383dba
VZ
1418 wxToolBarBase::SetToolBitmapSize(size);
1419
040d3c2e 1420 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
2bda0e17
KB
1421}
1422
8a0681f9 1423void wxToolBar::SetRows(int nRows)
2bda0e17 1424{
8a0681f9
VZ
1425 if ( nRows == m_maxRows )
1426 {
1427 // avoid resizing the frame uselessly
1428 return;
1429 }
1430
1431 // TRUE in wParam means to create at least as many rows, FALSE -
1432 // at most as many
1c383dba
VZ
1433 RECT rect;
1434 ::SendMessage(GetHwnd(), TB_SETROWS,
8a0681f9
VZ
1435 MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)),
1436 (LPARAM) &rect);
1c383dba
VZ
1437
1438 m_maxRows = nRows;
8a0681f9
VZ
1439
1440 UpdateSize();
1441}
1442
1443// The button size is bigger than the bitmap size
1444wxSize wxToolBar::GetToolSize() const
1445{
1446 // TB_GETBUTTONSIZE is supported from version 4.70
5438a566 1447#if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
50165591
CE
1448 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1449 && !defined (__DIGITALMARS__)
2a1f999f 1450 if ( wxApp::GetComCtl32Version() >= 470 )
8a0681f9
VZ
1451 {
1452 DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0);
1453
1454 return wxSize(LOWORD(dw), HIWORD(dw));
1455 }
1456 else
1457#endif // comctl32.dll 4.70+
1458 {
1459 // defaults
1460 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
1461 }
2bda0e17
KB
1462}
1463
82c9f85c
VZ
1464static
1465wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools,
1466 size_t index )
a8945eef 1467{
222ed1d6 1468 wxToolBarToolsList::compatibility_iterator current = tools.GetFirst();
a8945eef 1469
489f6cf7 1470 for ( ; current ; current = current->GetNext() )
a8945eef 1471 {
82c9f85c 1472 if ( index == 0 )
a8945eef 1473 return current->GetData();
82c9f85c
VZ
1474
1475 wxToolBarTool *tool = (wxToolBarTool *)current->GetData();
1476 size_t separators = tool->GetSeparatorsCount();
1477
1478 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1479 // otherwise, skip as many items as the separator count, plus the
1480 // control itself
1481 index -= separators ? separators + 1 : 1;
a8945eef
MB
1482 }
1483
1484 return 0;
1485}
1486
8a0681f9 1487wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
2bda0e17 1488{
8a0681f9
VZ
1489 POINT pt;
1490 pt.x = x;
1491 pt.y = y;
1492 int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt);
97071cdb 1493
37d0bdff
MB
1494 // MBN: when the point ( x, y ) is close to the toolbar border
1495 // TB_HITTEST returns m_nButtons ( not -1 )
1496 if ( index < 0 || (size_t)index >= m_nButtons )
8a0681f9 1497 // it's a separator or there is no tool at all there
d3b9f782 1498 return NULL;
1c383dba 1499
a84f66ef
VZ
1500 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1501 // we don't use the dummy separators hack
1502#ifdef TB_SETBUTTONINFO
2a1f999f 1503 if ( wxApp::GetComCtl32Version() >= 471 )
a8945eef
MB
1504 {
1505 return m_tools.Item((size_t)index)->GetData();
1506 }
1507 else
a84f66ef 1508#endif // TB_SETBUTTONINFO
a8945eef
MB
1509 {
1510 return GetItemSkippingDummySpacers( m_tools, (size_t) index );
1511 }
2bda0e17
KB
1512}
1513
8a0681f9 1514void wxToolBar::UpdateSize()
2bda0e17 1515{
48a102f9 1516 wxPoint pos = GetPosition();
93489fc8 1517 ::SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0);
48a102f9
JS
1518 if (pos != GetPosition())
1519 Move(pos);
93489fc8 1520
b93328d8
RD
1521 // In case Realize is called after the initial display (IOW the programmer
1522 // may have rebuilt the toolbar) give the frame the option of resizing the
1523 // toolbar to full width again, but only if the parent is a frame and the
1524 // toolbar is managed by the frame. Otherwise assume that some other
1525 // layout mechanism is controlling the toolbar size and leave it alone.
0dba08dd 1526 SendSizeEventToParent();
2bda0e17
KB
1527}
1528
c631abda
VZ
1529// ----------------------------------------------------------------------------
1530// toolbar styles
1531// ---------------------------------------------------------------------------
1532
2588b9c4
VZ
1533// get the TBSTYLE of the given toolbar window
1534long wxToolBar::GetMSWToolbarStyle() const
1535{
1536 return ::SendMessage(GetHwnd(), TB_GETSTYLE, 0, 0L);
1537}
1538
c631abda
VZ
1539void wxToolBar::SetWindowStyleFlag(long style)
1540{
24998710
VZ
1541 // the style bits whose changes force us to recreate the toolbar
1542 static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS;
c631abda 1543
24998710 1544 const long styleOld = GetWindowStyle();
c631abda 1545
24998710 1546 wxToolBarBase::SetWindowStyleFlag(style);
c631abda 1547
24998710
VZ
1548 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1549 // also fatal as we'd then try to recreate the toolbar when it's just being
1550 // created
1551 if ( GetToolsCount() &&
1552 (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) )
1553 {
1554 // to remove the text labels, simply re-realizing the toolbar is enough
1555 // but I don't know of any way to add the text to an existing toolbar
1556 // other than by recreating it entirely
1557 Recreate();
c631abda 1558 }
c631abda
VZ
1559}
1560
1c383dba
VZ
1561// ----------------------------------------------------------------------------
1562// tool state
1563// ----------------------------------------------------------------------------
1564
8a0681f9 1565void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
2bda0e17 1566{
8a0681f9
VZ
1567 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
1568 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0));
2bda0e17
KB
1569}
1570
8a0681f9 1571void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
2bda0e17 1572{
8a0681f9
VZ
1573 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
1574 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0));
2bda0e17
KB
1575}
1576
33ac7e6f 1577void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
088a95f5 1578{
8a0681f9
VZ
1579 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1580 // without, so we really need to delete the button and recreate it here
9a83f860 1581 wxFAIL_MSG( wxT("not implemented") );
2bda0e17
KB
1582}
1583
bbd321ff
RD
1584void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1585{
5c33522f 1586 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
bbd321ff
RD
1587 if ( tool )
1588 {
1589 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1590
1591 tool->SetNormalBitmap(bitmap);
1592 Realize();
f4322df6 1593 }
bbd321ff
RD
1594}
1595
1596void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1597{
5c33522f 1598 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
bbd321ff
RD
1599 if ( tool )
1600 {
1601 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1602
1603 tool->SetDisabledBitmap(bitmap);
1604 Realize();
f4322df6 1605 }
bbd321ff
RD
1606}
1607
1c383dba
VZ
1608// ----------------------------------------------------------------------------
1609// event handlers
1610// ----------------------------------------------------------------------------
2bda0e17
KB
1611
1612// Responds to colour changes, and passes event on to children.
8a0681f9 1613void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event)
2bda0e17 1614{
84c5b38d 1615 wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE));
2bda0e17
KB
1616
1617 // Remap the buttons
8a0681f9 1618 Realize();
2bda0e17 1619
56bd6aac
VZ
1620 // Relayout the toolbar
1621 int nrows = m_maxRows;
1622 m_maxRows = 0; // otherwise SetRows() wouldn't do anything
1623 SetRows(nrows);
1624
2bda0e17
KB
1625 Refresh();
1626
56bd6aac
VZ
1627 // let the event propagate further
1628 event.Skip();
2bda0e17
KB
1629}
1630
8a0681f9 1631void wxToolBar::OnMouseEvent(wxMouseEvent& event)
e6460682 1632{
6ae2a4b7 1633 if ( event.Leaving() )
fe87983b 1634 {
6ae2a4b7
VZ
1635 if ( m_pInTool )
1636 {
1637 OnMouseEnter(wxID_ANY);
1638 m_pInTool = NULL;
1639 }
1640
fe87983b
MB
1641 event.Skip();
1642 return;
1643 }
1644
2334fef6 1645 if ( event.RightDown() )
e6460682 1646 {
2334fef6 1647 // find the tool under the mouse
8d7eaf91 1648 wxCoord x = 0, y = 0;
2334fef6
VZ
1649 event.GetPosition(&x, &y);
1650
1651 wxToolBarToolBase *tool = FindToolForPosition(x, y);
1652 OnRightClick(tool ? tool->GetId() : -1, x, y);
e6460682
JS
1653 }
1654 else
1655 {
42e69d6b 1656 event.Skip();
e6460682
JS
1657 }
1658}
1659
2eb10e2a 1660bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
bdc72a22 1661{
fd6939a6
VZ
1662 // wait until we have some tools
1663 if ( !GetToolsCount() )
1664 return false;
1665
3bb63e5c 1666 // calculate our minor dimension ourselves - we're confusing the standard
7509fa8c 1667 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
3c30bbb9
VZ
1668 const RECT r = wxGetTBItemRect(GetHwnd(), 0);
1669 if ( !r.right )
1670 return false;
7509fa8c 1671
3c30bbb9
VZ
1672 int w, h;
1673
1674 if ( IsVertical() )
1675 {
1676 w = r.right - r.left;
1677 if ( m_maxRows )
7509fa8c 1678 {
3c30bbb9 1679 w *= (m_nButtons + m_maxRows - 1)/m_maxRows;
7509fa8c 1680 }
3c30bbb9
VZ
1681 h = HIWORD(lParam);
1682 }
1683 else
1684 {
1685 w = LOWORD(lParam);
1686 if (HasFlag( wxTB_FLAT ))
1687 h = r.bottom - r.top - 3;
7509fa8c 1688 else
3c30bbb9
VZ
1689 h = r.bottom - r.top;
1690 if ( m_maxRows )
7509fa8c 1691 {
3c30bbb9
VZ
1692 // FIXME: hardcoded separator line height...
1693 h += HasFlag(wxTB_NODIVIDER) ? 4 : 6;
1694 h *= m_maxRows;
7509fa8c 1695 }
3c30bbb9 1696 }
7509fa8c 1697
3c30bbb9
VZ
1698 if ( MAKELPARAM(w, h) != lParam )
1699 {
1700 // size really changed
1701 SetSize(w, h);
7509fa8c
VZ
1702 }
1703
cc260109
VZ
1704 UpdateStretchableSpacersSize();
1705
3c30bbb9
VZ
1706 // message processed
1707 return true;
7509fa8c
VZ
1708}
1709
6540d8d2 1710#ifndef __WXWINCE__
cc260109 1711
8c118ca4 1712bool wxToolBar::HandlePaint(WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
7509fa8c 1713{
8c118ca4
VZ
1714 // exclude the area occupied by the controls and stretchable spaces from
1715 // the update region to prevent the toolbar from drawing separators in it
cc260109 1716 int toolIndex = 0;
8c118ca4
VZ
1717 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1718 node;
1719 node = node->GetNext() )
7509fa8c 1720 {
8c118ca4
VZ
1721 wxToolBarTool * const
1722 tool = static_cast<wxToolBarTool *>(node->GetData());
cc260109 1723
8c118ca4
VZ
1724 if ( tool->IsControl() || tool->IsStretchableSpace() )
1725 {
1726 const size_t numSeps = tool->GetSeparatorsCount();
1727 for ( size_t n = 0; n < numSeps; n++, toolIndex++ )
8a0681f9 1728 {
8c118ca4 1729 const RECT rcItem = wxGetTBItemRect(GetHwnd(), toolIndex);
7509fa8c 1730
8c118ca4
VZ
1731 const wxRegion rgnItem(wxRectFromRECT(rcItem));
1732 if ( !ValidateRgn(GetHwnd(), GetHrgnOf(rgnItem)) )
1733 {
1734 wxLogLastError(wxT("ValidateRgn()"));
8a0681f9 1735 }
8a0681f9 1736 }
8a0681f9 1737 }
8c118ca4 1738 else
cc260109 1739 {
8c118ca4
VZ
1740 // normal tools never correspond to more than one native button
1741 toolIndex++;
cc260109 1742 }
bdc72a22 1743 }
7509fa8c 1744
8c118ca4
VZ
1745 // still let the native control draw everything else normally
1746 return false;
7509fa8c 1747}
6540d8d2 1748#endif // __WXWINCE__
7509fa8c 1749
2eb10e2a 1750void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
7509fa8c
VZ
1751{
1752 wxCoord x = GET_X_LPARAM(lParam),
1753 y = GET_Y_LPARAM(lParam);
1754 wxToolBarToolBase* tool = FindToolForPosition( x, y );
1755
6ae2a4b7
VZ
1756 // has the current tool changed?
1757 if ( tool != m_pInTool )
7509fa8c
VZ
1758 {
1759 m_pInTool = tool;
6ae2a4b7 1760 OnMouseEnter(tool ? tool->GetId() : wxID_ANY);
7509fa8c
VZ
1761 }
1762}
a8945eef 1763
c140b7e7 1764WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
7509fa8c
VZ
1765{
1766 switch ( nMsg )
1767 {
7509fa8c
VZ
1768 case WM_MOUSEMOVE:
1769 // we don't handle mouse moves, so always pass the message to
4012b958 1770 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
7509fa8c
VZ
1771 HandleMouseMove(wParam, lParam);
1772 break;
a8945eef 1773
4012b958
JS
1774 case WM_SIZE:
1775 if ( HandleSize(wParam, lParam) )
1776 return 0;
1777 break;
1778
1779#ifndef __WXWINCE__
7509fa8c 1780 case WM_PAINT:
1c2b921a
VZ
1781 // refreshing the controls in the toolbar inside a composite window
1782 // results in an endless stream of WM_PAINT messages -- and seems
1783 // to be unnecessary anyhow as everything works just fine without
1784 // any special workarounds in this case
1785 if ( !IsDoubleBuffered() && HandlePaint(wParam, lParam) )
7509fa8c 1786 return 0;
97071cdb 1787 break;
1c2b921a 1788#endif // __WXWINCE__
a8945eef 1789 }
bdc72a22 1790
8a0681f9 1791 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
bdc72a22
VZ
1792}
1793
1c383dba
VZ
1794// ----------------------------------------------------------------------------
1795// private functions
1796// ----------------------------------------------------------------------------
1797
fb7e87ef
VZ
1798#ifdef wxREMAP_BUTTON_COLOURS
1799
566fb299 1800WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height)
2bda0e17 1801{
566fb299 1802 MemoryHDC hdcMem;
56bd6aac 1803
566fb299 1804 if ( !hdcMem )
5e68f8f3 1805 {
9a83f860 1806 wxLogLastError(wxT("CreateCompatibleDC"));
566fb299
VZ
1807
1808 return bitmap;
5e68f8f3 1809 }
56bd6aac 1810
566fb299 1811 SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap);
56bd6aac 1812
566fb299 1813 if ( !bmpInHDC )
5e68f8f3 1814 {
9a83f860 1815 wxLogLastError(wxT("SelectObject"));
566fb299
VZ
1816
1817 return bitmap;
1818 }
56bd6aac 1819
90c1530a
VZ
1820 wxCOLORMAP *cmap = wxGetStdColourMap();
1821
684c68fd
VZ
1822 for ( int i = 0; i < width; i++ )
1823 {
1824 for ( int j = 0; j < height; j++ )
1825 {
1826 COLORREF pixel = ::GetPixel(hdcMem, i, j);
1827
90c1530a 1828 for ( size_t k = 0; k < wxSTD_COL_MAX; k++ )
684c68fd 1829 {
90c1530a 1830 COLORREF col = cmap[k].from;
684c68fd
VZ
1831 if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 &&
1832 abs(GetGValue(pixel) - GetGValue(col)) < 10 &&
1833 abs(GetBValue(pixel) - GetBValue(col)) < 10 )
1834 {
fb7e87ef
VZ
1835 if ( cmap[k].to != pixel )
1836 ::SetPixel(hdcMem, i, j, cmap[k].to);
684c68fd
VZ
1837 break;
1838 }
1839 }
1840 }
1841 }
3a3898f8
VZ
1842
1843 return bitmap;
566fb299 1844}
2bda0e17 1845
fb7e87ef
VZ
1846#endif // wxREMAP_BUTTON_COLOURS
1847
97071cdb 1848#endif // wxUSE_TOOLBAR