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