]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: msw/tbar95.cpp | |
3 | // Purpose: wxToolBar | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
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 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/frame.h" | |
29 | #include "wx/log.h" | |
30 | #include "wx/intl.h" | |
31 | #include "wx/dynarray.h" | |
32 | #include "wx/settings.h" | |
33 | #include "wx/bitmap.h" | |
34 | #include "wx/dcmemory.h" | |
35 | #include "wx/control.h" | |
36 | #endif | |
37 | ||
38 | #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE && !defined(__SMARTPHONE__) | |
39 | ||
40 | #include "wx/toolbar.h" | |
41 | #include "wx/sysopt.h" | |
42 | #include "wx/image.h" | |
43 | ||
44 | #include "wx/msw/private.h" | |
45 | ||
46 | #if wxUSE_UXTHEME | |
47 | #include "wx/msw/uxtheme.h" | |
48 | #endif | |
49 | ||
50 | // include <commctrl.h> "properly" | |
51 | #include "wx/msw/wrapcctl.h" | |
52 | ||
53 | #include "wx/app.h" // for GetComCtl32Version | |
54 | ||
55 | // ---------------------------------------------------------------------------- | |
56 | // constants | |
57 | // ---------------------------------------------------------------------------- | |
58 | ||
59 | // these standard constants are not always defined in compilers headers | |
60 | ||
61 | // Styles | |
62 | #ifndef TBSTYLE_FLAT | |
63 | #define TBSTYLE_LIST 0x1000 | |
64 | #define TBSTYLE_FLAT 0x0800 | |
65 | #endif | |
66 | ||
67 | #ifndef TBSTYLE_TRANSPARENT | |
68 | #define TBSTYLE_TRANSPARENT 0x8000 | |
69 | #endif | |
70 | ||
71 | #ifndef TBSTYLE_TOOLTIPS | |
72 | #define TBSTYLE_TOOLTIPS 0x0100 | |
73 | #endif | |
74 | ||
75 | // Messages | |
76 | #ifndef TB_GETSTYLE | |
77 | #define TB_SETSTYLE (WM_USER + 56) | |
78 | #define TB_GETSTYLE (WM_USER + 57) | |
79 | #endif | |
80 | ||
81 | #ifndef TB_HITTEST | |
82 | #define TB_HITTEST (WM_USER + 69) | |
83 | #endif | |
84 | ||
85 | #ifndef TB_GETMAXSIZE | |
86 | #define TB_GETMAXSIZE (WM_USER + 83) | |
87 | #endif | |
88 | ||
89 | // these values correspond to those used by comctl32.dll | |
90 | #define DEFAULTBITMAPX 16 | |
91 | #define DEFAULTBITMAPY 15 | |
92 | ||
93 | // ---------------------------------------------------------------------------- | |
94 | // wxWin macros | |
95 | // ---------------------------------------------------------------------------- | |
96 | ||
97 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl) | |
98 | ||
99 | /* | |
100 | TOOLBAR PROPERTIES | |
101 | tool | |
102 | bitmap | |
103 | bitmap2 | |
104 | tooltip | |
105 | longhelp | |
106 | radio (bool) | |
107 | toggle (bool) | |
108 | separator | |
109 | style ( wxNO_BORDER | wxTB_HORIZONTAL) | |
110 | bitmapsize | |
111 | margins | |
112 | packing | |
113 | separation | |
114 | ||
115 | dontattachtoframe | |
116 | */ | |
117 | ||
118 | BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase) | |
119 | EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent) | |
120 | EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged) | |
121 | EVT_ERASE_BACKGROUND(wxToolBar::OnEraseBackground) | |
122 | END_EVENT_TABLE() | |
123 | ||
124 | // ---------------------------------------------------------------------------- | |
125 | // private classes | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
128 | class wxToolBarTool : public wxToolBarToolBase | |
129 | { | |
130 | public: | |
131 | wxToolBarTool(wxToolBar *tbar, | |
132 | int id, | |
133 | const wxString& label, | |
134 | const wxBitmap& bmpNormal, | |
135 | const wxBitmap& bmpDisabled, | |
136 | wxItemKind kind, | |
137 | wxObject *clientData, | |
138 | const wxString& shortHelp, | |
139 | const wxString& longHelp) | |
140 | : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind, | |
141 | clientData, shortHelp, longHelp) | |
142 | { | |
143 | m_nSepCount = 0; | |
144 | } | |
145 | ||
146 | wxToolBarTool(wxToolBar *tbar, wxControl *control) | |
147 | : wxToolBarToolBase(tbar, control) | |
148 | { | |
149 | m_nSepCount = 1; | |
150 | } | |
151 | ||
152 | virtual void SetLabel(const wxString& label) | |
153 | { | |
154 | if ( label == m_label ) | |
155 | return; | |
156 | ||
157 | wxToolBarToolBase::SetLabel(label); | |
158 | ||
159 | // we need to update the label shown in the toolbar because it has a | |
160 | // pointer to the internal buffer of the old label | |
161 | // | |
162 | // TODO: use TB_SETBUTTONINFO | |
163 | } | |
164 | ||
165 | // set/get the number of separators which we use to cover the space used by | |
166 | // a control in the toolbar | |
167 | void SetSeparatorsCount(size_t count) { m_nSepCount = count; } | |
168 | size_t GetSeparatorsCount() const { return m_nSepCount; } | |
169 | ||
170 | private: | |
171 | size_t m_nSepCount; | |
172 | ||
173 | DECLARE_NO_COPY_CLASS(wxToolBarTool) | |
174 | }; | |
175 | ||
176 | ||
177 | // ============================================================================ | |
178 | // implementation | |
179 | // ============================================================================ | |
180 | ||
181 | // ---------------------------------------------------------------------------- | |
182 | // wxToolBarTool | |
183 | // ---------------------------------------------------------------------------- | |
184 | ||
185 | wxToolBarToolBase *wxToolBar::CreateTool(int id, | |
186 | const wxString& label, | |
187 | const wxBitmap& bmpNormal, | |
188 | const wxBitmap& bmpDisabled, | |
189 | wxItemKind kind, | |
190 | wxObject *clientData, | |
191 | const wxString& shortHelp, | |
192 | const wxString& longHelp) | |
193 | { | |
194 | return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind, | |
195 | clientData, shortHelp, longHelp); | |
196 | } | |
197 | ||
198 | wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control) | |
199 | { | |
200 | return new wxToolBarTool(this, control); | |
201 | } | |
202 | ||
203 | // ---------------------------------------------------------------------------- | |
204 | // wxToolBar construction | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
207 | void wxToolBar::Init() | |
208 | { | |
209 | m_hBitmap = 0; | |
210 | m_disabledImgList = NULL; | |
211 | ||
212 | m_nButtons = 0; | |
213 | ||
214 | m_defaultWidth = DEFAULTBITMAPX; | |
215 | m_defaultHeight = DEFAULTBITMAPY; | |
216 | ||
217 | m_pInTool = 0; | |
218 | } | |
219 | ||
220 | bool wxToolBar::Create(wxWindow *parent, | |
221 | wxWindowID id, | |
222 | const wxPoint& pos, | |
223 | const wxSize& size, | |
224 | long style, | |
225 | const wxString& name) | |
226 | { | |
227 | // common initialisation | |
228 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) | |
229 | return false; | |
230 | ||
231 | // MSW-specific initialisation | |
232 | if ( !MSWCreateToolbar(pos, size) ) | |
233 | return false; | |
234 | ||
235 | wxSetCCUnicodeFormat(GetHwnd()); | |
236 | ||
237 | // set up the colors and fonts | |
238 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE)); | |
239 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); | |
240 | ||
241 | // workaround for flat toolbar on Windows XP classic style: we have to set | |
242 | // the style after creating the control; doing it at creation time doesn't work | |
243 | #if wxUSE_UXTHEME | |
244 | if ( style & wxTB_FLAT ) | |
245 | { | |
246 | LRESULT style = ::SendMessage(GetHwnd(), TB_GETSTYLE, 0, 0L); | |
247 | ||
248 | if ( !(style & TBSTYLE_FLAT) ) | |
249 | { | |
250 | ::SendMessage(GetHwnd(), TB_SETSTYLE, 0, style | TBSTYLE_FLAT); | |
251 | } | |
252 | } | |
253 | #endif // wxUSE_UXTHEME | |
254 | ||
255 | return true; | |
256 | } | |
257 | ||
258 | bool wxToolBar::MSWCreateToolbar(const wxPoint& pos, const wxSize& size) | |
259 | { | |
260 | if ( !MSWCreateControl(TOOLBARCLASSNAME, wxEmptyString, pos, size) ) | |
261 | return false; | |
262 | ||
263 | // toolbar-specific post initialisation | |
264 | ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0); | |
265 | ||
266 | return true; | |
267 | } | |
268 | ||
269 | void wxToolBar::Recreate() | |
270 | { | |
271 | const HWND hwndOld = GetHwnd(); | |
272 | if ( !hwndOld ) | |
273 | { | |
274 | // we haven't been created yet, no need to recreate | |
275 | return; | |
276 | } | |
277 | ||
278 | // get the position and size before unsubclassing the old toolbar | |
279 | const wxPoint pos = GetPosition(); | |
280 | const wxSize size = GetSize(); | |
281 | ||
282 | UnsubclassWin(); | |
283 | ||
284 | if ( !MSWCreateToolbar(pos, size) ) | |
285 | { | |
286 | // what can we do? | |
287 | wxFAIL_MSG( _T("recreating the toolbar failed") ); | |
288 | ||
289 | return; | |
290 | } | |
291 | ||
292 | // reparent all our children under the new toolbar | |
293 | for ( wxWindowList::compatibility_iterator node = m_children.GetFirst(); | |
294 | node; | |
295 | node = node->GetNext() ) | |
296 | { | |
297 | wxWindow *win = node->GetData(); | |
298 | if ( !win->IsTopLevel() ) | |
299 | ::SetParent(GetHwndOf(win), GetHwnd()); | |
300 | } | |
301 | ||
302 | // only destroy the old toolbar now -- | |
303 | // after all the children had been reparented | |
304 | ::DestroyWindow(hwndOld); | |
305 | ||
306 | // it is for the old bitmap control and can't be used with the new one | |
307 | if ( m_hBitmap ) | |
308 | { | |
309 | ::DeleteObject((HBITMAP) m_hBitmap); | |
310 | m_hBitmap = 0; | |
311 | } | |
312 | ||
313 | if ( m_disabledImgList ) | |
314 | { | |
315 | delete m_disabledImgList; | |
316 | m_disabledImgList = NULL; | |
317 | } | |
318 | ||
319 | Realize(); | |
320 | UpdateSize(); | |
321 | } | |
322 | ||
323 | wxToolBar::~wxToolBar() | |
324 | { | |
325 | // we must refresh the frame size when the toolbar is deleted but the frame | |
326 | // is not - otherwise toolbar leaves a hole in the place it used to occupy | |
327 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); | |
328 | if ( frame && !frame->IsBeingDeleted() ) | |
329 | frame->SendSizeEvent(); | |
330 | ||
331 | if ( m_hBitmap ) | |
332 | ::DeleteObject((HBITMAP) m_hBitmap); | |
333 | ||
334 | delete m_disabledImgList; | |
335 | } | |
336 | ||
337 | wxSize wxToolBar::DoGetBestSize() const | |
338 | { | |
339 | wxSize sizeBest; | |
340 | ||
341 | SIZE size; | |
342 | if ( !::SendMessage(GetHwnd(), TB_GETMAXSIZE, 0, (LPARAM)&size) ) | |
343 | { | |
344 | // maybe an old (< 0x400) Windows version? try to approximate the | |
345 | // toolbar size ourselves | |
346 | sizeBest = GetToolSize(); | |
347 | sizeBest.y += 2 * ::GetSystemMetrics(SM_CYBORDER); // Add borders | |
348 | sizeBest.x *= GetToolsCount(); | |
349 | ||
350 | // reverse horz and vertical components if necessary | |
351 | if ( HasFlag(wxTB_VERTICAL) ) | |
352 | { | |
353 | int t = sizeBest.x; | |
354 | sizeBest.x = sizeBest.y; | |
355 | sizeBest.y = t; | |
356 | } | |
357 | } | |
358 | else | |
359 | { | |
360 | sizeBest.x = size.cx; | |
361 | sizeBest.y = size.cy; | |
362 | } | |
363 | ||
364 | CacheBestSize(sizeBest); | |
365 | return sizeBest; | |
366 | } | |
367 | ||
368 | WXDWORD wxToolBar::MSWGetStyle(long style, WXDWORD *exstyle) const | |
369 | { | |
370 | // toolbars never have border, giving one to them results in broken | |
371 | // appearance | |
372 | WXDWORD msStyle = wxControl::MSWGetStyle | |
373 | ( | |
374 | (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle | |
375 | ); | |
376 | ||
377 | // always include this one, it never hurts and setting it later only if we | |
378 | // do have tooltips wouldn't work | |
379 | msStyle |= TBSTYLE_TOOLTIPS; | |
380 | ||
381 | if ( style & (wxTB_FLAT | wxTB_HORZ_LAYOUT) ) | |
382 | { | |
383 | // static as it doesn't change during the program lifetime | |
384 | static int s_verComCtl = wxApp::GetComCtl32Version(); | |
385 | ||
386 | // comctl32.dll 4.00 doesn't support the flat toolbars and using this | |
387 | // style with 6.00 (part of Windows XP) leads to the toolbar with | |
388 | // incorrect background colour - and not using it still results in the | |
389 | // correct (flat) toolbar, so don't use it there | |
390 | if ( s_verComCtl > 400 && s_verComCtl < 600 ) | |
391 | msStyle |= TBSTYLE_FLAT | TBSTYLE_TRANSPARENT; | |
392 | ||
393 | if ( s_verComCtl >= 470 && style & wxTB_HORZ_LAYOUT ) | |
394 | msStyle |= TBSTYLE_LIST; | |
395 | } | |
396 | ||
397 | if ( style & wxTB_NODIVIDER ) | |
398 | msStyle |= CCS_NODIVIDER; | |
399 | ||
400 | if ( style & wxTB_NOALIGN ) | |
401 | msStyle |= CCS_NOPARENTALIGN; | |
402 | ||
403 | if ( style & wxTB_VERTICAL ) | |
404 | msStyle |= CCS_VERT; | |
405 | ||
406 | return msStyle; | |
407 | } | |
408 | ||
409 | // ---------------------------------------------------------------------------- | |
410 | // adding/removing tools | |
411 | // ---------------------------------------------------------------------------- | |
412 | ||
413 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool) | |
414 | { | |
415 | // nothing special to do here - we really create the toolbar buttons in | |
416 | // Realize() later | |
417 | tool->Attach(this); | |
418 | ||
419 | InvalidateBestSize(); | |
420 | return true; | |
421 | } | |
422 | ||
423 | bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool) | |
424 | { | |
425 | // the main difficulty we have here is with the controls in the toolbars: | |
426 | // as we (sometimes) use several separators to cover up the space used by | |
427 | // them, the indices are not the same for us and the toolbar | |
428 | ||
429 | // first determine the position of the first button to delete: it may be | |
430 | // different from pos if we use several separators to cover the space used | |
431 | // by a control | |
432 | wxToolBarToolsList::compatibility_iterator node; | |
433 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
434 | { | |
435 | wxToolBarToolBase *tool2 = node->GetData(); | |
436 | if ( tool2 == tool ) | |
437 | { | |
438 | // let node point to the next node in the list | |
439 | node = node->GetNext(); | |
440 | ||
441 | break; | |
442 | } | |
443 | ||
444 | if ( tool2->IsControl() ) | |
445 | pos += ((wxToolBarTool *)tool2)->GetSeparatorsCount() - 1; | |
446 | } | |
447 | ||
448 | // now determine the number of buttons to delete and the area taken by them | |
449 | size_t nButtonsToDelete = 1; | |
450 | ||
451 | // get the size of the button we're going to delete | |
452 | RECT r; | |
453 | if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, pos, (LPARAM)&r) ) | |
454 | { | |
455 | wxLogLastError(_T("TB_GETITEMRECT")); | |
456 | } | |
457 | ||
458 | int width = r.right - r.left; | |
459 | ||
460 | if ( tool->IsControl() ) | |
461 | { | |
462 | nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount(); | |
463 | ||
464 | width *= nButtonsToDelete; | |
465 | } | |
466 | ||
467 | // do delete all buttons | |
468 | m_nButtons -= nButtonsToDelete; | |
469 | while ( nButtonsToDelete-- > 0 ) | |
470 | { | |
471 | if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) ) | |
472 | { | |
473 | wxLogLastError(wxT("TB_DELETEBUTTON")); | |
474 | ||
475 | return false; | |
476 | } | |
477 | } | |
478 | ||
479 | tool->Detach(); | |
480 | ||
481 | // and finally reposition all the controls after this button (the toolbar | |
482 | // takes care of all normal items) | |
483 | for ( /* node -> first after deleted */ ; node; node = node->GetNext() ) | |
484 | { | |
485 | wxToolBarToolBase *tool2 = node->GetData(); | |
486 | if ( tool2->IsControl() ) | |
487 | { | |
488 | int x; | |
489 | wxControl *control = tool2->GetControl(); | |
490 | control->GetPosition(&x, NULL); | |
491 | control->Move(x - width, wxDefaultCoord); | |
492 | } | |
493 | } | |
494 | ||
495 | InvalidateBestSize(); | |
496 | return true; | |
497 | } | |
498 | ||
499 | void wxToolBar::CreateDisabledImageList() | |
500 | { | |
501 | if (m_disabledImgList != NULL) | |
502 | { | |
503 | delete m_disabledImgList; | |
504 | m_disabledImgList = NULL; | |
505 | } | |
506 | ||
507 | // as we can't use disabled image list with older versions of comctl32.dll, | |
508 | // don't even bother creating it | |
509 | if ( wxTheApp->GetComCtl32Version() >= 470 ) | |
510 | { | |
511 | // search for the first disabled button img in the toolbar, if any | |
512 | for ( wxToolBarToolsList::compatibility_iterator | |
513 | node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
514 | { | |
515 | wxToolBarToolBase *tool = node->GetData(); | |
516 | wxBitmap bmpDisabled = tool->GetDisabledBitmap(); | |
517 | if ( bmpDisabled.Ok() ) | |
518 | { | |
519 | m_disabledImgList = new wxImageList | |
520 | ( | |
521 | m_defaultWidth, | |
522 | m_defaultHeight, | |
523 | bmpDisabled.GetMask() != NULL, | |
524 | GetToolsCount() | |
525 | ); | |
526 | break; | |
527 | } | |
528 | } | |
529 | ||
530 | // we don't have any disabled bitmaps | |
531 | } | |
532 | } | |
533 | ||
534 | bool wxToolBar::Realize() | |
535 | { | |
536 | const size_t nTools = GetToolsCount(); | |
537 | if ( nTools == 0 ) | |
538 | { | |
539 | // nothing to do | |
540 | return true; | |
541 | } | |
542 | ||
543 | const bool isVertical = HasFlag(wxTB_VERTICAL); | |
544 | ||
545 | bool doRemap, doRemapBg, doTransparent; | |
546 | doRemapBg = doRemap = doTransparent = false; | |
547 | ||
548 | #ifndef __WXWINCE__ | |
549 | int remapValue = (-1); | |
550 | if (wxSystemOptions::HasOption(wxT("msw.remap"))) | |
551 | remapValue = wxSystemOptions::GetOptionInt(wxT("msw.remap")); | |
552 | ||
553 | doTransparent = (remapValue == 2); | |
554 | if (!doTransparent) | |
555 | { | |
556 | doRemap = (remapValue != 0); | |
557 | doRemapBg = !doRemap; | |
558 | } | |
559 | #endif | |
560 | ||
561 | // delete all old buttons, if any | |
562 | for ( size_t pos = 0; pos < m_nButtons; pos++ ) | |
563 | { | |
564 | if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) ) | |
565 | { | |
566 | wxLogDebug(wxT("TB_DELETEBUTTON failed")); | |
567 | } | |
568 | } | |
569 | ||
570 | // First, add the bitmap: we use one bitmap for all toolbar buttons | |
571 | // ---------------------------------------------------------------- | |
572 | ||
573 | wxToolBarToolsList::compatibility_iterator node; | |
574 | int bitmapId = 0; | |
575 | ||
576 | wxSize sizeBmp; | |
577 | if ( HasFlag(wxTB_NOICONS) ) | |
578 | { | |
579 | // no icons, don't leave space for them | |
580 | sizeBmp.x = | |
581 | sizeBmp.y = 0; | |
582 | } | |
583 | else // do show icons | |
584 | { | |
585 | // if we already have a bitmap, we'll replace the existing one -- | |
586 | // otherwise we'll install a new one | |
587 | HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap; | |
588 | ||
589 | sizeBmp.x = m_defaultWidth; | |
590 | sizeBmp.y = m_defaultHeight; | |
591 | ||
592 | const wxCoord totalBitmapWidth = m_defaultWidth * | |
593 | wx_truncate_cast(wxCoord, nTools), | |
594 | totalBitmapHeight = m_defaultHeight; | |
595 | ||
596 | // Create a bitmap and copy all the tool bitmaps to it | |
597 | wxMemoryDC dcAllButtons; | |
598 | wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight); | |
599 | dcAllButtons.SelectObject(bitmap); | |
600 | ||
601 | #ifndef __WXWINCE__ | |
602 | if (doTransparent) | |
603 | dcAllButtons.SetBackground(*wxTRANSPARENT_BRUSH); | |
604 | else | |
605 | dcAllButtons.SetBackground(wxBrush(GetBackgroundColour())); | |
606 | #else | |
607 | dcAllButtons.SetBackground(wxBrush(wxColour(192,192,192))); | |
608 | #endif | |
609 | dcAllButtons.Clear(); | |
610 | ||
611 | m_hBitmap = bitmap.GetHBITMAP(); | |
612 | HBITMAP hBitmap = (HBITMAP)m_hBitmap; | |
613 | ||
614 | #ifndef __WXWINCE__ | |
615 | if (doRemapBg) | |
616 | { | |
617 | dcAllButtons.SelectObject(wxNullBitmap); | |
618 | ||
619 | // Even if we're not remapping the bitmap | |
620 | // content, we still have to remap the background. | |
621 | hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap, | |
622 | totalBitmapWidth, totalBitmapHeight); | |
623 | ||
624 | dcAllButtons.SelectObject(bitmap); | |
625 | } | |
626 | #endif // !__WXWINCE__ | |
627 | ||
628 | // the button position | |
629 | wxCoord x = 0; | |
630 | ||
631 | // the number of buttons (not separators) | |
632 | int nButtons = 0; | |
633 | ||
634 | CreateDisabledImageList(); | |
635 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
636 | { | |
637 | wxToolBarToolBase *tool = node->GetData(); | |
638 | if ( tool->IsButton() ) | |
639 | { | |
640 | const wxBitmap& bmp = tool->GetNormalBitmap(); | |
641 | ||
642 | const int w = bmp.GetWidth(); | |
643 | const int h = bmp.GetHeight(); | |
644 | ||
645 | if ( bmp.Ok() ) | |
646 | { | |
647 | int xOffset = wxMax(0, (m_defaultWidth - w)/2); | |
648 | int yOffset = wxMax(0, (m_defaultHeight - h)/2); | |
649 | ||
650 | // notice the last parameter: do use mask | |
651 | dcAllButtons.DrawBitmap(bmp, x + xOffset, yOffset, true); | |
652 | } | |
653 | else | |
654 | { | |
655 | wxFAIL_MSG( _T("invalid tool button bitmap") ); | |
656 | } | |
657 | ||
658 | // also deal with disabled bitmap if we want to use them | |
659 | if ( m_disabledImgList ) | |
660 | { | |
661 | wxBitmap bmpDisabled = tool->GetDisabledBitmap(); | |
662 | #if wxUSE_IMAGE | |
663 | if ( !bmpDisabled.Ok() ) | |
664 | { | |
665 | // no disabled bitmap specified but we still need to | |
666 | // fill the space in the image list with something, so | |
667 | // we grey out the normal bitmap | |
668 | wxImage imgGreyed; | |
669 | wxCreateGreyedImage(bmp.ConvertToImage(), imgGreyed); | |
670 | ||
671 | if (doRemap) | |
672 | { | |
673 | // we need to have light grey background colour for | |
674 | // MapBitmap() to work correctly | |
675 | for ( int y = 0; y < h; y++ ) | |
676 | { | |
677 | for ( int x = 0; x < w; x++ ) | |
678 | { | |
679 | if ( imgGreyed.IsTransparent(x, y) ) | |
680 | imgGreyed.SetRGB(x, y, | |
681 | wxLIGHT_GREY->Red(), | |
682 | wxLIGHT_GREY->Green(), | |
683 | wxLIGHT_GREY->Blue()); | |
684 | } | |
685 | } | |
686 | } | |
687 | ||
688 | bmpDisabled = wxBitmap(imgGreyed); | |
689 | } | |
690 | #endif // wxUSE_IMAGE | |
691 | ||
692 | if (doRemap) | |
693 | MapBitmap(bmpDisabled.GetHBITMAP(), w, h); | |
694 | ||
695 | m_disabledImgList->Add(bmpDisabled); | |
696 | } | |
697 | ||
698 | // still inc width and number of buttons because otherwise the | |
699 | // subsequent buttons will all be shifted which is rather confusing | |
700 | // (and like this you'd see immediately which bitmap was bad) | |
701 | x += m_defaultWidth; | |
702 | nButtons++; | |
703 | } | |
704 | } | |
705 | ||
706 | dcAllButtons.SelectObject(wxNullBitmap); | |
707 | ||
708 | // don't delete this HBITMAP! | |
709 | bitmap.SetHBITMAP(0); | |
710 | ||
711 | if (doRemap) | |
712 | { | |
713 | // Map to system colours | |
714 | hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap, | |
715 | totalBitmapWidth, totalBitmapHeight); | |
716 | } | |
717 | ||
718 | bool addBitmap = true; | |
719 | ||
720 | if ( oldToolBarBitmap ) | |
721 | { | |
722 | #ifdef TB_REPLACEBITMAP | |
723 | if ( wxApp::GetComCtl32Version() >= 400 ) | |
724 | { | |
725 | TBREPLACEBITMAP replaceBitmap; | |
726 | replaceBitmap.hInstOld = NULL; | |
727 | replaceBitmap.hInstNew = NULL; | |
728 | replaceBitmap.nIDOld = (UINT) oldToolBarBitmap; | |
729 | replaceBitmap.nIDNew = (UINT) hBitmap; | |
730 | replaceBitmap.nButtons = nButtons; | |
731 | if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP, | |
732 | 0, (LPARAM) &replaceBitmap) ) | |
733 | { | |
734 | wxFAIL_MSG(wxT("Could not replace the old bitmap")); | |
735 | } | |
736 | ||
737 | ::DeleteObject(oldToolBarBitmap); | |
738 | ||
739 | // already done | |
740 | addBitmap = false; | |
741 | } | |
742 | else | |
743 | #endif // TB_REPLACEBITMAP | |
744 | { | |
745 | // we can't replace the old bitmap, so we will add another one | |
746 | // (awfully inefficient, but what else to do?) and shift the bitmap | |
747 | // indices accordingly | |
748 | addBitmap = true; | |
749 | ||
750 | bitmapId = m_nButtons; | |
751 | } | |
752 | } | |
753 | ||
754 | if ( addBitmap ) // no old bitmap or we can't replace it | |
755 | { | |
756 | TBADDBITMAP addBitmap; | |
757 | addBitmap.hInst = 0; | |
758 | addBitmap.nID = (UINT) hBitmap; | |
759 | if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP, | |
760 | (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 ) | |
761 | { | |
762 | wxFAIL_MSG(wxT("Could not add bitmap to toolbar")); | |
763 | } | |
764 | } | |
765 | ||
766 | if ( m_disabledImgList ) | |
767 | { | |
768 | HIMAGELIST oldImageList = (HIMAGELIST) | |
769 | ::SendMessage(GetHwnd(), | |
770 | TB_SETDISABLEDIMAGELIST, | |
771 | 0, | |
772 | (LPARAM)GetHimagelistOf(m_disabledImgList)); | |
773 | ||
774 | // delete previous image list if any | |
775 | if ( oldImageList ) | |
776 | ::DeleteObject( oldImageList ); | |
777 | } | |
778 | } | |
779 | ||
780 | // don't call SetToolBitmapSize() as we don't want to change the values of | |
781 | // m_defaultWidth/Height | |
782 | if ( !::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, | |
783 | MAKELONG(sizeBmp.x, sizeBmp.y)) ) | |
784 | { | |
785 | wxLogLastError(_T("TB_SETBITMAPSIZE")); | |
786 | } | |
787 | ||
788 | // Next add the buttons and separators | |
789 | // ----------------------------------- | |
790 | ||
791 | TBBUTTON *buttons = new TBBUTTON[nTools]; | |
792 | ||
793 | // this array will hold the indices of all controls in the toolbar | |
794 | wxArrayInt controlIds; | |
795 | ||
796 | bool lastWasRadio = false; | |
797 | int i = 0; | |
798 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
799 | { | |
800 | wxToolBarToolBase *tool = node->GetData(); | |
801 | ||
802 | // don't add separators to the vertical toolbar with old comctl32.dll | |
803 | // versions as they didn't handle this properly | |
804 | if ( isVertical && tool->IsSeparator() && | |
805 | wxApp::GetComCtl32Version() <= 472 ) | |
806 | { | |
807 | continue; | |
808 | } | |
809 | ||
810 | TBBUTTON& button = buttons[i]; | |
811 | ||
812 | wxZeroMemory(button); | |
813 | ||
814 | bool isRadio = false; | |
815 | switch ( tool->GetStyle() ) | |
816 | { | |
817 | case wxTOOL_STYLE_CONTROL: | |
818 | button.idCommand = tool->GetId(); | |
819 | // fall through: create just a separator too | |
820 | ||
821 | case wxTOOL_STYLE_SEPARATOR: | |
822 | button.fsState = TBSTATE_ENABLED; | |
823 | button.fsStyle = TBSTYLE_SEP; | |
824 | break; | |
825 | ||
826 | case wxTOOL_STYLE_BUTTON: | |
827 | if ( !HasFlag(wxTB_NOICONS) ) | |
828 | button.iBitmap = bitmapId; | |
829 | ||
830 | if ( HasFlag(wxTB_TEXT) ) | |
831 | { | |
832 | const wxString& label = tool->GetLabel(); | |
833 | if ( !label.empty() ) | |
834 | { | |
835 | button.iString = (int)label.c_str(); | |
836 | } | |
837 | } | |
838 | ||
839 | button.idCommand = tool->GetId(); | |
840 | ||
841 | if ( tool->IsEnabled() ) | |
842 | button.fsState |= TBSTATE_ENABLED; | |
843 | if ( tool->IsToggled() ) | |
844 | button.fsState |= TBSTATE_CHECKED; | |
845 | ||
846 | switch ( tool->GetKind() ) | |
847 | { | |
848 | case wxITEM_RADIO: | |
849 | button.fsStyle = TBSTYLE_CHECKGROUP; | |
850 | ||
851 | if ( !lastWasRadio ) | |
852 | { | |
853 | // the first item in the radio group is checked by | |
854 | // default to be consistent with wxGTK and the menu | |
855 | // radio items | |
856 | button.fsState |= TBSTATE_CHECKED; | |
857 | ||
858 | if (tool->Toggle(true)) | |
859 | { | |
860 | DoToggleTool(tool, true); | |
861 | } | |
862 | } | |
863 | else if (tool->IsToggled()) | |
864 | { | |
865 | wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious(); | |
866 | int prevIndex = i - 1; | |
867 | while ( nodePrev ) | |
868 | { | |
869 | TBBUTTON& prevButton = buttons[prevIndex]; | |
870 | wxToolBarToolBase *tool = nodePrev->GetData(); | |
871 | if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO ) | |
872 | break; | |
873 | ||
874 | if ( tool->Toggle(false) ) | |
875 | DoToggleTool(tool, false); | |
876 | ||
877 | prevButton.fsState = TBSTATE_ENABLED; | |
878 | nodePrev = nodePrev->GetPrevious(); | |
879 | prevIndex--; | |
880 | } | |
881 | } | |
882 | ||
883 | isRadio = true; | |
884 | break; | |
885 | ||
886 | case wxITEM_CHECK: | |
887 | button.fsStyle = TBSTYLE_CHECK; | |
888 | break; | |
889 | ||
890 | default: | |
891 | wxFAIL_MSG( _T("unexpected toolbar button kind") ); | |
892 | // fall through | |
893 | ||
894 | case wxITEM_NORMAL: | |
895 | button.fsStyle = TBSTYLE_BUTTON; | |
896 | } | |
897 | ||
898 | bitmapId++; | |
899 | break; | |
900 | } | |
901 | ||
902 | lastWasRadio = isRadio; | |
903 | ||
904 | i++; | |
905 | } | |
906 | ||
907 | if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, (WPARAM)i, (LPARAM)buttons) ) | |
908 | { | |
909 | wxLogLastError(wxT("TB_ADDBUTTONS")); | |
910 | } | |
911 | ||
912 | delete [] buttons; | |
913 | ||
914 | // Deal with the controls finally | |
915 | // ------------------------------ | |
916 | ||
917 | // adjust the controls size to fit nicely in the toolbar | |
918 | int y = 0; | |
919 | size_t index = 0; | |
920 | for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ ) | |
921 | { | |
922 | wxToolBarToolBase *tool = node->GetData(); | |
923 | ||
924 | // we calculate the running y coord for vertical toolbars so we need to | |
925 | // get the items size for all items but for the horizontal ones we | |
926 | // don't need to deal with the non controls | |
927 | bool isControl = tool->IsControl(); | |
928 | if ( !isControl && !isVertical ) | |
929 | continue; | |
930 | ||
931 | // note that we use TB_GETITEMRECT and not TB_GETRECT because the | |
932 | // latter only appeared in v4.70 of comctl32.dll | |
933 | RECT r; | |
934 | if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, | |
935 | index, (LPARAM)(LPRECT)&r) ) | |
936 | { | |
937 | wxLogLastError(wxT("TB_GETITEMRECT")); | |
938 | } | |
939 | ||
940 | if ( !isControl ) | |
941 | { | |
942 | // can only be control if isVertical | |
943 | y += r.bottom - r.top; | |
944 | ||
945 | continue; | |
946 | } | |
947 | ||
948 | wxControl *control = tool->GetControl(); | |
949 | wxSize size = control->GetSize(); | |
950 | ||
951 | // the position of the leftmost controls corner | |
952 | int left = wxDefaultCoord; | |
953 | ||
954 | // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+ | |
955 | #ifdef TB_SETBUTTONINFO | |
956 | // available in headers, now check whether it is available now | |
957 | // (during run-time) | |
958 | if ( wxApp::GetComCtl32Version() >= 471 ) | |
959 | { | |
960 | // set the (underlying) separators width to be that of the | |
961 | // control | |
962 | TBBUTTONINFO tbbi; | |
963 | tbbi.cbSize = sizeof(tbbi); | |
964 | tbbi.dwMask = TBIF_SIZE; | |
965 | tbbi.cx = (WORD)size.x; | |
966 | if ( !::SendMessage(GetHwnd(), TB_SETBUTTONINFO, | |
967 | tool->GetId(), (LPARAM)&tbbi) ) | |
968 | { | |
969 | // the id is probably invalid? | |
970 | wxLogLastError(wxT("TB_SETBUTTONINFO")); | |
971 | } | |
972 | } | |
973 | else | |
974 | #endif // comctl32.dll 4.71 | |
975 | // TB_SETBUTTONINFO unavailable | |
976 | { | |
977 | // try adding several separators to fit the controls width | |
978 | int widthSep = r.right - r.left; | |
979 | left = r.left; | |
980 | ||
981 | TBBUTTON tbb; | |
982 | wxZeroMemory(tbb); | |
983 | tbb.idCommand = 0; | |
984 | tbb.fsState = TBSTATE_ENABLED; | |
985 | tbb.fsStyle = TBSTYLE_SEP; | |
986 | ||
987 | size_t nSeparators = size.x / widthSep; | |
988 | for ( size_t nSep = 0; nSep < nSeparators; nSep++ ) | |
989 | { | |
990 | if ( !::SendMessage(GetHwnd(), TB_INSERTBUTTON, | |
991 | index, (LPARAM)&tbb) ) | |
992 | { | |
993 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
994 | } | |
995 | ||
996 | index++; | |
997 | } | |
998 | ||
999 | // remember the number of separators we used - we'd have to | |
1000 | // delete all of them later | |
1001 | ((wxToolBarTool *)tool)->SetSeparatorsCount(nSeparators); | |
1002 | ||
1003 | // adjust the controls width to exactly cover the separators | |
1004 | control->SetSize((nSeparators + 1)*widthSep, wxDefaultCoord); | |
1005 | } | |
1006 | ||
1007 | // position the control itself correctly vertically | |
1008 | int height = r.bottom - r.top; | |
1009 | int diff = height - size.y; | |
1010 | if ( diff < 0 ) | |
1011 | { | |
1012 | // the control is too high, resize to fit | |
1013 | control->SetSize(wxDefaultCoord, height - 2); | |
1014 | ||
1015 | diff = 2; | |
1016 | } | |
1017 | ||
1018 | int top; | |
1019 | if ( isVertical ) | |
1020 | { | |
1021 | left = 0; | |
1022 | top = y; | |
1023 | ||
1024 | y += height + 2*GetMargins().y; | |
1025 | } | |
1026 | else // horizontal toolbar | |
1027 | { | |
1028 | if ( left == wxDefaultCoord ) | |
1029 | left = r.left; | |
1030 | ||
1031 | top = r.top; | |
1032 | } | |
1033 | ||
1034 | control->Move(left, top + (diff + 1) / 2); | |
1035 | } | |
1036 | ||
1037 | // the max index is the "real" number of buttons - i.e. counting even the | |
1038 | // separators which we added just for aligning the controls | |
1039 | m_nButtons = index; | |
1040 | ||
1041 | if ( !isVertical ) | |
1042 | { | |
1043 | if ( m_maxRows == 0 ) | |
1044 | { | |
1045 | // if not set yet, only one row | |
1046 | SetRows(1); | |
1047 | } | |
1048 | } | |
1049 | else if ( m_nButtons > 0 ) // vertical non empty toolbar | |
1050 | { | |
1051 | if ( m_maxRows == 0 ) | |
1052 | { | |
1053 | // if not set yet, have one column | |
1054 | SetRows(m_nButtons); | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | InvalidateBestSize(); | |
1059 | SetBestFittingSize(); | |
1060 | ||
1061 | return true; | |
1062 | } | |
1063 | ||
1064 | // ---------------------------------------------------------------------------- | |
1065 | // message handlers | |
1066 | // ---------------------------------------------------------------------------- | |
1067 | ||
1068 | bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id) | |
1069 | { | |
1070 | wxToolBarToolBase *tool = FindById((int)id); | |
1071 | if ( !tool ) | |
1072 | return false; | |
1073 | ||
1074 | bool toggled = false; // just to suppress warnings | |
1075 | ||
1076 | if ( tool->CanBeToggled() ) | |
1077 | { | |
1078 | LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0); | |
1079 | toggled = (state & TBSTATE_CHECKED) != 0; | |
1080 | ||
1081 | // ignore the event when a radio button is released, as this doesn't | |
1082 | // seem to happen at all, and is handled otherwise | |
1083 | if ( tool->GetKind() == wxITEM_RADIO && !toggled ) | |
1084 | return true; | |
1085 | ||
1086 | tool->Toggle(toggled); | |
1087 | UnToggleRadioGroup(tool); | |
1088 | } | |
1089 | ||
1090 | // OnLeftClick() can veto the button state change - for buttons which | |
1091 | // may be toggled only, of couse | |
1092 | if ( !OnLeftClick((int)id, toggled) && tool->CanBeToggled() ) | |
1093 | { | |
1094 | // revert back | |
1095 | tool->Toggle(!toggled); | |
1096 | ||
1097 | ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(!toggled, 0)); | |
1098 | } | |
1099 | ||
1100 | return true; | |
1101 | } | |
1102 | ||
1103 | bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl), | |
1104 | WXLPARAM lParam, | |
1105 | WXLPARAM *WXUNUSED(result)) | |
1106 | { | |
1107 | #if wxUSE_TOOLTIPS | |
1108 | // First check if this applies to us | |
1109 | NMHDR *hdr = (NMHDR *)lParam; | |
1110 | ||
1111 | // the tooltips control created by the toolbar is sometimes Unicode, even | |
1112 | // in an ANSI application - this seems to be a bug in comctl32.dll v5 | |
1113 | UINT code = hdr->code; | |
1114 | if ( (code != (UINT) TTN_NEEDTEXTA) && (code != (UINT) TTN_NEEDTEXTW) ) | |
1115 | return false; | |
1116 | ||
1117 | HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0); | |
1118 | if ( toolTipWnd != hdr->hwndFrom ) | |
1119 | return false; | |
1120 | ||
1121 | LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam; | |
1122 | int id = (int)ttText->hdr.idFrom; | |
1123 | ||
1124 | wxToolBarToolBase *tool = FindById(id); | |
1125 | if ( !tool ) | |
1126 | return false; | |
1127 | ||
1128 | return HandleTooltipNotify(code, lParam, tool->GetShortHelp()); | |
1129 | #else | |
1130 | wxUnusedVar(lParam); | |
1131 | ||
1132 | return false; | |
1133 | #endif | |
1134 | } | |
1135 | ||
1136 | // ---------------------------------------------------------------------------- | |
1137 | // toolbar geometry | |
1138 | // ---------------------------------------------------------------------------- | |
1139 | ||
1140 | void wxToolBar::SetToolBitmapSize(const wxSize& size) | |
1141 | { | |
1142 | wxToolBarBase::SetToolBitmapSize(size); | |
1143 | ||
1144 | ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y)); | |
1145 | } | |
1146 | ||
1147 | void wxToolBar::SetRows(int nRows) | |
1148 | { | |
1149 | if ( nRows == m_maxRows ) | |
1150 | { | |
1151 | // avoid resizing the frame uselessly | |
1152 | return; | |
1153 | } | |
1154 | ||
1155 | // TRUE in wParam means to create at least as many rows, FALSE - | |
1156 | // at most as many | |
1157 | RECT rect; | |
1158 | ::SendMessage(GetHwnd(), TB_SETROWS, | |
1159 | MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)), | |
1160 | (LPARAM) &rect); | |
1161 | ||
1162 | m_maxRows = nRows; | |
1163 | ||
1164 | UpdateSize(); | |
1165 | } | |
1166 | ||
1167 | // The button size is bigger than the bitmap size | |
1168 | wxSize wxToolBar::GetToolSize() const | |
1169 | { | |
1170 | // TB_GETBUTTONSIZE is supported from version 4.70 | |
1171 | #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \ | |
1172 | && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) \ | |
1173 | && !defined (__DIGITALMARS__) | |
1174 | if ( wxApp::GetComCtl32Version() >= 470 ) | |
1175 | { | |
1176 | DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0); | |
1177 | ||
1178 | return wxSize(LOWORD(dw), HIWORD(dw)); | |
1179 | } | |
1180 | else | |
1181 | #endif // comctl32.dll 4.70+ | |
1182 | { | |
1183 | // defaults | |
1184 | return wxSize(m_defaultWidth + 8, m_defaultHeight + 7); | |
1185 | } | |
1186 | } | |
1187 | ||
1188 | static | |
1189 | wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools, | |
1190 | size_t index ) | |
1191 | { | |
1192 | wxToolBarToolsList::compatibility_iterator current = tools.GetFirst(); | |
1193 | ||
1194 | for ( ; current ; current = current->GetNext() ) | |
1195 | { | |
1196 | if ( index == 0 ) | |
1197 | return current->GetData(); | |
1198 | ||
1199 | wxToolBarTool *tool = (wxToolBarTool *)current->GetData(); | |
1200 | size_t separators = tool->GetSeparatorsCount(); | |
1201 | ||
1202 | // if it is a normal button, sepcount == 0, so skip 1 item (the button) | |
1203 | // otherwise, skip as many items as the separator count, plus the | |
1204 | // control itself | |
1205 | index -= separators ? separators + 1 : 1; | |
1206 | } | |
1207 | ||
1208 | return 0; | |
1209 | } | |
1210 | ||
1211 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const | |
1212 | { | |
1213 | POINT pt; | |
1214 | pt.x = x; | |
1215 | pt.y = y; | |
1216 | int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt); | |
1217 | // MBN: when the point ( x, y ) is close to the toolbar border | |
1218 | // TB_HITTEST returns m_nButtons ( not -1 ) | |
1219 | if ( index < 0 || (size_t)index >= m_nButtons ) | |
1220 | { | |
1221 | // it's a separator or there is no tool at all there | |
1222 | return (wxToolBarToolBase *)NULL; | |
1223 | } | |
1224 | ||
1225 | // when TB_SETBUTTONINFO is available (both during compile- and run-time), | |
1226 | // we don't use the dummy separators hack | |
1227 | #ifdef TB_SETBUTTONINFO | |
1228 | if ( wxApp::GetComCtl32Version() >= 471 ) | |
1229 | { | |
1230 | return m_tools.Item((size_t)index)->GetData(); | |
1231 | } | |
1232 | else | |
1233 | #endif // TB_SETBUTTONINFO | |
1234 | { | |
1235 | return GetItemSkippingDummySpacers( m_tools, (size_t) index ); | |
1236 | } | |
1237 | } | |
1238 | ||
1239 | void wxToolBar::UpdateSize() | |
1240 | { | |
1241 | // the toolbar size changed | |
1242 | ::SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0); | |
1243 | ||
1244 | // we must also refresh the frame after the toolbar size (possibly) changed | |
1245 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); | |
1246 | if ( frame ) | |
1247 | { | |
1248 | frame->SendSizeEvent(); | |
1249 | } | |
1250 | } | |
1251 | ||
1252 | // ---------------------------------------------------------------------------- | |
1253 | // toolbar styles | |
1254 | // --------------------------------------------------------------------------- | |
1255 | ||
1256 | void wxToolBar::SetWindowStyleFlag(long style) | |
1257 | { | |
1258 | // the style bits whose changes force us to recreate the toolbar | |
1259 | static const long MASK_NEEDS_RECREATE = wxTB_TEXT | wxTB_NOICONS; | |
1260 | ||
1261 | const long styleOld = GetWindowStyle(); | |
1262 | ||
1263 | wxToolBarBase::SetWindowStyleFlag(style); | |
1264 | ||
1265 | // don't recreate an empty toolbar: not only this is unnecessary, but it is | |
1266 | // also fatal as we'd then try to recreate the toolbar when it's just being | |
1267 | // created | |
1268 | if ( GetToolsCount() && | |
1269 | (style & MASK_NEEDS_RECREATE) != (styleOld & MASK_NEEDS_RECREATE) ) | |
1270 | { | |
1271 | // to remove the text labels, simply re-realizing the toolbar is enough | |
1272 | // but I don't know of any way to add the text to an existing toolbar | |
1273 | // other than by recreating it entirely | |
1274 | Recreate(); | |
1275 | } | |
1276 | } | |
1277 | ||
1278 | // ---------------------------------------------------------------------------- | |
1279 | // tool state | |
1280 | // ---------------------------------------------------------------------------- | |
1281 | ||
1282 | void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable) | |
1283 | { | |
1284 | ::SendMessage(GetHwnd(), TB_ENABLEBUTTON, | |
1285 | (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0)); | |
1286 | } | |
1287 | ||
1288 | void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle) | |
1289 | { | |
1290 | ::SendMessage(GetHwnd(), TB_CHECKBUTTON, | |
1291 | (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0)); | |
1292 | } | |
1293 | ||
1294 | void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)) | |
1295 | { | |
1296 | // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or | |
1297 | // without, so we really need to delete the button and recreate it here | |
1298 | wxFAIL_MSG( _T("not implemented") ); | |
1299 | } | |
1300 | ||
1301 | // ---------------------------------------------------------------------------- | |
1302 | // event handlers | |
1303 | // ---------------------------------------------------------------------------- | |
1304 | ||
1305 | // Responds to colour changes, and passes event on to children. | |
1306 | void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event) | |
1307 | { | |
1308 | wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE)); | |
1309 | ||
1310 | // Remap the buttons | |
1311 | Realize(); | |
1312 | ||
1313 | // Relayout the toolbar | |
1314 | int nrows = m_maxRows; | |
1315 | m_maxRows = 0; // otherwise SetRows() wouldn't do anything | |
1316 | SetRows(nrows); | |
1317 | ||
1318 | Refresh(); | |
1319 | ||
1320 | // let the event propagate further | |
1321 | event.Skip(); | |
1322 | } | |
1323 | ||
1324 | void wxToolBar::OnMouseEvent(wxMouseEvent& event) | |
1325 | { | |
1326 | if (event.Leaving() && m_pInTool) | |
1327 | { | |
1328 | OnMouseEnter( -1 ); | |
1329 | event.Skip(); | |
1330 | return; | |
1331 | } | |
1332 | ||
1333 | if ( event.RightDown() ) | |
1334 | { | |
1335 | // find the tool under the mouse | |
1336 | wxCoord x,y; | |
1337 | event.GetPosition(&x, &y); | |
1338 | ||
1339 | wxToolBarToolBase *tool = FindToolForPosition(x, y); | |
1340 | OnRightClick(tool ? tool->GetId() : -1, x, y); | |
1341 | } | |
1342 | else | |
1343 | { | |
1344 | event.Skip(); | |
1345 | } | |
1346 | } | |
1347 | ||
1348 | // This handler is required to allow the toolbar to be set to a non-default | |
1349 | // colour: for example, when it must blend in with a notebook page. | |
1350 | void wxToolBar::OnEraseBackground(wxEraseEvent& event) | |
1351 | { | |
1352 | wxColour bgCol = GetBackgroundColour(); | |
1353 | if (!bgCol.Ok()) | |
1354 | { | |
1355 | event.Skip(); | |
1356 | return; | |
1357 | } | |
1358 | ||
1359 | // notice that this 'dumb' implementation may cause flicker for some of the | |
1360 | // controls in which case they should intercept wxEraseEvent and process it | |
1361 | // themselves somehow | |
1362 | ||
1363 | RECT rect; | |
1364 | ::GetClientRect(GetHwnd(), &rect); | |
1365 | ||
1366 | HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(bgCol)); | |
1367 | ||
1368 | HDC hdc = GetHdcOf((*event.GetDC())); | |
1369 | ||
1370 | #ifndef __WXWINCE__ | |
1371 | int mode = ::SetMapMode(hdc, MM_TEXT); | |
1372 | #endif | |
1373 | ||
1374 | ::FillRect(hdc, &rect, hBrush); | |
1375 | ::DeleteObject(hBrush); | |
1376 | ||
1377 | #ifndef __WXWINCE__ | |
1378 | ::SetMapMode(hdc, mode); | |
1379 | #endif | |
1380 | } | |
1381 | ||
1382 | bool wxToolBar::HandleSize(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam) | |
1383 | { | |
1384 | // calculate our minor dimension ourselves - we're confusing the standard | |
1385 | // logic (TB_AUTOSIZE) with our horizontal toolbars and other hacks | |
1386 | RECT r; | |
1387 | if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT, 0, (LPARAM)&r) ) | |
1388 | { | |
1389 | int w, h; | |
1390 | ||
1391 | if ( GetWindowStyle() & wxTB_VERTICAL ) | |
1392 | { | |
1393 | w = r.right - r.left; | |
1394 | if ( m_maxRows ) | |
1395 | { | |
1396 | w *= (m_nButtons + m_maxRows - 1)/m_maxRows; | |
1397 | } | |
1398 | h = HIWORD(lParam); | |
1399 | } | |
1400 | else | |
1401 | { | |
1402 | w = LOWORD(lParam); | |
1403 | if (HasFlag( wxTB_FLAT )) | |
1404 | h = r.bottom - r.top - 3; | |
1405 | else | |
1406 | h = r.bottom - r.top; | |
1407 | if ( m_maxRows ) | |
1408 | { | |
1409 | // FIXME: hardcoded separator line height... | |
1410 | h += HasFlag(wxTB_NODIVIDER) ? 4 : 6; | |
1411 | h *= m_maxRows; | |
1412 | } | |
1413 | } | |
1414 | ||
1415 | if ( MAKELPARAM(w, h) != lParam ) | |
1416 | { | |
1417 | // size really changed | |
1418 | SetSize(w, h); | |
1419 | } | |
1420 | ||
1421 | // message processed | |
1422 | return true; | |
1423 | } | |
1424 | ||
1425 | return false; | |
1426 | } | |
1427 | ||
1428 | bool wxToolBar::HandlePaint(WXWPARAM wParam, WXLPARAM lParam) | |
1429 | { | |
1430 | // erase any dummy separators which we used for aligning the controls if | |
1431 | // any here | |
1432 | ||
1433 | // first of all, do we have any controls at all? | |
1434 | wxToolBarToolsList::compatibility_iterator node; | |
1435 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
1436 | { | |
1437 | if ( node->GetData()->IsControl() ) | |
1438 | break; | |
1439 | } | |
1440 | ||
1441 | if ( !node ) | |
1442 | { | |
1443 | // no controls, nothing to erase | |
1444 | return false; | |
1445 | } | |
1446 | ||
1447 | // prepare the DC on which we'll be drawing | |
1448 | wxClientDC dc(this); | |
1449 | dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID)); | |
1450 | dc.SetPen(*wxTRANSPARENT_PEN); | |
1451 | ||
1452 | RECT r; | |
1453 | if ( !::GetUpdateRect(GetHwnd(), &r, FALSE) ) | |
1454 | { | |
1455 | // nothing to redraw anyhow | |
1456 | return false; | |
1457 | } | |
1458 | ||
1459 | wxRect rectUpdate; | |
1460 | wxCopyRECTToRect(r, rectUpdate); | |
1461 | ||
1462 | dc.SetClippingRegion(rectUpdate); | |
1463 | ||
1464 | // draw the toolbar tools, separators &c normally | |
1465 | wxControl::MSWWindowProc(WM_PAINT, wParam, lParam); | |
1466 | ||
1467 | // for each control in the toolbar find all the separators intersecting it | |
1468 | // and erase them | |
1469 | // | |
1470 | // NB: this is really the only way to do it as we don't know if a separator | |
1471 | // corresponds to a control (i.e. is a dummy one) or a real one | |
1472 | // otherwise | |
1473 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
1474 | { | |
1475 | wxToolBarToolBase *tool = node->GetData(); | |
1476 | if ( tool->IsControl() ) | |
1477 | { | |
1478 | // get the control rect in our client coords | |
1479 | wxControl *control = tool->GetControl(); | |
1480 | wxRect rectCtrl = control->GetRect(); | |
1481 | ||
1482 | // iterate over all buttons | |
1483 | TBBUTTON tbb; | |
1484 | int count = ::SendMessage(GetHwnd(), TB_BUTTONCOUNT, 0, 0); | |
1485 | for ( int n = 0; n < count; n++ ) | |
1486 | { | |
1487 | // is it a separator? | |
1488 | if ( !::SendMessage(GetHwnd(), TB_GETBUTTON, | |
1489 | n, (LPARAM)&tbb) ) | |
1490 | { | |
1491 | wxLogDebug(_T("TB_GETBUTTON failed?")); | |
1492 | ||
1493 | continue; | |
1494 | } | |
1495 | ||
1496 | if ( tbb.fsStyle != TBSTYLE_SEP ) | |
1497 | continue; | |
1498 | ||
1499 | // get the bounding rect of the separator | |
1500 | RECT r; | |
1501 | if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, | |
1502 | n, (LPARAM)&r) ) | |
1503 | { | |
1504 | wxLogDebug(_T("TB_GETITEMRECT failed?")); | |
1505 | ||
1506 | continue; | |
1507 | } | |
1508 | ||
1509 | // does it intersect the control? | |
1510 | wxRect rectItem; | |
1511 | wxCopyRECTToRect(r, rectItem); | |
1512 | if ( rectCtrl.Intersects(rectItem) ) | |
1513 | { | |
1514 | // yes, do erase it! | |
1515 | dc.DrawRectangle(rectItem); | |
1516 | ||
1517 | // Necessary in case we use a no-paint-on-size | |
1518 | // style in the parent: the controls can disappear | |
1519 | control->Refresh(false); | |
1520 | } | |
1521 | } | |
1522 | } | |
1523 | } | |
1524 | ||
1525 | return true; | |
1526 | } | |
1527 | ||
1528 | void wxToolBar::HandleMouseMove(WXWPARAM WXUNUSED(wParam), WXLPARAM lParam) | |
1529 | { | |
1530 | wxCoord x = GET_X_LPARAM(lParam), | |
1531 | y = GET_Y_LPARAM(lParam); | |
1532 | wxToolBarToolBase* tool = FindToolForPosition( x, y ); | |
1533 | ||
1534 | // cursor left current tool | |
1535 | if( tool != m_pInTool && !tool ) | |
1536 | { | |
1537 | m_pInTool = 0; | |
1538 | OnMouseEnter( -1 ); | |
1539 | } | |
1540 | ||
1541 | // cursor entered a tool | |
1542 | if( tool != m_pInTool && tool ) | |
1543 | { | |
1544 | m_pInTool = tool; | |
1545 | OnMouseEnter( tool->GetId() ); | |
1546 | } | |
1547 | } | |
1548 | ||
1549 | WXLRESULT wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
1550 | { | |
1551 | switch ( nMsg ) | |
1552 | { | |
1553 | case WM_MOUSEMOVE: | |
1554 | // we don't handle mouse moves, so always pass the message to | |
1555 | // wxControl::MSWWindowProc (HandleMouseMove just calls OnMouseEnter) | |
1556 | HandleMouseMove(wParam, lParam); | |
1557 | break; | |
1558 | ||
1559 | case WM_SIZE: | |
1560 | if ( HandleSize(wParam, lParam) ) | |
1561 | return 0; | |
1562 | break; | |
1563 | ||
1564 | #ifndef __WXWINCE__ | |
1565 | case WM_PAINT: | |
1566 | if ( HandlePaint(wParam, lParam) ) | |
1567 | return 0; | |
1568 | #endif | |
1569 | } | |
1570 | ||
1571 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
1572 | } | |
1573 | ||
1574 | // ---------------------------------------------------------------------------- | |
1575 | // private functions | |
1576 | // ---------------------------------------------------------------------------- | |
1577 | ||
1578 | WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height) | |
1579 | { | |
1580 | MemoryHDC hdcMem; | |
1581 | ||
1582 | if ( !hdcMem ) | |
1583 | { | |
1584 | wxLogLastError(_T("CreateCompatibleDC")); | |
1585 | ||
1586 | return bitmap; | |
1587 | } | |
1588 | ||
1589 | SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap); | |
1590 | ||
1591 | if ( !bmpInHDC ) | |
1592 | { | |
1593 | wxLogLastError(_T("SelectObject")); | |
1594 | ||
1595 | return bitmap; | |
1596 | } | |
1597 | ||
1598 | wxCOLORMAP *cmap = wxGetStdColourMap(); | |
1599 | ||
1600 | for ( int i = 0; i < width; i++ ) | |
1601 | { | |
1602 | for ( int j = 0; j < height; j++ ) | |
1603 | { | |
1604 | COLORREF pixel = ::GetPixel(hdcMem, i, j); | |
1605 | ||
1606 | for ( size_t k = 0; k < wxSTD_COL_MAX; k++ ) | |
1607 | { | |
1608 | COLORREF col = cmap[k].from; | |
1609 | if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 && | |
1610 | abs(GetGValue(pixel) - GetGValue(col)) < 10 && | |
1611 | abs(GetBValue(pixel) - GetBValue(col)) < 10 ) | |
1612 | { | |
1613 | ::SetPixel(hdcMem, i, j, cmap[k].to); | |
1614 | break; | |
1615 | } | |
1616 | } | |
1617 | } | |
1618 | } | |
1619 | ||
1620 | return bitmap; | |
1621 | ||
1622 | // VZ: I leave here my attempts to map the bitmap to the system colours | |
1623 | // faster by using BitBlt() even though it's broken currently - but | |
1624 | // maybe someone else can finish it? It should be faster than iterating | |
1625 | // over all pixels... | |
1626 | #if 0 | |
1627 | MemoryHDC hdcMask, hdcDst; | |
1628 | if ( !hdcMask || !hdcDst ) | |
1629 | { | |
1630 | wxLogLastError(_T("CreateCompatibleDC")); | |
1631 | ||
1632 | return bitmap; | |
1633 | } | |
1634 | ||
1635 | // create the target bitmap | |
1636 | HBITMAP hbmpDst = ::CreateCompatibleBitmap(hdcDst, width, height); | |
1637 | if ( !hbmpDst ) | |
1638 | { | |
1639 | wxLogLastError(_T("CreateCompatibleBitmap")); | |
1640 | ||
1641 | return bitmap; | |
1642 | } | |
1643 | ||
1644 | // create the monochrome mask bitmap | |
1645 | HBITMAP hbmpMask = ::CreateBitmap(width, height, 1, 1, 0); | |
1646 | if ( !hbmpMask ) | |
1647 | { | |
1648 | wxLogLastError(_T("CreateBitmap(mono)")); | |
1649 | ||
1650 | ::DeleteObject(hbmpDst); | |
1651 | ||
1652 | return bitmap; | |
1653 | } | |
1654 | ||
1655 | SelectInHDC bmpInDst(hdcDst, hbmpDst), | |
1656 | bmpInMask(hdcMask, hbmpMask); | |
1657 | ||
1658 | // for each colour: | |
1659 | for ( n = 0; n < NUM_OF_MAPPED_COLOURS; n++ ) | |
1660 | { | |
1661 | // create the mask for this colour | |
1662 | ::SetBkColor(hdcMem, ColorMap[n].from); | |
1663 | ::BitBlt(hdcMask, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY); | |
1664 | ||
1665 | // replace this colour with the target one in the dst bitmap | |
1666 | HBRUSH hbr = ::CreateSolidBrush(ColorMap[n].to); | |
1667 | HGDIOBJ hbrOld = ::SelectObject(hdcDst, hbr); | |
1668 | ||
1669 | ::MaskBlt(hdcDst, 0, 0, width, height, | |
1670 | hdcMem, 0, 0, | |
1671 | hbmpMask, 0, 0, | |
1672 | MAKEROP4(PATCOPY, SRCCOPY)); | |
1673 | ||
1674 | (void)::SelectObject(hdcDst, hbrOld); | |
1675 | ::DeleteObject(hbr); | |
1676 | } | |
1677 | ||
1678 | ::DeleteObject((HBITMAP)bitmap); | |
1679 | ||
1680 | return (WXHBITMAP)hbmpDst; | |
1681 | #endif // 0 | |
1682 | } | |
1683 | ||
1684 | #endif // wxUSE_TOOLBAR && Win95 | |
1685 |