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