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