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