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