4 * Copyright 2004 - 2006 Thomas Weidenmueller <w3seek@reactos.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * This code was audited for completeness against the documented features
23 * of Comctl32.dll version 6.0 on Apr. 4, 2005, by Dimitrie O. Paun.
25 * Unless otherwise noted, we believe this code to be complete, as per
26 * the specification mentioned above.
27 * If you discover missing features, or bugs, please note them below.
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(progress);
44 INT WINAPI StrCmpNIW(LPCWSTR,LPCWSTR,INT);
51 } DOC_TEXTBLOCK, *PDOC_TEXTBLOCK;
53 #define LIF_FLAGSMASK (LIF_STATE | LIF_ITEMID | LIF_URL)
54 #define LIS_MASK (LIS_FOCUSED | LIS_ENABLED | LIS_VISITED)
62 typedef struct _DOC_ITEM
64 struct _DOC_ITEM *Next; /* Address to the next item */
65 UINT nText; /* Number of characters of the text */
66 SL_ITEM_TYPE Type; /* type of the item */
67 PDOC_TEXTBLOCK Blocks; /* Array of text blocks */
72 UINT state; /* Link state */
73 WCHAR *szID; /* Link ID string */
74 WCHAR *szUrl; /* Link URL string */
81 WCHAR Text[1]; /* Text of the document item */
82 } DOC_ITEM, *PDOC_ITEM;
86 HWND Self; /* The window handle for this control */
87 HWND Notify; /* The parent handle to receive notifications */
88 DWORD Style; /* Styles for this control */
89 PDOC_ITEM Items; /* Address to the first document item */
90 BOOL HasFocus; /* Whether the control has the input focus */
91 int MouseDownID; /* ID of the link that the mouse button first selected */
92 HFONT Font; /* Handle to the font for text */
93 HFONT LinkFont; /* Handle to the font for links */
94 COLORREF TextColor; /* Color of the text */
95 COLORREF LinkColor; /* Color of links */
96 COLORREF VisitedColor; /* Color of visited links */
97 WCHAR BreakChar; /* Break Character for the current font */
100 static const WCHAR SL_LINKOPEN[] = { '<','a', 0 };
101 static const WCHAR SL_HREF[] = { 'h','r','e','f','=','\"',0 };
102 static const WCHAR SL_ID[] = { 'i','d','=','\"',0 };
103 static const WCHAR SL_LINKCLOSE[] = { '<','/','a','>',0 };
105 /* Control configuration constants */
107 #define SL_LEFTMARGIN (0)
108 #define SL_TOPMARGIN (0)
109 #define SL_RIGHTMARGIN (0)
110 #define SL_BOTTOMMARGIN (0)
112 /***********************************************************************
113 * SYSLINK_FreeDocItem
114 * Frees all data and gdi objects associated with a document item
116 static VOID SYSLINK_FreeDocItem (PDOC_ITEM DocItem)
118 if(DocItem->Type == slLink)
120 Free(DocItem->u.Link.szID);
121 Free(DocItem->u.Link.szUrl);
124 /* we don't free Text because it's just a pointer to a character in the
125 entire window text string */
130 /***********************************************************************
131 * SYSLINK_AppendDocItem
132 * Create and append a new document item.
134 static PDOC_ITEM SYSLINK_AppendDocItem (SYSLINK_INFO *infoPtr, LPCWSTR Text, UINT textlen,
135 SL_ITEM_TYPE type, PDOC_ITEM LastItem)
139 textlen = min(textlen, strlenW(Text));
140 Item = Alloc(FIELD_OFFSET(DOC_ITEM, Text[textlen + 1]));
143 ERR("Failed to alloc DOC_ITEM structure!\n");
148 Item->nText = textlen;
154 LastItem->Next = Item;
158 infoPtr->Items = Item;
161 lstrcpynW(Item->Text, Text, textlen + 1);
166 /***********************************************************************
168 * Clears the document tree
170 static VOID SYSLINK_ClearDoc (SYSLINK_INFO *infoPtr)
172 PDOC_ITEM Item, Next;
174 Item = infoPtr->Items;
178 SYSLINK_FreeDocItem(Item);
182 infoPtr->Items = NULL;
185 /***********************************************************************
187 * Parses the window text string and creates a document. Returns the
188 * number of document items created.
190 static UINT SYSLINK_ParseText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
192 LPCWSTR current, textstart = NULL, linktext = NULL, firsttag = NULL;
193 int taglen = 0, textlen = 0, linklen = 0, docitems = 0;
194 PDOC_ITEM Last = NULL;
195 SL_ITEM_TYPE CurrentType = slText;
199 for(current = Text; *current != 0;)
203 if(!StrCmpNIW(current, SL_LINKOPEN, 2) && (CurrentType == slText))
205 BOOL ValidParam = FALSE, ValidLink = FALSE;
207 if(*(current + 2) == '>')
209 /* we just have to deal with a <a> tag */
218 else if(*(current + 2) == infoPtr->BreakChar)
220 /* we expect parameters, parse them */
221 LPCWSTR *CurrentParameter = NULL, tmp;
222 UINT *CurrentParameterLen = NULL;
225 tmp = current + taglen;
230 /* compare the current position with all known parameters */
231 if(!StrCmpNIW(tmp, SL_HREF, 6))
235 CurrentParameter = &lpUrl;
236 CurrentParameterLen = &lenUrl;
238 else if(!StrCmpNIW(tmp, SL_ID, 4))
242 CurrentParameter = &lpID;
243 CurrentParameterLen = &lenId;
252 /* we got a known parameter, now search until the next " character.
253 If we can't find a " character, there's a syntax error and we just assume it's text */
255 *CurrentParameter = current + taglen;
256 *CurrentParameterLen = 0;
258 for(tmp = *CurrentParameter; *tmp != 0; tmp++)
267 (*CurrentParameterLen)++;
272 /* we're done with this parameter, now there are only 2 possibilities:
273 * 1. another parameter is coming, so expect a ' ' (space) character
274 * 2. the tag is being closed, so expect a '<' character
276 if(*tmp == infoPtr->BreakChar)
278 /* we expect another parameter, do the whole thing again */
285 /* the tag is being closed, we're done */
292 if(ValidLink && ValidParam)
294 /* the <a ...> tag appears to be valid. save all information
295 so we can add the link if we find a valid </a> tag later */
296 CurrentType = slLink;
297 linktext = current + taglen;
306 if(textstart == NULL)
312 else if(!StrCmpNIW(current, SL_LINKCLOSE, 4) && (CurrentType == slLink) && firsttag)
314 /* there's a <a...> tag opened, first add the previous text, if present */
315 if(textstart != NULL && textlen > 0 && firsttag > textstart)
317 Last = SYSLINK_AppendDocItem(infoPtr, textstart, firsttag - textstart, slText, Last);
320 ERR("Unable to create new document item!\n");
328 /* now it's time to add the link to the document */
330 if(linktext != NULL && linklen > 0)
332 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slLink, Last);
335 ERR("Unable to create new document item!\n");
339 if(CurrentType == slLink)
343 if(!(infoPtr->Style & WS_DISABLED))
345 Last->u.Link.state |= LIS_ENABLED;
347 /* Copy the tag parameters */
350 nc = min(lenId, strlenW(lpID));
351 nc = min(nc, MAX_LINKID_TEXT - 1);
352 Last->u.Link.szID = Alloc((nc + 1) * sizeof(WCHAR));
353 if(Last->u.Link.szID != NULL)
355 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
359 Last->u.Link.szID = NULL;
362 nc = min(lenUrl, strlenW(lpUrl));
363 nc = min(nc, L_MAX_URL_LENGTH - 1);
364 Last->u.Link.szUrl = Alloc((nc + 1) * sizeof(WCHAR));
365 if(Last->u.Link.szUrl != NULL)
367 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
371 Last->u.Link.szUrl = NULL;
375 CurrentType = slText;
382 /* we don't know what tag it is, so just continue */
385 if(CurrentType == slText && textstart == NULL)
399 /* save the pointer of the current text item if we couldn't find a tag */
400 if(textstart == NULL && CurrentType == slText)
409 if(textstart != NULL && textlen > 0)
411 Last = SYSLINK_AppendDocItem(infoPtr, textstart, textlen, CurrentType, Last);
414 ERR("Unable to create new document item!\n");
417 if(CurrentType == slLink)
421 if(!(infoPtr->Style & WS_DISABLED))
423 Last->u.Link.state |= LIS_ENABLED;
425 /* Copy the tag parameters */
428 nc = min(lenId, strlenW(lpID));
429 nc = min(nc, MAX_LINKID_TEXT - 1);
430 Last->u.Link.szID = Alloc((nc + 1) * sizeof(WCHAR));
431 if(Last->u.Link.szID != NULL)
433 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
437 Last->u.Link.szID = NULL;
440 nc = min(lenUrl, strlenW(lpUrl));
441 nc = min(nc, L_MAX_URL_LENGTH - 1);
442 Last->u.Link.szUrl = Alloc((nc + 1) * sizeof(WCHAR));
443 if(Last->u.Link.szUrl != NULL)
445 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
449 Last->u.Link.szUrl = NULL;
454 if(linktext != NULL && linklen > 0)
456 /* we got an unclosed link, just display the text */
457 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slText, Last);
460 ERR("Unable to create new document item!\n");
469 /***********************************************************************
470 * SYSLINK_RepaintLink
473 static VOID SYSLINK_RepaintLink (const SYSLINK_INFO *infoPtr, const DOC_ITEM *DocItem)
478 if(DocItem->Type != slLink)
480 ERR("DocItem not a link!\n");
484 bl = DocItem->Blocks;
491 InvalidateRect(infoPtr->Self, &bl->rc, TRUE);
492 n -= bl->nChars + bl->nSkip;
498 /***********************************************************************
499 * SYSLINK_GetLinkItemByIndex
500 * Retrieves a document link by its index
502 static PDOC_ITEM SYSLINK_GetLinkItemByIndex (const SYSLINK_INFO *infoPtr, int iLink)
504 PDOC_ITEM Current = infoPtr->Items;
506 while(Current != NULL)
508 if((Current->Type == slLink) && (iLink-- <= 0))
512 Current = Current->Next;
517 /***********************************************************************
518 * SYSLINK_GetFocusLink
519 * Retrieves the link that has the LIS_FOCUSED bit
521 static PDOC_ITEM SYSLINK_GetFocusLink (const SYSLINK_INFO *infoPtr, int *LinkId)
523 PDOC_ITEM Current = infoPtr->Items;
526 while(Current != NULL)
528 if((Current->Type == slLink))
530 if(Current->u.Link.state & LIS_FOCUSED)
538 Current = Current->Next;
543 /***********************************************************************
544 * SYSLINK_GetNextLink
547 static PDOC_ITEM SYSLINK_GetNextLink (const SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
549 for(Current = (Current != NULL ? Current->Next : infoPtr->Items);
551 Current = Current->Next)
553 if(Current->Type == slLink)
561 /***********************************************************************
562 * SYSLINK_GetPrevLink
563 * Gets the previous link
565 static PDOC_ITEM SYSLINK_GetPrevLink (const SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
569 /* returns the last link */
570 PDOC_ITEM Last = NULL;
572 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
574 if(Current->Type == slLink)
583 /* returns the previous link */
584 PDOC_ITEM Cur, Prev = NULL;
586 for(Cur = infoPtr->Items; Cur != NULL; Cur = Cur->Next)
592 if(Cur->Type == slLink)
601 /***********************************************************************
603 * Tries to wrap a line.
605 static BOOL SYSLINK_WrapLine (LPWSTR Text, WCHAR BreakChar, int *LineLen,
606 int nFit, LPSIZE Extent)
617 Current = Text + nFit;
619 /* check if we're in the middle of a word */
620 if((*Current) != BreakChar)
622 /* search for the beginning of the word */
623 while(Current > Text && (*(Current - 1)) != BreakChar)
640 /***********************************************************************
642 * Renders the document in memory
644 static VOID SYSLINK_Render (const SYSLINK_INFO *infoPtr, HDC hdc, PRECT pRect)
649 int x, y, LineHeight;
652 szDoc.cx = szDoc.cy = 0;
655 rc.right -= SL_RIGHTMARGIN;
656 rc.bottom -= SL_BOTTOMMARGIN;
658 if(rc.right - SL_LEFTMARGIN < 0)
660 if (rc.bottom - SL_TOPMARGIN < 0)
663 hOldFont = SelectObject(hdc, infoPtr->Font);
669 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
673 PDOC_TEXTBLOCK bl, cbl;
677 if(Current->nText == 0)
685 Free(Current->Blocks);
686 Current->Blocks = NULL;
690 if(Current->Type == slText)
692 SelectObject(hdc, infoPtr->Font);
694 else if(Current->Type == slLink)
696 SelectObject(hdc, infoPtr->LinkFont);
703 /* skip break characters unless they're the first of the doc item */
704 if(tx != Current->Text || x == SL_LEFTMARGIN)
706 while(n > 0 && (*tx) == infoPtr->BreakChar)
714 if((n == 0 && SkipChars != 0) ||
715 GetTextExtentExPointW(hdc, tx, n, rc.right - x, &nFit, NULL, &szDim))
723 Wrap = SYSLINK_WrapLine(tx, infoPtr->BreakChar, &LineLen, nFit, &szDim);
727 if(x > SL_LEFTMARGIN)
729 /* move one line down, the word didn't fit into the line */
737 /* the word starts at the beginning of the line and doesn't
738 fit into the line, so break it at the last character that fits */
739 LineLen = max(nFit, 1);
745 if(!GetTextExtentExPointW(hdc, tx, LineLen, rc.right - x, NULL, NULL, &szDim))
758 nbl = ReAlloc(bl, (nBlocks + 1) * sizeof(DOC_TEXTBLOCK));
764 cbl = bl + nBlocks - 1;
766 cbl->nChars = LineLen;
767 cbl->nSkip = SkipChars;
770 cbl->rc.right = x + szDim.cx;
771 cbl->rc.bottom = y + szDim.cy;
773 if (cbl->rc.right > szDoc.cx)
774 szDoc.cx = cbl->rc.right;
775 if (cbl->rc.bottom > szDoc.cy)
776 szDoc.cy = cbl->rc.bottom;
781 LineHeight = max(LineHeight, szDim.cy);
797 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
811 Current->Blocks = bl;
815 SelectObject(hdc, hOldFont);
817 pRect->right = pRect->left + szDoc.cx;
818 pRect->bottom = pRect->top + szDoc.cy;
821 /***********************************************************************
823 * Draws the SysLink control.
825 static LRESULT SYSLINK_Draw (const SYSLINK_INFO *infoPtr, HDC hdc)
830 COLORREF OldTextColor, OldBkColor;
832 hOldFont = SelectObject(hdc, infoPtr->Font);
833 OldTextColor = SetTextColor(hdc, infoPtr->TextColor);
834 OldBkColor = SetBkColor(hdc, comctl32_color.clrBtnFace);
836 GetClientRect(infoPtr->Self, &rc);
837 rc.right -= SL_RIGHTMARGIN + SL_LEFTMARGIN;
838 rc.bottom -= SL_BOTTOMMARGIN + SL_TOPMARGIN;
840 if(rc.right < 0 || rc.bottom < 0) return 0;
842 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
848 bl = Current->Blocks;
854 if(Current->Type == slText)
856 SelectObject(hdc, infoPtr->Font);
857 SetTextColor(hdc, infoPtr->TextColor);
861 SelectObject(hdc, infoPtr->LinkFont);
862 SetTextColor(hdc, (!(Current->u.Link.state & LIS_VISITED) ? infoPtr->LinkColor : infoPtr->VisitedColor));
868 ExtTextOutW(hdc, bl->rc.left, bl->rc.top, ETO_OPAQUE | ETO_CLIPPED, &bl->rc, tx, bl->nChars, NULL);
869 if((Current->Type == slLink) && (Current->u.Link.state & LIS_FOCUSED) && infoPtr->HasFocus)
871 COLORREF PrevTextColor;
872 PrevTextColor = SetTextColor(hdc, infoPtr->TextColor);
873 DrawFocusRect(hdc, &bl->rc);
874 SetTextColor(hdc, PrevTextColor);
877 n -= bl->nChars + bl->nSkip;
883 SetBkColor(hdc, OldBkColor);
884 SetTextColor(hdc, OldTextColor);
885 SelectObject(hdc, hOldFont);
891 /***********************************************************************
893 * Handles the WM_PAINT message.
895 static LRESULT SYSLINK_Paint (const SYSLINK_INFO *infoPtr, HDC hdcParam)
900 hdc = hdcParam ? hdcParam : BeginPaint (infoPtr->Self, &ps);
903 SYSLINK_Draw (infoPtr, hdc);
904 if (!hdcParam) EndPaint (infoPtr->Self, &ps);
910 /***********************************************************************
912 * Set new Font for the SysLink control.
914 static HFONT SYSLINK_SetFont (SYSLINK_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
920 HFONT hOldFont = infoPtr->Font;
921 infoPtr->Font = hFont;
923 /* free the underline font */
924 if(infoPtr->LinkFont != NULL)
926 DeleteObject(infoPtr->LinkFont);
927 infoPtr->LinkFont = NULL;
930 /* Render text position and word wrapping in memory */
931 if (GetClientRect(infoPtr->Self, &rcClient))
933 hdc = GetDC(infoPtr->Self);
936 /* create a new underline font */
937 if(GetTextMetricsW(hdc, &tm) &&
938 GetObjectW(infoPtr->Font, sizeof(LOGFONTW), &lf))
940 lf.lfUnderline = TRUE;
941 infoPtr->LinkFont = CreateFontIndirectW(&lf);
942 infoPtr->BreakChar = tm.tmBreakChar;
946 ERR("Failed to create link font!\n");
949 SYSLINK_Render(infoPtr, hdc, &rcClient);
950 ReleaseDC(infoPtr->Self, hdc);
956 RedrawWindow(infoPtr->Self, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
962 /***********************************************************************
964 * Set new text for the SysLink control.
966 static LRESULT SYSLINK_SetText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
968 /* clear the document */
969 SYSLINK_ClearDoc(infoPtr);
971 if(Text == NULL || *Text == 0)
976 /* let's parse the string and create a document */
977 if(SYSLINK_ParseText(infoPtr, Text) > 0)
981 /* Render text position and word wrapping in memory */
982 if (GetClientRect(infoPtr->Self, &rcClient))
984 HDC hdc = GetDC(infoPtr->Self);
987 SYSLINK_Render(infoPtr, hdc, &rcClient);
988 ReleaseDC(infoPtr->Self, hdc);
990 InvalidateRect(infoPtr->Self, NULL, TRUE);
998 /***********************************************************************
999 * SYSLINK_SetFocusLink
1000 * Updates the focus status bits and focusses the specified link.
1001 * If no document item is specified, the focus bit will be removed from all links.
1002 * Returns the previous focused item.
1004 static PDOC_ITEM SYSLINK_SetFocusLink (const SYSLINK_INFO *infoPtr, const DOC_ITEM *DocItem)
1006 PDOC_ITEM Current, PrevFocus = NULL;
1008 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1010 if(Current->Type == slLink)
1012 if((PrevFocus == NULL) && (Current->u.Link.state & LIS_FOCUSED))
1014 PrevFocus = Current;
1017 if(Current == DocItem)
1019 Current->u.Link.state |= LIS_FOCUSED;
1023 Current->u.Link.state &= ~LIS_FOCUSED;
1031 /***********************************************************************
1033 * Sets the states and attributes of a link item.
1035 static LRESULT SYSLINK_SetItem (const SYSLINK_INFO *infoPtr, const LITEM *Item)
1041 BOOL Repaint = FALSE;
1043 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1045 ERR("Invalid Flags!\n");
1049 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1052 ERR("Link %d couldn't be found\n", Item->iLink);
1056 if(Item->mask & LIF_ITEMID)
1058 nc = min(lstrlenW(Item->szID), MAX_LINKID_TEXT - 1);
1059 szId = Alloc((nc + 1) * sizeof(WCHAR));
1062 lstrcpynW(szId, Item->szID, nc + 1);
1066 ERR("Unable to allocate memory for link id\n");
1071 if(Item->mask & LIF_URL)
1073 nc = min(lstrlenW(Item->szUrl), L_MAX_URL_LENGTH - 1);
1074 szUrl = Alloc((nc + 1) * sizeof(WCHAR));
1077 lstrcpynW(szUrl, Item->szUrl, nc + 1);
1083 ERR("Unable to allocate memory for link url\n");
1088 if(Item->mask & LIF_ITEMID)
1090 Free(di->u.Link.szID);
1091 di->u.Link.szID = szId;
1094 if(Item->mask & LIF_URL)
1096 Free(di->u.Link.szUrl);
1097 di->u.Link.szUrl = szUrl;
1100 if(Item->mask & LIF_STATE)
1102 UINT oldstate = di->u.Link.state;
1103 /* clear the masked bits */
1104 di->u.Link.state &= ~(Item->stateMask & LIS_MASK);
1106 di->u.Link.state |= (Item->state & Item->stateMask) & LIS_MASK;
1107 Repaint = (oldstate != di->u.Link.state);
1109 /* update the focus */
1110 SYSLINK_SetFocusLink(infoPtr, ((di->u.Link.state & LIS_FOCUSED) ? di : NULL));
1115 SYSLINK_RepaintLink(infoPtr, di);
1121 /***********************************************************************
1123 * Retrieves the states and attributes of a link item.
1125 static LRESULT SYSLINK_GetItem (const SYSLINK_INFO *infoPtr, PLITEM Item)
1129 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1131 ERR("Invalid Flags!\n");
1135 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1138 ERR("Link %d couldn't be found\n", Item->iLink);
1142 if(Item->mask & LIF_STATE)
1144 Item->state = (di->u.Link.state & Item->stateMask);
1145 if(!infoPtr->HasFocus)
1147 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1148 Item->state &= ~LIS_FOCUSED;
1152 if(Item->mask & LIF_ITEMID)
1156 lstrcpyW(Item->szID, di->u.Link.szID);
1164 if(Item->mask & LIF_URL)
1166 if(di->u.Link.szUrl)
1168 lstrcpyW(Item->szUrl, di->u.Link.szUrl);
1179 /***********************************************************************
1180 * SYSLINK_PtInDocItem
1181 * Determines if a point is in the region of a document item
1183 static BOOL SYSLINK_PtInDocItem (const DOC_ITEM *DocItem, POINT pt)
1188 bl = DocItem->Blocks;
1195 if (PtInRect(&bl->rc, pt))
1199 n -= bl->nChars + bl->nSkip;
1207 /***********************************************************************
1209 * Determines the link the user clicked on.
1211 static LRESULT SYSLINK_HitTest (const SYSLINK_INFO *infoPtr, PLHITTESTINFO HitTest)
1216 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1218 if(Current->Type == slLink)
1220 if(SYSLINK_PtInDocItem(Current, HitTest->pt))
1222 HitTest->item.mask = 0;
1223 HitTest->item.iLink = id;
1224 HitTest->item.state = 0;
1225 HitTest->item.stateMask = 0;
1226 if(Current->u.Link.szID)
1228 lstrcpyW(HitTest->item.szID, Current->u.Link.szID);
1232 HitTest->item.szID[0] = 0;
1234 if(Current->u.Link.szUrl)
1236 lstrcpyW(HitTest->item.szUrl, Current->u.Link.szUrl);
1240 HitTest->item.szUrl[0] = 0;
1251 /***********************************************************************
1252 * SYSLINK_GetIdealHeight
1253 * Returns the preferred height of a link at the current control's width.
1255 static LRESULT SYSLINK_GetIdealHeight (const SYSLINK_INFO *infoPtr)
1257 HDC hdc = GetDC(infoPtr->Self);
1262 HGDIOBJ hOldFont = SelectObject(hdc, infoPtr->Font);
1264 if(GetTextMetricsW(hdc, &tm))
1266 height = tm.tmHeight;
1272 SelectObject(hdc, hOldFont);
1273 ReleaseDC(infoPtr->Self, hdc);
1280 /***********************************************************************
1281 * SYSLINK_SendParentNotify
1282 * Sends a WM_NOTIFY message to the parent window.
1284 static LRESULT SYSLINK_SendParentNotify (const SYSLINK_INFO *infoPtr, UINT code, const DOC_ITEM *Link, int iLink)
1288 nml.hdr.hwndFrom = infoPtr->Self;
1289 nml.hdr.idFrom = GetWindowLongPtrW(infoPtr->Self, GWLP_ID);
1290 nml.hdr.code = code;
1293 nml.item.iLink = iLink;
1295 nml.item.stateMask = 0;
1296 if(Link->u.Link.szID)
1298 lstrcpyW(nml.item.szID, Link->u.Link.szID);
1302 nml.item.szID[0] = 0;
1304 if(Link->u.Link.szUrl)
1306 lstrcpyW(nml.item.szUrl, Link->u.Link.szUrl);
1310 nml.item.szUrl[0] = 0;
1313 return SendMessageW(infoPtr->Notify, WM_NOTIFY, nml.hdr.idFrom, (LPARAM)&nml);
1316 /***********************************************************************
1318 * Handles receiving the input focus.
1320 static LRESULT SYSLINK_SetFocus (SYSLINK_INFO *infoPtr)
1324 infoPtr->HasFocus = TRUE;
1326 /* We always select the first link, even if we activated the control using
1327 SHIFT+TAB. This is the default behavior */
1328 Focus = SYSLINK_GetNextLink(infoPtr, NULL);
1331 SYSLINK_SetFocusLink(infoPtr, Focus);
1332 SYSLINK_RepaintLink(infoPtr, Focus);
1337 /***********************************************************************
1339 * Handles losing the input focus.
1341 static LRESULT SYSLINK_KillFocus (SYSLINK_INFO *infoPtr)
1345 infoPtr->HasFocus = FALSE;
1346 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1350 SYSLINK_RepaintLink(infoPtr, Focus);
1356 /***********************************************************************
1358 * Returns a link at the specified position
1360 static PDOC_ITEM SYSLINK_LinkAtPt (const SYSLINK_INFO *infoPtr, const POINT *pt, int *LinkId, BOOL MustBeEnabled)
1365 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1367 if((Current->Type == slLink) && SYSLINK_PtInDocItem(Current, *pt) &&
1368 (!MustBeEnabled || (MustBeEnabled && (Current->u.Link.state & LIS_ENABLED))))
1382 /***********************************************************************
1383 * SYSLINK_LButtonDown
1384 * Handles mouse clicks
1386 static LRESULT SYSLINK_LButtonDown (SYSLINK_INFO *infoPtr, const POINT *pt)
1388 PDOC_ITEM Current, Old;
1391 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1394 SetFocus(infoPtr->Self);
1396 Old = SYSLINK_SetFocusLink(infoPtr, Current);
1397 if(Old != NULL && Old != Current)
1399 SYSLINK_RepaintLink(infoPtr, Old);
1401 infoPtr->MouseDownID = id;
1402 SYSLINK_RepaintLink(infoPtr, Current);
1408 /***********************************************************************
1410 * Handles mouse clicks
1412 static LRESULT SYSLINK_LButtonUp (SYSLINK_INFO *infoPtr, const POINT *pt)
1414 if(infoPtr->MouseDownID > -1)
1419 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1420 if((Current != NULL) && (Current->u.Link.state & LIS_FOCUSED) && (infoPtr->MouseDownID == id))
1422 SYSLINK_SendParentNotify(infoPtr, NM_CLICK, Current, id);
1426 infoPtr->MouseDownID = -1;
1431 /***********************************************************************
1433 * Handles ENTER key events
1435 static BOOL SYSLINK_OnEnter (const SYSLINK_INFO *infoPtr)
1437 if(infoPtr->HasFocus)
1442 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1445 SYSLINK_SendParentNotify(infoPtr, NM_RETURN, Focus, id);
1452 /***********************************************************************
1453 * SYSKEY_SelectNextPrevLink
1454 * Changes the currently focused link
1456 static BOOL SYSKEY_SelectNextPrevLink (const SYSLINK_INFO *infoPtr, BOOL Prev)
1458 if(infoPtr->HasFocus)
1463 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1466 PDOC_ITEM NewFocus, OldFocus;
1469 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1471 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1473 if(NewFocus != NULL)
1475 OldFocus = SYSLINK_SetFocusLink(infoPtr, NewFocus);
1477 if(OldFocus && OldFocus != NewFocus)
1479 SYSLINK_RepaintLink(infoPtr, OldFocus);
1481 SYSLINK_RepaintLink(infoPtr, NewFocus);
1489 /***********************************************************************
1490 * SYSKEY_SelectNextPrevLink
1491 * Determines if there's a next or previous link to decide whether the control
1492 * should capture the tab key message
1494 static BOOL SYSLINK_NoNextLink (const SYSLINK_INFO *infoPtr, BOOL Prev)
1496 PDOC_ITEM Focus, NewFocus;
1498 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1500 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1502 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1504 return NewFocus == NULL;
1507 /***********************************************************************
1508 * SYSLINK_GetIdealSize
1509 * Calculates the ideal size of a link control at a given maximum width.
1511 static VOID SYSLINK_GetIdealSize (const SYSLINK_INFO *infoPtr, int cxMaxWidth, LPSIZE lpSize)
1516 rc.left = rc.top = rc.bottom = 0;
1517 rc.right = cxMaxWidth;
1519 hdc = GetDC(infoPtr->Self);
1522 HGDIOBJ hOldFont = SelectObject(hdc, infoPtr->Font);
1524 SYSLINK_Render(infoPtr, hdc, &rc);
1526 SelectObject(hdc, hOldFont);
1527 ReleaseDC(infoPtr->Self, hdc);
1529 lpSize->cx = rc.right;
1530 lpSize->cy = rc.bottom;
1534 /***********************************************************************
1537 static LRESULT WINAPI SysLinkWindowProc(HWND hwnd, UINT message,
1538 WPARAM wParam, LPARAM lParam)
1540 SYSLINK_INFO *infoPtr;
1542 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd, message, wParam, lParam);
1544 infoPtr = (SYSLINK_INFO *)GetWindowLongPtrW(hwnd, 0);
1546 if (!infoPtr && message != WM_CREATE)
1547 goto HandleDefaultMessage;
1550 case WM_PRINTCLIENT:
1552 return SYSLINK_Paint (infoPtr, (HDC)wParam);
1557 DWORD mp = GetMessagePos();
1559 ht.pt.x = (short)LOWORD(mp);
1560 ht.pt.y = (short)HIWORD(mp);
1562 ScreenToClient(infoPtr->Self, &ht.pt);
1563 if(SYSLINK_HitTest (infoPtr, &ht))
1565 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_HAND));
1568 /* let the default window proc handle this message */
1569 goto HandleDefaultMessage;
1575 if (GetClientRect(infoPtr->Self, &rcClient))
1577 HDC hdc = GetDC(infoPtr->Self);
1580 SYSLINK_Render(infoPtr, hdc, &rcClient);
1581 ReleaseDC(infoPtr->Self, hdc);
1588 return (LRESULT)infoPtr->Font;
1591 return (LRESULT)SYSLINK_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1594 SYSLINK_SetText(infoPtr, (LPWSTR)lParam);
1595 goto HandleDefaultMessage;
1597 case WM_LBUTTONDOWN:
1600 pt.x = (short)LOWORD(lParam);
1601 pt.y = (short)HIWORD(lParam);
1602 return SYSLINK_LButtonDown(infoPtr, &pt);
1607 pt.x = (short)LOWORD(lParam);
1608 pt.y = (short)HIWORD(lParam);
1609 return SYSLINK_LButtonUp(infoPtr, &pt);
1617 SYSLINK_OnEnter(infoPtr);
1621 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1622 SYSKEY_SelectNextPrevLink(infoPtr, shift);
1626 goto HandleDefaultMessage;
1631 LRESULT Ret = DLGC_HASSETSEL;
1632 int vk = (lParam != 0 ? (int)((LPMSG)lParam)->wParam : 0);
1636 Ret |= DLGC_WANTMESSAGE;
1640 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1641 if(!SYSLINK_NoNextLink(infoPtr, shift))
1643 Ret |= DLGC_WANTTAB;
1647 Ret |= DLGC_WANTCHARS;
1659 pt.x = (short)LOWORD(lParam);
1660 pt.y = (short)HIWORD(lParam);
1662 GetClientRect(infoPtr->Self, &rc);
1663 ScreenToClient(infoPtr->Self, &pt);
1664 if(pt.x < 0 || pt.y < 0 || pt.x > rc.right || pt.y > rc.bottom)
1669 if(SYSLINK_LinkAtPt(infoPtr, &pt, NULL, FALSE))
1674 return HTTRANSPARENT;
1678 return SYSLINK_HitTest(infoPtr, (PLHITTESTINFO)lParam);
1681 return SYSLINK_SetItem(infoPtr, (PLITEM)lParam);
1684 return SYSLINK_GetItem(infoPtr, (PLITEM)lParam);
1686 case LM_GETIDEALHEIGHT:
1689 /* LM_GETIDEALSIZE */
1690 SYSLINK_GetIdealSize(infoPtr, (int)wParam, (LPSIZE)lParam);
1692 return SYSLINK_GetIdealHeight(infoPtr);
1695 return SYSLINK_SetFocus(infoPtr);
1698 return SYSLINK_KillFocus(infoPtr);
1701 infoPtr->Style &= ~WS_DISABLED;
1702 infoPtr->Style |= (wParam ? 0 : WS_DISABLED);
1703 InvalidateRect (infoPtr->Self, NULL, FALSE);
1706 case WM_STYLECHANGED:
1707 if (wParam == GWL_STYLE)
1709 infoPtr->Style = ((LPSTYLESTRUCT)lParam)->styleNew;
1711 InvalidateRect(infoPtr->Self, NULL, TRUE);
1716 /* allocate memory for info struct */
1717 infoPtr = Alloc (sizeof(SYSLINK_INFO));
1718 if (!infoPtr) return -1;
1719 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1721 /* initialize the info struct */
1722 infoPtr->Self = hwnd;
1723 infoPtr->Notify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1724 infoPtr->Style = ((LPCREATESTRUCTW)lParam)->style;
1726 infoPtr->LinkFont = 0;
1727 infoPtr->Items = NULL;
1728 infoPtr->HasFocus = FALSE;
1729 infoPtr->MouseDownID = -1;
1730 infoPtr->TextColor = comctl32_color.clrWindowText;
1731 infoPtr->LinkColor = comctl32_color.clrHighlight;
1732 infoPtr->VisitedColor = comctl32_color.clrHighlight;
1733 infoPtr->BreakChar = ' ';
1734 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd);
1735 SYSLINK_SetText(infoPtr, ((LPCREATESTRUCTW)lParam)->lpszName);
1739 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd);
1740 SYSLINK_ClearDoc(infoPtr);
1741 if(infoPtr->Font != 0) DeleteObject(infoPtr->Font);
1742 if(infoPtr->LinkFont != 0) DeleteObject(infoPtr->LinkFont);
1743 SetWindowLongPtrW(hwnd, 0, 0);
1747 case WM_SYSCOLORCHANGE:
1748 COMCTL32_RefreshSysColors();
1752 HandleDefaultMessage:
1753 if ((message >= WM_USER) && (message < WM_APP) && !COMCTL32_IsReflectedMessage(message))
1755 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message, wParam, lParam );
1757 return DefWindowProcW(hwnd, message, wParam, lParam);
1762 /***********************************************************************
1763 * SYSLINK_Register [Internal]
1765 * Registers the SysLink window class.
1767 VOID SYSLINK_Register (void)
1771 ZeroMemory (&wndClass, sizeof(wndClass));
1772 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1773 wndClass.lpfnWndProc = SysLinkWindowProc;
1774 wndClass.cbClsExtra = 0;
1775 wndClass.cbWndExtra = sizeof (SYSLINK_INFO *);
1776 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1777 wndClass.lpszClassName = WC_LINK;
1779 RegisterClassW (&wndClass);
1783 /***********************************************************************
1784 * SYSLINK_Unregister [Internal]
1786 * Unregisters the SysLink window class.
1788 VOID SYSLINK_Unregister (void)
1790 UnregisterClassW (WC_LINK, NULL);