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