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