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__)
40 #include "wx/msw/gnuwin32/extra.h"
43 #include "wx/msw/dib.h"
44 #include "wx/msw/tabctrl.h"
46 #include "wx/msw/private.h"
47 #include "wx/msw/imaglist.h"
49 #if !USE_SHARED_LIBRARY
50 IMPLEMENT_DYNAMIC_CLASS(wxTabCtrl
, wxControl
)
52 BEGIN_EVENT_TABLE(wxTabCtrl
, wxControl
)
53 EVT_SIZE(wxTabCtrl::OnSize
)
54 EVT_PAINT(wxTabCtrl::OnPaint
)
55 EVT_KILL_FOCUS(wxTabCtrl::OnKillFocus
)
56 EVT_MOUSE_EVENTS(wxTabCtrl::OnMouseEvent
)
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
;
75 m_defaultForegroundColour
= *wxBLACK
;
76 m_defaultBackgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
77 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
86 m_windowStyle
= style
;
88 SetFont(* (wxTheFontList
->FindOrCreateFont(11, wxSWISS
, wxNORMAL
, wxNORMAL
)));
95 msflags
|= WS_CHILD
| WS_VISIBLE
;
106 m_windowId
= (id
< 0 ? NewControlId() : id
);
109 if (m_windowStyle
& wxTC_MULTILINE
)
110 tabStyle
|= TCS_MULTILINE
;
111 if (m_windowStyle
& wxTC_RIGHTJUSTIFY
)
112 tabStyle
|= TCS_RIGHTJUSTIFY
;
113 if (m_windowStyle
& wxTC_FIXEDWIDTH
)
114 tabStyle
|= TCS_FIXEDWIDTH
;
115 if (m_windowStyle
& wxTC_OWNERDRAW
)
116 tabStyle
|= TCS_OWNERDRAWFIXED
;
118 tabStyle
|= TCS_TOOLTIPS
;
120 // Create the toolbar control.
121 HWND hWndTabCtrl
= CreateWindowEx(0L, // No extended styles.
122 WC_TABCONTROL
, // Class name for the tab control
123 "", // No default text.
124 WS_CHILD
| WS_BORDER
| WS_VISIBLE
| tabStyle
, // Styles and defaults.
125 x
, y
, width
, height
, // Standard size and position.
126 (HWND
) parent
->GetHWND(), // Parent window
127 (HMENU
)m_windowId
, // ID.
128 wxGetInstance(), // Current instance.
129 NULL
); // No class data.
131 m_hWnd
= (WXHWND
) hWndTabCtrl
;
132 if (parent
) parent
->AddChild(this);
134 SubclassWin((WXHWND
) hWndTabCtrl
);
139 wxTabCtrl::~wxTabCtrl()
144 void wxTabCtrl::Command(wxCommandEvent
& event
)
148 bool wxTabCtrl::MSWCommand(const WXUINT cmd
, const WXWORD id
)
153 bool wxTabCtrl::MSWNotify(const WXWPARAM wParam
, const WXLPARAM lParam
)
155 wxTabEvent
event(wxEVT_NULL
, m_windowId
);
156 wxEventType eventType
= wxEVT_NULL
;
157 NMHDR
* hdr1
= (NMHDR
*) lParam
;
158 switch ( hdr1
->code
)
162 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGED
;
163 event
.SetInt( (int) LOWORD(wParam
) ) ;
166 case TCN_SELCHANGING
:
168 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGING
;
169 event
.SetInt( (int) LOWORD(wParam
) ) ;
175 // if (tool->m_shortHelpString != "")
176 // ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
177 return wxControl::MSWNotify(wParam
, lParam
);
182 return wxControl::MSWNotify(wParam
, lParam
);
186 event
.SetEventObject( this );
187 event
.SetEventType(eventType
);
189 if ( !ProcessEvent(event
) )
194 // Responds to colour changes, and passes event on to children.
195 void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
197 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
198 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
199 m_defaultBackgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
200 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
209 // Propagate the event to the non-top-level children
210 wxWindow::OnSysColourChanged(event
);
214 bool wxTabCtrl::DeleteAllItems()
216 return ( TabCtrl_DeleteAllItems( (HWND
) GetHWND() ) != FALSE
);
220 bool wxTabCtrl::DeleteItem(int item
)
222 return ( TabCtrl_DeleteItem( (HWND
) GetHWND(), item
) != FALSE
);
226 int wxTabCtrl::GetSelection() const
228 return (int) TabCtrl_GetCurSel( (HWND
) GetHWND() );
231 // Get the associated image list
232 wxImageList
* wxTabCtrl::GetImageList() const
237 // Get the number of items
238 int wxTabCtrl::GetItemCount() const
240 return (int) TabCtrl_GetItemCount( (HWND
) GetHWND() );
243 // Get the rect corresponding to the tab
244 bool wxTabCtrl::GetItemRect(int item
, wxRect
& wxrect
) const
247 if ( !TabCtrl_GetItemRect( (HWND
) GetHWND(), item
, & rect
) )
251 wxrect
.x
= rect
.left
; wxrect
.y
= rect
.top
;
252 wxrect
.width
= rect
.right
- rect
.left
;
253 wxrect
.height
= rect
.bottom
- rect
.top
;
258 // Get the number of rows
259 int wxTabCtrl::GetRowCount() const
261 return (int) TabCtrl_GetRowCount( (HWND
) GetHWND() );
265 wxString
wxTabCtrl::GetItemText(int item
) const
270 tcItem
.mask
= TCIF_TEXT
;
271 tcItem
.pszText
= buf
;
272 tcItem
.cchTextMax
= 256;
274 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
275 str
= tcItem
.pszText
;
280 // Get the item image
281 int wxTabCtrl::GetItemImage(int item
) const
284 tcItem
.mask
= TCIF_IMAGE
;
286 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
287 return tcItem
.iImage
;
293 void* wxTabCtrl::GetItemData(int item
) const
296 tcItem
.mask
= TCIF_PARAM
;
298 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
299 return (void*) tcItem
.lParam
;
305 int wxTabCtrl::HitTest(const wxPoint
& pt
, long& flags
)
307 TC_HITTESTINFO hitTestInfo
;
308 hitTestInfo
.pt
.x
= pt
.x
;
309 hitTestInfo
.pt
.y
= pt
.y
;
310 int item
= TabCtrl_HitTest( (HWND
) GetHWND(), & hitTestInfo
) ;
313 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
314 flags
|= wxTAB_HITTEST_NOWHERE
;
315 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
316 flags
|= wxTAB_HITTEST_ONICON
;
317 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
318 flags
|= wxTAB_HITTEST_ONLABEL
;
324 bool wxTabCtrl::InsertItem(int item
, const wxString
& text
, int imageId
, void* data
)
328 tcItem
.mask
= TCIF_PARAM
;
329 tcItem
.lParam
= (long) data
;
332 tcItem
.mask
|= TCIF_TEXT
;
333 strcpy(buf
, (const char*) text
);
334 tcItem
.pszText
= buf
;
335 tcItem
.cchTextMax
= 256;
339 tcItem
.mask
|= TCIF_IMAGE
;
340 tcItem
.iImage
= imageId
;
343 return (TabCtrl_InsertItem( (HWND
) GetHWND(), item
, & tcItem
) != -1);
347 int wxTabCtrl::SetSelection(int item
)
349 return (int) TabCtrl_SetCurSel( (HWND
) GetHWND(), item
);
352 // Set the image list
353 void wxTabCtrl::SetImageList(wxImageList
* imageList
)
355 m_imageList
= imageList
;
356 TabCtrl_SetImageList( (HWND
) GetHWND(), (HIMAGELIST
) imageList
->GetHIMAGELIST() );
359 // Set the text for an item
360 bool wxTabCtrl::SetItemText(int item
, const wxString
& text
)
364 tcItem
.mask
= TCIF_TEXT
;
365 strcpy(buf
, (const char*) text
);
366 tcItem
.pszText
= buf
;
367 tcItem
.cchTextMax
= 256;
369 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
372 // Set the image for an item
373 bool wxTabCtrl::SetItemImage(int item
, int image
)
376 tcItem
.mask
= TCIF_IMAGE
;
377 tcItem
.iImage
= image
;
379 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
382 // Set the data for an item
383 bool wxTabCtrl::SetItemData(int item
, void* data
)
386 tcItem
.mask
= TCIF_PARAM
;
387 tcItem
.lParam
= (long) data
;
389 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
392 // Set the size for a fixed-width tab control
393 void wxTabCtrl::SetItemSize(const wxSize
& size
)
395 TabCtrl_SetItemSize( (HWND
) GetHWND(), size
.x
, size
.y
);
398 // Set the padding between tabs
399 void wxTabCtrl::SetPadding(const wxSize
& padding
)
401 TabCtrl_SetPadding( (HWND
) GetHWND(), padding
.x
, padding
.y
);
405 // These are the default colors used to map the bitmap colors
406 // to the current system colors
408 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
409 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
410 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
411 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
412 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
413 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
415 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
417 COLORMAP ColorMap
[] = {
418 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
419 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
420 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
421 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
422 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
423 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
426 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
428 for ( n
= 0; n
< NUM_MAPS
; n
++)
430 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
434 HDC hdcMem
= CreateCompatibleDC(NULL
);
438 hbmOld
= SelectObject(hdcMem
, hBitmap
);
441 for ( i
= 0; i
< width
; i
++)
443 for ( j
= 0; j
< height
; j
++)
445 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
447 BYTE red = GetRValue(pixel);
448 BYTE green = GetGValue(pixel);
449 BYTE blue = GetBValue(pixel);
452 for ( k
= 0; k
< NUM_MAPS
; k
++)
454 if ( ColorMap
[k
].from
== pixel
)
456 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
464 SelectObject(hdcMem
, hbmOld
);
465 DeleteObject(hdcMem
);
472 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent
, wxCommandEvent
)
474 wxTabEvent::wxTabEvent(wxEventType commandType
, int id
):
475 wxCommandEvent(commandType
, id
)