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