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