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