1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "tabctrl.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #if defined(__WIN95__)
29 #include "wx/msw/private.h"
31 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
34 #include "wx/msw/gnuwin32/extra.h"
37 #include "wx/tabctrl.h"
39 #include "wx/msw/imaglist.h"
41 IMPLEMENT_DYNAMIC_CLASS(wxTabCtrl
, wxControl
)
42 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent
, wxNotifyEvent
)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGED
)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TAB_SEL_CHANGING
)
47 BEGIN_EVENT_TABLE(wxTabCtrl
, wxControl
)
48 EVT_SYS_COLOUR_CHANGED(wxTabCtrl::OnSysColourChanged
)
51 wxTabCtrl::wxTabCtrl()
56 bool wxTabCtrl::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
57 long style
, const wxString
& name
)
61 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
62 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
63 m_foregroundColour
= *wxBLACK
;
72 m_windowStyle
= style
;
85 m_windowId
= (id
< 0 ? NewControlId() : id
);
87 long tabStyle
= WS_CHILD
| WS_VISIBLE
;
88 if (m_windowStyle
& wxTC_MULTILINE
)
89 tabStyle
|= TCS_MULTILINE
;
90 if (m_windowStyle
& wxTC_RIGHTJUSTIFY
)
91 tabStyle
|= TCS_RIGHTJUSTIFY
;
92 if (m_windowStyle
& wxTC_FIXEDWIDTH
)
93 tabStyle
|= TCS_FIXEDWIDTH
;
94 if (m_windowStyle
& wxTC_OWNERDRAW
)
95 tabStyle
|= TCS_OWNERDRAWFIXED
;
96 if (m_windowStyle
& wxBORDER
)
97 tabStyle
|= WS_BORDER
;
100 tabStyle
|= TCS_TOOLTIPS
;
103 // Create the toolbar control.
104 HWND hWndTabCtrl
= CreateWindowEx(0L, // No extended styles.
105 WC_TABCONTROL
, // Class name for the tab control
106 wxEmptyString
, // No default text.
107 tabStyle
, // Styles and defaults.
108 x
, y
, width
, height
, // Standard size and position.
109 (HWND
) parent
->GetHWND(), // Parent window
110 (HMENU
)m_windowId
, // ID.
111 wxGetInstance(), // Current instance.
112 NULL
); // No class data.
114 m_hWnd
= (WXHWND
) hWndTabCtrl
;
115 if (parent
) parent
->AddChild(this);
117 SubclassWin((WXHWND
) hWndTabCtrl
);
119 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
124 wxTabCtrl::~wxTabCtrl()
129 bool wxTabCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
131 wxTabEvent
event(wxEVT_NULL
, m_windowId
);
132 wxEventType eventType
wxDUMMY_INITIALIZE(wxEVT_NULL
);
133 NMHDR
* hdr1
= (NMHDR
*) lParam
;
134 switch ( hdr1
->code
)
137 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGED
;
140 case TCN_SELCHANGING
:
141 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGING
;
148 // if (!tool->m_shortHelpString.empty())
149 // ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
153 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
156 event
.SetEventObject( this );
157 event
.SetEventType(eventType
);
158 event
.SetInt(idCtrl
) ;
159 event
.SetSelection(idCtrl
);
161 return ProcessEvent(event
);
164 // Responds to colour changes, and passes event on to children.
165 void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
167 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
168 GetGValue(GetSysColor(COLOR_BTNFACE
)),
169 GetBValue(GetSysColor(COLOR_BTNFACE
)));
173 // Propagate the event to the non-top-level children
174 wxWindow::OnSysColourChanged(event
);
178 bool wxTabCtrl::DeleteAllItems()
180 return ( TabCtrl_DeleteAllItems( (HWND
) GetHWND() ) != FALSE
);
184 bool wxTabCtrl::DeleteItem(int item
)
186 return ( TabCtrl_DeleteItem( (HWND
) GetHWND(), item
) != FALSE
);
190 int wxTabCtrl::GetSelection() const
192 return (int) TabCtrl_GetCurSel( (HWND
) GetHWND() );
195 // Get the tab with the current keyboard focus
196 int wxTabCtrl::GetCurFocus() const
198 return (int) TabCtrl_GetCurFocus( (HWND
) GetHWND() );
201 // Get the associated image list
202 wxImageList
* wxTabCtrl::GetImageList() const
207 // Get the number of items
208 int wxTabCtrl::GetItemCount() const
210 return (int) TabCtrl_GetItemCount( (HWND
) GetHWND() );
213 // Get the rect corresponding to the tab
214 bool wxTabCtrl::GetItemRect(int item
, wxRect
& wxrect
) const
217 if ( !TabCtrl_GetItemRect( (HWND
) GetHWND(), item
, & rect
) )
221 wxrect
.x
= rect
.left
; wxrect
.y
= rect
.top
;
222 wxrect
.width
= rect
.right
- rect
.left
;
223 wxrect
.height
= rect
.bottom
- rect
.top
;
228 // Get the number of rows
229 int wxTabCtrl::GetRowCount() const
231 return (int) TabCtrl_GetRowCount( (HWND
) GetHWND() );
235 wxString
wxTabCtrl::GetItemText(int item
) const
238 wxString
str(wxEmptyString
);
240 tcItem
.mask
= TCIF_TEXT
;
241 tcItem
.pszText
= buf
;
242 tcItem
.cchTextMax
= 256;
244 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
245 str
= tcItem
.pszText
;
250 // Get the item image
251 int wxTabCtrl::GetItemImage(int item
) const
254 tcItem
.mask
= TCIF_IMAGE
;
256 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
257 return tcItem
.iImage
;
263 void* wxTabCtrl::GetItemData(int item
) const
266 tcItem
.mask
= TCIF_PARAM
;
268 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
269 return (void*) tcItem
.lParam
;
275 int wxTabCtrl::HitTest(const wxPoint
& pt
, long& flags
)
277 TC_HITTESTINFO hitTestInfo
;
278 hitTestInfo
.pt
.x
= pt
.x
;
279 hitTestInfo
.pt
.y
= pt
.y
;
280 int item
= TabCtrl_HitTest( (HWND
) GetHWND(), & hitTestInfo
) ;
283 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
284 flags
|= wxTAB_HITTEST_NOWHERE
;
285 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
286 flags
|= wxTAB_HITTEST_ONICON
;
287 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
288 flags
|= wxTAB_HITTEST_ONLABEL
;
294 bool wxTabCtrl::InsertItem(int item
, const wxString
& text
, int imageId
, void* data
)
298 tcItem
.mask
= TCIF_PARAM
;
299 tcItem
.lParam
= (long) data
;
300 if (text
!= wxEmptyString
)
302 tcItem
.mask
|= TCIF_TEXT
;
303 wxStrcpy(buf
, (const wxChar
*) text
);
304 tcItem
.pszText
= buf
;
305 tcItem
.cchTextMax
= 256;
309 tcItem
.mask
|= TCIF_IMAGE
;
310 tcItem
.iImage
= imageId
;
313 return (TabCtrl_InsertItem( (HWND
) GetHWND(), item
, & tcItem
) != -1);
317 int wxTabCtrl::SetSelection(int item
)
319 return (int) TabCtrl_SetCurSel( (HWND
) GetHWND(), item
);
322 // Set the image list
323 void wxTabCtrl::SetImageList(wxImageList
* imageList
)
325 m_imageList
= imageList
;
326 TabCtrl_SetImageList( (HWND
) GetHWND(), (HIMAGELIST
) imageList
->GetHIMAGELIST() );
329 // Set the text for an item
330 bool wxTabCtrl::SetItemText(int item
, const wxString
& text
)
334 tcItem
.mask
= TCIF_TEXT
;
335 wxStrcpy(buf
, (const wxChar
*) text
);
336 tcItem
.pszText
= buf
;
337 tcItem
.cchTextMax
= 256;
339 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
342 // Set the image for an item
343 bool wxTabCtrl::SetItemImage(int item
, int image
)
346 tcItem
.mask
= TCIF_IMAGE
;
347 tcItem
.iImage
= image
;
349 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
352 // Set the data for an item
353 bool wxTabCtrl::SetItemData(int item
, void* data
)
356 tcItem
.mask
= TCIF_PARAM
;
357 tcItem
.lParam
= (long) data
;
359 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
362 // Set the size for a fixed-width tab control
363 void wxTabCtrl::SetItemSize(const wxSize
& size
)
365 TabCtrl_SetItemSize( (HWND
) GetHWND(), size
.x
, size
.y
);
368 // Set the padding between tabs
369 void wxTabCtrl::SetPadding(const wxSize
& padding
)
371 TabCtrl_SetPadding( (HWND
) GetHWND(), padding
.x
, padding
.y
);
375 // These are the default colors used to map the bitmap colors
376 // to the current system colors
378 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
379 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
380 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
381 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
382 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
383 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
385 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
387 COLORMAP ColorMap
[] = {
388 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
389 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
390 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
391 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
392 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
393 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
396 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
398 for ( n
= 0; n
< NUM_MAPS
; n
++)
400 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
404 HDC hdcMem
= CreateCompatibleDC(NULL
);
408 hbmOld
= SelectObject(hdcMem
, hBitmap
);
411 for ( i
= 0; i
< width
; i
++)
413 for ( j
= 0; j
< height
; j
++)
415 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
417 BYTE red = GetRValue(pixel);
418 BYTE green = GetGValue(pixel);
419 BYTE blue = GetBValue(pixel);
422 for ( k
= 0; k
< NUM_MAPS
; k
++)
424 if ( ColorMap
[k
].from
== pixel
)
426 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
434 SelectObject(hdcMem
, hbmOld
);
435 DeleteObject(hdcMem
);