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