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