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