]> git.saurik.com Git - wxWidgets.git/blame - src/msw/toolbar.cpp
Don't explicitly set the background colour for wxChoice.
[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();
943 }
944
945 button.idCommand = tool->GetId();
1c383dba
VZ
946 button.fsState = TBSTATE_ENABLED;
947 button.fsStyle = TBSTYLE_SEP;
948 break;
949
950 case wxTOOL_STYLE_BUTTON:
24998710
VZ
951 if ( !HasFlag(wxTB_NOICONS) )
952 button.iBitmap = bitmapId;
c631abda 953
24998710 954 if ( HasFlag(wxTB_TEXT) )
c631abda 955 {
24998710
VZ
956 const wxString& label = tool->GetLabel();
957 if ( !label.empty() )
dca0f651 958 button.iString = (INT_PTR)label.wx_str();
c631abda
VZ
959 }
960
8a0681f9 961 button.idCommand = tool->GetId();
1c383dba 962
8a0681f9 963 if ( tool->IsEnabled() )
1c383dba 964 button.fsState |= TBSTATE_ENABLED;
8a0681f9 965 if ( tool->IsToggled() )
1c383dba 966 button.fsState |= TBSTATE_CHECKED;
8a0681f9 967
c631abda
VZ
968 switch ( tool->GetKind() )
969 {
970 case wxITEM_RADIO:
971 button.fsStyle = TBSTYLE_CHECKGROUP;
972
973 if ( !lastWasRadio )
974 {
975 // the first item in the radio group is checked by
976 // default to be consistent with wxGTK and the menu
977 // radio items
978 button.fsState |= TBSTATE_CHECKED;
f3ba93c1 979
730d3366
RD
980 if (tool->Toggle(true))
981 {
982 DoToggleTool(tool, true);
983 }
984 }
0b62d387 985 else if ( tool->IsToggled() )
730d3366
RD
986 {
987 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
988 int prevIndex = i - 1;
989 while ( nodePrev )
990 {
991 TBBUTTON& prevButton = buttons[prevIndex];
992 wxToolBarToolBase *tool = nodePrev->GetData();
993 if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO )
994 break;
ed7fdb86 995
730d3366 996 if ( tool->Toggle(false) )
730d3366 997 DoToggleTool(tool, false);
a8cb1a49 998
a2dd2ea7 999 prevButton.fsState &= ~TBSTATE_CHECKED;
730d3366
RD
1000 nodePrev = nodePrev->GetPrevious();
1001 prevIndex--;
ed7fdb86 1002 }
c631abda
VZ
1003 }
1004
bfbb0b4c 1005 isRadio = true;
c631abda
VZ
1006 break;
1007
1008 case wxITEM_CHECK:
1009 button.fsStyle = TBSTYLE_CHECK;
1010 break;
1011
97071cdb
DS
1012 case wxITEM_NORMAL:
1013 button.fsStyle = TBSTYLE_BUTTON;
1014 break;
1015
a9a0ceca
VZ
1016 case wxITEM_DROPDOWN:
1017 button.fsStyle = TBSTYLE_DROPDOWN;
1018 break;
1019
c631abda 1020 default:
9a83f860 1021 wxFAIL_MSG( wxT("unexpected toolbar button kind") );
c631abda 1022 button.fsStyle = TBSTYLE_BUTTON;
97071cdb 1023 break;
c631abda 1024 }
1c383dba
VZ
1025
1026 bitmapId++;
1027 break;
1028 }
2bda0e17 1029
c631abda
VZ
1030 lastWasRadio = isRadio;
1031
1c383dba 1032 i++;
2bda0e17 1033 }
1c383dba 1034
0580ddb2 1035 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, i, (LPARAM)buttons.get()) )
2bda0e17 1036 {
f6bcfd97 1037 wxLogLastError(wxT("TB_ADDBUTTONS"));
2bda0e17 1038 }
89b892a2 1039
2bda0e17 1040
cc260109
VZ
1041 // Adjust controls and stretchable spaces
1042 // --------------------------------------
8a0681f9 1043
cc260109
VZ
1044 // adjust the controls size to fit nicely in the toolbar and compute its
1045 // total size while doing it
1046 m_totalFixedSize = 0;
1047 int toolIndex = 0;
1048 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), toolIndex++ )
1c383dba 1049 {
cc260109 1050 wxToolBarTool * const tool = (wxToolBarTool*)node->GetData();
1c383dba 1051
cc260109 1052 const RECT r = wxGetTBItemRect(GetHwnd(), toolIndex);
bdc72a22 1053
cc260109 1054 if ( !tool->IsControl() )
34e5028f 1055 {
cc260109
VZ
1056 if ( IsVertical() )
1057 m_totalFixedSize += r.bottom - r.top;
1058 else
1059 m_totalFixedSize += r.right - r.left;
1060
1061 continue;
1062 }
34e5028f 1063
cc260109
VZ
1064 if ( IsVertical() )
1065 {
1066 // don't embed controls in the vertical toolbar, this doesn't look
1067 // good and wxGTK doesn't do it neither (and the code below can't
1068 // deal with this case)
34e5028f
VZ
1069 continue;
1070 }
1071
cc260109 1072 wxControl * const control = tool->GetControl();
cdb11cb9
VZ
1073 wxStaticText * const staticText = tool->GetStaticText();
1074
34e5028f 1075 wxSize size = control->GetSize();
cdb11cb9
VZ
1076 wxSize staticTextSize;
1077 if ( staticText )
1078 {
1079 staticTextSize = staticText->GetSize();
1080 staticTextSize.y += 3; // margin between control and its label
1081 }
34e5028f 1082
bdc72a22 1083 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
5b59df83 1084#ifdef TB_SETBUTTONINFO
34e5028f
VZ
1085 // available in headers, now check whether it is available now
1086 // (during run-time)
2a1f999f 1087 if ( wxApp::GetComCtl32Version() >= 471 )
34e5028f
VZ
1088 {
1089 // set the (underlying) separators width to be that of the
1090 // control
1091 TBBUTTONINFO tbbi;
1092 tbbi.cbSize = sizeof(tbbi);
1093 tbbi.dwMask = TBIF_SIZE;
5c519b6c 1094 tbbi.cx = (WORD)size.x;
bfbb0b4c
WS
1095 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO,
1096 tool->GetId(), (LPARAM)&tbbi) )
bdc72a22 1097 {
34e5028f
VZ
1098 // the id is probably invalid?
1099 wxLogLastError(wxT("TB_SETBUTTONINFO"));
bdc72a22 1100 }
34e5028f
VZ
1101 }
1102 else
1103#endif // comctl32.dll 4.71
1104 // TB_SETBUTTONINFO unavailable
1105 {
1106 // try adding several separators to fit the controls width
1107 int widthSep = r.right - r.left;
34e5028f
VZ
1108
1109 TBBUTTON tbb;
1110 wxZeroMemory(tbb);
1111 tbb.idCommand = 0;
1112 tbb.fsState = TBSTATE_ENABLED;
1113 tbb.fsStyle = TBSTYLE_SEP;
1114
1115 size_t nSeparators = size.x / widthSep;
1116 for ( size_t nSep = 0; nSep < nSeparators; nSep++ )
bdc72a22 1117 {
bfbb0b4c 1118 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON,
cc260109 1119 toolIndex, (LPARAM)&tbb) )
bdc72a22 1120 {
34e5028f 1121 wxLogLastError(wxT("TB_INSERTBUTTON"));
bdc72a22
VZ
1122 }
1123
cc260109 1124 toolIndex++;
bdc72a22 1125 }
1c383dba 1126
34e5028f
VZ
1127 // remember the number of separators we used - we'd have to
1128 // delete all of them later
cc260109 1129 tool->SetSeparatorsCount(nSeparators);
34e5028f
VZ
1130
1131 // adjust the controls width to exactly cover the separators
cdb11cb9
VZ
1132 size.x = (nSeparators + 1)*widthSep;
1133 control->SetSize(size.x, wxDefaultCoord);
34e5028f
VZ
1134 }
1135
cdb11cb9
VZ
1136 // position the control itself correctly vertically centering it on the
1137 // icon area of the toolbar
1138 int height = r.bottom - r.top - staticTextSize.y;
1139
1c383dba 1140 int diff = height - size.y;
cdb11cb9 1141 if ( diff < 0 || !HasFlag(wxTB_TEXT) )
1c383dba 1142 {
cdb11cb9
VZ
1143 // not enough room for the static text
1144 if ( staticText )
1145 staticText->Hide();
1146
1147 // recalculate height & diff without the staticText control
1148 height = r.bottom - r.top;
1149 diff = height - size.y;
1150 if ( diff < 0 )
1151 {
1152 // the control is too high, resize to fit
1153 control->SetSize(wxDefaultCoord, height - 2);
1c383dba 1154
cdb11cb9
VZ
1155 diff = 2;
1156 }
1157 }
1158 else // enough space for both the control and the label
1159 {
1160 if ( staticText )
1161 staticText->Show();
1c383dba 1162 }
2bda0e17 1163
cc260109 1164 control->Move(r.left, r.top + (diff + 1) / 2);
cdb11cb9
VZ
1165 if ( staticText )
1166 {
cc260109 1167 staticText->Move(r.left + (size.x - staticTextSize.x)/2,
cdb11cb9
VZ
1168 r.bottom - staticTextSize.y);
1169 }
cc260109
VZ
1170
1171 m_totalFixedSize += size.x;
1c383dba 1172 }
89b892a2 1173
8a0681f9
VZ
1174 // the max index is the "real" number of buttons - i.e. counting even the
1175 // separators which we added just for aligning the controls
cc260109 1176 m_nButtons = toolIndex;
89b892a2 1177
7a976304 1178 if ( !IsVertical() )
8a0681f9
VZ
1179 {
1180 if ( m_maxRows == 0 )
8a0681f9
VZ
1181 // if not set yet, only one row
1182 SetRows(1);
8a0681f9
VZ
1183 }
1184 else if ( m_nButtons > 0 ) // vertical non empty toolbar
1185 {
e49426a2
RD
1186 // if not set yet, have one column
1187 m_maxRows = 1;
f4322df6 1188 SetRows(m_nButtons);
8a0681f9 1189 }
2bda0e17 1190
9f884528 1191 InvalidateBestSize();
2091f5e7 1192 UpdateSize();
c6dcd973 1193
bfbb0b4c 1194 return true;
2bda0e17
KB
1195}
1196
cc260109
VZ
1197void wxToolBar::UpdateStretchableSpacersSize()
1198{
1199 // we can't resize the spacers if TB_SETBUTTONINFO is not supported
1200 if ( wxApp::GetComCtl32Version() < 471 )
1201 return;
1202
1203 // check if we have any stretchable spacers in the first place
1204 unsigned numSpaces = 0;
1205 wxToolBarToolsList::compatibility_iterator node;
1206 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1207 {
1208 wxToolBarTool * const tool = (wxToolBarTool*)node->GetData();
1209 if ( tool->IsStretchableSpace() )
1210 numSpaces++;
1211 }
1212
1213 if ( !numSpaces )
1214 return;
1215
1216 // we do, adjust their size: either distribute the extra size among them or
1217 // reduce their size if there is not enough place for all tools
1218 const int totalSize = IsVertical() ? GetClientSize().y : GetClientSize().x;
1219 const int extraSize = totalSize - m_totalFixedSize;
1220 const int sizeSpacer = extraSize > 0 ? extraSize / numSpaces : 0;
1221
1222 // the last spacer should consume all remaining space if we have too much
1223 // of it (which can be greater than sizeSpacer because of the rounding)
1224 const int sizeLastSpacer = extraSize > 0
1225 ? extraSize - (numSpaces - 1)*sizeSpacer
1226 : 0;
1227
1228 // cumulated offset by which we need to move all the following controls to
1229 // the right: while the toolbar takes care of the normal items, we must
1230 // move the controls manually ourselves to ensure they remain at the
1231 // correct place
1232 int offset = 0;
1233 int toolIndex = 0;
1234 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), toolIndex++ )
1235 {
1236 wxToolBarTool * const tool = (wxToolBarTool*)node->GetData();
1237
1238 if ( tool->IsControl() && offset )
1239 {
1240 tool->MoveBy(offset);
1241
1242 continue;
1243 }
1244
1245 if ( !tool->IsStretchableSpace() )
1246 continue;
1247
1248 const RECT rcOld = wxGetTBItemRect(GetHwnd(), toolIndex);
1249
1250 WinStruct<TBBUTTONINFO> tbbi;
1251 tbbi.dwMask = TBIF_SIZE;
1252 tbbi.cx = --numSpaces ? sizeSpacer : sizeLastSpacer;
1253
1254 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO,
1255 tool->GetId(), (LPARAM)&tbbi) )
1256 {
1257 wxLogLastError(wxT("TB_SETBUTTONINFO"));
1258 }
1259 else
1260 {
1261 // we successfully resized this one, move all the controls after it
1262 // by the corresponding amount (may be positive or negative)
1263 offset += tbbi.cx - (rcOld.right - rcOld.left);
1264 }
1265 }
1266}
1267
1c383dba
VZ
1268// ----------------------------------------------------------------------------
1269// message handlers
1270// ----------------------------------------------------------------------------
1271
0edeeb6d 1272bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id_)
2bda0e17 1273{
0edeeb6d
VZ
1274 // cast to signed is important as we compare this id with (signed) ints in
1275 // FindById() and without the cast we'd get a positive int from a
1276 // "negative" (i.e. > 32767) WORD
1277 const int id = (signed short)id_;
1278
1279 wxToolBarToolBase *tool = FindById(id);
8a0681f9 1280 if ( !tool )
bfbb0b4c 1281 return false;
1c383dba 1282
1bba1946 1283 bool toggled = false; // just to suppress warnings
6bb7cee4 1284
ed7701bd
VS
1285 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
1286
8a0681f9 1287 if ( tool->CanBeToggled() )
1c383dba 1288 {
6bb7cee4
VZ
1289 toggled = (state & TBSTATE_CHECKED) != 0;
1290
0356c259
VZ
1291 // ignore the event when a radio button is released, as this doesn't
1292 // seem to happen at all, and is handled otherwise
6bb7cee4 1293 if ( tool->GetKind() == wxITEM_RADIO && !toggled )
bfbb0b4c 1294 return true;
1c383dba 1295
6bb7cee4
VZ
1296 tool->Toggle(toggled);
1297 UnToggleRadioGroup(tool);
1298 }
8a0681f9 1299
ed7701bd
VS
1300 // Without the two lines of code below, if the toolbar was repainted during
1301 // OnLeftClick(), then it could end up without the tool bitmap temporarily
1302 // (see http://lists.nongnu.org/archive/html/lmi/2008-10/msg00014.html).
1303 // The Update() call bellow ensures that this won't happen, by repainting
1304 // invalidated areas of the toolbar immediately.
1305 //
1306 // To complicate matters, the tool would be drawn in depressed state (this
1307 // code is called when mouse button is released, not pressed). That's not
1308 // ideal, having the tool pressed for the duration of OnLeftClick()
1309 // provides the user with useful visual clue that the app is busy reacting
1310 // to the event. So we manually put the tool into pressed state, handle the
1311 // event and then finally restore tool's original state.
1312 ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state | TBSTATE_PRESSED, 0));
1313 Update();
1314
1315 bool allowLeftClick = OnLeftClick(id, toggled);
1316
5f4250ed
VS
1317 // Restore the unpressed state. Enabled/toggled state might have been
1318 // changed since so take care of it.
1319 if (tool->IsEnabled())
1320 state |= TBSTATE_ENABLED;
1321 else
1322 state &= ~TBSTATE_ENABLED;
1323 if (tool->IsToggled())
1324 state |= TBSTATE_CHECKED;
1325 else
1326 state &= ~TBSTATE_CHECKED;
ed7701bd
VS
1327 ::SendMessage(GetHwnd(), TB_SETSTATE, id, MAKELONG(state, 0));
1328
6bb7cee4
VZ
1329 // OnLeftClick() can veto the button state change - for buttons which
1330 // may be toggled only, of couse
ed7701bd 1331 if ( !allowLeftClick && tool->CanBeToggled() )
f3ba93c1 1332 {
6bb7cee4
VZ
1333 // revert back
1334 tool->Toggle(!toggled);
8a0681f9 1335
0356c259 1336 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(!toggled, 0));
1c383dba
VZ
1337 }
1338
bfbb0b4c 1339 return true;
2bda0e17
KB
1340}
1341
8a0681f9 1342bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl),
fd3f686c 1343 WXLPARAM lParam,
33ac7e6f 1344 WXLPARAM *WXUNUSED(result))
2bda0e17 1345{
a9a0ceca
VZ
1346 LPNMHDR hdr = (LPNMHDR)lParam;
1347 if ( hdr->code == TBN_DROPDOWN )
1348 {
1349 LPNMTOOLBAR tbhdr = (LPNMTOOLBAR)lParam;
1350
1351 wxCommandEvent evt(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED, tbhdr->iItem);
937013e0 1352 if ( HandleWindowEvent(evt) )
a9a0ceca
VZ
1353 {
1354 // Event got handled, don't display default popup menu
1355 return false;
1356 }
1357
1358 const wxToolBarToolBase * const tool = FindById(tbhdr->iItem);
9a83f860 1359 wxCHECK_MSG( tool, false, wxT("drop down message for unknown tool") );
a9a0ceca
VZ
1360
1361 wxMenu * const menu = tool->GetDropdownMenu();
1362 if ( !menu )
1363 return false;
1364
1365 // Display popup menu below button
3c30bbb9
VZ
1366 const RECT r = wxGetTBItemRect(GetHwnd(), GetToolPos(tbhdr->iItem));
1367 if ( r.right )
a9a0ceca
VZ
1368 PopupMenu(menu, r.left, r.bottom);
1369
1370 return true;
1371 }
1372
1373
6a1b3ead
VZ
1374 if( !HasFlag(wxTB_NO_TOOLTIPS) )
1375 {
3bce6687 1376#if wxUSE_TOOLTIPS
6a1b3ead 1377 // First check if this applies to us
2bda0e17 1378
6a1b3ead
VZ
1379 // the tooltips control created by the toolbar is sometimes Unicode, even
1380 // in an ANSI application - this seems to be a bug in comctl32.dll v5
1381 UINT code = hdr->code;
1382 if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) )
1383 return false;
2bda0e17 1384
6a1b3ead
VZ
1385 HWND toolTipWnd = (HWND)::SendMessage(GetHwnd(), TB_GETTOOLTIPS, 0, 0);
1386 if ( toolTipWnd != hdr->hwndFrom )
1387 return false;
2bda0e17 1388
6a1b3ead
VZ
1389 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
1390 int id = (int)ttText->hdr.idFrom;
89b892a2 1391
6a1b3ead
VZ
1392 wxToolBarToolBase *tool = FindById(id);
1393 if ( tool )
1394 return HandleTooltipNotify(code, lParam, tool->GetShortHelp());
3bce6687 1395#else
6a1b3ead
VZ
1396 wxUnusedVar(lParam);
1397#endif
1398 }
abf912c5 1399
bfbb0b4c 1400 return false;
2bda0e17
KB
1401}
1402
1c383dba 1403// ----------------------------------------------------------------------------
8a0681f9 1404// toolbar geometry
1c383dba
VZ
1405// ----------------------------------------------------------------------------
1406
8a0681f9 1407void wxToolBar::SetToolBitmapSize(const wxSize& size)
2bda0e17 1408{
1c383dba
VZ
1409 wxToolBarBase::SetToolBitmapSize(size);
1410
040d3c2e 1411 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
2bda0e17
KB
1412}
1413
8a0681f9 1414void wxToolBar::SetRows(int nRows)
2bda0e17 1415{
8a0681f9
VZ
1416 if ( nRows == m_maxRows )
1417 {
1418 // avoid resizing the frame uselessly
1419 return;
1420 }
1421
1422 // TRUE in wParam means to create at least as many rows, FALSE -
1423 // at most as many
1c383dba
VZ
1424 RECT rect;
1425 ::SendMessage(GetHwnd(), TB_SETROWS,
8a0681f9
VZ
1426 MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)),
1427 (LPARAM) &rect);
1c383dba
VZ
1428
1429 m_maxRows = nRows;
8a0681f9
VZ
1430
1431 UpdateSize();
1432}
1433
1434// The button size is bigger than the bitmap size
1435wxSize wxToolBar::GetToolSize() const
1436{
1437 // TB_GETBUTTONSIZE is supported from version 4.70
5438a566 1438#if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
50165591
CE
1439 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \
1440 && !defined (__DIGITALMARS__)
2a1f999f 1441 if ( wxApp::GetComCtl32Version() >= 470 )
8a0681f9
VZ
1442 {
1443 DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0);
1444
1445 return wxSize(LOWORD(dw), HIWORD(dw));
1446 }
1447 else
1448#endif // comctl32.dll 4.70+
1449 {
1450 // defaults
1451 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
1452 }
2bda0e17
KB
1453}
1454
82c9f85c
VZ
1455static
1456wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools,
1457 size_t index )
a8945eef 1458{
222ed1d6 1459 wxToolBarToolsList::compatibility_iterator current = tools.GetFirst();
a8945eef 1460
489f6cf7 1461 for ( ; current ; current = current->GetNext() )
a8945eef 1462 {
82c9f85c 1463 if ( index == 0 )
a8945eef 1464 return current->GetData();
82c9f85c
VZ
1465
1466 wxToolBarTool *tool = (wxToolBarTool *)current->GetData();
1467 size_t separators = tool->GetSeparatorsCount();
1468
1469 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
1470 // otherwise, skip as many items as the separator count, plus the
1471 // control itself
1472 index -= separators ? separators + 1 : 1;
a8945eef
MB
1473 }
1474
1475 return 0;
1476}
1477
8a0681f9 1478wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
2bda0e17 1479{
8a0681f9
VZ
1480 POINT pt;
1481 pt.x = x;
1482 pt.y = y;
1483 int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt);
97071cdb 1484
37d0bdff
MB
1485 // MBN: when the point ( x, y ) is close to the toolbar border
1486 // TB_HITTEST returns m_nButtons ( not -1 )
1487 if ( index < 0 || (size_t)index >= m_nButtons )
8a0681f9 1488 // it's a separator or there is no tool at all there
d3b9f782 1489 return NULL;
1c383dba 1490
a84f66ef
VZ
1491 // when TB_SETBUTTONINFO is available (both during compile- and run-time),
1492 // we don't use the dummy separators hack
1493#ifdef TB_SETBUTTONINFO
2a1f999f 1494 if ( wxApp::GetComCtl32Version() >= 471 )
a8945eef
MB
1495 {
1496 return m_tools.Item((size_t)index)->GetData();
1497 }
1498 else
a84f66ef 1499#endif // TB_SETBUTTONINFO
a8945eef
MB
1500 {
1501 return GetItemSkippingDummySpacers( m_tools, (size_t) index );
1502 }
2bda0e17
KB
1503}
1504
8a0681f9 1505void wxToolBar::UpdateSize()
2bda0e17 1506{
48a102f9 1507 wxPoint pos = GetPosition();
93489fc8 1508 ::SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0);
48a102f9
JS
1509 if (pos != GetPosition())
1510 Move(pos);
93489fc8 1511
b93328d8
RD
1512 // In case Realize is called after the initial display (IOW the programmer
1513 // may have rebuilt the toolbar) give the frame the option of resizing the
1514 // toolbar to full width again, but only if the parent is a frame and the
1515 // toolbar is managed by the frame. Otherwise assume that some other
1516 // layout mechanism is controlling the toolbar size and leave it alone.
0dba08dd 1517 SendSizeEventToParent();
2bda0e17
KB
1518}
1519
c631abda
VZ
1520// ----------------------------------------------------------------------------
1521// toolbar styles
1522// ---------------------------------------------------------------------------
1523
2588b9c4
VZ
1524// get the TBSTYLE of the given toolbar window
1525long wxToolBar::GetMSWToolbarStyle() const
1526{
1527 return ::SendMessage(GetHwnd(), TB_GETSTYLE, 0, 0L);
1528}
1529
c631abda
VZ
1530void wxToolBar::SetWindowStyleFlag(long style)
1531{
24998710
VZ
1532 // the style bits whose changes force us to recreate the toolbar
1533 static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS;
c631abda 1534
24998710 1535 const long styleOld = GetWindowStyle();
c631abda 1536
24998710 1537 wxToolBarBase::SetWindowStyleFlag(style);
c631abda 1538
24998710
VZ
1539 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1540 // also fatal as we'd then try to recreate the toolbar when it's just being
1541 // created
1542 if ( GetToolsCount() &&
1543 (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) )
1544 {
1545 // to remove the text labels, simply re-realizing the toolbar is enough
1546 // but I don't know of any way to add the text to an existing toolbar
1547 // other than by recreating it entirely
1548 Recreate();
c631abda 1549 }
c631abda
VZ
1550}
1551
1c383dba
VZ
1552// ----------------------------------------------------------------------------
1553// tool state
1554// ----------------------------------------------------------------------------
1555
8a0681f9 1556void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
2bda0e17 1557{
8a0681f9
VZ
1558 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
1559 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0));
2bda0e17
KB
1560}
1561
8a0681f9 1562void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
2bda0e17 1563{
8a0681f9
VZ
1564 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
1565 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0));
2bda0e17
KB
1566}
1567
33ac7e6f 1568void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
088a95f5 1569{
8a0681f9
VZ
1570 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1571 // without, so we really need to delete the button and recreate it here
9a83f860 1572 wxFAIL_MSG( wxT("not implemented") );
2bda0e17
KB
1573}
1574
bbd321ff
RD
1575void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1576{
5c33522f 1577 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
bbd321ff
RD
1578 if ( tool )
1579 {
1580 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1581
1582 tool->SetNormalBitmap(bitmap);
1583 Realize();
f4322df6 1584 }
bbd321ff
RD
1585}
1586
1587void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1588{
5c33522f 1589 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
bbd321ff
RD
1590 if ( tool )
1591 {
1592 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1593
1594 tool->SetDisabledBitmap(bitmap);
1595 Realize();
f4322df6 1596 }
bbd321ff
RD
1597}
1598
1c383dba
VZ
1599// ----------------------------------------------------------------------------
1600// event handlers
1601// ----------------------------------------------------------------------------
2bda0e17
KB
1602
1603// Responds to colour changes, and passes event on to children.
8a0681f9 1604void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event)
2bda0e17 1605{
84c5b38d 1606 wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE));
2bda0e17
KB
1607
1608 // Remap the buttons
8a0681f9 1609 Realize();
2bda0e17 1610
56bd6aac
VZ
1611 // Relayout the toolbar
1612 int nrows = m_maxRows;
1613 m_maxRows = 0; // otherwise SetRows() wouldn't do anything
1614 SetRows(nrows);
1615
2bda0e17
KB
1616 Refresh();
1617
56bd6aac
VZ
1618 // let the event propagate further
1619 event.Skip();
2bda0e17
KB
1620}
1621
8a0681f9 1622void wxToolBar::OnMouseEvent(wxMouseEvent& event)
e6460682 1623{
6ae2a4b7 1624 if ( event.Leaving() )
fe87983b 1625 {
6ae2a4b7
VZ
1626 if ( m_pInTool )
1627 {
1628 OnMouseEnter(wxID_ANY);
1629 m_pInTool = NULL;
1630 }
1631
fe87983b
MB
1632 event.Skip();
1633 return;
1634 }
1635
2334fef6 1636 if ( event.RightDown() )
e6460682 1637 {
2334fef6 1638 // find the tool under the mouse
8d7eaf91 1639 wxCoord x = 0, y = 0;
2334fef6
VZ
1640 event.GetPosition(&x, &y);
1641
1642 wxToolBarToolBase *tool = FindToolForPosition(x, y);
1643 OnRightClick(tool ? tool->GetId() : -1, x, y);
e6460682
JS
1644 }
1645 else
1646 {
42e69d6b 1647 event.Skip();
e6460682
JS
1648 }
1649}
1650
2eb10e2a 1651bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
bdc72a22 1652{
fd6939a6
VZ
1653 // wait until we have some tools
1654 if ( !GetToolsCount() )
1655 return false;
1656
3bb63e5c 1657 // calculate our minor dimension ourselves - we're confusing the standard
7509fa8c 1658 // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks
3c30bbb9
VZ
1659 const RECT r = wxGetTBItemRect(GetHwnd(), 0);
1660 if ( !r.right )
1661 return false;
7509fa8c 1662
3c30bbb9
VZ
1663 int w, h;
1664
1665 if ( IsVertical() )
1666 {
1667 w = r.right - r.left;
1668 if ( m_maxRows )
7509fa8c 1669 {
3c30bbb9 1670 w *= (m_nButtons + m_maxRows - 1)/m_maxRows;
7509fa8c 1671 }
3c30bbb9
VZ
1672 h = HIWORD(lParam);
1673 }
1674 else
1675 {
1676 w = LOWORD(lParam);
1677 if (HasFlag( wxTB_FLAT ))
1678 h = r.bottom - r.top - 3;
7509fa8c 1679 else
3c30bbb9
VZ
1680 h = r.bottom - r.top;
1681 if ( m_maxRows )
7509fa8c 1682 {
3c30bbb9
VZ
1683 // FIXME: hardcoded separator line height...
1684 h += HasFlag(wxTB_NODIVIDER) ? 4 : 6;
1685 h *= m_maxRows;
7509fa8c 1686 }
3c30bbb9 1687 }
7509fa8c 1688
3c30bbb9
VZ
1689 if ( MAKELPARAM(w, h) != lParam )
1690 {
1691 // size really changed
1692 SetSize(w, h);
7509fa8c
VZ
1693 }
1694
cc260109
VZ
1695 UpdateStretchableSpacersSize();
1696
3c30bbb9
VZ
1697 // message processed
1698 return true;
7509fa8c
VZ
1699}
1700
6540d8d2 1701#ifndef __WXWINCE__
cc260109 1702
b93051ef 1703void wxToolBar::MSWEraseRect(wxDC& dc, const wxRect& rectItem)
cc260109 1704{
b93051ef 1705 dc.DrawRectangle(rectItem);
cc260109
VZ
1706}
1707
7509fa8c
VZ
1708bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam)
1709{
cc260109
VZ
1710 // erase any dummy separators which were used only for reserving space in
1711 // the toolbar (either for a control or just for a stretchable space)
7509fa8c 1712
97071cdb 1713 // first of all, are there any controls at all?
222ed1d6 1714 wxToolBarToolsList::compatibility_iterator node;
7509fa8c
VZ
1715 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1716 {
cc260109
VZ
1717 wxToolBarToolBase * const tool = node->GetData();
1718 if ( tool->IsControl() || tool->IsStretchableSpace() )
7509fa8c
VZ
1719 break;
1720 }
1721
1722 if ( !node )
cc260109 1723 {
7509fa8c 1724 // no controls, nothing to erase
bfbb0b4c 1725 return false;
cc260109 1726 }
99be3b7c 1727
7509fa8c
VZ
1728 // prepare the DC on which we'll be drawing
1729 wxClientDC dc(this);
388feca6 1730 dc.SetBrush(GetBackgroundColour());
7509fa8c
VZ
1731 dc.SetPen(*wxTRANSPARENT_PEN);
1732
cc260109
VZ
1733 RECT rcUpdate;
1734 if ( !::GetUpdateRect(GetHwnd(), &rcUpdate, FALSE) )
1735 {
7509fa8c 1736 // nothing to redraw anyhow
bfbb0b4c 1737 return false;
cc260109 1738 }
7509fa8c 1739
cc260109 1740 const wxRect rectUpdate = wxRectFromRECT(rcUpdate);
7509fa8c 1741 dc.SetClippingRegion(rectUpdate);
8a0681f9 1742
7509fa8c
VZ
1743 // draw the toolbar tools, separators &c normally
1744 wxControl::MSWWindowProc(WM_PAINT, wParam, lParam);
1745
1746 // for each control in the toolbar find all the separators intersecting it
1747 // and erase them
1748 //
1749 // NB: this is really the only way to do it as we don't know if a separator
1750 // corresponds to a control (i.e. is a dummy one) or a real one
1751 // otherwise
cc260109
VZ
1752 int toolIndex = 0;
1753 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), toolIndex++ )
7509fa8c 1754 {
cdb11cb9 1755 wxToolBarTool *tool = (wxToolBarTool*)node->GetData();
7509fa8c
VZ
1756 if ( tool->IsControl() )
1757 {
1758 // get the control rect in our client coords
1759 wxControl *control = tool->GetControl();
cdb11cb9 1760 wxStaticText *staticText = tool->GetStaticText();
7509fa8c 1761 wxRect rectCtrl = control->GetRect();
cc260109 1762 wxRect rectStaticText;
cdb11cb9 1763 if ( staticText )
cdb11cb9 1764 rectStaticText = staticText->GetRect();
7509fa8c 1765
cc260109
VZ
1766 if ( !rectCtrl.Intersects(rectUpdate) &&
1767 (!staticText || !rectStaticText.Intersects(rectUpdate)) )
1768 continue;
1769
1770 // iterate over all buttons to find all separators intersecting
1771 // this control
7509fa8c
VZ
1772 TBBUTTON tbb;
1773 int count = ::SendMessage(GetHwnd(), TB_BUTTONCOUNT, 0, 0);
1774 for ( int n = 0; n < count; n++ )
8a0681f9 1775 {
7509fa8c
VZ
1776 // is it a separator?
1777 if ( !::SendMessage(GetHwnd(), TB_GETBUTTON,
1778 n, (LPARAM)&tbb) )
8a0681f9 1779 {
9a83f860 1780 wxLogDebug(wxT("TB_GETBUTTON failed?"));
7509fa8c
VZ
1781
1782 continue;
8a0681f9 1783 }
7509fa8c
VZ
1784
1785 if ( tbb.fsStyle != TBSTYLE_SEP )
1786 continue;
1787
1788 // get the bounding rect of the separator
3c30bbb9
VZ
1789 RECT r = wxGetTBItemRect(GetHwnd(), n);
1790 if ( !r.right )
7509fa8c 1791 continue;
8a0681f9 1792
cc260109 1793 const wxRect rectItem = wxRectFromRECT(r);
99be3b7c 1794
cc260109
VZ
1795 // does it intersect the update region at all?
1796 if ( !rectUpdate.Intersects(rectItem) )
1797 continue;
cdb11cb9 1798
cc260109
VZ
1799 // does it intersect the control itself or its label?
1800 //
1801 // if it does, refresh it so it's redrawn on top of the
1802 // background
921dd444 1803 if ( rectCtrl.Intersects(rectItem) )
921dd444 1804 control->Refresh(false);
cc260109 1805 else if ( staticText && rectStaticText.Intersects(rectItem) )
cdb11cb9 1806 staticText->Refresh(false);
cc260109
VZ
1807 else
1808 continue;
1809
b93051ef 1810 MSWEraseRect(dc, rectItem);
8a0681f9 1811 }
8a0681f9 1812 }
cc260109
VZ
1813 else if ( tool->IsStretchableSpace() )
1814 {
1815 const wxRect
1816 rectItem = wxRectFromRECT(wxGetTBItemRect(GetHwnd(), toolIndex));
1817
1818 if ( rectUpdate.Intersects(rectItem) )
b93051ef 1819 MSWEraseRect(dc, rectItem);
cc260109 1820 }
bdc72a22 1821 }
7509fa8c 1822
bfbb0b4c 1823 return true;
7509fa8c 1824}
6540d8d2 1825#endif // __WXWINCE__
7509fa8c 1826
2eb10e2a 1827void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
7509fa8c
VZ
1828{
1829 wxCoord x = GET_X_LPARAM(lParam),
1830 y = GET_Y_LPARAM(lParam);
1831 wxToolBarToolBase* tool = FindToolForPosition( x, y );
1832
6ae2a4b7
VZ
1833 // has the current tool changed?
1834 if ( tool != m_pInTool )
7509fa8c
VZ
1835 {
1836 m_pInTool = tool;
6ae2a4b7 1837 OnMouseEnter(tool ? tool->GetId() : wxID_ANY);
7509fa8c
VZ
1838 }
1839}
a8945eef 1840
c140b7e7 1841WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
7509fa8c
VZ
1842{
1843 switch ( nMsg )
1844 {
7509fa8c
VZ
1845 case WM_MOUSEMOVE:
1846 // we don't handle mouse moves, so always pass the message to
4012b958 1847 // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter)
7509fa8c
VZ
1848 HandleMouseMove(wParam, lParam);
1849 break;
a8945eef 1850
4012b958
JS
1851 case WM_SIZE:
1852 if ( HandleSize(wParam, lParam) )
1853 return 0;
1854 break;
1855
1856#ifndef __WXWINCE__
7509fa8c 1857 case WM_PAINT:
1c2b921a
VZ
1858 // refreshing the controls in the toolbar inside a composite window
1859 // results in an endless stream of WM_PAINT messages -- and seems
1860 // to be unnecessary anyhow as everything works just fine without
1861 // any special workarounds in this case
1862 if ( !IsDoubleBuffered() && HandlePaint(wParam, lParam) )
7509fa8c 1863 return 0;
97071cdb 1864 break;
1c2b921a 1865#endif // __WXWINCE__
a8945eef 1866 }
bdc72a22 1867
8a0681f9 1868 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
bdc72a22
VZ
1869}
1870
1c383dba
VZ
1871// ----------------------------------------------------------------------------
1872// private functions
1873// ----------------------------------------------------------------------------
1874
fb7e87ef
VZ
1875#ifdef wxREMAP_BUTTON_COLOURS
1876
566fb299 1877WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height)
2bda0e17 1878{
566fb299 1879 MemoryHDC hdcMem;
56bd6aac 1880
566fb299 1881 if ( !hdcMem )
5e68f8f3 1882 {
9a83f860 1883 wxLogLastError(wxT("CreateCompatibleDC"));
566fb299
VZ
1884
1885 return bitmap;
5e68f8f3 1886 }
56bd6aac 1887
566fb299 1888 SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap);
56bd6aac 1889
566fb299 1890 if ( !bmpInHDC )
5e68f8f3 1891 {
9a83f860 1892 wxLogLastError(wxT("SelectObject"));
566fb299
VZ
1893
1894 return bitmap;
1895 }
56bd6aac 1896
90c1530a
VZ
1897 wxCOLORMAP *cmap = wxGetStdColourMap();
1898
684c68fd
VZ
1899 for ( int i = 0; i < width; i++ )
1900 {
1901 for ( int j = 0; j < height; j++ )
1902 {
1903 COLORREF pixel = ::GetPixel(hdcMem, i, j);
1904
90c1530a 1905 for ( size_t k = 0; k < wxSTD_COL_MAX; k++ )
684c68fd 1906 {
90c1530a 1907 COLORREF col = cmap[k].from;
684c68fd
VZ
1908 if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 &&
1909 abs(GetGValue(pixel) - GetGValue(col)) < 10 &&
1910 abs(GetBValue(pixel) - GetBValue(col)) < 10 )
1911 {
fb7e87ef
VZ
1912 if ( cmap[k].to != pixel )
1913 ::SetPixel(hdcMem, i, j, cmap[k].to);
684c68fd
VZ
1914 break;
1915 }
1916 }
1917 }
1918 }
3a3898f8
VZ
1919
1920 return bitmap;
566fb299 1921}
2bda0e17 1922
fb7e87ef
VZ
1923#endif // wxREMAP_BUTTON_COLOURS
1924
97071cdb 1925#endif // wxUSE_TOOLBAR