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__)
41 #include "wx/msw/gnuwin32/extra.h"
45 #include "wx/msw/dib.h"
46 #include "wx/msw/tabctrl.h"
48 #include "wx/msw/private.h"
49 #include "wx/msw/imaglist.h"
51 #if !USE_SHARED_LIBRARY
52 IMPLEMENT_DYNAMIC_CLASS(wxTabCtrl
, wxControl
)
54 BEGIN_EVENT_TABLE(wxTabCtrl
, wxControl
)
55 EVT_SYS_COLOUR_CHANGED(wxTabCtrl::OnSysColourChanged
)
59 wxTabCtrl::wxTabCtrl()
64 bool wxTabCtrl::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
65 long style
, const wxString
& name
)
69 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
70 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
71 m_foregroundColour
= *wxBLACK
;
80 m_windowStyle
= style
;
82 SetFont(* (wxTheFontList
->FindOrCreateFont(11, wxSWISS
, wxNORMAL
, wxNORMAL
)));
89 msflags
|= WS_CHILD
| WS_VISIBLE
;
100 m_windowId
= (id
< 0 ? NewControlId() : id
);
103 if (m_windowStyle
& wxTC_MULTILINE
)
104 tabStyle
|= TCS_MULTILINE
;
105 if (m_windowStyle
& wxTC_RIGHTJUSTIFY
)
106 tabStyle
|= TCS_RIGHTJUSTIFY
;
107 if (m_windowStyle
& wxTC_FIXEDWIDTH
)
108 tabStyle
|= TCS_FIXEDWIDTH
;
109 if (m_windowStyle
& wxTC_OWNERDRAW
)
110 tabStyle
|= TCS_OWNERDRAWFIXED
;
112 tabStyle
|= TCS_TOOLTIPS
;
114 // Create the toolbar control.
115 HWND hWndTabCtrl
= CreateWindowEx(0L, // No extended styles.
116 WC_TABCONTROL
, // Class name for the tab control
117 "", // No default text.
118 WS_CHILD
| WS_BORDER
| WS_VISIBLE
| tabStyle
, // Styles and defaults.
119 x
, y
, width
, height
, // Standard size and position.
120 (HWND
) parent
->GetHWND(), // Parent window
121 (HMENU
)m_windowId
, // ID.
122 wxGetInstance(), // Current instance.
123 NULL
); // No class data.
125 m_hWnd
= (WXHWND
) hWndTabCtrl
;
126 if (parent
) parent
->AddChild(this);
128 SubclassWin((WXHWND
) hWndTabCtrl
);
133 wxTabCtrl::~wxTabCtrl()
138 bool wxTabCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
140 wxTabEvent
event(wxEVT_NULL
, m_windowId
);
141 wxEventType eventType
= wxEVT_NULL
;
142 NMHDR
* hdr1
= (NMHDR
*) lParam
;
143 switch ( hdr1
->code
)
146 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGED
;
149 case TCN_SELCHANGING
:
150 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGING
;
156 // if (tool->m_shortHelpString != "")
157 // ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
161 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
164 event
.SetEventObject( this );
165 event
.SetEventType(eventType
);
166 event
.SetInt(idCtrl
) ;
168 return ProcessEvent(event
);
171 // Responds to colour changes, and passes event on to children.
172 void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
174 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
175 GetGValue(GetSysColor(COLOR_BTNFACE
)),
176 GetBValue(GetSysColor(COLOR_BTNFACE
)));
180 // Propagate the event to the non-top-level children
181 wxWindow::OnSysColourChanged(event
);
185 bool wxTabCtrl::DeleteAllItems()
187 return ( TabCtrl_DeleteAllItems( (HWND
) GetHWND() ) != FALSE
);
191 bool wxTabCtrl::DeleteItem(int item
)
193 return ( TabCtrl_DeleteItem( (HWND
) GetHWND(), item
) != FALSE
);
197 int wxTabCtrl::GetSelection() const
199 return (int) TabCtrl_GetCurSel( (HWND
) GetHWND() );
202 // Get the tab with the current keyboard focus
203 int wxTabCtrl::GetCurFocus() const
205 return (int) TabCtrl_GetCurFocus( (HWND
) GetHWND() );
208 // Get the associated image list
209 wxImageList
* wxTabCtrl::GetImageList() const
214 // Get the number of items
215 int wxTabCtrl::GetItemCount() const
217 return (int) TabCtrl_GetItemCount( (HWND
) GetHWND() );
220 // Get the rect corresponding to the tab
221 bool wxTabCtrl::GetItemRect(int item
, wxRect
& wxrect
) const
224 if ( !TabCtrl_GetItemRect( (HWND
) GetHWND(), item
, & rect
) )
228 wxrect
.x
= rect
.left
; wxrect
.y
= rect
.top
;
229 wxrect
.width
= rect
.right
- rect
.left
;
230 wxrect
.height
= rect
.bottom
- rect
.top
;
235 // Get the number of rows
236 int wxTabCtrl::GetRowCount() const
238 return (int) TabCtrl_GetRowCount( (HWND
) GetHWND() );
242 wxString
wxTabCtrl::GetItemText(int item
) const
247 tcItem
.mask
= TCIF_TEXT
;
248 tcItem
.pszText
= buf
;
249 tcItem
.cchTextMax
= 256;
251 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
252 str
= tcItem
.pszText
;
257 // Get the item image
258 int wxTabCtrl::GetItemImage(int item
) const
261 tcItem
.mask
= TCIF_IMAGE
;
263 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
264 return tcItem
.iImage
;
270 void* wxTabCtrl::GetItemData(int item
) const
273 tcItem
.mask
= TCIF_PARAM
;
275 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
276 return (void*) tcItem
.lParam
;
282 int wxTabCtrl::HitTest(const wxPoint
& pt
, long& flags
)
284 TC_HITTESTINFO hitTestInfo
;
285 hitTestInfo
.pt
.x
= pt
.x
;
286 hitTestInfo
.pt
.y
= pt
.y
;
287 int item
= TabCtrl_HitTest( (HWND
) GetHWND(), & hitTestInfo
) ;
290 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
291 flags
|= wxTAB_HITTEST_NOWHERE
;
292 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
293 flags
|= wxTAB_HITTEST_ONICON
;
294 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
295 flags
|= wxTAB_HITTEST_ONLABEL
;
301 bool wxTabCtrl::InsertItem(int item
, const wxString
& text
, int imageId
, void* data
)
305 tcItem
.mask
= TCIF_PARAM
;
306 tcItem
.lParam
= (long) data
;
309 tcItem
.mask
|= TCIF_TEXT
;
310 strcpy(buf
, (const char*) text
);
311 tcItem
.pszText
= buf
;
312 tcItem
.cchTextMax
= 256;
316 tcItem
.mask
|= TCIF_IMAGE
;
317 tcItem
.iImage
= imageId
;
320 return (TabCtrl_InsertItem( (HWND
) GetHWND(), item
, & tcItem
) != -1);
324 int wxTabCtrl::SetSelection(int item
)
326 return (int) TabCtrl_SetCurSel( (HWND
) GetHWND(), item
);
329 // Set the image list
330 void wxTabCtrl::SetImageList(wxImageList
* imageList
)
332 m_imageList
= imageList
;
333 TabCtrl_SetImageList( (HWND
) GetHWND(), (HIMAGELIST
) imageList
->GetHIMAGELIST() );
336 // Set the text for an item
337 bool wxTabCtrl::SetItemText(int item
, const wxString
& text
)
341 tcItem
.mask
= TCIF_TEXT
;
342 strcpy(buf
, (const char*) text
);
343 tcItem
.pszText
= buf
;
344 tcItem
.cchTextMax
= 256;
346 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
349 // Set the image for an item
350 bool wxTabCtrl::SetItemImage(int item
, int image
)
353 tcItem
.mask
= TCIF_IMAGE
;
354 tcItem
.iImage
= image
;
356 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
359 // Set the data for an item
360 bool wxTabCtrl::SetItemData(int item
, void* data
)
363 tcItem
.mask
= TCIF_PARAM
;
364 tcItem
.lParam
= (long) data
;
366 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
369 // Set the size for a fixed-width tab control
370 void wxTabCtrl::SetItemSize(const wxSize
& size
)
372 TabCtrl_SetItemSize( (HWND
) GetHWND(), size
.x
, size
.y
);
375 // Set the padding between tabs
376 void wxTabCtrl::SetPadding(const wxSize
& padding
)
378 TabCtrl_SetPadding( (HWND
) GetHWND(), padding
.x
, padding
.y
);
382 // These are the default colors used to map the bitmap colors
383 // to the current system colors
385 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
386 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
387 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
388 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
389 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
390 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
392 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
394 COLORMAP ColorMap
[] = {
395 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
396 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
397 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
398 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
399 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
400 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
403 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
405 for ( n
= 0; n
< NUM_MAPS
; n
++)
407 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
411 HDC hdcMem
= CreateCompatibleDC(NULL
);
415 hbmOld
= SelectObject(hdcMem
, hBitmap
);
418 for ( i
= 0; i
< width
; i
++)
420 for ( j
= 0; j
< height
; j
++)
422 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
424 BYTE red = GetRValue(pixel);
425 BYTE green = GetGValue(pixel);
426 BYTE blue = GetBValue(pixel);
429 for ( k
= 0; k
< NUM_MAPS
; k
++)
431 if ( ColorMap
[k
].from
== pixel
)
433 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
441 SelectObject(hdcMem
, hbmOld
);
442 DeleteObject(hdcMem
);
449 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent
, wxCommandEvent
)
451 wxTabEvent::wxTabEvent(wxEventType commandType
, int id
):
452 wxCommandEvent(commandType
, id
)