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__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
41 #ifndef wxUSE_NORLANDER_HEADERS
42 #include "wx/msw/gnuwin32/extra.h"
47 #include "wx/msw/dib.h"
48 #include "wx/msw/tabctrl.h"
50 #include "wx/msw/private.h"
51 #include "wx/msw/imaglist.h"
53 #if !USE_SHARED_LIBRARY
54 IMPLEMENT_DYNAMIC_CLASS(wxTabCtrl
, wxControl
)
56 BEGIN_EVENT_TABLE(wxTabCtrl
, wxControl
)
57 EVT_SYS_COLOUR_CHANGED(wxTabCtrl::OnSysColourChanged
)
61 wxTabCtrl::wxTabCtrl()
66 bool wxTabCtrl::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
67 long style
, const wxString
& name
)
71 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
72 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
73 m_foregroundColour
= *wxBLACK
;
82 m_windowStyle
= style
;
84 SetFont(* (wxTheFontList
->FindOrCreateFont(11, wxSWISS
, wxNORMAL
, wxNORMAL
)));
91 msflags
|= WS_CHILD
| WS_VISIBLE
;
102 m_windowId
= (id
< 0 ? NewControlId() : id
);
105 if (m_windowStyle
& wxTC_MULTILINE
)
106 tabStyle
|= TCS_MULTILINE
;
107 if (m_windowStyle
& wxTC_RIGHTJUSTIFY
)
108 tabStyle
|= TCS_RIGHTJUSTIFY
;
109 if (m_windowStyle
& wxTC_FIXEDWIDTH
)
110 tabStyle
|= TCS_FIXEDWIDTH
;
111 if (m_windowStyle
& wxTC_OWNERDRAW
)
112 tabStyle
|= TCS_OWNERDRAWFIXED
;
114 tabStyle
|= TCS_TOOLTIPS
;
116 // Create the toolbar control.
117 HWND hWndTabCtrl
= CreateWindowEx(0L, // No extended styles.
118 WC_TABCONTROL
, // Class name for the tab control
119 _T(""), // No default text.
120 WS_CHILD
| WS_BORDER
| WS_VISIBLE
| tabStyle
, // Styles and defaults.
121 x
, y
, width
, height
, // Standard size and position.
122 (HWND
) parent
->GetHWND(), // Parent window
123 (HMENU
)m_windowId
, // ID.
124 wxGetInstance(), // Current instance.
125 NULL
); // No class data.
127 m_hWnd
= (WXHWND
) hWndTabCtrl
;
128 if (parent
) parent
->AddChild(this);
130 SubclassWin((WXHWND
) hWndTabCtrl
);
135 wxTabCtrl::~wxTabCtrl()
140 bool wxTabCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
142 wxTabEvent
event(wxEVT_NULL
, m_windowId
);
143 wxEventType eventType
= wxEVT_NULL
;
144 NMHDR
* hdr1
= (NMHDR
*) lParam
;
145 switch ( hdr1
->code
)
148 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGED
;
151 case TCN_SELCHANGING
:
152 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGING
;
158 // if (tool->m_shortHelpString != "")
159 // ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
163 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
166 event
.SetEventObject( this );
167 event
.SetEventType(eventType
);
168 event
.SetInt(idCtrl
) ;
170 return ProcessEvent(event
);
173 // Responds to colour changes, and passes event on to children.
174 void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
176 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
177 GetGValue(GetSysColor(COLOR_BTNFACE
)),
178 GetBValue(GetSysColor(COLOR_BTNFACE
)));
182 // Propagate the event to the non-top-level children
183 wxWindow::OnSysColourChanged(event
);
187 bool wxTabCtrl::DeleteAllItems()
189 return ( TabCtrl_DeleteAllItems( (HWND
) GetHWND() ) != FALSE
);
193 bool wxTabCtrl::DeleteItem(int item
)
195 return ( TabCtrl_DeleteItem( (HWND
) GetHWND(), item
) != FALSE
);
199 int wxTabCtrl::GetSelection() const
201 return (int) TabCtrl_GetCurSel( (HWND
) GetHWND() );
204 // Get the tab with the current keyboard focus
205 int wxTabCtrl::GetCurFocus() const
207 return (int) TabCtrl_GetCurFocus( (HWND
) GetHWND() );
210 // Get the associated image list
211 wxImageList
* wxTabCtrl::GetImageList() const
216 // Get the number of items
217 int wxTabCtrl::GetItemCount() const
219 return (int) TabCtrl_GetItemCount( (HWND
) GetHWND() );
222 // Get the rect corresponding to the tab
223 bool wxTabCtrl::GetItemRect(int item
, wxRect
& wxrect
) const
226 if ( !TabCtrl_GetItemRect( (HWND
) GetHWND(), item
, & rect
) )
230 wxrect
.x
= rect
.left
; wxrect
.y
= rect
.top
;
231 wxrect
.width
= rect
.right
- rect
.left
;
232 wxrect
.height
= rect
.bottom
- rect
.top
;
237 // Get the number of rows
238 int wxTabCtrl::GetRowCount() const
240 return (int) TabCtrl_GetRowCount( (HWND
) GetHWND() );
244 wxString
wxTabCtrl::GetItemText(int item
) const
247 wxString
str(_T(""));
249 tcItem
.mask
= TCIF_TEXT
;
250 tcItem
.pszText
= buf
;
251 tcItem
.cchTextMax
= 256;
253 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
254 str
= tcItem
.pszText
;
259 // Get the item image
260 int wxTabCtrl::GetItemImage(int item
) const
263 tcItem
.mask
= TCIF_IMAGE
;
265 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
266 return tcItem
.iImage
;
272 void* wxTabCtrl::GetItemData(int item
) const
275 tcItem
.mask
= TCIF_PARAM
;
277 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
278 return (void*) tcItem
.lParam
;
284 int wxTabCtrl::HitTest(const wxPoint
& pt
, long& flags
)
286 TC_HITTESTINFO hitTestInfo
;
287 hitTestInfo
.pt
.x
= pt
.x
;
288 hitTestInfo
.pt
.y
= pt
.y
;
289 int item
= TabCtrl_HitTest( (HWND
) GetHWND(), & hitTestInfo
) ;
292 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
293 flags
|= wxTAB_HITTEST_NOWHERE
;
294 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
295 flags
|= wxTAB_HITTEST_ONICON
;
296 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
297 flags
|= wxTAB_HITTEST_ONLABEL
;
303 bool wxTabCtrl::InsertItem(int item
, const wxString
& text
, int imageId
, void* data
)
307 tcItem
.mask
= TCIF_PARAM
;
308 tcItem
.lParam
= (long) data
;
311 tcItem
.mask
|= TCIF_TEXT
;
312 wxStrcpy(buf
, (const wxChar
*) text
);
313 tcItem
.pszText
= buf
;
314 tcItem
.cchTextMax
= 256;
318 tcItem
.mask
|= TCIF_IMAGE
;
319 tcItem
.iImage
= imageId
;
322 return (TabCtrl_InsertItem( (HWND
) GetHWND(), item
, & tcItem
) != -1);
326 int wxTabCtrl::SetSelection(int item
)
328 return (int) TabCtrl_SetCurSel( (HWND
) GetHWND(), item
);
331 // Set the image list
332 void wxTabCtrl::SetImageList(wxImageList
* imageList
)
334 m_imageList
= imageList
;
335 TabCtrl_SetImageList( (HWND
) GetHWND(), (HIMAGELIST
) imageList
->GetHIMAGELIST() );
338 // Set the text for an item
339 bool wxTabCtrl::SetItemText(int item
, const wxString
& text
)
343 tcItem
.mask
= TCIF_TEXT
;
344 wxStrcpy(buf
, (const wxChar
*) text
);
345 tcItem
.pszText
= buf
;
346 tcItem
.cchTextMax
= 256;
348 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
351 // Set the image for an item
352 bool wxTabCtrl::SetItemImage(int item
, int image
)
355 tcItem
.mask
= TCIF_IMAGE
;
356 tcItem
.iImage
= image
;
358 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
361 // Set the data for an item
362 bool wxTabCtrl::SetItemData(int item
, void* data
)
365 tcItem
.mask
= TCIF_PARAM
;
366 tcItem
.lParam
= (long) data
;
368 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
371 // Set the size for a fixed-width tab control
372 void wxTabCtrl::SetItemSize(const wxSize
& size
)
374 TabCtrl_SetItemSize( (HWND
) GetHWND(), size
.x
, size
.y
);
377 // Set the padding between tabs
378 void wxTabCtrl::SetPadding(const wxSize
& padding
)
380 TabCtrl_SetPadding( (HWND
) GetHWND(), padding
.x
, padding
.y
);
384 // These are the default colors used to map the bitmap colors
385 // to the current system colors
387 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
388 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
389 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
390 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
391 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
392 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
394 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
396 COLORMAP ColorMap
[] = {
397 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
398 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
399 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
400 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
401 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
402 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
405 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
407 for ( n
= 0; n
< NUM_MAPS
; n
++)
409 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
413 HDC hdcMem
= CreateCompatibleDC(NULL
);
417 hbmOld
= SelectObject(hdcMem
, hBitmap
);
420 for ( i
= 0; i
< width
; i
++)
422 for ( j
= 0; j
< height
; j
++)
424 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
426 BYTE red = GetRValue(pixel);
427 BYTE green = GetGValue(pixel);
428 BYTE blue = GetBValue(pixel);
431 for ( k
= 0; k
< NUM_MAPS
; k
++)
433 if ( ColorMap
[k
].from
== pixel
)
435 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
443 SelectObject(hdcMem
, hbmOld
);
444 DeleteObject(hdcMem
);
451 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent
, wxCommandEvent
)
453 wxTabEvent::wxTabEvent(wxEventType commandType
, int id
):
454 wxCommandEvent(commandType
, id
)