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