]> git.saurik.com Git - wxWidgets.git/blame - src/msw/tabctrl.cpp
fixed typo in library name
[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
c42404a5 9// Licence: wxWindows license
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
33#include <windows.h>
34
ae090fdb 35#if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
c42404a5
VZ
36 #include <commctrl.h>
37#else
38 #include "wx/msw/gnuwin32/extra.h"
65fd5cb0 39#endif
2bda0e17
KB
40
41#include "wx/msw/dib.h"
186ef90c 42#include "wx/tabctrl.h"
2bda0e17
KB
43#include "wx/app.h"
44#include "wx/msw/private.h"
45#include "wx/msw/imaglist.h"
46
2bda0e17 47IMPLEMENT_DYNAMIC_CLASS(wxTabCtrl, wxControl)
2b5f62a0 48IMPLEMENT_DYNAMIC_CLASS(wxTabEvent, wxNotifyEvent)
2bda0e17 49
2e4df4bf
VZ
50DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGED)
51DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGING)
52
2bda0e17 53BEGIN_EVENT_TABLE(wxTabCtrl, wxControl)
2bda0e17
KB
54 EVT_SYS_COLOUR_CHANGED(wxTabCtrl::OnSysColourChanged)
55END_EVENT_TABLE()
2bda0e17 56
ee4f8c2a 57wxTabCtrl::wxTabCtrl()
2bda0e17
KB
58{
59 m_imageList = NULL;
60}
61
ee4f8c2a
JS
62bool wxTabCtrl::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
63 long style, const wxString& name)
2bda0e17
KB
64{
65 m_imageList = NULL;
66
67 m_backgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
42e69d6b 68 GetGValue(GetSysColor(COLOR_BTNFACE)), GetBValue(GetSysColor(COLOR_BTNFACE)));
2bda0e17
KB
69 m_foregroundColour = *wxBLACK ;
70
2bda0e17
KB
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
2bda0e17
KB
80 SetParent(parent);
81
2bda0e17
KB
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;
2b5f62a0
VZ
102 if (m_windowStyle & wxBORDER)
103 tabStyle |= WS_BORDER;
2bda0e17
KB
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
223d09f6 110 wxT(""), // 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
2bda0e17
KB
148 case TTN_NEEDTEXT:
149 {
150 // TODO
151// if (tool->m_shortHelpString != "")
152// ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
2bda0e17
KB
153 }
154
42e69d6b
VZ
155 default :
156 return wxControl::MSWOnNotify(idCtrl, lParam, result);
157 }
2bda0e17 158
42e69d6b
VZ
159 event.SetEventObject( this );
160 event.SetEventType(eventType);
161 event.SetInt(idCtrl) ;
2b5f62a0 162 event.SetSelection(idCtrl);
2bda0e17 163
42e69d6b 164 return ProcessEvent(event);
2bda0e17
KB
165}
166
167// Responds to colour changes, and passes event on to children.
168void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent& event)
169{
170 m_backgroundColour = wxColour(GetRValue(GetSysColor(COLOR_BTNFACE)),
42e69d6b
VZ
171 GetGValue(GetSysColor(COLOR_BTNFACE)),
172 GetBValue(GetSysColor(COLOR_BTNFACE)));
2bda0e17
KB
173
174 Refresh();
175
176 // Propagate the event to the non-top-level children
177 wxWindow::OnSysColourChanged(event);
178}
179
180// Delete all items
ee4f8c2a 181bool wxTabCtrl::DeleteAllItems()
2bda0e17
KB
182{
183 return ( TabCtrl_DeleteAllItems( (HWND) GetHWND() ) != FALSE );
184}
185
186// Delete an item
ee4f8c2a 187bool wxTabCtrl::DeleteItem(int item)
2bda0e17
KB
188{
189 return ( TabCtrl_DeleteItem( (HWND) GetHWND(), item) != FALSE );
190}
191
192// Get the selection
ee4f8c2a 193int wxTabCtrl::GetSelection() const
2bda0e17
KB
194{
195 return (int) TabCtrl_GetCurSel( (HWND) GetHWND() );
196}
197
da87a1ca
JS
198// Get the tab with the current keyboard focus
199int wxTabCtrl::GetCurFocus() const
200{
201 return (int) TabCtrl_GetCurFocus( (HWND) GetHWND() );
202}
203
2bda0e17 204// Get the associated image list
ee4f8c2a 205wxImageList* wxTabCtrl::GetImageList() const
2bda0e17
KB
206{
207 return m_imageList;
208}
209
210// Get the number of items
ee4f8c2a 211int wxTabCtrl::GetItemCount() const
2bda0e17
KB
212{
213 return (int) TabCtrl_GetItemCount( (HWND) GetHWND() );
214}
215
216// Get the rect corresponding to the tab
ee4f8c2a 217bool wxTabCtrl::GetItemRect(int item, wxRect& wxrect) const
2bda0e17
KB
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
ee4f8c2a 232int wxTabCtrl::GetRowCount() const
2bda0e17
KB
233{
234 return (int) TabCtrl_GetRowCount( (HWND) GetHWND() );
235}
236
237// Get the item text
ee4f8c2a 238wxString wxTabCtrl::GetItemText(int item) const
2bda0e17 239{
837e5743 240 wxChar buf[256];
223d09f6 241 wxString str(wxT(""));
2bda0e17
KB
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
ee4f8c2a 254int wxTabCtrl::GetItemImage(int item) const
2bda0e17
KB
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
ee4f8c2a 266void* wxTabCtrl::GetItemData(int item) const
2bda0e17
KB
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
278int 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
ee4f8c2a 297bool wxTabCtrl::InsertItem(int item, const wxString& text, int imageId, void* data)
2bda0e17 298{
837e5743 299 wxChar buf[256];
2bda0e17
KB
300 TC_ITEM tcItem;
301 tcItem.mask = TCIF_PARAM;
302 tcItem.lParam = (long) data;
223d09f6 303 if (text != wxT(""))
2bda0e17
KB
304 {
305 tcItem.mask |= TCIF_TEXT;
837e5743 306 wxStrcpy(buf, (const wxChar*) text);
2bda0e17
KB
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
ee4f8c2a 316 return (TabCtrl_InsertItem( (HWND) GetHWND(), item, & tcItem) != -1);
2bda0e17
KB
317}
318
319// Set the selection
ee4f8c2a 320int wxTabCtrl::SetSelection(int item)
2bda0e17
KB
321{
322 return (int) TabCtrl_SetCurSel( (HWND) GetHWND(), item );
323}
324
325// Set the image list
326void 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
ee4f8c2a 333bool wxTabCtrl::SetItemText(int item, const wxString& text)
2bda0e17 334{
837e5743 335 wxChar buf[256];
2bda0e17
KB
336 TC_ITEM tcItem;
337 tcItem.mask = TCIF_TEXT;
837e5743 338 wxStrcpy(buf, (const wxChar*) text);
2bda0e17
KB
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
ee4f8c2a 346bool wxTabCtrl::SetItemImage(int item, int image)
2bda0e17
KB
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
ee4f8c2a 356bool wxTabCtrl::SetItemData(int item, void* data)
2bda0e17
KB
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
366void wxTabCtrl::SetItemSize(const wxSize& size)
367{
368 TabCtrl_SetItemSize( (HWND) GetHWND(), size.x, size.y );
369}
370
371// Set the padding between tabs
372void 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
388void 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
2bda0e17
KB
444#endif
445 // __WIN95__