4 * Copyright 1998 Anders Carlsson
5 * Copyright 1999 Alex Priem <alexp@sci.kun.nl>
6 * Copyright 1999 Francis Beaudet
21 DEFAULT_DEBUG_CHANNEL(tab)
23 /******************************************************************************
24 * Positioning constants
26 #define SELECTED_TAB_OFFSET 2
27 #define HORIZONTAL_ITEM_PADDING 5
28 #define VERTICAL_ITEM_PADDING 3
29 #define ROUND_CORNER_SIZE 2
30 #define FOCUS_RECT_HOFFSET 2
31 #define FOCUS_RECT_VOFFSET 1
32 #define DISPLAY_AREA_PADDINGX 5
33 #define DISPLAY_AREA_PADDINGY 5
34 #define CONTROL_BORDER_SIZEX 2
35 #define CONTROL_BORDER_SIZEY 2
36 #define BUTTON_SPACINGX 10
37 #define DEFAULT_TAB_WIDTH 96
39 #define TAB_GetInfoPtr(hwnd) ((TAB_INFO *)GetWindowLongA(hwnd,0))
41 /******************************************************************************
44 static void TAB_Refresh (HWND hwnd, HDC hdc);
45 static void TAB_InvalidateTabArea(HWND hwnd, TAB_INFO* infoPtr);
46 static void TAB_EnsureSelectionVisible(HWND hwnd, TAB_INFO* infoPtr);
49 TAB_SendSimpleNotify (HWND hwnd, UINT code)
53 nmhdr.hwndFrom = hwnd;
54 nmhdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
57 return (BOOL) SendMessageA (GetParent (hwnd), WM_NOTIFY,
58 (WPARAM) nmhdr.idFrom, (LPARAM) &nmhdr);
63 TAB_RelayEvent (HWND hwndTip, HWND hwndMsg, UINT uMsg,
64 WPARAM wParam, LPARAM lParam)
72 msg.time = GetMessageTime ();
73 msg.pt.x = LOWORD(GetMessagePos ());
74 msg.pt.y = HIWORD(GetMessagePos ());
76 SendMessageA (hwndTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
82 TAB_GetCurSel (HWND hwnd)
84 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
86 return infoPtr->iSelected;
90 TAB_GetCurFocus (HWND hwnd)
92 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
94 return infoPtr->uFocus;
98 TAB_GetToolTips (HWND hwnd, WPARAM wParam, LPARAM lParam)
100 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
102 if (infoPtr == NULL) return 0;
103 return infoPtr->hwndToolTip;
108 TAB_SetCurSel (HWND hwnd,WPARAM wParam)
110 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
111 INT iItem=(INT) wParam;
115 if ((iItem >= 0) && (iItem < infoPtr->uNumItem)) {
116 prevItem=infoPtr->iSelected;
117 infoPtr->iSelected=iItem;
123 TAB_SetCurFocus (HWND hwnd,WPARAM wParam)
125 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
126 INT iItem=(INT) wParam;
128 if ((iItem < 0) || (iItem > infoPtr->uNumItem)) return 0;
130 infoPtr->uFocus=iItem;
131 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BUTTONS) {
132 FIXME (tab,"Should set input focus\n");
134 if (infoPtr->iSelected != iItem) {
135 if (TAB_SendSimpleNotify(hwnd, TCN_SELCHANGING)!=TRUE) {
136 infoPtr->iSelected = iItem;
137 TAB_SendSimpleNotify(hwnd, TCN_SELCHANGE);
139 TAB_EnsureSelectionVisible(hwnd, infoPtr);
140 TAB_InvalidateTabArea(hwnd, infoPtr);
148 TAB_SetToolTips (HWND hwnd, WPARAM wParam, LPARAM lParam)
150 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
152 if (infoPtr == NULL) return 0;
153 infoPtr->hwndToolTip = (HWND)wParam;
157 /******************************************************************************
158 * TAB_InternalGetItemRect
160 * This method will calculate the rectangle representing a given tab item in
161 * client coordinates. This method takes scrolling into account.
163 * This method returns TRUE if the item is visible in the window and FALSE
164 * if it is completely outside the client area.
166 static BOOL TAB_InternalGetItemRect(
176 * Perform a sanity check and a trivial visibility check.
178 if ( (infoPtr->uNumItem <=0) ||
179 (itemIndex >= infoPtr->uNumItem) ||
180 (itemIndex < infoPtr->leftmostVisible) )
184 * Avoid special cases in this procedure by assigning the "out"
185 * parameters if the caller didn't supply them
188 itemRect = &tmpItemRect;
191 * Retrieve the unmodified item rect.
193 *itemRect = infoPtr->items[itemIndex].rect;
196 * "scroll" it to make sure the item at the very left of the
197 * tab control is the leftmost visible tab.
200 -infoPtr->items[infoPtr->leftmostVisible].rect.left,
204 * Move the rectangle so the first item is slightly offset from
205 * the left of the tab control.
213 * Now, calculate the position of the item as if it were selected.
215 if (selectedRect!=NULL)
217 CopyRect(selectedRect, itemRect);
220 * The rectangle of a selected item is a bit wider.
222 InflateRect(selectedRect, SELECTED_TAB_OFFSET, 0);
225 * If it also a bit higher.
227 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BOTTOM)
229 selectedRect->top -=2; /* the border is thicker on the bottom */
230 selectedRect->bottom +=SELECTED_TAB_OFFSET;
234 selectedRect->top -=SELECTED_TAB_OFFSET;
235 selectedRect->bottom+=1;
242 static BOOL TAB_GetItemRect(HWND hwnd, WPARAM wParam, LPARAM lParam)
244 return TAB_InternalGetItemRect(hwnd, TAB_GetInfoPtr(hwnd), (INT)wParam,
245 (LPRECT)lParam, (LPRECT)NULL);
248 /******************************************************************************
251 * This method is called to handle keyboard input
253 static LRESULT TAB_KeyUp(
257 TAB_INFO* infoPtr = TAB_GetInfoPtr(hwnd);
263 newItem = infoPtr->uFocus-1;
266 newItem = infoPtr->uFocus+1;
271 * If we changed to a valid item, change the selection
273 if ( (newItem >= 0) &&
274 (newItem < infoPtr->uNumItem) &&
275 (infoPtr->uFocus != newItem) )
277 if (!TAB_SendSimpleNotify(hwnd, TCN_SELCHANGING))
279 infoPtr->iSelected = newItem;
280 infoPtr->uFocus = newItem;
281 TAB_SendSimpleNotify(hwnd, TCN_SELCHANGE);
283 TAB_EnsureSelectionVisible(hwnd, infoPtr);
284 TAB_InvalidateTabArea(hwnd, infoPtr);
291 /******************************************************************************
294 * This method is called whenever the focus goes in or out of this control
295 * it is used to update the visual state of the control.
297 static LRESULT TAB_FocusChanging(
303 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
308 * Get the rectangle for the item.
310 isVisible = TAB_InternalGetItemRect(hwnd,
317 * If the rectangle is not completely invisible, invalidate that
318 * portion of the window.
322 InvalidateRect(hwnd, &selectedRect, TRUE);
326 * Don't otherwise disturb normal behavior.
328 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
331 static HWND TAB_InternalHitTest (
341 for (iCount = 0; iCount < infoPtr->uNumItem; iCount++)
343 TAB_InternalGetItemRect(hwnd,
349 if (PtInRect (&rect, pt))
351 *flags = TCHT_ONITEM;
361 TAB_HitTest (HWND hwnd, WPARAM wParam, LPARAM lParam)
363 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
364 LPTCHITTESTINFO lptest=(LPTCHITTESTINFO) lParam;
366 return TAB_InternalHitTest (hwnd, infoPtr,lptest->pt,&lptest->flags);
371 TAB_LButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
373 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
375 if (infoPtr->hwndToolTip)
376 TAB_RelayEvent (infoPtr->hwndToolTip, hwnd,
377 WM_LBUTTONDOWN, wParam, lParam);
379 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_FOCUSONBUTTONDOWN ) {
386 TAB_LButtonUp (HWND hwnd, WPARAM wParam, LPARAM lParam)
388 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
392 if (infoPtr->hwndToolTip)
393 TAB_RelayEvent (infoPtr->hwndToolTip, hwnd,
394 WM_LBUTTONDOWN, wParam, lParam);
396 pt.x = (INT)LOWORD(lParam);
397 pt.y = (INT)HIWORD(lParam);
399 newItem=TAB_InternalHitTest (hwnd, infoPtr,pt,&dummy);
401 TRACE(tab, "On Tab, item %d\n", newItem);
403 if ( (newItem!=-1) &&
404 (infoPtr->iSelected != newItem) )
406 if (TAB_SendSimpleNotify(hwnd, TCN_SELCHANGING)!=TRUE)
408 infoPtr->iSelected = newItem;
409 infoPtr->uFocus = newItem;
410 TAB_SendSimpleNotify(hwnd, TCN_SELCHANGE);
412 TAB_EnsureSelectionVisible(hwnd, infoPtr);
414 TAB_InvalidateTabArea(hwnd, infoPtr);
417 TAB_SendSimpleNotify(hwnd, NM_CLICK);
423 TAB_RButtonDown (HWND hwnd, WPARAM wParam, LPARAM lParam)
425 TAB_SendSimpleNotify(hwnd, NM_RCLICK);
430 TAB_MouseMove (HWND hwnd, WPARAM wParam, LPARAM lParam)
432 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
434 if (infoPtr->hwndToolTip)
435 TAB_RelayEvent (infoPtr->hwndToolTip, hwnd,
436 WM_LBUTTONDOWN, wParam, lParam);
440 /******************************************************************************
443 * Calculates the tab control's display area given the windows rectangle or
444 * the window rectangle given the requested display rectangle.
446 static LRESULT TAB_AdjustRect(
451 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
456 * Go from display rectangle
460 * Add the height of the tabs.
462 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BOTTOM)
463 prc->bottom += infoPtr->tabHeight;
465 prc->top -= infoPtr->tabHeight;
468 * Inflate the rectangle for the padding
470 InflateRect(prc, DISPLAY_AREA_PADDINGX, DISPLAY_AREA_PADDINGY);
473 * Inflate for the border
475 InflateRect(prc, CONTROL_BORDER_SIZEX, CONTROL_BORDER_SIZEX);
480 * Go from window rectangle.
484 * Deflate the rectangle for the border
486 InflateRect(prc, -CONTROL_BORDER_SIZEX, -CONTROL_BORDER_SIZEX);
489 * Deflate the rectangle for the padding
491 InflateRect(prc, -DISPLAY_AREA_PADDINGX, -DISPLAY_AREA_PADDINGY);
494 * Remove the height of the tabs.
496 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BOTTOM)
497 prc->bottom -= infoPtr->tabHeight;
499 prc->top += infoPtr->tabHeight;
506 /******************************************************************************
509 * This method will handle the notification from the scroll control and
510 * perform the scrolling operation on the tab control.
512 static LRESULT TAB_OnHScroll(
518 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
520 if (nScrollCode == SB_LINELEFT)
522 if (infoPtr->leftmostVisible>0)
524 infoPtr->leftmostVisible--;
526 TAB_InvalidateTabArea(hwnd, infoPtr);
529 else if (nScrollCode == SB_LINERIGHT)
531 if (infoPtr->leftmostVisible< (infoPtr->uNumItem-1))
533 infoPtr->leftmostVisible++;
535 TAB_InvalidateTabArea(hwnd, infoPtr);
542 /******************************************************************************
545 * This method will check the current scrolling state and make sure the
546 * scrolling control is displayed (or not).
548 static void TAB_SetupScrolling(
551 const RECT* clientRect)
553 if (infoPtr->needsScrolling)
558 * Calculate the position of the scroll control.
560 controlPos.right = clientRect->right;
561 controlPos.left = controlPos.right - 2*GetSystemMetrics(SM_CXHSCROLL);
563 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BOTTOM)
565 controlPos.top = clientRect->bottom - infoPtr->tabHeight;
566 controlPos.bottom = controlPos.top + GetSystemMetrics(SM_CYHSCROLL);
570 controlPos.bottom = clientRect->top + infoPtr->tabHeight;
571 controlPos.top = controlPos.bottom - GetSystemMetrics(SM_CYHSCROLL);
575 * If we don't have a scroll control yet, we want to create one.
576 * If we have one, we want to make sure it's positioned right.
578 if (infoPtr->hwndUpDown==0)
581 * I use a scrollbar since it seems to be more stable than the Updown
584 infoPtr->hwndUpDown = CreateWindowA("ScrollBar",
586 WS_VISIBLE | WS_CHILD | WS_OVERLAPPED | SBS_HORZ,
587 controlPos.left, controlPos.top,
588 controlPos.right - controlPos.left,
589 controlPos.bottom - controlPos.top,
597 SetWindowPos(infoPtr->hwndUpDown,
599 controlPos.left, controlPos.top,
600 controlPos.right - controlPos.left,
601 controlPos.bottom - controlPos.top,
602 SWP_SHOWWINDOW | SWP_NOZORDER);
608 * If we once had a scroll control... hide it.
610 if (infoPtr->hwndUpDown!=0)
612 ShowWindow(infoPtr->hwndUpDown, SW_HIDE);
617 /******************************************************************************
620 * This method will calculate the position rectangles of all the items in the
621 * control. The rectangle calculated starts at 0 for the first item in the
622 * list and ignores scrolling and selection.
623 * It also uses the current font to determine the height of the tab row and
624 * it checks if all the tabs fit in the client area of the window. If they
625 * dont, a scrolling control is added.
627 static void TAB_SetItemBounds (HWND hwnd)
629 TAB_INFO* infoPtr = TAB_GetInfoPtr(hwnd);
630 LONG lStyle = GetWindowLongA(hwnd, GWL_STYLE);
631 TEXTMETRICA fontMetrics;
634 HFONT hFont, hOldFont;
640 * We need to get text information so we need a DC and we need to select
645 hFont = infoPtr->hFont ? infoPtr->hFont : GetStockObject (SYSTEM_FONT);
646 hOldFont = SelectObject (hdc, hFont);
649 * We will base the rectangle calculations on the client rectangle
652 GetClientRect(hwnd, &clientRect);
655 * The leftmost item will be "0" aligned
659 if (!((lStyle & TCS_FIXEDWIDTH) || (lStyle & TCS_OWNERDRAWFIXED)))
662 * Use the current font to determine the height of a tab.
664 GetTextMetricsA(hdc, &fontMetrics);
667 * Make sure there is enough space for the letters + growing the
668 * selected item + extra space for the selected item.
670 infoPtr->tabHeight = fontMetrics.tmHeight + 2*VERTICAL_ITEM_PADDING +
674 for (curItem = 0; curItem < infoPtr->uNumItem; curItem++)
677 * Calculate the vertical position of the tab
679 if (lStyle & TCS_BOTTOM)
681 infoPtr->items[curItem].rect.bottom = clientRect.bottom -
683 infoPtr->items[curItem].rect.top = clientRect.bottom -
688 infoPtr->items[curItem].rect.top = clientRect.top +
690 infoPtr->items[curItem].rect.bottom = clientRect.top +
695 * Set the leftmost position of the tab.
697 infoPtr->items[curItem].rect.left = curItemLeftPos;
699 if ((lStyle & TCS_FIXEDWIDTH) || (lStyle & TCS_OWNERDRAWFIXED))
701 infoPtr->items[curItem].rect.right = infoPtr->items[curItem].rect.left +
703 2*HORIZONTAL_ITEM_PADDING;
708 * Calculate how wide the tab is depending on the text it contains
710 GetTextExtentPoint32A(hdc, infoPtr->items[curItem].pszText,
711 lstrlenA(infoPtr->items[curItem].pszText), &size);
713 infoPtr->items[curItem].rect.right = infoPtr->items[curItem].rect.left +
714 size.cx + 2*HORIZONTAL_ITEM_PADDING;
717 TRACE(tab, "TextSize: %i\n ", size.cx);
718 TRACE(tab, "Rect: T %i, L %i, B %i, R %i\n",
719 infoPtr->items[curItem].rect.top,
720 infoPtr->items[curItem].rect.left,
721 infoPtr->items[curItem].rect.bottom,
722 infoPtr->items[curItem].rect.right);
725 * The leftmost position of the next item is the rightmost position
728 if (lStyle & TCS_BUTTONS)
729 curItemLeftPos = infoPtr->items[curItem].rect.right + BUTTON_SPACINGX;
731 curItemLeftPos = infoPtr->items[curItem].rect.right;
735 * Check if we need a scrolling control.
737 infoPtr->needsScrolling = (curItemLeftPos + (2*SELECTED_TAB_OFFSET) >
740 TAB_SetupScrolling(hwnd, infoPtr, &clientRect);
745 SelectObject (hdc, hOldFont);
746 ReleaseDC (hwnd, hdc);
749 /******************************************************************************
752 * This method is used to draw a single tab into the tab control.
754 static void TAB_DrawItem(
759 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
760 LONG lStyle = GetWindowLongA(hwnd, GWL_STYLE);
767 * Get the rectangle for the item.
769 isVisible = TAB_InternalGetItemRect(hwnd,
777 HBRUSH hbr = CreateSolidBrush (GetSysColor(COLOR_BACKGROUND));
778 HPEN hwPen = GetSysColorPen (COLOR_3DHILIGHT);
779 HPEN hbPen = GetSysColorPen (COLOR_BTNSHADOW);
780 HPEN hsdPen = GetSysColorPen (COLOR_BTNTEXT);
781 HPEN hfocusPen = CreatePen(PS_DOT, 1, GetSysColor(COLOR_BTNTEXT));
786 if (lStyle & TCS_BUTTONS)
789 * Get item rectangle.
793 holdPen = SelectObject (hdc, hwPen);
795 if (iItem == infoPtr->iSelected)
800 if (!(lStyle & TCS_OWNERDRAWFIXED))
801 hbr = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
804 * Erase the background.
806 FillRect(hdc, &r, hbr);
810 * The rectangles calculated exclude the right and bottom
811 * borders of the rectangle. To simply the following code, those
812 * borders are shaved-off beforehand.
818 MoveToEx (hdc, r.left, r.bottom, NULL);
819 LineTo (hdc, r.right, r.bottom);
820 LineTo (hdc, r.right, r.top);
823 SelectObject(hdc, hbPen);
824 LineTo (hdc, r.left, r.top);
825 LineTo (hdc, r.left, r.bottom);
830 * Erase the background.
832 FillRect(hdc, &r, hbr);
835 MoveToEx (hdc, r.left, r.bottom, NULL);
836 LineTo (hdc, r.left, r.top);
837 LineTo (hdc, r.right, r.top);
840 SelectObject(hdc, hbPen);
841 LineTo (hdc, r.right, r.bottom);
842 LineTo (hdc, r.left, r.bottom);
850 hbr = CreateSolidBrush(GetSysColor(COLOR_BACKGROUND));
853 * We draw a rectangle of different sizes depending on the selection
856 if (iItem == infoPtr->iSelected)
862 * Erase the background.
863 * This is necessary when drawing the selected item since it is larger
864 * than the others, it might overlap with stuff already drawn by the
867 FillRect(hdc, &r, hbr);
871 * The rectangles calculated exclude the right and bottom
872 * borders of the rectangle. To simply the following code, those
873 * borders are shaved-off beforehand.
878 holdPen = SelectObject (hdc, hwPen);
880 if (lStyle & TCS_BOTTOM)
883 MoveToEx (hdc, r.left, r.top, NULL);
884 LineTo (hdc, r.left, r.bottom - ROUND_CORNER_SIZE);
885 LineTo (hdc, r.left + ROUND_CORNER_SIZE, r.bottom);
888 SelectObject(hdc, hbPen);
889 LineTo (hdc, r.right - ROUND_CORNER_SIZE, r.bottom);
890 LineTo (hdc, r.right, r.bottom - ROUND_CORNER_SIZE);
891 LineTo (hdc, r.right, r.top);
896 MoveToEx (hdc, r.left, r.bottom, NULL);
897 LineTo (hdc, r.left, r.top + ROUND_CORNER_SIZE);
898 LineTo (hdc, r.left + ROUND_CORNER_SIZE, r.top);
899 LineTo (hdc, r.right - ROUND_CORNER_SIZE, r.top);
902 SelectObject(hdc, hbPen);
903 LineTo (hdc, r.right, r.top + ROUND_CORNER_SIZE);
904 LineTo (hdc, r.right, r.bottom);
911 SelectObject(hdc, hsdPen);
913 oldBkMode = SetBkMode(hdc, TRANSPARENT);
914 SetTextColor (hdc, COLOR_BTNTEXT);
917 * Deflate the rectangle to acount for the padding
919 InflateRect(&r, -HORIZONTAL_ITEM_PADDING, -VERTICAL_ITEM_PADDING);
926 ImageList_Draw (infoPtr->himl, iItem, hdc,
927 r.left, r.top+1, ILD_NORMAL);
928 ImageList_GetIconSize (infoPtr->himl, &cx, &cy);
936 infoPtr->items[iItem].pszText,
937 lstrlenA(infoPtr->items[iItem].pszText),
939 DT_LEFT|DT_SINGLELINE|DT_VCENTER);
942 * Draw the focus rectangle
944 if (((lStyle & TCS_FOCUSNEVER) == 0) &&
945 (GetFocus() == hwnd) &&
946 (iItem == infoPtr->uFocus) )
948 InflateRect(&r, FOCUS_RECT_HOFFSET, FOCUS_RECT_VOFFSET);
950 SelectObject(hdc, hfocusPen);
952 MoveToEx (hdc, r.left, r.top, NULL);
953 LineTo (hdc, r.right-1, r.top);
954 LineTo (hdc, r.right-1, r.bottom -1);
955 LineTo (hdc, r.left, r.bottom -1);
956 LineTo (hdc, r.left, r.top);
962 SetBkMode(hdc, oldBkMode);
963 SelectObject(hdc, holdPen);
967 /******************************************************************************
970 * This method is used to draw the raised border around the tab control
973 static void TAB_DrawBorder (HWND hwnd, HDC hdc)
975 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
977 HPEN hwPen = GetSysColorPen (COLOR_3DHILIGHT);
978 HPEN hbPen = GetSysColorPen (COLOR_3DDKSHADOW);
979 HPEN hShade = GetSysColorPen (COLOR_BTNSHADOW);
982 GetClientRect (hwnd, &rect);
985 * Adjust for the style
987 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BOTTOM)
989 rect.bottom -= infoPtr->tabHeight;
993 rect.top += infoPtr->tabHeight;
997 * Shave-off the right and bottom margins (exluded in the
1004 htmPen = SelectObject (hdc, hwPen);
1006 MoveToEx (hdc, rect.left, rect.bottom, NULL);
1007 LineTo (hdc, rect.left, rect.top);
1008 LineTo (hdc, rect.right, rect.top);
1011 SelectObject (hdc, hbPen);
1012 LineTo (hdc, rect.right, rect.bottom );
1013 LineTo (hdc, rect.left, rect.bottom);
1016 SelectObject (hdc, hShade );
1017 MoveToEx (hdc, rect.right-1, rect.top, NULL);
1018 LineTo (hdc, rect.right-1, rect.bottom-1);
1019 LineTo (hdc, rect.left, rect.bottom-1);
1021 SelectObject(hdc, htmPen);
1024 /******************************************************************************
1027 * This method repaints the tab control..
1029 static void TAB_Refresh (HWND hwnd, HDC hdc)
1031 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1035 if (!infoPtr->DoRedraw)
1038 hOldFont = SelectObject (hdc, infoPtr->hFont);
1040 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BUTTONS)
1042 for (i = 0; i < infoPtr->uNumItem; i++)
1044 TAB_DrawItem (hwnd, hdc, i);
1050 * Draw all the non selected item first.
1052 for (i = 0; i < infoPtr->uNumItem; i++)
1054 if (i != infoPtr->iSelected)
1055 TAB_DrawItem (hwnd, hdc, i);
1059 * Now, draw the border, draw it before the selected item
1060 * since the selected item overwrites part of the border.
1062 TAB_DrawBorder (hwnd, hdc);
1065 * Then, draw the selected item
1067 TAB_DrawItem (hwnd, hdc, infoPtr->iSelected);
1070 SelectObject (hdc, hOldFont);
1074 TAB_SetRedraw (HWND hwnd, WPARAM wParam)
1076 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1078 infoPtr->DoRedraw=(BOOL) wParam;
1082 static LRESULT TAB_EraseBackground(
1089 HBRUSH brush = CreateSolidBrush(GetSysColor(COLOR_BACKGROUND));
1091 hdc = givenDC ? givenDC : GetDC(hwnd);
1093 GetClientRect(hwnd, &clientRect);
1095 FillRect(hdc, &clientRect, brush);
1098 ReleaseDC(hwnd, hdc);
1103 /******************************************************************************
1104 * TAB_EnsureSelectionVisible
1106 * This method will make sure that the current selection is completely
1107 * visible by scrolling until it is.
1109 static void TAB_EnsureSelectionVisible(
1119 * Do the trivial cases first.
1121 if ( (!infoPtr->needsScrolling) ||
1122 (infoPtr->hwndUpDown==0) )
1125 if (infoPtr->leftmostVisible > infoPtr->iSelected)
1127 infoPtr->leftmostVisible = infoPtr->iSelected;
1132 * Calculate the part of the client area that is visible.
1134 GetClientRect(hwnd, &visibleRect);
1135 GetClientRect(infoPtr->hwndUpDown, &scrollerRect);
1136 visibleRect.right -= scrollerRect.right;
1139 * Get the rectangle for the item
1141 isVisible = TAB_InternalGetItemRect(hwnd,
1148 * If this function can't say it's completely invisible, maybe it
1149 * is partially visible. Let's check.
1153 POINT pt1 = { selectedRect.left, selectedRect.top };
1154 POINT pt2 = { selectedRect.right - 1, selectedRect.bottom - 1 };
1156 isVisible = PtInRect(&visibleRect, pt1) && PtInRect(&visibleRect, pt2);
1159 while ( (infoPtr->leftmostVisible < infoPtr->iSelected) &&
1162 infoPtr->leftmostVisible++;
1165 * Get the rectangle for the item
1167 isVisible = TAB_InternalGetItemRect(hwnd,
1174 * If this function can't say it's completely invisible, maybe it
1175 * is partially visible. Let's check.
1179 POINT pt1 = { selectedRect.left, selectedRect.top };
1180 POINT pt2 = { selectedRect.right - 1, selectedRect.bottom - 1 };
1182 isVisible = PtInRect(&visibleRect, pt1) && PtInRect(&visibleRect, pt2);
1187 /******************************************************************************
1188 * TAB_InvalidateTabArea
1190 * This method will invalidate the portion of the control that contains the
1191 * tabs. It is called when the state of the control changes and needs
1194 static void TAB_InvalidateTabArea(
1200 GetClientRect(hwnd, &clientRect);
1202 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_BOTTOM)
1204 clientRect.top = clientRect.bottom - (infoPtr->tabHeight + 1);
1208 clientRect.bottom = clientRect.top + (infoPtr->tabHeight + 1);
1211 InvalidateRect(hwnd, &clientRect, TRUE);
1215 TAB_Paint (HWND hwnd, WPARAM wParam)
1220 hdc = wParam== 0 ? BeginPaint (hwnd, &ps) : (HDC)wParam;
1221 TAB_Refresh (hwnd, hdc);
1224 EndPaint (hwnd, &ps);
1230 TAB_InsertItem (HWND hwnd, WPARAM wParam, LPARAM lParam)
1232 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1237 GetClientRect (hwnd, &rect);
1238 TRACE(tab, "Rect: %x T %i, L %i, B %i, R %i\n", hwnd,
1239 rect.top, rect.left, rect.bottom, rect.right);
1241 pti = (TCITEMA *)lParam;
1242 iItem = (INT)wParam;
1244 if (iItem < 0) return -1;
1245 if (iItem > infoPtr->uNumItem)
1246 iItem = infoPtr->uNumItem;
1248 if (infoPtr->uNumItem == 0) {
1249 infoPtr->items = COMCTL32_Alloc (sizeof (TAB_ITEM));
1250 infoPtr->uNumItem++;
1253 TAB_ITEM *oldItems = infoPtr->items;
1255 infoPtr->uNumItem++;
1256 infoPtr->items = COMCTL32_Alloc (sizeof (TAB_ITEM) * infoPtr->uNumItem);
1258 /* pre insert copy */
1260 memcpy (&infoPtr->items[0], &oldItems[0],
1261 iItem * sizeof(TAB_ITEM));
1264 /* post insert copy */
1265 if (iItem < infoPtr->uNumItem - 1) {
1266 memcpy (&infoPtr->items[iItem+1], &oldItems[iItem],
1267 (infoPtr->uNumItem - iItem - 1) * sizeof(TAB_ITEM));
1271 COMCTL32_Free (oldItems);
1274 infoPtr->items[iItem].mask = pti->mask;
1275 if (pti->mask & TCIF_TEXT) {
1276 len = lstrlenA (pti->pszText);
1277 infoPtr->items[iItem].pszText = COMCTL32_Alloc (len+1);
1278 lstrcpyA (infoPtr->items[iItem].pszText, pti->pszText);
1279 infoPtr->items[iItem].cchTextMax = pti->cchTextMax;
1282 if (pti->mask & TCIF_IMAGE)
1283 infoPtr->items[iItem].iImage = pti->iImage;
1285 if (pti->mask & TCIF_PARAM)
1286 infoPtr->items[iItem].lParam = pti->lParam;
1288 TAB_InvalidateTabArea(hwnd, infoPtr);
1290 TRACE(tab, "[%04x]: added item %d '%s'\n",
1291 hwnd, iItem, infoPtr->items[iItem].pszText);
1293 TAB_SetItemBounds(hwnd);
1298 TAB_SetItemSize (HWND hwnd, WPARAM wParam, LPARAM lParam)
1300 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1301 LONG lStyle = GetWindowLongA(hwnd, GWL_STYLE);
1304 if ((lStyle & TCS_FIXEDWIDTH) || (lStyle & TCS_OWNERDRAWFIXED))
1306 lResult = MAKELONG(infoPtr->tabWidth, infoPtr->tabHeight);
1307 infoPtr->tabWidth = (INT)LOWORD(lParam);
1308 infoPtr->tabHeight = (INT)HIWORD(lParam);
1315 TAB_SetItemA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1317 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1323 tabItem=(LPTCITEMA ) lParam;
1324 TRACE (tab,"%d %p\n",iItem, tabItem);
1325 if ((iItem<0) || (iItem>infoPtr->uNumItem)) return FALSE;
1327 wineItem=& infoPtr->items[iItem];
1329 if (tabItem->mask & TCIF_IMAGE)
1330 wineItem->iImage=tabItem->iImage;
1332 if (tabItem->mask & TCIF_PARAM)
1333 wineItem->lParam=tabItem->lParam;
1335 if (tabItem->mask & TCIF_RTLREADING)
1336 FIXME (tab,"TCIF_RTLREADING\n");
1338 if (tabItem->mask & TCIF_STATE)
1339 wineItem->dwState=tabItem->dwState;
1341 if (tabItem->mask & TCIF_TEXT) {
1342 len=lstrlenA (tabItem->pszText);
1343 if (len>wineItem->cchTextMax)
1344 wineItem->pszText= COMCTL32_ReAlloc (wineItem->pszText, len+1);
1345 lstrcpynA (wineItem->pszText, tabItem->pszText, len);
1352 TAB_GetItemCount (HWND hwnd, WPARAM wParam, LPARAM lParam)
1354 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1356 return infoPtr->uNumItem;
1361 TAB_GetItemA (HWND hwnd, WPARAM wParam, LPARAM lParam)
1363 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1369 tabItem=(LPTCITEMA) lParam;
1371 if ((iItem<0) || (iItem>infoPtr->uNumItem)) return FALSE;
1373 wineItem=& infoPtr->items[iItem];
1375 if (tabItem->mask & TCIF_IMAGE)
1376 tabItem->iImage=wineItem->iImage;
1378 if (tabItem->mask & TCIF_PARAM)
1379 tabItem->lParam=wineItem->lParam;
1381 if (tabItem->mask & TCIF_RTLREADING)
1382 FIXME (tab, "TCIF_RTLREADING\n");
1384 if (tabItem->mask & TCIF_STATE)
1385 tabItem->dwState=wineItem->dwState;
1387 if (tabItem->mask & TCIF_TEXT)
1388 lstrcpynA (tabItem->pszText, wineItem->pszText, tabItem->cchTextMax);
1394 TAB_DeleteItem (HWND hwnd, WPARAM wParam, LPARAM lParam)
1396 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1397 INT iItem = (INT) wParam;
1398 BOOL bResult = FALSE;
1400 if ((iItem >= 0) && (iItem < infoPtr->uNumItem))
1402 TAB_ITEM *oldItems = infoPtr->items;
1404 infoPtr->uNumItem--;
1405 infoPtr->items = COMCTL32_Alloc(sizeof (TAB_ITEM) * infoPtr->uNumItem);
1408 memcpy(&infoPtr->items[0], &oldItems[0], iItem * sizeof(TAB_ITEM));
1410 if (iItem < infoPtr->uNumItem)
1411 memcpy(&infoPtr->items[iItem], &oldItems[iItem + 1],
1412 (infoPtr->uNumItem - iItem) * sizeof(TAB_ITEM));
1414 COMCTL32_Free (oldItems);
1422 TAB_DeleteAllItems (HWND hwnd, WPARAM wParam, LPARAM lParam)
1424 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1426 COMCTL32_Free (infoPtr->items);
1427 infoPtr->uNumItem=0;
1434 TAB_GetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
1436 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1439 return (LRESULT)infoPtr->hFont;
1443 TAB_SetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
1446 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1448 TRACE (tab,"%x %lx\n",wParam, lParam);
1450 infoPtr->hFont = (HFONT)wParam;
1452 TAB_SetItemBounds(hwnd);
1454 TAB_InvalidateTabArea(hwnd, infoPtr);
1461 TAB_GetImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
1463 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1466 return (LRESULT)infoPtr->himl;
1470 TAB_SetImageList (HWND hwnd, WPARAM wParam, LPARAM lParam)
1472 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1473 HIMAGELIST himlPrev;
1476 himlPrev = infoPtr->himl;
1477 infoPtr->himl= (HIMAGELIST)lParam;
1478 return (LRESULT)himlPrev;
1483 TAB_Size (HWND hwnd, WPARAM wParam, LPARAM lParam)
1486 /* I'm not really sure what the following code was meant to do.
1487 This is what it is doing:
1488 When WM_SIZE is sent with SIZE_RESTORED, the control
1489 gets positioned in the top left corner.
1493 UINT uPosFlags,cx,cy;
1497 parent = GetParent (hwnd);
1498 GetClientRect(parent, &parent_rect);
1501 if (GetWindowLongA(hwnd, GWL_STYLE) & CCS_NORESIZE)
1502 uPosFlags |= (SWP_NOSIZE | SWP_NOMOVE);
1504 SetWindowPos (hwnd, 0, parent_rect.left, parent_rect.top,
1505 cx, cy, uPosFlags | SWP_NOZORDER);
1507 FIXME (tab,"WM_SIZE flag %x %lx not handled\n", wParam, lParam);
1511 * Recompute the size/position of the tabs.
1513 TAB_SetItemBounds (hwnd);
1516 * Force a repaint of the control.
1518 InvalidateRect(hwnd, NULL, TRUE);
1525 TAB_Create (HWND hwnd, WPARAM wParam, LPARAM lParam)
1528 TEXTMETRICA fontMetrics;
1532 infoPtr = (TAB_INFO *)COMCTL32_Alloc (sizeof(TAB_INFO));
1534 SetWindowLongA(hwnd, 0, (DWORD)infoPtr);
1536 infoPtr->uNumItem = 0;
1539 infoPtr->hcurArrow = LoadCursorA (0, IDC_ARROWA);
1540 infoPtr->iSelected = 0;
1541 infoPtr->uFocus = 0;
1542 infoPtr->hwndToolTip = 0;
1543 infoPtr->DoRedraw = TRUE;
1544 infoPtr->needsScrolling = FALSE;
1545 infoPtr->hwndUpDown = 0;
1546 infoPtr->leftmostVisible = 0;
1548 TRACE(tab, "Created tab control, hwnd [%04x]\n", hwnd);
1549 if (GetWindowLongA(hwnd, GWL_STYLE) & TCS_TOOLTIPS) {
1550 /* Create tooltip control */
1551 infoPtr->hwndToolTip =
1552 CreateWindowExA (0, TOOLTIPS_CLASSA, NULL, 0,
1553 CW_USEDEFAULT, CW_USEDEFAULT,
1554 CW_USEDEFAULT, CW_USEDEFAULT,
1557 /* Send NM_TOOLTIPSCREATED notification */
1558 if (infoPtr->hwndToolTip) {
1559 NMTOOLTIPSCREATED nmttc;
1561 nmttc.hdr.hwndFrom = hwnd;
1562 nmttc.hdr.idFrom = GetWindowLongA(hwnd, GWL_ID);
1563 nmttc.hdr.code = NM_TOOLTIPSCREATED;
1564 nmttc.hwndToolTips = infoPtr->hwndToolTip;
1566 SendMessageA (GetParent (hwnd), WM_NOTIFY,
1567 (WPARAM)GetWindowLongA(hwnd, GWL_ID), (LPARAM)&nmttc);
1572 * We need to get text information so we need a DC and we need to select
1576 hOldFont = SelectObject (hdc, GetStockObject (SYSTEM_FONT));
1579 * Use the system font to determine the initial height of a tab.
1581 GetTextMetricsA(hdc, &fontMetrics);
1584 * Make sure there is enough space for the letters + growing the
1585 * selected item + extra space for the selected item.
1587 infoPtr->tabHeight = fontMetrics.tmHeight + 2*VERTICAL_ITEM_PADDING +
1588 SELECTED_TAB_OFFSET;
1591 * Initialize the width of a tab.
1593 infoPtr->tabWidth = DEFAULT_TAB_WIDTH;
1595 SelectObject (hdc, hOldFont);
1596 ReleaseDC(hwnd, hdc);
1602 TAB_Destroy (HWND hwnd, WPARAM wParam, LPARAM lParam)
1604 TAB_INFO *infoPtr = TAB_GetInfoPtr(hwnd);
1607 if (infoPtr->items) {
1608 for (iItem = 0; iItem < infoPtr->uNumItem; iItem++) {
1609 if (infoPtr->items[iItem].pszText)
1610 COMCTL32_Free (infoPtr->items[iItem].pszText);
1612 COMCTL32_Free (infoPtr->items);
1615 if (infoPtr->hwndToolTip)
1616 DestroyWindow (infoPtr->hwndToolTip);
1618 if (infoPtr->hwndUpDown)
1619 DestroyWindow(infoPtr->hwndUpDown);
1621 COMCTL32_Free (infoPtr);
1626 TAB_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1630 case TCM_GETIMAGELIST:
1631 return TAB_GetImageList (hwnd, wParam, lParam);
1633 case TCM_SETIMAGELIST:
1634 return TAB_SetImageList (hwnd, wParam, lParam);
1636 case TCM_GETITEMCOUNT:
1637 return TAB_GetItemCount (hwnd, wParam, lParam);
1640 return TAB_GetItemA (hwnd, wParam, lParam);
1643 FIXME (tab, "Unimplemented msg TCM_GETITEMW\n");
1647 return TAB_SetItemA (hwnd, wParam, lParam);
1650 FIXME (tab, "Unimplemented msg TCM_SETITEMW\n");
1653 case TCM_DELETEITEM:
1654 return TAB_DeleteItem (hwnd, wParam, lParam);
1656 case TCM_DELETEALLITEMS:
1657 return TAB_DeleteAllItems (hwnd, wParam, lParam);
1659 case TCM_GETITEMRECT:
1660 return TAB_GetItemRect (hwnd, wParam, lParam);
1663 return TAB_GetCurSel (hwnd);
1666 return TAB_HitTest (hwnd, wParam, lParam);
1669 return TAB_SetCurSel (hwnd, wParam);
1671 case TCM_INSERTITEMA:
1672 return TAB_InsertItem (hwnd, wParam, lParam);
1674 case TCM_INSERTITEMW:
1675 FIXME (tab, "Unimplemented msg TCM_INSERTITEM32W\n");
1678 case TCM_SETITEMEXTRA:
1679 FIXME (tab, "Unimplemented msg TCM_SETITEMEXTRA\n");
1682 case TCM_ADJUSTRECT:
1683 return TAB_AdjustRect (hwnd, (BOOL)wParam, (LPRECT)lParam);
1685 case TCM_SETITEMSIZE:
1686 return TAB_SetItemSize (hwnd, wParam, lParam);
1688 case TCM_REMOVEIMAGE:
1689 FIXME (tab, "Unimplemented msg TCM_REMOVEIMAGE\n");
1692 case TCM_SETPADDING:
1693 FIXME (tab, "Unimplemented msg TCM_SETPADDING\n");
1696 case TCM_GETROWCOUNT:
1697 FIXME (tab, "Unimplemented msg TCM_GETROWCOUNT\n");
1700 case TCM_GETUNICODEFORMAT:
1701 FIXME (tab, "Unimplemented msg TCM_GETUNICODEFORMAT\n");
1704 case TCM_SETUNICODEFORMAT:
1705 FIXME (tab, "Unimplemented msg TCM_SETUNICODEFORMAT\n");
1708 case TCM_HIGHLIGHTITEM:
1709 FIXME (tab, "Unimplemented msg TCM_HIGHLIGHTITEM\n");
1712 case TCM_GETTOOLTIPS:
1713 return TAB_GetToolTips (hwnd, wParam, lParam);
1715 case TCM_SETTOOLTIPS:
1716 return TAB_SetToolTips (hwnd, wParam, lParam);
1718 case TCM_GETCURFOCUS:
1719 return TAB_GetCurFocus (hwnd);
1721 case TCM_SETCURFOCUS:
1722 return TAB_SetCurFocus (hwnd, wParam);
1724 case TCM_SETMINTTABWIDTH:
1725 FIXME (tab, "Unimplemented msg TCM_SETMINTTABWIDTH\n");
1728 case TCM_DESELECTALL:
1729 FIXME (tab, "Unimplemented msg TCM_DESELECTALL\n");
1732 case TCM_GETEXTENDEDSTYLE:
1733 FIXME (tab, "Unimplemented msg TCM_GETEXTENDEDSTYLE\n");
1736 case TCM_SETEXTENDEDSTYLE:
1737 FIXME (tab, "Unimplemented msg TCM_SETEXTENDEDSTYLE\n");
1741 return TAB_GetFont (hwnd, wParam, lParam);
1744 return TAB_SetFont (hwnd, wParam, lParam);
1747 return TAB_Create (hwnd, wParam, lParam);
1750 return TAB_Destroy (hwnd, wParam, lParam);
1753 return DLGC_WANTARROWS | DLGC_WANTCHARS;
1755 case WM_LBUTTONDOWN:
1756 return TAB_LButtonDown (hwnd, wParam, lParam);
1759 return TAB_LButtonUp (hwnd, wParam, lParam);
1761 case WM_RBUTTONDOWN:
1762 return TAB_RButtonDown (hwnd, wParam, lParam);
1765 return TAB_MouseMove (hwnd, wParam, lParam);
1768 return TAB_EraseBackground (hwnd, (HDC)wParam);
1771 return TAB_Paint (hwnd, wParam);
1774 return TAB_Size (hwnd, wParam, lParam);
1777 return TAB_SetRedraw (hwnd, wParam);
1780 return TAB_OnHScroll(hwnd, (int)LOWORD(wParam), (int)HIWORD(wParam), (HWND)lParam);
1784 return TAB_FocusChanging(hwnd, uMsg, wParam, lParam);
1787 return TAB_KeyUp(hwnd, wParam);
1790 if (uMsg >= WM_USER)
1791 ERR (tab, "unknown msg %04x wp=%08x lp=%08lx\n",
1792 uMsg, wParam, lParam);
1793 return DefWindowProcA (hwnd, uMsg, wParam, lParam);
1805 if (GlobalFindAtomA (WC_TABCONTROLA)) return;
1807 ZeroMemory (&wndClass, sizeof(WNDCLASSA));
1808 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS | CS_SAVEBITS;
1809 wndClass.lpfnWndProc = (WNDPROC)TAB_WindowProc;
1810 wndClass.cbClsExtra = 0;
1811 wndClass.cbWndExtra = sizeof(TAB_INFO *);
1812 wndClass.hCursor = LoadCursorA (0, IDC_ARROWA);
1813 wndClass.hbrBackground = (HBRUSH)NULL;
1814 wndClass.lpszClassName = WC_TABCONTROLA;
1816 RegisterClassA (&wndClass);
1821 TAB_Unregister (VOID)
1823 if (GlobalFindAtomA (WC_TABCONTROLA))
1824 UnregisterClassA (WC_TABCONTROLA, (HINSTANCE)NULL);