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