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