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