]> git.saurik.com Git - wxWidgets.git/blob - src/msw/wince/tbarwce.cpp
Better alphabetized the event handler macros
[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 defined(WINCE_WITHOUT_COMMANDBAR)
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& WXUNUSED(pos), const wxSize& WXUNUSED(size), wxMenuBar* menuBar)
247 {
248 SetMenuBar(menuBar);
249 if (m_menuBar)
250 m_menuBar->SetToolBar(this);
251
252 #if defined(WINCE_WITHOUT_COMMANDBAR)
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 defined(__HANDHELDPC__)
372 // TODO ???
373 return 0;
374 #else
375 if (GetHWND())
376 {
377 return (WXHMENU) (HMENU)::SendMessage((HWND) GetHWND(), SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
378 }
379 else
380 return 0;
381 #endif
382 }
383
384
385 WXDWORD wxToolBar::MSWGetStyle(long style, WXDWORD *exstyle) const
386 {
387 // toolbars never have border, giving one to them results in broken
388 // appearance
389 WXDWORD msStyle = wxControl::MSWGetStyle
390 (
391 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
392 );
393
394 // always include this one, it never hurts and setting it later only if we
395 // do have tooltips wouldn't work
396 msStyle |= TBSTYLE_TOOLTIPS;
397
398 if ( style & (wxTB_FLAT | wxTB_HORZ_LAYOUT) )
399 {
400 // static as it doesn't change during the program lifetime
401 static int s_verComCtl = wxTheApp->GetComCtl32Version();
402
403 // comctl32.dll 4.00 doesn't support the flat toolbars and using this
404 // style with 6.00 (part of Windows XP) leads to the toolbar with
405 // incorrect background colour - and not using it still results in the
406 // correct (flat) toolbar, so don't use it there
407 if ( s_verComCtl > 400 && s_verComCtl < 600 )
408 {
409 msStyle |= TBSTYLE_FLAT | TBSTYLE_TRANSPARENT;
410 }
411
412 if ( s_verComCtl >= 470 && style & wxTB_HORZ_LAYOUT )
413 {
414 msStyle |= TBSTYLE_LIST;
415 }
416 }
417
418 if ( style & wxTB_NODIVIDER )
419 msStyle |= CCS_NODIVIDER;
420
421 if ( style & wxTB_NOALIGN )
422 msStyle |= CCS_NOPARENTALIGN;
423
424 if ( style & wxTB_VERTICAL )
425 msStyle |= CCS_VERT;
426
427 return msStyle;
428 }
429
430 // ----------------------------------------------------------------------------
431 // adding/removing tools
432 // ----------------------------------------------------------------------------
433
434 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
435 {
436 // nothing special to do here - we really create the toolbar buttons in
437 // Realize() later
438 tool->Attach(this);
439
440 return true;
441 }
442
443 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
444 {
445 // the main difficulty we have here is with the controls in the toolbars:
446 // as we (sometimes) use several separators to cover up the space used by
447 // them, the indices are not the same for us and the toolbar
448
449 // first determine the position of the first button to delete: it may be
450 // different from pos if we use several separators to cover the space used
451 // by a control
452 wxToolBarToolsList::compatibility_iterator node;
453 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
454 {
455 wxToolBarToolBase *tool2 = node->GetData();
456 if ( tool2 == tool )
457 {
458 // let node point to the next node in the list
459 node = node->GetNext();
460
461 break;
462 }
463
464 if ( tool2->IsControl() )
465 {
466 pos += ((wxToolBarTool *)tool2)->GetSeparatorsCount() - 1;
467 }
468 }
469
470 // now determine the number of buttons to delete and the area taken by them
471 size_t nButtonsToDelete = 1;
472
473 // get the size of the button we're going to delete
474 RECT r;
475 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, pos, (LPARAM)&r) )
476 {
477 wxLogLastError(_T("TB_GETITEMRECT"));
478 }
479
480 int width = r.right - r.left;
481
482 if ( tool->IsControl() )
483 {
484 nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount();
485
486 width *= nButtonsToDelete;
487 }
488
489 // do delete all buttons
490 m_nButtons -= nButtonsToDelete;
491 while ( nButtonsToDelete-- > 0 )
492 {
493 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) )
494 {
495 wxLogLastError(wxT("TB_DELETEBUTTON"));
496
497 return false;
498 }
499 }
500
501 tool->Detach();
502
503 // and finally reposition all the controls after this button (the toolbar
504 // takes care of all normal items)
505 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
506 {
507 wxToolBarToolBase *tool2 = node->GetData();
508 if ( tool2->IsControl() )
509 {
510 int x;
511 wxControl *control = tool2->GetControl();
512 control->GetPosition(&x, NULL);
513 control->Move(x - width, wxDefaultCoord);
514 }
515 }
516
517 return true;
518 }
519
520 struct wxToolBarIdMapping
521 {
522 int m_wxwinId;
523 int m_winceId;
524 };
525
526 static wxToolBarIdMapping sm_ToolBarIdMappingArray[] =
527 {
528 { wxID_COPY, STD_COPY },
529 { wxID_CUT, STD_CUT },
530 { wxID_FIND, STD_FIND },
531 { wxID_PASTE, STD_PASTE },
532 { wxID_NEW, STD_FILENEW },
533 { wxID_OPEN, STD_FILEOPEN },
534 { wxID_SAVE, STD_FILESAVE },
535 { wxID_PRINT, STD_PRINT },
536 { wxID_PREVIEW, STD_PRINTPRE },
537 { wxID_UNDO, STD_UNDO },
538 { wxID_REDO, STD_REDOW },
539 { wxID_HELP, STD_HELP },
540 { wxID_DELETE, STD_DELETE },
541 { wxID_REPLACE, STD_REPLACE },
542 { wxID_PROPERTIES, STD_PROPERTIES },
543 { wxID_VIEW_DETAILS, VIEW_DETAILS },
544 { wxID_VIEW_SORTDATE, VIEW_SORTDATE },
545 { wxID_VIEW_LARGEICONS, VIEW_LARGEICONS },
546 { wxID_VIEW_SORTNAME, VIEW_SORTNAME },
547 { wxID_VIEW_LIST, VIEW_LIST },
548 { wxID_VIEW_SORTSIZE, VIEW_SORTSIZE },
549 { wxID_VIEW_SMALLICONS, VIEW_SMALLICONS },
550 { wxID_VIEW_SORTTYPE, VIEW_SORTTYPE },
551 { 0, 0},
552 };
553
554 static int wxFindIdForwxWinId(int id)
555 {
556 int i = 0;
557 while (true)
558 {
559 if (sm_ToolBarIdMappingArray[i].m_wxwinId == 0)
560 return -1;
561 else if (sm_ToolBarIdMappingArray[i].m_wxwinId == id)
562 return sm_ToolBarIdMappingArray[i].m_winceId;
563 i ++;
564 }
565 return -1;
566 }
567
568
569 bool wxToolBar::Realize()
570 {
571 const size_t nTools = GetToolsCount();
572 if ( nTools == 0 )
573 {
574 // nothing to do
575 return true;
576 }
577
578 #if 0
579 // delete all old buttons, if any
580 for ( size_t pos = 0; pos < m_nButtons; pos++ )
581 {
582 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) )
583 {
584 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
585 }
586 }
587 #endif // 0
588
589 // add the buttons and separators
590 // ------------------------------
591
592 // Use standard buttons
593 CommandBar_AddBitmap((HWND) GetHWND(), HINST_COMMCTRL,
594 IDB_STD_SMALL_COLOR, 0, 16, 16);
595
596 TBBUTTON *buttons = new TBBUTTON[nTools];
597
598 // this array will hold the indices of all controls in the toolbar
599 wxArrayInt controlIds;
600
601 bool lastWasRadio = false;
602 int i = 0;
603 wxToolBarToolsList::Node* node;
604 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
605 {
606 wxToolBarToolBase *tool = node->GetData();
607
608 bool processedThis = true;
609
610 TBBUTTON& button = buttons[i];
611
612 wxZeroMemory(button);
613
614 bool isRadio = false;
615 switch ( tool->GetStyle() )
616 {
617 case wxTOOL_STYLE_CONTROL:
618 button.idCommand = tool->GetId();
619 // fall through: create just a separator too
620
621 case wxTOOL_STYLE_SEPARATOR:
622 button.fsState = TBSTATE_ENABLED;
623 button.fsStyle = TBSTYLE_SEP;
624 break;
625
626 case wxTOOL_STYLE_BUTTON:
627 // if ( !HasFlag(wxTB_NOICONS) )
628 // button.iBitmap = bitmapId;
629
630 if ( HasFlag(wxTB_TEXT) )
631 {
632 const wxString& label = tool->GetLabel();
633 if ( !label.empty() )
634 {
635 button.iString = (int)label.c_str();
636 }
637 }
638
639 int winceId = wxFindIdForwxWinId(tool->GetId());
640 if (winceId > -1)
641 {
642 button.idCommand = tool->GetId();
643 // if ( !HasFlag(wxTB_NOICONS) )
644 button.iBitmap = winceId;
645 }
646 else
647 processedThis = false;
648
649 if ( tool->IsEnabled() )
650 button.fsState |= TBSTATE_ENABLED;
651 if ( tool->IsToggled() )
652 button.fsState |= TBSTATE_CHECKED;
653
654 switch ( tool->GetKind() )
655 {
656 case wxITEM_RADIO:
657 button.fsStyle = TBSTYLE_CHECKGROUP;
658
659 if ( !lastWasRadio )
660 {
661 // the first item in the radio group is checked by
662 // default to be consistent with wxGTK and the menu
663 // radio items
664 button.fsState |= TBSTATE_CHECKED;
665
666 tool->Toggle(true);
667 }
668
669 isRadio = true;
670 break;
671
672 case wxITEM_CHECK:
673 button.fsStyle = TBSTYLE_CHECK;
674 break;
675
676 default:
677 wxFAIL_MSG( _T("unexpected toolbar button kind") );
678 // fall through
679
680 case wxITEM_NORMAL:
681 button.fsStyle = TBSTYLE_BUTTON;
682 }
683
684 // bitmapId++;
685 break;
686 }
687
688 lastWasRadio = isRadio;
689
690 if (processedThis)
691 i++;
692 }
693
694 // Add buttons to Commandbar
695 if (!CommandBar_AddButtons(GetHwnd(), i, buttons))
696 {
697 wxLogLastError(wxT("CommandBar_AddButtons"));
698 }
699
700 delete [] buttons;
701
702 #if 0
703
704 const bool isVertical = HasFlag(wxTB_VERTICAL);
705
706 // Deal with the controls finally
707 // ------------------------------
708
709 // adjust the controls size to fit nicely in the toolbar
710 int y = 0;
711 size_t index = 0;
712 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ )
713 {
714 wxToolBarToolBase *tool = node->GetData();
715
716 // we calculate the running y coord for vertical toolbars so we need to
717 // get the items size for all items but for the horizontal ones we
718 // don't need to deal with the non controls
719 bool isControl = tool->IsControl();
720 if ( !isControl && !isVertical )
721 continue;
722
723 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
724 // latter only appeared in v4.70 of comctl32.dll
725 RECT r;
726 if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT,
727 index, (LPARAM)(LPRECT)&r) )
728 {
729 wxLogLastError(wxT("TB_GETITEMRECT"));
730 }
731
732 if ( !isControl )
733 {
734 // can only be control if isVertical
735 y += r.bottom - r.top;
736
737 continue;
738 }
739
740 wxControl *control = tool->GetControl();
741
742 wxSize size = control->GetSize();
743
744 // the position of the leftmost controls corner
745 int left = -1;
746
747 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
748 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
749 // available in headers, now check whether it is available now
750 // (during run-time)
751 if ( wxTheApp->GetComCtl32Version() >= 471 )
752 {
753 // set the (underlying) separators width to be that of the
754 // control
755 TBBUTTONINFO tbbi;
756 tbbi.cbSize = sizeof(tbbi);
757 tbbi.dwMask = TBIF_SIZE;
758 tbbi.cx = size.x;
759 if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO,
760 tool->GetId(), (LPARAM)&tbbi) )
761 {
762 // the id is probably invalid?
763 wxLogLastError(wxT("TB_SETBUTTONINFO"));
764 }
765 }
766 else
767 #endif // comctl32.dll 4.71
768 // TB_SETBUTTONINFO unavailable
769 {
770 // try adding several separators to fit the controls width
771 int widthSep = r.right - r.left;
772 left = r.left;
773
774 TBBUTTON tbb;
775 wxZeroMemory(tbb);
776 tbb.idCommand = 0;
777 tbb.fsState = TBSTATE_ENABLED;
778 tbb.fsStyle = TBSTYLE_SEP;
779
780 size_t nSeparators = size.x / widthSep;
781 for ( size_t nSep = 0; nSep < nSeparators; nSep++ )
782 {
783 if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON,
784 index, (LPARAM)&tbb) )
785 {
786 wxLogLastError(wxT("TB_INSERTBUTTON"));
787 }
788
789 index++;
790 }
791
792 // remember the number of separators we used - we'd have to
793 // delete all of them later
794 ((wxToolBarTool *)tool)->SetSeparatorsCount(nSeparators);
795
796 // adjust the controls width to exactly cover the separators
797 control->SetSize((nSeparators + 1)*widthSep, wxDefaultCoord);
798 }
799
800 // position the control itself correctly vertically
801 int height = r.bottom - r.top;
802 int diff = height - size.y;
803 if ( diff < 0 )
804 {
805 // the control is too high, resize to fit
806 control->SetSize(wxDefaultCoord, height - 2);
807
808 diff = 2;
809 }
810
811 int top;
812 if ( isVertical )
813 {
814 left = 0;
815 top = y;
816
817 y += height + 2*GetMargins().y;
818 }
819 else // horizontal toolbar
820 {
821 if ( left == -1 )
822 left = r.left;
823
824 top = r.top;
825 }
826
827 control->Move(left, top + (diff + 1) / 2);
828 }
829
830 // the max index is the "real" number of buttons - i.e. counting even the
831 // separators which we added just for aligning the controls
832 m_nButtons = index;
833
834 if ( !isVertical )
835 {
836 if ( m_maxRows == 0 )
837 {
838 // if not set yet, only one row
839 SetRows(1);
840 }
841 }
842 else if ( m_nButtons > 0 ) // vertical non empty toolbar
843 {
844 if ( m_maxRows == 0 )
845 {
846 // if not set yet, have one column
847 SetRows(m_nButtons);
848 }
849 }
850 #endif // 0
851
852 return true;
853 }
854
855 // ----------------------------------------------------------------------------
856 // message handlers
857 // ----------------------------------------------------------------------------
858
859 bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id)
860 {
861 wxToolBarToolBase *tool = FindById((int)id);
862 if ( !tool )
863 {
864 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED);
865 event.SetEventObject(this);
866 event.SetId(id);
867 event.SetInt(id);
868
869 return GetEventHandler()->ProcessEvent(event);
870 }
871
872 if ( tool->CanBeToggled() )
873 {
874 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
875 tool->Toggle((state & TBSTATE_CHECKED) != 0);
876 }
877
878 bool toggled = tool->IsToggled();
879
880 // avoid sending the event when a radio button is released, this is not
881 // interesting
882 if ( !tool->CanBeToggled() || tool->GetKind() != wxITEM_RADIO || toggled )
883 {
884 // OnLeftClick() can veto the button state change - for buttons which
885 // may be toggled only, of couse
886 if ( !OnLeftClick((int)id, toggled) && tool->CanBeToggled() )
887 {
888 // revert back
889 toggled = !toggled;
890 tool->SetToggle(toggled);
891
892 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(toggled, 0));
893 }
894 }
895
896 return true;
897 }
898
899 bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl),
900 WXLPARAM lParam,
901 WXLPARAM *WXUNUSED(result))
902 {
903 #if wxUSE_TOOLTIPS
904 // First check if this applies to us
905 NMHDR *hdr = (NMHDR *)lParam;
906
907 // the tooltips control created by the toolbar is sometimes Unicode, even
908 // in an ANSI application - this seems to be a bug in comctl32.dll v5
909 UINT code = hdr->code;
910 if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) )
911 return false;
912
913 HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0);
914 if ( toolTipWnd != hdr->hwndFrom )
915 return false;
916
917 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
918 int id = (int)ttText->hdr.idFrom;
919
920 wxToolBarToolBase *tool = FindById(id);
921 if ( !tool )
922 return false;
923
924 return HandleTooltipNotify(code, lParam, tool->GetShortHelp());
925 #else
926 wxUnusedVar(lParam);
927 return false;
928 #endif
929 }
930
931 // ----------------------------------------------------------------------------
932 // toolbar geometry
933 // ----------------------------------------------------------------------------
934
935 void wxToolBar::SetToolBitmapSize(const wxSize& size)
936 {
937 wxToolBarBase::SetToolBitmapSize(size);
938
939 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
940 }
941
942 void wxToolBar::SetRows(int nRows)
943 {
944 if ( nRows == m_maxRows )
945 {
946 // avoid resizing the frame uselessly
947 return;
948 }
949
950 // TRUE in wParam means to create at least as many rows, FALSE -
951 // at most as many
952 RECT rect;
953 ::SendMessage(GetHwnd(), TB_SETROWS,
954 MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)),
955 (LPARAM) &rect);
956
957 m_maxRows = nRows;
958
959 UpdateSize();
960 }
961
962 // The button size is bigger than the bitmap size
963 wxSize wxToolBar::GetToolSize() const
964 {
965 // TB_GETBUTTONSIZE is supported from version 4.70
966 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
967 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
968 if ( wxTheApp->GetComCtl32Version() >= 470 )
969 {
970 DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0);
971
972 return wxSize(LOWORD(dw), HIWORD(dw));
973 }
974 else
975 #endif // comctl32.dll 4.70+
976 {
977 // defaults
978 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
979 }
980 }
981
982 static
983 wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools,
984 size_t index )
985 {
986 wxToolBarToolsList::compatibility_iterator current = tools.GetFirst();
987
988 for ( ; current != 0; current = current->GetNext() )
989 {
990 if ( index == 0 )
991 return current->GetData();
992
993 wxToolBarTool *tool = (wxToolBarTool *)current->GetData();
994 size_t separators = tool->GetSeparatorsCount();
995
996 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
997 // otherwise, skip as many items as the separator count, plus the
998 // control itself
999 index -= separators ? separators + 1 : 1;
1000 }
1001
1002 return 0;
1003 }
1004
1005 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1006 {
1007 POINT pt;
1008 pt.x = x;
1009 pt.y = y;
1010 int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt);
1011 // MBN: when the point ( x, y ) is close to the toolbar border
1012 // TB_HITTEST returns m_nButtons ( not -1 )
1013 if ( index < 0 || (size_t)index >= m_nButtons )
1014 {
1015 // it's a separator or there is no tool at all there
1016 return (wxToolBarToolBase *)NULL;
1017 }
1018
1019 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1020 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1021 if ( wxTheApp->GetComCtl32Version() >= 471 )
1022 {
1023 return m_tools.Item((size_t)index)->GetData();
1024 }
1025 else
1026 #endif
1027 {
1028 return GetItemSkippingDummySpacers( m_tools, (size_t) index );
1029 }
1030 }
1031
1032 void wxToolBar::UpdateSize()
1033 {
1034 // the toolbar size changed
1035 ::SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0);
1036
1037 // we must also refresh the frame after the toolbar size (possibly) changed
1038 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
1039 if ( frame )
1040 {
1041 frame->SendSizeEvent();
1042 }
1043 }
1044
1045 // ----------------------------------------------------------------------------
1046 // toolbar styles
1047 // ---------------------------------------------------------------------------
1048
1049 void wxToolBar::SetWindowStyleFlag(long style)
1050 {
1051 // the style bits whose changes force us to recreate the toolbar
1052 static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS;
1053
1054 const long styleOld = GetWindowStyle();
1055
1056 wxToolBarBase::SetWindowStyleFlag(style);
1057
1058 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1059 // also fatal as we'd then try to recreate the toolbar when it's just being
1060 // created
1061 if ( GetToolsCount() &&
1062 (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) )
1063 {
1064 // to remove the text labels, simply re-realizing the toolbar is enough
1065 // but I don't know of any way to add the text to an existing toolbar
1066 // other than by recreating it entirely
1067 Recreate();
1068 }
1069 }
1070
1071 // ----------------------------------------------------------------------------
1072 // tool state
1073 // ----------------------------------------------------------------------------
1074
1075 void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
1076 {
1077 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
1078 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0));
1079 }
1080
1081 void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
1082 {
1083 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
1084 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0));
1085 }
1086
1087 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1088 {
1089 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1090 // without, so we really need to delete the button and recreate it here
1091 wxFAIL_MSG( _T("not implemented") );
1092 }
1093
1094 // ----------------------------------------------------------------------------
1095 // event handlers
1096 // ----------------------------------------------------------------------------
1097
1098 // Responds to colour changes, and passes event on to children.
1099 void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event)
1100 {
1101 wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE));
1102
1103 // Remap the buttons
1104 Realize();
1105
1106 // Relayout the toolbar
1107 int nrows = m_maxRows;
1108 m_maxRows = 0; // otherwise SetRows() wouldn't do anything
1109 SetRows(nrows);
1110
1111 Refresh();
1112
1113 // let the event propagate further
1114 event.Skip();
1115 }
1116
1117 void wxToolBar::OnMouseEvent(wxMouseEvent& event)
1118 {
1119 if (event.Leaving() && m_pInTool)
1120 {
1121 OnMouseEnter( -1 );
1122 event.Skip();
1123 return;
1124 }
1125
1126 if (event.RightDown())
1127 {
1128 // For now, we don't have an id. Later we could
1129 // try finding the tool.
1130 OnRightClick((int)-1, event.GetX(), event.GetY());
1131 }
1132 else
1133 {
1134 event.Skip();
1135 }
1136 }
1137
1138 void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
1139 {
1140 wxCoord x = GET_X_LPARAM(lParam),
1141 y = GET_Y_LPARAM(lParam);
1142 wxToolBarToolBase* tool = FindToolForPosition( x, y );
1143
1144 // cursor left current tool
1145 if( tool != m_pInTool && !tool )
1146 {
1147 m_pInTool = 0;
1148 OnMouseEnter( -1 );
1149 }
1150
1151 // cursor entered a tool
1152 if( tool != m_pInTool && tool )
1153 {
1154 m_pInTool = tool;
1155 OnMouseEnter( tool->GetId() );
1156 }
1157 }
1158
1159 WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1160 {
1161 #if 0
1162 switch ( nMsg )
1163 {
1164 case WM_SIZE:
1165 if ( HandleSize(wParam, lParam) )
1166 return 0;
1167 break;
1168
1169 case WM_MOUSEMOVE:
1170 // we don't handle mouse moves, so always pass the message to
1171 // wxControl::MSWWindowProc
1172 HandleMouseMove(wParam, lParam);
1173 break;
1174 }
1175 #endif
1176 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
1177 }
1178
1179 #endif // wxUSE_TOOLBAR && Win95
1180