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(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
38 #include "wx/msw/gnuwin32/extra.h"
41 #include "wx/msw/dib.h"
42 #include "wx/tabctrl.h"
44 #include "wx/msw/private.h"
45 #include "wx/msw/imaglist.h"
47 IMPLEMENT_DYNAMIC_CLASS(wxTabCtrl
, wxControl
)
48 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent
, wxNotifyEvent
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGED
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGING
)
53 BEGIN_EVENT_TABLE(wxTabCtrl
, wxControl
)
54 EVT_SYS_COLOUR_CHANGED(wxTabCtrl::OnSysColourChanged
)
57 wxTabCtrl::wxTabCtrl()
62 bool wxTabCtrl::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
63 long style
, const wxString
& name
)
67 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
68 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
69 m_foregroundColour
= *wxBLACK
;
78 m_windowStyle
= style
;
91 m_windowId
= (id
< 0 ? NewControlId() : id
);
94 if (m_windowStyle
& wxTC_MULTILINE
)
95 tabStyle
|= TCS_MULTILINE
;
96 if (m_windowStyle
& wxTC_RIGHTJUSTIFY
)
97 tabStyle
|= TCS_RIGHTJUSTIFY
;
98 if (m_windowStyle
& wxTC_FIXEDWIDTH
)
99 tabStyle
|= TCS_FIXEDWIDTH
;
100 if (m_windowStyle
& wxTC_OWNERDRAW
)
101 tabStyle
|= TCS_OWNERDRAWFIXED
;
102 if (m_windowStyle
& wxBORDER
)
103 tabStyle
|= WS_BORDER
;
105 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 wxT(""), // 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
;
151 // if (tool->m_shortHelpString != "")
152 // ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
156 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
159 event
.SetEventObject( this );
160 event
.SetEventType(eventType
);
161 event
.SetInt(idCtrl
) ;
162 event
.SetSelection(idCtrl
);
164 return ProcessEvent(event
);
167 // Responds to colour changes, and passes event on to children.
168 void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
170 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
171 GetGValue(GetSysColor(COLOR_BTNFACE
)),
172 GetBValue(GetSysColor(COLOR_BTNFACE
)));
176 // Propagate the event to the non-top-level children
177 wxWindow::OnSysColourChanged(event
);
181 bool wxTabCtrl::DeleteAllItems()
183 return ( TabCtrl_DeleteAllItems( (HWND
) GetHWND() ) != FALSE
);
187 bool wxTabCtrl::DeleteItem(int item
)
189 return ( TabCtrl_DeleteItem( (HWND
) GetHWND(), item
) != FALSE
);
193 int wxTabCtrl::GetSelection() const
195 return (int) TabCtrl_GetCurSel( (HWND
) GetHWND() );
198 // Get the tab with the current keyboard focus
199 int wxTabCtrl::GetCurFocus() const
201 return (int) TabCtrl_GetCurFocus( (HWND
) GetHWND() );
204 // Get the associated image list
205 wxImageList
* wxTabCtrl::GetImageList() const
210 // Get the number of items
211 int wxTabCtrl::GetItemCount() const
213 return (int) TabCtrl_GetItemCount( (HWND
) GetHWND() );
216 // Get the rect corresponding to the tab
217 bool wxTabCtrl::GetItemRect(int item
, wxRect
& wxrect
) const
220 if ( !TabCtrl_GetItemRect( (HWND
) GetHWND(), item
, & rect
) )
224 wxrect
.x
= rect
.left
; wxrect
.y
= rect
.top
;
225 wxrect
.width
= rect
.right
- rect
.left
;
226 wxrect
.height
= rect
.bottom
- rect
.top
;
231 // Get the number of rows
232 int wxTabCtrl::GetRowCount() const
234 return (int) TabCtrl_GetRowCount( (HWND
) GetHWND() );
238 wxString
wxTabCtrl::GetItemText(int item
) const
241 wxString
str(wxT(""));
243 tcItem
.mask
= TCIF_TEXT
;
244 tcItem
.pszText
= buf
;
245 tcItem
.cchTextMax
= 256;
247 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
248 str
= tcItem
.pszText
;
253 // Get the item image
254 int wxTabCtrl::GetItemImage(int item
) const
257 tcItem
.mask
= TCIF_IMAGE
;
259 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
260 return tcItem
.iImage
;
266 void* wxTabCtrl::GetItemData(int item
) const
269 tcItem
.mask
= TCIF_PARAM
;
271 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
272 return (void*) tcItem
.lParam
;
278 int wxTabCtrl::HitTest(const wxPoint
& pt
, long& flags
)
280 TC_HITTESTINFO hitTestInfo
;
281 hitTestInfo
.pt
.x
= pt
.x
;
282 hitTestInfo
.pt
.y
= pt
.y
;
283 int item
= TabCtrl_HitTest( (HWND
) GetHWND(), & hitTestInfo
) ;
286 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
287 flags
|= wxTAB_HITTEST_NOWHERE
;
288 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
289 flags
|= wxTAB_HITTEST_ONICON
;
290 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
291 flags
|= wxTAB_HITTEST_ONLABEL
;
297 bool wxTabCtrl::InsertItem(int item
, const wxString
& text
, int imageId
, void* data
)
301 tcItem
.mask
= TCIF_PARAM
;
302 tcItem
.lParam
= (long) data
;
305 tcItem
.mask
|= TCIF_TEXT
;
306 wxStrcpy(buf
, (const wxChar
*) text
);
307 tcItem
.pszText
= buf
;
308 tcItem
.cchTextMax
= 256;
312 tcItem
.mask
|= TCIF_IMAGE
;
313 tcItem
.iImage
= imageId
;
316 return (TabCtrl_InsertItem( (HWND
) GetHWND(), item
, & tcItem
) != -1);
320 int wxTabCtrl::SetSelection(int item
)
322 return (int) TabCtrl_SetCurSel( (HWND
) GetHWND(), item
);
325 // Set the image list
326 void wxTabCtrl::SetImageList(wxImageList
* imageList
)
328 m_imageList
= imageList
;
329 TabCtrl_SetImageList( (HWND
) GetHWND(), (HIMAGELIST
) imageList
->GetHIMAGELIST() );
332 // Set the text for an item
333 bool wxTabCtrl::SetItemText(int item
, const wxString
& text
)
337 tcItem
.mask
= TCIF_TEXT
;
338 wxStrcpy(buf
, (const wxChar
*) text
);
339 tcItem
.pszText
= buf
;
340 tcItem
.cchTextMax
= 256;
342 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
345 // Set the image for an item
346 bool wxTabCtrl::SetItemImage(int item
, int image
)
349 tcItem
.mask
= TCIF_IMAGE
;
350 tcItem
.iImage
= image
;
352 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
355 // Set the data for an item
356 bool wxTabCtrl::SetItemData(int item
, void* data
)
359 tcItem
.mask
= TCIF_PARAM
;
360 tcItem
.lParam
= (long) data
;
362 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
365 // Set the size for a fixed-width tab control
366 void wxTabCtrl::SetItemSize(const wxSize
& size
)
368 TabCtrl_SetItemSize( (HWND
) GetHWND(), size
.x
, size
.y
);
371 // Set the padding between tabs
372 void wxTabCtrl::SetPadding(const wxSize
& padding
)
374 TabCtrl_SetPadding( (HWND
) GetHWND(), padding
.x
, padding
.y
);
378 // These are the default colors used to map the bitmap colors
379 // to the current system colors
381 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
382 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
383 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
384 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
385 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
386 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
388 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
390 COLORMAP ColorMap
[] = {
391 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
392 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
393 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
394 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
395 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
396 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
399 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
401 for ( n
= 0; n
< NUM_MAPS
; n
++)
403 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
407 HDC hdcMem
= CreateCompatibleDC(NULL
);
411 hbmOld
= SelectObject(hdcMem
, hBitmap
);
414 for ( i
= 0; i
< width
; i
++)
416 for ( j
= 0; j
< height
; j
++)
418 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
420 BYTE red = GetRValue(pixel);
421 BYTE green = GetGValue(pixel);
422 BYTE blue = GetBValue(pixel);
425 for ( k
= 0; k
< NUM_MAPS
; k
++)
427 if ( ColorMap
[k
].from
== pixel
)
429 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
437 SelectObject(hdcMem
, hbmOld
);
438 DeleteObject(hdcMem
);