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