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