]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tbar95.cpp
applied doc patch for wxPostEvent().
[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 // Messages
105 #ifndef TB_GETSTYLE
106 #define TB_SETSTYLE (WM_USER + 56)
107 #define TB_GETSTYLE (WM_USER + 57)
108 #endif
109
110 #ifndef TB_HITTEST
111 #define TB_HITTEST (WM_USER + 69)
112 #endif
113
114 // these values correspond to those used by comctl32.dll
115 #define DEFAULTBITMAPX 16
116 #define DEFAULTBITMAPY 15
117 #define DEFAULTBUTTONX 24
118 #define DEFAULTBUTTONY 24
119 #define DEFAULTBARHEIGHT 27
120
121 // ----------------------------------------------------------------------------
122 // wxWin macros
123 // ----------------------------------------------------------------------------
124
125 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
126
127 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
128 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent)
129 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged)
130 END_EVENT_TABLE()
131
132 // ----------------------------------------------------------------------------
133 // private classes
134 // ----------------------------------------------------------------------------
135
136 class wxToolBarTool : public wxToolBarToolBase
137 {
138 public:
139 wxToolBarTool(wxToolBar *tbar,
140 int id,
141 const wxBitmap& bitmap1,
142 const wxBitmap& bitmap2,
143 bool toggle,
144 wxObject *clientData,
145 const wxString& shortHelpString,
146 const wxString& longHelpString)
147 : wxToolBarToolBase(tbar, id, bitmap1, bitmap2, toggle,
148 clientData, shortHelpString, longHelpString)
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 // set/get the number of separators which we use to cover the space used by
160 // a control in the toolbar
161 void SetSeparatorsCount(size_t count) { m_nSepCount = count; }
162 size_t GetSeparatorsCount() const { return m_nSepCount; }
163
164 private:
165 size_t m_nSepCount;
166 };
167
168
169 // ============================================================================
170 // implementation
171 // ============================================================================
172
173 // ----------------------------------------------------------------------------
174 // wxToolBarTool
175 // ----------------------------------------------------------------------------
176
177 wxToolBarToolBase *wxToolBar::CreateTool(int id,
178 const wxBitmap& bitmap1,
179 const wxBitmap& bitmap2,
180 bool toggle,
181 wxObject *clientData,
182 const wxString& shortHelpString,
183 const wxString& longHelpString)
184 {
185 return new wxToolBarTool(this, id, bitmap1, bitmap2, toggle,
186 clientData, shortHelpString, longHelpString);
187 }
188
189 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
190 {
191 return new wxToolBarTool(this, control);
192 }
193
194 // ----------------------------------------------------------------------------
195 // wxToolBar construction
196 // ----------------------------------------------------------------------------
197
198 void wxToolBar::Init()
199 {
200 m_hBitmap = 0;
201
202 m_nButtons = 0;
203
204 m_defaultWidth = DEFAULTBITMAPX;
205 m_defaultHeight = DEFAULTBITMAPY;
206
207 m_pInTool = 0;
208 }
209
210 bool wxToolBar::Create(wxWindow *parent,
211 wxWindowID id,
212 const wxPoint& pos,
213 const wxSize& size,
214 long style,
215 const wxString& name)
216 {
217 // toolbars never have border, giving one to them results in broken
218 // appearance
219 style &= ~wxBORDER_MASK;
220 style |= wxBORDER_NONE;
221
222 // common initialisation
223 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
224 return FALSE;
225
226 // prepare flags
227 DWORD msflags = 0; // WS_VISIBLE | WS_CHILD always included
228
229 if ( style & wxCLIP_SIBLINGS )
230 msflags |= WS_CLIPSIBLINGS;
231
232 #ifdef TBSTYLE_TOOLTIPS
233 msflags |= TBSTYLE_TOOLTIPS;
234 #endif
235
236 if (style & wxTB_FLAT)
237 {
238 // static as it doesn't change during the program lifetime
239 static int s_verComCtl = wxTheApp->GetComCtl32Version();
240
241 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
242 // style with 6.00 (part of Windows XP) leads to the toolbar with
243 // incorrect background colour - and not using it still results in the
244 // correct (flat) toolbar, so don't use it there
245 if ( s_verComCtl > 400 && s_verComCtl < 600 )
246 {
247 msflags |= TBSTYLE_FLAT | TBSTYLE_TRANSPARENT;
248 }
249 }
250
251 // MSW-specific initialisation
252 if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME, msflags) )
253 return FALSE;
254
255 // toolbar-specific post initialisation
256 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
257
258 // set up the colors and fonts
259 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR));
260 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
261
262 // position it
263 int x = pos.x;
264 int y = pos.y;
265 int width = size.x;
266 int height = size.y;
267
268 if (width <= 0)
269 width = 100;
270 if (height <= 0)
271 height = m_defaultHeight;
272 if (x < 0)
273 x = 0;
274 if (y < 0)
275 y = 0;
276
277 SetSize(x, y, width, height);
278
279 return TRUE;
280 }
281
282 wxToolBar::~wxToolBar()
283 {
284 // we must refresh the frame size when the toolbar is deleted but the frame
285 // is not - otherwise toolbar leaves a hole in the place it used to occupy
286 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
287 if ( frame && !frame->IsBeingDeleted() )
288 {
289 frame->SendSizeEvent();
290 }
291
292 if ( m_hBitmap )
293 {
294 ::DeleteObject((HBITMAP) m_hBitmap);
295 }
296 }
297
298 // ----------------------------------------------------------------------------
299 // adding/removing tools
300 // ----------------------------------------------------------------------------
301
302 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
303 wxToolBarToolBase *tool)
304 {
305 // nothing special to do here - we really create the toolbar buttons in
306 // Realize() later
307 tool->Attach(this);
308
309 return TRUE;
310 }
311
312 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
313 {
314 // the main difficulty we have here is with the controls in the toolbars:
315 // as we (sometimes) use several separators to cover up the space used by
316 // them, the indices are not the same for us and the toolbar
317
318 // first determine the position of the first button to delete: it may be
319 // different from pos if we use several separators to cover the space used
320 // by a control
321 wxToolBarToolsList::Node *node;
322 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
323 {
324 wxToolBarToolBase *tool2 = node->GetData();
325 if ( tool2 == tool )
326 {
327 // let node point to the next node in the list
328 node = node->GetNext();
329
330 break;
331 }
332
333 if ( tool2->IsControl() )
334 {
335 pos += ((wxToolBarTool *)tool2)->GetSeparatorsCount() - 1;
336 }
337 }
338
339 // now determine the number of buttons to delete and the area taken by them
340 size_t nButtonsToDelete = 1;
341
342 // get the size of the button we're going to delete
343 RECT r;
344 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, pos, (LPARAM)&r) )
345 {
346 wxLogLastError(_T("TB_GETITEMRECT"));
347 }
348
349 int width = r.right - r.left;
350
351 if ( tool->IsControl() )
352 {
353 nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount();
354
355 width *= nButtonsToDelete;
356 }
357
358 // do delete all buttons
359 m_nButtons -= nButtonsToDelete;
360 while ( nButtonsToDelete-- > 0 )
361 {
362 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) )
363 {
364 wxLogLastError(wxT("TB_DELETEBUTTON"));
365
366 return FALSE;
367 }
368 }
369
370 tool->Detach();
371
372 // and finally reposition all the controls after this button (the toolbar
373 // takes care of all normal items)
374 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
375 {
376 wxToolBarToolBase *tool2 = node->GetData();
377 if ( tool2->IsControl() )
378 {
379 int x;
380 wxControl *control = tool2->GetControl();
381 control->GetPosition(&x, NULL);
382 control->Move(x - width, -1);
383 }
384 }
385
386 return TRUE;
387 }
388
389 bool wxToolBar::Realize()
390 {
391 size_t nTools = GetToolsCount();
392 if ( nTools == 0 )
393 {
394 // nothing to do
395 return TRUE;
396 }
397
398 bool isVertical = (GetWindowStyle() & wxTB_VERTICAL) != 0;
399
400 // First, add the bitmap: we use one bitmap for all toolbar buttons
401 // ----------------------------------------------------------------
402
403 // if we already have a bitmap, we'll replace the existing one - otherwise
404 // we'll install a new one
405 HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap;
406
407 int totalBitmapWidth = (int)(m_defaultWidth * nTools);
408 int totalBitmapHeight = (int)m_defaultHeight;
409
410 // Create a bitmap and copy all the tool bitmaps to it
411 #if USE_BITMAP_MASKS
412 wxMemoryDC dcAllButtons;
413 wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight);
414 dcAllButtons.SelectObject(bitmap);
415 dcAllButtons.SetBackground(*wxLIGHT_GREY_BRUSH);
416 dcAllButtons.Clear();
417
418 m_hBitmap = bitmap.GetHBITMAP();
419 HBITMAP hBitmap = (HBITMAP)m_hBitmap;
420 #else // !USE_BITMAP_MASKS
421 HBITMAP hBitmap = ::CreateCompatibleBitmap(ScreenHDC(),
422 totalBitmapWidth,
423 totalBitmapHeight);
424 if ( !hBitmap )
425 {
426 wxLogLastError(_T("CreateCompatibleBitmap"));
427
428 return FALSE;
429 }
430
431 m_hBitmap = (WXHBITMAP)hBitmap;
432
433 HDC memoryDC = ::CreateCompatibleDC(NULL);
434 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(memoryDC, hBitmap);
435
436 HDC memoryDC2 = ::CreateCompatibleDC(NULL);
437 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
438
439 // the button position
440 wxCoord x = 0;
441
442 // the number of buttons (not separators)
443 int nButtons = 0;
444
445 wxToolBarToolsList::Node *node = m_tools.GetFirst();
446 while ( node )
447 {
448 wxToolBarToolBase *tool = node->GetData();
449 if ( tool->IsButton() )
450 {
451 const wxBitmap& bmp = tool->GetBitmap1();
452 if ( bmp.Ok() )
453 {
454 #if USE_BITMAP_MASKS
455 // notice the last parameter: do use mask
456 dcAllButtons.DrawBitmap(tool->GetBitmap1(), x, 0, TRUE);
457 #else // !USE_BITMAP_MASKS
458 HBITMAP hbmp = GetHbitmapOf(bmp);
459 HBITMAP oldBitmap2 = (HBITMAP)::SelectObject(memoryDC2, hbmp);
460 if ( !BitBlt(memoryDC, x, 0, m_defaultWidth, m_defaultHeight,
461 memoryDC2, 0, 0, SRCCOPY) )
462 {
463 wxLogLastError(wxT("BitBlt"));
464 }
465
466 ::SelectObject(memoryDC2, oldBitmap2);
467 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
468 }
469 else
470 {
471 wxFAIL_MSG( _T("invalid tool button bitmap") );
472 }
473
474 // still inc width and number of buttons because otherwise the
475 // subsequent buttons will all be shifted which is rather confusing
476 // (and like this you'd see immediately which bitmap was bad)
477 x += m_defaultWidth;
478 nButtons++;
479 }
480
481 node = node->GetNext();
482 }
483
484 #if USE_BITMAP_MASKS
485 dcAllButtons.SelectObject(wxNullBitmap);
486
487 // don't delete this HBITMAP!
488 bitmap.SetHBITMAP(0);
489 #else // !USE_BITMAP_MASKS
490 ::SelectObject(memoryDC, oldBitmap);
491 ::DeleteDC(memoryDC);
492 ::DeleteDC(memoryDC2);
493 #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS
494
495 // Map to system colours
496 hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap,
497 totalBitmapWidth, totalBitmapHeight);
498
499 int bitmapId = 0;
500
501 bool addBitmap = TRUE;
502
503 if ( oldToolBarBitmap )
504 {
505 #ifdef TB_REPLACEBITMAP
506 if ( wxTheApp->GetComCtl32Version() >= 400 )
507 {
508 TBREPLACEBITMAP replaceBitmap;
509 replaceBitmap.hInstOld = NULL;
510 replaceBitmap.hInstNew = NULL;
511 replaceBitmap.nIDOld = (UINT) oldToolBarBitmap;
512 replaceBitmap.nIDNew = (UINT) hBitmap;
513 replaceBitmap.nButtons = nButtons;
514 if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP,
515 0, (LPARAM) &replaceBitmap) )
516 {
517 wxFAIL_MSG(wxT("Could not replace the old bitmap"));
518 }
519
520 ::DeleteObject(oldToolBarBitmap);
521
522 // already done
523 addBitmap = FALSE;
524 }
525 else
526 #endif // TB_REPLACEBITMAP
527 {
528 // we can't replace the old bitmap, so we will add another one
529 // (awfully inefficient, but what else to do?) and shift the bitmap
530 // indices accordingly
531 addBitmap = TRUE;
532
533 bitmapId = m_nButtons;
534 }
535
536 // Now delete all the buttons
537 for ( size_t pos = 0; pos < m_nButtons; pos++ )
538 {
539 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) )
540 {
541 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
542 }
543 }
544
545 }
546
547 if ( addBitmap ) // no old bitmap or we can't replace it
548 {
549 TBADDBITMAP addBitmap;
550 addBitmap.hInst = 0;
551 addBitmap.nID = (UINT) hBitmap;
552 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP,
553 (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 )
554 {
555 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
556 }
557 }
558
559 // Next add the buttons and separators
560 // -----------------------------------
561
562 TBBUTTON *buttons = new TBBUTTON[nTools];
563
564 // this array will hold the indices of all controls in the toolbar
565 wxArrayInt controlIds;
566
567 int i = 0;
568 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
569 {
570 wxToolBarToolBase *tool = node->GetData();
571
572 // don't add separators to the vertical toolbar - looks ugly
573 if ( isVertical && tool->IsSeparator() )
574 continue;
575
576 TBBUTTON& button = buttons[i];
577
578 wxZeroMemory(button);
579
580 switch ( tool->GetStyle() )
581 {
582 case wxTOOL_STYLE_CONTROL:
583 button.idCommand = tool->GetId();
584 // fall through: create just a separator too
585
586 case wxTOOL_STYLE_SEPARATOR:
587 button.fsState = TBSTATE_ENABLED;
588 button.fsStyle = TBSTYLE_SEP;
589 break;
590
591 case wxTOOL_STYLE_BUTTON:
592 button.iBitmap = bitmapId;
593 button.idCommand = tool->GetId();
594
595 if ( tool->IsEnabled() )
596 button.fsState |= TBSTATE_ENABLED;
597 if ( tool->IsToggled() )
598 button.fsState |= TBSTATE_CHECKED;
599
600 button.fsStyle = tool->CanBeToggled() ? TBSTYLE_CHECK
601 : TBSTYLE_BUTTON;
602
603 bitmapId++;
604 break;
605 }
606
607 i++;
608 }
609
610 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS,
611 (WPARAM)i, (LPARAM)buttons) )
612 {
613 wxLogLastError(wxT("TB_ADDBUTTONS"));
614 }
615
616 delete [] buttons;
617
618 // Deal with the controls finally
619 // ------------------------------
620
621 // adjust the controls size to fit nicely in the toolbar
622 int y = 0;
623 size_t index = 0;
624 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ )
625 {
626 wxToolBarToolBase *tool = node->GetData();
627
628 // we calculate the running y coord for vertical toolbars so we need to
629 // get the items size for all items but for the horizontal ones we
630 // don't need to deal with the non controls
631 bool isControl = tool->IsControl();
632 if ( !isControl && !isVertical )
633 continue;
634
635 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
636 // latter only appeared in v4.70 of comctl32.dll
637 RECT r;
638 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT,
639 index, (LPARAM)(LPRECT)&r) )
640 {
641 wxLogLastError(wxT("TB_GETITEMRECT"));
642 }
643
644 if ( !isControl )
645 {
646 // can only be control if isVertical
647 y += r.bottom - r.top;
648
649 continue;
650 }
651
652 wxControl *control = tool->GetControl();
653
654 wxSize size = control->GetSize();
655
656 // the position of the leftmost controls corner
657 int left = -1;
658
659 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
660 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
661 // available in headers, now check whether it is available now
662 // (during run-time)
663 if ( wxTheApp->GetComCtl32Version() >= 471 )
664 {
665 // set the (underlying) separators width to be that of the
666 // control
667 TBBUTTONINFO tbbi;
668 tbbi.cbSize = sizeof(tbbi);
669 tbbi.dwMask = TBIF_SIZE;
670 tbbi.cx = size.x;
671 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO,
672 tool->GetId(), (LPARAM)&tbbi) )
673 {
674 // the id is probably invalid?
675 wxLogLastError(wxT("TB_SETBUTTONINFO"));
676 }
677 }
678 else
679 #endif // comctl32.dll 4.71
680 // TB_SETBUTTONINFO unavailable
681 {
682 // try adding several separators to fit the controls width
683 int widthSep = r.right - r.left;
684 left = r.left;
685
686 TBBUTTON tbb;
687 wxZeroMemory(tbb);
688 tbb.idCommand = 0;
689 tbb.fsState = TBSTATE_ENABLED;
690 tbb.fsStyle = TBSTYLE_SEP;
691
692 size_t nSeparators = size.x / widthSep;
693 for ( size_t nSep = 0; nSep < nSeparators; nSep++ )
694 {
695 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON,
696 index, (LPARAM)&tbb) )
697 {
698 wxLogLastError(wxT("TB_INSERTBUTTON"));
699 }
700
701 index++;
702 }
703
704 // remember the number of separators we used - we'd have to
705 // delete all of them later
706 ((wxToolBarTool *)tool)->SetSeparatorsCount(nSeparators);
707
708 // adjust the controls width to exactly cover the separators
709 control->SetSize((nSeparators + 1)*widthSep, -1);
710 }
711
712 // position the control itself correctly vertically
713 int height = r.bottom - r.top;
714 int diff = height - size.y;
715 if ( diff < 0 )
716 {
717 // the control is too high, resize to fit
718 control->SetSize(-1, height - 2);
719
720 diff = 2;
721 }
722
723 int top;
724 if ( isVertical )
725 {
726 left = 0;
727 top = y;
728
729 y += height + 2*GetMargins().y;
730 }
731 else // horizontal toolbar
732 {
733 if ( left == -1 )
734 left = r.left;
735
736 top = r.top;
737 }
738
739 control->Move(left, top + (diff + 1) / 2);
740 }
741
742 // the max index is the "real" number of buttons - i.e. counting even the
743 // separators which we added just for aligning the controls
744 m_nButtons = index;
745
746 if ( !isVertical )
747 {
748 if ( m_maxRows == 0 )
749 {
750 // if not set yet, only one row
751 SetRows(1);
752 }
753 }
754 else if ( m_nButtons > 0 ) // vertical non empty toolbar
755 {
756 if ( m_maxRows == 0 )
757 {
758 // if not set yet, have one column
759 SetRows(m_nButtons);
760 }
761 }
762
763 return TRUE;
764 }
765
766 // ----------------------------------------------------------------------------
767 // message handlers
768 // ----------------------------------------------------------------------------
769
770 bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id)
771 {
772 wxToolBarToolBase *tool = FindById((int)id);
773 if ( !tool )
774 return FALSE;
775
776 if ( tool->CanBeToggled() )
777 {
778 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
779 tool->Toggle((state & TBSTATE_CHECKED) != 0);
780 }
781
782 bool toggled = tool->IsToggled();
783
784 // OnLeftClick() can veto the button state change - for buttons which may
785 // be toggled only, of couse
786 if ( !OnLeftClick((int)id, toggled) && tool->CanBeToggled() )
787 {
788 // revert back
789 toggled = !toggled;
790 tool->SetToggle(toggled);
791
792 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(toggled, 0));
793 }
794
795 return TRUE;
796 }
797
798 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl),
799 WXLPARAM lParam,
800 WXLPARAM *WXUNUSED(result))
801 {
802 // First check if this applies to us
803 NMHDR *hdr = (NMHDR *)lParam;
804
805 // the tooltips control created by the toolbar is sometimes Unicode, even
806 // in an ANSI application - this seems to be a bug in comctl32.dll v5
807 int code = (int)hdr->code;
808 if ( (code != TTN_NEEDTEXTA) && (code != TTN_NEEDTEXTW) )
809 return FALSE;
810
811 HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0);
812 if ( toolTipWnd != hdr->hwndFrom )
813 return FALSE;
814
815 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
816 int id = (int)ttText->hdr.idFrom;
817
818 wxToolBarToolBase *tool = FindById(id);
819 if ( !tool )
820 return FALSE;
821
822 const wxString& help = tool->GetShortHelp();
823
824 if ( !help.IsEmpty() )
825 {
826 if ( code == TTN_NEEDTEXTA )
827 {
828 ttText->lpszText = (wxChar *)help.c_str();
829 }
830 else
831 {
832 #if wxUSE_UNICODE
833 ttText->lpszText = (wxChar *)help.c_str();
834 #else
835 // VZ: I don't know why it happens, but the versions of
836 // comctl32.dll starting from 4.70 sometimes send TTN_NEEDTEXTW
837 // even to ANSI programs (normally, this message is supposed
838 // to be sent to Unicode programs only) - hence we need to
839 // handle it as well, otherwise no tooltips will be shown in
840 // this case
841
842 size_t lenAnsi = help.Len();
843 #if defined( __MWERKS__ ) || defined( __CYGWIN__ )
844 // MetroWerks doesn't like calling mbstowcs with NULL argument
845 // neither Cygwin does
846 size_t lenUnicode = 2*lenAnsi;
847 #else
848 size_t lenUnicode = mbstowcs(NULL, help, lenAnsi);
849 #endif
850
851 // using the pointer of right type avoids us doing all sorts of
852 // pointer arithmetics ourselves
853 wchar_t *dst = (wchar_t *)ttText->szText,
854 *pwz = new wchar_t[lenUnicode + 1];
855 mbstowcs(pwz, help, lenAnsi + 1);
856 memcpy(dst, pwz, lenUnicode*sizeof(wchar_t));
857
858 // put the terminating _wide_ NUL
859 dst[lenUnicode] = 0;
860
861 delete [] pwz;
862 #endif
863 }
864 }
865
866 return TRUE;
867 }
868
869 // ----------------------------------------------------------------------------
870 // toolbar geometry
871 // ----------------------------------------------------------------------------
872
873 void wxToolBar::SetToolBitmapSize(const wxSize& size)
874 {
875 wxToolBarBase::SetToolBitmapSize(size);
876
877 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
878 }
879
880 void wxToolBar::SetRows(int nRows)
881 {
882 if ( nRows == m_maxRows )
883 {
884 // avoid resizing the frame uselessly
885 return;
886 }
887
888 // TRUE in wParam means to create at least as many rows, FALSE -
889 // at most as many
890 RECT rect;
891 ::SendMessage(GetHwnd(), TB_SETROWS,
892 MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)),
893 (LPARAM) &rect);
894
895 m_maxRows = nRows;
896
897 UpdateSize();
898 }
899
900 // The button size is bigger than the bitmap size
901 wxSize wxToolBar::GetToolSize() const
902 {
903 // TB_GETBUTTONSIZE is supported from version 4.70
904 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
905 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
906 if ( wxTheApp->GetComCtl32Version() >= 470 )
907 {
908 DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0);
909
910 return wxSize(LOWORD(dw), HIWORD(dw));
911 }
912 else
913 #endif // comctl32.dll 4.70+
914 {
915 // defaults
916 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
917 }
918 }
919
920 static
921 wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools,
922 size_t index )
923 {
924 wxToolBarToolsList::Node* current = tools.GetFirst();
925
926 for ( ; current != 0; current = current->GetNext() )
927 {
928 if ( index == 0 )
929 return current->GetData();
930
931 wxToolBarTool *tool = (wxToolBarTool *)current->GetData();
932 size_t separators = tool->GetSeparatorsCount();
933
934 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
935 // otherwise, skip as many items as the separator count, plus the
936 // control itself
937 index -= separators ? separators + 1 : 1;
938 }
939
940 return 0;
941 }
942
943 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
944 {
945 POINT pt;
946 pt.x = x;
947 pt.y = y;
948 int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt);
949 // MBN: when the point ( x, y ) is close to the toolbar border
950 // TB_HITTEST returns m_nButtons ( not -1 )
951 if ( index < 0 || (size_t)index >= m_nButtons )
952 {
953 // it's a separator or there is no tool at all there
954 return (wxToolBarToolBase *)NULL;
955 }
956
957 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
958 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
959 if ( wxTheApp->GetComCtl32Version() >= 471 )
960 {
961 return m_tools.Item((size_t)index)->GetData();
962 }
963 else
964 #endif
965 {
966 return GetItemSkippingDummySpacers( m_tools, (size_t) index );
967 }
968 }
969
970 void wxToolBar::UpdateSize()
971 {
972 // the toolbar size changed
973 SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0);
974
975 // we must also refresh the frame after the toolbar size (possibly) changed
976 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
977 if ( frame )
978 {
979 frame->SendSizeEvent();
980 }
981 }
982
983 // ----------------------------------------------------------------------------
984 // tool state
985 // ----------------------------------------------------------------------------
986
987 void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
988 {
989 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
990 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0));
991 }
992
993 void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
994 {
995 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
996 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0));
997 }
998
999 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1000 {
1001 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1002 // without, so we really need to delete the button and recreate it here
1003 wxFAIL_MSG( _T("not implemented") );
1004 }
1005
1006 // ----------------------------------------------------------------------------
1007 // event handlers
1008 // ----------------------------------------------------------------------------
1009
1010 // Responds to colour changes, and passes event on to children.
1011 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event)
1012 {
1013 wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE));
1014
1015 // Remap the buttons
1016 Realize();
1017
1018 // Relayout the toolbar
1019 int nrows = m_maxRows;
1020 m_maxRows = 0; // otherwise SetRows() wouldn't do anything
1021 SetRows(nrows);
1022
1023 Refresh();
1024
1025 // let the event propagate further
1026 event.Skip();
1027 }
1028
1029 void wxToolBar::OnMouseEvent(wxMouseEvent& event)
1030 {
1031 if (event.RightDown())
1032 {
1033 // For now, we don't have an id. Later we could
1034 // try finding the tool.
1035 OnRightClick((int)-1, event.GetX(), event.GetY());
1036 }
1037 else
1038 {
1039 event.Skip();
1040 }
1041 }
1042
1043 long wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1044 {
1045 if ( nMsg == WM_SIZE )
1046 {
1047 // calculate our minor dimenstion ourselves - we're confusing the
1048 // standard logic (TB_AUTOSIZE) with our horizontal toolbars and other
1049 // hacks
1050 RECT r;
1051 if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT, 0, (LPARAM)&r) )
1052 {
1053 int w, h;
1054
1055 if ( GetWindowStyle() & wxTB_VERTICAL )
1056 {
1057 w = r.right - r.left;
1058 if ( m_maxRows )
1059 {
1060 w *= (m_nButtons + m_maxRows - 1)/m_maxRows;
1061 }
1062 h = HIWORD(lParam);
1063 }
1064 else
1065 {
1066 w = LOWORD(lParam);
1067 h = r.bottom - r.top;
1068 if ( m_maxRows )
1069 {
1070 h += 6; // FIXME: this is the separator line height...
1071 h *= m_maxRows;
1072 }
1073 }
1074
1075 if ( MAKELPARAM(w, h) != lParam )
1076 {
1077 // size really changed
1078 SetSize(w, h);
1079 }
1080
1081 // message processed
1082 return 0;
1083 }
1084 }
1085 else if ( nMsg == WM_MOUSEMOVE )
1086 {
1087 wxCoord x = GET_X_LPARAM(lParam), y = GET_Y_LPARAM(lParam);
1088 wxToolBarToolBase* tool = FindToolForPosition( x, y );
1089
1090 // cursor left current tool
1091 if( tool != m_pInTool && !tool )
1092 {
1093 m_pInTool = 0;
1094 OnMouseEnter( -1 );
1095 }
1096
1097 // cursor entered a tool
1098 if( tool != m_pInTool && tool )
1099 {
1100 m_pInTool = tool;
1101 OnMouseEnter( tool->GetId() );
1102 }
1103
1104 // we don't handle mouse moves, so fall through
1105 // to wxControl::MSWWindowProc
1106 }
1107
1108 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
1109 }
1110
1111 // ----------------------------------------------------------------------------
1112 // private functions
1113 // ----------------------------------------------------------------------------
1114
1115 WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height)
1116 {
1117 MemoryHDC hdcMem;
1118
1119 if ( !hdcMem )
1120 {
1121 wxLogLastError(_T("CreateCompatibleDC"));
1122
1123 return bitmap;
1124 }
1125
1126 SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap);
1127
1128 if ( !bmpInHDC )
1129 {
1130 wxLogLastError(_T("SelectObject"));
1131
1132 return bitmap;
1133 }
1134
1135 wxCOLORMAP *cmap = wxGetStdColourMap();
1136
1137 for ( int i = 0; i < width; i++ )
1138 {
1139 for ( int j = 0; j < height; j++ )
1140 {
1141 COLORREF pixel = ::GetPixel(hdcMem, i, j);
1142
1143 for ( size_t k = 0; k < wxSTD_COL_MAX; k++ )
1144 {
1145 COLORREF col = cmap[k].from;
1146 if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 &&
1147 abs(GetGValue(pixel) - GetGValue(col)) < 10 &&
1148 abs(GetBValue(pixel) - GetBValue(col)) < 10 )
1149 {
1150 ::SetPixel(hdcMem, i, j, cmap[k].to);
1151 break;
1152 }
1153 }
1154 }
1155 }
1156
1157 return bitmap;
1158
1159 // VZ: I leave here my attempts to map the bitmap to the system colours
1160 // faster by using BitBlt() even though it's broken currently - but
1161 // maybe someone else can finish it? It should be faster than iterating
1162 // over all pixels...
1163 #if 0
1164 MemoryHDC hdcMask, hdcDst;
1165 if ( !hdcMask || !hdcDst )
1166 {
1167 wxLogLastError(_T("CreateCompatibleDC"));
1168
1169 return bitmap;
1170 }
1171
1172 // create the target bitmap
1173 HBITMAP hbmpDst = ::CreateCompatibleBitmap(hdcDst, width, height);
1174 if ( !hbmpDst )
1175 {
1176 wxLogLastError(_T("CreateCompatibleBitmap"));
1177
1178 return bitmap;
1179 }
1180
1181 // create the monochrome mask bitmap
1182 HBITMAP hbmpMask = ::CreateBitmap(width, height, 1, 1, 0);
1183 if ( !hbmpMask )
1184 {
1185 wxLogLastError(_T("CreateBitmap(mono)"));
1186
1187 ::DeleteObject(hbmpDst);
1188
1189 return bitmap;
1190 }
1191
1192 SelectInHDC bmpInDst(hdcDst, hbmpDst),
1193 bmpInMask(hdcMask, hbmpMask);
1194
1195 // for each colour:
1196 for ( n = 0; n < NUM_OF_MAPPED_COLOURS; n++ )
1197 {
1198 // create the mask for this colour
1199 ::SetBkColor(hdcMem, ColorMap[n].from);
1200 ::BitBlt(hdcMask, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY);
1201
1202 // replace this colour with the target one in the dst bitmap
1203 HBRUSH hbr = ::CreateSolidBrush(ColorMap[n].to);
1204 HGDIOBJ hbrOld = ::SelectObject(hdcDst, hbr);
1205
1206 ::MaskBlt(hdcDst, 0, 0, width, height,
1207 hdcMem, 0, 0,
1208 hbmpMask, 0, 0,
1209 MAKEROP4(PATCOPY, SRCCOPY));
1210
1211 (void)::SelectObject(hdcDst, hbrOld);
1212 ::DeleteObject(hbr);
1213 }
1214
1215 ::DeleteObject((HBITMAP)bitmap);
1216
1217 return (WXHBITMAP)hbmpDst;
1218 #endif // 0
1219 }
1220
1221 #endif // wxUSE_TOOLBAR && Win95
1222