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