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_SIZE(wxTabCtrl::OnSize
)
56 EVT_PAINT(wxTabCtrl::OnPaint
)
57 EVT_KILL_FOCUS(wxTabCtrl::OnKillFocus
)
58 EVT_MOUSE_EVENTS(wxTabCtrl::OnMouseEvent
)
59 EVT_SYS_COLOUR_CHANGED(wxTabCtrl::OnSysColourChanged
)
63 wxTabCtrl::wxTabCtrl()
68 bool wxTabCtrl::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
69 long style
, const wxString
& name
)
73 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
74 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
75 m_foregroundColour
= *wxBLACK
;
84 m_windowStyle
= style
;
86 SetFont(* (wxTheFontList
->FindOrCreateFont(11, wxSWISS
, wxNORMAL
, wxNORMAL
)));
93 msflags
|= WS_CHILD
| WS_VISIBLE
;
104 m_windowId
= (id
< 0 ? NewControlId() : id
);
107 if (m_windowStyle
& wxTC_MULTILINE
)
108 tabStyle
|= TCS_MULTILINE
;
109 if (m_windowStyle
& wxTC_RIGHTJUSTIFY
)
110 tabStyle
|= TCS_RIGHTJUSTIFY
;
111 if (m_windowStyle
& wxTC_FIXEDWIDTH
)
112 tabStyle
|= TCS_FIXEDWIDTH
;
113 if (m_windowStyle
& wxTC_OWNERDRAW
)
114 tabStyle
|= TCS_OWNERDRAWFIXED
;
116 tabStyle
|= TCS_TOOLTIPS
;
118 // Create the toolbar control.
119 HWND hWndTabCtrl
= CreateWindowEx(0L, // No extended styles.
120 WC_TABCONTROL
, // Class name for the tab control
121 "", // No default text.
122 WS_CHILD
| WS_BORDER
| WS_VISIBLE
| tabStyle
, // Styles and defaults.
123 x
, y
, width
, height
, // Standard size and position.
124 (HWND
) parent
->GetHWND(), // Parent window
125 (HMENU
)m_windowId
, // ID.
126 wxGetInstance(), // Current instance.
127 NULL
); // No class data.
129 m_hWnd
= (WXHWND
) hWndTabCtrl
;
130 if (parent
) parent
->AddChild(this);
132 SubclassWin((WXHWND
) hWndTabCtrl
);
137 wxTabCtrl::~wxTabCtrl()
142 void wxTabCtrl::Command(wxCommandEvent
& event
)
146 bool wxTabCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
151 bool wxTabCtrl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
*result
)
153 wxTabEvent
event(wxEVT_NULL
, m_windowId
);
154 wxEventType eventType
= wxEVT_NULL
;
155 NMHDR
* hdr1
= (NMHDR
*) lParam
;
156 switch ( hdr1
->code
)
159 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGED
;
162 case TCN_SELCHANGING
:
163 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGING
;
169 // if (tool->m_shortHelpString != "")
170 // ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
174 return wxControl::MSWNotify(wParam
, lParam
, result
);
177 event
.SetEventObject( this );
178 event
.SetEventType(eventType
);
179 event
.SetInt( (int) LOWORD(wParam
) ) ;
181 return ProcessEvent(event
);
184 // Responds to colour changes, and passes event on to children.
185 void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
187 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
188 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
197 // Propagate the event to the non-top-level children
198 wxWindow::OnSysColourChanged(event
);
202 bool wxTabCtrl::DeleteAllItems()
204 return ( TabCtrl_DeleteAllItems( (HWND
) GetHWND() ) != FALSE
);
208 bool wxTabCtrl::DeleteItem(int item
)
210 return ( TabCtrl_DeleteItem( (HWND
) GetHWND(), item
) != FALSE
);
214 int wxTabCtrl::GetSelection() const
216 return (int) TabCtrl_GetCurSel( (HWND
) GetHWND() );
219 // Get the tab with the current keyboard focus
220 int wxTabCtrl::GetCurFocus() const
222 return (int) TabCtrl_GetCurFocus( (HWND
) GetHWND() );
225 // Get the associated image list
226 wxImageList
* wxTabCtrl::GetImageList() const
231 // Get the number of items
232 int wxTabCtrl::GetItemCount() const
234 return (int) TabCtrl_GetItemCount( (HWND
) GetHWND() );
237 // Get the rect corresponding to the tab
238 bool wxTabCtrl::GetItemRect(int item
, wxRect
& wxrect
) const
241 if ( !TabCtrl_GetItemRect( (HWND
) GetHWND(), item
, & rect
) )
245 wxrect
.x
= rect
.left
; wxrect
.y
= rect
.top
;
246 wxrect
.width
= rect
.right
- rect
.left
;
247 wxrect
.height
= rect
.bottom
- rect
.top
;
252 // Get the number of rows
253 int wxTabCtrl::GetRowCount() const
255 return (int) TabCtrl_GetRowCount( (HWND
) GetHWND() );
259 wxString
wxTabCtrl::GetItemText(int item
) const
264 tcItem
.mask
= TCIF_TEXT
;
265 tcItem
.pszText
= buf
;
266 tcItem
.cchTextMax
= 256;
268 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
269 str
= tcItem
.pszText
;
274 // Get the item image
275 int wxTabCtrl::GetItemImage(int item
) const
278 tcItem
.mask
= TCIF_IMAGE
;
280 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
281 return tcItem
.iImage
;
287 void* wxTabCtrl::GetItemData(int item
) const
290 tcItem
.mask
= TCIF_PARAM
;
292 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
293 return (void*) tcItem
.lParam
;
299 int wxTabCtrl::HitTest(const wxPoint
& pt
, long& flags
)
301 TC_HITTESTINFO hitTestInfo
;
302 hitTestInfo
.pt
.x
= pt
.x
;
303 hitTestInfo
.pt
.y
= pt
.y
;
304 int item
= TabCtrl_HitTest( (HWND
) GetHWND(), & hitTestInfo
) ;
307 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
308 flags
|= wxTAB_HITTEST_NOWHERE
;
309 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
310 flags
|= wxTAB_HITTEST_ONICON
;
311 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
312 flags
|= wxTAB_HITTEST_ONLABEL
;
318 bool wxTabCtrl::InsertItem(int item
, const wxString
& text
, int imageId
, void* data
)
322 tcItem
.mask
= TCIF_PARAM
;
323 tcItem
.lParam
= (long) data
;
326 tcItem
.mask
|= TCIF_TEXT
;
327 strcpy(buf
, (const char*) text
);
328 tcItem
.pszText
= buf
;
329 tcItem
.cchTextMax
= 256;
333 tcItem
.mask
|= TCIF_IMAGE
;
334 tcItem
.iImage
= imageId
;
337 return (TabCtrl_InsertItem( (HWND
) GetHWND(), item
, & tcItem
) != -1);
341 int wxTabCtrl::SetSelection(int item
)
343 return (int) TabCtrl_SetCurSel( (HWND
) GetHWND(), item
);
346 // Set the image list
347 void wxTabCtrl::SetImageList(wxImageList
* imageList
)
349 m_imageList
= imageList
;
350 TabCtrl_SetImageList( (HWND
) GetHWND(), (HIMAGELIST
) imageList
->GetHIMAGELIST() );
353 // Set the text for an item
354 bool wxTabCtrl::SetItemText(int item
, const wxString
& text
)
358 tcItem
.mask
= TCIF_TEXT
;
359 strcpy(buf
, (const char*) text
);
360 tcItem
.pszText
= buf
;
361 tcItem
.cchTextMax
= 256;
363 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
366 // Set the image for an item
367 bool wxTabCtrl::SetItemImage(int item
, int image
)
370 tcItem
.mask
= TCIF_IMAGE
;
371 tcItem
.iImage
= image
;
373 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
376 // Set the data for an item
377 bool wxTabCtrl::SetItemData(int item
, void* data
)
380 tcItem
.mask
= TCIF_PARAM
;
381 tcItem
.lParam
= (long) data
;
383 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
386 // Set the size for a fixed-width tab control
387 void wxTabCtrl::SetItemSize(const wxSize
& size
)
389 TabCtrl_SetItemSize( (HWND
) GetHWND(), size
.x
, size
.y
);
392 // Set the padding between tabs
393 void wxTabCtrl::SetPadding(const wxSize
& padding
)
395 TabCtrl_SetPadding( (HWND
) GetHWND(), padding
.x
, padding
.y
);
399 // These are the default colors used to map the bitmap colors
400 // to the current system colors
402 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
403 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
404 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
405 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
406 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
407 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
409 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
411 COLORMAP ColorMap
[] = {
412 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
413 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
414 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
415 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
416 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
417 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
420 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
422 for ( n
= 0; n
< NUM_MAPS
; n
++)
424 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
428 HDC hdcMem
= CreateCompatibleDC(NULL
);
432 hbmOld
= SelectObject(hdcMem
, hBitmap
);
435 for ( i
= 0; i
< width
; i
++)
437 for ( j
= 0; j
< height
; j
++)
439 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
441 BYTE red = GetRValue(pixel);
442 BYTE green = GetGValue(pixel);
443 BYTE blue = GetBValue(pixel);
446 for ( k
= 0; k
< NUM_MAPS
; k
++)
448 if ( ColorMap
[k
].from
== pixel
)
450 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
458 SelectObject(hdcMem
, hbmOld
);
459 DeleteObject(hdcMem
);
466 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent
, wxCommandEvent
)
468 wxTabEvent::wxTabEvent(wxEventType commandType
, int id
):
469 wxCommandEvent(commandType
, id
)