Don Kelly
[wine.git] / dlls / comctl32 / tab.c
blob760d73d1d1ca7c4154348e9dacefe037ce6e8bf9
1 /*
2 * Tab control
4 * Copyright 1998 Anders Carlsson
5 * Copyright 1999 Alex Priem <alexp@sci.kun.nl>
6 * Copyright 1999 Francis Beaudet
8 * TODO:
9 * Image list support
10 * Multiline support
11 * Unicode support
14 #include <string.h>
16 #include "winbase.h"
17 #include "commctrl.h"
18 #include "tab.h"
19 #include "debugtools.h"
20 #include "cache.h"
21 #include "win.h"
23 DEFAULT_DEBUG_CHANNEL(tab)
25 /******************************************************************************
26 * Positioning constants
28 #define SELECTED_TAB_OFFSET 2
29 #define HORIZONTAL_ITEM_PADDING 5
30 #define VERTICAL_ITEM_PADDING 3
31 #define ROUND_CORNER_SIZE 2
32 #define FOCUS_RECT_HOFFSET 2
33 #define FOCUS_RECT_VOFFSET 1
34 #define DISPLAY_AREA_PADDINGX 2
35 #define DISPLAY_AREA_PADDINGY 2
36 #define CONTROL_BORDER_SIZEX 2
37 #define CONTROL_BORDER_SIZEY 2
38 #define BUTTON_SPACINGX 10
39 #define DEFAULT_TAB_WIDTH 96
41 #define TAB_GetInfoPtr(hwnd) ((TAB_INFO *)GetWindowLongA(hwnd,0))
43 /******************************************************************************
44 * Prototypes
46 static void TAB_Refresh (HWND hwnd, HDC hdc);
47 static void TAB_InvalidateTabArea(HWND hwnd, TAB_INFO* infoPtr);
48 static void TAB_EnsureSelectionVisible(HWND hwnd, TAB_INFO* infoPtr);
50 static BOOL
51 TAB_SendSimpleNotify (HWND hwnd, UINT code)
53 NMHDR nmhdr;
55 nmhdr.hwndFrom = hwnd;
56 nmhdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
57 nmhdr.code = code;
59 return (BOOL) SendMessageA (GetParent (hwnd), WM_NOTIFY,
60 (WPARAM) nmhdr.idFrom, (LPARAM) &nmhdr);
64 static VOID
65 TAB_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
66 WPARAM wParam, LPARAM lParam)
68 MSG msg;
70 msg.hwnd = hwndMsg;
71 msg.message = uMsg;
72 msg.wParam = wParam;
73 msg.lParam = lParam;
74 msg.time = GetMessageTime ();
75 msg.pt.x = LOWORD(GetMessagePos ());
76 msg.pt.y = HIWORD(GetMessagePos ());
78 SendMessageA (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
83 static LRESULT
84 TAB_GetCurSel (HWND hwnd)
86 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
88 return infoPtr->iSelected;
91 static LRESULT
92 TAB_GetCurFocus (HWND hwnd)
94 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
96 return infoPtr->uFocus;
99 static LRESULT
100 TAB_GetToolTips (HWND hwnd, WPARAM wParam, LPARAM lParam)
102 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
104 if (infoPtr == NULL) return 0;
105 return infoPtr->hwndToolTip;
109 static LRESULT
110 TAB_SetCurSel (HWND hwnd,WPARAM wParam)
112 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
113 INT iItem=(INT) wParam;
114 INT prevItem;
116 prevItem=-1;
117 if ((iItem >= 0) && (iItem < infoPtr->uNumItem)) {
118 prevItem=infoPtr->iSelected;
119 infoPtr->iSelected=iItem;
120 TAB_EnsureSelectionVisible(hwnd, infoPtr);
121 TAB_InvalidateTabArea(hwnd, infoPtr);
123 return prevItem;
126 static LRESULT
127 TAB_SetCurFocus (HWND hwnd,WPARAM wParam)
129 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
130 INT iItem=(INT) wParam;
132 if ((iItem < 0) || (iItem >= infoPtr->uNumItem)) return 0;
134 infoPtr->uFocus=iItem;
135 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BUTTONS) {
136 FIXME("Should set input focus\n");
137 } else {
138 if (infoPtr->iSelected != iItem) {
139 if (TAB_SendSimpleNotify(hwnd, TCN_SELCHANGING)!=TRUE) {
140 infoPtr->iSelected = iItem;
141 TAB_SendSimpleNotify(hwnd, TCN_SELCHANGE);
143 TAB_EnsureSelectionVisible(hwnd, infoPtr);
144 TAB_InvalidateTabArea(hwnd, infoPtr);
148 return 0;
151 static LRESULT
152 TAB_SetToolTips (HWND hwnd, WPARAM wParam, LPARAM lParam)
154 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
156 if (infoPtr == NULL) return 0;
157 infoPtr->hwndToolTip = (HWND)wParam;
158 return 0;
161 /******************************************************************************
162 * TAB_InternalGetItemRect
164 * This method will calculate the rectangle representing a given tab item in
165 * client coordinates. This method takes scrolling into account.
167 * This method returns TRUE if the item is visible in the window and FALSE
168 * if it is completely outside the client area.
170 static BOOL TAB_InternalGetItemRect(
171 HWND hwnd,
172 TAB_INFO* infoPtr,
173 INT itemIndex,
174 RECT* itemRect,
175 RECT* selectedRect)
177 RECT tmpItemRect;
180 * Perform a sanity check and a trivial visibility check.
182 if ( (infoPtr->uNumItem <=0) ||
183 (itemIndex >= infoPtr->uNumItem) ||
184 (itemIndex < infoPtr->leftmostVisible) )
185 return FALSE;
188 * Avoid special cases in this procedure by assigning the "out"
189 * parameters if the caller didn't supply them
191 if (itemRect==NULL)
192 itemRect = &tmpItemRect;
195 * Retrieve the unmodified item rect.
197 *itemRect = infoPtr->items[itemIndex].rect;
200 * "scroll" it to make sure the item at the very left of the
201 * tab control is the leftmost visible tab.
203 OffsetRect(itemRect,
204 -infoPtr->items[infoPtr->leftmostVisible].rect.left,
208 * Move the rectangle so the first item is slightly offset from
209 * the left of the tab control.
211 OffsetRect(itemRect,
212 SELECTED_TAB_OFFSET,
217 * Now, calculate the position of the item as if it were selected.
219 if (selectedRect!=NULL)
221 CopyRect(selectedRect, itemRect);
224 * The rectangle of a selected item is a bit wider.
226 InflateRect(selectedRect, SELECTED_TAB_OFFSET, 0);
229 * If it also a bit higher.
231 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BOTTOM)
233 selectedRect->top -=2; /* the border is thicker on the bottom */
234 selectedRect->bottom +=SELECTED_TAB_OFFSET;
236 else
238 selectedRect->top -=SELECTED_TAB_OFFSET;
239 selectedRect->bottom+=1;
243 return TRUE;
246 static BOOL TAB_GetItemRect(HWND hwnd, WPARAM wParam, LPARAM lParam)
248 return TAB_InternalGetItemRect(hwnd, TAB_GetInfoPtr(hwnd), (INT)wParam,
249 (LPRECT)lParam, (LPRECT)NULL);
252 /******************************************************************************
253 * TAB_KeyUp
255 * This method is called to handle keyboard input
257 static LRESULT TAB_KeyUp(
258 HWND hwnd,
259 WPARAM keyCode)
261 TAB_INFO* infoPtr = TAB_GetInfoPtr(hwnd);
262 int newItem = -1;
264 switch (keyCode)
266 case VK_LEFT:
267 newItem = infoPtr->uFocus-1;
268 break;
269 case VK_RIGHT:
270 newItem = infoPtr->uFocus+1;
271 break;
275 * If we changed to a valid item, change the selection
277 if ( (newItem >= 0) &&
278 (newItem < infoPtr->uNumItem) &&
279 (infoPtr->uFocus != newItem) )
281 if (!TAB_SendSimpleNotify(hwnd, TCN_SELCHANGING))
283 infoPtr->iSelected = newItem;
284 infoPtr->uFocus = newItem;
285 TAB_SendSimpleNotify(hwnd, TCN_SELCHANGE);
287 TAB_EnsureSelectionVisible(hwnd, infoPtr);
288 TAB_InvalidateTabArea(hwnd, infoPtr);
292 return 0;
295 /******************************************************************************
296 * TAB_FocusChanging
298 * This method is called whenever the focus goes in or out of this control
299 * it is used to update the visual state of the control.
301 static LRESULT TAB_FocusChanging(
302 HWND hwnd,
303 UINT uMsg,
304 WPARAM wParam,
305 LPARAM lParam)
307 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
308 RECT selectedRect;
309 BOOL isVisible;
312 * Get the rectangle for the item.
314 isVisible = TAB_InternalGetItemRect(hwnd,
315 infoPtr,
316 infoPtr->uFocus,
317 NULL,
318 &selectedRect);
321 * If the rectangle is not completely invisible, invalidate that
322 * portion of the window.
324 if (isVisible)
326 InvalidateRect(hwnd, &selectedRect, TRUE);
330 * Don't otherwise disturb normal behavior.
332 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
335 static HWND TAB_InternalHitTest (
336 HWND hwnd,
337 TAB_INFO* infoPtr,
338 POINT pt,
339 UINT* flags)
342 RECT rect;
343 int iCount;
345 for (iCount = 0; iCount < infoPtr->uNumItem; iCount++)
347 TAB_InternalGetItemRect(hwnd,
348 infoPtr,
349 iCount,
350 &rect,
351 NULL);
353 if (PtInRect (&rect, pt))
355 *flags = TCHT_ONITEM;
356 return iCount;
360 *flags=TCHT_NOWHERE;
361 return -1;
364 static LRESULT
365 TAB_HitTest (HWND hwnd, WPARAM wParam, LPARAM lParam)
367 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
368 LPTCHITTESTINFO lptest=(LPTCHITTESTINFO) lParam;
370 return TAB_InternalHitTest (hwnd, infoPtr,lptest->pt,&lptest->flags);
374 static LRESULT
375 TAB_LButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
377 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
378 POINT pt;
379 INT newItem,dummy;
381 if (infoPtr->hwndToolTip)
382 TAB_RelayEvent (infoPtr->hwndToolTip, hwnd,
383 WM_LBUTTONDOWN, wParam, lParam);
385 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_FOCUSONBUTTONDOWN ) {
386 SetFocus (hwnd);
389 if (infoPtr->hwndToolTip)
390 TAB_RelayEvent (infoPtr->hwndToolTip, hwnd,
391 WM_LBUTTONDOWN, wParam, lParam);
393 pt.x = (INT)LOWORD(lParam);
394 pt.y = (INT)HIWORD(lParam);
396 newItem=TAB_InternalHitTest (hwnd, infoPtr,pt,&dummy);
398 TRACE("On Tab, item %d\n", newItem);
400 if ( (newItem!=-1) &&
401 (infoPtr->iSelected != newItem) )
403 if (TAB_SendSimpleNotify(hwnd, TCN_SELCHANGING)!=TRUE)
405 infoPtr->iSelected = newItem;
406 infoPtr->uFocus = newItem;
407 TAB_SendSimpleNotify(hwnd, TCN_SELCHANGE);
409 TAB_EnsureSelectionVisible(hwnd, infoPtr);
411 TAB_InvalidateTabArea(hwnd, infoPtr);
414 return 0;
417 static LRESULT
418 TAB_LButtonUp (HWND hwnd, WPARAM wParam, LPARAM lParam)
420 TAB_SendSimpleNotify(hwnd, NM_CLICK);
422 return 0;
425 static LRESULT
426 TAB_RButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
428 TAB_SendSimpleNotify(hwnd, NM_RCLICK);
429 return 0;
432 static LRESULT
433 TAB_MouseMove (HWND hwnd, WPARAM wParam, LPARAM lParam)
435 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
437 if (infoPtr->hwndToolTip)
438 TAB_RelayEvent (infoPtr->hwndToolTip, hwnd,
439 WM_LBUTTONDOWN, wParam, lParam);
440 return 0;
443 /******************************************************************************
444 * TAB_AdjustRect
446 * Calculates the tab control's display area given the windows rectangle or
447 * the window rectangle given the requested display rectangle.
449 static LRESULT TAB_AdjustRect(
450 HWND hwnd,
451 WPARAM fLarger,
452 LPRECT prc)
454 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
456 if (fLarger)
459 * Go from display rectangle
463 * Add the height of the tabs.
465 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BOTTOM)
466 prc->bottom += infoPtr->tabHeight;
467 else
468 prc->top -= infoPtr->tabHeight;
471 * Inflate the rectangle for the padding
473 InflateRect(prc, DISPLAY_AREA_PADDINGX, DISPLAY_AREA_PADDINGY);
476 * Inflate for the border
478 InflateRect(prc, CONTROL_BORDER_SIZEX, CONTROL_BORDER_SIZEX);
480 else
483 * Go from window rectangle.
487 * Deflate the rectangle for the border
489 InflateRect(prc, -CONTROL_BORDER_SIZEX, -CONTROL_BORDER_SIZEX);
492 * Deflate the rectangle for the padding
494 InflateRect(prc, -DISPLAY_AREA_PADDINGX, -DISPLAY_AREA_PADDINGY);
497 * Remove the height of the tabs.
499 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BOTTOM)
500 prc->bottom -= infoPtr->tabHeight;
501 else
502 prc->top += infoPtr->tabHeight;
506 return 0;
509 /******************************************************************************
510 * TAB_OnHScroll
512 * This method will handle the notification from the scroll control and
513 * perform the scrolling operation on the tab control.
515 static LRESULT TAB_OnHScroll(
516 HWND hwnd,
517 int nScrollCode,
518 int nPos,
519 HWND hwndScroll)
521 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
523 if(nScrollCode == SB_THUMBPOSITION && nPos != infoPtr->leftmostVisible)
525 if(nPos < infoPtr->leftmostVisible)
526 infoPtr->leftmostVisible--;
527 else
528 infoPtr->leftmostVisible++;
530 TAB_InvalidateTabArea(hwnd, infoPtr);
531 SendMessageA(infoPtr->hwndUpDown, UDM_SETPOS, 0,
532 MAKELONG(infoPtr->leftmostVisible, 0));
535 return 0;
538 /******************************************************************************
539 * TAB_SetupScroling
541 * This method will check the current scrolling state and make sure the
542 * scrolling control is displayed (or not).
544 static void TAB_SetupScrolling(
545 HWND hwnd,
546 TAB_INFO* infoPtr,
547 const RECT* clientRect)
549 INT maxRange = 0;
550 if (infoPtr->needsScrolling)
552 RECT controlPos;
553 INT vsize, tabwidth;
556 * Calculate the position of the scroll control.
558 controlPos.right = clientRect->right;
559 controlPos.left = controlPos.right - 2*GetSystemMetrics(SM_CXHSCROLL);
561 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BOTTOM)
563 controlPos.top = clientRect->bottom - infoPtr->tabHeight;
564 controlPos.bottom = controlPos.top + GetSystemMetrics(SM_CYHSCROLL);
566 else
568 controlPos.bottom = clientRect->top + infoPtr->tabHeight;
569 controlPos.top = controlPos.bottom - GetSystemMetrics(SM_CYHSCROLL);
573 * If we don't have a scroll control yet, we want to create one.
574 * If we have one, we want to make sure it's positioned right.
576 if (infoPtr->hwndUpDown==0)
578 infoPtr->hwndUpDown = CreateWindowA("msctls_updown32",
580 WS_VISIBLE | WS_CHILD | UDS_HORZ,
581 controlPos.left, controlPos.top,
582 controlPos.right - controlPos.left,
583 controlPos.bottom - controlPos.top,
584 hwnd,
585 (HMENU)NULL,
586 (HINSTANCE)NULL,
587 NULL);
589 else
591 SetWindowPos(infoPtr->hwndUpDown,
592 (HWND)NULL,
593 controlPos.left, controlPos.top,
594 controlPos.right - controlPos.left,
595 controlPos.bottom - controlPos.top,
596 SWP_SHOWWINDOW | SWP_NOZORDER);
599 /* Now calculate upper limit of the updown control range.
600 * We do this by calculating how many tabs will be offscreen when the
601 * last tab is visible.
603 if(infoPtr->uNumItem)
605 vsize = clientRect->right - (controlPos.right - controlPos.left + 1);
606 maxRange = infoPtr->uNumItem;
607 tabwidth = infoPtr->items[maxRange-1].rect.right;
609 for(; maxRange > 0; maxRange--)
611 if(tabwidth - infoPtr->items[maxRange - 1].rect.left > vsize)
612 break;
615 if(maxRange == infoPtr->uNumItem)
616 maxRange--;
619 else
622 * If we once had a scroll control... hide it.
624 if (infoPtr->hwndUpDown!=0)
626 ShowWindow(infoPtr->hwndUpDown, SW_HIDE);
629 if (infoPtr->hwndUpDown)
630 SendMessageA(infoPtr->hwndUpDown, UDM_SETRANGE32, 0, maxRange);
633 /******************************************************************************
634 * TAB_SetItemBounds
636 * This method will calculate the position rectangles of all the items in the
637 * control. The rectangle calculated starts at 0 for the first item in the
638 * list and ignores scrolling and selection.
639 * It also uses the current font to determine the height of the tab row and
640 * it checks if all the tabs fit in the client area of the window. If they
641 * dont, a scrolling control is added.
643 static void TAB_SetItemBounds (HWND hwnd)
645 TAB_INFO* infoPtr = TAB_GetInfoPtr(hwnd);
646 LONG lStyle = GetWindowLongA(hwnd, GWL_STYLE);
647 TEXTMETRICA fontMetrics;
648 INT curItem;
649 INT curItemLeftPos;
650 HFONT hFont, hOldFont;
651 HDC hdc;
652 RECT clientRect;
653 SIZE size;
656 * We need to get text information so we need a DC and we need to select
657 * a font.
659 hdc = GetDC(hwnd);
661 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
662 hOldFont = SelectObject (hdc, hFont);
665 * We will base the rectangle calculations on the client rectangle
666 * of the control.
668 GetClientRect(hwnd, &clientRect);
671 * The leftmost item will be "0" aligned
673 curItemLeftPos = 0;
675 if ( !(lStyle & TCS_FIXEDWIDTH) && !((lStyle & TCS_OWNERDRAWFIXED) && infoPtr->fSizeSet) )
677 int item_height;
678 int icon_height = 0;
681 * Use the current font to determine the height of a tab.
683 GetTextMetricsA(hdc, &fontMetrics);
686 * Get the icon height
688 if (infoPtr->himl)
689 ImageList_GetIconSize(infoPtr->himl, 0, &icon_height);
692 * Take the highest between font or icon
694 if (fontMetrics.tmHeight > icon_height)
695 item_height = fontMetrics.tmHeight;
696 else
697 item_height = icon_height;
700 * Make sure there is enough space for the letters + icon + growing the
701 * selected item + extra space for the selected item.
703 infoPtr->tabHeight = item_height + 2*VERTICAL_ITEM_PADDING +
704 SELECTED_TAB_OFFSET;
707 for (curItem = 0; curItem < infoPtr->uNumItem; curItem++)
710 * Calculate the vertical position of the tab
712 if (lStyle & TCS_BOTTOM)
714 infoPtr->items[curItem].rect.bottom = clientRect.bottom -
715 SELECTED_TAB_OFFSET;
716 infoPtr->items[curItem].rect.top = clientRect.bottom -
717 infoPtr->tabHeight;
719 else
721 infoPtr->items[curItem].rect.top = clientRect.top +
722 SELECTED_TAB_OFFSET;
723 infoPtr->items[curItem].rect.bottom = clientRect.top +
724 infoPtr->tabHeight;
728 * Set the leftmost position of the tab.
730 infoPtr->items[curItem].rect.left = curItemLeftPos;
732 if ( (lStyle & TCS_FIXEDWIDTH) || ((lStyle & TCS_OWNERDRAWFIXED) && infoPtr->fSizeSet))
734 infoPtr->items[curItem].rect.right = infoPtr->items[curItem].rect.left +
735 infoPtr->tabWidth +
736 2*HORIZONTAL_ITEM_PADDING;
738 else
740 int icon_width = 0;
741 int num = 2;
744 * Calculate how wide the tab is depending on the text it contains
746 GetTextExtentPoint32A(hdc, infoPtr->items[curItem].pszText,
747 lstrlenA(infoPtr->items[curItem].pszText), &size);
750 * Add the icon width
752 if (infoPtr->himl)
754 ImageList_GetIconSize(infoPtr->himl, &icon_width, 0);
755 num++;
758 infoPtr->items[curItem].rect.right = infoPtr->items[curItem].rect.left +
759 size.cx + icon_width +
760 num*HORIZONTAL_ITEM_PADDING;
763 TRACE("TextSize: %i\n ", size.cx);
764 TRACE("Rect: T %i, L %i, B %i, R %i\n",
765 infoPtr->items[curItem].rect.top,
766 infoPtr->items[curItem].rect.left,
767 infoPtr->items[curItem].rect.bottom,
768 infoPtr->items[curItem].rect.right);
771 * The leftmost position of the next item is the rightmost position
772 * of this one.
774 if (lStyle & TCS_BUTTONS)
775 curItemLeftPos = infoPtr->items[curItem].rect.right + BUTTON_SPACINGX;
776 else
777 curItemLeftPos = infoPtr->items[curItem].rect.right;
781 * Check if we need a scrolling control.
783 infoPtr->needsScrolling = (curItemLeftPos + (2*SELECTED_TAB_OFFSET) >
784 clientRect.right);
786 TAB_SetupScrolling(hwnd, infoPtr, &clientRect);
789 * Cleanup
791 SelectObject (hdc, hOldFont);
792 ReleaseDC (hwnd, hdc);
795 /******************************************************************************
796 * TAB_DrawItem
798 * This method is used to draw a single tab into the tab control.
800 static void TAB_DrawItem(
801 HWND hwnd,
802 HDC hdc,
803 INT iItem)
805 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
806 LONG lStyle = GetWindowLongA(hwnd, GWL_STYLE);
807 RECT itemRect;
808 RECT selectedRect;
809 BOOL isVisible;
810 RECT r;
813 * Get the rectangle for the item.
815 isVisible = TAB_InternalGetItemRect(hwnd,
816 infoPtr,
817 iItem,
818 &itemRect,
819 &selectedRect);
821 if (isVisible)
823 HBRUSH hbr = CreateSolidBrush (GetSysColor(COLOR_BTNFACE));
824 HPEN hwPen = GetSysColorPen (COLOR_3DHILIGHT);
825 HPEN hbPen = GetSysColorPen (COLOR_BTNSHADOW);
826 HPEN hsdPen = GetSysColorPen (COLOR_BTNTEXT);
827 HPEN hfocusPen = CreatePen(PS_DOT, 1, GetSysColor(COLOR_BTNTEXT));
828 HPEN holdPen;
829 INT oldBkMode;
830 INT cx,cy;
831 BOOL deleteBrush = TRUE;
833 if (lStyle & TCS_BUTTONS)
836 * Get item rectangle.
838 r = itemRect;
840 holdPen = SelectObject (hdc, hwPen);
842 if (iItem == infoPtr->iSelected)
845 * Background color.
847 if (!((lStyle & TCS_OWNERDRAWFIXED) && infoPtr->fSizeSet))
849 COLORREF bk = GetSysColor(COLOR_3DHILIGHT);
850 DeleteObject(hbr);
851 hbr = GetSysColorBrush(COLOR_SCROLLBAR);
852 SetTextColor(hdc, GetSysColor(COLOR_3DFACE));
853 SetBkColor(hdc, bk);
855 /* if COLOR_WINDOW happens to be the same as COLOR_3DHILIGHT
856 * we better use 0x55aa bitmap brush to make scrollbar's background
857 * look different from the window background.
859 if (bk == GetSysColor(COLOR_WINDOW))
860 hbr = CACHE_GetPattern55AABrush();
862 deleteBrush = FALSE;
866 * Erase the background.
868 FillRect(hdc, &r, hbr);
871 * Draw the tab now.
872 * The rectangles calculated exclude the right and bottom
873 * borders of the rectangle. To simply the following code, those
874 * borders are shaved-off beforehand.
876 r.right--;
877 r.bottom--;
879 /* highlight */
880 MoveToEx (hdc, r.left, r.bottom, NULL);
881 LineTo (hdc, r.right, r.bottom);
882 LineTo (hdc, r.right, r.top);
884 /* shadow */
885 SelectObject(hdc, hbPen);
886 LineTo (hdc, r.left, r.top);
887 LineTo (hdc, r.left, r.bottom);
889 else
892 * Erase the background.
894 FillRect(hdc, &r, hbr);
896 /* highlight */
897 MoveToEx (hdc, r.left, r.bottom, NULL);
898 LineTo (hdc, r.left, r.top);
899 LineTo (hdc, r.right, r.top);
901 /* shadow */
902 SelectObject(hdc, hbPen);
903 LineTo (hdc, r.right, r.bottom);
904 LineTo (hdc, r.left, r.bottom);
907 else
910 * Background color.
912 DeleteObject(hbr);
913 hbr = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
916 * We draw a rectangle of different sizes depending on the selection
917 * state.
919 if (iItem == infoPtr->iSelected)
920 r = selectedRect;
921 else
922 r = itemRect;
925 * Erase the background.
926 * This is necessary when drawing the selected item since it is larger
927 * than the others, it might overlap with stuff already drawn by the
928 * other tabs
930 FillRect(hdc, &r, hbr);
933 * Draw the tab now.
934 * The rectangles calculated exclude the right and bottom
935 * borders of the rectangle. To simply the following code, those
936 * borders are shaved-off beforehand.
938 r.right--;
939 r.bottom--;
941 holdPen = SelectObject (hdc, hwPen);
943 if (lStyle & TCS_BOTTOM)
945 /* highlight */
946 MoveToEx (hdc, r.left, r.top, NULL);
947 LineTo (hdc, r.left, r.bottom - ROUND_CORNER_SIZE);
948 LineTo (hdc, r.left + ROUND_CORNER_SIZE, r.bottom);
950 /* shadow */
951 SelectObject(hdc, hbPen);
952 LineTo (hdc, r.right - ROUND_CORNER_SIZE, r.bottom);
953 LineTo (hdc, r.right, r.bottom - ROUND_CORNER_SIZE);
954 LineTo (hdc, r.right, r.top);
956 else
958 /* highlight */
959 MoveToEx (hdc, r.left, r.bottom, NULL);
960 LineTo (hdc, r.left, r.top + ROUND_CORNER_SIZE);
961 LineTo (hdc, r.left + ROUND_CORNER_SIZE, r.top);
962 LineTo (hdc, r.right - ROUND_CORNER_SIZE, r.top);
964 /* shadow */
965 SelectObject(hdc, hbPen);
966 LineTo (hdc, r.right, r.top + ROUND_CORNER_SIZE);
967 LineTo (hdc, r.right, r.bottom);
972 * Text pen
974 SelectObject(hdc, hsdPen);
976 oldBkMode = SetBkMode(hdc, TRANSPARENT);
977 SetTextColor (hdc, COLOR_BTNTEXT);
980 * Deflate the rectangle to acount for the padding
982 InflateRect(&r, -HORIZONTAL_ITEM_PADDING, -VERTICAL_ITEM_PADDING);
985 * if owner draw, tell the owner to draw
987 if ( (lStyle & TCS_OWNERDRAWFIXED) && GetParent(hwnd) )
989 DRAWITEMSTRUCT dis;
990 WND *pwndPtr;
991 UINT id;
994 * get the control id
996 pwndPtr = WIN_FindWndPtr( hwnd );
997 id = pwndPtr->wIDmenu;
998 WIN_ReleaseWndPtr(pwndPtr);
1001 * put together the DRAWITEMSTRUCT
1003 dis.CtlType = ODT_TAB;
1004 dis.CtlID = id;
1005 dis.itemID = iItem;
1006 dis.itemAction = ODA_DRAWENTIRE;
1007 if ( iItem == infoPtr->iSelected )
1008 dis.itemState = ODS_SELECTED;
1009 else
1010 dis.itemState = 0;
1011 dis.hwndItem = hwnd; /* */
1012 dis.hDC = hdc;
1013 dis.rcItem = r; /* */
1014 dis.itemData = infoPtr->items[iItem].lParam;
1017 * send the draw message
1019 SendMessageA( GetParent(hwnd), WM_DRAWITEM, (WPARAM)id, (LPARAM)&dis );
1021 else
1024 * If not owner draw, then do the drawing ourselves.
1026 * Draw the icon.
1028 if (infoPtr->himl && (infoPtr->items[iItem].mask & TCIF_IMAGE) )
1030 ImageList_Draw (infoPtr->himl, infoPtr->items[iItem].iImage, hdc,
1031 r.left, r.top+1, ILD_NORMAL);
1032 ImageList_GetIconSize (infoPtr->himl, &cx, &cy);
1033 r.left+=(cx + HORIZONTAL_ITEM_PADDING);
1037 * Draw the text;
1039 DrawTextA(hdc,
1040 infoPtr->items[iItem].pszText,
1041 lstrlenA(infoPtr->items[iItem].pszText),
1042 &r,
1043 DT_LEFT|DT_SINGLELINE|DT_VCENTER);
1047 * Draw the focus rectangle
1049 if (((lStyle & TCS_FOCUSNEVER) == 0) &&
1050 (GetFocus() == hwnd) &&
1051 (iItem == infoPtr->uFocus) )
1053 InflateRect(&r, FOCUS_RECT_HOFFSET, FOCUS_RECT_VOFFSET);
1055 SelectObject(hdc, hfocusPen);
1057 MoveToEx (hdc, r.left, r.top, NULL);
1058 LineTo (hdc, r.right-1, r.top);
1059 LineTo (hdc, r.right-1, r.bottom -1);
1060 LineTo (hdc, r.left, r.bottom -1);
1061 LineTo (hdc, r.left, r.top);
1065 * Cleanup
1067 SetBkMode(hdc, oldBkMode);
1068 SelectObject(hdc, holdPen);
1069 DeleteObject(hfocusPen);
1070 if (deleteBrush) DeleteObject(hbr);
1074 /******************************************************************************
1075 * TAB_DrawBorder
1077 * This method is used to draw the raised border around the tab control
1078 * "content" area.
1080 static void TAB_DrawBorder (HWND hwnd, HDC hdc)
1082 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1083 HPEN htmPen;
1084 HPEN hwPen = GetSysColorPen (COLOR_3DHILIGHT);
1085 HPEN hbPen = GetSysColorPen (COLOR_3DDKSHADOW);
1086 HPEN hShade = GetSysColorPen (COLOR_BTNSHADOW);
1087 RECT rect;
1089 GetClientRect (hwnd, &rect);
1092 * Adjust for the style
1094 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BOTTOM)
1096 rect.bottom -= infoPtr->tabHeight;
1098 else
1100 rect.top += infoPtr->tabHeight;
1104 * Shave-off the right and bottom margins (exluded in the
1105 * rect)
1107 rect.right--;
1108 rect.bottom--;
1110 /* highlight */
1111 htmPen = SelectObject (hdc, hwPen);
1113 MoveToEx (hdc, rect.left, rect.bottom, NULL);
1114 LineTo (hdc, rect.left, rect.top);
1115 LineTo (hdc, rect.right, rect.top);
1117 /* Dark Shadow */
1118 SelectObject (hdc, hbPen);
1119 LineTo (hdc, rect.right, rect.bottom );
1120 LineTo (hdc, rect.left, rect.bottom);
1122 /* shade */
1123 SelectObject (hdc, hShade );
1124 MoveToEx (hdc, rect.right-1, rect.top, NULL);
1125 LineTo (hdc, rect.right-1, rect.bottom-1);
1126 LineTo (hdc, rect.left, rect.bottom-1);
1128 SelectObject(hdc, htmPen);
1131 /******************************************************************************
1132 * TAB_Refresh
1134 * This method repaints the tab control..
1136 static void TAB_Refresh (HWND hwnd, HDC hdc)
1138 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1139 HFONT hOldFont;
1140 INT i;
1142 if (!infoPtr->DoRedraw)
1143 return;
1145 hOldFont = SelectObject (hdc, infoPtr->hFont);
1147 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BUTTONS)
1149 for (i = 0; i < infoPtr->uNumItem; i++)
1151 TAB_DrawItem (hwnd, hdc, i);
1154 else
1157 * Draw all the non selected item first.
1159 for (i = 0; i < infoPtr->uNumItem; i++)
1161 if (i != infoPtr->iSelected)
1162 TAB_DrawItem (hwnd, hdc, i);
1166 * Now, draw the border, draw it before the selected item
1167 * since the selected item overwrites part of the border.
1169 TAB_DrawBorder (hwnd, hdc);
1172 * Then, draw the selected item
1174 TAB_DrawItem (hwnd, hdc, infoPtr->iSelected);
1177 SelectObject (hdc, hOldFont);
1180 static LRESULT
1181 TAB_SetRedraw (HWND hwnd, WPARAM wParam)
1183 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1185 infoPtr->DoRedraw=(BOOL) wParam;
1186 return 0;
1189 static LRESULT TAB_EraseBackground(
1190 HWND hwnd,
1191 HDC givenDC)
1193 HDC hdc;
1194 RECT clientRect;
1196 HBRUSH brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
1198 hdc = givenDC ? givenDC : GetDC(hwnd);
1200 GetClientRect(hwnd, &clientRect);
1202 FillRect(hdc, &clientRect, brush);
1204 if (givenDC==0)
1205 ReleaseDC(hwnd, hdc);
1207 DeleteObject(brush);
1209 return 0;
1212 /******************************************************************************
1213 * TAB_EnsureSelectionVisible
1215 * This method will make sure that the current selection is completely
1216 * visible by scrolling until it is.
1218 static void TAB_EnsureSelectionVisible(
1219 HWND hwnd,
1220 TAB_INFO* infoPtr)
1222 INT iSelected = infoPtr->iSelected;
1225 * Do the trivial cases first.
1227 if ( (!infoPtr->needsScrolling) ||
1228 (infoPtr->hwndUpDown==0) )
1229 return;
1231 if (infoPtr->leftmostVisible >= iSelected)
1233 infoPtr->leftmostVisible = iSelected;
1235 else
1237 RECT r;
1238 INT width, i;
1240 * Calculate the part of the client area that is visible.
1242 GetClientRect(hwnd, &r);
1243 width = r.right;
1245 GetClientRect(infoPtr->hwndUpDown, &r);
1246 width -= r.right;
1248 if ((infoPtr->items[iSelected].rect.right -
1249 infoPtr->items[iSelected].rect.left) >= width )
1251 /* Special case: width of selected item is greater than visible
1252 * part of control.
1254 infoPtr->leftmostVisible = iSelected;
1256 else
1258 for (i = infoPtr->leftmostVisible; i < infoPtr->uNumItem; i++)
1260 if ((infoPtr->items[iSelected].rect.right -
1261 infoPtr->items[i].rect.left) < width)
1262 break;
1264 infoPtr->leftmostVisible = i;
1268 SendMessageA(infoPtr->hwndUpDown, UDM_SETPOS, 0,
1269 MAKELONG(infoPtr->leftmostVisible, 0));
1272 /******************************************************************************
1273 * TAB_InvalidateTabArea
1275 * This method will invalidate the portion of the control that contains the
1276 * tabs. It is called when the state of the control changes and needs
1277 * to be redisplayed
1279 static void TAB_InvalidateTabArea(
1280 HWND hwnd,
1281 TAB_INFO* infoPtr)
1283 RECT clientRect;
1285 GetClientRect(hwnd, &clientRect);
1287 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BOTTOM)
1289 clientRect.top = clientRect.bottom - (infoPtr->tabHeight + 3);
1291 else
1293 clientRect.bottom = clientRect.top + (infoPtr->tabHeight + 1);
1296 InvalidateRect(hwnd, &clientRect, TRUE);
1299 static LRESULT
1300 TAB_Paint (HWND hwnd, WPARAM wParam)
1302 HDC hdc;
1303 PAINTSTRUCT ps;
1305 hdc = wParam== 0 ? BeginPaint (hwnd, &ps) : (HDC)wParam;
1306 TAB_Refresh (hwnd, hdc);
1308 if(!wParam)
1309 EndPaint (hwnd, &ps);
1311 return 0;
1314 static LRESULT
1315 TAB_InsertItem (HWND hwnd, WPARAM wParam, LPARAM lParam)
1317 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1318 TCITEMA *pti;
1319 INT iItem, len;
1320 RECT rect;
1322 GetClientRect (hwnd, &rect);
1323 TRACE("Rect: %x T %i, L %i, B %i, R %i\n", hwnd,
1324 rect.top, rect.left, rect.bottom, rect.right);
1326 pti = (TCITEMA *)lParam;
1327 iItem = (INT)wParam;
1329 if (iItem < 0) return -1;
1330 if (iItem > infoPtr->uNumItem)
1331 iItem = infoPtr->uNumItem;
1333 if (infoPtr->uNumItem == 0) {
1334 infoPtr->items = COMCTL32_Alloc (sizeof (TAB_ITEM));
1335 infoPtr->uNumItem++;
1336 infoPtr->iSelected = 0;
1338 else {
1339 TAB_ITEM *oldItems = infoPtr->items;
1341 infoPtr->uNumItem++;
1342 infoPtr->items = COMCTL32_Alloc (sizeof (TAB_ITEM) * infoPtr->uNumItem);
1344 /* pre insert copy */
1345 if (iItem > 0) {
1346 memcpy (&infoPtr->items[0], &oldItems[0],
1347 iItem * sizeof(TAB_ITEM));
1350 /* post insert copy */
1351 if (iItem < infoPtr->uNumItem - 1) {
1352 memcpy (&infoPtr->items[iItem+1], &oldItems[iItem],
1353 (infoPtr->uNumItem - iItem - 1) * sizeof(TAB_ITEM));
1357 if (iItem <= infoPtr->iSelected)
1358 infoPtr->iSelected++;
1360 COMCTL32_Free (oldItems);
1363 infoPtr->items[iItem].mask = pti->mask;
1364 if (pti->mask & TCIF_TEXT) {
1365 len = lstrlenA (pti->pszText);
1366 infoPtr->items[iItem].pszText = COMCTL32_Alloc (len+1);
1367 lstrcpyA (infoPtr->items[iItem].pszText, pti->pszText);
1368 infoPtr->items[iItem].cchTextMax = pti->cchTextMax;
1371 if (pti->mask & TCIF_IMAGE)
1372 infoPtr->items[iItem].iImage = pti->iImage;
1374 if (pti->mask & TCIF_PARAM)
1375 infoPtr->items[iItem].lParam = pti->lParam;
1377 TAB_InvalidateTabArea(hwnd, infoPtr);
1379 TRACE("[%04x]: added item %d '%s'\n",
1380 hwnd, iItem, infoPtr->items[iItem].pszText);
1382 TAB_SetItemBounds(hwnd);
1383 return iItem;
1386 static LRESULT
1387 TAB_SetItemSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
1389 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1390 LONG lStyle = GetWindowLongA(hwnd, GWL_STYLE);
1391 LONG lResult = 0;
1393 if ((lStyle & TCS_FIXEDWIDTH) || (lStyle & TCS_OWNERDRAWFIXED))
1395 lResult = MAKELONG(infoPtr->tabWidth, infoPtr->tabHeight);
1396 infoPtr->tabWidth = (INT)LOWORD(lParam);
1397 infoPtr->tabHeight = (INT)HIWORD(lParam);
1399 infoPtr->fSizeSet = TRUE;
1401 return lResult;
1404 static LRESULT
1405 TAB_SetItemA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1407 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1408 TCITEMA *tabItem;
1409 TAB_ITEM *wineItem;
1410 INT iItem,len;
1412 iItem=(INT) wParam;
1413 tabItem=(LPTCITEMA ) lParam;
1414 TRACE("%d %p\n",iItem, tabItem);
1415 if ((iItem<0) || (iItem>=infoPtr->uNumItem)) return FALSE;
1417 wineItem=& infoPtr->items[iItem];
1419 if (tabItem->mask & TCIF_IMAGE)
1420 wineItem->iImage=tabItem->iImage;
1422 if (tabItem->mask & TCIF_PARAM)
1423 wineItem->lParam=tabItem->lParam;
1425 if (tabItem->mask & TCIF_RTLREADING)
1426 FIXME("TCIF_RTLREADING\n");
1428 if (tabItem->mask & TCIF_STATE)
1429 wineItem->dwState=tabItem->dwState;
1431 if (tabItem->mask & TCIF_TEXT) {
1432 len=lstrlenA (tabItem->pszText);
1433 if (len>wineItem->cchTextMax)
1434 wineItem->pszText= COMCTL32_ReAlloc (wineItem->pszText, len+1);
1435 lstrcpyA (wineItem->pszText, tabItem->pszText);
1439 * Update and repaint tabs.
1441 TAB_SetItemBounds(hwnd);
1442 TAB_InvalidateTabArea(hwnd,infoPtr);
1444 return TRUE;
1447 static LRESULT
1448 TAB_GetItemCount (HWND hwnd, WPARAM wParam, LPARAM lParam)
1450 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1452 return infoPtr->uNumItem;
1456 static LRESULT
1457 TAB_GetItemA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1459 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1460 TCITEMA *tabItem;
1461 TAB_ITEM *wineItem;
1462 INT iItem;
1464 iItem=(INT) wParam;
1465 tabItem=(LPTCITEMA) lParam;
1466 TRACE("\n");
1467 if ((iItem<0) || (iItem>=infoPtr->uNumItem)) return FALSE;
1469 wineItem=& infoPtr->items[iItem];
1471 if (tabItem->mask & TCIF_IMAGE)
1472 tabItem->iImage=wineItem->iImage;
1474 if (tabItem->mask & TCIF_PARAM)
1475 tabItem->lParam=wineItem->lParam;
1477 if (tabItem->mask & TCIF_RTLREADING)
1478 FIXME("TCIF_RTLREADING\n");
1480 if (tabItem->mask & TCIF_STATE)
1481 tabItem->dwState=wineItem->dwState;
1483 if (tabItem->mask & TCIF_TEXT)
1484 lstrcpynA (tabItem->pszText, wineItem->pszText, tabItem->cchTextMax);
1486 return TRUE;
1489 static LRESULT
1490 TAB_DeleteItem (HWND hwnd, WPARAM wParam, LPARAM lParam)
1492 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1493 INT iItem = (INT) wParam;
1494 BOOL bResult = FALSE;
1496 if ((iItem >= 0) && (iItem < infoPtr->uNumItem))
1498 TAB_ITEM *oldItems = infoPtr->items;
1500 infoPtr->uNumItem--;
1501 infoPtr->items = COMCTL32_Alloc(sizeof (TAB_ITEM) * infoPtr->uNumItem);
1503 if (iItem > 0)
1504 memcpy(&infoPtr->items[0], &oldItems[0], iItem * sizeof(TAB_ITEM));
1506 if (iItem < infoPtr->uNumItem)
1507 memcpy(&infoPtr->items[iItem], &oldItems[iItem + 1],
1508 (infoPtr->uNumItem - iItem) * sizeof(TAB_ITEM));
1510 COMCTL32_Free (oldItems);
1513 * Readjust the selected index.
1515 if ((iItem == infoPtr->iSelected) && (iItem > 0))
1516 infoPtr->iSelected--;
1518 if (iItem < infoPtr->iSelected)
1519 infoPtr->iSelected--;
1521 if (infoPtr->uNumItem == 0)
1522 infoPtr->iSelected = -1;
1525 * Reposition and repaint tabs.
1527 TAB_SetItemBounds(hwnd);
1528 TAB_InvalidateTabArea(hwnd,infoPtr);
1530 bResult = TRUE;
1533 return bResult;
1536 static LRESULT
1537 TAB_DeleteAllItems (HWND hwnd, WPARAM wParam, LPARAM lParam)
1539 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1541 COMCTL32_Free (infoPtr->items);
1542 infoPtr->uNumItem = 0;
1543 infoPtr->iSelected = -1;
1545 TAB_SetItemBounds(hwnd);
1546 TAB_InvalidateTabArea(hwnd,infoPtr);
1547 return TRUE;
1551 static LRESULT
1552 TAB_GetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
1554 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1556 TRACE("\n");
1557 return (LRESULT)infoPtr->hFont;
1560 static LRESULT
1561 TAB_SetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
1564 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1566 TRACE("%x %lx\n",wParam, lParam);
1568 infoPtr->hFont = (HFONT)wParam;
1570 TAB_SetItemBounds(hwnd);
1572 TAB_InvalidateTabArea(hwnd, infoPtr);
1574 return 0;
1578 static LRESULT
1579 TAB_GetImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
1581 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1583 TRACE("\n");
1584 return (LRESULT)infoPtr->himl;
1587 static LRESULT
1588 TAB_SetImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
1590 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1591 HIMAGELIST himlPrev;
1593 TRACE("\n");
1594 himlPrev = infoPtr->himl;
1595 infoPtr->himl= (HIMAGELIST)lParam;
1596 return (LRESULT)himlPrev;
1600 static LRESULT
1601 TAB_Size (HWND hwnd, WPARAM wParam, LPARAM lParam)
1604 /* I'm not really sure what the following code was meant to do.
1605 This is what it is doing:
1606 When WM_SIZE is sent with SIZE_RESTORED, the control
1607 gets positioned in the top left corner.
1609 RECT parent_rect;
1610 HWND parent;
1611 UINT uPosFlags,cx,cy;
1613 uPosFlags=0;
1614 if (!wParam) {
1615 parent = GetParent (hwnd);
1616 GetClientRect(parent, &parent_rect);
1617 cx=LOWORD (lParam);
1618 cy=HIWORD (lParam);
1619 if (GetWindowLongA(hwnd, GWL_STYLE) & CCS_NORESIZE)
1620 uPosFlags |= (SWP_NOSIZE | SWP_NOMOVE);
1622 SetWindowPos (hwnd, 0, parent_rect.left, parent_rect.top,
1623 cx, cy, uPosFlags | SWP_NOZORDER);
1624 } else {
1625 FIXME (tab,"WM_SIZE flag %x %lx not handled\n", wParam, lParam);
1626 } */
1629 * Recompute the size/position of the tabs.
1631 TAB_SetItemBounds (hwnd);
1634 * Force a repaint of the control.
1636 InvalidateRect(hwnd, NULL, TRUE);
1638 return 0;
1642 static LRESULT
1643 TAB_Create (HWND hwnd, WPARAM wParam, LPARAM lParam)
1645 TAB_INFO *infoPtr;
1646 TEXTMETRICA fontMetrics;
1647 HDC hdc;
1648 HFONT hOldFont;
1649 DWORD dwStyle;
1651 infoPtr = (TAB_INFO *)COMCTL32_Alloc (sizeof(TAB_INFO));
1653 SetWindowLongA(hwnd, 0, (DWORD)infoPtr);
1655 infoPtr->uNumItem = 0;
1656 infoPtr->hFont = 0;
1657 infoPtr->items = 0;
1658 infoPtr->hcurArrow = LoadCursorA (0, IDC_ARROWA);
1659 infoPtr->iSelected = -1;
1660 infoPtr->uFocus = 0;
1661 infoPtr->hwndToolTip = 0;
1662 infoPtr->DoRedraw = TRUE;
1663 infoPtr->needsScrolling = FALSE;
1664 infoPtr->hwndUpDown = 0;
1665 infoPtr->leftmostVisible = 0;
1666 infoPtr->fSizeSet = FALSE;
1668 TRACE("Created tab control, hwnd [%04x]\n", hwnd);
1670 /* The tab control always has the WS_CLIPSIBLINGS style. Even
1671 if you don't specify in CreateWindow. This is necesary in
1672 order for paint to work correctly. This follows windows behaviour. */
1673 dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
1674 SetWindowLongA(hwnd, GWL_STYLE, dwStyle|WS_CLIPSIBLINGS);
1676 if (dwStyle & TCS_TOOLTIPS) {
1677 /* Create tooltip control */
1678 infoPtr->hwndToolTip =
1679 CreateWindowExA (0, TOOLTIPS_CLASSA, NULL, 0,
1680 CW_USEDEFAULT, CW_USEDEFAULT,
1681 CW_USEDEFAULT, CW_USEDEFAULT,
1682 hwnd, 0, 0, 0);
1684 /* Send NM_TOOLTIPSCREATED notification */
1685 if (infoPtr->hwndToolTip) {
1686 NMTOOLTIPSCREATED nmttc;
1688 nmttc.hdr.hwndFrom = hwnd;
1689 nmttc.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
1690 nmttc.hdr.code = NM_TOOLTIPSCREATED;
1691 nmttc.hwndToolTips = infoPtr->hwndToolTip;
1693 SendMessageA (GetParent (hwnd), WM_NOTIFY,
1694 (WPARAM)GetWindowLongA(hwnd, GWL_ID), (LPARAM)&nmttc);
1699 * We need to get text information so we need a DC and we need to select
1700 * a font.
1702 hdc = GetDC(hwnd);
1703 hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1706 * Use the system font to determine the initial height of a tab.
1708 GetTextMetricsA(hdc, &fontMetrics);
1711 * Make sure there is enough space for the letters + growing the
1712 * selected item + extra space for the selected item.
1714 infoPtr->tabHeight = fontMetrics.tmHeight + 2*VERTICAL_ITEM_PADDING +
1715 SELECTED_TAB_OFFSET;
1718 * Initialize the width of a tab.
1720 infoPtr->tabWidth = DEFAULT_TAB_WIDTH;
1722 SelectObject (hdc, hOldFont);
1723 ReleaseDC(hwnd, hdc);
1725 return 0;
1728 static LRESULT
1729 TAB_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
1731 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1732 INT iItem;
1734 if (!infoPtr)
1735 return 0;
1737 if (infoPtr->items) {
1738 for (iItem = 0; iItem < infoPtr->uNumItem; iItem++) {
1739 if (infoPtr->items[iItem].pszText)
1740 COMCTL32_Free (infoPtr->items[iItem].pszText);
1742 COMCTL32_Free (infoPtr->items);
1745 if (infoPtr->hwndToolTip)
1746 DestroyWindow (infoPtr->hwndToolTip);
1748 if (infoPtr->hwndUpDown)
1749 DestroyWindow(infoPtr->hwndUpDown);
1751 COMCTL32_Free (infoPtr);
1752 return 0;
1755 static LRESULT WINAPI
1756 TAB_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1758 switch (uMsg)
1760 case TCM_GETIMAGELIST:
1761 return TAB_GetImageList (hwnd, wParam, lParam);
1763 case TCM_SETIMAGELIST:
1764 return TAB_SetImageList (hwnd, wParam, lParam);
1766 case TCM_GETITEMCOUNT:
1767 return TAB_GetItemCount (hwnd, wParam, lParam);
1769 case TCM_GETITEMA:
1770 return TAB_GetItemA (hwnd, wParam, lParam);
1772 case TCM_GETITEMW:
1773 FIXME("Unimplemented msg TCM_GETITEMW\n");
1774 return 0;
1776 case TCM_SETITEMA:
1777 return TAB_SetItemA (hwnd, wParam, lParam);
1779 case TCM_SETITEMW:
1780 FIXME("Unimplemented msg TCM_SETITEMW\n");
1781 return 0;
1783 case TCM_DELETEITEM:
1784 return TAB_DeleteItem (hwnd, wParam, lParam);
1786 case TCM_DELETEALLITEMS:
1787 return TAB_DeleteAllItems (hwnd, wParam, lParam);
1789 case TCM_GETITEMRECT:
1790 return TAB_GetItemRect (hwnd, wParam, lParam);
1792 case TCM_GETCURSEL:
1793 return TAB_GetCurSel (hwnd);
1795 case TCM_HITTEST:
1796 return TAB_HitTest (hwnd, wParam, lParam);
1798 case TCM_SETCURSEL:
1799 return TAB_SetCurSel (hwnd, wParam);
1801 case TCM_INSERTITEMA:
1802 return TAB_InsertItem (hwnd, wParam, lParam);
1804 case TCM_INSERTITEMW:
1805 FIXME("Unimplemented msg TCM_INSERTITEMW\n");
1806 return 0;
1808 case TCM_SETITEMEXTRA:
1809 FIXME("Unimplemented msg TCM_SETITEMEXTRA\n");
1810 return 0;
1812 case TCM_ADJUSTRECT:
1813 return TAB_AdjustRect (hwnd, (BOOL)wParam, (LPRECT)lParam);
1815 case TCM_SETITEMSIZE:
1816 return TAB_SetItemSize (hwnd, wParam, lParam);
1818 case TCM_REMOVEIMAGE:
1819 FIXME("Unimplemented msg TCM_REMOVEIMAGE\n");
1820 return 0;
1822 case TCM_SETPADDING:
1823 FIXME("Unimplemented msg TCM_SETPADDING\n");
1824 return 0;
1826 case TCM_GETROWCOUNT:
1827 FIXME("Unimplemented msg TCM_GETROWCOUNT\n");
1828 return 0;
1830 case TCM_GETUNICODEFORMAT:
1831 FIXME("Unimplemented msg TCM_GETUNICODEFORMAT\n");
1832 return 0;
1834 case TCM_SETUNICODEFORMAT:
1835 FIXME("Unimplemented msg TCM_SETUNICODEFORMAT\n");
1836 return 0;
1838 case TCM_HIGHLIGHTITEM:
1839 FIXME("Unimplemented msg TCM_HIGHLIGHTITEM\n");
1840 return 0;
1842 case TCM_GETTOOLTIPS:
1843 return TAB_GetToolTips (hwnd, wParam, lParam);
1845 case TCM_SETTOOLTIPS:
1846 return TAB_SetToolTips (hwnd, wParam, lParam);
1848 case TCM_GETCURFOCUS:
1849 return TAB_GetCurFocus (hwnd);
1851 case TCM_SETCURFOCUS:
1852 return TAB_SetCurFocus (hwnd, wParam);
1854 case TCM_SETMINTABWIDTH:
1855 FIXME("Unimplemented msg TCM_SETMINTABWIDTH\n");
1856 return 0;
1858 case TCM_DESELECTALL:
1859 FIXME("Unimplemented msg TCM_DESELECTALL\n");
1860 return 0;
1862 case TCM_GETEXTENDEDSTYLE:
1863 FIXME("Unimplemented msg TCM_GETEXTENDEDSTYLE\n");
1864 return 0;
1866 case TCM_SETEXTENDEDSTYLE:
1867 FIXME("Unimplemented msg TCM_SETEXTENDEDSTYLE\n");
1868 return 0;
1870 case WM_GETFONT:
1871 return TAB_GetFont (hwnd, wParam, lParam);
1873 case WM_SETFONT:
1874 return TAB_SetFont (hwnd, wParam, lParam);
1876 case WM_CREATE:
1877 return TAB_Create (hwnd, wParam, lParam);
1879 case WM_NCDESTROY:
1880 return TAB_Destroy (hwnd, wParam, lParam);
1882 case WM_GETDLGCODE:
1883 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1885 case WM_LBUTTONDOWN:
1886 return TAB_LButtonDown (hwnd, wParam, lParam);
1888 case WM_LBUTTONUP:
1889 return TAB_LButtonUp (hwnd, wParam, lParam);
1891 case WM_RBUTTONDOWN:
1892 return TAB_RButtonDown (hwnd, wParam, lParam);
1894 case WM_MOUSEMOVE:
1895 return TAB_MouseMove (hwnd, wParam, lParam);
1897 case WM_ERASEBKGND:
1898 return TAB_EraseBackground (hwnd, (HDC)wParam);
1900 case WM_PAINT:
1901 return TAB_Paint (hwnd, wParam);
1903 case WM_SIZE:
1904 return TAB_Size (hwnd, wParam, lParam);
1906 case WM_SETREDRAW:
1907 return TAB_SetRedraw (hwnd, wParam);
1909 case WM_HSCROLL:
1910 return TAB_OnHScroll(hwnd, (int)LOWORD(wParam), (int)HIWORD(wParam), (HWND)lParam);
1912 case WM_STYLECHANGED:
1913 TAB_SetItemBounds (hwnd);
1914 InvalidateRect(hwnd, NULL, TRUE);
1915 return 0;
1917 case WM_KILLFOCUS:
1918 case WM_SETFOCUS:
1919 return TAB_FocusChanging(hwnd, uMsg, wParam, lParam);
1921 case WM_KEYUP:
1922 return TAB_KeyUp(hwnd, wParam);
1924 default:
1925 if (uMsg >= WM_USER)
1926 ERR("unknown msg %04x wp=%08x lp=%08lx\n",
1927 uMsg, wParam, lParam);
1928 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1931 return 0;
1935 VOID
1936 TAB_Register (void)
1938 WNDCLASSA wndClass;
1940 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1941 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
1942 wndClass.lpfnWndProc = (WNDPROC)TAB_WindowProc;
1943 wndClass.cbClsExtra = 0;
1944 wndClass.cbWndExtra = sizeof(TAB_INFO *);
1945 wndClass.hCursor = LoadCursorA (0, IDC_ARROWA);
1946 wndClass.hbrBackground = (HBRUSH)NULL;
1947 wndClass.lpszClassName = WC_TABCONTROLA;
1949 RegisterClassA (&wndClass);
1953 VOID
1954 TAB_Unregister (void)
1956 UnregisterClassA (WC_TABCONTROLA, (HINSTANCE)NULL);