]> git.saurik.com Git - wxWidgets.git/blame - src/msw/wince/tbarwce.cpp
Warning fix for assignment of unused value.
[wxWidgets.git] / src / msw / wince / tbarwce.cpp
CommitLineData
39d2f9a7
JS
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
65571936 9// Licence: wxWindows licence
39d2f9a7
JS
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
39d2f9a7
JS
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
a96b4743
JS
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
a9928e9d 44#if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__))
39d2f9a7
JS
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>
eae4425d 57#include <shellapi.h>
39d2f9a7 58#include <commctrl.h>
3d487566 59#if defined(WINCE_WITHOUT_COMMANDBAR)
2d36b3d8
JS
60 #include <aygshell.h>
61#endif
62#include "wx/msw/wince/missing.h"
39d2f9a7
JS
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
39d2f9a7
JS
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
ffcb4ee4 118IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
39d2f9a7
JS
119
120BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
121 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent)
122 EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged)
123END_EVENT_TABLE()
124
125// ----------------------------------------------------------------------------
126// private classes
127// ----------------------------------------------------------------------------
128
129class wxToolBarTool : public wxToolBarToolBase
130{
131public:
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
171private:
172 size_t m_nSepCount;
173};
174
175
176// ============================================================================
177// implementation
178// ============================================================================
179
180// ----------------------------------------------------------------------------
181// wxToolBarTool
182// ----------------------------------------------------------------------------
183
184wxToolBarToolBase *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
197wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
198{
199 return new wxToolBarTool(this, control);
200}
201
202// ----------------------------------------------------------------------------
203// wxToolBar construction
204// ----------------------------------------------------------------------------
205
206void 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
219bool 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
246bool wxToolBar::MSWCreateToolbar(const wxPoint& pos, const wxSize& size, wxMenuBar* menuBar)
247{
248 SetMenuBar(menuBar);
249 if (m_menuBar)
250 m_menuBar->SetToolBar(this);
251
3d487566 252#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7
JS
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();
d7da7f13 259#ifdef __SMARTPHONE__
74d0b78c
JS
260 mbi.nToolBarId = 5002;
261#else
39d2f9a7 262 mbi.nToolBarId = 5000;
74d0b78c 263#endif
39d2f9a7
JS
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);
960b193e
JS
276#else
277 HWND hWnd = CommandBar_Create(wxGetInstance(), (HWND) GetParent()->GetHWND(), GetId());
278 SetHWND((WXHWND) hWnd);
2d36b3d8
JS
279#endif
280
77ffb593 281 // install wxWidgets window proc for this window
39d2f9a7
JS
282 SubclassWin(m_hWnd);
283
284 if (menuBar)
285 menuBar->Create();
39d2f9a7 286
39d2f9a7
JS
287 return TRUE;
288}
289
290void 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
340wxToolBar::~wxToolBar()
341{
bf95a04f
JS
342 if (GetMenuBar())
343 GetMenuBar()->SetToolBar(NULL);
344
39d2f9a7
JS
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
359wxSize 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
369WXHMENU wxToolBar::GetHMenu()
370{
781a24e8
RR
371#if defined(__HANDHELDPC__)
372 // TODO ???
373 return 0;
374#else
39d2f9a7
JS
375 if (GetHWND())
376 {
377 return (WXHMENU) (HMENU)::SendMessage((HWND) GetHWND(), SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
378 }
379 else
380 return 0;
781a24e8 381#endif
39d2f9a7
JS
382}
383
384
385WXDWORD 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
434bool 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
443bool 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, -1);
514 }
515 }
516
517 return TRUE;
518}
519
bf95a04f
JS
520struct wxToolBarIdMapping
521{
522 int m_wxwinId;
523 int m_winceId;
524};
525
526static 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
bf95a04f
JS
554static 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
39d2f9a7
JS
569bool wxToolBar::Realize()
570{
571 const size_t nTools = GetToolsCount();
572 if ( nTools == 0 )
573 {
574 // nothing to do
575 return TRUE;
576 }
577
578 const bool isVertical = HasFlag(wxTB_VERTICAL);
579
580#if 0
581 // delete all old buttons, if any
582 for ( size_t pos = 0; pos < m_nButtons; pos++ )
583 {
584 if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) )
585 {
586 wxLogDebug(wxT("TB_DELETEBUTTON failed"));
587 }
588 }
589#endif
590
591 // add the buttons and separators
592 // ------------------------------
593
bf95a04f
JS
594 // Use standard buttons
595 CommandBar_AddBitmap((HWND) GetHWND(), HINST_COMMCTRL,
596 IDB_STD_SMALL_COLOR, 0, 16, 16);
597
39d2f9a7
JS
598 TBBUTTON *buttons = new TBBUTTON[nTools];
599
600 // this array will hold the indices of all controls in the toolbar
601 wxArrayInt controlIds;
602
603 bool lastWasRadio = FALSE;
604 int i = 0;
605 wxToolBarToolsList::Node* node;
606 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
607 {
608 wxToolBarToolBase *tool = node->GetData();
609
bf95a04f
JS
610 bool processedThis = TRUE;
611
39d2f9a7
JS
612 TBBUTTON& button = buttons[i];
613
614 wxZeroMemory(button);
615
616 bool isRadio = FALSE;
617 switch ( tool->GetStyle() )
618 {
619 case wxTOOL_STYLE_CONTROL:
620 button.idCommand = tool->GetId();
621 // fall through: create just a separator too
622
623 case wxTOOL_STYLE_SEPARATOR:
624 button.fsState = TBSTATE_ENABLED;
625 button.fsStyle = TBSTYLE_SEP;
626 break;
627
628 case wxTOOL_STYLE_BUTTON:
bf95a04f
JS
629// if ( !HasFlag(wxTB_NOICONS) )
630// button.iBitmap = bitmapId;
39d2f9a7
JS
631
632 if ( HasFlag(wxTB_TEXT) )
633 {
634 const wxString& label = tool->GetLabel();
635 if ( !label.empty() )
636 {
637 button.iString = (int)label.c_str();
638 }
639 }
640
bf95a04f
JS
641 int winceId = wxFindIdForwxWinId(tool->GetId());
642 if (winceId > -1)
643 {
644 button.idCommand = tool->GetId();
645// if ( !HasFlag(wxTB_NOICONS) )
646 button.iBitmap = winceId;
647 }
648 else
649 processedThis = FALSE;
39d2f9a7
JS
650
651 if ( tool->IsEnabled() )
652 button.fsState |= TBSTATE_ENABLED;
653 if ( tool->IsToggled() )
654 button.fsState |= TBSTATE_CHECKED;
655
656 switch ( tool->GetKind() )
657 {
658 case wxITEM_RADIO:
659 button.fsStyle = TBSTYLE_CHECKGROUP;
660
661 if ( !lastWasRadio )
662 {
663 // the first item in the radio group is checked by
664 // default to be consistent with wxGTK and the menu
665 // radio items
666 button.fsState |= TBSTATE_CHECKED;
667
668 tool->Toggle(TRUE);
669 }
670
671 isRadio = TRUE;
672 break;
673
674 case wxITEM_CHECK:
675 button.fsStyle = TBSTYLE_CHECK;
676 break;
677
678 default:
679 wxFAIL_MSG( _T("unexpected toolbar button kind") );
680 // fall through
681
682 case wxITEM_NORMAL:
683 button.fsStyle = TBSTYLE_BUTTON;
684 }
685
bf95a04f 686// bitmapId++;
39d2f9a7
JS
687 break;
688 }
689
690 lastWasRadio = isRadio;
691
bf95a04f
JS
692 if (processedThis)
693 i++;
39d2f9a7
JS
694 }
695
bf95a04f 696 // Add buttons to Commandbar
39d2f9a7
JS
697 if (!CommandBar_AddButtons(GetHwnd(), i, buttons))
698 {
699 wxLogLastError(wxT("CommandBar_AddButtons"));
700 }
701
702 delete [] buttons;
703
704#if 0
705 // Deal with the controls finally
706 // ------------------------------
707
708 // adjust the controls size to fit nicely in the toolbar
709 int y = 0;
710 size_t index = 0;
711 for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ )
712 {
713 wxToolBarToolBase *tool = node->GetData();
714
715 // we calculate the running y coord for vertical toolbars so we need to
716 // get the items size for all items but for the horizontal ones we
717 // don't need to deal with the non controls
718 bool isControl = tool->IsControl();
719 if ( !isControl && !isVertical )
720 continue;
721
722 // note that we use TB_GETITEMRECT and not TB_GETRECT because the
723 // latter only appeared in v4.70 of comctl32.dll
724 RECT r;
725 if ( !SendMessage(GetHwnd(), TB_GETITEMRECT,
726 index, (LPARAM)(LPRECT)&r) )
727 {
728 wxLogLastError(wxT("TB_GETITEMRECT"));
729 }
730
731 if ( !isControl )
732 {
733 // can only be control if isVertical
734 y += r.bottom - r.top;
735
736 continue;
737 }
738
739 wxControl *control = tool->GetControl();
740
741 wxSize size = control->GetSize();
742
743 // the position of the leftmost controls corner
744 int left = -1;
745
746 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
747#if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
748 // available in headers, now check whether it is available now
749 // (during run-time)
750 if ( wxTheApp->GetComCtl32Version() >= 471 )
751 {
752 // set the (underlying) separators width to be that of the
753 // control
754 TBBUTTONINFO tbbi;
755 tbbi.cbSize = sizeof(tbbi);
756 tbbi.dwMask = TBIF_SIZE;
757 tbbi.cx = size.x;
758 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO,
759 tool->GetId(), (LPARAM)&tbbi) )
760 {
761 // the id is probably invalid?
762 wxLogLastError(wxT("TB_SETBUTTONINFO"));
763 }
764 }
765 else
766#endif // comctl32.dll 4.71
767 // TB_SETBUTTONINFO unavailable
768 {
769 // try adding several separators to fit the controls width
770 int widthSep = r.right - r.left;
771 left = r.left;
772
773 TBBUTTON tbb;
774 wxZeroMemory(tbb);
775 tbb.idCommand = 0;
776 tbb.fsState = TBSTATE_ENABLED;
777 tbb.fsStyle = TBSTYLE_SEP;
778
779 size_t nSeparators = size.x / widthSep;
780 for ( size_t nSep = 0; nSep < nSeparators; nSep++ )
781 {
782 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON,
783 index, (LPARAM)&tbb) )
784 {
785 wxLogLastError(wxT("TB_INSERTBUTTON"));
786 }
787
788 index++;
789 }
790
791 // remember the number of separators we used - we'd have to
792 // delete all of them later
793 ((wxToolBarTool *)tool)->SetSeparatorsCount(nSeparators);
794
795 // adjust the controls width to exactly cover the separators
796 control->SetSize((nSeparators + 1)*widthSep, -1);
797 }
798
799 // position the control itself correctly vertically
800 int height = r.bottom - r.top;
801 int diff = height - size.y;
802 if ( diff < 0 )
803 {
804 // the control is too high, resize to fit
805 control->SetSize(-1, height - 2);
806
807 diff = 2;
808 }
809
810 int top;
811 if ( isVertical )
812 {
813 left = 0;
814 top = y;
815
816 y += height + 2*GetMargins().y;
817 }
818 else // horizontal toolbar
819 {
820 if ( left == -1 )
821 left = r.left;
822
823 top = r.top;
824 }
825
826 control->Move(left, top + (diff + 1) / 2);
827 }
828
829 // the max index is the "real" number of buttons - i.e. counting even the
830 // separators which we added just for aligning the controls
831 m_nButtons = index;
832
833 if ( !isVertical )
834 {
835 if ( m_maxRows == 0 )
836 {
837 // if not set yet, only one row
838 SetRows(1);
839 }
840 }
841 else if ( m_nButtons > 0 ) // vertical non empty toolbar
842 {
843 if ( m_maxRows == 0 )
844 {
845 // if not set yet, have one column
846 SetRows(m_nButtons);
847 }
848 }
849#endif
850
851 return TRUE;
852}
853
854// ----------------------------------------------------------------------------
855// message handlers
856// ----------------------------------------------------------------------------
857
858bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id)
859{
860 wxToolBarToolBase *tool = FindById((int)id);
861 if ( !tool )
862 {
863 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED);
864 event.SetEventObject(this);
865 event.SetId(id);
866 event.SetInt(id);
867
868 return GetEventHandler()->ProcessEvent(event);
869 }
870
871 if ( tool->CanBeToggled() )
872 {
873 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
874 tool->Toggle((state & TBSTATE_CHECKED) != 0);
875 }
876
877 bool toggled = tool->IsToggled();
878
879 // avoid sending the event when a radio button is released, this is not
880 // interesting
881 if ( !tool->CanBeToggled() || tool->GetKind() != wxITEM_RADIO || toggled )
882 {
883 // OnLeftClick() can veto the button state change - for buttons which
884 // may be toggled only, of couse
885 if ( !OnLeftClick((int)id, toggled) && tool->CanBeToggled() )
886 {
887 // revert back
888 toggled = !toggled;
889 tool->SetToggle(toggled);
890
891 ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(toggled, 0));
892 }
893 }
894
895 return TRUE;
896}
897
898bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl),
899 WXLPARAM lParam,
900 WXLPARAM *WXUNUSED(result))
901{
902#if wxUSE_TOOLTIPS
903 // First check if this applies to us
904 NMHDR *hdr = (NMHDR *)lParam;
905
906 // the tooltips control created by the toolbar is sometimes Unicode, even
907 // in an ANSI application - this seems to be a bug in comctl32.dll v5
908 UINT code = hdr->code;
909 if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) )
910 return FALSE;
911
912 HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0);
913 if ( toolTipWnd != hdr->hwndFrom )
914 return FALSE;
915
916 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
917 int id = (int)ttText->hdr.idFrom;
918
919 wxToolBarToolBase *tool = FindById(id);
920 if ( !tool )
991420e6 921 return false;
39d2f9a7
JS
922
923 return HandleTooltipNotify(code, lParam, tool->GetShortHelp());
924#else
991420e6
WS
925 wxUnusedVar(lParam);
926 return false;
39d2f9a7
JS
927#endif
928}
929
930// ----------------------------------------------------------------------------
931// toolbar geometry
932// ----------------------------------------------------------------------------
933
934void wxToolBar::SetToolBitmapSize(const wxSize& size)
935{
936 wxToolBarBase::SetToolBitmapSize(size);
937
938 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
939}
940
941void wxToolBar::SetRows(int nRows)
942{
943 if ( nRows == m_maxRows )
944 {
945 // avoid resizing the frame uselessly
946 return;
947 }
948
949 // TRUE in wParam means to create at least as many rows, FALSE -
950 // at most as many
951 RECT rect;
952 ::SendMessage(GetHwnd(), TB_SETROWS,
953 MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)),
954 (LPARAM) &rect);
955
956 m_maxRows = nRows;
957
958 UpdateSize();
959}
960
961// The button size is bigger than the bitmap size
962wxSize wxToolBar::GetToolSize() const
963{
964 // TB_GETBUTTONSIZE is supported from version 4.70
965#if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \
966 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
967 if ( wxTheApp->GetComCtl32Version() >= 470 )
968 {
969 DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0);
970
971 return wxSize(LOWORD(dw), HIWORD(dw));
972 }
973 else
974#endif // comctl32.dll 4.70+
975 {
976 // defaults
977 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
978 }
979}
980
981static
982wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools,
983 size_t index )
984{
985 wxToolBarToolsList::compatibility_iterator current = tools.GetFirst();
986
987 for ( ; current != 0; current = current->GetNext() )
988 {
989 if ( index == 0 )
990 return current->GetData();
991
992 wxToolBarTool *tool = (wxToolBarTool *)current->GetData();
993 size_t separators = tool->GetSeparatorsCount();
994
995 // if it is a normal button, sepcount == 0, so skip 1 item (the button)
996 // otherwise, skip as many items as the separator count, plus the
997 // control itself
998 index -= separators ? separators + 1 : 1;
999 }
1000
1001 return 0;
1002}
1003
1004wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1005{
1006 POINT pt;
1007 pt.x = x;
1008 pt.y = y;
1009 int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt);
1010 // MBN: when the point ( x, y ) is close to the toolbar border
1011 // TB_HITTEST returns m_nButtons ( not -1 )
1012 if ( index < 0 || (size_t)index >= m_nButtons )
1013 {
1014 // it's a separator or there is no tool at all there
1015 return (wxToolBarToolBase *)NULL;
1016 }
1017
1018 // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers
1019#if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
1020 if ( wxTheApp->GetComCtl32Version() >= 471 )
1021 {
1022 return m_tools.Item((size_t)index)->GetData();
1023 }
1024 else
1025#endif
1026 {
1027 return GetItemSkippingDummySpacers( m_tools, (size_t) index );
1028 }
1029}
1030
1031void wxToolBar::UpdateSize()
1032{
1033 // the toolbar size changed
1034 SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0);
1035
1036 // we must also refresh the frame after the toolbar size (possibly) changed
1037 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
1038 if ( frame )
1039 {
1040 frame->SendSizeEvent();
1041 }
1042}
1043
1044// ----------------------------------------------------------------------------
1045// toolbar styles
1046// ---------------------------------------------------------------------------
1047
1048void wxToolBar::SetWindowStyleFlag(long style)
1049{
1050 // the style bits whose changes force us to recreate the toolbar
1051 static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS;
1052
1053 const long styleOld = GetWindowStyle();
1054
1055 wxToolBarBase::SetWindowStyleFlag(style);
1056
1057 // don't recreate an empty toolbar: not only this is unnecessary, but it is
1058 // also fatal as we'd then try to recreate the toolbar when it's just being
1059 // created
1060 if ( GetToolsCount() &&
1061 (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) )
1062 {
1063 // to remove the text labels, simply re-realizing the toolbar is enough
1064 // but I don't know of any way to add the text to an existing toolbar
1065 // other than by recreating it entirely
1066 Recreate();
1067 }
1068}
1069
1070// ----------------------------------------------------------------------------
1071// tool state
1072// ----------------------------------------------------------------------------
1073
1074void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
1075{
1076 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
1077 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0));
1078}
1079
1080void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
1081{
1082 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
1083 (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0));
1084}
1085
1086void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1087{
1088 // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or
1089 // without, so we really need to delete the button and recreate it here
1090 wxFAIL_MSG( _T("not implemented") );
1091}
1092
1093// ----------------------------------------------------------------------------
1094// event handlers
1095// ----------------------------------------------------------------------------
1096
1097// Responds to colour changes, and passes event on to children.
1098void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event)
1099{
1100 wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE));
1101
1102 // Remap the buttons
1103 Realize();
1104
1105 // Relayout the toolbar
1106 int nrows = m_maxRows;
1107 m_maxRows = 0; // otherwise SetRows() wouldn't do anything
1108 SetRows(nrows);
1109
1110 Refresh();
1111
1112 // let the event propagate further
1113 event.Skip();
1114}
1115
1116void wxToolBar::OnMouseEvent(wxMouseEvent& event)
1117{
1118 if (event.Leaving() && m_pInTool)
1119 {
1120 OnMouseEnter( -1 );
1121 event.Skip();
1122 return;
1123 }
1124
1125 if (event.RightDown())
1126 {
1127 // For now, we don't have an id. Later we could
1128 // try finding the tool.
1129 OnRightClick((int)-1, event.GetX(), event.GetY());
1130 }
1131 else
1132 {
1133 event.Skip();
1134 }
1135}
1136
991420e6 1137void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam)
39d2f9a7
JS
1138{
1139 wxCoord x = GET_X_LPARAM(lParam),
1140 y = GET_Y_LPARAM(lParam);
1141 wxToolBarToolBase* tool = FindToolForPosition( x, y );
1142
1143 // cursor left current tool
1144 if( tool != m_pInTool && !tool )
1145 {
1146 m_pInTool = 0;
1147 OnMouseEnter( -1 );
1148 }
1149
1150 // cursor entered a tool
1151 if( tool != m_pInTool && tool )
1152 {
1153 m_pInTool = tool;
1154 OnMouseEnter( tool->GetId() );
1155 }
1156}
1157
c140b7e7 1158WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
39d2f9a7
JS
1159{
1160#if 0
1161 switch ( nMsg )
1162 {
1163 case WM_SIZE:
1164 if ( HandleSize(wParam, lParam) )
1165 return 0;
1166 break;
1167
1168 case WM_MOUSEMOVE:
1169 // we don't handle mouse moves, so always pass the message to
1170 // wxControl::MSWWindowProc
1171 HandleMouseMove(wParam, lParam);
1172 break;
1173 }
1174#endif
1175 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
1176}
1177
39d2f9a7
JS
1178#endif // wxUSE_TOOLBAR && Win95
1179