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