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(WXUINT cmd
, WXWORD id
)
153 bool wxTabCtrl::MSWNotify(WXWPARAM wParam
, 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 tab with the current keyboard focus
232 int wxTabCtrl::GetCurFocus() const
234 return (int) TabCtrl_GetCurFocus( (HWND
) GetHWND() );
237 // Get the associated image list
238 wxImageList
* wxTabCtrl::GetImageList() const
243 // Get the number of items
244 int wxTabCtrl::GetItemCount() const
246 return (int) TabCtrl_GetItemCount( (HWND
) GetHWND() );
249 // Get the rect corresponding to the tab
250 bool wxTabCtrl::GetItemRect(int item
, wxRect
& wxrect
) const
253 if ( !TabCtrl_GetItemRect( (HWND
) GetHWND(), item
, & rect
) )
257 wxrect
.x
= rect
.left
; wxrect
.y
= rect
.top
;
258 wxrect
.width
= rect
.right
- rect
.left
;
259 wxrect
.height
= rect
.bottom
- rect
.top
;
264 // Get the number of rows
265 int wxTabCtrl::GetRowCount() const
267 return (int) TabCtrl_GetRowCount( (HWND
) GetHWND() );
271 wxString
wxTabCtrl::GetItemText(int item
) const
276 tcItem
.mask
= TCIF_TEXT
;
277 tcItem
.pszText
= buf
;
278 tcItem
.cchTextMax
= 256;
280 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
281 str
= tcItem
.pszText
;
286 // Get the item image
287 int wxTabCtrl::GetItemImage(int item
) const
290 tcItem
.mask
= TCIF_IMAGE
;
292 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
293 return tcItem
.iImage
;
299 void* wxTabCtrl::GetItemData(int item
) const
302 tcItem
.mask
= TCIF_PARAM
;
304 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
305 return (void*) tcItem
.lParam
;
311 int wxTabCtrl::HitTest(const wxPoint
& pt
, long& flags
)
313 TC_HITTESTINFO hitTestInfo
;
314 hitTestInfo
.pt
.x
= pt
.x
;
315 hitTestInfo
.pt
.y
= pt
.y
;
316 int item
= TabCtrl_HitTest( (HWND
) GetHWND(), & hitTestInfo
) ;
319 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
320 flags
|= wxTAB_HITTEST_NOWHERE
;
321 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
322 flags
|= wxTAB_HITTEST_ONICON
;
323 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
324 flags
|= wxTAB_HITTEST_ONLABEL
;
330 bool wxTabCtrl::InsertItem(int item
, const wxString
& text
, int imageId
, void* data
)
334 tcItem
.mask
= TCIF_PARAM
;
335 tcItem
.lParam
= (long) data
;
338 tcItem
.mask
|= TCIF_TEXT
;
339 strcpy(buf
, (const char*) text
);
340 tcItem
.pszText
= buf
;
341 tcItem
.cchTextMax
= 256;
345 tcItem
.mask
|= TCIF_IMAGE
;
346 tcItem
.iImage
= imageId
;
349 return (TabCtrl_InsertItem( (HWND
) GetHWND(), item
, & tcItem
) != -1);
353 int wxTabCtrl::SetSelection(int item
)
355 return (int) TabCtrl_SetCurSel( (HWND
) GetHWND(), item
);
358 // Set the image list
359 void wxTabCtrl::SetImageList(wxImageList
* imageList
)
361 m_imageList
= imageList
;
362 TabCtrl_SetImageList( (HWND
) GetHWND(), (HIMAGELIST
) imageList
->GetHIMAGELIST() );
365 // Set the text for an item
366 bool wxTabCtrl::SetItemText(int item
, const wxString
& text
)
370 tcItem
.mask
= TCIF_TEXT
;
371 strcpy(buf
, (const char*) text
);
372 tcItem
.pszText
= buf
;
373 tcItem
.cchTextMax
= 256;
375 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
378 // Set the image for an item
379 bool wxTabCtrl::SetItemImage(int item
, int image
)
382 tcItem
.mask
= TCIF_IMAGE
;
383 tcItem
.iImage
= image
;
385 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
388 // Set the data for an item
389 bool wxTabCtrl::SetItemData(int item
, void* data
)
392 tcItem
.mask
= TCIF_PARAM
;
393 tcItem
.lParam
= (long) data
;
395 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
398 // Set the size for a fixed-width tab control
399 void wxTabCtrl::SetItemSize(const wxSize
& size
)
401 TabCtrl_SetItemSize( (HWND
) GetHWND(), size
.x
, size
.y
);
404 // Set the padding between tabs
405 void wxTabCtrl::SetPadding(const wxSize
& padding
)
407 TabCtrl_SetPadding( (HWND
) GetHWND(), padding
.x
, padding
.y
);
411 // These are the default colors used to map the bitmap colors
412 // to the current system colors
414 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
415 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
416 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
417 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
418 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
419 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
421 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
423 COLORMAP ColorMap
[] = {
424 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
425 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
426 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
427 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
428 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
429 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
432 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
434 for ( n
= 0; n
< NUM_MAPS
; n
++)
436 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
440 HDC hdcMem
= CreateCompatibleDC(NULL
);
444 hbmOld
= SelectObject(hdcMem
, hBitmap
);
447 for ( i
= 0; i
< width
; i
++)
449 for ( j
= 0; j
< height
; j
++)
451 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
453 BYTE red = GetRValue(pixel);
454 BYTE green = GetGValue(pixel);
455 BYTE blue = GetBValue(pixel);
458 for ( k
= 0; k
< NUM_MAPS
; k
++)
460 if ( ColorMap
[k
].from
== pixel
)
462 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
470 SelectObject(hdcMem
, hbmOld
);
471 DeleteObject(hdcMem
);
478 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent
, wxCommandEvent
)
480 wxTabEvent::wxTabEvent(wxEventType commandType
, int id
):
481 wxCommandEvent(commandType
, id
)