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