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