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