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__)
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
, WXLPARAM
*result
)
151 wxTabEvent
event(wxEVT_NULL
, m_windowId
);
152 wxEventType eventType
= wxEVT_NULL
;
153 NMHDR
* hdr1
= (NMHDR
*) lParam
;
154 switch ( hdr1
->code
)
157 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGED
;
160 case TCN_SELCHANGING
:
161 eventType
= wxEVT_COMMAND_TAB_SEL_CHANGING
;
167 // if (tool->m_shortHelpString != "")
168 // ttText->lpszText = (char *) (const char *)tool->m_shortHelpString;
172 return wxControl::MSWNotify(wParam
, lParam
, result
);
175 event
.SetEventObject( this );
176 event
.SetEventType(eventType
);
177 event
.SetInt( (int) LOWORD(wParam
) ) ;
179 return ProcessEvent(event
);
182 // Responds to colour changes, and passes event on to children.
183 void wxTabCtrl::OnSysColourChanged(wxSysColourChangedEvent
& event
)
185 m_backgroundColour
= wxColour(GetRValue(GetSysColor(COLOR_BTNFACE
)),
186 GetGValue(GetSysColor(COLOR_BTNFACE
)), GetBValue(GetSysColor(COLOR_BTNFACE
)));
195 // Propagate the event to the non-top-level children
196 wxWindow::OnSysColourChanged(event
);
200 bool wxTabCtrl::DeleteAllItems()
202 return ( TabCtrl_DeleteAllItems( (HWND
) GetHWND() ) != FALSE
);
206 bool wxTabCtrl::DeleteItem(int item
)
208 return ( TabCtrl_DeleteItem( (HWND
) GetHWND(), item
) != FALSE
);
212 int wxTabCtrl::GetSelection() const
214 return (int) TabCtrl_GetCurSel( (HWND
) GetHWND() );
217 // Get the tab with the current keyboard focus
218 int wxTabCtrl::GetCurFocus() const
220 return (int) TabCtrl_GetCurFocus( (HWND
) GetHWND() );
223 // Get the associated image list
224 wxImageList
* wxTabCtrl::GetImageList() const
229 // Get the number of items
230 int wxTabCtrl::GetItemCount() const
232 return (int) TabCtrl_GetItemCount( (HWND
) GetHWND() );
235 // Get the rect corresponding to the tab
236 bool wxTabCtrl::GetItemRect(int item
, wxRect
& wxrect
) const
239 if ( !TabCtrl_GetItemRect( (HWND
) GetHWND(), item
, & rect
) )
243 wxrect
.x
= rect
.left
; wxrect
.y
= rect
.top
;
244 wxrect
.width
= rect
.right
- rect
.left
;
245 wxrect
.height
= rect
.bottom
- rect
.top
;
250 // Get the number of rows
251 int wxTabCtrl::GetRowCount() const
253 return (int) TabCtrl_GetRowCount( (HWND
) GetHWND() );
257 wxString
wxTabCtrl::GetItemText(int item
) const
262 tcItem
.mask
= TCIF_TEXT
;
263 tcItem
.pszText
= buf
;
264 tcItem
.cchTextMax
= 256;
266 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
267 str
= tcItem
.pszText
;
272 // Get the item image
273 int wxTabCtrl::GetItemImage(int item
) const
276 tcItem
.mask
= TCIF_IMAGE
;
278 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
279 return tcItem
.iImage
;
285 void* wxTabCtrl::GetItemData(int item
) const
288 tcItem
.mask
= TCIF_PARAM
;
290 if (TabCtrl_GetItem( (HWND
) GetHWND(), item
, & tcItem
) )
291 return (void*) tcItem
.lParam
;
297 int wxTabCtrl::HitTest(const wxPoint
& pt
, long& flags
)
299 TC_HITTESTINFO hitTestInfo
;
300 hitTestInfo
.pt
.x
= pt
.x
;
301 hitTestInfo
.pt
.y
= pt
.y
;
302 int item
= TabCtrl_HitTest( (HWND
) GetHWND(), & hitTestInfo
) ;
305 if ((hitTestInfo
.flags
& TCHT_NOWHERE
) == TCHT_NOWHERE
)
306 flags
|= wxTAB_HITTEST_NOWHERE
;
307 if ((hitTestInfo
.flags
& TCHT_ONITEMICON
) == TCHT_ONITEMICON
)
308 flags
|= wxTAB_HITTEST_ONICON
;
309 if ((hitTestInfo
.flags
& TCHT_ONITEMLABEL
) == TCHT_ONITEMLABEL
)
310 flags
|= wxTAB_HITTEST_ONLABEL
;
316 bool wxTabCtrl::InsertItem(int item
, const wxString
& text
, int imageId
, void* data
)
320 tcItem
.mask
= TCIF_PARAM
;
321 tcItem
.lParam
= (long) data
;
324 tcItem
.mask
|= TCIF_TEXT
;
325 strcpy(buf
, (const char*) text
);
326 tcItem
.pszText
= buf
;
327 tcItem
.cchTextMax
= 256;
331 tcItem
.mask
|= TCIF_IMAGE
;
332 tcItem
.iImage
= imageId
;
335 return (TabCtrl_InsertItem( (HWND
) GetHWND(), item
, & tcItem
) != -1);
339 int wxTabCtrl::SetSelection(int item
)
341 return (int) TabCtrl_SetCurSel( (HWND
) GetHWND(), item
);
344 // Set the image list
345 void wxTabCtrl::SetImageList(wxImageList
* imageList
)
347 m_imageList
= imageList
;
348 TabCtrl_SetImageList( (HWND
) GetHWND(), (HIMAGELIST
) imageList
->GetHIMAGELIST() );
351 // Set the text for an item
352 bool wxTabCtrl::SetItemText(int item
, const wxString
& text
)
356 tcItem
.mask
= TCIF_TEXT
;
357 strcpy(buf
, (const char*) text
);
358 tcItem
.pszText
= buf
;
359 tcItem
.cchTextMax
= 256;
361 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
364 // Set the image for an item
365 bool wxTabCtrl::SetItemImage(int item
, int image
)
368 tcItem
.mask
= TCIF_IMAGE
;
369 tcItem
.iImage
= image
;
371 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
374 // Set the data for an item
375 bool wxTabCtrl::SetItemData(int item
, void* data
)
378 tcItem
.mask
= TCIF_PARAM
;
379 tcItem
.lParam
= (long) data
;
381 return ( TabCtrl_SetItem( (HWND
) GetHWND(), item
, & tcItem
) != 0 );
384 // Set the size for a fixed-width tab control
385 void wxTabCtrl::SetItemSize(const wxSize
& size
)
387 TabCtrl_SetItemSize( (HWND
) GetHWND(), size
.x
, size
.y
);
390 // Set the padding between tabs
391 void wxTabCtrl::SetPadding(const wxSize
& padding
)
393 TabCtrl_SetPadding( (HWND
) GetHWND(), padding
.x
, padding
.y
);
397 // These are the default colors used to map the bitmap colors
398 // to the current system colors
400 #define BGR_BUTTONTEXT (RGB(000,000,000)) // black
401 #define BGR_BUTTONSHADOW (RGB(128,128,128)) // dark grey
402 #define BGR_BUTTONFACE (RGB(192,192,192)) // bright grey
403 #define BGR_BUTTONHILIGHT (RGB(255,255,255)) // white
404 #define BGR_BACKGROUNDSEL (RGB(255,000,000)) // blue
405 #define BGR_BACKGROUND (RGB(255,000,255)) // magenta
407 void wxMapBitmap(HBITMAP hBitmap
, int width
, int height
)
409 COLORMAP ColorMap
[] = {
410 {BGR_BUTTONTEXT
, COLOR_BTNTEXT
}, // black
411 {BGR_BUTTONSHADOW
, COLOR_BTNSHADOW
}, // dark grey
412 {BGR_BUTTONFACE
, COLOR_BTNFACE
}, // bright grey
413 {BGR_BUTTONHILIGHT
, COLOR_BTNHIGHLIGHT
},// white
414 {BGR_BACKGROUNDSEL
, COLOR_HIGHLIGHT
}, // blue
415 {BGR_BACKGROUND
, COLOR_WINDOW
} // magenta
418 int NUM_MAPS
= (sizeof(ColorMap
)/sizeof(COLORMAP
));
420 for ( n
= 0; n
< NUM_MAPS
; n
++)
422 ColorMap
[n
].to
= ::GetSysColor(ColorMap
[n
].to
);
426 HDC hdcMem
= CreateCompatibleDC(NULL
);
430 hbmOld
= SelectObject(hdcMem
, hBitmap
);
433 for ( i
= 0; i
< width
; i
++)
435 for ( j
= 0; j
< height
; j
++)
437 COLORREF pixel
= ::GetPixel(hdcMem
, i
, j
);
439 BYTE red = GetRValue(pixel);
440 BYTE green = GetGValue(pixel);
441 BYTE blue = GetBValue(pixel);
444 for ( k
= 0; k
< NUM_MAPS
; k
++)
446 if ( ColorMap
[k
].from
== pixel
)
448 /* COLORREF actualPixel = */ ::SetPixel(hdcMem
, i
, j
, ColorMap
[k
].to
);
456 SelectObject(hdcMem
, hbmOld
);
457 DeleteObject(hdcMem
);
464 IMPLEMENT_DYNAMIC_CLASS(wxTabEvent
, wxCommandEvent
)
466 wxTabEvent::wxTabEvent(wxEventType commandType
, int id
):
467 wxCommandEvent(commandType
, id
)