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