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