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