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