]>
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 | ||
47d67540 | 27 | #if wxUSE_BUTTONBAR && wxUSE_TOOLBAR |
2bda0e17 KB |
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 | ||
81d66cf3 JS |
41 | #define DEFAULTBITMAPX 16 |
42 | #define DEFAULTBITMAPY 15 | |
43 | #define DEFAULTBUTTONX 24 | |
44 | #define DEFAULTBUTTONY 22 | |
45 | #define DEFAULTBARHEIGHT 27 | |
46 | ||
2bda0e17 KB |
47 | /////// Non-Windows 95 implementation |
48 | ||
f97c9854 | 49 | #if !wxUSE_IMAGE_LOADING_IN_MSW |
47d67540 | 50 | #error If wxUSE_IMAGE_LOADING_IN_MSW is set to 0, then wxUSE_BUTTONBAR must be set to 0 too. |
2bda0e17 KB |
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 | ||
debe6624 | 77 | bool wxToolBarMSW::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, |
81d66cf3 | 78 | long style, const wxString& name) |
2bda0e17 KB |
79 | { |
80 | if ( ! wxWindow::Create(parent, id, pos, size, style, name) ) | |
81 | return FALSE; | |
82 | ||
81d66cf3 | 83 | if ( style & wxTB_HORIZONTAL ) |
2bda0e17 KB |
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 | ||
81d66cf3 | 118 | void wxToolBarMSW::SetToolBitmapSize(const wxSize& size) |
2bda0e17 KB |
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 | |
81d66cf3 | 126 | wxSize wxToolBarMSW::GetToolSize(void) const |
2bda0e17 KB |
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. | |
debe6624 | 298 | void wxToolBarMSW::EnableTool(int toolIndex, bool enable) |
2bda0e17 KB |
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. | |
debe6624 JS |
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) | |
2bda0e17 KB |
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__) | |
1e6d9499 | 342 | wxBitmap bitmap2; |
2bda0e17 KB |
343 | if (toggle) |
344 | { | |
1e6d9499 | 345 | bitmap2.SetHBITMAP( (WXHBITMAP) CreateMappedBitmap(wxGetInstance(), (HBITMAP) ((wxBitmap& )bitmap).GetHBITMAP())); |
2bda0e17 KB |
346 | } |
347 | ||
1e6d9499 | 348 | wxToolBarTool *tool = new wxToolBarTool(index, bitmap, bitmap2, toggle, xPos, yPos, helpString1, helpString2); |
2bda0e17 | 349 | #else |
c0ed460c | 350 | wxToolBarTool *tool = new wxToolBarTool(index, bitmap, wxNullBitmap, toggle, xPos, yPos, helpString1, helpString2); |
2bda0e17 KB |
351 | #endif |
352 | ||
353 | tool->m_clientData = clientData; | |
354 | ||
355 | if (xPos > -1) | |
356 | tool->m_x = xPos; | |
357 | else | |
358 | tool->m_x = m_xMargin; | |
359 | ||
360 | if (yPos > -1) | |
361 | tool->m_y = yPos; | |
362 | else | |
363 | tool->m_y = m_yMargin; | |
364 | ||
365 | tool->m_deleteSecondBitmap = TRUE; | |
81d66cf3 | 366 | tool->SetSize(GetToolSize().x, GetToolSize().y); |
2bda0e17 KB |
367 | |
368 | // Calculate reasonable max size in case Layout() not called | |
369 | if ((tool->m_x + bitmap.GetWidth() + m_xMargin) > m_maxWidth) | |
370 | m_maxWidth = (tool->m_x + tool->GetWidth() + m_xMargin); | |
371 | ||
372 | if ((tool->m_y + bitmap.GetHeight() + m_yMargin) > m_maxHeight) | |
373 | m_maxHeight = (tool->m_y + tool->GetHeight() + m_yMargin); | |
374 | ||
375 | m_tools.Append((long)index, tool); | |
376 | return tool; | |
377 | } | |
378 | ||
81d66cf3 JS |
379 | void wxToolBarMSW::Layout(void) |
380 | { | |
381 | m_currentRowsOrColumns = 0; | |
382 | m_lastX = m_xMargin; | |
383 | m_lastY = m_yMargin; | |
384 | int maxToolWidth = 0; | |
385 | int maxToolHeight = 0; | |
386 | m_maxWidth = 0; | |
387 | m_maxHeight = 0; | |
388 | ||
389 | // Find the maximum tool width and height | |
390 | wxNode *node = m_tools.First(); | |
391 | while (node) | |
392 | { | |
393 | wxToolBarTool *tool = (wxToolBarTool *)node->Data(); | |
394 | if (tool->GetWidth() > maxToolWidth) | |
395 | maxToolWidth = (int)tool->GetWidth(); | |
396 | if (tool->GetHeight() > maxToolHeight) | |
397 | maxToolHeight = (int)tool->GetHeight(); | |
398 | node = node->Next(); | |
399 | } | |
400 | ||
401 | int separatorSize = m_toolSeparation; | |
402 | ||
403 | node = m_tools.First(); | |
404 | while (node) | |
405 | { | |
406 | wxToolBarTool *tool = (wxToolBarTool *)node->Data(); | |
407 | if (tool->m_toolStyle == wxTOOL_STYLE_SEPARATOR) | |
408 | { | |
409 | if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) | |
410 | { | |
411 | if (m_currentRowsOrColumns >= m_maxCols) | |
412 | m_lastY += separatorSize; | |
413 | else | |
414 | m_lastX += separatorSize; | |
415 | } | |
416 | else | |
417 | { | |
418 | if (m_currentRowsOrColumns >= m_maxRows) | |
419 | m_lastX += separatorSize; | |
420 | else | |
421 | m_lastY += separatorSize; | |
422 | } | |
423 | } | |
424 | else if (tool->m_toolStyle == wxTOOL_STYLE_BUTTON) | |
425 | { | |
426 | if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) | |
427 | { | |
428 | if (m_currentRowsOrColumns >= m_maxCols) | |
429 | { | |
430 | m_currentRowsOrColumns = 0; | |
431 | m_lastX = m_xMargin; | |
432 | m_lastY += maxToolHeight + m_toolPacking; | |
433 | } | |
434 | tool->m_x = (long) (m_lastX + (maxToolWidth - tool->GetWidth())/2.0); | |
435 | tool->m_y = (long) (m_lastY + (maxToolHeight - tool->GetHeight())/2.0); | |
436 | ||
437 | m_lastX += maxToolWidth + m_toolPacking; | |
438 | } | |
439 | else | |
440 | { | |
441 | if (m_currentRowsOrColumns >= m_maxRows) | |
442 | { | |
443 | m_currentRowsOrColumns = 0; | |
444 | m_lastX += (maxToolWidth + m_toolPacking); | |
445 | m_lastY = m_yMargin; | |
446 | } | |
447 | tool->m_x = (long) (m_lastX + (maxToolWidth - tool->GetWidth())/2.0); | |
448 | tool->m_y = (long) (m_lastY + (maxToolHeight - tool->GetHeight())/2.0); | |
449 | ||
450 | m_lastY += maxToolHeight + m_toolPacking; | |
451 | } | |
452 | m_currentRowsOrColumns ++; | |
453 | } | |
454 | ||
455 | if (m_lastX > m_maxWidth) | |
456 | m_maxWidth = m_lastX; | |
457 | if (m_lastY > m_maxHeight) | |
458 | m_maxHeight = m_lastY; | |
459 | ||
460 | node = node->Next(); | |
461 | } | |
462 | if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) | |
463 | m_maxWidth += maxToolWidth; | |
464 | else | |
465 | m_maxHeight += maxToolHeight; | |
466 | ||
467 | m_maxWidth += m_xMargin; | |
468 | m_maxHeight += m_yMargin; | |
469 | } | |
470 | ||
471 | ||
2bda0e17 KB |
472 | bool wxToolBarMSW::InitGlobalObjects(void) |
473 | { | |
474 | GetSysColors(); | |
475 | if (!CreateDitherBrush()) | |
476 | return FALSE; | |
477 | ||
478 | m_hdcMono = (WXHDC) CreateCompatibleDC(NULL); | |
479 | if (!m_hdcMono) | |
480 | return FALSE; | |
481 | ||
81d66cf3 | 482 | m_hbmMono = (WXHBITMAP) CreateBitmap((int)GetToolSize().x, (int)GetToolSize().y, 1, 1, NULL); |
2bda0e17 KB |
483 | if (!m_hbmMono) |
484 | return FALSE; | |
485 | ||
486 | m_hbmDefault = (WXHBITMAP) SelectObject((HDC) m_hdcMono, (HBITMAP) m_hbmMono); | |
487 | return TRUE; | |
488 | } | |
489 | ||
490 | void wxToolBarMSW::FreeGlobalObjects(void) | |
491 | { | |
492 | FreeDitherBrush(); | |
493 | ||
494 | if (m_hdcMono) { | |
495 | if (m_hbmDefault) | |
496 | { | |
497 | SelectObject((HDC) m_hdcMono, (HBITMAP) m_hbmDefault); | |
498 | m_hbmDefault = 0; | |
499 | } | |
500 | DeleteDC((HDC) m_hdcMono); // toast the DCs | |
501 | } | |
502 | m_hdcMono = 0; | |
503 | ||
504 | if (m_hbmMono) | |
505 | DeleteObject((HBITMAP) m_hbmMono); | |
506 | m_hbmMono = 0; | |
507 | } | |
508 | ||
509 | ||
510 | void wxToolBarMSW::PatB(WXHDC hdc,int x,int y,int dx,int dy, long rgb) | |
511 | { | |
512 | RECT rc; | |
513 | ||
514 | rc.left = x; | |
515 | rc.top = y; | |
516 | rc.right = x + dx; | |
517 | rc.bottom = y + dy; | |
518 | ||
519 | SetBkColor((HDC) hdc,rgb); | |
520 | ExtTextOut((HDC) hdc,0,0,ETO_OPAQUE,&rc,NULL,0,NULL); | |
521 | } | |
522 | ||
523 | ||
524 | // create a mono bitmap mask: | |
525 | // 1's where color == COLOR_BTNFACE || COLOR_HILIGHT | |
526 | // 0's everywhere else | |
527 | ||
528 | void wxToolBarMSW::CreateMask(WXHDC hdc, int xoffset, int yoffset, int dx, int dy) | |
529 | { | |
530 | HDC globalDC = ::GetDC(NULL); | |
531 | HDC hdcGlyphs = CreateCompatibleDC((HDC) globalDC); | |
532 | ReleaseDC(NULL, (HDC) globalDC); | |
533 | ||
534 | // krj - create a new bitmap and copy the image from hdc. | |
535 | //HBITMAP bitmapOld = SelectObject(hdcGlyphs, hBitmap); | |
536 | HBITMAP hBitmap = CreateCompatibleBitmap((HDC) hdc, dx, dy); | |
c4e7c2aa | 537 | HBITMAP bitmapOld = (HBITMAP) SelectObject(hdcGlyphs, hBitmap); |
2bda0e17 KB |
538 | BitBlt(hdcGlyphs, 0,0, dx, dy, (HDC) hdc, 0, 0, SRCCOPY); |
539 | ||
540 | // initalize whole area with 1's | |
541 | PatBlt((HDC) m_hdcMono, 0, 0, dx, dy, WHITENESS); | |
542 | ||
543 | // create mask based on color bitmap | |
544 | // convert this to 1's | |
545 | SetBkColor(hdcGlyphs, m_rgbFace); | |
81d66cf3 | 546 | BitBlt((HDC) m_hdcMono, xoffset, yoffset, (int)GetToolBitmapSize().x, (int)GetToolBitmapSize().y, |
2bda0e17 KB |
547 | hdcGlyphs, 0, 0, SRCCOPY); |
548 | // convert this to 1's | |
549 | SetBkColor(hdcGlyphs, m_rgbHilight); | |
550 | // OR in the new 1's | |
81d66cf3 | 551 | BitBlt((HDC) m_hdcMono, xoffset, yoffset, (int)GetToolBitmapSize().x, (int)GetToolBitmapSize().y, |
2bda0e17 KB |
552 | hdcGlyphs, 0, 0, SRCPAINT); |
553 | ||
554 | SelectObject(hdcGlyphs, bitmapOld); | |
555 | DeleteObject(hBitmap); | |
556 | DeleteDC(hdcGlyphs); | |
557 | } | |
558 | ||
559 | void wxToolBarMSW::DrawBlankButton(WXHDC hdc, int x, int y, int dx, int dy, int state) | |
560 | { | |
561 | // face color | |
562 | PatB(hdc, x, y, dx, dy, m_rgbFace); | |
563 | ||
564 | if (state & wxTBSTATE_PRESSED) { | |
565 | PatB(hdc, x + 1, y, dx - 2, 1, m_rgbFrame); | |
566 | PatB(hdc, x + 1, y + dy - 1, dx - 2, 1, m_rgbFrame); | |
567 | PatB(hdc, x, y + 1, 1, dy - 2, m_rgbFrame); | |
568 | PatB(hdc, x + dx - 1, y +1, 1, dy - 2, m_rgbFrame); | |
569 | PatB(hdc, x + 1, y + 1, 1, dy-2, m_rgbShadow); | |
570 | PatB(hdc, x + 1, y + 1, dx-2, 1, m_rgbShadow); | |
571 | } | |
572 | else { | |
573 | PatB(hdc, x + 1, y, dx - 2, 1, m_rgbFrame); | |
574 | PatB(hdc, x + 1, y + dy - 1, dx - 2, 1, m_rgbFrame); | |
575 | PatB(hdc, x, y + 1, 1, dy - 2, m_rgbFrame); | |
576 | PatB(hdc, x + dx - 1, y + 1, 1, dy - 2, m_rgbFrame); | |
577 | dx -= 2; | |
578 | dy -= 2; | |
579 | PatB(hdc, x + 1, y + 1, 1, dy - 1, m_rgbHilight); | |
580 | PatB(hdc, x + 1, y + 1, dx - 1, 1, m_rgbHilight); | |
581 | PatB(hdc, x + dx, y + 1, 1, dy, m_rgbShadow); | |
582 | PatB(hdc, x + 1, y + dy, dx, 1, m_rgbShadow); | |
583 | PatB(hdc, x + dx - 1, y + 2, 1, dy - 2, m_rgbShadow); | |
584 | PatB(hdc, x + 2, y + dy - 1, dx - 2, 1, m_rgbShadow); | |
585 | } | |
586 | } | |
587 | ||
588 | void wxToolBarMSW::DrawButton(WXHDC hdc, int x, int y, int dx, int dy, wxToolBarTool *tool, int state) | |
589 | { | |
590 | int yOffset; | |
591 | HBRUSH hbrOld, hbr; | |
592 | BOOL bMaskCreated = FALSE; | |
593 | int xButton = 0; // assume button is down | |
594 | int dxFace, dyFace; | |
595 | int xCenterOffset; | |
596 | ||
597 | dxFace = dx; | |
598 | dyFace = dy; | |
599 | ||
600 | // HBITMAP hBitmap = (HBITMAP) tool->m_bitmap1.GetHBITMAP(); | |
601 | HDC globalDC = ::GetDC(NULL); | |
602 | HDC hdcGlyphs = CreateCompatibleDC(globalDC); | |
603 | ReleaseDC(NULL, globalDC); | |
604 | ||
605 | // get the proper button look - up or down. | |
606 | if (!(state & (wxTBSTATE_PRESSED | wxTBSTATE_CHECKED))) { | |
607 | xButton = dx; // use 'up' version of button | |
608 | dxFace -= 2; | |
609 | dyFace -= 2; // extents to ignore button highlight | |
610 | } | |
611 | ||
612 | DrawBlankButton(hdc, x, y, dx, dy, state); | |
613 | ||
614 | ||
615 | // move coordinates inside border and away from upper left highlight. | |
616 | // the extents change accordingly. | |
617 | x += 2; | |
618 | y += 2; | |
619 | dxFace -= 3; | |
620 | dyFace -= 3; | |
621 | ||
622 | // Using bitmap2 can cause problems (don't know why!) | |
623 | #if !defined(__WIN32__) && !defined(__WIN386__) | |
624 | HBITMAP bitmapOld; | |
625 | if (tool->m_bitmap2.Ok()) | |
c4e7c2aa | 626 | bitmapOld = (HBITMAP) SelectObject(hdcGlyphs, (HBITMAP) tool->m_bitmap2.GetHBITMAP()); |
2bda0e17 | 627 | else |
c4e7c2aa | 628 | bitmapOld = (HBITMAP) SelectObject(hdcGlyphs, (HBITMAP) tool->m_bitmap1.GetHBITMAP()); |
2bda0e17 | 629 | #else |
c4e7c2aa | 630 | HBITMAP bitmapOld = (HBITMAP) SelectObject(hdcGlyphs, (HBITMAP) tool->m_bitmap1.GetHBITMAP()); |
2bda0e17 KB |
631 | #endif |
632 | ||
633 | // calculate offset of face from (x,y). y is always from the top, | |
634 | // so the offset is easy. x needs to be centered in face. | |
635 | yOffset = 1; | |
81d66cf3 | 636 | xCenterOffset = (dxFace - (int)GetToolBitmapSize().x)/2; |
2bda0e17 KB |
637 | if (state & (wxTBSTATE_PRESSED | wxTBSTATE_CHECKED)) |
638 | { | |
639 | // pressed state moves down and to the right | |
640 | // (x moves automatically as face size grows) | |
641 | yOffset++; | |
642 | } | |
643 | ||
644 | // now put on the face | |
645 | if (state & wxTBSTATE_ENABLED) { | |
646 | // regular version | |
81d66cf3 | 647 | BitBlt((HDC) hdc, x+xCenterOffset, y + yOffset, (int)GetToolBitmapSize().x, (int)GetToolBitmapSize().y, |
2bda0e17 KB |
648 | hdcGlyphs, 0, 0, SRCCOPY); |
649 | } else { | |
650 | // disabled version (or indeterminate) | |
651 | bMaskCreated = TRUE; | |
652 | CreateMask((WXHDC) hdcGlyphs, xCenterOffset, yOffset, dxFace, dyFace); | |
653 | // CreateMask(hBitmap, xCenterOffset, yOffset, dxFace, dyFace); | |
654 | ||
655 | SetTextColor((HDC) hdc, 0L); // 0's in mono -> 0 (for ROP) | |
656 | SetBkColor((HDC) hdc, 0x00FFFFFF); // 1's in mono -> 1 | |
657 | ||
658 | // draw glyph's white understrike | |
659 | if (!(state & wxTBSTATE_INDETERMINATE)) { | |
660 | hbr = CreateSolidBrush(m_rgbHilight); | |
661 | if (hbr) { | |
c4e7c2aa | 662 | hbrOld = (HBRUSH) SelectObject((HDC) hdc, hbr); |
2bda0e17 KB |
663 | if (hbrOld) { |
664 | // draw hilight color where we have 0's in the mask | |
665 | BitBlt((HDC) hdc, x + 1, y + 1, dxFace, dyFace, (HDC) m_hdcMono, 0, 0, 0x00B8074A); | |
666 | SelectObject((HDC) hdc, hbrOld); | |
667 | } | |
668 | DeleteObject(hbr); | |
669 | } | |
670 | } | |
671 | ||
672 | // gray out glyph | |
673 | hbr = CreateSolidBrush(m_rgbShadow); | |
674 | if (hbr) { | |
c4e7c2aa | 675 | hbrOld = (HBRUSH) SelectObject((HDC) hdc, hbr); |
2bda0e17 KB |
676 | if (hbrOld) { |
677 | // draw the shadow color where we have 0's in the mask | |
678 | BitBlt((HDC) hdc, x, y, dxFace, dyFace, (HDC) m_hdcMono, 0, 0, 0x00B8074A); | |
679 | SelectObject((HDC) hdc, hbrOld); | |
680 | } | |
681 | DeleteObject(hbr); | |
682 | } | |
683 | ||
684 | if (state & wxTBSTATE_CHECKED) { | |
685 | BitBlt((HDC) m_hdcMono, 1, 1, dxFace - 1, dyFace - 1, (HDC) m_hdcMono, 0, 0, SRCAND); | |
686 | } | |
687 | } | |
688 | ||
689 | if (state & (wxTBSTATE_CHECKED | wxTBSTATE_INDETERMINATE)) { | |
690 | ||
c4e7c2aa | 691 | hbrOld = (HBRUSH) SelectObject((HDC) hdc, (HBRUSH) m_hbrDither); |
2bda0e17 KB |
692 | if (hbrOld) { |
693 | ||
694 | if (!bMaskCreated) | |
695 | CreateMask((WXHDC) hdcGlyphs, xCenterOffset, yOffset, dxFace, dyFace); | |
696 | // CreateMask(hBitmap, xCenterOffset, yOffset, dxFace, dyFace); | |
697 | ||
698 | SetTextColor((HDC) hdc, 0L); // 0 -> 0 | |
699 | SetBkColor((HDC) hdc, 0x00FFFFFF); // 1 -> 1 | |
700 | ||
701 | // only draw the dither brush where the mask is 1's | |
702 | BitBlt((HDC) hdc, x, y, dxFace, dyFace, (HDC) m_hdcMono, 0, 0, 0x00E20746); | |
703 | ||
704 | SelectObject((HDC) hdc, hbrOld); | |
705 | } | |
706 | } | |
707 | SelectObject(hdcGlyphs, bitmapOld); | |
708 | DeleteDC(hdcGlyphs); | |
709 | } | |
710 | ||
711 | void wxToolBarMSW::GetSysColors(void) | |
712 | { | |
713 | static COLORREF rgbSaveFace = 0xffffffffL, | |
714 | rgbSaveShadow = 0xffffffffL, | |
715 | rgbSaveHilight = 0xffffffffL, | |
716 | rgbSaveFrame = 0xffffffffL; | |
717 | ||
718 | // For now, override these because the colour replacement isn't working, | |
719 | // and we get inconsistent colours. Assume all buttons are grey for the moment. | |
720 | ||
721 | // m_rgbFace = GetSysColor(COLOR_BTNFACE); | |
722 | m_rgbFace = RGB(192,192,192); | |
723 | // m_rgbShadow = GetSysColor(COLOR_BTNSHADOW); | |
724 | m_rgbShadow = RGB(128,128,128); | |
725 | // m_rgbHilight = GetSysColor(COLOR_BTNHIGHLIGHT); | |
726 | m_rgbHilight = RGB(255, 255, 255); | |
727 | ||
728 | m_rgbFrame = GetSysColor(COLOR_WINDOWFRAME); | |
729 | ||
730 | if (rgbSaveFace!=m_rgbFace || rgbSaveShadow!=m_rgbShadow | |
731 | || rgbSaveHilight!=m_rgbHilight || rgbSaveFrame!=m_rgbFrame) | |
732 | { | |
733 | rgbSaveFace = m_rgbFace; | |
734 | rgbSaveShadow = m_rgbShadow; | |
735 | rgbSaveHilight = m_rgbHilight; | |
736 | rgbSaveFrame = m_rgbFrame; | |
737 | ||
738 | // Update the brush for pushed-in buttons | |
739 | CreateDitherBrush(); | |
740 | } | |
741 | } | |
742 | ||
743 | WXHBITMAP wxToolBarMSW::CreateDitherBitmap() | |
744 | { | |
745 | BITMAPINFO* pbmi; | |
746 | HBITMAP hbm; | |
747 | HDC hdc; | |
748 | int i; | |
749 | long patGray[8]; | |
750 | DWORD rgb; | |
751 | ||
752 | pbmi = (BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER) + 16*sizeof(RGBQUAD)); | |
753 | memset(pbmi, 0, (sizeof(BITMAPINFOHEADER) + 16*sizeof(RGBQUAD))); | |
754 | ||
755 | pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); | |
756 | pbmi->bmiHeader.biWidth = 8; | |
757 | pbmi->bmiHeader.biHeight = 8; | |
758 | pbmi->bmiHeader.biPlanes = 1; | |
759 | pbmi->bmiHeader.biBitCount = 1; | |
760 | pbmi->bmiHeader.biCompression = BI_RGB; | |
761 | ||
762 | // rgb = GetSysColor(COLOR_BTNFACE); | |
763 | rgb = RGB(192,192,192); | |
764 | ||
765 | pbmi->bmiColors[0].rgbBlue = GetBValue(rgb); | |
766 | pbmi->bmiColors[0].rgbGreen = GetGValue(rgb); | |
767 | pbmi->bmiColors[0].rgbRed = GetRValue(rgb); | |
768 | pbmi->bmiColors[0].rgbReserved = 0; | |
769 | ||
770 | // rgb = GetSysColor(COLOR_BTNHIGHLIGHT); | |
771 | rgb = RGB(255, 255, 255); | |
772 | ||
773 | pbmi->bmiColors[1].rgbBlue = GetBValue(rgb); | |
774 | pbmi->bmiColors[1].rgbGreen = GetGValue(rgb); | |
775 | pbmi->bmiColors[1].rgbRed = GetRValue(rgb); | |
776 | pbmi->bmiColors[1].rgbReserved = 0; | |
777 | ||
778 | /* initialize the brushes */ | |
779 | ||
780 | for (i = 0; i < 8; i++) | |
781 | if (i & 1) | |
782 | patGray[i] = 0xAAAA5555L; // 0x11114444L; // lighter gray | |
783 | else | |
784 | patGray[i] = 0x5555AAAAL; // 0x11114444L; // lighter gray | |
785 | ||
786 | hdc = ::GetDC(NULL); | |
787 | ||
788 | hbm = CreateDIBitmap(hdc, &pbmi->bmiHeader, CBM_INIT, patGray, pbmi, DIB_RGB_COLORS); | |
789 | ||
790 | ReleaseDC(NULL, hdc); | |
791 | free(pbmi); | |
792 | ||
793 | return (WXHBITMAP)hbm; | |
794 | } | |
795 | ||
796 | bool wxToolBarMSW::CreateDitherBrush(void) | |
797 | { | |
798 | HBITMAP hbmGray; | |
799 | HBRUSH hbrSave; | |
800 | if (m_hbrDither) | |
801 | return TRUE; | |
802 | hbmGray = (HBITMAP) CreateDitherBitmap(); | |
803 | ||
804 | if (hbmGray) | |
805 | { | |
806 | hbrSave = (HBRUSH) m_hbrDither; | |
807 | m_hbrDither = (WXHBRUSH) CreatePatternBrush(hbmGray); | |
808 | DeleteObject(hbmGray); | |
809 | if (m_hbrDither) | |
810 | { | |
811 | if (hbrSave) | |
812 | { | |
813 | DeleteObject(hbrSave); | |
814 | } | |
815 | return TRUE; | |
816 | } | |
817 | else | |
818 | { | |
819 | m_hbrDither = (WXHBRUSH) hbrSave; | |
820 | } | |
821 | } | |
822 | ||
823 | return FALSE; | |
824 | } | |
825 | ||
826 | bool wxToolBarMSW::FreeDitherBrush(void) | |
827 | { | |
828 | if (m_hbrDither) | |
829 | DeleteObject((HBRUSH) m_hbrDither); | |
830 | m_hbrDither = 0; | |
831 | return TRUE; | |
832 | } | |
833 | ||
834 | typedef struct tagCOLORMAP2 | |
835 | { | |
836 | COLORREF bgrfrom; | |
837 | COLORREF bgrto; | |
838 | COLORREF sysColor; | |
839 | } COLORMAP2; | |
840 | ||
841 | // these are the default colors used to map the dib colors | |
842 | // to the current system colors | |
843 | ||
844 | #define BGR_BUTTONTEXT (RGB(000,000,000)) // black | |
845 | #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey | |
846 | #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey | |
847 | #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white | |
848 | #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue | |
849 | #define BGR_BACKGROUND (RGB(255,000,255)) // magenta | |
850 | #define FlipColor(rgb) (RGB(GetBValue(rgb), GetGValue(rgb), GetRValue(rgb))) | |
851 | ||
852 | WXHBITMAP wxToolBarMSW::CreateMappedBitmap(WXHINSTANCE WXUNUSED(hInstance), void *info) | |
853 | { | |
854 | LPBITMAPINFOHEADER lpBitmapInfo = (LPBITMAPINFOHEADER)info; | |
855 | HDC hdc, hdcMem = NULL; | |
856 | ||
857 | DWORD FAR *p; | |
858 | LPSTR lpBits; | |
859 | HBITMAP hbm = NULL, hbmOld; | |
860 | int numcolors, i; | |
861 | int wid, hgt; | |
862 | static COLORMAP2 ColorMap[] = { | |
863 | {BGR_BUTTONTEXT, BGR_BUTTONTEXT, COLOR_BTNTEXT}, // black | |
864 | {BGR_BUTTONSHADOW, BGR_BUTTONSHADOW, COLOR_BTNSHADOW}, // dark grey | |
865 | {BGR_BUTTONFACE, BGR_BUTTONFACE, COLOR_BTNFACE}, // bright grey | |
866 | {BGR_BUTTONHILIGHT, BGR_BUTTONHILIGHT, COLOR_BTNHIGHLIGHT},// white | |
867 | {BGR_BACKGROUNDSEL, BGR_BACKGROUNDSEL, COLOR_HIGHLIGHT}, // blue | |
868 | {BGR_BACKGROUND, BGR_BACKGROUND, COLOR_WINDOW} // magenta | |
869 | }; | |
870 | ||
871 | #define NUM_MAPS (sizeof(ColorMap)/sizeof(COLORMAP2)) | |
872 | ||
873 | if (!lpBitmapInfo) | |
874 | return 0; | |
875 | ||
876 | // | |
877 | // So what are the new colors anyway ? | |
878 | // | |
879 | for (i=0; i < (int) NUM_MAPS; i++) { | |
880 | ColorMap[i].bgrto = (long unsigned int) FlipColor(GetSysColor((int)ColorMap[i].sysColor)); | |
881 | } | |
882 | ||
883 | p = (DWORD FAR *)(((LPSTR)lpBitmapInfo) + lpBitmapInfo->biSize); | |
884 | ||
885 | /* Replace button-face and button-shadow colors with the current values | |
886 | */ | |
887 | numcolors = 16; | |
888 | ||
889 | while (numcolors-- > 0) { | |
890 | for (i = 0; i < (int) NUM_MAPS; i++) { | |
891 | if (*p == ColorMap[i].bgrfrom) { | |
892 | *p = ColorMap[i].bgrto; | |
893 | break; | |
894 | } | |
895 | } | |
896 | p++; | |
897 | } | |
898 | ||
899 | /* First skip over the header structure */ | |
900 | lpBits = (LPSTR)(lpBitmapInfo + 1); | |
901 | ||
902 | /* Skip the color table entries, if any */ | |
903 | lpBits += (1 << (lpBitmapInfo->biBitCount)) * sizeof(RGBQUAD); | |
904 | ||
905 | /* Create a color bitmap compatible with the display device */ | |
906 | i = wid = (int)lpBitmapInfo->biWidth; | |
907 | hgt = (int)lpBitmapInfo->biHeight; | |
908 | hdc = ::GetDC(NULL); | |
909 | ||
910 | hdcMem = CreateCompatibleDC(hdc); | |
911 | if (hdcMem) { | |
912 | // hbm = CreateDiscardableBitmap(hdc, i, hgt); | |
913 | hbm = CreateCompatibleBitmap(hdc, i, hgt); | |
914 | if (hbm) { | |
c4e7c2aa | 915 | hbmOld = (HBITMAP) SelectObject(hdcMem, hbm); |
2bda0e17 KB |
916 | |
917 | // set the main image | |
918 | StretchDIBits(hdcMem, 0, 0, wid, hgt, 0, 0, wid, hgt, lpBits, | |
919 | (LPBITMAPINFO)lpBitmapInfo, DIB_RGB_COLORS, SRCCOPY); | |
920 | ||
921 | SelectObject(hdcMem, hbmOld); | |
922 | } | |
923 | ||
924 | DeleteObject(hdcMem); | |
925 | } | |
926 | ||
927 | ReleaseDC(NULL, hdc); | |
928 | ||
929 | return (WXHBITMAP) hbm; | |
930 | } | |
931 | ||
932 | WXHBITMAP wxToolBarMSW::CreateMappedBitmap(WXHINSTANCE hInstance, WXHBITMAP hBitmap) | |
933 | { | |
934 | HANDLE hDIB = BitmapToDIB((HBITMAP) hBitmap, 0); | |
935 | if (hDIB) | |
936 | { | |
937 | #ifdef __WINDOWS_386__ | |
938 | LPBITMAPINFOHEADER lpbmInfoHdr = (LPBITMAPINFOHEADER)MK_FP32(GlobalLock(hDIB)); | |
939 | #else | |
940 | LPBITMAPINFOHEADER lpbmInfoHdr = (LPBITMAPINFOHEADER)GlobalLock(hDIB); | |
941 | #endif | |
942 | HBITMAP newBitmap = (HBITMAP) CreateMappedBitmap((WXHINSTANCE) wxGetInstance(), lpbmInfoHdr); | |
943 | GlobalUnlock(hDIB); | |
944 | GlobalFree(hDIB); | |
945 | return (WXHBITMAP) newBitmap; | |
946 | } | |
947 | return 0; | |
948 | } | |
949 | ||
950 | #endif |