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