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