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