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