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