1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
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__)
33 #include "wx/msw/private.h"
35 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
38 #include "wx/msw/gnuwin32/extra.h"
41 #include "wx/tabctrl.h"
43 #include "wx/msw/imaglist.h"
45 IMPLEMENT_DYNAMIC_CLASS(wxTabCtrl
, wxControl
)
46 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent
, wxNotifyEvent
)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGED
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGING
)
51 BEGIN_EVENT_TABLE(wxTabCtrl
, wxControl
)
52 EVT_SYS_COLOUR_CHANGED(wxTabCtrl::OnSysColourChanged
)
55 wxTabCtrl::wxTabCtrl()
60 bool wxTabCtrl::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
61 long style
, const wxString
& name
)
65 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
66 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
67 m_foregroundColour
= *wxBLACK
;
76 m_windowStyle
= style
;
89 m_windowId
= (id
< 0 ? NewControlId() : id
);
91 long tabStyle
= WS_CHILD
| WS_VISIBLE
;
92 if (m_windowStyle
& wxTC_MULTILINE
)
93 tabStyle
|= TCS_MULTILINE
;
94 if (m_windowStyle
& wxTC_RIGHTJUSTIFY
)
95 tabStyle
|= TCS_RIGHTJUSTIFY
;
96 if (m_windowStyle
& wxTC_FIXEDWIDTH
)
97 tabStyle
|= TCS_FIXEDWIDTH
;
98 if (m_windowStyle
& wxTC_OWNERDRAW
)
99 tabStyle
|= TCS_OWNERDRAWFIXED
;
100 if (m_windowStyle
& wxBORDER
)
101 tabStyle
|= WS_BORDER
;
104 tabStyle
|= TCS_TOOLTIPS
;
107 // Create the toolbar control.
108 HWND hWndTabCtrl
= CreateWindowEx(0L, // No extended styles.
109 WC_TABCONTROL
, // Class name for the tab control
110 wxEmptyString
, // No default text.
111 tabStyle
, // Styles and defaults.
112 x
, y
, width
, height
, // Standard size and position.
113 (HWND
) parent
->GetHWND(), // Parent window
114 (HMENU
)m_windowId
, // ID.
115 wxGetInstance(), // Current instance.
116 NULL
); // No class data.
118 m_hWnd
= (WXHWND
) hWndTabCtrl
;
119 if (parent
) parent
->AddChild(this);
121 SubclassWin((WXHWND
) hWndTabCtrl
);
123 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
128 wxTabCtrl::~wxTabCtrl()
133 bool wxTabCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
135 wxTabEvent
event(wxEVT_NULL
, m_windowId
);
136 wxEventType eventType
= wxEVT_NULL
;
137 NMHDR
* hdr1
= (NMHDR
*) lParam
;
138 switch ( hdr1
->code
)
141 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGED
;
144 case TCN_SELCHANGING
:
145 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGING
;
152 // if (tool->m_shortHelpString != "")
153 // ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
157 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
160 event
.SetEventObject( this );
161 event
.SetEventType(eventType
);
162 event
.SetInt(idCtrl
) ;
163 event
.SetSelection(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(wxEmptyString
);
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
;
304 if (text
!= wxEmptyString
)
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
);