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