]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: tbarmsw.cpp | |
3 | // Purpose: wxToolBarMSW | |
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 | #ifdef __GNUG__ | |
13 | #pragma implementation "tbarmsw.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 | |
24 | #include "wx.h" | |
25 | #endif | |
26 | ||
27 | #if USE_BUTTONBAR && USE_TOOLBAR | |
28 | ||
29 | #ifndef __GNUWIN32__ | |
30 | #include "malloc.h" | |
31 | #endif | |
32 | ||
33 | #include <memory.h> | |
34 | #include <stdlib.h> | |
35 | ||
36 | #include "wx/tbarmsw.h" | |
37 | #include "wx/app.h" | |
38 | #include "wx/msw/private.h" | |
39 | #include "wx/msw/dib.h" | |
40 | ||
41 | /////// Non-Windows 95 implementation | |
42 | ||
43 | #if !USE_IMAGE_LOADING_IN_MSW | |
44 | #error If USE_IMAGE_LOADING_IN_MSW is set to 0, then USE_BUTTONBAR must be set to 0 too. | |
45 | #endif | |
46 | ||
47 | #if !USE_SHARED_LIBRARY | |
48 | IMPLEMENT_DYNAMIC_CLASS(wxToolBarMSW, wxToolBarBase) | |
49 | ||
50 | BEGIN_EVENT_TABLE(wxToolBarMSW, wxToolBarBase) | |
51 | EVT_SIZE(wxToolBarMSW::OnSize) | |
52 | EVT_PAINT(wxToolBarMSW::OnPaint) | |
53 | EVT_MOUSE_EVENTS(wxToolBarMSW::OnMouseEvent) | |
54 | END_EVENT_TABLE() | |
55 | #endif | |
56 | ||
57 | wxToolBarMSW::wxToolBarMSW(void) | |
58 | { | |
59 | m_hbrDither = 0; | |
60 | m_rgbFace = 0; | |
61 | m_rgbShadow = 0; | |
62 | m_rgbHilight = 0; | |
63 | m_rgbFrame = 0; | |
64 | m_hdcMono = 0; | |
65 | m_hbmMono = 0; | |
66 | m_hbmDefault = 0; | |
67 | m_defaultWidth = DEFAULTBITMAPX; | |
68 | m_defaultHeight = DEFAULTBITMAPY; | |
69 | } | |
70 | ||
71 | bool wxToolBarMSW::Create(wxWindow *parent, const wxWindowID id, const wxPoint& pos, const wxSize& size, | |
72 | const long style, const int orientation, | |
73 | const int RowsOrColumns, const wxString& name) | |
74 | { | |
75 | if ( ! wxWindow::Create(parent, id, pos, size, style, name) ) | |
76 | return FALSE; | |
77 | ||
78 | m_tilingDirection = orientation; | |
79 | m_rowsOrColumns = RowsOrColumns; | |
80 | if ( m_tilingDirection == wxVERTICAL ) | |
81 | { m_lastX = 3; m_lastY = 7; } | |
82 | else | |
83 | { m_lastX = 7; m_lastY = 3; } | |
84 | m_maxWidth = m_maxHeight = 0; | |
85 | m_pressedTool = m_currentTool = -1; | |
86 | m_xMargin = 0; | |
87 | m_yMargin = 0; | |
88 | m_toolPacking = 1; | |
89 | m_toolSeparation = 5; | |
90 | ||
91 | // Set it to grey | |
92 | SetBackgroundColour(wxColour(192, 192, 192)); | |
93 | ||
94 | m_hbrDither = 0; | |
95 | m_rgbFace = 0; | |
96 | m_rgbShadow = 0; | |
97 | m_rgbHilight = 0; | |
98 | m_rgbFrame = 0; | |
99 | m_hdcMono = 0; | |
100 | m_hbmMono = 0; | |
101 | m_hbmDefault = 0; | |
102 | m_defaultWidth = DEFAULTBITMAPX; | |
103 | m_defaultHeight = DEFAULTBITMAPY; | |
104 | ||
105 | InitGlobalObjects(); | |
106 | ||
107 | return TRUE; | |
108 | } | |
109 | ||
110 | wxToolBarMSW::~wxToolBarMSW(void) | |
111 | { | |
112 | FreeGlobalObjects(); | |
113 | } | |
114 | ||
115 | void wxToolBarMSW::SetDefaultSize(const wxSize& size) | |
116 | { | |
117 | m_defaultWidth = size.x; m_defaultHeight = size.y; | |
118 | FreeGlobalObjects(); | |
119 | InitGlobalObjects(); | |
120 | } | |
121 | ||
122 | // The button size is bigger than the bitmap size | |
123 | wxSize wxToolBarMSW::GetDefaultButtonSize(void) const | |
124 | { | |
125 | return wxSize(m_defaultWidth + 8, m_defaultHeight + 7); | |
126 | } | |
127 | ||
128 | void wxToolBarMSW::OnPaint(wxPaintEvent& event) | |
129 | { | |
130 | wxPaintDC dc(this); | |
131 | ||
132 | static int wxOnPaintCount = 0; | |
133 | ||
134 | // Prevent reentry of OnPaint which would cause | |
135 | // wxMemoryDC errors. | |
136 | if (wxOnPaintCount > 0) | |
137 | return; | |
138 | wxOnPaintCount ++; | |
139 | ||
140 | wxNode *node = m_tools.First(); | |
141 | while (node) | |
142 | { | |
143 | wxToolBarTool *tool = (wxToolBarTool *)node->Data(); | |
144 | if (tool->m_toolStyle != wxTOOL_STYLE_SEPARATOR) | |
145 | { | |
146 | int state = wxTBSTATE_ENABLED; | |
147 | if (!tool->m_enabled) | |
148 | state = 0; | |
149 | if (tool->m_isToggle && tool->m_toggleState) | |
150 | state |= wxTBSTATE_CHECKED; | |
151 | DrawTool(dc, tool, state); | |
152 | } | |
153 | node = node->Next(); | |
154 | } | |
155 | wxOnPaintCount --; | |
156 | } | |
157 | ||
158 | void wxToolBarMSW::OnSize(wxSizeEvent& event) | |
159 | { | |
160 | wxToolBarBase::OnSize(event); | |
161 | } | |
162 | ||
163 | // If a Button is disabled, then NO function (besides leaving | |
164 | // or entering) should be carried out. Therefore the additions | |
165 | // of 'enabled' testing (Stefan Hammes). | |
166 | void wxToolBarMSW::OnMouseEvent(wxMouseEvent& event) | |
167 | { | |
168 | static wxToolBarTool *eventCurrentTool = NULL; | |
169 | wxClientDC dc(this); | |
170 | ||
171 | if (event.Leaving()) | |
172 | { | |
173 | m_currentTool = -1; | |
174 | if (eventCurrentTool && eventCurrentTool->m_enabled) | |
175 | { | |
176 | ::ReleaseCapture(); | |
177 | int state = wxTBSTATE_ENABLED; | |
178 | if (eventCurrentTool->m_toggleState) | |
179 | state |= wxTBSTATE_CHECKED; | |
180 | DrawTool(dc, eventCurrentTool, state); | |
181 | eventCurrentTool = NULL; | |
182 | } | |
183 | OnMouseEnter(-1); | |
184 | return; | |
185 | } | |
186 | ||
187 | long x, y; | |
188 | event.Position(&x, &y); | |
189 | wxToolBarTool *tool = FindToolForPosition(x, y); | |
190 | ||
191 | if (!tool) | |
192 | { | |
193 | if (eventCurrentTool && eventCurrentTool->m_enabled) | |
194 | { | |
195 | ::ReleaseCapture(); | |
196 | ||
197 | int state = wxTBSTATE_ENABLED; | |
198 | if (eventCurrentTool->m_toggleState) | |
199 | state |= wxTBSTATE_CHECKED; | |
200 | DrawTool(dc, eventCurrentTool, state); | |
201 | eventCurrentTool = NULL; | |
202 | } | |
203 | if (m_currentTool > -1) | |
204 | { | |
205 | m_currentTool = -1; | |
206 | OnMouseEnter(-1); | |
207 | } | |
208 | return; | |
209 | } | |
210 | ||
211 | if (!event.Dragging() && !event.IsButton()) | |
212 | { | |
213 | if (tool->m_index != m_currentTool) | |
214 | { | |
215 | OnMouseEnter(tool->m_index); | |
216 | m_currentTool = tool->m_index; | |
217 | return; | |
218 | } | |
219 | } | |
220 | if (event.Dragging() && tool->m_enabled) | |
221 | { | |
222 | if (eventCurrentTool) | |
223 | { | |
224 | // Might have dragged outside tool | |
225 | if (eventCurrentTool != tool) | |
226 | { | |
227 | int state = wxTBSTATE_ENABLED; | |
228 | if (tool->m_toggleState) | |
229 | state |= wxTBSTATE_CHECKED; | |
230 | DrawTool(dc, tool, state); | |
231 | eventCurrentTool = NULL; | |
232 | return; | |
233 | } | |
234 | } | |
235 | else | |
236 | { | |
237 | if (tool && event.LeftIsDown() && tool->m_enabled) | |
238 | { | |
239 | eventCurrentTool = tool; | |
240 | ::SetCapture((HWND) GetHWND()); | |
241 | int state = wxTBSTATE_ENABLED|wxTBSTATE_PRESSED; | |
242 | if (tool->m_toggleState) | |
243 | state |= wxTBSTATE_CHECKED; | |
244 | DrawTool(dc, tool, state); | |
245 | } | |
246 | } | |
247 | } | |
248 | if (event.LeftDown() && tool->m_enabled) | |
249 | { | |
250 | eventCurrentTool = tool; | |
251 | ::SetCapture((HWND) GetHWND()); | |
252 | int state = wxTBSTATE_ENABLED|wxTBSTATE_PRESSED; | |
253 | if (tool->m_toggleState) | |
254 | state |= wxTBSTATE_CHECKED; | |
255 | DrawTool(dc, tool, state); | |
256 | } | |
257 | else if (event.LeftUp() && tool->m_enabled) | |
258 | { | |
259 | if (eventCurrentTool) | |
260 | ::ReleaseCapture(); | |
261 | if (eventCurrentTool == tool) | |
262 | { | |
263 | if (tool->m_isToggle) | |
264 | { | |
265 | tool->m_toggleState = !tool->m_toggleState; | |
266 | if (!OnLeftClick(tool->m_index, tool->m_toggleState)) | |
267 | { | |
268 | tool->m_toggleState = !tool->m_toggleState; | |
269 | } | |
270 | int state = wxTBSTATE_ENABLED; | |
271 | if (tool->m_toggleState) | |
272 | state |= wxTBSTATE_CHECKED; | |
273 | DrawTool(dc, tool, state); | |
274 | } | |
275 | else | |
276 | { | |
277 | int state = wxTBSTATE_ENABLED; | |
278 | if (tool->m_toggleState) | |
279 | state |= wxTBSTATE_CHECKED; | |
280 | DrawTool(dc, tool, state); | |
281 | OnLeftClick(tool->m_index, tool->m_toggleState); | |
282 | } | |
283 | } | |
284 | eventCurrentTool = NULL; | |
285 | } | |
286 | else if (event.RightDown() && tool->m_enabled) | |
287 | { | |
288 | OnRightClick(tool->m_index, x, y); | |
289 | } | |
290 | } | |
291 | ||
292 | // This function enables/disables a toolbar tool and redraws it. | |
293 | // If that would not be done, the enabling/disabling wouldn't be | |
294 | // visible on the screen. | |
295 | void wxToolBarMSW::EnableTool(const int toolIndex, const bool enable) | |
296 | { | |
297 | wxNode *node = m_tools.Find((long)toolIndex); | |
298 | if (node) | |
299 | { | |
300 | wxClientDC dc(this); | |
301 | ||
302 | // at first do the enabling/disabling in the base class | |
303 | wxToolBarBase::EnableTool(toolIndex,enable); | |
304 | // then calculate the state of the tool and draw it | |
305 | wxToolBarTool *tool = (wxToolBarTool *)node->Data(); | |
306 | int state = 0; | |
307 | if(tool->m_toggleState) state |= wxTBSTATE_CHECKED; | |
308 | if(tool->m_enabled) state |= wxTBSTATE_ENABLED; | |
309 | // how can i access the PRESSED state??? | |
310 | DrawTool(dc, tool,state); | |
311 | } | |
312 | } | |
313 | ||
314 | void wxToolBarMSW::DrawTool(wxDC& dc, wxToolBarTool *tool, int state) | |
315 | { | |
316 | DrawButton(dc.GetHDC(), (int)tool->m_x, (int)tool->m_y, (int)tool->GetWidth(), (int)tool->GetHeight(), tool, state); | |
317 | } | |
318 | ||
319 | void wxToolBarMSW::DrawTool(wxDC& dc, wxMemoryDC& , wxToolBarTool *tool) | |
320 | { | |
321 | int state = wxTBSTATE_ENABLED; | |
322 | if (!tool->m_enabled) | |
323 | state = 0; | |
324 | if (tool->m_toggleState) | |
325 | state |= wxTBSTATE_CHECKED; | |
326 | DrawTool(dc, tool, state); | |
327 | } | |
328 | ||
329 | // If pushedBitmap is NULL, a reversed version of bitmap is | |
330 | // created and used as the pushed/toggled image. | |
331 | // If toggle is TRUE, the button toggles between the two states. | |
332 | wxToolBarTool *wxToolBarMSW::AddTool(const int index, const wxBitmap& bitmap, const wxBitmap& pushedBitmap, | |
333 | const bool toggle, const long xPos, const long yPos, wxObject *clientData, const wxString& helpString1, const wxString& helpString2) | |
334 | { | |
335 | // Using bitmap2 can cause problems (don't know why!) | |
336 | ||
337 | // TODO: use the mapping code from wxToolBar95 to get it right in this class | |
338 | #if !defined(__WIN32__) && !defined(__WIN386__) | |
339 | wxBitmap *bitmap2 = NULL; | |
340 | if (toggle) | |
341 | { | |
342 | bitmap2 = new wxBitmap; | |
343 | bitmap2->SetHBITMAP( (WXHBITMAP) CreateMappedBitmap(wxGetInstance(), (HBITMAP) ((wxBitmap& )bitmap).GetHBITMAP())); | |
344 | } | |
345 | ||
346 | wxToolBarTool *tool = new wxToolBarTool(index, bitmap, *bitmap2, toggle, xPos, yPos, helpString1, helpString2); | |
347 | #else | |
348 | wxToolBarTool *tool = new wxToolBarTool(index, bitmap, (wxBitmap *)NULL, toggle, xPos, yPos, helpString1, helpString2); | |
349 | #endif | |
350 | ||
351 | tool->m_clientData = clientData; | |
352 | ||
353 | if (xPos > -1) | |
354 | tool->m_x = xPos; | |
355 | else | |
356 | tool->m_x = m_xMargin; | |
357 | ||
358 | if (yPos > -1) | |
359 | tool->m_y = yPos; | |
360 | else | |
361 | tool->m_y = m_yMargin; | |
362 | ||
363 | tool->m_deleteSecondBitmap = TRUE; | |
364 | tool->SetSize(GetDefaultButtonWidth(), GetDefaultButtonHeight()); | |
365 | ||
366 | // Calculate reasonable max size in case Layout() not called | |
367 | if ((tool->m_x + bitmap.GetWidth() + m_xMargin) > m_maxWidth) | |
368 | m_maxWidth = (tool->m_x + tool->GetWidth() + m_xMargin); | |
369 | ||
370 | if ((tool->m_y + bitmap.GetHeight() + m_yMargin) > m_maxHeight) | |
371 | m_maxHeight = (tool->m_y + tool->GetHeight() + m_yMargin); | |
372 | ||
373 | m_tools.Append((long)index, tool); | |
374 | return tool; | |
375 | } | |
376 | ||
377 | bool wxToolBarMSW::InitGlobalObjects(void) | |
378 | { | |
379 | GetSysColors(); | |
380 | if (!CreateDitherBrush()) | |
381 | return FALSE; | |
382 | ||
383 | m_hdcMono = (WXHDC) CreateCompatibleDC(NULL); | |
384 | if (!m_hdcMono) | |
385 | return FALSE; | |
386 | ||
387 | m_hbmMono = (WXHBITMAP) CreateBitmap((int)GetDefaultButtonWidth(), (int)GetDefaultButtonHeight(), 1, 1, NULL); | |
388 | if (!m_hbmMono) | |
389 | return FALSE; | |
390 | ||
391 | m_hbmDefault = (WXHBITMAP) SelectObject((HDC) m_hdcMono, (HBITMAP) m_hbmMono); | |
392 | return TRUE; | |
393 | } | |
394 | ||
395 | void wxToolBarMSW::FreeGlobalObjects(void) | |
396 | { | |
397 | FreeDitherBrush(); | |
398 | ||
399 | if (m_hdcMono) { | |
400 | if (m_hbmDefault) | |
401 | { | |
402 | SelectObject((HDC) m_hdcMono, (HBITMAP) m_hbmDefault); | |
403 | m_hbmDefault = 0; | |
404 | } | |
405 | DeleteDC((HDC) m_hdcMono); // toast the DCs | |
406 | } | |
407 | m_hdcMono = 0; | |
408 | ||
409 | if (m_hbmMono) | |
410 | DeleteObject((HBITMAP) m_hbmMono); | |
411 | m_hbmMono = 0; | |
412 | } | |
413 | ||
414 | ||
415 | void wxToolBarMSW::PatB(WXHDC hdc,int x,int y,int dx,int dy, long rgb) | |
416 | { | |
417 | RECT rc; | |
418 | ||
419 | rc.left = x; | |
420 | rc.top = y; | |
421 | rc.right = x + dx; | |
422 | rc.bottom = y + dy; | |
423 | ||
424 | SetBkColor((HDC) hdc,rgb); | |
425 | ExtTextOut((HDC) hdc,0,0,ETO_OPAQUE,&rc,NULL,0,NULL); | |
426 | } | |
427 | ||
428 | ||
429 | // create a mono bitmap mask: | |
430 | // 1's where color == COLOR_BTNFACE || COLOR_HILIGHT | |
431 | // 0's everywhere else | |
432 | ||
433 | void wxToolBarMSW::CreateMask(WXHDC hdc, int xoffset, int yoffset, int dx, int dy) | |
434 | { | |
435 | HDC globalDC = ::GetDC(NULL); | |
436 | HDC hdcGlyphs = CreateCompatibleDC((HDC) globalDC); | |
437 | ReleaseDC(NULL, (HDC) globalDC); | |
438 | ||
439 | // krj - create a new bitmap and copy the image from hdc. | |
440 | //HBITMAP bitmapOld = SelectObject(hdcGlyphs, hBitmap); | |
441 | HBITMAP hBitmap = CreateCompatibleBitmap((HDC) hdc, dx, dy); | |
442 | HBITMAP bitmapOld = SelectObject(hdcGlyphs, hBitmap); | |
443 | BitBlt(hdcGlyphs, 0,0, dx, dy, (HDC) hdc, 0, 0, SRCCOPY); | |
444 | ||
445 | // initalize whole area with 1's | |
446 | PatBlt((HDC) m_hdcMono, 0, 0, dx, dy, WHITENESS); | |
447 | ||
448 | // create mask based on color bitmap | |
449 | // convert this to 1's | |
450 | SetBkColor(hdcGlyphs, m_rgbFace); | |
451 | BitBlt((HDC) m_hdcMono, xoffset, yoffset, (int)GetDefaultWidth(), (int)GetDefaultHeight(), | |
452 | hdcGlyphs, 0, 0, SRCCOPY); | |
453 | // convert this to 1's | |
454 | SetBkColor(hdcGlyphs, m_rgbHilight); | |
455 | // OR in the new 1's | |
456 | BitBlt((HDC) m_hdcMono, xoffset, yoffset, (int)GetDefaultWidth(), (int)GetDefaultHeight(), | |
457 | hdcGlyphs, 0, 0, SRCPAINT); | |
458 | ||
459 | SelectObject(hdcGlyphs, bitmapOld); | |
460 | DeleteObject(hBitmap); | |
461 | DeleteDC(hdcGlyphs); | |
462 | } | |
463 | ||
464 | void wxToolBarMSW::DrawBlankButton(WXHDC hdc, int x, int y, int dx, int dy, int state) | |
465 | { | |
466 | // face color | |
467 | PatB(hdc, x, y, dx, dy, m_rgbFace); | |
468 | ||
469 | if (state & wxTBSTATE_PRESSED) { | |
470 | PatB(hdc, x + 1, y, dx - 2, 1, m_rgbFrame); | |
471 | PatB(hdc, x + 1, y + dy - 1, dx - 2, 1, m_rgbFrame); | |
472 | PatB(hdc, x, y + 1, 1, dy - 2, m_rgbFrame); | |
473 | PatB(hdc, x + dx - 1, y +1, 1, dy - 2, m_rgbFrame); | |
474 | PatB(hdc, x + 1, y + 1, 1, dy-2, m_rgbShadow); | |
475 | PatB(hdc, x + 1, y + 1, dx-2, 1, m_rgbShadow); | |
476 | } | |
477 | else { | |
478 | PatB(hdc, x + 1, y, dx - 2, 1, m_rgbFrame); | |
479 | PatB(hdc, x + 1, y + dy - 1, dx - 2, 1, m_rgbFrame); | |
480 | PatB(hdc, x, y + 1, 1, dy - 2, m_rgbFrame); | |
481 | PatB(hdc, x + dx - 1, y + 1, 1, dy - 2, m_rgbFrame); | |
482 | dx -= 2; | |
483 | dy -= 2; | |
484 | PatB(hdc, x + 1, y + 1, 1, dy - 1, m_rgbHilight); | |
485 | PatB(hdc, x + 1, y + 1, dx - 1, 1, m_rgbHilight); | |
486 | PatB(hdc, x + dx, y + 1, 1, dy, m_rgbShadow); | |
487 | PatB(hdc, x + 1, y + dy, dx, 1, m_rgbShadow); | |
488 | PatB(hdc, x + dx - 1, y + 2, 1, dy - 2, m_rgbShadow); | |
489 | PatB(hdc, x + 2, y + dy - 1, dx - 2, 1, m_rgbShadow); | |
490 | } | |
491 | } | |
492 | ||
493 | void wxToolBarMSW::DrawButton(WXHDC hdc, int x, int y, int dx, int dy, wxToolBarTool *tool, int state) | |
494 | { | |
495 | int yOffset; | |
496 | HBRUSH hbrOld, hbr; | |
497 | BOOL bMaskCreated = FALSE; | |
498 | int xButton = 0; // assume button is down | |
499 | int dxFace, dyFace; | |
500 | int xCenterOffset; | |
501 | ||
502 | dxFace = dx; | |
503 | dyFace = dy; | |
504 | ||
505 | // HBITMAP hBitmap = (HBITMAP) tool->m_bitmap1.GetHBITMAP(); | |
506 | HDC globalDC = ::GetDC(NULL); | |
507 | HDC hdcGlyphs = CreateCompatibleDC(globalDC); | |
508 | ReleaseDC(NULL, globalDC); | |
509 | ||
510 | // get the proper button look - up or down. | |
511 | if (!(state & (wxTBSTATE_PRESSED | wxTBSTATE_CHECKED))) { | |
512 | xButton = dx; // use 'up' version of button | |
513 | dxFace -= 2; | |
514 | dyFace -= 2; // extents to ignore button highlight | |
515 | } | |
516 | ||
517 | DrawBlankButton(hdc, x, y, dx, dy, state); | |
518 | ||
519 | ||
520 | // move coordinates inside border and away from upper left highlight. | |
521 | // the extents change accordingly. | |
522 | x += 2; | |
523 | y += 2; | |
524 | dxFace -= 3; | |
525 | dyFace -= 3; | |
526 | ||
527 | // Using bitmap2 can cause problems (don't know why!) | |
528 | #if !defined(__WIN32__) && !defined(__WIN386__) | |
529 | HBITMAP bitmapOld; | |
530 | if (tool->m_bitmap2.Ok()) | |
531 | bitmapOld = SelectObject(hdcGlyphs, (HBITMAP) tool->m_bitmap2.GetHBITMAP()); | |
532 | else | |
533 | bitmapOld = SelectObject(hdcGlyphs, (HBITMAP) tool->m_bitmap1.GetHBITMAP()); | |
534 | #else | |
535 | HBITMAP bitmapOld = SelectObject(hdcGlyphs, (HBITMAP) tool->m_bitmap1.GetHBITMAP()); | |
536 | #endif | |
537 | ||
538 | // calculate offset of face from (x,y). y is always from the top, | |
539 | // so the offset is easy. x needs to be centered in face. | |
540 | yOffset = 1; | |
541 | xCenterOffset = (dxFace - (int)GetDefaultWidth())/2; | |
542 | if (state & (wxTBSTATE_PRESSED | wxTBSTATE_CHECKED)) | |
543 | { | |
544 | // pressed state moves down and to the right | |
545 | // (x moves automatically as face size grows) | |
546 | yOffset++; | |
547 | } | |
548 | ||
549 | // now put on the face | |
550 | if (state & wxTBSTATE_ENABLED) { | |
551 | // regular version | |
552 | BitBlt((HDC) hdc, x+xCenterOffset, y + yOffset, (int)GetDefaultWidth(), (int)GetDefaultHeight(), | |
553 | hdcGlyphs, 0, 0, SRCCOPY); | |
554 | } else { | |
555 | // disabled version (or indeterminate) | |
556 | bMaskCreated = TRUE; | |
557 | CreateMask((WXHDC) hdcGlyphs, xCenterOffset, yOffset, dxFace, dyFace); | |
558 | // CreateMask(hBitmap, xCenterOffset, yOffset, dxFace, dyFace); | |
559 | ||
560 | SetTextColor((HDC) hdc, 0L); // 0's in mono -> 0 (for ROP) | |
561 | SetBkColor((HDC) hdc, 0x00FFFFFF); // 1's in mono -> 1 | |
562 | ||
563 | // draw glyph's white understrike | |
564 | if (!(state & wxTBSTATE_INDETERMINATE)) { | |
565 | hbr = CreateSolidBrush(m_rgbHilight); | |
566 | if (hbr) { | |
567 | hbrOld = SelectObject((HDC) hdc, hbr); | |
568 | if (hbrOld) { | |
569 | // draw hilight color where we have 0's in the mask | |
570 | BitBlt((HDC) hdc, x + 1, y + 1, dxFace, dyFace, (HDC) m_hdcMono, 0, 0, 0x00B8074A); | |
571 | SelectObject((HDC) hdc, hbrOld); | |
572 | } | |
573 | DeleteObject(hbr); | |
574 | } | |
575 | } | |
576 | ||
577 | // gray out glyph | |
578 | hbr = CreateSolidBrush(m_rgbShadow); | |
579 | if (hbr) { | |
580 | hbrOld = SelectObject((HDC) hdc, hbr); | |
581 | if (hbrOld) { | |
582 | // draw the shadow color where we have 0's in the mask | |
583 | BitBlt((HDC) hdc, x, y, dxFace, dyFace, (HDC) m_hdcMono, 0, 0, 0x00B8074A); | |
584 | SelectObject((HDC) hdc, hbrOld); | |
585 | } | |
586 | DeleteObject(hbr); | |
587 | } | |
588 | ||
589 | if (state & wxTBSTATE_CHECKED) { | |
590 | BitBlt((HDC) m_hdcMono, 1, 1, dxFace - 1, dyFace - 1, (HDC) m_hdcMono, 0, 0, SRCAND); | |
591 | } | |
592 | } | |
593 | ||
594 | if (state & (wxTBSTATE_CHECKED | wxTBSTATE_INDETERMINATE)) { | |
595 | ||
596 | hbrOld = SelectObject((HDC) hdc, (HBRUSH) m_hbrDither); | |
597 | if (hbrOld) { | |
598 | ||
599 | if (!bMaskCreated) | |
600 | CreateMask((WXHDC) hdcGlyphs, xCenterOffset, yOffset, dxFace, dyFace); | |
601 | // CreateMask(hBitmap, xCenterOffset, yOffset, dxFace, dyFace); | |
602 | ||
603 | SetTextColor((HDC) hdc, 0L); // 0 -> 0 | |
604 | SetBkColor((HDC) hdc, 0x00FFFFFF); // 1 -> 1 | |
605 | ||
606 | // only draw the dither brush where the mask is 1's | |
607 | BitBlt((HDC) hdc, x, y, dxFace, dyFace, (HDC) m_hdcMono, 0, 0, 0x00E20746); | |
608 | ||
609 | SelectObject((HDC) hdc, hbrOld); | |
610 | } | |
611 | } | |
612 | SelectObject(hdcGlyphs, bitmapOld); | |
613 | DeleteDC(hdcGlyphs); | |
614 | } | |
615 | ||
616 | void wxToolBarMSW::GetSysColors(void) | |
617 | { | |
618 | static COLORREF rgbSaveFace = 0xffffffffL, | |
619 | rgbSaveShadow = 0xffffffffL, | |
620 | rgbSaveHilight = 0xffffffffL, | |
621 | rgbSaveFrame = 0xffffffffL; | |
622 | ||
623 | // For now, override these because the colour replacement isn't working, | |
624 | // and we get inconsistent colours. Assume all buttons are grey for the moment. | |
625 | ||
626 | // m_rgbFace = GetSysColor(COLOR_BTNFACE); | |
627 | m_rgbFace = RGB(192,192,192); | |
628 | // m_rgbShadow = GetSysColor(COLOR_BTNSHADOW); | |
629 | m_rgbShadow = RGB(128,128,128); | |
630 | // m_rgbHilight = GetSysColor(COLOR_BTNHIGHLIGHT); | |
631 | m_rgbHilight = RGB(255, 255, 255); | |
632 | ||
633 | m_rgbFrame = GetSysColor(COLOR_WINDOWFRAME); | |
634 | ||
635 | if (rgbSaveFace!=m_rgbFace || rgbSaveShadow!=m_rgbShadow | |
636 | || rgbSaveHilight!=m_rgbHilight || rgbSaveFrame!=m_rgbFrame) | |
637 | { | |
638 | rgbSaveFace = m_rgbFace; | |
639 | rgbSaveShadow = m_rgbShadow; | |
640 | rgbSaveHilight = m_rgbHilight; | |
641 | rgbSaveFrame = m_rgbFrame; | |
642 | ||
643 | // Update the brush for pushed-in buttons | |
644 | CreateDitherBrush(); | |
645 | } | |
646 | } | |
647 | ||
648 | WXHBITMAP wxToolBarMSW::CreateDitherBitmap() | |
649 | { | |
650 | BITMAPINFO* pbmi; | |
651 | HBITMAP hbm; | |
652 | HDC hdc; | |
653 | int i; | |
654 | long patGray[8]; | |
655 | DWORD rgb; | |
656 | ||
657 | pbmi = (BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER) + 16*sizeof(RGBQUAD)); | |
658 | memset(pbmi, 0, (sizeof(BITMAPINFOHEADER) + 16*sizeof(RGBQUAD))); | |
659 | ||
660 | pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | |
661 | pbmi->bmiHeader.biWidth = 8; | |
662 | pbmi->bmiHeader.biHeight = 8; | |
663 | pbmi->bmiHeader.biPlanes = 1; | |
664 | pbmi->bmiHeader.biBitCount = 1; | |
665 | pbmi->bmiHeader.biCompression = BI_RGB; | |
666 | ||
667 | // rgb = GetSysColor(COLOR_BTNFACE); | |
668 | rgb = RGB(192,192,192); | |
669 | ||
670 | pbmi->bmiColors[0].rgbBlue = GetBValue(rgb); | |
671 | pbmi->bmiColors[0].rgbGreen = GetGValue(rgb); | |
672 | pbmi->bmiColors[0].rgbRed = GetRValue(rgb); | |
673 | pbmi->bmiColors[0].rgbReserved = 0; | |
674 | ||
675 | // rgb = GetSysColor(COLOR_BTNHIGHLIGHT); | |
676 | rgb = RGB(255, 255, 255); | |
677 | ||
678 | pbmi->bmiColors[1].rgbBlue = GetBValue(rgb); | |
679 | pbmi->bmiColors[1].rgbGreen = GetGValue(rgb); | |
680 | pbmi->bmiColors[1].rgbRed = GetRValue(rgb); | |
681 | pbmi->bmiColors[1].rgbReserved = 0; | |
682 | ||
683 | /* initialize the brushes */ | |
684 | ||
685 | for (i = 0; i < 8; i++) | |
686 | if (i & 1) | |
687 | patGray[i] = 0xAAAA5555L; // 0x11114444L; // lighter gray | |
688 | else | |
689 | patGray[i] = 0x5555AAAAL; // 0x11114444L; // lighter gray | |
690 | ||
691 | hdc = ::GetDC(NULL); | |
692 | ||
693 | hbm = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT, patGray, pbmi, DIB_RGB_COLORS); | |
694 | ||
695 | ReleaseDC(NULL, hdc); | |
696 | free(pbmi); | |
697 | ||
698 | return (WXHBITMAP)hbm; | |
699 | } | |
700 | ||
701 | bool wxToolBarMSW::CreateDitherBrush(void) | |
702 | { | |
703 | HBITMAP hbmGray; | |
704 | HBRUSH hbrSave; | |
705 | if (m_hbrDither) | |
706 | return TRUE; | |
707 | hbmGray = (HBITMAP) CreateDitherBitmap(); | |
708 | ||
709 | if (hbmGray) | |
710 | { | |
711 | hbrSave = (HBRUSH) m_hbrDither; | |
712 | m_hbrDither = (WXHBRUSH) CreatePatternBrush(hbmGray); | |
713 | DeleteObject(hbmGray); | |
714 | if (m_hbrDither) | |
715 | { | |
716 | if (hbrSave) | |
717 | { | |
718 | DeleteObject(hbrSave); | |
719 | } | |
720 | return TRUE; | |
721 | } | |
722 | else | |
723 | { | |
724 | m_hbrDither = (WXHBRUSH) hbrSave; | |
725 | } | |
726 | } | |
727 | ||
728 | return FALSE; | |
729 | } | |
730 | ||
731 | bool wxToolBarMSW::FreeDitherBrush(void) | |
732 | { | |
733 | if (m_hbrDither) | |
734 | DeleteObject((HBRUSH) m_hbrDither); | |
735 | m_hbrDither = 0; | |
736 | return TRUE; | |
737 | } | |
738 | ||
739 | typedef struct tagCOLORMAP2 | |
740 | { | |
741 | COLORREF bgrfrom; | |
742 | COLORREF bgrto; | |
743 | COLORREF sysColor; | |
744 | } COLORMAP2; | |
745 | ||
746 | // these are the default colors used to map the dib colors | |
747 | // to the current system colors | |
748 | ||
749 | #define BGR_BUTTONTEXT (RGB(000,000,000)) // black | |
750 | #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey | |
751 | #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey | |
752 | #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white | |
753 | #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue | |
754 | #define BGR_BACKGROUND (RGB(255,000,255)) // magenta | |
755 | #define FlipColor(rgb) (RGB(GetBValue(rgb), GetGValue(rgb), GetRValue(rgb))) | |
756 | ||
757 | WXHBITMAP wxToolBarMSW::CreateMappedBitmap(WXHINSTANCE WXUNUSED(hInstance), void *info) | |
758 | { | |
759 | LPBITMAPINFOHEADER lpBitmapInfo = (LPBITMAPINFOHEADER)info; | |
760 | HDC hdc, hdcMem = NULL; | |
761 | ||
762 | DWORD FAR *p; | |
763 | LPSTR lpBits; | |
764 | HBITMAP hbm = NULL, hbmOld; | |
765 | int numcolors, i; | |
766 | int wid, hgt; | |
767 | static COLORMAP2 ColorMap[] = { | |
768 | {BGR_BUTTONTEXT, BGR_BUTTONTEXT, COLOR_BTNTEXT}, // black | |
769 | {BGR_BUTTONSHADOW, BGR_BUTTONSHADOW, COLOR_BTNSHADOW}, // dark grey | |
770 | {BGR_BUTTONFACE, BGR_BUTTONFACE, COLOR_BTNFACE}, // bright grey | |
771 | {BGR_BUTTONHILIGHT, BGR_BUTTONHILIGHT, COLOR_BTNHIGHLIGHT},// white | |
772 | {BGR_BACKGROUNDSEL, BGR_BACKGROUNDSEL, COLOR_HIGHLIGHT}, // blue | |
773 | {BGR_BACKGROUND, BGR_BACKGROUND, COLOR_WINDOW} // magenta | |
774 | }; | |
775 | ||
776 | #define NUM_MAPS (sizeof(ColorMap)/sizeof(COLORMAP2)) | |
777 | ||
778 | if (!lpBitmapInfo) | |
779 | return 0; | |
780 | ||
781 | // | |
782 | // So what are the new colors anyway ? | |
783 | // | |
784 | for (i=0; i < (int) NUM_MAPS; i++) { | |
785 | ColorMap[i].bgrto = (long unsigned int) FlipColor(GetSysColor((int)ColorMap[i].sysColor)); | |
786 | } | |
787 | ||
788 | p = (DWORD FAR *)(((LPSTR)lpBitmapInfo) + lpBitmapInfo->biSize); | |
789 | ||
790 | /* Replace button-face and button-shadow colors with the current values | |
791 | */ | |
792 | numcolors = 16; | |
793 | ||
794 | while (numcolors-- > 0) { | |
795 | for (i = 0; i < (int) NUM_MAPS; i++) { | |
796 | if (*p == ColorMap[i].bgrfrom) { | |
797 | *p = ColorMap[i].bgrto; | |
798 | break; | |
799 | } | |
800 | } | |
801 | p++; | |
802 | } | |
803 | ||
804 | /* First skip over the header structure */ | |
805 | lpBits = (LPSTR)(lpBitmapInfo + 1); | |
806 | ||
807 | /* Skip the color table entries, if any */ | |
808 | lpBits += (1 << (lpBitmapInfo->biBitCount)) * sizeof(RGBQUAD); | |
809 | ||
810 | /* Create a color bitmap compatible with the display device */ | |
811 | i = wid = (int)lpBitmapInfo->biWidth; | |
812 | hgt = (int)lpBitmapInfo->biHeight; | |
813 | hdc = ::GetDC(NULL); | |
814 | ||
815 | hdcMem = CreateCompatibleDC(hdc); | |
816 | if (hdcMem) { | |
817 | // hbm = CreateDiscardableBitmap(hdc, i, hgt); | |
818 | hbm = CreateCompatibleBitmap(hdc, i, hgt); | |
819 | if (hbm) { | |
820 | hbmOld = SelectObject(hdcMem, hbm); | |
821 | ||
822 | // set the main image | |
823 | StretchDIBits(hdcMem, 0, 0, wid, hgt, 0, 0, wid, hgt, lpBits, | |
824 | (LPBITMAPINFO)lpBitmapInfo, DIB_RGB_COLORS, SRCCOPY); | |
825 | ||
826 | SelectObject(hdcMem, hbmOld); | |
827 | } | |
828 | ||
829 | DeleteObject(hdcMem); | |
830 | } | |
831 | ||
832 | ReleaseDC(NULL, hdc); | |
833 | ||
834 | return (WXHBITMAP) hbm; | |
835 | } | |
836 | ||
837 | WXHBITMAP wxToolBarMSW::CreateMappedBitmap(WXHINSTANCE hInstance, WXHBITMAP hBitmap) | |
838 | { | |
839 | HANDLE hDIB = BitmapToDIB((HBITMAP) hBitmap, 0); | |
840 | if (hDIB) | |
841 | { | |
842 | #ifdef __WINDOWS_386__ | |
843 | LPBITMAPINFOHEADER lpbmInfoHdr = (LPBITMAPINFOHEADER)MK_FP32(GlobalLock(hDIB)); | |
844 | #else | |
845 | LPBITMAPINFOHEADER lpbmInfoHdr = (LPBITMAPINFOHEADER)GlobalLock(hDIB); | |
846 | #endif | |
847 | HBITMAP newBitmap = (HBITMAP) CreateMappedBitmap((WXHINSTANCE) wxGetInstance(), lpbmInfoHdr); | |
848 | GlobalUnlock(hDIB); | |
849 | GlobalFree(hDIB); | |
850 | return (WXHBITMAP) newBitmap; | |
851 | } | |
852 | return 0; | |
853 | } | |
854 | ||
855 | #endif |