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