]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tbar95.cpp
1. wxGetOsDescription() function added
[wxWidgets.git] / src / msw / tbar95.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/tbar95.cpp
3 // Purpose: wxToolBar95
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "tbar95.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/log.h"
33 #include "wx/intl.h"
34 #include "wx/dynarray.h"
35 #include "wx/settings.h"
36 #include "wx/bitmap.h"
37 #endif
38
39 #if wxUSE_BUTTONBAR && wxUSE_TOOLBAR && defined(__WIN95__)
40
41 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
42 #include "malloc.h"
43 #endif
44
45 #include "wx/msw/private.h"
46
47 #ifndef __TWIN32__
48
49 #ifdef __GNUWIN32_OLD__
50 #include "wx/msw/gnuwin32/extra.h"
51 #else
52 #include <commctrl.h>
53 #endif
54
55 #endif // __TWIN32__
56
57 #include "wx/msw/dib.h"
58 #include "wx/tbar95.h"
59 #include "wx/app.h" // for GetComCtl32Version
60
61 // ----------------------------------------------------------------------------
62 // constants
63 // ----------------------------------------------------------------------------
64
65 // these standard constants are not always defined in compilers headers
66
67 // Styles
68 #ifndef TBSTYLE_FLAT
69 #define TBSTYLE_LIST 0x1000
70 #define TBSTYLE_FLAT 0x0800
71 #define TBSTYLE_TRANSPARENT 0x8000
72 #endif
73 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
74
75 // Messages
76 #ifndef TB_GETSTYLE
77 #define TB_GETSTYLE (WM_USER + 57)
78 #define TB_SETSTYLE (WM_USER + 56)
79 #endif
80
81 // these values correspond to those used by comctl32.dll
82 #define DEFAULTBITMAPX 16
83 #define DEFAULTBITMAPY 15
84 #define DEFAULTBUTTONX 24
85 #define DEFAULTBUTTONY 24
86 #define DEFAULTBARHEIGHT 27
87
88 // ----------------------------------------------------------------------------
89 // function prototypes
90 // ----------------------------------------------------------------------------
91
92 static void wxMapBitmap(HBITMAP hBitmap, int width, int height);
93
94 // ----------------------------------------------------------------------------
95 // wxWin macros
96 // ----------------------------------------------------------------------------
97
98 #if !USE_SHARED_LIBRARY
99 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
100 #endif
101
102 BEGIN_EVENT_TABLE(wxToolBar95, wxToolBarBase)
103 EVT_MOUSE_EVENTS(wxToolBar95::OnMouseEvent)
104 EVT_SYS_COLOUR_CHANGED(wxToolBar95::OnSysColourChanged)
105 END_EVENT_TABLE()
106
107 // ============================================================================
108 // implementation
109 // ============================================================================
110
111 // ----------------------------------------------------------------------------
112 // wxToolBar95 construction
113 // ----------------------------------------------------------------------------
114
115 void wxToolBar95::Init()
116 {
117 m_maxWidth = -1;
118 m_maxHeight = -1;
119 m_hBitmap = 0;
120 m_defaultWidth = DEFAULTBITMAPX;
121 m_defaultHeight = DEFAULTBITMAPY;
122 }
123
124 bool wxToolBar95::Create(wxWindow *parent,
125 wxWindowID id,
126 const wxPoint& pos,
127 const wxSize& size,
128 long style,
129 const wxString& name)
130 {
131 wxASSERT_MSG( (style & wxTB_VERTICAL) == 0,
132 wxT("Sorry, wxToolBar95 under Windows 95 only "
133 "supports horizontal orientation.") );
134
135 // common initialisation
136 if ( !CreateControl(parent, id, pos, size, style, name) )
137 return FALSE;
138
139 // prepare flags
140 DWORD msflags = 0; // WS_VISIBLE | WS_CHILD always included
141 if (style & wxBORDER)
142 msflags |= WS_BORDER;
143 msflags |= TBSTYLE_TOOLTIPS;
144
145 if (style & wxTB_FLAT)
146 {
147 if (wxTheApp->GetComCtl32Version() > 400)
148 msflags |= TBSTYLE_FLAT;
149 }
150
151 // MSW-specific initialisation
152 if ( !wxControl::MSWCreateControl(TOOLBARCLASSNAME, msflags) )
153 return FALSE;
154
155 // toolbar-specific post initialisation
156 ::SendMessage(GetHwnd(), TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
157
158 // set up the colors and fonts
159 wxRGBToColour(m_backgroundColour, GetSysColor(COLOR_BTNFACE));
160 m_foregroundColour = *wxBLACK;
161
162 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
163
164 // position it
165 int x = pos.x;
166 int y = pos.y;
167 int width = size.x;
168 int height = size.y;
169
170 if (width <= 0)
171 width = 100;
172 if (height <= 0)
173 height = m_defaultHeight;
174 if (x < 0)
175 x = 0;
176 if (y < 0)
177 y = 0;
178
179 SetSize(x, y, width, height);
180
181 return TRUE;
182 }
183
184 wxToolBar95::~wxToolBar95()
185 {
186 UnsubclassWin();
187
188 if (m_hBitmap)
189 {
190 ::DeleteObject((HBITMAP) m_hBitmap);
191 }
192 }
193
194 // ----------------------------------------------------------------------------
195 // adding/removing buttons
196 // ----------------------------------------------------------------------------
197
198 void wxToolBar95::ClearTools()
199 {
200 // TODO: Don't know how to reset the toolbar bitmap, as yet.
201 // But adding tools and calling CreateTools should probably
202 // recreate a buttonbar OK.
203 wxToolBarBase::ClearTools();
204 }
205
206 bool wxToolBar95::DeleteTool(int id)
207 {
208 int index = GetIndexFromId(id);
209 wxASSERT_MSG( index != wxNOT_FOUND, _T("invalid toolbar button id") );
210
211 if ( !SendMessage(GetHwnd(), TB_DELETEBUTTON, index, 0) )
212 {
213 wxLogLastError("TB_DELETEBUTTON");
214
215 return FALSE;
216 }
217
218 wxNode *node = m_tools.Nth(index);
219 delete (wxToolBarTool *)node->Data();
220 m_tools.DeleteNode(node);
221
222 m_ids.RemoveAt(index);
223
224 return TRUE;
225 }
226
227 bool wxToolBar95::AddControl(wxControl *control)
228 {
229 wxCHECK_MSG( control, FALSE, _T("toolbar: can't insert NULL control") );
230
231 wxCHECK_MSG( control->GetParent() == this, FALSE,
232 _T("control must have toolbar as parent") );
233
234 wxToolBarTool *tool = new wxToolBarTool(control);
235
236 m_tools.Append(control->GetId(), tool);
237 m_ids.Add(control->GetId());
238
239 return TRUE;
240 }
241
242 wxToolBarTool *wxToolBar95::AddTool(int index,
243 const wxBitmap& bitmap,
244 const wxBitmap& pushedBitmap,
245 bool toggle,
246 long xPos, long yPos,
247 wxObject *clientData,
248 const wxString& helpString1,
249 const wxString& helpString2)
250 {
251 wxToolBarTool *tool = new wxToolBarTool(index, bitmap, wxNullBitmap,
252 toggle, xPos, yPos,
253 helpString1, helpString2);
254 tool->m_clientData = clientData;
255
256 if (xPos > -1)
257 tool->m_x = xPos;
258 else
259 tool->m_x = m_xMargin;
260
261 if (yPos > -1)
262 tool->m_y = yPos;
263 else
264 tool->m_y = m_yMargin;
265
266 tool->SetSize(GetToolSize().x, GetToolSize().y);
267
268 m_tools.Append((long)index, tool);
269 m_ids.Add(index);
270
271 return tool;
272 }
273
274 bool wxToolBar95::CreateTools()
275 {
276 size_t nTools = m_tools.GetCount();
277 if ( nTools == 0 )
278 return FALSE;
279
280 HBITMAP oldToolBarBitmap = (HBITMAP) m_hBitmap;
281
282 int totalBitmapWidth = (int)(m_defaultWidth * nTools);
283 int totalBitmapHeight = (int)m_defaultHeight;
284
285 // Create a bitmap for all the tool bitmaps
286 HDC dc = ::GetDC(NULL);
287 m_hBitmap = (WXHBITMAP) ::CreateCompatibleBitmap(dc,
288 totalBitmapWidth,
289 totalBitmapHeight);
290 ::ReleaseDC(NULL, dc);
291
292 // Now blit all the tools onto this bitmap
293 HDC memoryDC = ::CreateCompatibleDC(NULL);
294 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(memoryDC, (HBITMAP)m_hBitmap);
295
296 HDC memoryDC2 = ::CreateCompatibleDC(NULL);
297
298 // the button position
299 wxCoord x = 0;
300
301 // the number of buttons (not separators)
302 int noButtons = 0;
303
304 wxNode *node = m_tools.First();
305 while (node)
306 {
307 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
308 if ( tool->m_toolStyle == wxTOOL_STYLE_BUTTON && tool->m_bitmap1.Ok() )
309 {
310 HBITMAP hbmp = GetHbitmapOf(tool->m_bitmap1);
311 if ( hbmp )
312 {
313 HBITMAP oldBitmap2 = (HBITMAP)::SelectObject(memoryDC2, hbmp);
314 if ( !BitBlt(memoryDC, x, 0, m_defaultWidth, m_defaultHeight,
315 memoryDC2, 0, 0, SRCCOPY) )
316 {
317 wxLogLastError("BitBlt");
318 }
319
320 ::SelectObject(memoryDC2, oldBitmap2);
321
322 x += m_defaultWidth;
323 noButtons++;
324 }
325 }
326 node = node->Next();
327 }
328
329 ::SelectObject(memoryDC, oldBitmap);
330 ::DeleteDC(memoryDC);
331 ::DeleteDC(memoryDC2);
332
333 // Map to system colours
334 wxMapBitmap((HBITMAP) m_hBitmap, totalBitmapWidth, totalBitmapHeight);
335
336 if ( oldToolBarBitmap )
337 {
338 TBREPLACEBITMAP replaceBitmap;
339 replaceBitmap.hInstOld = NULL;
340 replaceBitmap.hInstNew = NULL;
341 replaceBitmap.nIDOld = (UINT) oldToolBarBitmap;
342 replaceBitmap.nIDNew = (UINT) (HBITMAP) m_hBitmap;
343 replaceBitmap.nButtons = noButtons;
344 if ( ::SendMessage(GetHwnd(), TB_REPLACEBITMAP,
345 0, (LPARAM) &replaceBitmap) == -1 )
346 {
347 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
348 }
349
350 ::DeleteObject((HBITMAP) oldToolBarBitmap);
351
352 // Now delete all the buttons
353 int i = 0;
354 while ( TRUE )
355 {
356 // TODO: What about separators???? They don't have an id!
357 if ( ! ::SendMessage( GetHwnd(), TB_DELETEBUTTON, i, 0 ) )
358 break;
359 }
360 }
361 else
362 {
363 TBADDBITMAP addBitmap;
364 addBitmap.hInst = 0;
365 addBitmap.nID = (UINT)m_hBitmap;
366 if ( ::SendMessage(GetHwnd(), TB_ADDBITMAP,
367 (WPARAM) noButtons, (LPARAM)&addBitmap) == -1 )
368 {
369 wxFAIL_MSG(wxT("Could not add bitmap to toolbar"));
370 }
371 }
372
373 // Now add the buttons.
374 TBBUTTON *buttons = new TBBUTTON[nTools];
375
376 // this array will holds the indices of all controls in the toolbar
377 wxArrayInt controlIds;
378
379 int i = 0;
380 int bitmapId = 0;
381
382 node = m_tools.First();
383 while (node)
384 {
385 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
386 TBBUTTON& button = buttons[i];
387
388 wxZeroMemory(button);
389
390 switch ( tool->m_toolStyle )
391 {
392 case wxTOOL_STYLE_CONTROL:
393 controlIds.Add(i);
394 button.idCommand = tool->m_index;
395 // fall through: create just a separator too
396
397 case wxTOOL_STYLE_SEPARATOR:
398 button.fsState = TBSTATE_ENABLED;
399 button.fsStyle = TBSTYLE_SEP;
400 break;
401
402 case wxTOOL_STYLE_BUTTON:
403 button.iBitmap = bitmapId;
404 button.idCommand = tool->m_index;
405
406 if (tool->m_enabled)
407 button.fsState |= TBSTATE_ENABLED;
408 if (tool->m_toggleState)
409 button.fsState |= TBSTATE_CHECKED;
410 button.fsStyle = tool->m_isToggle ? TBSTYLE_CHECK
411 : TBSTYLE_BUTTON;
412
413 bitmapId++;
414 break;
415 }
416
417 i++;
418 node = node->Next();
419 }
420
421 if ( !::SendMessage(GetHwnd(), TB_ADDBUTTONS,
422 (WPARAM)i, (LPARAM)buttons) )
423 {
424 wxLogLastError("TB_ADDBUTTONS");
425 }
426
427 delete [] buttons;
428
429 // adjust the controls size to fit nicely in the toolbar
430 size_t nControls = controlIds.GetCount();
431 for ( size_t nCtrl = 0; nCtrl < nControls; nCtrl++ )
432 {
433 wxToolBarTool *tool = (wxToolBarTool *)
434 m_tools.Nth(controlIds[nCtrl])->Data();
435 wxControl *control = tool->GetControl();
436
437 wxSize size = control->GetSize();
438
439 // the position of the leftmost controls corner
440 int left = -1;
441
442 // TB_SETBUTTONINFO message is only supported by comctl32.dll 4.71+
443 #if defined(_WIN32_IE) && (_WIN32_IE >= 0x400 )
444 // available in headers, now check whether it is available now
445 // (during run-time)
446 if ( wxTheApp->GetComCtl32Version() >= 471 )
447 {
448 // set the (underlying) separators width to be that of the
449 // control
450 TBBUTTONINFO tbbi;
451 tbbi.cbSize = sizeof(tbbi);
452 tbbi.dwMask = TBIF_SIZE;
453 tbbi.cx = size.x;
454 if ( !SendMessage(GetHwnd(), TB_SETBUTTONINFO,
455 tool->m_index, (LPARAM)&tbbi) )
456 {
457 // the index is probably invalid
458 wxLogLastError("TB_SETBUTTONINFO");
459 }
460
461 }
462 else
463 #endif // comctl32.dll 4.71
464 // TB_SETBUTTONINFO unavailable
465 {
466 int index = GetIndexFromId(tool->m_index);
467 wxASSERT_MSG( index != wxNOT_FOUND,
468 _T("control wasn't added to the tbar?") );
469
470 // try adding several separators to fit the controls width
471 RECT r;
472 if ( !SendMessage(GetHwnd(), TB_GETRECT,
473 tool->m_index, (LPARAM)(LPRECT)&r) )
474 {
475 wxLogLastError("TB_GETITEMRECT");
476 }
477
478 int widthSep = r.right - r.left;
479 left = r.left;
480
481 TBBUTTON tbb;
482 wxZeroMemory(tbb);
483 tbb.idCommand = 0;
484 tbb.fsState = TBSTATE_ENABLED;
485 tbb.fsStyle = TBSTYLE_SEP;
486
487 size_t nSeparators = size.x / widthSep;
488 for ( size_t nSep = 0; nSep < nSeparators; nSep++ )
489 {
490 m_ids.Insert(0, (size_t)index);
491
492 if ( !SendMessage(GetHwnd(), TB_INSERTBUTTON,
493 index, (LPARAM)&tbb) )
494 {
495 wxLogLastError("TB_INSERTBUTTON");
496 }
497 }
498
499 // adjust the controls width to exactly cover the separators
500 control->SetSize((nSeparators + 1)*widthSep, -1);
501 }
502
503 // and position the control itself correctly vertically
504 RECT r;
505 if ( !SendMessage(GetHwnd(), TB_GETRECT,
506 tool->m_index, (LPARAM)(LPRECT)&r) )
507 {
508 wxLogLastError("TB_GETRECT");
509 }
510
511 int height = r.bottom - r.top;
512 int diff = height - size.y;
513 if ( diff < 0 )
514 {
515 // the control is too high, resize to fit
516 control->SetSize(-1, height - 2);
517
518 diff = 2;
519 }
520
521 control->Move(left == -1 ? r.left : left, r.top + diff / 2);
522 }
523
524 (void)::SendMessage(GetHwnd(), TB_AUTOSIZE, (WPARAM)0, (LPARAM) 0);
525
526 SetRows(m_maxRows);
527
528 return TRUE;
529 }
530
531 // ----------------------------------------------------------------------------
532 // message handlers
533 // ----------------------------------------------------------------------------
534
535 bool wxToolBar95::MSWCommand(WXUINT cmd, WXWORD id)
536 {
537 wxNode *node = m_tools.Find((long)id);
538 if (!node)
539 return FALSE;
540
541 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
542 if (tool->m_isToggle)
543 {
544 LRESULT state = ::SendMessage(GetHwnd(), TB_GETSTATE, id, 0);
545 tool->m_toggleState = state & TBSTATE_CHECKED;
546 }
547
548 BOOL ret = OnLeftClick((int)id, tool->m_toggleState);
549 if ( ret == FALSE && tool->m_isToggle )
550 {
551 tool->m_toggleState = !tool->m_toggleState;
552 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
553 (WPARAM)id, (LPARAM)MAKELONG(tool->m_toggleState, 0));
554 }
555
556 return TRUE;
557 }
558
559 bool wxToolBar95::MSWOnNotify(int WXUNUSED(idCtrl),
560 WXLPARAM lParam,
561 WXLPARAM *result)
562 {
563 // First check if this applies to us
564 NMHDR *hdr = (NMHDR *)lParam;
565
566 // the tooltips control created by the toolbar is sometimes Unicode, even
567 // in an ANSI application - this seems to be a bug in comctl32.dll v5
568 int code = (int)hdr->code;
569 if ( (code != TTN_NEEDTEXTA) && (code != TTN_NEEDTEXTW) )
570 return FALSE;
571
572 HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0);
573 if ( toolTipWnd != hdr->hwndFrom )
574 return FALSE;
575
576 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
577 int id = (int)ttText->hdr.idFrom;
578 wxNode *node = m_tools.Find((long)id);
579 if (!node)
580 return FALSE;
581
582 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
583
584 const wxString& help = tool->m_shortHelpString;
585
586 if ( !help.IsEmpty() )
587 {
588 if ( code == TTN_NEEDTEXTA )
589 {
590 ttText->lpszText = (wxChar *)help.c_str();
591 }
592 #if (_WIN32_IE >= 0x0300)
593 else
594 {
595 // FIXME this is a temp hack only until I understand better what
596 // must be done in both ANSI and Unicode builds
597
598 size_t lenAnsi = help.Len();
599 #ifdef __MWERKS__
600 // MetroWerks doesn't like calling mbstowcs with NULL argument
601 size_t lenUnicode = 2*lenAnsi;
602 #else
603 size_t lenUnicode = mbstowcs(NULL, help, lenAnsi);
604 #endif
605
606 // using the pointer of right type avoids us doing all sorts of
607 // pointer arithmetics ourselves
608 wchar_t *dst = (wchar_t *)ttText->szText,
609 *pwz = new wchar_t[lenUnicode + 1];
610 mbstowcs(pwz, help, lenAnsi + 1);
611 memcpy(dst, pwz, lenUnicode*sizeof(wchar_t));
612
613 // put the terminating _wide_ NUL
614 dst[lenUnicode] = 0;
615
616 delete [] pwz;
617 }
618 #endif // _WIN32_IE >= 0x0300
619 }
620
621 // For backward compatibility...
622 OnMouseEnter(tool->m_index);
623
624 return TRUE;
625 }
626
627 // ----------------------------------------------------------------------------
628 // sizing stuff
629 // ----------------------------------------------------------------------------
630
631 void wxToolBar95::SetToolBitmapSize(const wxSize& size)
632 {
633 wxToolBarBase::SetToolBitmapSize(size);
634
635 ::SendMessage(GetHwnd(), TB_SETBITMAPSIZE, 0, MAKELONG(size.x, size.y));
636 }
637
638 void wxToolBar95::SetRows(int nRows)
639 {
640 // TRUE in wParam means to create at least as many rows
641 RECT rect;
642 ::SendMessage(GetHwnd(), TB_SETROWS,
643 MAKEWPARAM(nRows, TRUE), (LPARAM) &rect);
644
645 m_maxWidth = (rect.right - rect.left + 2);
646 m_maxHeight = (rect.bottom - rect.top + 2);
647
648 m_maxRows = nRows;
649 }
650
651 wxSize wxToolBar95::GetMaxSize() const
652 {
653 if ( (m_maxWidth == -1) || (m_maxHeight == -1) )
654 {
655 // it has a side effect of filling m_maxWidth/Height variables
656 ((wxToolBar95 *)this)->SetRows(m_maxRows); // const_cast
657 }
658
659 return wxSize(m_maxWidth, m_maxHeight);
660 }
661
662 // The button size is bigger than the bitmap size
663 wxSize wxToolBar95::GetToolSize() const
664 {
665 // FIXME: this is completely bogus (VZ)
666 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
667 }
668
669 // ----------------------------------------------------------------------------
670 // tool state
671 // ----------------------------------------------------------------------------
672
673 void wxToolBar95::EnableTool(int toolIndex, bool enable)
674 {
675 wxNode *node = m_tools.Find((long)toolIndex);
676 if (node)
677 {
678 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
679 tool->m_enabled = enable;
680 ::SendMessage(GetHwnd(), TB_ENABLEBUTTON,
681 (WPARAM)toolIndex, (LPARAM)MAKELONG(enable, 0));
682 }
683 }
684
685 void wxToolBar95::ToggleTool(int toolIndex, bool toggle)
686 {
687 wxNode *node = m_tools.Find((long)toolIndex);
688 if (node)
689 {
690 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
691 if (tool->m_isToggle)
692 {
693 tool->m_toggleState = toggle;
694 ::SendMessage(GetHwnd(), TB_CHECKBUTTON,
695 (WPARAM)toolIndex, (LPARAM)MAKELONG(toggle, 0));
696 }
697 }
698 }
699
700 bool wxToolBar95::GetToolState(int toolIndex) const
701 {
702 wxASSERT_MSG( GetIndexFromId(toolIndex) != wxNOT_FOUND,
703 _T("invalid toolbar button id") );
704
705 return ::SendMessage(GetHwnd(), TB_ISBUTTONCHECKED,
706 (WPARAM)toolIndex, (LPARAM)0) != 0;
707 }
708
709 // ----------------------------------------------------------------------------
710 // event handlers
711 // ----------------------------------------------------------------------------
712
713 // Responds to colour changes, and passes event on to children.
714 void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent& event)
715 {
716 m_backgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
717 GetGValue(GetSysColor(COLOR_BTNFACE)), GetBValue(GetSysColor(COLOR_BTNFACE)));
718
719 // Remap the buttons
720 CreateTools();
721
722 Refresh();
723
724 // Propagate the event to the non-top-level children
725 wxWindow::OnSysColourChanged(event);
726 }
727
728 void wxToolBar95::OnMouseEvent(wxMouseEvent& event)
729 {
730 if (event.RightDown())
731 {
732 // For now, we don't have an id. Later we could
733 // try finding the tool.
734 OnRightClick((int)-1, event.GetX(), event.GetY());
735 }
736 else
737 {
738 event.Skip();
739 }
740 }
741
742
743 // ----------------------------------------------------------------------------
744 // helpers
745 // ----------------------------------------------------------------------------
746
747 int wxToolBar95::GetIndexFromId(int id) const
748 {
749 size_t count = m_ids.GetCount();
750 for ( size_t n = 0; n < count; n++ )
751 {
752 if ( m_ids[n] == id )
753 return n;
754 }
755
756 return wxNOT_FOUND;
757 }
758
759 // ----------------------------------------------------------------------------
760 // private functions
761 // ----------------------------------------------------------------------------
762
763 // These are the default colors used to map the bitmap colors to the current
764 // system colors. Note that they are in BGR format because this is what Windows
765 // wants (and not RGB)
766
767 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
768 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
769 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
770 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
771 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
772 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
773
774 void wxMapBitmap(HBITMAP hBitmap, int width, int height)
775 {
776 COLORMAP ColorMap[] =
777 {
778 {BGR_BUTTONTEXT, COLOR_BTNTEXT}, // black
779 {BGR_BUTTONSHADOW, COLOR_BTNSHADOW}, // dark grey
780 {BGR_BUTTONFACE, COLOR_BTNFACE}, // bright grey
781 {BGR_BUTTONHILIGHT, COLOR_BTNHIGHLIGHT},// white
782 {BGR_BACKGROUNDSEL, COLOR_HIGHLIGHT}, // blue
783 {BGR_BACKGROUND, COLOR_WINDOW} // magenta
784 };
785
786 int NUM_MAPS = (sizeof(ColorMap)/sizeof(COLORMAP));
787 int n;
788 for ( n = 0; n < NUM_MAPS; n++)
789 {
790 ColorMap[n].to = ::GetSysColor(ColorMap[n].to);
791 }
792
793 HBITMAP hbmOld;
794 HDC hdcMem = CreateCompatibleDC(NULL);
795
796 if (hdcMem)
797 {
798 hbmOld = (HBITMAP) SelectObject(hdcMem, hBitmap);
799
800 int i, j, k;
801 for ( i = 0; i < width; i++)
802 {
803 for ( j = 0; j < height; j++)
804 {
805 COLORREF pixel = ::GetPixel(hdcMem, i, j);
806 /*
807 BYTE red = GetRValue(pixel);
808 BYTE green = GetGValue(pixel);
809 BYTE blue = GetBValue(pixel);
810 */
811
812 for ( k = 0; k < NUM_MAPS; k ++)
813 {
814 if ( ColorMap[k].from == pixel )
815 {
816 /* COLORREF actualPixel = */ ::SetPixel(hdcMem, i, j, ColorMap[k].to);
817 break;
818 }
819 }
820 }
821 }
822
823
824 SelectObject(hdcMem, hbmOld);
825 DeleteObject(hdcMem);
826 }
827
828 }
829
830 // Some experiments...
831 #if 0
832 // What we want to do is create another bitmap which has a depth of 4,
833 // and set the bits. So probably we want to convert this HBITMAP into a
834 // DIB, then call SetDIBits.
835 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
836 // the screen), then SetDIBits fails.
837 HBITMAP newBitmap = ::CreateBitmap(totalBitmapWidth, totalBitmapHeight, 1, 4, NULL);
838 HANDLE newDIB = ::BitmapToDIB((HBITMAP) m_hBitmap, NULL);
839 LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) GlobalLock(newDIB);
840
841 dc = ::GetDC(NULL);
842 // LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
843
844 int result = ::SetDIBits(dc, newBitmap, 0, lpbmi->biHeight, FindDIBBits((LPSTR)lpbmi), (LPBITMAPINFO)lpbmi,
845 DIB_PAL_COLORS);
846 DWORD err = GetLastError();
847
848 ::ReleaseDC(NULL, dc);
849
850 // Delete the DIB
851 GlobalUnlock (newDIB);
852 GlobalFree (newDIB);
853
854 // WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
855 // Substitute our new bitmap for the old one
856 ::DeleteObject((HBITMAP) m_hBitmap);
857 m_hBitmap = (WXHBITMAP) newBitmap;
858 #endif
859
860
861 #endif // !(wxUSE_TOOLBAR && Win95)