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