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