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