1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "tabctrl.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #if defined(__WIN95__)
29 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__)
35 #if !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
38 #include "wx/msw/gnuwin32/extra.h"
41 #include "wx/msw/dib.h"
42 #include "wx/msw/tabctrl.h"
44 #include "wx/msw/private.h"
45 #include "wx/msw/imaglist.h"
47 IMPLEMENT_DYNAMIC_CLASS(wxTabCtrl
, wxControl
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGED
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGING
)
52 BEGIN_EVENT_TABLE(wxTabCtrl
, wxControl
)
53 EVT_SYS_COLOUR_CHANGED(wxTabCtrl::OnSysColourChanged
)
56 wxTabCtrl::wxTabCtrl()
61 bool wxTabCtrl::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
62 long style
, const wxString
& name
)
66 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
67 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
68 m_foregroundColour
= *wxBLACK
;
77 m_windowStyle
= style
;
79 SetFont(* (wxTheFontList
->FindOrCreateFont(11, wxSWISS
, wxNORMAL
, wxNORMAL
)));
86 msflags
|= WS_CHILD
| WS_VISIBLE
;
97 m_windowId
= (id
< 0 ? NewControlId() : id
);
100 if (m_windowStyle
& wxTC_MULTILINE
)
101 tabStyle
|= TCS_MULTILINE
;
102 if (m_windowStyle
& wxTC_RIGHTJUSTIFY
)
103 tabStyle
|= TCS_RIGHTJUSTIFY
;
104 if (m_windowStyle
& wxTC_FIXEDWIDTH
)
105 tabStyle
|= TCS_FIXEDWIDTH
;
106 if (m_windowStyle
& wxTC_OWNERDRAW
)
107 tabStyle
|= TCS_OWNERDRAWFIXED
;
109 tabStyle
|= TCS_TOOLTIPS
;
111 // Create the toolbar control.
112 HWND hWndTabCtrl
= CreateWindowEx(0L, // No extended styles.
113 WC_TABCONTROL
, // Class name for the tab control
114 wxT(""), // No default text.
115 WS_CHILD
| WS_BORDER
| WS_VISIBLE
| tabStyle
, // Styles and defaults.
116 x
, y
, width
, height
, // Standard size and position.
117 (HWND
) parent
->GetHWND(), // Parent window
118 (HMENU
)m_windowId
, // ID.
119 wxGetInstance(), // Current instance.
120 NULL
); // No class data.
122 m_hWnd
= (WXHWND
) hWndTabCtrl
;
123 if (parent
) parent
->AddChild(this);
125 SubclassWin((WXHWND
) hWndTabCtrl
);
130 wxTabCtrl::~wxTabCtrl()
135 bool wxTabCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
137 wxTabEvent
event(wxEVT_NULL
, m_windowId
);
138 wxEventType eventType
= wxEVT_NULL
;
139 NMHDR
* hdr1
= (NMHDR
*) lParam
;
140 switch ( hdr1
->code
)
143 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGED
;
146 case TCN_SELCHANGING
:
147 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGING
;
153 // if (tool->m_shortHelpString != "")
154 // ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
158 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
161 event
.SetEventObject( this );
162 event
.SetEventType(eventType
);
163 event
.SetInt(idCtrl
) ;
165 return ProcessEvent(event
);
168 // Responds to colour changes, and passes event on to children.
169 void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
171 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
172 GetGValue(GetSysColor(COLOR_BTNFACE
)),
173 GetBValue(GetSysColor(COLOR_BTNFACE
)));
177 // Propagate the event to the non-top-level children
178 wxWindow::OnSysColourChanged(event
);
182 bool wxTabCtrl::DeleteAllItems()
184 return ( TabCtrl_DeleteAllItems( (HWND
) GetHWND() ) != FALSE
);
188 bool wxTabCtrl::DeleteItem(int item
)
190 return ( TabCtrl_DeleteItem( (HWND
) GetHWND(), item
) != FALSE
);
194 int wxTabCtrl::GetSelection() const
196 return (int) TabCtrl_GetCurSel( (HWND
) GetHWND() );
199 // Get the tab with the current keyboard focus
200 int wxTabCtrl::GetCurFocus() const
202 return (int) TabCtrl_GetCurFocus( (HWND
) GetHWND() );
205 // Get the associated image list
206 wxImageList
* wxTabCtrl::GetImageList() const
211 // Get the number of items
212 int wxTabCtrl::GetItemCount() const
214 return (int) TabCtrl_GetItemCount( (HWND
) GetHWND() );
217 // Get the rect corresponding to the tab
218 bool wxTabCtrl::GetItemRect(int item
, wxRect
& wxrect
) const
221 if ( !TabCtrl_GetItemRect( (HWND
) GetHWND(), item
, & rect
) )
225 wxrect
.x
= rect
.left
; wxrect
.y
= rect
.top
;
226 wxrect
.width
= rect
.right
- rect
.left
;
227 wxrect
.height
= rect
.bottom
- rect
.top
;
232 // Get the number of rows
233 int wxTabCtrl::GetRowCount() const
235 return (int) TabCtrl_GetRowCount( (HWND
) GetHWND() );
239 wxString
wxTabCtrl::GetItemText(int item
) const
242 wxString
str(wxT(""));
244 tcItem
.mask
= TCIF_TEXT
;
245 tcItem
.pszText
= buf
;
246 tcItem
.cchTextMax
= 256;
248 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
249 str
= tcItem
.pszText
;
254 // Get the item image
255 int wxTabCtrl::GetItemImage(int item
) const
258 tcItem
.mask
= TCIF_IMAGE
;
260 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
261 return tcItem
.iImage
;
267 void* wxTabCtrl::GetItemData(int item
) const
270 tcItem
.mask
= TCIF_PARAM
;
272 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
273 return (void*) tcItem
.lParam
;
279 int wxTabCtrl::HitTest(const wxPoint
& pt
, long& flags
)
281 TC_HITTESTINFO hitTestInfo
;
282 hitTestInfo
.pt
.x
= pt
.x
;
283 hitTestInfo
.pt
.y
= pt
.y
;
284 int item
= TabCtrl_HitTest( (HWND
) GetHWND(), & hitTestInfo
) ;
287 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
288 flags
|= wxTAB_HITTEST_NOWHERE
;
289 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
290 flags
|= wxTAB_HITTEST_ONICON
;
291 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
292 flags
|= wxTAB_HITTEST_ONLABEL
;
298 bool wxTabCtrl::InsertItem(int item
, const wxString
& text
, int imageId
, void* data
)
302 tcItem
.mask
= TCIF_PARAM
;
303 tcItem
.lParam
= (long) data
;
306 tcItem
.mask
|= TCIF_TEXT
;
307 wxStrcpy(buf
, (const wxChar
*) text
);
308 tcItem
.pszText
= buf
;
309 tcItem
.cchTextMax
= 256;
313 tcItem
.mask
|= TCIF_IMAGE
;
314 tcItem
.iImage
= imageId
;
317 return (TabCtrl_InsertItem( (HWND
) GetHWND(), item
, & tcItem
) != -1);
321 int wxTabCtrl::SetSelection(int item
)
323 return (int) TabCtrl_SetCurSel( (HWND
) GetHWND(), item
);
326 // Set the image list
327 void wxTabCtrl::SetImageList(wxImageList
* imageList
)
329 m_imageList
= imageList
;
330 TabCtrl_SetImageList( (HWND
) GetHWND(), (HIMAGELIST
) imageList
->GetHIMAGELIST() );
333 // Set the text for an item
334 bool wxTabCtrl::SetItemText(int item
, const wxString
& text
)
338 tcItem
.mask
= TCIF_TEXT
;
339 wxStrcpy(buf
, (const wxChar
*) text
);
340 tcItem
.pszText
= buf
;
341 tcItem
.cchTextMax
= 256;
343 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
346 // Set the image for an item
347 bool wxTabCtrl::SetItemImage(int item
, int image
)
350 tcItem
.mask
= TCIF_IMAGE
;
351 tcItem
.iImage
= image
;
353 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
356 // Set the data for an item
357 bool wxTabCtrl::SetItemData(int item
, void* data
)
360 tcItem
.mask
= TCIF_PARAM
;
361 tcItem
.lParam
= (long) data
;
363 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
366 // Set the size for a fixed-width tab control
367 void wxTabCtrl::SetItemSize(const wxSize
& size
)
369 TabCtrl_SetItemSize( (HWND
) GetHWND(), size
.x
, size
.y
);
372 // Set the padding between tabs
373 void wxTabCtrl::SetPadding(const wxSize
& padding
)
375 TabCtrl_SetPadding( (HWND
) GetHWND(), padding
.x
, padding
.y
);
379 // These are the default colors used to map the bitmap colors
380 // to the current system colors
382 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
383 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
384 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
385 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
386 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
387 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
389 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
391 COLORMAP ColorMap
[] = {
392 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
393 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
394 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
395 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
396 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
397 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
400 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
402 for ( n
= 0; n
< NUM_MAPS
; n
++)
404 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
408 HDC hdcMem
= CreateCompatibleDC(NULL
);
412 hbmOld
= SelectObject(hdcMem
, hBitmap
);
415 for ( i
= 0; i
< width
; i
++)
417 for ( j
= 0; j
< height
; j
++)
419 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
421 BYTE red = GetRValue(pixel);
422 BYTE green = GetGValue(pixel);
423 BYTE blue = GetBValue(pixel);
426 for ( k
= 0; k
< NUM_MAPS
; k
++)
428 if ( ColorMap
[k
].from
== pixel
)
430 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
438 SelectObject(hdcMem
, hbmOld
);
439 DeleteObject(hdcMem
);
446 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent
, wxCommandEvent
)
448 wxTabEvent::wxTabEvent(wxEventType commandType
, int id
):
449 wxCommandEvent(commandType
, id
)