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