]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tbar95.cpp
Dialog items inherit parent's font now
[wxWidgets.git] / src / msw / tbar95.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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
89b892a2 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "tbar95.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
2432b92d 24#include "wx/wx.h"
2bda0e17
KB
25#endif
26
47d67540 27#if wxUSE_BUTTONBAR && wxUSE_TOOLBAR && defined(__WIN95__)
2bda0e17 28
ce3ed50d 29#if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
2bda0e17
KB
30#include "malloc.h"
31#endif
32
33#include <windows.h>
34
57c208c5 35#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
2bda0e17
KB
36#include <commctrl.h>
37#endif
38
57c208c5 39#ifndef __TWIN32__
2bda0e17
KB
40#ifdef __GNUWIN32__
41#include "wx/msw/gnuwin32/extra.h"
42#endif
57c208c5 43#endif
2bda0e17
KB
44
45#include "wx/msw/dib.h"
46#include "wx/tbar95.h"
47#include "wx/app.h"
48#include "wx/msw/private.h"
49
6a23cbce 50// Styles
bb6290e3
JS
51#ifndef TBSTYLE_FLAT
52#define TBSTYLE_LIST 0x1000
53#define TBSTYLE_FLAT 0x0800
54#define TBSTYLE_TRANSPARENT 0x8000
55#endif
56 // use TBSTYLE_TRANSPARENT if you use TBSTYLE_FLAT
57
6a23cbce
JS
58// Messages
59#ifndef TB_GETSTYLE
60#define TB_GETSTYLE (WM_USER + 57)
61#define TB_SETSTYLE (WM_USER + 56)
62#endif
63
64/* Hint from a newsgroup for custom flatbar drawing:
65Set the TBSTYLE_CUSTOMERASE style, then handle the
66NM_CUSTOMDRAW message and do your custom drawing.
67*/
68
81d66cf3
JS
69#define DEFAULTBITMAPX 16
70#define DEFAULTBITMAPY 15
71#define DEFAULTBUTTONX 24
72#define DEFAULTBUTTONY 24
73#define DEFAULTBARHEIGHT 27
74
2bda0e17
KB
75#if !USE_SHARED_LIBRARY
76IMPLEMENT_DYNAMIC_CLASS(wxToolBar95, wxToolBarBase)
89b892a2 77#endif
2bda0e17
KB
78
79BEGIN_EVENT_TABLE(wxToolBar95, wxToolBarBase)
601d9e60
JS
80 EVT_SIZE(wxToolBar95::OnSize)
81 EVT_PAINT(wxToolBar95::OnPaint)
89b892a2 82 EVT_MOUSE_EVENTS(wxToolBar95::OnMouseEvent)
601d9e60 83 EVT_KILL_FOCUS(wxToolBar95::OnKillFocus)
2bda0e17
KB
84 EVT_SYS_COLOUR_CHANGED(wxToolBar95::OnSysColourChanged)
85END_EVENT_TABLE()
2bda0e17 86
89b892a2 87static void wxMapBitmap(HBITMAP hBitmap, int width, int height);
2bda0e17 88
89b892a2 89wxToolBar95::wxToolBar95()
2bda0e17 90{
2bda0e17
KB
91 m_maxWidth = -1;
92 m_maxHeight = -1;
93 m_hBitmap = 0;
94 m_defaultWidth = DEFAULTBITMAPX;
95 m_defaultHeight = DEFAULTBITMAPY;
96}
97
89b892a2
VZ
98bool wxToolBar95::Create(wxWindow *parent,
99 wxWindowID id,
100 const wxPoint& pos,
101 const wxSize& size,
102 long style,
103 const wxString& name)
2bda0e17
KB
104{
105 m_backgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
89b892a2
VZ
106 GetGValue(GetSysColor(COLOR_BTNFACE)),
107 GetBValue(GetSysColor(COLOR_BTNFACE)));
2bda0e17
KB
108 m_foregroundColour = *wxBLACK ;
109
89b892a2
VZ
110 wxASSERT_MSG( (style & wxTB_VERTICAL) == 0,
111 "Sorry, wxToolBar95 under Windows 95 only "
112 "supports horizontal orientation." );
113
2bda0e17
KB
114 m_maxWidth = -1;
115 m_maxHeight = -1;
89b892a2 116
2bda0e17
KB
117 m_hBitmap = 0;
118
119 m_defaultWidth = DEFAULTBITMAPX;
120 m_defaultHeight = DEFAULTBITMAPY;
121 SetName(name);
122
2bda0e17
KB
123 m_windowStyle = style;
124
1c089c47 125 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
2bda0e17
KB
126 SetParent(parent);
127
89b892a2
VZ
128 int x = pos.x;
129 int y = pos.y;
130 int width = size.x;
131 int height = size.y;
132
2bda0e17
KB
133 if (width <= 0)
134 width = 100;
135 if (height <= 0)
136 height = 30;
137 if (x < 0)
138 x = 0;
139 if (y < 0)
140 y = 0;
141
142 m_windowId = (id < 0 ? NewControlId() : id);
89b892a2
VZ
143 DWORD msflags = 0;
144 if (style & wxBORDER)
145 msflags |= WS_BORDER;
146 msflags |= WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS;
bb6290e3
JS
147
148 if (style & wxTB_FLAT)
149 {
150 if (wxTheApp->GetComCtl32Version() > 400)
89b892a2 151 msflags |= TBSTYLE_FLAT;
bb6290e3 152 }
2bda0e17
KB
153
154 // Create the toolbar control.
89b892a2
VZ
155 HWND hWndToolbar = CreateWindowEx
156 (
157 0L, // No extended styles.
158 TOOLBARCLASSNAME, // Class name for the toolbar.
159 "", // No default text.
160 msflags, // Styles
161 x, y, width, height, // Standard toolbar size and position.
162 (HWND) parent->GetHWND(), // Parent window of the toolbar.
163 (HMENU)m_windowId, // Toolbar ID.
164 wxGetInstance(), // Current instance.
165 NULL // No class data.
166 );
167
168 wxCHECK_MSG( hWndToolbar, FALSE, "Toolbar creation failed" );
2bda0e17
KB
169
170 // Toolbar-specific initialisation
89b892a2
VZ
171 ::SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE,
172 (WPARAM)sizeof(TBBUTTON), (LPARAM)0);
2bda0e17
KB
173
174 m_hWnd = (WXHWND) hWndToolbar;
89b892a2
VZ
175 if (parent)
176 parent->AddChild(this);
177
178 SubclassWin((WXHWND)hWndToolbar);
2bda0e17
KB
179
180 return TRUE;
181}
182
89b892a2 183wxToolBar95::~wxToolBar95()
2bda0e17
KB
184{
185 UnsubclassWin();
186
187 if (m_hBitmap)
188 {
189 ::DeleteObject((HBITMAP) m_hBitmap);
190 m_hBitmap = 0;
191 }
192}
193
89b892a2 194bool wxToolBar95::CreateTools()
2bda0e17
KB
195{
196 if (m_tools.Number() == 0)
197 return FALSE;
198
199 HBITMAP oldToolBarBitmap = (HBITMAP) m_hBitmap;
89b892a2 200
2bda0e17
KB
201 int totalBitmapWidth = (int)(m_defaultWidth * m_tools.Number());
202 int totalBitmapHeight = (int)m_defaultHeight;
203
204 // Create a bitmap for all the tool bitmaps
205 HDC dc = ::GetDC(NULL);
206 m_hBitmap = (WXHBITMAP) ::CreateCompatibleBitmap(dc, totalBitmapWidth, totalBitmapHeight);
207 ::ReleaseDC(NULL, dc);
89b892a2 208
2bda0e17
KB
209 // Now blit all the tools onto this bitmap
210 HDC memoryDC = ::CreateCompatibleDC(NULL);
c4e7c2aa 211 HBITMAP oldBitmap = (HBITMAP) ::SelectObject(memoryDC, (HBITMAP) m_hBitmap);
2bda0e17
KB
212
213 HDC memoryDC2 = ::CreateCompatibleDC(NULL);
214 int x = 0;
215 wxNode *node = m_tools.First();
216 int noButtons = 0;
217 while (node)
218 {
219 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
220 if ((tool->m_toolStyle != wxTOOL_STYLE_SEPARATOR) && tool->m_bitmap1.Ok() && tool->m_bitmap1.GetHBITMAP())
221 {
222// wxPalette *palette = tool->m_bitmap1->GetPalette();
223
c4e7c2aa 224 HBITMAP oldBitmap2 = (HBITMAP) ::SelectObject(memoryDC2, (HBITMAP) tool->m_bitmap1.GetHBITMAP());
2bda0e17 225 /* int bltResult = */
89b892a2 226 BitBlt(memoryDC, x, 0, (int) m_defaultWidth, (int) m_defaultHeight, memoryDC2,
2bda0e17
KB
227 0, 0, SRCCOPY);
228 ::SelectObject(memoryDC2, oldBitmap2);
229 x += (int)m_defaultWidth;
230 noButtons ++;
231 }
232 node = node->Next();
233 }
234 ::SelectObject(memoryDC, oldBitmap);
235 ::DeleteDC(memoryDC);
236 ::DeleteDC(memoryDC2);
237
238 // Map to system colours
239 wxMapBitmap((HBITMAP) m_hBitmap, totalBitmapWidth, totalBitmapHeight);
240
241 if ( oldToolBarBitmap )
242 {
243 TBREPLACEBITMAP replaceBitmap;
244 replaceBitmap.hInstOld = NULL;
245 replaceBitmap.hInstNew = NULL;
246 replaceBitmap.nIDOld = (UINT) oldToolBarBitmap;
247 replaceBitmap.nIDNew = (UINT) (HBITMAP) m_hBitmap;
248 replaceBitmap.nButtons = noButtons;
249 if (::SendMessage((HWND) GetHWND(), TB_REPLACEBITMAP, (WPARAM) 0, (LPARAM) &replaceBitmap) == -1)
250 wxMessageBox("Could not add bitmap to toolbar");
251
252 ::DeleteObject((HBITMAP) oldToolBarBitmap);
253
254 // Now delete all the buttons
255 int i = 0;
256 while ( TRUE )
257 {
258 // TODO: What about separators???? They don't have an id!
259 if ( ! ::SendMessage( (HWND) GetHWND(), TB_DELETEBUTTON, i, 0 ) )
260 break;
261 }
262 }
263 else
264 {
265 TBADDBITMAP addBitmap;
266 addBitmap.hInst = 0;
267 addBitmap.nID = (UINT)m_hBitmap;
268 if (::SendMessage((HWND) GetHWND(), TB_ADDBITMAP, (WPARAM) noButtons, (LPARAM) &addBitmap) == -1)
269 wxMessageBox("Could not add bitmap to toolbar");
270 }
271
272 // Now add the buttons.
273 TBBUTTON buttons[50];
274
275 node = m_tools.First();
276 int i = 0;
277 int bitmapId = 0;
278 while (node)
279 {
280 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
281 if (tool->m_toolStyle == wxTOOL_STYLE_SEPARATOR)
282 {
283 buttons[i].iBitmap = 0;
284 buttons[i].idCommand = 0;
285
286 buttons[i].fsState = TBSTATE_ENABLED;
287 buttons[i].fsStyle = TBSTYLE_SEP;
288 buttons[i].dwData = 0L;
289 buttons[i].iString = 0;
290 }
291 else
292 {
293 buttons[i].iBitmap = bitmapId;
294 buttons[i].idCommand = tool->m_index;
295
296 buttons[i].fsState = 0;
297 if (tool->m_enabled)
298 buttons[i].fsState |= TBSTATE_ENABLED;
299 if (tool->m_toggleState)
300 buttons[i].fsState |= TBSTATE_CHECKED;
301 buttons[i].fsStyle = tool->m_isToggle ? TBSTYLE_CHECK : TBSTYLE_BUTTON;
302 buttons[i].dwData = 0L;
303 buttons[i].iString = 0;
304
305 bitmapId ++;
306 }
89b892a2 307
2bda0e17
KB
308 i ++;
309 node = node->Next();
310 }
311
89b892a2 312 long rc = ::SendMessage((HWND) GetHWND(), TB_ADDBUTTONS, (WPARAM)i, (LPARAM)& buttons);
2bda0e17 313
89b892a2
VZ
314 wxCHECK_MSG( rc, FALSE, "failed to add buttons to the toolbar" );
315
316 (void)::SendMessage((HWND) GetHWND(), TB_AUTOSIZE, (WPARAM)0, (LPARAM) 0);
317
318 SetRows(m_maxRows);
2bda0e17
KB
319
320 return TRUE;
321}
322
debe6624 323bool wxToolBar95::MSWCommand(WXUINT cmd, WXWORD id)
2bda0e17
KB
324{
325 wxNode *node = m_tools.Find((long)id);
326 if (!node)
327 return FALSE;
328 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
329 if (tool->m_isToggle)
330 tool->m_toggleState = (1 == (1 & (int)::SendMessage((HWND) GetHWND(), TB_GETSTATE, (WPARAM) id, (LPARAM) 0)));
331
332 BOOL ret = OnLeftClick((int)id, tool->m_toggleState);
333 if (ret == FALSE && tool->m_isToggle)
334 {
335 tool->m_toggleState = !tool->m_toggleState;
336 ::SendMessage((HWND) GetHWND(), TB_CHECKBUTTON, (WPARAM)id, (LPARAM)MAKELONG(tool->m_toggleState, 0));
337 }
338 return TRUE;
339}
340
fd3f686c
VZ
341bool wxToolBar95::MSWNotify(WXWPARAM WXUNUSED(wParam),
342 WXLPARAM lParam,
343 WXLPARAM *result)
2bda0e17 344{
89b892a2 345 // First check if this applies to us
2bda0e17 346 NMHDR *hdr = (NMHDR *)lParam;
2bda0e17 347
89b892a2
VZ
348 // the tooltips control created by the toolbar is sometimes Unicode, even in
349 // an ANSI application
350 if ( (hdr->code != TTN_NEEDTEXTA) && (hdr->code != TTN_NEEDTEXTW) )
351 return FALSE;
2bda0e17 352
89b892a2
VZ
353 HWND toolTipWnd = (HWND)::SendMessage((HWND)GetHWND(), TB_GETTOOLTIPS, 0, 0);
354 if ( toolTipWnd != hdr->hwndFrom )
355 return FALSE;
2bda0e17 356
89b892a2
VZ
357 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
358 int id = (int)ttText->hdr.idFrom;
359 wxNode *node = m_tools.Find((long)id);
360 if (!node)
361 return FALSE;
362
363 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
2bda0e17 364
89b892a2
VZ
365 if ( tool->m_shortHelpString != "" )
366 {
367 if ( hdr->code == TTN_NEEDTEXTA )
368 {
369 ttText->lpszText = (char *)(const char *)tool->m_shortHelpString;
370 }
371#if (_WIN32_IE >= 0x0300)
372 else
373 {
374 // FIXME this is a temp hack only until I understand better what
375 // must be done in both ANSI and Unicode builds
376 size_t lenAnsi = tool->m_shortHelpString.Len();
377 size_t lenUnicode = mbstowcs(NULL, tool->m_shortHelpString, lenAnsi);
378 wchar_t *pwz = new wchar_t[lenUnicode + 1];
379 mbstowcs(pwz, tool->m_shortHelpString, lenAnsi + 1);
380 memcpy(ttText->szText, pwz,
381 (sizeof(ttText->szText) - 1)/sizeof(ttText->szText[0]));
382 ttText->szText[WXSIZEOF(ttText->szText)] = 0;
383
384 delete [] pwz;
385 }
386#endif // _WIN32_IE >= 0x0300
2bda0e17 387 }
89b892a2
VZ
388
389 // For backward compatibility...
390 OnMouseEnter(tool->m_index);
391
392 return TRUE;
2bda0e17
KB
393}
394
81d66cf3 395void wxToolBar95::SetToolBitmapSize(const wxSize& size)
2bda0e17 396{
89b892a2
VZ
397 m_defaultWidth = size.x;
398 m_defaultHeight = size.y;
2bda0e17
KB
399 ::SendMessage((HWND) GetHWND(), TB_SETBITMAPSIZE, 0, (LPARAM) MAKELONG ((int)size.x, (int)size.y));
400}
401
debe6624 402void wxToolBar95::SetRows(int nRows)
2bda0e17
KB
403{
404 RECT rect;
405 ::SendMessage((HWND) GetHWND(), TB_SETROWS, MAKEWPARAM(nRows, TRUE), (LPARAM) & rect);
406 m_maxWidth = (rect.right - rect.left + 2);
407 m_maxHeight = (rect.bottom - rect.top + 2);
408}
409
89b892a2 410wxSize wxToolBar95::GetMaxSize() const
2bda0e17 411{
dfad0599 412 if ((m_maxWidth == -1) || (m_maxHeight == -1))
2bda0e17
KB
413 {
414 RECT rect;
81d66cf3 415 ::SendMessage((HWND) GetHWND(), TB_SETROWS, MAKEWPARAM(m_maxRows, TRUE), (LPARAM) & rect);
2bda0e17
KB
416 ((wxToolBar95 *)this)->m_maxWidth = (rect.right - rect.left + 2); // ???
417 ((wxToolBar95 *)this)->m_maxHeight = (rect.bottom - rect.top + 2); // ???
418 }
419 return wxSize(m_maxWidth, m_maxHeight);
420}
421
422void wxToolBar95::GetSize(int *w, int *h) const
423{
424 wxWindow::GetSize(w, h);
425 // For some reason, the returned height is several pixels bigger than that
426 // displayed!
427 *h -= 2;
428}
429
430// The button size is bigger than the bitmap size
89b892a2 431wxSize wxToolBar95::GetToolSize() const
2bda0e17
KB
432{
433 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
434}
435
debe6624 436void wxToolBar95::EnableTool(int toolIndex, bool enable)
2bda0e17
KB
437{
438 wxNode *node = m_tools.Find((long)toolIndex);
439 if (node)
440 {
441 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
442 tool->m_enabled = enable;
443 ::SendMessage((HWND) GetHWND(), TB_ENABLEBUTTON, (WPARAM)toolIndex, (LPARAM)MAKELONG(enable, 0));
444 }
445}
446
debe6624 447void wxToolBar95::ToggleTool(int toolIndex, bool toggle)
2bda0e17
KB
448{
449 wxNode *node = m_tools.Find((long)toolIndex);
450 if (node)
451 {
452 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
453 if (tool->m_isToggle)
454 {
455 tool->m_toggleState = toggle;
456 ::SendMessage((HWND) GetHWND(), TB_CHECKBUTTON, (WPARAM)toolIndex, (LPARAM)MAKELONG(toggle, 0));
457 }
458 }
459}
460
088a95f5
JS
461bool wxToolBar95::GetToolState(int toolIndex) const
462{
463 return (::SendMessage((HWND) GetHWND(), TB_ISBUTTONCHECKED, (WPARAM)toolIndex, (LPARAM)0) != 0);
464}
465
89b892a2 466void wxToolBar95::ClearTools()
2bda0e17
KB
467{
468 // TODO: Don't know how to reset the toolbar bitmap, as yet.
469 // But adding tools and calling CreateTools should probably
470 // recreate a buttonbar OK.
471 wxToolBarBase::ClearTools();
472}
473
474// If pushedBitmap is NULL, a reversed version of bitmap is
475// created and used as the pushed/toggled image.
476// If toggle is TRUE, the button toggles between the two states.
debe6624
JS
477wxToolBarTool *wxToolBar95::AddTool(int index, const wxBitmap& bitmap, const wxBitmap& pushedBitmap,
478 bool toggle, long xPos, long yPos, wxObject *clientData, const wxString& helpString1, const wxString& helpString2)
2bda0e17 479{
dfad0599 480 wxToolBarTool *tool = new wxToolBarTool(index, bitmap, wxNullBitmap, toggle, xPos, yPos, helpString1, helpString2);
2bda0e17
KB
481 tool->m_clientData = clientData;
482
483 if (xPos > -1)
484 tool->m_x = xPos;
485 else
486 tool->m_x = m_xMargin;
487
488 if (yPos > -1)
489 tool->m_y = yPos;
490 else
491 tool->m_y = m_yMargin;
492
ca5e9f67 493 tool->SetSize(GetToolSize().x, GetToolSize().y);
2bda0e17
KB
494
495 m_tools.Append((long)index, tool);
496 return tool;
497}
498
499// Responds to colour changes, and passes event on to children.
500void wxToolBar95::OnSysColourChanged(wxSysColourChangedEvent& event)
501{
502 m_backgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
89b892a2 503 GetGValue(GetSysColor(COLOR_BTNFACE)), GetBValue(GetSysColor(COLOR_BTNFACE)));
2bda0e17
KB
504
505 // Remap the buttons
506 CreateTools();
507
508 Default();
509
510 Refresh();
511
512 // Propagate the event to the non-top-level children
513 wxWindow::OnSysColourChanged(event);
514}
515
e6460682
JS
516void wxToolBar95::OnMouseEvent(wxMouseEvent& event)
517{
518 if (event.RightDown())
519 {
520 // For now, we don't have an id. Later we could
521 // try finding the tool.
522 OnRightClick((int)-1, event.GetX(), event.GetY());
523 }
524 else
525 {
526 Default();
527 }
528}
529
2bda0e17
KB
530// These are the default colors used to map the bitmap colors
531// to the current system colors
532
533#define BGR_BUTTONTEXT (RGB(000,000,000)) // black
534#define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
535#define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
536#define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
537#define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
538#define BGR_BACKGROUND (RGB(255,000,255)) // magenta
539
540void wxMapBitmap(HBITMAP hBitmap, int width, int height)
541{
542 COLORMAP ColorMap[] = {
543 {BGR_BUTTONTEXT, COLOR_BTNTEXT}, // black
544 {BGR_BUTTONSHADOW, COLOR_BTNSHADOW}, // dark grey
545 {BGR_BUTTONFACE, COLOR_BTNFACE}, // bright grey
546 {BGR_BUTTONHILIGHT, COLOR_BTNHIGHLIGHT},// white
547 {BGR_BACKGROUNDSEL, COLOR_HIGHLIGHT}, // blue
548 {BGR_BACKGROUND, COLOR_WINDOW} // magenta
549 };
550
551 int NUM_MAPS = (sizeof(ColorMap)/sizeof(COLORMAP));
552 int n;
553 for ( n = 0; n < NUM_MAPS; n++)
554 {
555 ColorMap[n].to = ::GetSysColor(ColorMap[n].to);
556 }
557
558 HBITMAP hbmOld;
559 HDC hdcMem = CreateCompatibleDC(NULL);
560
561 if (hdcMem)
562 {
c4e7c2aa 563 hbmOld = (HBITMAP) SelectObject(hdcMem, hBitmap);
2bda0e17
KB
564
565 int i, j, k;
566 for ( i = 0; i < width; i++)
567 {
568 for ( j = 0; j < height; j++)
569 {
570 COLORREF pixel = ::GetPixel(hdcMem, i, j);
571/*
572 BYTE red = GetRValue(pixel);
573 BYTE green = GetGValue(pixel);
574 BYTE blue = GetBValue(pixel);
575*/
576
577 for ( k = 0; k < NUM_MAPS; k ++)
578 {
579 if ( ColorMap[k].from == pixel )
580 {
581 /* COLORREF actualPixel = */ ::SetPixel(hdcMem, i, j, ColorMap[k].to);
582 break;
583 }
584 }
585 }
586 }
587
588
589 SelectObject(hdcMem, hbmOld);
590 DeleteObject(hdcMem);
591 }
592
593}
594
595// Some experiments...
596#if 0
597 // What we want to do is create another bitmap which has a depth of 4,
598 // and set the bits. So probably we want to convert this HBITMAP into a
599 // DIB, then call SetDIBits.
600 // AAAGH. The stupid thing is that if newBitmap has a depth of 4 (less than that of
601 // the screen), then SetDIBits fails.
602 HBITMAP newBitmap = ::CreateBitmap(totalBitmapWidth, totalBitmapHeight, 1, 4, NULL);
603 HANDLE newDIB = ::BitmapToDIB((HBITMAP) m_hBitmap, NULL);
604 LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) GlobalLock(newDIB);
605
606 dc = ::GetDC(NULL);
607// LPBITMAPINFOHEADER lpbmi = (LPBITMAPINFOHEADER) newDIB;
608
609 int result = ::SetDIBits(dc, newBitmap, 0, lpbmi->biHeight, FindDIBBits((LPSTR)lpbmi), (LPBITMAPINFO)lpbmi,
610 DIB_PAL_COLORS);
611 DWORD err = GetLastError();
612
613 ::ReleaseDC(NULL, dc);
614
615 // Delete the DIB
616 GlobalUnlock (newDIB);
617 GlobalFree (newDIB);
618
619// WXHBITMAP hBitmap2 = wxCreateMappedBitmap((WXHINSTANCE) wxGetInstance(), (WXHBITMAP) m_hBitmap);
620 // Substitute our new bitmap for the old one
621 ::DeleteObject((HBITMAP) m_hBitmap);
622 m_hBitmap = (WXHBITMAP) newBitmap;
623#endif
624
625
626#endif