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