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__)
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/private.h"
44 #include "wx/msw/imaglist.h"
46 IMPLEMENT_DYNAMIC_CLASS(wxTabCtrl
, wxControl
)
47 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent
, wxNotifyEvent
)
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
;
90 m_windowId
= (id
< 0 ? NewControlId() : id
);
92 long tabStyle
= WS_CHILD
| WS_VISIBLE
;
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
;
101 if (m_windowStyle
& wxBORDER
)
102 tabStyle
|= WS_BORDER
;
104 tabStyle
|= TCS_TOOLTIPS
;
106 // Create the toolbar control.
107 HWND hWndTabCtrl
= CreateWindowEx(0L, // No extended styles.
108 WC_TABCONTROL
, // Class name for the tab control
109 wxEmptyString
, // No default text.
110 tabStyle
, // Styles and defaults.
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.
117 m_hWnd
= (WXHWND
) hWndTabCtrl
;
118 if (parent
) parent
->AddChild(this);
120 SubclassWin((WXHWND
) hWndTabCtrl
);
122 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
127 wxTabCtrl::~wxTabCtrl()
132 bool wxTabCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
134 wxTabEvent
event(wxEVT_NULL
, m_windowId
);
135 wxEventType eventType
= wxEVT_NULL
;
136 NMHDR
* hdr1
= (NMHDR
*) lParam
;
137 switch ( hdr1
->code
)
140 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGED
;
143 case TCN_SELCHANGING
:
144 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGING
;
150 // if (tool->m_shortHelpString != "")
151 // ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
155 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
158 event
.SetEventObject( this );
159 event
.SetEventType(eventType
);
160 event
.SetInt(idCtrl
) ;
161 event
.SetSelection(idCtrl
);
163 return ProcessEvent(event
);
166 // Responds to colour changes, and passes event on to children.
167 void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
169 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
170 GetGValue(GetSysColor(COLOR_BTNFACE
)),
171 GetBValue(GetSysColor(COLOR_BTNFACE
)));
175 // Propagate the event to the non-top-level children
176 wxWindow::OnSysColourChanged(event
);
180 bool wxTabCtrl::DeleteAllItems()
182 return ( TabCtrl_DeleteAllItems( (HWND
) GetHWND() ) != FALSE
);
186 bool wxTabCtrl::DeleteItem(int item
)
188 return ( TabCtrl_DeleteItem( (HWND
) GetHWND(), item
) != FALSE
);
192 int wxTabCtrl::GetSelection() const
194 return (int) TabCtrl_GetCurSel( (HWND
) GetHWND() );
197 // Get the tab with the current keyboard focus
198 int wxTabCtrl::GetCurFocus() const
200 return (int) TabCtrl_GetCurFocus( (HWND
) GetHWND() );
203 // Get the associated image list
204 wxImageList
* wxTabCtrl::GetImageList() const
209 // Get the number of items
210 int wxTabCtrl::GetItemCount() const
212 return (int) TabCtrl_GetItemCount( (HWND
) GetHWND() );
215 // Get the rect corresponding to the tab
216 bool wxTabCtrl::GetItemRect(int item
, wxRect
& wxrect
) const
219 if ( !TabCtrl_GetItemRect( (HWND
) GetHWND(), item
, & rect
) )
223 wxrect
.x
= rect
.left
; wxrect
.y
= rect
.top
;
224 wxrect
.width
= rect
.right
- rect
.left
;
225 wxrect
.height
= rect
.bottom
- rect
.top
;
230 // Get the number of rows
231 int wxTabCtrl::GetRowCount() const
233 return (int) TabCtrl_GetRowCount( (HWND
) GetHWND() );
237 wxString
wxTabCtrl::GetItemText(int item
) const
240 wxString
str(wxEmptyString
);
242 tcItem
.mask
= TCIF_TEXT
;
243 tcItem
.pszText
= buf
;
244 tcItem
.cchTextMax
= 256;
246 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
247 str
= tcItem
.pszText
;
252 // Get the item image
253 int wxTabCtrl::GetItemImage(int item
) const
256 tcItem
.mask
= TCIF_IMAGE
;
258 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
259 return tcItem
.iImage
;
265 void* wxTabCtrl::GetItemData(int item
) const
268 tcItem
.mask
= TCIF_PARAM
;
270 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
271 return (void*) tcItem
.lParam
;
277 int wxTabCtrl::HitTest(const wxPoint
& pt
, long& flags
)
279 TC_HITTESTINFO hitTestInfo
;
280 hitTestInfo
.pt
.x
= pt
.x
;
281 hitTestInfo
.pt
.y
= pt
.y
;
282 int item
= TabCtrl_HitTest( (HWND
) GetHWND(), & hitTestInfo
) ;
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
;
296 bool wxTabCtrl::InsertItem(int item
, const wxString
& text
, int imageId
, void* data
)
300 tcItem
.mask
= TCIF_PARAM
;
301 tcItem
.lParam
= (long) data
;
302 if (text
!= wxEmptyString
)
304 tcItem
.mask
|= TCIF_TEXT
;
305 wxStrcpy(buf
, (const wxChar
*) text
);
306 tcItem
.pszText
= buf
;
307 tcItem
.cchTextMax
= 256;
311 tcItem
.mask
|= TCIF_IMAGE
;
312 tcItem
.iImage
= imageId
;
315 return (TabCtrl_InsertItem( (HWND
) GetHWND(), item
, & tcItem
) != -1);
319 int wxTabCtrl::SetSelection(int item
)
321 return (int) TabCtrl_SetCurSel( (HWND
) GetHWND(), item
);
324 // Set the image list
325 void wxTabCtrl::SetImageList(wxImageList
* imageList
)
327 m_imageList
= imageList
;
328 TabCtrl_SetImageList( (HWND
) GetHWND(), (HIMAGELIST
) imageList
->GetHIMAGELIST() );
331 // Set the text for an item
332 bool wxTabCtrl::SetItemText(int item
, const wxString
& text
)
336 tcItem
.mask
= TCIF_TEXT
;
337 wxStrcpy(buf
, (const wxChar
*) text
);
338 tcItem
.pszText
= buf
;
339 tcItem
.cchTextMax
= 256;
341 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
344 // Set the image for an item
345 bool wxTabCtrl::SetItemImage(int item
, int image
)
348 tcItem
.mask
= TCIF_IMAGE
;
349 tcItem
.iImage
= image
;
351 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
354 // Set the data for an item
355 bool wxTabCtrl::SetItemData(int item
, void* data
)
358 tcItem
.mask
= TCIF_PARAM
;
359 tcItem
.lParam
= (long) data
;
361 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
364 // Set the size for a fixed-width tab control
365 void wxTabCtrl::SetItemSize(const wxSize
& size
)
367 TabCtrl_SetItemSize( (HWND
) GetHWND(), size
.x
, size
.y
);
370 // Set the padding between tabs
371 void wxTabCtrl::SetPadding(const wxSize
& padding
)
373 TabCtrl_SetPadding( (HWND
) GetHWND(), padding
.x
, padding
.y
);
377 // These are the default colors used to map the bitmap colors
378 // to the current system colors
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
387 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
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
398 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
400 for ( n
= 0; n
< NUM_MAPS
; n
++)
402 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
406 HDC hdcMem
= CreateCompatibleDC(NULL
);
410 hbmOld
= SelectObject(hdcMem
, hBitmap
);
413 for ( i
= 0; i
< width
; i
++)
415 for ( j
= 0; j
< height
; j
++)
417 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
419 BYTE red = GetRValue(pixel);
420 BYTE green = GetGValue(pixel);
421 BYTE blue = GetBValue(pixel);
424 for ( k
= 0; k
< NUM_MAPS
; k
++)
426 if ( ColorMap
[k
].from
== pixel
)
428 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
436 SelectObject(hdcMem
, hbmOld
);
437 DeleteObject(hdcMem
);