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