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