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