1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
23 #if defined(__WIN95__)
25 #include "wx/msw/private.h"
29 #include "wx/tabctrl.h"
31 #include "wx/msw/imaglist.h"
33 IMPLEMENT_DYNAMIC_CLASS(wxTabCtrl
, wxControl
)
34 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent
, wxNotifyEvent
)
36 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGED
)
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGING
)
39 BEGIN_EVENT_TABLE(wxTabCtrl
, wxControl
)
40 EVT_SYS_COLOUR_CHANGED(wxTabCtrl::OnSysColourChanged
)
43 wxTabCtrl::wxTabCtrl()
48 bool wxTabCtrl::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
49 long style
, const wxString
& name
)
53 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
54 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
55 m_foregroundColour
= *wxBLACK
;
64 m_windowStyle
= style
;
77 m_windowId
= (id
< 0 ? NewControlId() : id
);
79 long tabStyle
= WS_CHILD
| WS_VISIBLE
;
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
;
88 if (m_windowStyle
& wxBORDER
)
89 tabStyle
|= WS_BORDER
;
92 tabStyle
|= TCS_TOOLTIPS
;
95 // Create the toolbar control.
96 HWND hWndTabCtrl
= CreateWindowEx(0L, // No extended styles.
97 WC_TABCONTROL
, // Class name for the tab control
98 wxEmptyString
, // No default text.
99 tabStyle
, // Styles and defaults.
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.
106 m_hWnd
= (WXHWND
) hWndTabCtrl
;
107 if (parent
) parent
->AddChild(this);
109 SubclassWin((WXHWND
) hWndTabCtrl
);
111 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
116 wxTabCtrl::~wxTabCtrl()
121 bool wxTabCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
123 wxTabEvent
event(wxEVT_NULL
, m_windowId
);
124 wxEventType eventType
wxDUMMY_INITIALIZE(wxEVT_NULL
);
125 NMHDR
* hdr1
= (NMHDR
*) lParam
;
126 switch ( hdr1
->code
)
129 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGED
;
132 case TCN_SELCHANGING
:
133 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGING
;
140 // if (!tool->m_shortHelpString.empty())
141 // ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
145 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
148 event
.SetEventObject( this );
149 event
.SetEventType(eventType
);
150 event
.SetInt(idCtrl
) ;
151 event
.SetSelection(idCtrl
);
153 return ProcessEvent(event
);
156 // Responds to colour changes, and passes event on to children.
157 void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
159 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
160 GetGValue(GetSysColor(COLOR_BTNFACE
)),
161 GetBValue(GetSysColor(COLOR_BTNFACE
)));
165 // Propagate the event to the non-top-level children
166 wxWindow::OnSysColourChanged(event
);
170 bool wxTabCtrl::DeleteAllItems()
172 return ( TabCtrl_DeleteAllItems( (HWND
) GetHWND() ) != FALSE
);
176 bool wxTabCtrl::DeleteItem(int item
)
178 return ( TabCtrl_DeleteItem( (HWND
) GetHWND(), item
) != FALSE
);
182 int wxTabCtrl::GetSelection() const
184 return (int) TabCtrl_GetCurSel( (HWND
) GetHWND() );
187 // Get the tab with the current keyboard focus
188 int wxTabCtrl::GetCurFocus() const
190 return (int) TabCtrl_GetCurFocus( (HWND
) GetHWND() );
193 // Get the associated image list
194 wxImageList
* wxTabCtrl::GetImageList() const
199 // Get the number of items
200 int wxTabCtrl::GetItemCount() const
202 return (int) TabCtrl_GetItemCount( (HWND
) GetHWND() );
205 // Get the rect corresponding to the tab
206 bool wxTabCtrl::GetItemRect(int item
, wxRect
& wxrect
) const
209 if ( !TabCtrl_GetItemRect( (HWND
) GetHWND(), item
, & rect
) )
213 wxrect
.x
= rect
.left
; wxrect
.y
= rect
.top
;
214 wxrect
.width
= rect
.right
- rect
.left
;
215 wxrect
.height
= rect
.bottom
- rect
.top
;
220 // Get the number of rows
221 int wxTabCtrl::GetRowCount() const
223 return (int) TabCtrl_GetRowCount( (HWND
) GetHWND() );
227 wxString
wxTabCtrl::GetItemText(int item
) const
230 wxString
str(wxEmptyString
);
232 tcItem
.mask
= TCIF_TEXT
;
233 tcItem
.pszText
= buf
;
234 tcItem
.cchTextMax
= 256;
236 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
237 str
= tcItem
.pszText
;
242 // Get the item image
243 int wxTabCtrl::GetItemImage(int item
) const
246 tcItem
.mask
= TCIF_IMAGE
;
248 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
249 return tcItem
.iImage
;
255 void* wxTabCtrl::GetItemData(int item
) const
258 tcItem
.mask
= TCIF_PARAM
;
260 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
261 return (void*) tcItem
.lParam
;
267 int wxTabCtrl::HitTest(const wxPoint
& pt
, long& flags
)
269 TC_HITTESTINFO hitTestInfo
;
270 hitTestInfo
.pt
.x
= pt
.x
;
271 hitTestInfo
.pt
.y
= pt
.y
;
272 int item
= TabCtrl_HitTest( (HWND
) GetHWND(), & hitTestInfo
) ;
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
;
286 bool wxTabCtrl::InsertItem(int item
, const wxString
& text
, int imageId
, void* data
)
290 tcItem
.mask
= TCIF_PARAM
;
291 tcItem
.lParam
= (long) data
;
294 tcItem
.mask
|= TCIF_TEXT
;
295 wxStrcpy(buf
, (const wxChar
*) text
);
296 tcItem
.pszText
= buf
;
297 tcItem
.cchTextMax
= 256;
301 tcItem
.mask
|= TCIF_IMAGE
;
302 tcItem
.iImage
= imageId
;
305 return (TabCtrl_InsertItem( (HWND
) GetHWND(), item
, & tcItem
) != -1);
309 int wxTabCtrl::SetSelection(int item
)
311 return (int) TabCtrl_SetCurSel( (HWND
) GetHWND(), item
);
314 // Set the image list
315 void wxTabCtrl::SetImageList(wxImageList
* imageList
)
317 m_imageList
= imageList
;
318 TabCtrl_SetImageList( (HWND
) GetHWND(), (HIMAGELIST
) imageList
->GetHIMAGELIST() );
321 // Set the text for an item
322 bool wxTabCtrl::SetItemText(int item
, const wxString
& text
)
326 tcItem
.mask
= TCIF_TEXT
;
327 wxStrcpy(buf
, (const wxChar
*) text
);
328 tcItem
.pszText
= buf
;
329 tcItem
.cchTextMax
= 256;
331 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
334 // Set the image for an item
335 bool wxTabCtrl::SetItemImage(int item
, int image
)
338 tcItem
.mask
= TCIF_IMAGE
;
339 tcItem
.iImage
= image
;
341 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
344 // Set the data for an item
345 bool wxTabCtrl::SetItemData(int item
, void* data
)
348 tcItem
.mask
= TCIF_PARAM
;
349 tcItem
.lParam
= (long) data
;
351 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
354 // Set the size for a fixed-width tab control
355 void wxTabCtrl::SetItemSize(const wxSize
& size
)
357 TabCtrl_SetItemSize( (HWND
) GetHWND(), size
.x
, size
.y
);
360 // Set the padding between tabs
361 void wxTabCtrl::SetPadding(const wxSize
& padding
)
363 TabCtrl_SetPadding( (HWND
) GetHWND(), padding
.x
, padding
.y
);
367 // These are the default colors used to map the bitmap colors
368 // to the current system colors
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
377 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
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
388 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
390 for ( n
= 0; n
< NUM_MAPS
; n
++)
392 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
396 HDC hdcMem
= CreateCompatibleDC(NULL
);
400 hbmOld
= SelectObject(hdcMem
, hBitmap
);
403 for ( i
= 0; i
< width
; i
++)
405 for ( j
= 0; j
< height
; j
++)
407 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
409 BYTE red = GetRValue(pixel);
410 BYTE green = GetGValue(pixel);
411 BYTE blue = GetBValue(pixel);
414 for ( k
= 0; k
< NUM_MAPS
; k
++)
416 if ( ColorMap
[k
].from
== pixel
)
418 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
426 SelectObject(hdcMem
, hbmOld
);
427 DeleteObject(hdcMem
);