]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tabctrl.cpp
Avoid core dumps when SetImageList is used.
[wxWidgets.git] / src / msw / tabctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tabctrl.cpp
3 // Purpose: wxTabCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "tabctrl.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 defined(__WIN95__)
28
29 #include "wx/msw/private.h"
30
31 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
32 #include <commctrl.h>
33 #else
34 #include "wx/msw/gnuwin32/extra.h"
35 #endif
36
37 #include "wx/tabctrl.h"
38 #include "wx/app.h"
39 #include "wx/msw/imaglist.h"
40
41 IMPLEMENT_DYNAMIC_CLASS(wxTabCtrl, wxControl)
42 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent, wxNotifyEvent)
43
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGED)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGING)
46
47 BEGIN_EVENT_TABLE(wxTabCtrl, wxControl)
48 EVT_SYS_COLOUR_CHANGED(wxTabCtrl::OnSysColourChanged)
49 END_EVENT_TABLE()
50
51 wxTabCtrl::wxTabCtrl()
52 {
53 m_imageList = NULL;
54 }
55
56 bool wxTabCtrl::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
57 long style, const wxString& name)
58 {
59 m_imageList = NULL;
60
61 m_backgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
62 GetGValue(GetSysColor(COLOR_BTNFACE)), GetBValue(GetSysColor(COLOR_BTNFACE)));
63 m_foregroundColour = *wxBLACK ;
64
65 SetName(name);
66
67 int x = pos.x;
68 int y = pos.y;
69 int width = size.x;
70 int height = size.y;
71
72 m_windowStyle = style;
73
74 SetParent(parent);
75
76 if (width <= 0)
77 width = 100;
78 if (height <= 0)
79 height = 30;
80 if (x < 0)
81 x = 0;
82 if (y < 0)
83 y = 0;
84
85 m_windowId = (id < 0 ? NewControlId() : id);
86
87 long tabStyle = WS_CHILD | WS_VISIBLE;
88 if (m_windowStyle & wxTC_MULTILINE)
89 tabStyle |= TCS_MULTILINE;
90 if (m_windowStyle & wxTC_RIGHTJUSTIFY)
91 tabStyle |= TCS_RIGHTJUSTIFY;
92 if (m_windowStyle & wxTC_FIXEDWIDTH)
93 tabStyle |= TCS_FIXEDWIDTH;
94 if (m_windowStyle & wxTC_OWNERDRAW)
95 tabStyle |= TCS_OWNERDRAWFIXED;
96 if (m_windowStyle & wxBORDER)
97 tabStyle |= WS_BORDER;
98
99 #ifndef __WXWINCE__
100 tabStyle |= TCS_TOOLTIPS;
101 #endif
102
103 // Create the toolbar control.
104 HWND hWndTabCtrl = CreateWindowEx(0L, // No extended styles.
105 WC_TABCONTROL, // Class name for the tab control
106 wxEmptyString, // No default text.
107 tabStyle, // Styles and defaults.
108 x, y, width, height, // Standard size and position.
109 (HWND) parent->GetHWND(), // Parent window
110 (HMENU)m_windowId, // ID.
111 wxGetInstance(), // Current instance.
112 NULL ); // No class data.
113
114 m_hWnd = (WXHWND) hWndTabCtrl;
115 if (parent) parent->AddChild(this);
116
117 SubclassWin((WXHWND) hWndTabCtrl);
118
119 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
120
121 return TRUE;
122 }
123
124 wxTabCtrl::~wxTabCtrl()
125 {
126 UnsubclassWin();
127 }
128
129 bool wxTabCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
130 {
131 wxTabEvent event(wxEVT_NULL, m_windowId);
132 wxEventType eventType = wxEVT_NULL;
133 NMHDR* hdr1 = (NMHDR*) lParam;
134 switch ( hdr1->code )
135 {
136 case TCN_SELCHANGE:
137 eventType = wxEVT_COMMAND_TAB_SEL_CHANGED;
138 break;
139
140 case TCN_SELCHANGING:
141 eventType = wxEVT_COMMAND_TAB_SEL_CHANGING;
142 break;
143
144 #ifndef __WXWINCE__
145 case TTN_NEEDTEXT:
146 {
147 // TODO
148 // if (tool->m_shortHelpString != "")
149 // ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
150 }
151 #endif
152 default :
153 return wxControl::MSWOnNotify(idCtrl, lParam, result);
154 }
155
156 event.SetEventObject( this );
157 event.SetEventType(eventType);
158 event.SetInt(idCtrl) ;
159 event.SetSelection(idCtrl);
160
161 return ProcessEvent(event);
162 }
163
164 // Responds to colour changes, and passes event on to children.
165 void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent& event)
166 {
167 m_backgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
168 GetGValue(GetSysColor(COLOR_BTNFACE)),
169 GetBValue(GetSysColor(COLOR_BTNFACE)));
170
171 Refresh();
172
173 // Propagate the event to the non-top-level children
174 wxWindow::OnSysColourChanged(event);
175 }
176
177 // Delete all items
178 bool wxTabCtrl::DeleteAllItems()
179 {
180 return ( TabCtrl_DeleteAllItems( (HWND) GetHWND() ) != FALSE );
181 }
182
183 // Delete an item
184 bool wxTabCtrl::DeleteItem(int item)
185 {
186 return ( TabCtrl_DeleteItem( (HWND) GetHWND(), item) != FALSE );
187 }
188
189 // Get the selection
190 int wxTabCtrl::GetSelection() const
191 {
192 return (int) TabCtrl_GetCurSel( (HWND) GetHWND() );
193 }
194
195 // Get the tab with the current keyboard focus
196 int wxTabCtrl::GetCurFocus() const
197 {
198 return (int) TabCtrl_GetCurFocus( (HWND) GetHWND() );
199 }
200
201 // Get the associated image list
202 wxImageList* wxTabCtrl::GetImageList() const
203 {
204 return m_imageList;
205 }
206
207 // Get the number of items
208 int wxTabCtrl::GetItemCount() const
209 {
210 return (int) TabCtrl_GetItemCount( (HWND) GetHWND() );
211 }
212
213 // Get the rect corresponding to the tab
214 bool wxTabCtrl::GetItemRect(int item, wxRect& wxrect) const
215 {
216 RECT rect;
217 if ( !TabCtrl_GetItemRect( (HWND) GetHWND(), item, & rect) )
218 return FALSE;
219 else
220 {
221 wxrect.x = rect.left; wxrect.y = rect.top;
222 wxrect.width = rect.right - rect.left;
223 wxrect.height = rect.bottom - rect.top;
224 return TRUE;
225 }
226 }
227
228 // Get the number of rows
229 int wxTabCtrl::GetRowCount() const
230 {
231 return (int) TabCtrl_GetRowCount( (HWND) GetHWND() );
232 }
233
234 // Get the item text
235 wxString wxTabCtrl::GetItemText(int item) const
236 {
237 wxChar buf[256];
238 wxString str(wxEmptyString);
239 TC_ITEM tcItem;
240 tcItem.mask = TCIF_TEXT;
241 tcItem.pszText = buf;
242 tcItem.cchTextMax = 256;
243
244 if (TabCtrl_GetItem( (HWND) GetHWND(), item, & tcItem) )
245 str = tcItem.pszText;
246
247 return str;
248 }
249
250 // Get the item image
251 int wxTabCtrl::GetItemImage(int item) const
252 {
253 TC_ITEM tcItem;
254 tcItem.mask = TCIF_IMAGE;
255
256 if (TabCtrl_GetItem( (HWND) GetHWND(), item, & tcItem) )
257 return tcItem.iImage;
258 else
259 return -1;
260 }
261
262 // Get the item data
263 void* wxTabCtrl::GetItemData(int item) const
264 {
265 TC_ITEM tcItem;
266 tcItem.mask = TCIF_PARAM;
267
268 if (TabCtrl_GetItem( (HWND) GetHWND(), item, & tcItem) )
269 return (void*) tcItem.lParam;
270 else
271 return 0;
272 }
273
274 // Hit test
275 int wxTabCtrl::HitTest(const wxPoint& pt, long& flags)
276 {
277 TC_HITTESTINFO hitTestInfo;
278 hitTestInfo.pt.x = pt.x;
279 hitTestInfo.pt.y = pt.y;
280 int item = TabCtrl_HitTest( (HWND) GetHWND(), & hitTestInfo ) ;
281 flags = 0;
282
283 if ((hitTestInfo.flags & TCHT_NOWHERE) == TCHT_NOWHERE)
284 flags |= wxTAB_HITTEST_NOWHERE;
285 if ((hitTestInfo.flags & TCHT_ONITEMICON) == TCHT_ONITEMICON)
286 flags |= wxTAB_HITTEST_ONICON;
287 if ((hitTestInfo.flags & TCHT_ONITEMLABEL) == TCHT_ONITEMLABEL)
288 flags |= wxTAB_HITTEST_ONLABEL;
289
290 return item;
291 }
292
293 // Insert an item
294 bool wxTabCtrl::InsertItem(int item, const wxString& text, int imageId, void* data)
295 {
296 wxChar buf[256];
297 TC_ITEM tcItem;
298 tcItem.mask = TCIF_PARAM;
299 tcItem.lParam = (long) data;
300 if (text != wxEmptyString)
301 {
302 tcItem.mask |= TCIF_TEXT;
303 wxStrcpy(buf, (const wxChar*) text);
304 tcItem.pszText = buf;
305 tcItem.cchTextMax = 256;
306 }
307 if (imageId != -1)
308 {
309 tcItem.mask |= TCIF_IMAGE;
310 tcItem.iImage = imageId;
311 }
312
313 return (TabCtrl_InsertItem( (HWND) GetHWND(), item, & tcItem) != -1);
314 }
315
316 // Set the selection
317 int wxTabCtrl::SetSelection(int item)
318 {
319 return (int) TabCtrl_SetCurSel( (HWND) GetHWND(), item );
320 }
321
322 // Set the image list
323 void wxTabCtrl::SetImageList(wxImageList* imageList)
324 {
325 m_imageList = imageList;
326 TabCtrl_SetImageList( (HWND) GetHWND(), (HIMAGELIST) imageList->GetHIMAGELIST() );
327 }
328
329 // Set the text for an item
330 bool wxTabCtrl::SetItemText(int item, const wxString& text)
331 {
332 wxChar buf[256];
333 TC_ITEM tcItem;
334 tcItem.mask = TCIF_TEXT;
335 wxStrcpy(buf, (const wxChar*) text);
336 tcItem.pszText = buf;
337 tcItem.cchTextMax = 256;
338
339 return ( TabCtrl_SetItem( (HWND) GetHWND(), item, & tcItem) != 0 );
340 }
341
342 // Set the image for an item
343 bool wxTabCtrl::SetItemImage(int item, int image)
344 {
345 TC_ITEM tcItem;
346 tcItem.mask = TCIF_IMAGE;
347 tcItem.iImage = image;
348
349 return ( TabCtrl_SetItem( (HWND) GetHWND(), item, & tcItem) != 0 );
350 }
351
352 // Set the data for an item
353 bool wxTabCtrl::SetItemData(int item, void* data)
354 {
355 TC_ITEM tcItem;
356 tcItem.mask = TCIF_PARAM;
357 tcItem.lParam = (long) data;
358
359 return ( TabCtrl_SetItem( (HWND) GetHWND(), item, & tcItem) != 0 );
360 }
361
362 // Set the size for a fixed-width tab control
363 void wxTabCtrl::SetItemSize(const wxSize& size)
364 {
365 TabCtrl_SetItemSize( (HWND) GetHWND(), size.x, size.y );
366 }
367
368 // Set the padding between tabs
369 void wxTabCtrl::SetPadding(const wxSize& padding)
370 {
371 TabCtrl_SetPadding( (HWND) GetHWND(), padding.x, padding.y );
372 }
373
374 #if 0
375 // These are the default colors used to map the bitmap colors
376 // to the current system colors
377
378 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
379 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
380 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
381 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
382 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
383 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
384
385 void wxMapBitmap(HBITMAP hBitmap, int width, int height)
386 {
387 COLORMAP ColorMap[] = {
388 {BGR_BUTTONTEXT, COLOR_BTNTEXT}, // black
389 {BGR_BUTTONSHADOW, COLOR_BTNSHADOW}, // dark grey
390 {BGR_BUTTONFACE, COLOR_BTNFACE}, // bright grey
391 {BGR_BUTTONHILIGHT, COLOR_BTNHIGHLIGHT},// white
392 {BGR_BACKGROUNDSEL, COLOR_HIGHLIGHT}, // blue
393 {BGR_BACKGROUND, COLOR_WINDOW} // magenta
394 };
395
396 int NUM_MAPS = (sizeof(ColorMap)/sizeof(COLORMAP));
397 int n;
398 for ( n = 0; n < NUM_MAPS; n++)
399 {
400 ColorMap[n].to = ::GetSysColor(ColorMap[n].to);
401 }
402
403 HBITMAP hbmOld;
404 HDC hdcMem = CreateCompatibleDC(NULL);
405
406 if (hdcMem)
407 {
408 hbmOld = SelectObject(hdcMem, hBitmap);
409
410 int i, j, k;
411 for ( i = 0; i < width; i++)
412 {
413 for ( j = 0; j < height; j++)
414 {
415 COLORREF pixel = ::GetPixel(hdcMem, i, j);
416 /*
417 BYTE red = GetRValue(pixel);
418 BYTE green = GetGValue(pixel);
419 BYTE blue = GetBValue(pixel);
420 */
421
422 for ( k = 0; k < NUM_MAPS; k ++)
423 {
424 if ( ColorMap[k].from == pixel )
425 {
426 /* COLORREF actualPixel = */ ::SetPixel(hdcMem, i, j, ColorMap[k].to);
427 break;
428 }
429 }
430 }
431 }
432
433
434 SelectObject(hdcMem, hbmOld);
435 DeleteObject(hdcMem);
436 }
437
438 }
439 #endif
440
441 #endif
442 // __WIN95__