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