]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tabctrl.cpp
Changes for wxEventType
[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.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
50 IMPLEMENT_DYNAMIC_CLASS(wxTabCtrl, wxControl)
51
52 BEGIN_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)
58 END_EVENT_TABLE()
59 #endif
60
61 wxTabCtrl::wxTabCtrl()
62 {
63 m_imageList = NULL;
64 }
65
66 bool wxTabCtrl::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
67 long style, const wxString& name)
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
139 wxTabCtrl::~wxTabCtrl()
140 {
141 UnsubclassWin();
142 }
143
144 void wxTabCtrl::Command(wxCommandEvent& event)
145 {
146 }
147
148 bool wxTabCtrl::MSWCommand(const WXUINT cmd, const WXWORD id)
149 {
150 return FALSE;
151 }
152
153 bool wxTabCtrl::MSWNotify(const WXWPARAM wParam, const WXLPARAM lParam)
154 {
155 wxTabEvent event(wxEVT_NULL, m_windowId);
156 wxEventType eventType = wxEVT_NULL;
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.
195 void 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
214 bool wxTabCtrl::DeleteAllItems()
215 {
216 return ( TabCtrl_DeleteAllItems( (HWND) GetHWND() ) != FALSE );
217 }
218
219 // Delete an item
220 bool wxTabCtrl::DeleteItem(int item)
221 {
222 return ( TabCtrl_DeleteItem( (HWND) GetHWND(), item) != FALSE );
223 }
224
225 // Get the selection
226 int wxTabCtrl::GetSelection() const
227 {
228 return (int) TabCtrl_GetCurSel( (HWND) GetHWND() );
229 }
230
231 // Get the associated image list
232 wxImageList* wxTabCtrl::GetImageList() const
233 {
234 return m_imageList;
235 }
236
237 // Get the number of items
238 int wxTabCtrl::GetItemCount() const
239 {
240 return (int) TabCtrl_GetItemCount( (HWND) GetHWND() );
241 }
242
243 // Get the rect corresponding to the tab
244 bool wxTabCtrl::GetItemRect(int item, wxRect& wxrect) const
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
259 int wxTabCtrl::GetRowCount() const
260 {
261 return (int) TabCtrl_GetRowCount( (HWND) GetHWND() );
262 }
263
264 // Get the item text
265 wxString wxTabCtrl::GetItemText(int item) const
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
281 int wxTabCtrl::GetItemImage(int item) const
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
293 void* wxTabCtrl::GetItemData(int item) const
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
305 int 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
324 bool wxTabCtrl::InsertItem(int item, const wxString& text, int imageId, void* data)
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
343 return (TabCtrl_InsertItem( (HWND) GetHWND(), item, & tcItem) != -1);
344 }
345
346 // Set the selection
347 int wxTabCtrl::SetSelection(int item)
348 {
349 return (int) TabCtrl_SetCurSel( (HWND) GetHWND(), item );
350 }
351
352 // Set the image list
353 void 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
360 bool wxTabCtrl::SetItemText(int item, const wxString& text)
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
373 bool wxTabCtrl::SetItemImage(int item, int image)
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
383 bool wxTabCtrl::SetItemData(int item, void* data)
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
393 void wxTabCtrl::SetItemSize(const wxSize& size)
394 {
395 TabCtrl_SetItemSize( (HWND) GetHWND(), size.x, size.y );
396 }
397
398 // Set the padding between tabs
399 void 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
415 void 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
472 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent, wxCommandEvent)
473
474 wxTabEvent::wxTabEvent(wxEventType commandType, int id):
475 wxCommandEvent(commandType, id)
476 {
477 }
478
479
480 #endif
481 // __WIN95__