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