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