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