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
;
82 m_windowStyle
= style
;
84 SetFont(* (wxTheFontList
->FindOrCreateFont(11, wxSWISS
, wxNORMAL
, wxNORMAL
)));
91 msflags
|= WS_CHILD
| WS_VISIBLE
;
102 m_windowId
= (id
< 0 ? NewControlId() : id
);
105 if (m_windowStyle
& wxTC_MULTILINE
)
106 tabStyle
|= TCS_MULTILINE
;
107 if (m_windowStyle
& wxTC_RIGHTJUSTIFY
)
108 tabStyle
|= TCS_RIGHTJUSTIFY
;
109 if (m_windowStyle
& wxTC_FIXEDWIDTH
)
110 tabStyle
|= TCS_FIXEDWIDTH
;
111 if (m_windowStyle
& wxTC_OWNERDRAW
)
112 tabStyle
|= TCS_OWNERDRAWFIXED
;
114 tabStyle
|= TCS_TOOLTIPS
;
116 // Create the toolbar control.
117 HWND hWndTabCtrl
= CreateWindowEx(0L, // No extended styles.
118 WC_TABCONTROL
, // Class name for the tab control
119 "", // No default text.
120 WS_CHILD
| WS_BORDER
| WS_VISIBLE
| tabStyle
, // Styles and defaults.
121 x
, y
, width
, height
, // Standard size and position.
122 (HWND
) parent
->GetHWND(), // Parent window
123 (HMENU
)m_windowId
, // ID.
124 wxGetInstance(), // Current instance.
125 NULL
); // No class data.
127 m_hWnd
= (WXHWND
) hWndTabCtrl
;
128 if (parent
) parent
->AddChild(this);
130 SubclassWin((WXHWND
) hWndTabCtrl
);
135 wxTabCtrl::~wxTabCtrl()
140 void wxTabCtrl::Command(wxCommandEvent
& event
)
144 bool wxTabCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
149 bool wxTabCtrl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
)
151 wxTabEvent
event(wxEVT_NULL
, m_windowId
);
152 wxEventType eventType
= wxEVT_NULL
;
153 NMHDR
* hdr1
= (NMHDR
*) lParam
;
154 switch ( hdr1
->code
)
158 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGED
;
159 event
.SetInt( (int) LOWORD(wParam
) ) ;
162 case TCN_SELCHANGING
:
164 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGING
;
165 event
.SetInt( (int) LOWORD(wParam
) ) ;
171 // if (tool->m_shortHelpString != "")
172 // ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
173 return wxControl::MSWNotify(wParam
, lParam
);
178 return wxControl::MSWNotify(wParam
, lParam
);
182 event
.SetEventObject( this );
183 event
.SetEventType(eventType
);
185 if ( !ProcessEvent(event
) )
190 // Responds to colour changes, and passes event on to children.
191 void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
193 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
194 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
203 // Propagate the event to the non-top-level children
204 wxWindow::OnSysColourChanged(event
);
208 bool wxTabCtrl::DeleteAllItems()
210 return ( TabCtrl_DeleteAllItems( (HWND
) GetHWND() ) != FALSE
);
214 bool wxTabCtrl::DeleteItem(int item
)
216 return ( TabCtrl_DeleteItem( (HWND
) GetHWND(), item
) != FALSE
);
220 int wxTabCtrl::GetSelection() const
222 return (int) TabCtrl_GetCurSel( (HWND
) GetHWND() );
225 // Get the tab with the current keyboard focus
226 int wxTabCtrl::GetCurFocus() const
228 return (int) TabCtrl_GetCurFocus( (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
)