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