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