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