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