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