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