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