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