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