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