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