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 BEGIN_EVENT_TABLE(wxTabCtrl
, wxControl
)
50 EVT_SYS_COLOUR_CHANGED(wxTabCtrl::OnSysColourChanged
)
53 wxTabCtrl::wxTabCtrl()
58 bool wxTabCtrl::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
59 long style
, const wxString
& name
)
63 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
64 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
65 m_foregroundColour
= *wxBLACK
;
74 m_windowStyle
= style
;
76 SetFont(* (wxTheFontList
->FindOrCreateFont(11, wxSWISS
, wxNORMAL
, wxNORMAL
)));
83 msflags
|= WS_CHILD
| WS_VISIBLE
;
94 m_windowId
= (id
< 0 ? NewControlId() : id
);
97 if (m_windowStyle
& wxTC_MULTILINE
)
98 tabStyle
|= TCS_MULTILINE
;
99 if (m_windowStyle
& wxTC_RIGHTJUSTIFY
)
100 tabStyle
|= TCS_RIGHTJUSTIFY
;
101 if (m_windowStyle
& wxTC_FIXEDWIDTH
)
102 tabStyle
|= TCS_FIXEDWIDTH
;
103 if (m_windowStyle
& wxTC_OWNERDRAW
)
104 tabStyle
|= TCS_OWNERDRAWFIXED
;
106 tabStyle
|= TCS_TOOLTIPS
;
108 // Create the toolbar control.
109 HWND hWndTabCtrl
= CreateWindowEx(0L, // No extended styles.
110 WC_TABCONTROL
, // Class name for the tab control
111 wxT(""), // No default text.
112 WS_CHILD
| WS_BORDER
| WS_VISIBLE
| tabStyle
, // Styles and defaults.
113 x
, y
, width
, height
, // Standard size and position.
114 (HWND
) parent
->GetHWND(), // Parent window
115 (HMENU
)m_windowId
, // ID.
116 wxGetInstance(), // Current instance.
117 NULL
); // No class data.
119 m_hWnd
= (WXHWND
) hWndTabCtrl
;
120 if (parent
) parent
->AddChild(this);
122 SubclassWin((WXHWND
) hWndTabCtrl
);
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
) ;
162 return ProcessEvent(event
);
165 // Responds to colour changes, and passes event on to children.
166 void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
168 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
169 GetGValue(GetSysColor(COLOR_BTNFACE
)),
170 GetBValue(GetSysColor(COLOR_BTNFACE
)));
174 // Propagate the event to the non-top-level children
175 wxWindow::OnSysColourChanged(event
);
179 bool wxTabCtrl::DeleteAllItems()
181 return ( TabCtrl_DeleteAllItems( (HWND
) GetHWND() ) != FALSE
);
185 bool wxTabCtrl::DeleteItem(int item
)
187 return ( TabCtrl_DeleteItem( (HWND
) GetHWND(), item
) != FALSE
);
191 int wxTabCtrl::GetSelection() const
193 return (int) TabCtrl_GetCurSel( (HWND
) GetHWND() );
196 // Get the tab with the current keyboard focus
197 int wxTabCtrl::GetCurFocus() const
199 return (int) TabCtrl_GetCurFocus( (HWND
) GetHWND() );
202 // Get the associated image list
203 wxImageList
* wxTabCtrl::GetImageList() const
208 // Get the number of items
209 int wxTabCtrl::GetItemCount() const
211 return (int) TabCtrl_GetItemCount( (HWND
) GetHWND() );
214 // Get the rect corresponding to the tab
215 bool wxTabCtrl::GetItemRect(int item
, wxRect
& wxrect
) const
218 if ( !TabCtrl_GetItemRect( (HWND
) GetHWND(), item
, & rect
) )
222 wxrect
.x
= rect
.left
; wxrect
.y
= rect
.top
;
223 wxrect
.width
= rect
.right
- rect
.left
;
224 wxrect
.height
= rect
.bottom
- rect
.top
;
229 // Get the number of rows
230 int wxTabCtrl::GetRowCount() const
232 return (int) TabCtrl_GetRowCount( (HWND
) GetHWND() );
236 wxString
wxTabCtrl::GetItemText(int item
) const
239 wxString
str(wxT(""));
241 tcItem
.mask
= TCIF_TEXT
;
242 tcItem
.pszText
= buf
;
243 tcItem
.cchTextMax
= 256;
245 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
246 str
= tcItem
.pszText
;
251 // Get the item image
252 int wxTabCtrl::GetItemImage(int item
) const
255 tcItem
.mask
= TCIF_IMAGE
;
257 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
258 return tcItem
.iImage
;
264 void* wxTabCtrl::GetItemData(int item
) const
267 tcItem
.mask
= TCIF_PARAM
;
269 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
270 return (void*) tcItem
.lParam
;
276 int wxTabCtrl::HitTest(const wxPoint
& pt
, long& flags
)
278 TC_HITTESTINFO hitTestInfo
;
279 hitTestInfo
.pt
.x
= pt
.x
;
280 hitTestInfo
.pt
.y
= pt
.y
;
281 int item
= TabCtrl_HitTest( (HWND
) GetHWND(), & hitTestInfo
) ;
284 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
285 flags
|= wxTAB_HITTEST_NOWHERE
;
286 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
287 flags
|= wxTAB_HITTEST_ONICON
;
288 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
289 flags
|= wxTAB_HITTEST_ONLABEL
;
295 bool wxTabCtrl::InsertItem(int item
, const wxString
& text
, int imageId
, void* data
)
299 tcItem
.mask
= TCIF_PARAM
;
300 tcItem
.lParam
= (long) data
;
303 tcItem
.mask
|= TCIF_TEXT
;
304 wxStrcpy(buf
, (const wxChar
*) text
);
305 tcItem
.pszText
= buf
;
306 tcItem
.cchTextMax
= 256;
310 tcItem
.mask
|= TCIF_IMAGE
;
311 tcItem
.iImage
= imageId
;
314 return (TabCtrl_InsertItem( (HWND
) GetHWND(), item
, & tcItem
) != -1);
318 int wxTabCtrl::SetSelection(int item
)
320 return (int) TabCtrl_SetCurSel( (HWND
) GetHWND(), item
);
323 // Set the image list
324 void wxTabCtrl::SetImageList(wxImageList
* imageList
)
326 m_imageList
= imageList
;
327 TabCtrl_SetImageList( (HWND
) GetHWND(), (HIMAGELIST
) imageList
->GetHIMAGELIST() );
330 // Set the text for an item
331 bool wxTabCtrl::SetItemText(int item
, const wxString
& text
)
335 tcItem
.mask
= TCIF_TEXT
;
336 wxStrcpy(buf
, (const wxChar
*) text
);
337 tcItem
.pszText
= buf
;
338 tcItem
.cchTextMax
= 256;
340 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
343 // Set the image for an item
344 bool wxTabCtrl::SetItemImage(int item
, int image
)
347 tcItem
.mask
= TCIF_IMAGE
;
348 tcItem
.iImage
= image
;
350 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
353 // Set the data for an item
354 bool wxTabCtrl::SetItemData(int item
, void* data
)
357 tcItem
.mask
= TCIF_PARAM
;
358 tcItem
.lParam
= (long) data
;
360 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
363 // Set the size for a fixed-width tab control
364 void wxTabCtrl::SetItemSize(const wxSize
& size
)
366 TabCtrl_SetItemSize( (HWND
) GetHWND(), size
.x
, size
.y
);
369 // Set the padding between tabs
370 void wxTabCtrl::SetPadding(const wxSize
& padding
)
372 TabCtrl_SetPadding( (HWND
) GetHWND(), padding
.x
, padding
.y
);
376 // These are the default colors used to map the bitmap colors
377 // to the current system colors
379 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
380 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
381 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
382 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
383 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
384 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
386 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
388 COLORMAP ColorMap
[] = {
389 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
390 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
391 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
392 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
393 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
394 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
397 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
399 for ( n
= 0; n
< NUM_MAPS
; n
++)
401 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
405 HDC hdcMem
= CreateCompatibleDC(NULL
);
409 hbmOld
= SelectObject(hdcMem
, hBitmap
);
412 for ( i
= 0; i
< width
; i
++)
414 for ( j
= 0; j
< height
; j
++)
416 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
418 BYTE red = GetRValue(pixel);
419 BYTE green = GetGValue(pixel);
420 BYTE blue = GetBValue(pixel);
423 for ( k
= 0; k
< NUM_MAPS
; k
++)
425 if ( ColorMap
[k
].from
== pixel
)
427 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
435 SelectObject(hdcMem
, hbmOld
);
436 DeleteObject(hdcMem
);
443 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent
, wxCommandEvent
)
445 wxTabEvent::wxTabEvent(wxEventType commandType
, int id
):
446 wxCommandEvent(commandType
, id
)