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