]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
1c383dba | 2 | // Name: msw/tbar95.cpp |
8a0681f9 | 3 | // Purpose: wxToolBar |
2bda0e17 KB |
4 | // Author: Julian Smart |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
89b892a2 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
1c383dba VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
1c383dba | 21 | #pragma implementation "tbar95.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
1c383dba | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
8a0681f9 | 32 | #include "wx/frame.h" |
1c383dba VZ |
33 | #include "wx/log.h" |
34 | #include "wx/intl.h" | |
35 | #include "wx/dynarray.h" | |
4e15f6c5 | 36 | #include "wx/settings.h" |
381dd4bf | 37 | #include "wx/bitmap.h" |
f6bcfd97 | 38 | #include "wx/dcmemory.h" |
f538710f | 39 | #include "wx/control.h" |
2bda0e17 KB |
40 | #endif |
41 | ||
8a0681f9 VZ |
42 | #if wxUSE_TOOLBAR && defined(__WIN95__) && wxUSE_TOOLBAR_NATIVE |
43 | ||
44 | #include "wx/toolbar.h" | |
2bda0e17 | 45 | |
ce3ed50d | 46 | #if !defined(__GNUWIN32__) && !defined(__SALFORDC__) |
1c383dba | 47 | #include "malloc.h" |
2bda0e17 KB |
48 | #endif |
49 | ||
1c383dba | 50 | #include "wx/msw/private.h" |
2bda0e17 | 51 | |
57c208c5 | 52 | #ifndef __TWIN32__ |
1c383dba | 53 | |
ae090fdb | 54 | #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__)) |
1c383dba | 55 | #include <commctrl.h> |
ae090fdb JS |
56 | #else |
57 | #include "wx/msw/gnuwin32/extra.h" | |
65fd5cb0 | 58 | #endif |
2bda0e17 | 59 | |
1c383dba VZ |
60 | #endif // __TWIN32__ |
61 | ||
2bda0e17 | 62 | #include "wx/msw/dib.h" |
1c383dba VZ |
63 | #include "wx/app.h" // for GetComCtl32Version |
64 | ||
de85a884 VZ |
65 | #if defined(__MWERKS__) && defined(__WXMSW__) |
66 | // including <windef.h> for max definition doesn't seem | |
67 | // to work using CodeWarrior 6 Windows. So we define it | |
68 | // here. (Otherwise we get a undefined identifier 'max' | |
69 | // later on in this file.) (Added by dimitri@shortcut.nl) | |
70 | # ifndef max | |
71 | # define max(a,b) (((a) > (b)) ? (a) : (b)) | |
72 | # endif | |
73 | ||
74 | #endif | |
75 | ||
f6bcfd97 BP |
76 | // ---------------------------------------------------------------------------- |
77 | // conditional compilation | |
78 | // ---------------------------------------------------------------------------- | |
79 | ||
80 | // wxWindows previously always considered that toolbar buttons have light grey | |
81 | // (0xc0c0c0) background and so ignored any bitmap masks - however, this | |
82 | // doesn't work with XPMs which then appear to have black background. To make | |
83 | // this work, we must respect the bitmap masks - which we do now. This should | |
84 | // be ok in any case, but to restore 100% compatible with the old version | |
85 | // behaviour, you can set this to 0. | |
86 | #define USE_BITMAP_MASKS 1 | |
87 | ||
1c383dba VZ |
88 | // ---------------------------------------------------------------------------- |
89 | // constants | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
92 | // these standard constants are not always defined in compilers headers | |
2bda0e17 | 93 | |
6a23cbce | 94 | // Styles |
bb6290e3 | 95 | #ifndef TBSTYLE_FLAT |
1c383dba VZ |
96 | #define TBSTYLE_LIST 0x1000 |
97 | #define TBSTYLE_FLAT 0x0800 | |
dd177170 RL |
98 | #endif |
99 | ||
100 | #ifndef TBSTYLE_TRANSPARENT | |
1c383dba | 101 | #define TBSTYLE_TRANSPARENT 0x8000 |
bb6290e3 | 102 | #endif |
bb6290e3 | 103 | |
6a23cbce JS |
104 | // Messages |
105 | #ifndef TB_GETSTYLE | |
1c383dba | 106 | #define TB_SETSTYLE (WM_USER + 56) |
8a0681f9 VZ |
107 | #define TB_GETSTYLE (WM_USER + 57) |
108 | #endif | |
109 | ||
110 | #ifndef TB_HITTEST | |
111 | #define TB_HITTEST (WM_USER + 69) | |
6a23cbce JS |
112 | #endif |
113 | ||
1c383dba | 114 | // these values correspond to those used by comctl32.dll |
81d66cf3 JS |
115 | #define DEFAULTBITMAPX 16 |
116 | #define DEFAULTBITMAPY 15 | |
117 | #define DEFAULTBUTTONX 24 | |
118 | #define DEFAULTBUTTONY 24 | |
119 | #define DEFAULTBARHEIGHT 27 | |
120 | ||
1c383dba VZ |
121 | // ---------------------------------------------------------------------------- |
122 | // wxWin macros | |
123 | // ---------------------------------------------------------------------------- | |
124 | ||
12ed316d | 125 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase) |
2bda0e17 | 126 | |
8a0681f9 VZ |
127 | BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase) |
128 | EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent) | |
129 | EVT_SYS_COLOUR_CHANGED(wxToolBar::OnSysColourChanged) | |
2bda0e17 | 130 | END_EVENT_TABLE() |
2bda0e17 | 131 | |
8a0681f9 VZ |
132 | // ---------------------------------------------------------------------------- |
133 | // private classes | |
134 | // ---------------------------------------------------------------------------- | |
135 | ||
136 | class wxToolBarTool : public wxToolBarToolBase | |
137 | { | |
138 | public: | |
139 | wxToolBarTool(wxToolBar *tbar, | |
140 | int id, | |
141 | const wxBitmap& bitmap1, | |
142 | const wxBitmap& bitmap2, | |
143 | bool toggle, | |
144 | wxObject *clientData, | |
145 | const wxString& shortHelpString, | |
146 | const wxString& longHelpString) | |
147 | : wxToolBarToolBase(tbar, id, bitmap1, bitmap2, toggle, | |
148 | clientData, shortHelpString, longHelpString) | |
149 | { | |
150 | m_nSepCount = 0; | |
151 | } | |
152 | ||
153 | wxToolBarTool(wxToolBar *tbar, wxControl *control) | |
154 | : wxToolBarToolBase(tbar, control) | |
155 | { | |
156 | m_nSepCount = 1; | |
157 | } | |
158 | ||
159 | // set/get the number of separators which we use to cover the space used by | |
160 | // a control in the toolbar | |
161 | void SetSeparatorsCount(size_t count) { m_nSepCount = count; } | |
162 | size_t GetSeparatorsCount() const { return m_nSepCount; } | |
163 | ||
164 | private: | |
165 | size_t m_nSepCount; | |
166 | }; | |
167 | ||
168 | ||
1c383dba VZ |
169 | // ============================================================================ |
170 | // implementation | |
171 | // ============================================================================ | |
2bda0e17 | 172 | |
1c383dba | 173 | // ---------------------------------------------------------------------------- |
8a0681f9 | 174 | // wxToolBarTool |
1c383dba VZ |
175 | // ---------------------------------------------------------------------------- |
176 | ||
8a0681f9 VZ |
177 | wxToolBarToolBase *wxToolBar::CreateTool(int id, |
178 | const wxBitmap& bitmap1, | |
179 | const wxBitmap& bitmap2, | |
180 | bool toggle, | |
181 | wxObject *clientData, | |
182 | const wxString& shortHelpString, | |
183 | const wxString& longHelpString) | |
184 | { | |
185 | return new wxToolBarTool(this, id, bitmap1, bitmap2, toggle, | |
186 | clientData, shortHelpString, longHelpString); | |
187 | } | |
188 | ||
189 | wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control) | |
190 | { | |
191 | return new wxToolBarTool(this, control); | |
192 | } | |
193 | ||
194 | // ---------------------------------------------------------------------------- | |
195 | // wxToolBar construction | |
196 | // ---------------------------------------------------------------------------- | |
197 | ||
198 | void wxToolBar::Init() | |
2bda0e17 | 199 | { |
1c383dba | 200 | m_hBitmap = 0; |
8a0681f9 VZ |
201 | |
202 | m_nButtons = 0; | |
203 | ||
1c383dba VZ |
204 | m_defaultWidth = DEFAULTBITMAPX; |
205 | m_defaultHeight = DEFAULTBITMAPY; | |
37d0bdff MB |
206 | |
207 | m_pInTool = 0; | |
2bda0e17 KB |
208 | } |
209 | ||
8a0681f9 VZ |
210 | bool wxToolBar::Create(wxWindow *parent, |
211 | wxWindowID id, | |
212 | const wxPoint& pos, | |
213 | const wxSize& size, | |
214 | long style, | |
215 | const wxString& name) | |
2bda0e17 | 216 | { |
a4aad4de VZ |
217 | // toolbars never have border, giving one to them results in broken |
218 | // appearance | |
219 | style &= ~wxBORDER_MASK; | |
220 | style |= wxBORDER_NONE; | |
221 | ||
1c383dba | 222 | // common initialisation |
11b6a93b | 223 | if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) ) |
1c383dba | 224 | return FALSE; |
89b892a2 | 225 | |
1c383dba VZ |
226 | // prepare flags |
227 | DWORD msflags = 0; // WS_VISIBLE | WS_CHILD always included | |
c25a510b | 228 | |
b0766406 JS |
229 | if ( style & wxCLIP_SIBLINGS ) |
230 | msflags |= WS_CLIPSIBLINGS; | |
231 | ||
c25a510b | 232 | #ifdef TBSTYLE_TOOLTIPS |
1c383dba | 233 | msflags |= TBSTYLE_TOOLTIPS; |
c25a510b | 234 | #endif |
2bda0e17 | 235 | |
1c383dba VZ |
236 | if (style & wxTB_FLAT) |
237 | { | |
7d6d3bf3 VZ |
238 | // static as it doesn't change during the program lifetime |
239 | static int s_verComCtl = wxTheApp->GetComCtl32Version(); | |
240 | ||
241 | // comctl32.dll 4.00 doesn't support the flat toolbars and using this | |
242 | // style with 6.00 (part of Windows XP) leads to the toolbar with | |
243 | // incorrect background colour - and not using it still results in the | |
244 | // correct (flat) toolbar, so don't use it there | |
245 | if ( s_verComCtl > 400 && s_verComCtl < 600 ) | |
246 | { | |
247 | msflags |= TBSTYLE_FLAT | TBSTYLE_TRANSPARENT; | |
248 | } | |
1c383dba | 249 | } |
2bda0e17 | 250 | |
1c383dba VZ |
251 | // MSW-specific initialisation |
252 | if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME, msflags) ) | |
253 | return FALSE; | |
2bda0e17 | 254 | |
c8f1f088 | 255 | // toolbar-specific post initialisation |
1c383dba | 256 | ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0); |
89b892a2 | 257 | |
c8f1f088 | 258 | // set up the colors and fonts |
7d6d3bf3 | 259 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR)); |
a756f210 | 260 | SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); |
c8f1f088 | 261 | |
1c383dba VZ |
262 | // position it |
263 | int x = pos.x; | |
264 | int y = pos.y; | |
265 | int width = size.x; | |
266 | int height = size.y; | |
2bda0e17 | 267 | |
1c383dba VZ |
268 | if (width <= 0) |
269 | width = 100; | |
270 | if (height <= 0) | |
271 | height = m_defaultHeight; | |
272 | if (x < 0) | |
273 | x = 0; | |
274 | if (y < 0) | |
275 | y = 0; | |
bb6290e3 | 276 | |
1c383dba | 277 | SetSize(x, y, width, height); |
2bda0e17 | 278 | |
1c383dba | 279 | return TRUE; |
2bda0e17 KB |
280 | } |
281 | ||
8a0681f9 | 282 | wxToolBar::~wxToolBar() |
2bda0e17 | 283 | { |
f6bcfd97 BP |
284 | // we must refresh the frame size when the toolbar is deleted but the frame |
285 | // is not - otherwise toolbar leaves a hole in the place it used to occupy | |
f6bcfd97 | 286 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); |
c2fd78b1 | 287 | if ( frame && !frame->IsBeingDeleted() ) |
f6bcfd97 BP |
288 | { |
289 | frame->SendSizeEvent(); | |
290 | } | |
291 | ||
292 | if ( m_hBitmap ) | |
1c383dba VZ |
293 | { |
294 | ::DeleteObject((HBITMAP) m_hBitmap); | |
295 | } | |
296 | } | |
297 | ||
bdc72a22 | 298 | // ---------------------------------------------------------------------------- |
8a0681f9 | 299 | // adding/removing tools |
bdc72a22 VZ |
300 | // ---------------------------------------------------------------------------- |
301 | ||
8a0681f9 VZ |
302 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), |
303 | wxToolBarToolBase *tool) | |
1c383dba | 304 | { |
8a0681f9 VZ |
305 | // nothing special to do here - we really create the toolbar buttons in |
306 | // Realize() later | |
307 | tool->Attach(this); | |
308 | ||
309 | return TRUE; | |
1c383dba VZ |
310 | } |
311 | ||
8a0681f9 | 312 | bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool) |
bdc72a22 | 313 | { |
f6bcfd97 BP |
314 | // the main difficulty we have here is with the controls in the toolbars: |
315 | // as we (sometimes) use several separators to cover up the space used by | |
316 | // them, the indices are not the same for us and the toolbar | |
317 | ||
318 | // first determine the position of the first button to delete: it may be | |
319 | // different from pos if we use several separators to cover the space used | |
320 | // by a control | |
321 | wxToolBarToolsList::Node *node; | |
322 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
323 | { | |
324 | wxToolBarToolBase *tool2 = node->GetData(); | |
325 | if ( tool2 == tool ) | |
326 | { | |
327 | // let node point to the next node in the list | |
328 | node = node->GetNext(); | |
329 | ||
330 | break; | |
331 | } | |
332 | ||
333 | if ( tool2->IsControl() ) | |
334 | { | |
56bd6aac | 335 | pos += ((wxToolBarTool *)tool2)->GetSeparatorsCount() - 1; |
f6bcfd97 BP |
336 | } |
337 | } | |
338 | ||
339 | // now determine the number of buttons to delete and the area taken by them | |
8a0681f9 | 340 | size_t nButtonsToDelete = 1; |
bdc72a22 | 341 | |
8a0681f9 VZ |
342 | // get the size of the button we're going to delete |
343 | RECT r; | |
344 | if ( !::SendMessage(GetHwnd(), TB_GETITEMRECT, pos, (LPARAM)&r) ) | |
bdc72a22 | 345 | { |
8a0681f9 | 346 | wxLogLastError(_T("TB_GETITEMRECT")); |
bdc72a22 VZ |
347 | } |
348 | ||
8a0681f9 | 349 | int width = r.right - r.left; |
bdc72a22 | 350 | |
8a0681f9 VZ |
351 | if ( tool->IsControl() ) |
352 | { | |
353 | nButtonsToDelete = ((wxToolBarTool *)tool)->GetSeparatorsCount(); | |
bdc72a22 | 354 | |
8a0681f9 VZ |
355 | width *= nButtonsToDelete; |
356 | } | |
bdc72a22 | 357 | |
f6bcfd97 BP |
358 | // do delete all buttons |
359 | m_nButtons -= nButtonsToDelete; | |
8a0681f9 VZ |
360 | while ( nButtonsToDelete-- > 0 ) |
361 | { | |
362 | if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, pos, 0) ) | |
363 | { | |
f6bcfd97 | 364 | wxLogLastError(wxT("TB_DELETEBUTTON")); |
1c383dba | 365 | |
8a0681f9 VZ |
366 | return FALSE; |
367 | } | |
368 | } | |
1c383dba | 369 | |
8a0681f9 | 370 | tool->Detach(); |
1c383dba | 371 | |
f6bcfd97 BP |
372 | // and finally reposition all the controls after this button (the toolbar |
373 | // takes care of all normal items) | |
374 | for ( /* node -> first after deleted */ ; node; node = node->GetNext() ) | |
8a0681f9 VZ |
375 | { |
376 | wxToolBarToolBase *tool2 = node->GetData(); | |
377 | if ( tool2->IsControl() ) | |
378 | { | |
379 | int x; | |
380 | wxControl *control = tool2->GetControl(); | |
381 | control->GetPosition(&x, NULL); | |
382 | control->Move(x - width, -1); | |
383 | } | |
384 | } | |
1c383dba VZ |
385 | |
386 | return TRUE; | |
387 | } | |
388 | ||
8a0681f9 | 389 | bool wxToolBar::Realize() |
1c383dba | 390 | { |
8a0681f9 VZ |
391 | size_t nTools = GetToolsCount(); |
392 | if ( nTools == 0 ) | |
393 | { | |
394 | // nothing to do | |
395 | return TRUE; | |
396 | } | |
1c383dba | 397 | |
8a0681f9 | 398 | bool isVertical = (GetWindowStyle() & wxTB_VERTICAL) != 0; |
2bda0e17 | 399 | |
8a0681f9 VZ |
400 | // First, add the bitmap: we use one bitmap for all toolbar buttons |
401 | // ---------------------------------------------------------------- | |
2bda0e17 | 402 | |
8a0681f9 VZ |
403 | // if we already have a bitmap, we'll replace the existing one - otherwise |
404 | // we'll install a new one | |
405 | HBITMAP oldToolBarBitmap = (HBITMAP)m_hBitmap; | |
89b892a2 | 406 | |
1c383dba VZ |
407 | int totalBitmapWidth = (int)(m_defaultWidth * nTools); |
408 | int totalBitmapHeight = (int)m_defaultHeight; | |
2bda0e17 | 409 | |
f6bcfd97 BP |
410 | // Create a bitmap and copy all the tool bitmaps to it |
411 | #if USE_BITMAP_MASKS | |
412 | wxMemoryDC dcAllButtons; | |
413 | wxBitmap bitmap(totalBitmapWidth, totalBitmapHeight); | |
414 | dcAllButtons.SelectObject(bitmap); | |
415 | dcAllButtons.SetBackground(*wxLIGHT_GREY_BRUSH); | |
416 | dcAllButtons.Clear(); | |
417 | ||
418 | m_hBitmap = bitmap.GetHBITMAP(); | |
419 | HBITMAP hBitmap = (HBITMAP)m_hBitmap; | |
420 | #else // !USE_BITMAP_MASKS | |
8a0681f9 VZ |
421 | HBITMAP hBitmap = ::CreateCompatibleBitmap(ScreenHDC(), |
422 | totalBitmapWidth, | |
423 | totalBitmapHeight); | |
424 | if ( !hBitmap ) | |
425 | { | |
426 | wxLogLastError(_T("CreateCompatibleBitmap")); | |
427 | ||
428 | return FALSE; | |
429 | } | |
430 | ||
431 | m_hBitmap = (WXHBITMAP)hBitmap; | |
89b892a2 | 432 | |
1c383dba | 433 | HDC memoryDC = ::CreateCompatibleDC(NULL); |
8a0681f9 | 434 | HBITMAP oldBitmap = (HBITMAP) ::SelectObject(memoryDC, hBitmap); |
2bda0e17 | 435 | |
1c383dba | 436 | HDC memoryDC2 = ::CreateCompatibleDC(NULL); |
f6bcfd97 | 437 | #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS |
2bda0e17 | 438 | |
1c383dba VZ |
439 | // the button position |
440 | wxCoord x = 0; | |
2bda0e17 | 441 | |
1c383dba | 442 | // the number of buttons (not separators) |
8a0681f9 | 443 | int nButtons = 0; |
1c383dba | 444 | |
8a0681f9 VZ |
445 | wxToolBarToolsList::Node *node = m_tools.GetFirst(); |
446 | while ( node ) | |
051205e6 | 447 | { |
8a0681f9 VZ |
448 | wxToolBarToolBase *tool = node->GetData(); |
449 | if ( tool->IsButton() ) | |
1c383dba | 450 | { |
f6bcfd97 BP |
451 | const wxBitmap& bmp = tool->GetBitmap1(); |
452 | if ( bmp.Ok() ) | |
1c383dba | 453 | { |
f6bcfd97 BP |
454 | #if USE_BITMAP_MASKS |
455 | // notice the last parameter: do use mask | |
456 | dcAllButtons.DrawBitmap(tool->GetBitmap1(), x, 0, TRUE); | |
457 | #else // !USE_BITMAP_MASKS | |
458 | HBITMAP hbmp = GetHbitmapOf(bmp); | |
1c383dba VZ |
459 | HBITMAP oldBitmap2 = (HBITMAP)::SelectObject(memoryDC2, hbmp); |
460 | if ( !BitBlt(memoryDC, x, 0, m_defaultWidth, m_defaultHeight, | |
461 | memoryDC2, 0, 0, SRCCOPY) ) | |
462 | { | |
f6bcfd97 | 463 | wxLogLastError(wxT("BitBlt")); |
1c383dba VZ |
464 | } |
465 | ||
466 | ::SelectObject(memoryDC2, oldBitmap2); | |
f6bcfd97 | 467 | #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS |
1c383dba | 468 | } |
8a0681f9 VZ |
469 | else |
470 | { | |
471 | wxFAIL_MSG( _T("invalid tool button bitmap") ); | |
472 | } | |
473 | ||
474 | // still inc width and number of buttons because otherwise the | |
475 | // subsequent buttons will all be shifted which is rather confusing | |
476 | // (and like this you'd see immediately which bitmap was bad) | |
477 | x += m_defaultWidth; | |
478 | nButtons++; | |
1c383dba | 479 | } |
8a0681f9 VZ |
480 | |
481 | node = node->GetNext(); | |
051205e6 | 482 | } |
2bda0e17 | 483 | |
f6bcfd97 BP |
484 | #if USE_BITMAP_MASKS |
485 | dcAllButtons.SelectObject(wxNullBitmap); | |
486 | ||
487 | // don't delete this HBITMAP! | |
488 | bitmap.SetHBITMAP(0); | |
489 | #else // !USE_BITMAP_MASKS | |
1c383dba VZ |
490 | ::SelectObject(memoryDC, oldBitmap); |
491 | ::DeleteDC(memoryDC); | |
492 | ::DeleteDC(memoryDC2); | |
f6bcfd97 | 493 | #endif // USE_BITMAP_MASKS/!USE_BITMAP_MASKS |
2bda0e17 | 494 | |
1c383dba | 495 | // Map to system colours |
566fb299 VZ |
496 | hBitmap = (HBITMAP)MapBitmap((WXHBITMAP) hBitmap, |
497 | totalBitmapWidth, totalBitmapHeight); | |
1c383dba | 498 | |
9f83044f VZ |
499 | int bitmapId = 0; |
500 | ||
501 | bool addBitmap = TRUE; | |
502 | ||
1c383dba | 503 | if ( oldToolBarBitmap ) |
2bda0e17 | 504 | { |
9f83044f VZ |
505 | #ifdef TB_REPLACEBITMAP |
506 | if ( wxTheApp->GetComCtl32Version() >= 400 ) | |
1c383dba | 507 | { |
9f83044f VZ |
508 | TBREPLACEBITMAP replaceBitmap; |
509 | replaceBitmap.hInstOld = NULL; | |
510 | replaceBitmap.hInstNew = NULL; | |
511 | replaceBitmap.nIDOld = (UINT) oldToolBarBitmap; | |
512 | replaceBitmap.nIDNew = (UINT) hBitmap; | |
513 | replaceBitmap.nButtons = nButtons; | |
514 | if ( !::SendMessage(GetHwnd(), TB_REPLACEBITMAP, | |
515 | 0, (LPARAM) &replaceBitmap) ) | |
516 | { | |
517 | wxFAIL_MSG(wxT("Could not replace the old bitmap")); | |
518 | } | |
519 | ||
520 | ::DeleteObject(oldToolBarBitmap); | |
521 | ||
522 | // already done | |
523 | addBitmap = FALSE; | |
1c383dba | 524 | } |
9f83044f VZ |
525 | else |
526 | #endif // TB_REPLACEBITMAP | |
527 | { | |
528 | // we can't replace the old bitmap, so we will add another one | |
529 | // (awfully inefficient, but what else to do?) and shift the bitmap | |
530 | // indices accordingly | |
531 | addBitmap = TRUE; | |
1c383dba | 532 | |
9f83044f VZ |
533 | bitmapId = m_nButtons; |
534 | } | |
1c383dba VZ |
535 | |
536 | // Now delete all the buttons | |
8a0681f9 | 537 | for ( size_t pos = 0; pos < m_nButtons; pos++ ) |
1c383dba | 538 | { |
8a0681f9 VZ |
539 | if ( !::SendMessage(GetHwnd(), TB_DELETEBUTTON, 0, 0) ) |
540 | { | |
56bd6aac | 541 | wxLogDebug(wxT("TB_DELETEBUTTON failed")); |
8a0681f9 | 542 | } |
1c383dba | 543 | } |
9f83044f | 544 | |
2bda0e17 | 545 | } |
9f83044f VZ |
546 | |
547 | if ( addBitmap ) // no old bitmap or we can't replace it | |
051205e6 | 548 | { |
1c383dba VZ |
549 | TBADDBITMAP addBitmap; |
550 | addBitmap.hInst = 0; | |
8a0681f9 | 551 | addBitmap.nID = (UINT) hBitmap; |
1c383dba | 552 | if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP, |
8a0681f9 | 553 | (WPARAM) nButtons, (LPARAM)&addBitmap) == -1 ) |
1c383dba VZ |
554 | { |
555 | wxFAIL_MSG(wxT("Could not add bitmap to toolbar")); | |
556 | } | |
051205e6 | 557 | } |
2bda0e17 | 558 | |
8a0681f9 VZ |
559 | // Next add the buttons and separators |
560 | // ----------------------------------- | |
561 | ||
1c383dba | 562 | TBBUTTON *buttons = new TBBUTTON[nTools]; |
2bda0e17 | 563 | |
8a0681f9 | 564 | // this array will hold the indices of all controls in the toolbar |
1c383dba VZ |
565 | wxArrayInt controlIds; |
566 | ||
567 | int i = 0; | |
8a0681f9 | 568 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) |
2bda0e17 | 569 | { |
8a0681f9 VZ |
570 | wxToolBarToolBase *tool = node->GetData(); |
571 | ||
572 | // don't add separators to the vertical toolbar - looks ugly | |
573 | if ( isVertical && tool->IsSeparator() ) | |
574 | continue; | |
575 | ||
1c383dba VZ |
576 | TBBUTTON& button = buttons[i]; |
577 | ||
578 | wxZeroMemory(button); | |
579 | ||
8a0681f9 | 580 | switch ( tool->GetStyle() ) |
1c383dba VZ |
581 | { |
582 | case wxTOOL_STYLE_CONTROL: | |
8a0681f9 | 583 | button.idCommand = tool->GetId(); |
1c383dba VZ |
584 | // fall through: create just a separator too |
585 | ||
586 | case wxTOOL_STYLE_SEPARATOR: | |
587 | button.fsState = TBSTATE_ENABLED; | |
588 | button.fsStyle = TBSTYLE_SEP; | |
589 | break; | |
590 | ||
591 | case wxTOOL_STYLE_BUTTON: | |
592 | button.iBitmap = bitmapId; | |
8a0681f9 | 593 | button.idCommand = tool->GetId(); |
1c383dba | 594 | |
8a0681f9 | 595 | if ( tool->IsEnabled() ) |
1c383dba | 596 | button.fsState |= TBSTATE_ENABLED; |
8a0681f9 | 597 | if ( tool->IsToggled() ) |
1c383dba | 598 | button.fsState |= TBSTATE_CHECKED; |
8a0681f9 VZ |
599 | |
600 | button.fsStyle = tool->CanBeToggled() ? TBSTYLE_CHECK | |
601 | : TBSTYLE_BUTTON; | |
1c383dba VZ |
602 | |
603 | bitmapId++; | |
604 | break; | |
605 | } | |
2bda0e17 | 606 | |
1c383dba | 607 | i++; |
2bda0e17 | 608 | } |
1c383dba VZ |
609 | |
610 | if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS, | |
611 | (WPARAM)i, (LPARAM)buttons) ) | |
2bda0e17 | 612 | { |
f6bcfd97 | 613 | wxLogLastError(wxT("TB_ADDBUTTONS")); |
2bda0e17 | 614 | } |
89b892a2 | 615 | |
1c383dba | 616 | delete [] buttons; |
2bda0e17 | 617 | |
8a0681f9 VZ |
618 | // Deal with the controls finally |
619 | // ------------------------------ | |
620 | ||
1c383dba | 621 | // adjust the controls size to fit nicely in the toolbar |
34e5028f | 622 | int y = 0; |
8a0681f9 VZ |
623 | size_t index = 0; |
624 | for ( node = m_tools.GetFirst(); node; node = node->GetNext(), index++ ) | |
1c383dba | 625 | { |
8a0681f9 | 626 | wxToolBarToolBase *tool = node->GetData(); |
1c383dba | 627 | |
34e5028f VZ |
628 | // we calculate the running y coord for vertical toolbars so we need to |
629 | // get the items size for all items but for the horizontal ones we | |
630 | // don't need to deal with the non controls | |
631 | bool isControl = tool->IsControl(); | |
632 | if ( !isControl && !isVertical ) | |
633 | continue; | |
bdc72a22 | 634 | |
8a0681f9 VZ |
635 | // note that we use TB_GETITEMRECT and not TB_GETRECT because the |
636 | // latter only appeared in v4.70 of comctl32.dll | |
637 | RECT r; | |
638 | if ( !SendMessage(GetHwnd(), TB_GETITEMRECT, | |
639 | index, (LPARAM)(LPRECT)&r) ) | |
640 | { | |
f6bcfd97 | 641 | wxLogLastError(wxT("TB_GETITEMRECT")); |
8a0681f9 VZ |
642 | } |
643 | ||
34e5028f VZ |
644 | if ( !isControl ) |
645 | { | |
646 | // can only be control if isVertical | |
647 | y += r.bottom - r.top; | |
648 | ||
649 | continue; | |
650 | } | |
651 | ||
652 | wxControl *control = tool->GetControl(); | |
653 | ||
654 | wxSize size = control->GetSize(); | |
655 | ||
656 | // the position of the leftmost controls corner | |
657 | int left = -1; | |
658 | ||
bdc72a22 | 659 | // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+ |
34e5028f VZ |
660 | #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 ) |
661 | // available in headers, now check whether it is available now | |
662 | // (during run-time) | |
663 | if ( wxTheApp->GetComCtl32Version() >= 471 ) | |
664 | { | |
665 | // set the (underlying) separators width to be that of the | |
666 | // control | |
667 | TBBUTTONINFO tbbi; | |
668 | tbbi.cbSize = sizeof(tbbi); | |
669 | tbbi.dwMask = TBIF_SIZE; | |
670 | tbbi.cx = size.x; | |
671 | if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO, | |
672 | tool->GetId(), (LPARAM)&tbbi) ) | |
bdc72a22 | 673 | { |
34e5028f VZ |
674 | // the id is probably invalid? |
675 | wxLogLastError(wxT("TB_SETBUTTONINFO")); | |
bdc72a22 | 676 | } |
34e5028f VZ |
677 | } |
678 | else | |
679 | #endif // comctl32.dll 4.71 | |
680 | // TB_SETBUTTONINFO unavailable | |
681 | { | |
682 | // try adding several separators to fit the controls width | |
683 | int widthSep = r.right - r.left; | |
684 | left = r.left; | |
685 | ||
686 | TBBUTTON tbb; | |
687 | wxZeroMemory(tbb); | |
688 | tbb.idCommand = 0; | |
689 | tbb.fsState = TBSTATE_ENABLED; | |
690 | tbb.fsStyle = TBSTYLE_SEP; | |
691 | ||
692 | size_t nSeparators = size.x / widthSep; | |
693 | for ( size_t nSep = 0; nSep < nSeparators; nSep++ ) | |
bdc72a22 | 694 | { |
34e5028f VZ |
695 | if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON, |
696 | index, (LPARAM)&tbb) ) | |
bdc72a22 | 697 | { |
34e5028f | 698 | wxLogLastError(wxT("TB_INSERTBUTTON")); |
bdc72a22 VZ |
699 | } |
700 | ||
34e5028f | 701 | index++; |
bdc72a22 | 702 | } |
1c383dba | 703 | |
34e5028f VZ |
704 | // remember the number of separators we used - we'd have to |
705 | // delete all of them later | |
706 | ((wxToolBarTool *)tool)->SetSeparatorsCount(nSeparators); | |
707 | ||
708 | // adjust the controls width to exactly cover the separators | |
709 | control->SetSize((nSeparators + 1)*widthSep, -1); | |
710 | } | |
711 | ||
712 | // position the control itself correctly vertically | |
1c383dba VZ |
713 | int height = r.bottom - r.top; |
714 | int diff = height - size.y; | |
715 | if ( diff < 0 ) | |
716 | { | |
717 | // the control is too high, resize to fit | |
718 | control->SetSize(-1, height - 2); | |
719 | ||
720 | diff = 2; | |
721 | } | |
2bda0e17 | 722 | |
34e5028f VZ |
723 | int top; |
724 | if ( isVertical ) | |
725 | { | |
726 | left = 0; | |
727 | top = y; | |
728 | ||
729 | y += height + 2*GetMargins().y; | |
730 | } | |
731 | else // horizontal toolbar | |
732 | { | |
733 | if ( left == -1 ) | |
734 | left = r.left; | |
735 | ||
736 | top = r.top; | |
737 | } | |
738 | ||
739 | control->Move(left, top + (diff + 1) / 2); | |
1c383dba | 740 | } |
89b892a2 | 741 | |
8a0681f9 VZ |
742 | // the max index is the "real" number of buttons - i.e. counting even the |
743 | // separators which we added just for aligning the controls | |
744 | m_nButtons = index; | |
89b892a2 | 745 | |
8a0681f9 VZ |
746 | if ( !isVertical ) |
747 | { | |
748 | if ( m_maxRows == 0 ) | |
749 | { | |
750 | // if not set yet, only one row | |
751 | SetRows(1); | |
752 | } | |
753 | } | |
754 | else if ( m_nButtons > 0 ) // vertical non empty toolbar | |
755 | { | |
756 | if ( m_maxRows == 0 ) | |
757 | { | |
758 | // if not set yet, have one column | |
759 | SetRows(m_nButtons); | |
760 | } | |
761 | } | |
2bda0e17 | 762 | |
1c383dba | 763 | return TRUE; |
2bda0e17 KB |
764 | } |
765 | ||
1c383dba VZ |
766 | // ---------------------------------------------------------------------------- |
767 | // message handlers | |
768 | // ---------------------------------------------------------------------------- | |
769 | ||
33ac7e6f | 770 | bool wxToolBar::MSWCommand(WXUINT WXUNUSED(cmd), WXWORD id) |
2bda0e17 | 771 | { |
8a0681f9 VZ |
772 | wxToolBarToolBase *tool = FindById((int)id); |
773 | if ( !tool ) | |
1c383dba VZ |
774 | return FALSE; |
775 | ||
8a0681f9 | 776 | if ( tool->CanBeToggled() ) |
1c383dba VZ |
777 | { |
778 | LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0); | |
82dd98a7 | 779 | tool->Toggle((state & TBSTATE_CHECKED) != 0); |
1c383dba VZ |
780 | } |
781 | ||
8a0681f9 VZ |
782 | bool toggled = tool->IsToggled(); |
783 | ||
784 | // OnLeftClick() can veto the button state change - for buttons which may | |
785 | // be toggled only, of couse | |
786 | if ( !OnLeftClick((int)id, toggled) && tool->CanBeToggled() ) | |
1c383dba | 787 | { |
8a0681f9 VZ |
788 | // revert back |
789 | toggled = !toggled; | |
790 | tool->SetToggle(toggled); | |
791 | ||
792 | ::SendMessage(GetHwnd(), TB_CHECKBUTTON, id, MAKELONG(toggled, 0)); | |
1c383dba VZ |
793 | } |
794 | ||
795 | return TRUE; | |
2bda0e17 KB |
796 | } |
797 | ||
8a0681f9 | 798 | bool wxToolBar::MSWOnNotify(int WXUNUSED(idCtrl), |
fd3f686c | 799 | WXLPARAM lParam, |
33ac7e6f | 800 | WXLPARAM *WXUNUSED(result)) |
2bda0e17 | 801 | { |
89b892a2 | 802 | // First check if this applies to us |
2bda0e17 | 803 | NMHDR *hdr = (NMHDR *)lParam; |
2bda0e17 | 804 | |
1c383dba VZ |
805 | // the tooltips control created by the toolbar is sometimes Unicode, even |
806 | // in an ANSI application - this seems to be a bug in comctl32.dll v5 | |
a17e237f VZ |
807 | int code = (int)hdr->code; |
808 | if ( (code != TTN_NEEDTEXTA) && (code != TTN_NEEDTEXTW) ) | |
89b892a2 | 809 | return FALSE; |
2bda0e17 | 810 | |
89b892a2 VZ |
811 | HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0); |
812 | if ( toolTipWnd != hdr->hwndFrom ) | |
813 | return FALSE; | |
2bda0e17 | 814 | |
89b892a2 VZ |
815 | LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam; |
816 | int id = (int)ttText->hdr.idFrom; | |
89b892a2 | 817 | |
8a0681f9 VZ |
818 | wxToolBarToolBase *tool = FindById(id); |
819 | if ( !tool ) | |
820 | return FALSE; | |
2bda0e17 | 821 | |
8a0681f9 | 822 | const wxString& help = tool->GetShortHelp(); |
8df13671 VZ |
823 | |
824 | if ( !help.IsEmpty() ) | |
89b892a2 | 825 | { |
a17e237f | 826 | if ( code == TTN_NEEDTEXTA ) |
89b892a2 | 827 | { |
837e5743 | 828 | ttText->lpszText = (wxChar *)help.c_str(); |
89b892a2 | 829 | } |
89b892a2 VZ |
830 | else |
831 | { | |
f6bcfd97 BP |
832 | #if wxUSE_UNICODE |
833 | ttText->lpszText = (wxChar *)help.c_str(); | |
834 | #else | |
0d910be7 | 835 | // VZ: I don't know why it happens, but the versions of |
56bd6aac | 836 | // comctl32.dll starting from 4.70 sometimes send TTN_NEEDTEXTW |
0d910be7 VZ |
837 | // even to ANSI programs (normally, this message is supposed |
838 | // to be sent to Unicode programs only) - hence we need to | |
839 | // handle it as well, otherwise no tooltips will be shown in | |
840 | // this case | |
8df13671 VZ |
841 | |
842 | size_t lenAnsi = help.Len(); | |
a9582178 | 843 | #if defined( __MWERKS__ ) || defined( __CYGWIN__ ) |
8df13671 | 844 | // MetroWerks doesn't like calling mbstowcs with NULL argument |
a9582178 | 845 | // neither Cygwin does |
8df13671 | 846 | size_t lenUnicode = 2*lenAnsi; |
b2cce0c4 | 847 | #else |
8df13671 | 848 | size_t lenUnicode = mbstowcs(NULL, help, lenAnsi); |
b2cce0c4 | 849 | #endif |
8df13671 VZ |
850 | |
851 | // using the pointer of right type avoids us doing all sorts of | |
852 | // pointer arithmetics ourselves | |
853 | wchar_t *dst = (wchar_t *)ttText->szText, | |
854 | *pwz = new wchar_t[lenUnicode + 1]; | |
855 | mbstowcs(pwz, help, lenAnsi + 1); | |
856 | memcpy(dst, pwz, lenUnicode*sizeof(wchar_t)); | |
857 | ||
858 | // put the terminating _wide_ NUL | |
859 | dst[lenUnicode] = 0; | |
89b892a2 VZ |
860 | |
861 | delete [] pwz; | |
f6bcfd97 | 862 | #endif |
89b892a2 | 863 | } |
2bda0e17 | 864 | } |
89b892a2 | 865 | |
89b892a2 | 866 | return TRUE; |
2bda0e17 KB |
867 | } |
868 | ||
1c383dba | 869 | // ---------------------------------------------------------------------------- |
8a0681f9 | 870 | // toolbar geometry |
1c383dba VZ |
871 | // ---------------------------------------------------------------------------- |
872 | ||
8a0681f9 | 873 | void wxToolBar::SetToolBitmapSize(const wxSize& size) |
2bda0e17 | 874 | { |
1c383dba VZ |
875 | wxToolBarBase::SetToolBitmapSize(size); |
876 | ||
877 | ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y)); | |
2bda0e17 KB |
878 | } |
879 | ||
8a0681f9 | 880 | void wxToolBar::SetRows(int nRows) |
2bda0e17 | 881 | { |
8a0681f9 VZ |
882 | if ( nRows == m_maxRows ) |
883 | { | |
884 | // avoid resizing the frame uselessly | |
885 | return; | |
886 | } | |
887 | ||
888 | // TRUE in wParam means to create at least as many rows, FALSE - | |
889 | // at most as many | |
1c383dba VZ |
890 | RECT rect; |
891 | ::SendMessage(GetHwnd(), TB_SETROWS, | |
8a0681f9 VZ |
892 | MAKEWPARAM(nRows, !(GetWindowStyle() & wxTB_VERTICAL)), |
893 | (LPARAM) &rect); | |
1c383dba VZ |
894 | |
895 | m_maxRows = nRows; | |
8a0681f9 VZ |
896 | |
897 | UpdateSize(); | |
898 | } | |
899 | ||
900 | // The button size is bigger than the bitmap size | |
901 | wxSize wxToolBar::GetToolSize() const | |
902 | { | |
903 | // TB_GETBUTTONSIZE is supported from version 4.70 | |
5438a566 VZ |
904 | #if defined(_WIN32_IE) && (_WIN32_IE >= 0x300 ) \ |
905 | && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) | |
8a0681f9 VZ |
906 | if ( wxTheApp->GetComCtl32Version() >= 470 ) |
907 | { | |
908 | DWORD dw = ::SendMessage(GetHwnd(), TB_GETBUTTONSIZE, 0, 0); | |
909 | ||
910 | return wxSize(LOWORD(dw), HIWORD(dw)); | |
911 | } | |
912 | else | |
913 | #endif // comctl32.dll 4.70+ | |
914 | { | |
915 | // defaults | |
916 | return wxSize(m_defaultWidth + 8, m_defaultHeight + 7); | |
917 | } | |
2bda0e17 KB |
918 | } |
919 | ||
82c9f85c VZ |
920 | static |
921 | wxToolBarToolBase *GetItemSkippingDummySpacers(const wxToolBarToolsList& tools, | |
922 | size_t index ) | |
a8945eef MB |
923 | { |
924 | wxToolBarToolsList::Node* current = tools.GetFirst(); | |
925 | ||
82c9f85c | 926 | for ( ; current != 0; current = current->GetNext() ) |
a8945eef | 927 | { |
82c9f85c | 928 | if ( index == 0 ) |
a8945eef | 929 | return current->GetData(); |
82c9f85c VZ |
930 | |
931 | wxToolBarTool *tool = (wxToolBarTool *)current->GetData(); | |
932 | size_t separators = tool->GetSeparatorsCount(); | |
933 | ||
934 | // if it is a normal button, sepcount == 0, so skip 1 item (the button) | |
935 | // otherwise, skip as many items as the separator count, plus the | |
936 | // control itself | |
937 | index -= separators ? separators + 1 : 1; | |
a8945eef MB |
938 | } |
939 | ||
940 | return 0; | |
941 | } | |
942 | ||
8a0681f9 | 943 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const |
2bda0e17 | 944 | { |
8a0681f9 VZ |
945 | POINT pt; |
946 | pt.x = x; | |
947 | pt.y = y; | |
948 | int index = (int)::SendMessage(GetHwnd(), TB_HITTEST, 0, (LPARAM)&pt); | |
37d0bdff MB |
949 | // MBN: when the point ( x, y ) is close to the toolbar border |
950 | // TB_HITTEST returns m_nButtons ( not -1 ) | |
951 | if ( index < 0 || (size_t)index >= m_nButtons ) | |
1c383dba | 952 | { |
8a0681f9 VZ |
953 | // it's a separator or there is no tool at all there |
954 | return (wxToolBarToolBase *)NULL; | |
1c383dba VZ |
955 | } |
956 | ||
82c9f85c | 957 | // if comctl32 version < 4.71 wxToolBar95 adds dummy spacers |
a8945eef MB |
958 | #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 ) |
959 | if ( wxTheApp->GetComCtl32Version() >= 471 ) | |
960 | { | |
961 | return m_tools.Item((size_t)index)->GetData(); | |
962 | } | |
963 | else | |
82c9f85c | 964 | #endif |
a8945eef MB |
965 | { |
966 | return GetItemSkippingDummySpacers( m_tools, (size_t) index ); | |
967 | } | |
2bda0e17 KB |
968 | } |
969 | ||
8a0681f9 | 970 | void wxToolBar::UpdateSize() |
2bda0e17 | 971 | { |
0d7ea902 VZ |
972 | // the toolbar size changed |
973 | SendMessage(GetHwnd(), TB_AUTOSIZE, 0, 0); | |
974 | ||
975 | // we must also refresh the frame after the toolbar size (possibly) changed | |
8a0681f9 VZ |
976 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); |
977 | if ( frame ) | |
978 | { | |
f6bcfd97 | 979 | frame->SendSizeEvent(); |
8a0681f9 | 980 | } |
2bda0e17 KB |
981 | } |
982 | ||
1c383dba VZ |
983 | // ---------------------------------------------------------------------------- |
984 | // tool state | |
985 | // ---------------------------------------------------------------------------- | |
986 | ||
8a0681f9 | 987 | void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable) |
2bda0e17 | 988 | { |
8a0681f9 VZ |
989 | ::SendMessage(GetHwnd(), TB_ENABLEBUTTON, |
990 | (WPARAM)tool->GetId(), (LPARAM)MAKELONG(enable, 0)); | |
2bda0e17 KB |
991 | } |
992 | ||
8a0681f9 | 993 | void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle) |
2bda0e17 | 994 | { |
8a0681f9 VZ |
995 | ::SendMessage(GetHwnd(), TB_CHECKBUTTON, |
996 | (WPARAM)tool->GetId(), (LPARAM)MAKELONG(toggle, 0)); | |
2bda0e17 KB |
997 | } |
998 | ||
33ac7e6f | 999 | void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)) |
088a95f5 | 1000 | { |
8a0681f9 VZ |
1001 | // VZ: AFAIK, the button has to be created either with TBSTYLE_CHECK or |
1002 | // without, so we really need to delete the button and recreate it here | |
1003 | wxFAIL_MSG( _T("not implemented") ); | |
2bda0e17 KB |
1004 | } |
1005 | ||
1c383dba VZ |
1006 | // ---------------------------------------------------------------------------- |
1007 | // event handlers | |
1008 | // ---------------------------------------------------------------------------- | |
2bda0e17 KB |
1009 | |
1010 | // Responds to colour changes, and passes event on to children. | |
8a0681f9 | 1011 | void wxToolBar::OnSysColourChanged(wxSysColourChangedEvent& event) |
2bda0e17 | 1012 | { |
84c5b38d | 1013 | wxRGBToColour(m_backgroundColour, ::GetSysColor(COLOR_BTNFACE)); |
2bda0e17 KB |
1014 | |
1015 | // Remap the buttons | |
8a0681f9 | 1016 | Realize(); |
2bda0e17 | 1017 | |
56bd6aac VZ |
1018 | // Relayout the toolbar |
1019 | int nrows = m_maxRows; | |
1020 | m_maxRows = 0; // otherwise SetRows() wouldn't do anything | |
1021 | SetRows(nrows); | |
1022 | ||
2bda0e17 KB |
1023 | Refresh(); |
1024 | ||
56bd6aac VZ |
1025 | // let the event propagate further |
1026 | event.Skip(); | |
2bda0e17 KB |
1027 | } |
1028 | ||
8a0681f9 | 1029 | void wxToolBar::OnMouseEvent(wxMouseEvent& event) |
e6460682 JS |
1030 | { |
1031 | if (event.RightDown()) | |
1032 | { | |
1033 | // For now, we don't have an id. Later we could | |
1034 | // try finding the tool. | |
1035 | OnRightClick((int)-1, event.GetX(), event.GetY()); | |
1036 | } | |
1037 | else | |
1038 | { | |
42e69d6b | 1039 | event.Skip(); |
e6460682 JS |
1040 | } |
1041 | } | |
1042 | ||
8a0681f9 | 1043 | long wxToolBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
bdc72a22 | 1044 | { |
8a0681f9 | 1045 | if ( nMsg == WM_SIZE ) |
bdc72a22 | 1046 | { |
8a0681f9 VZ |
1047 | // calculate our minor dimenstion ourselves - we're confusing the |
1048 | // standard logic (TB_AUTOSIZE) with our horizontal toolbars and other | |
1049 | // hacks | |
1050 | RECT r; | |
1051 | if ( ::SendMessage(GetHwnd(), TB_GETITEMRECT, 0, (LPARAM)&r) ) | |
1052 | { | |
1053 | int w, h; | |
1054 | ||
1055 | if ( GetWindowStyle() & wxTB_VERTICAL ) | |
1056 | { | |
1057 | w = r.right - r.left; | |
1058 | if ( m_maxRows ) | |
1059 | { | |
1060 | w *= (m_nButtons + m_maxRows - 1)/m_maxRows; | |
1061 | } | |
1062 | h = HIWORD(lParam); | |
1063 | } | |
1064 | else | |
1065 | { | |
1066 | w = LOWORD(lParam); | |
1067 | h = r.bottom - r.top; | |
1068 | if ( m_maxRows ) | |
1069 | { | |
1070 | h += 6; // FIXME: this is the separator line height... | |
1071 | h *= m_maxRows; | |
1072 | } | |
1073 | } | |
1074 | ||
1075 | if ( MAKELPARAM(w, h) != lParam ) | |
1076 | { | |
1077 | // size really changed | |
1078 | SetSize(w, h); | |
1079 | } | |
1080 | ||
1081 | // message processed | |
1082 | return 0; | |
1083 | } | |
bdc72a22 | 1084 | } |
a8945eef MB |
1085 | else if ( nMsg == WM_MOUSEMOVE ) |
1086 | { | |
1087 | wxCoord x = GET_X_LPARAM(lParam), y = GET_Y_LPARAM(lParam); | |
1088 | wxToolBarToolBase* tool = FindToolForPosition( x, y ); | |
1089 | ||
1090 | // cursor left current tool | |
1091 | if( tool != m_pInTool && !tool ) | |
1092 | { | |
1093 | m_pInTool = 0; | |
1094 | OnMouseEnter( -1 ); | |
1095 | } | |
1096 | ||
1097 | // cursor entered a tool | |
1098 | if( tool != m_pInTool && tool ) | |
1099 | { | |
1100 | m_pInTool = tool; | |
1101 | OnMouseEnter( tool->GetId() ); | |
1102 | } | |
1103 | ||
1104 | // we don't handle mouse moves, so fall through | |
1105 | // to wxControl::MSWWindowProc | |
1106 | } | |
bdc72a22 | 1107 | |
8a0681f9 | 1108 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); |
bdc72a22 VZ |
1109 | } |
1110 | ||
1c383dba VZ |
1111 | // ---------------------------------------------------------------------------- |
1112 | // private functions | |
1113 | // ---------------------------------------------------------------------------- | |
1114 | ||
566fb299 | 1115 | WXHBITMAP wxToolBar::MapBitmap(WXHBITMAP bitmap, int width, int height) |
2bda0e17 | 1116 | { |
566fb299 | 1117 | MemoryHDC hdcMem; |
56bd6aac | 1118 | |
566fb299 | 1119 | if ( !hdcMem ) |
5e68f8f3 | 1120 | { |
566fb299 VZ |
1121 | wxLogLastError(_T("CreateCompatibleDC")); |
1122 | ||
1123 | return bitmap; | |
5e68f8f3 | 1124 | } |
56bd6aac | 1125 | |
566fb299 | 1126 | SelectInHDC bmpInHDC(hdcMem, (HBITMAP)bitmap); |
56bd6aac | 1127 | |
566fb299 | 1128 | if ( !bmpInHDC ) |
5e68f8f3 | 1129 | { |
566fb299 VZ |
1130 | wxLogLastError(_T("SelectObject")); |
1131 | ||
1132 | return bitmap; | |
1133 | } | |
56bd6aac | 1134 | |
90c1530a VZ |
1135 | wxCOLORMAP *cmap = wxGetStdColourMap(); |
1136 | ||
684c68fd VZ |
1137 | for ( int i = 0; i < width; i++ ) |
1138 | { | |
1139 | for ( int j = 0; j < height; j++ ) | |
1140 | { | |
1141 | COLORREF pixel = ::GetPixel(hdcMem, i, j); | |
1142 | ||
90c1530a | 1143 | for ( size_t k = 0; k < wxSTD_COL_MAX; k++ ) |
684c68fd | 1144 | { |
90c1530a | 1145 | COLORREF col = cmap[k].from; |
684c68fd VZ |
1146 | if ( abs(GetRValue(pixel) - GetRValue(col)) < 10 && |
1147 | abs(GetGValue(pixel) - GetGValue(col)) < 10 && | |
1148 | abs(GetBValue(pixel) - GetBValue(col)) < 10 ) | |
1149 | { | |
90c1530a | 1150 | ::SetPixel(hdcMem, i, j, cmap[k].to); |
684c68fd VZ |
1151 | break; |
1152 | } | |
1153 | } | |
1154 | } | |
1155 | } | |
3a3898f8 VZ |
1156 | |
1157 | return bitmap; | |
1158 | ||
566fb299 VZ |
1159 | // VZ: I leave here my attempts to map the bitmap to the system colours |
1160 | // faster by using BitBlt() even though it's broken currently - but | |
1161 | // maybe someone else can finish it? It should be faster than iterating | |
1162 | // over all pixels... | |
3a3898f8 | 1163 | #if 0 |
566fb299 VZ |
1164 | MemoryHDC hdcMask, hdcDst; |
1165 | if ( !hdcMask || !hdcDst ) | |
1166 | { | |
1167 | wxLogLastError(_T("CreateCompatibleDC")); | |
56bd6aac | 1168 | |
566fb299 | 1169 | return bitmap; |
2bda0e17 | 1170 | } |
2bda0e17 | 1171 | |
566fb299 VZ |
1172 | // create the target bitmap |
1173 | HBITMAP hbmpDst = ::CreateCompatibleBitmap(hdcDst, width, height); | |
1174 | if ( !hbmpDst ) | |
1175 | { | |
1176 | wxLogLastError(_T("CreateCompatibleBitmap")); | |
1177 | ||
1178 | return bitmap; | |
1179 | } | |
1180 | ||
1181 | // create the monochrome mask bitmap | |
1182 | HBITMAP hbmpMask = ::CreateBitmap(width, height, 1, 1, 0); | |
1183 | if ( !hbmpMask ) | |
1184 | { | |
1185 | wxLogLastError(_T("CreateBitmap(mono)")); | |
1186 | ||
1187 | ::DeleteObject(hbmpDst); | |
1188 | ||
1189 | return bitmap; | |
1190 | } | |
1191 | ||
1192 | SelectInHDC bmpInDst(hdcDst, hbmpDst), | |
1193 | bmpInMask(hdcMask, hbmpMask); | |
1194 | ||
1195 | // for each colour: | |
1196 | for ( n = 0; n < NUM_OF_MAPPED_COLOURS; n++ ) | |
1197 | { | |
1198 | // create the mask for this colour | |
1199 | ::SetBkColor(hdcMem, ColorMap[n].from); | |
1200 | ::BitBlt(hdcMask, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY); | |
1201 | ||
1202 | // replace this colour with the target one in the dst bitmap | |
1203 | HBRUSH hbr = ::CreateSolidBrush(ColorMap[n].to); | |
1204 | HGDIOBJ hbrOld = ::SelectObject(hdcDst, hbr); | |
2bda0e17 | 1205 | |
566fb299 VZ |
1206 | ::MaskBlt(hdcDst, 0, 0, width, height, |
1207 | hdcMem, 0, 0, | |
1208 | hbmpMask, 0, 0, | |
1209 | MAKEROP4(PATCOPY, SRCCOPY)); | |
1210 | ||
1211 | (void)::SelectObject(hdcDst, hbrOld); | |
1212 | ::DeleteObject(hbr); | |
1213 | } | |
1214 | ||
1215 | ::DeleteObject((HBITMAP)bitmap); | |
1216 | ||
1217 | return (WXHBITMAP)hbmpDst; | |
3a3898f8 | 1218 | #endif // 0 |
566fb299 | 1219 | } |
2bda0e17 | 1220 | |
8a0681f9 | 1221 | #endif // wxUSE_TOOLBAR && Win95 |
33ac7e6f | 1222 |