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