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(syslink);
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 */
98 BOOL IgnoreReturn; /* (infoPtr->Style & LWS_IGNORERETURN) on creation */
101 static const WCHAR SL_LINKOPEN[] = { '<','a', 0 };
102 static const WCHAR SL_HREF[] = { 'h','r','e','f','=','\"',0 };
103 static const WCHAR SL_ID[] = { 'i','d','=','\"',0 };
104 static const WCHAR SL_LINKCLOSE[] = { '<','/','a','>',0 };
106 /* Control configuration constants */
108 #define SL_LEFTMARGIN (0)
109 #define SL_TOPMARGIN (0)
110 #define SL_RIGHTMARGIN (0)
111 #define SL_BOTTOMMARGIN (0)
113 /***********************************************************************
114 * SYSLINK_FreeDocItem
115 * Frees all data and gdi objects associated with a document item
117 static VOID SYSLINK_FreeDocItem (PDOC_ITEM DocItem)
119 if(DocItem->Type == slLink)
121 Free(DocItem->u.Link.szID);
122 Free(DocItem->u.Link.szUrl);
125 /* we don't free Text because it's just a pointer to a character in the
126 entire window text string */
131 /***********************************************************************
132 * SYSLINK_AppendDocItem
133 * Create and append a new document item.
135 static PDOC_ITEM SYSLINK_AppendDocItem (SYSLINK_INFO *infoPtr, LPCWSTR Text, UINT textlen,
136 SL_ITEM_TYPE type, PDOC_ITEM LastItem)
140 textlen = min(textlen, strlenW(Text));
141 Item = Alloc(FIELD_OFFSET(DOC_ITEM, Text[textlen + 1]));
144 ERR("Failed to alloc DOC_ITEM structure!\n");
149 Item->nText = textlen;
155 LastItem->Next = Item;
159 infoPtr->Items = Item;
162 lstrcpynW(Item->Text, Text, textlen + 1);
167 /***********************************************************************
169 * Clears the document tree
171 static VOID SYSLINK_ClearDoc (SYSLINK_INFO *infoPtr)
173 PDOC_ITEM Item, Next;
175 Item = infoPtr->Items;
179 SYSLINK_FreeDocItem(Item);
183 infoPtr->Items = NULL;
186 /***********************************************************************
188 * Parses the window text string and creates a document. Returns the
189 * number of document items created.
191 static UINT SYSLINK_ParseText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
193 LPCWSTR current, textstart = NULL, linktext = NULL, firsttag = NULL;
194 int taglen = 0, textlen = 0, linklen = 0, docitems = 0;
195 PDOC_ITEM Last = NULL;
196 SL_ITEM_TYPE CurrentType = slText;
200 TRACE("(%p %s)\n", infoPtr, debugstr_w(Text));
202 for(current = Text; *current != 0;)
206 if(!StrCmpNIW(current, SL_LINKOPEN, 2) && (CurrentType == slText))
208 BOOL ValidParam = FALSE, ValidLink = FALSE;
210 if(*(current + 2) == '>')
212 /* we just have to deal with a <a> tag */
221 else if(*(current + 2) == infoPtr->BreakChar)
223 /* we expect parameters, parse them */
224 LPCWSTR *CurrentParameter = NULL, tmp;
225 UINT *CurrentParameterLen = NULL;
228 tmp = current + taglen;
233 /* compare the current position with all known parameters */
234 if(!StrCmpNIW(tmp, SL_HREF, 6))
238 CurrentParameter = &lpUrl;
239 CurrentParameterLen = &lenUrl;
241 else if(!StrCmpNIW(tmp, SL_ID, 4))
245 CurrentParameter = &lpID;
246 CurrentParameterLen = &lenId;
255 /* we got a known parameter, now search until the next " character.
256 If we can't find a " character, there's a syntax error and we just assume it's text */
258 *CurrentParameter = current + taglen;
259 *CurrentParameterLen = 0;
261 for(tmp = *CurrentParameter; *tmp != 0; tmp++)
270 (*CurrentParameterLen)++;
275 /* we're done with this parameter, now there are only 2 possibilities:
276 * 1. another parameter is coming, so expect a ' ' (space) character
277 * 2. the tag is being closed, so expect a '<' character
279 if(*tmp == infoPtr->BreakChar)
281 /* we expect another parameter, do the whole thing again */
288 /* the tag is being closed, we're done */
295 if(ValidLink && ValidParam)
297 /* the <a ...> tag appears to be valid. save all information
298 so we can add the link if we find a valid </a> tag later */
299 CurrentType = slLink;
300 linktext = current + taglen;
309 if(textstart == NULL)
315 else if(!StrCmpNIW(current, SL_LINKCLOSE, 4) && (CurrentType == slLink) && firsttag)
317 /* there's a <a...> tag opened, first add the previous text, if present */
318 if(textstart != NULL && textlen > 0 && firsttag > textstart)
320 Last = SYSLINK_AppendDocItem(infoPtr, textstart, firsttag - textstart, slText, Last);
323 ERR("Unable to create new document item!\n");
331 /* now it's time to add the link to the document */
333 if(linktext != NULL && linklen > 0)
335 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slLink, Last);
338 ERR("Unable to create new document item!\n");
342 if(CurrentType == slLink)
346 if(!(infoPtr->Style & WS_DISABLED))
348 Last->u.Link.state |= LIS_ENABLED;
350 /* Copy the tag parameters */
353 nc = min(lenId, strlenW(lpID));
354 nc = min(nc, MAX_LINKID_TEXT - 1);
355 Last->u.Link.szID = Alloc((nc + 1) * sizeof(WCHAR));
356 if(Last->u.Link.szID != NULL)
358 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
362 Last->u.Link.szID = NULL;
365 nc = min(lenUrl, strlenW(lpUrl));
366 nc = min(nc, L_MAX_URL_LENGTH - 1);
367 Last->u.Link.szUrl = Alloc((nc + 1) * sizeof(WCHAR));
368 if(Last->u.Link.szUrl != NULL)
370 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
374 Last->u.Link.szUrl = NULL;
378 CurrentType = slText;
385 /* we don't know what tag it is, so just continue */
388 if(CurrentType == slText && textstart == NULL)
402 /* save the pointer of the current text item if we couldn't find a tag */
403 if(textstart == NULL && CurrentType == slText)
412 if(textstart != NULL && textlen > 0)
414 Last = SYSLINK_AppendDocItem(infoPtr, textstart, textlen, CurrentType, Last);
417 ERR("Unable to create new document item!\n");
420 if(CurrentType == slLink)
424 if(!(infoPtr->Style & WS_DISABLED))
426 Last->u.Link.state |= LIS_ENABLED;
428 /* Copy the tag parameters */
431 nc = min(lenId, strlenW(lpID));
432 nc = min(nc, MAX_LINKID_TEXT - 1);
433 Last->u.Link.szID = Alloc((nc + 1) * sizeof(WCHAR));
434 if(Last->u.Link.szID != NULL)
436 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
440 Last->u.Link.szID = NULL;
443 nc = min(lenUrl, strlenW(lpUrl));
444 nc = min(nc, L_MAX_URL_LENGTH - 1);
445 Last->u.Link.szUrl = Alloc((nc + 1) * sizeof(WCHAR));
446 if(Last->u.Link.szUrl != NULL)
448 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
452 Last->u.Link.szUrl = NULL;
457 if(linktext != NULL && linklen > 0)
459 /* we got an unclosed link, just display the text */
460 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slText, Last);
463 ERR("Unable to create new document item!\n");
472 /***********************************************************************
473 * SYSLINK_RepaintLink
476 static VOID SYSLINK_RepaintLink (const SYSLINK_INFO *infoPtr, const DOC_ITEM *DocItem)
481 if(DocItem->Type != slLink)
483 ERR("DocItem not a link!\n");
487 bl = DocItem->Blocks;
494 InvalidateRect(infoPtr->Self, &bl->rc, TRUE);
495 n -= bl->nChars + bl->nSkip;
501 /***********************************************************************
502 * SYSLINK_GetLinkItemByIndex
503 * Retrieves a document link by its index
505 static PDOC_ITEM SYSLINK_GetLinkItemByIndex (const SYSLINK_INFO *infoPtr, int iLink)
507 PDOC_ITEM Current = infoPtr->Items;
509 while(Current != NULL)
511 if((Current->Type == slLink) && (iLink-- <= 0))
515 Current = Current->Next;
520 /***********************************************************************
521 * SYSLINK_GetFocusLink
522 * Retrieves the link that has the LIS_FOCUSED bit
524 static PDOC_ITEM SYSLINK_GetFocusLink (const SYSLINK_INFO *infoPtr, int *LinkId)
526 PDOC_ITEM Current = infoPtr->Items;
529 while(Current != NULL)
531 if(Current->Type == slLink)
533 if(Current->u.Link.state & LIS_FOCUSED)
541 Current = Current->Next;
546 /***********************************************************************
547 * SYSLINK_GetNextLink
550 static PDOC_ITEM SYSLINK_GetNextLink (const SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
552 for(Current = (Current != NULL ? Current->Next : infoPtr->Items);
554 Current = Current->Next)
556 if(Current->Type == slLink)
564 /***********************************************************************
565 * SYSLINK_GetPrevLink
566 * Gets the previous link
568 static PDOC_ITEM SYSLINK_GetPrevLink (const SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
572 /* returns the last link */
573 PDOC_ITEM Last = NULL;
575 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
577 if(Current->Type == slLink)
586 /* returns the previous link */
587 PDOC_ITEM Cur, Prev = NULL;
589 for(Cur = infoPtr->Items; Cur != NULL; Cur = Cur->Next)
595 if(Cur->Type == slLink)
604 /***********************************************************************
606 * Tries to wrap a line.
608 static BOOL SYSLINK_WrapLine (LPWSTR Text, WCHAR BreakChar, int x, int *LineLen,
609 int nFit, LPSIZE Extent)
613 for (i = 0; i < nFit; i++) if (Text[i] == '\n') break;
615 if (i == *LineLen) return FALSE;
617 /* check if we're in the middle of a word */
618 if (Text[i] != '\n' && Text[i] != BreakChar)
620 /* search for the beginning of the word */
621 while (i && Text[i - 1] != BreakChar) i--;
627 if (x == SL_LEFTMARGIN) i = max( nFit, 1 );
634 /***********************************************************************
636 * Renders the document in memory
638 static VOID SYSLINK_Render (const SYSLINK_INFO *infoPtr, HDC hdc, PRECT pRect)
643 int x, y, LineHeight;
647 szDoc.cx = szDoc.cy = 0;
650 rc.right -= SL_RIGHTMARGIN;
651 rc.bottom -= SL_BOTTOMMARGIN;
653 if(rc.right - SL_LEFTMARGIN < 0)
655 if (rc.bottom - SL_TOPMARGIN < 0)
658 hOldFont = SelectObject(hdc, infoPtr->Font);
662 GetTextMetricsW( hdc, &tm );
663 LineHeight = tm.tmHeight + tm.tmExternalLeading;
665 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
669 PDOC_TEXTBLOCK bl, cbl;
674 if(Current->nText == 0)
682 Free(Current->Blocks);
683 Current->Blocks = NULL;
687 if(Current->Type == slText)
689 SelectObject(hdc, infoPtr->Font);
691 else if(Current->Type == slLink)
693 SelectObject(hdc, infoPtr->LinkFont);
698 /* skip break characters unless they're the first of the doc item */
699 if(tx != Current->Text || x == SL_LEFTMARGIN)
701 if (n && *tx == '\n')
707 while(n > 0 && (*tx) == infoPtr->BreakChar)
715 if((n == 0 && SkipChars != 0) ||
716 GetTextExtentExPointW(hdc, tx, n, rc.right - x, &nFit, NULL, &szDim))
724 Wrap = SYSLINK_WrapLine(tx, infoPtr->BreakChar, x, &LineLen, nFit, &szDim);
728 /* move one line down, the word didn't fit into the line */
736 if(!GetTextExtentExPointW(hdc, tx, LineLen, rc.right - x, NULL, NULL, &szDim))
749 nbl = ReAlloc(bl, (nBlocks + 1) * sizeof(DOC_TEXTBLOCK));
755 cbl = bl + nBlocks - 1;
757 cbl->nChars = LineLen;
758 cbl->nSkip = SkipChars;
761 cbl->rc.right = x + szDim.cx;
762 cbl->rc.bottom = y + szDim.cy;
764 if (cbl->rc.right > szDoc.cx)
765 szDoc.cx = cbl->rc.right;
766 if (cbl->rc.bottom > szDoc.cy)
767 szDoc.cy = cbl->rc.bottom;
785 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
800 Current->Blocks = bl;
804 SelectObject(hdc, hOldFont);
806 pRect->right = pRect->left + szDoc.cx;
807 pRect->bottom = pRect->top + szDoc.cy;
810 /***********************************************************************
812 * Draws the SysLink control.
814 static LRESULT SYSLINK_Draw (const SYSLINK_INFO *infoPtr, HDC hdc)
819 COLORREF OldTextColor, OldBkColor;
821 UINT text_flags = ETO_CLIPPED;
822 UINT mode = GetBkMode( hdc );
824 hOldFont = SelectObject(hdc, infoPtr->Font);
825 OldTextColor = SetTextColor(hdc, infoPtr->TextColor);
826 OldBkColor = SetBkColor(hdc, comctl32_color.clrWindow);
828 GetClientRect(infoPtr->Self, &rc);
829 rc.right -= SL_RIGHTMARGIN + SL_LEFTMARGIN;
830 rc.bottom -= SL_BOTTOMMARGIN + SL_TOPMARGIN;
832 if(rc.right < 0 || rc.bottom < 0) return 0;
834 hBrush = (HBRUSH)SendMessageW(infoPtr->Notify, WM_CTLCOLORSTATIC,
835 (WPARAM)hdc, (LPARAM)infoPtr->Self);
836 if (!(infoPtr->Style & LWS_TRANSPARENT))
838 FillRect(hdc, &rc, hBrush);
839 if (GetBkMode( hdc ) == OPAQUE) text_flags |= ETO_OPAQUE;
841 else SetBkMode( hdc, TRANSPARENT );
843 DeleteObject(hBrush);
845 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
851 bl = Current->Blocks;
857 if(Current->Type == slText)
859 SelectObject(hdc, infoPtr->Font);
860 SetTextColor(hdc, infoPtr->TextColor);
864 SelectObject(hdc, infoPtr->LinkFont);
865 SetTextColor(hdc, (!(Current->u.Link.state & LIS_VISITED) ? infoPtr->LinkColor : infoPtr->VisitedColor));
871 ExtTextOutW(hdc, bl->rc.left, bl->rc.top, text_flags, &bl->rc, tx, bl->nChars, NULL);
872 if((Current->Type == slLink) && (Current->u.Link.state & LIS_FOCUSED) && infoPtr->HasFocus)
874 COLORREF PrevTextColor;
875 PrevTextColor = SetTextColor(hdc, infoPtr->TextColor);
876 DrawFocusRect(hdc, &bl->rc);
877 SetTextColor(hdc, PrevTextColor);
880 n -= bl->nChars + bl->nSkip;
886 SetBkColor(hdc, OldBkColor);
887 SetTextColor(hdc, OldTextColor);
888 SelectObject(hdc, hOldFont);
889 SetBkMode(hdc, mode);
894 /***********************************************************************
896 * Handles the WM_PAINT message.
898 static LRESULT SYSLINK_Paint (const SYSLINK_INFO *infoPtr, HDC hdcParam)
903 hdc = hdcParam ? hdcParam : BeginPaint (infoPtr->Self, &ps);
906 SYSLINK_Draw (infoPtr, hdc);
907 if (!hdcParam) EndPaint (infoPtr->Self, &ps);
912 /***********************************************************************
914 * Set new Font for the SysLink control.
916 static HFONT SYSLINK_SetFont (SYSLINK_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
922 HFONT hOldFont = infoPtr->Font;
923 infoPtr->Font = hFont;
925 /* free the underline font */
926 if(infoPtr->LinkFont != NULL)
928 DeleteObject(infoPtr->LinkFont);
929 infoPtr->LinkFont = NULL;
932 /* Render text position and word wrapping in memory */
933 if (GetClientRect(infoPtr->Self, &rcClient))
935 hdc = GetDC(infoPtr->Self);
938 /* create a new underline font */
939 if(GetTextMetricsW(hdc, &tm) &&
940 GetObjectW(infoPtr->Font, sizeof(LOGFONTW), &lf))
942 lf.lfUnderline = TRUE;
943 infoPtr->LinkFont = CreateFontIndirectW(&lf);
944 infoPtr->BreakChar = tm.tmBreakChar;
948 ERR("Failed to create link font!\n");
951 SYSLINK_Render(infoPtr, hdc, &rcClient);
952 ReleaseDC(infoPtr->Self, hdc);
958 RedrawWindow(infoPtr->Self, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
964 /***********************************************************************
966 * Set new text for the SysLink control.
968 static LRESULT SYSLINK_SetText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
970 /* clear the document */
971 SYSLINK_ClearDoc(infoPtr);
973 if(Text == NULL || *Text == 0)
978 /* let's parse the string and create a document */
979 if(SYSLINK_ParseText(infoPtr, Text) > 0)
983 /* Render text position and word wrapping in memory */
984 if (GetClientRect(infoPtr->Self, &rcClient))
986 HDC hdc = GetDC(infoPtr->Self);
989 SYSLINK_Render(infoPtr, hdc, &rcClient);
990 ReleaseDC(infoPtr->Self, hdc);
992 InvalidateRect(infoPtr->Self, NULL, TRUE);
1000 /***********************************************************************
1001 * SYSLINK_SetFocusLink
1002 * Updates the focus status bits and focusses the specified link.
1003 * If no document item is specified, the focus bit will be removed from all links.
1004 * Returns the previous focused item.
1006 static PDOC_ITEM SYSLINK_SetFocusLink (const SYSLINK_INFO *infoPtr, const DOC_ITEM *DocItem)
1008 PDOC_ITEM Current, PrevFocus = NULL;
1010 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1012 if(Current->Type == slLink)
1014 if((PrevFocus == NULL) && (Current->u.Link.state & LIS_FOCUSED))
1016 PrevFocus = Current;
1019 if(Current == DocItem)
1021 Current->u.Link.state |= LIS_FOCUSED;
1025 Current->u.Link.state &= ~LIS_FOCUSED;
1033 /***********************************************************************
1035 * Sets the states and attributes of a link item.
1037 static LRESULT SYSLINK_SetItem (const SYSLINK_INFO *infoPtr, const LITEM *Item)
1043 BOOL Repaint = FALSE;
1045 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1047 ERR("Invalid Flags!\n");
1051 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1054 ERR("Link %d couldn't be found\n", Item->iLink);
1058 if(Item->mask & LIF_ITEMID)
1060 nc = min(lstrlenW(Item->szID), MAX_LINKID_TEXT - 1);
1061 szId = Alloc((nc + 1) * sizeof(WCHAR));
1064 lstrcpynW(szId, Item->szID, nc + 1);
1068 ERR("Unable to allocate memory for link id\n");
1073 if(Item->mask & LIF_URL)
1075 nc = min(lstrlenW(Item->szUrl), L_MAX_URL_LENGTH - 1);
1076 szUrl = Alloc((nc + 1) * sizeof(WCHAR));
1079 lstrcpynW(szUrl, Item->szUrl, nc + 1);
1085 ERR("Unable to allocate memory for link url\n");
1090 if(Item->mask & LIF_ITEMID)
1092 Free(di->u.Link.szID);
1093 di->u.Link.szID = szId;
1096 if(Item->mask & LIF_URL)
1098 Free(di->u.Link.szUrl);
1099 di->u.Link.szUrl = szUrl;
1102 if(Item->mask & LIF_STATE)
1104 UINT oldstate = di->u.Link.state;
1105 /* clear the masked bits */
1106 di->u.Link.state &= ~(Item->stateMask & LIS_MASK);
1108 di->u.Link.state |= (Item->state & Item->stateMask) & LIS_MASK;
1109 Repaint = (oldstate != di->u.Link.state);
1111 /* update the focus */
1112 SYSLINK_SetFocusLink(infoPtr, ((di->u.Link.state & LIS_FOCUSED) ? di : NULL));
1117 SYSLINK_RepaintLink(infoPtr, di);
1123 /***********************************************************************
1125 * Retrieves the states and attributes of a link item.
1127 static LRESULT SYSLINK_GetItem (const SYSLINK_INFO *infoPtr, PLITEM Item)
1131 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1133 ERR("Invalid Flags!\n");
1137 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1140 ERR("Link %d couldn't be found\n", Item->iLink);
1144 if(Item->mask & LIF_STATE)
1146 Item->state = (di->u.Link.state & Item->stateMask);
1147 if(!infoPtr->HasFocus)
1149 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1150 Item->state &= ~LIS_FOCUSED;
1154 if(Item->mask & LIF_ITEMID)
1158 lstrcpyW(Item->szID, di->u.Link.szID);
1166 if(Item->mask & LIF_URL)
1168 if(di->u.Link.szUrl)
1170 lstrcpyW(Item->szUrl, di->u.Link.szUrl);
1181 /***********************************************************************
1182 * SYSLINK_PtInDocItem
1183 * Determines if a point is in the region of a document item
1185 static BOOL SYSLINK_PtInDocItem (const DOC_ITEM *DocItem, POINT pt)
1190 bl = DocItem->Blocks;
1197 if (PtInRect(&bl->rc, pt))
1201 n -= bl->nChars + bl->nSkip;
1209 /***********************************************************************
1211 * Determines the link the user clicked on.
1213 static LRESULT SYSLINK_HitTest (const SYSLINK_INFO *infoPtr, PLHITTESTINFO HitTest)
1218 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1220 if(Current->Type == slLink)
1222 if(SYSLINK_PtInDocItem(Current, HitTest->pt))
1224 HitTest->item.mask = 0;
1225 HitTest->item.iLink = id;
1226 HitTest->item.state = 0;
1227 HitTest->item.stateMask = 0;
1228 if(Current->u.Link.szID)
1230 lstrcpyW(HitTest->item.szID, Current->u.Link.szID);
1234 HitTest->item.szID[0] = 0;
1236 if(Current->u.Link.szUrl)
1238 lstrcpyW(HitTest->item.szUrl, Current->u.Link.szUrl);
1242 HitTest->item.szUrl[0] = 0;
1253 /***********************************************************************
1254 * SYSLINK_GetIdealHeight
1255 * Returns the preferred height of a link at the current control's width.
1257 static LRESULT SYSLINK_GetIdealHeight (const SYSLINK_INFO *infoPtr)
1259 HDC hdc = GetDC(infoPtr->Self);
1264 HGDIOBJ hOldFont = SelectObject(hdc, infoPtr->Font);
1266 if(GetTextMetricsW(hdc, &tm))
1268 height = tm.tmHeight;
1274 SelectObject(hdc, hOldFont);
1275 ReleaseDC(infoPtr->Self, hdc);
1282 /***********************************************************************
1283 * SYSLINK_SendParentNotify
1284 * Sends a WM_NOTIFY message to the parent window.
1286 static LRESULT SYSLINK_SendParentNotify (const SYSLINK_INFO *infoPtr, UINT code, const DOC_ITEM *Link, int iLink)
1290 nml.hdr.hwndFrom = infoPtr->Self;
1291 nml.hdr.idFrom = GetWindowLongPtrW(infoPtr->Self, GWLP_ID);
1292 nml.hdr.code = code;
1295 nml.item.iLink = iLink;
1297 nml.item.stateMask = 0;
1298 if(Link->u.Link.szID)
1300 lstrcpyW(nml.item.szID, Link->u.Link.szID);
1304 nml.item.szID[0] = 0;
1306 if(Link->u.Link.szUrl)
1308 lstrcpyW(nml.item.szUrl, Link->u.Link.szUrl);
1312 nml.item.szUrl[0] = 0;
1315 return SendMessageW(infoPtr->Notify, WM_NOTIFY, nml.hdr.idFrom, (LPARAM)&nml);
1318 /***********************************************************************
1320 * Handles receiving the input focus.
1322 static LRESULT SYSLINK_SetFocus (SYSLINK_INFO *infoPtr)
1326 infoPtr->HasFocus = TRUE;
1328 /* We always select the first link, even if we activated the control using
1329 SHIFT+TAB. This is the default behavior */
1330 Focus = SYSLINK_GetNextLink(infoPtr, NULL);
1333 SYSLINK_SetFocusLink(infoPtr, Focus);
1334 SYSLINK_RepaintLink(infoPtr, Focus);
1339 /***********************************************************************
1341 * Handles losing the input focus.
1343 static LRESULT SYSLINK_KillFocus (SYSLINK_INFO *infoPtr)
1347 infoPtr->HasFocus = FALSE;
1348 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1352 SYSLINK_RepaintLink(infoPtr, Focus);
1358 /***********************************************************************
1360 * Returns a link at the specified position
1362 static PDOC_ITEM SYSLINK_LinkAtPt (const SYSLINK_INFO *infoPtr, const POINT *pt, int *LinkId, BOOL MustBeEnabled)
1367 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1369 if((Current->Type == slLink) && SYSLINK_PtInDocItem(Current, *pt) &&
1370 (!MustBeEnabled || (MustBeEnabled && (Current->u.Link.state & LIS_ENABLED))))
1384 /***********************************************************************
1385 * SYSLINK_LButtonDown
1386 * Handles mouse clicks
1388 static LRESULT SYSLINK_LButtonDown (SYSLINK_INFO *infoPtr, const POINT *pt)
1390 PDOC_ITEM Current, Old;
1393 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1396 SetFocus(infoPtr->Self);
1398 Old = SYSLINK_SetFocusLink(infoPtr, Current);
1399 if(Old != NULL && Old != Current)
1401 SYSLINK_RepaintLink(infoPtr, Old);
1403 infoPtr->MouseDownID = id;
1404 SYSLINK_RepaintLink(infoPtr, Current);
1410 /***********************************************************************
1412 * Handles mouse clicks
1414 static LRESULT SYSLINK_LButtonUp (SYSLINK_INFO *infoPtr, const POINT *pt)
1416 if(infoPtr->MouseDownID > -1)
1421 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1422 if((Current != NULL) && (Current->u.Link.state & LIS_FOCUSED) && (infoPtr->MouseDownID == id))
1424 SYSLINK_SendParentNotify(infoPtr, NM_CLICK, Current, id);
1428 infoPtr->MouseDownID = -1;
1433 /***********************************************************************
1435 * Handles ENTER key events
1437 static BOOL SYSLINK_OnEnter (const SYSLINK_INFO *infoPtr)
1439 if(infoPtr->HasFocus && !infoPtr->IgnoreReturn)
1444 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1447 SYSLINK_SendParentNotify(infoPtr, NM_RETURN, Focus, id);
1454 /***********************************************************************
1455 * SYSKEY_SelectNextPrevLink
1456 * Changes the currently focused link
1458 static BOOL SYSKEY_SelectNextPrevLink (const SYSLINK_INFO *infoPtr, BOOL Prev)
1460 if(infoPtr->HasFocus)
1465 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1468 PDOC_ITEM NewFocus, OldFocus;
1471 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1473 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1475 if(NewFocus != NULL)
1477 OldFocus = SYSLINK_SetFocusLink(infoPtr, NewFocus);
1479 if(OldFocus && OldFocus != NewFocus)
1481 SYSLINK_RepaintLink(infoPtr, OldFocus);
1483 SYSLINK_RepaintLink(infoPtr, NewFocus);
1491 /***********************************************************************
1492 * SYSKEY_SelectNextPrevLink
1493 * Determines if there's a next or previous link to decide whether the control
1494 * should capture the tab key message
1496 static BOOL SYSLINK_NoNextLink (const SYSLINK_INFO *infoPtr, BOOL Prev)
1498 PDOC_ITEM Focus, NewFocus;
1500 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1502 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1504 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1506 return NewFocus == NULL;
1509 /***********************************************************************
1510 * SYSLINK_GetIdealSize
1511 * Calculates the ideal size of a link control at a given maximum width.
1513 static VOID SYSLINK_GetIdealSize (const SYSLINK_INFO *infoPtr, int cxMaxWidth, LPSIZE lpSize)
1518 rc.left = rc.top = rc.bottom = 0;
1519 rc.right = cxMaxWidth;
1521 hdc = GetDC(infoPtr->Self);
1524 HGDIOBJ hOldFont = SelectObject(hdc, infoPtr->Font);
1526 SYSLINK_Render(infoPtr, hdc, &rc);
1528 SelectObject(hdc, hOldFont);
1529 ReleaseDC(infoPtr->Self, hdc);
1531 lpSize->cx = rc.right;
1532 lpSize->cy = rc.bottom;
1536 /***********************************************************************
1539 static LRESULT WINAPI SysLinkWindowProc(HWND hwnd, UINT message,
1540 WPARAM wParam, LPARAM lParam)
1542 SYSLINK_INFO *infoPtr;
1544 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd, message, wParam, lParam);
1546 infoPtr = (SYSLINK_INFO *)GetWindowLongPtrW(hwnd, 0);
1548 if (!infoPtr && message != WM_CREATE)
1549 return DefWindowProcW(hwnd, message, wParam, lParam);
1552 case WM_PRINTCLIENT:
1554 return SYSLINK_Paint (infoPtr, (HDC)wParam);
1557 if (!(infoPtr->Style & LWS_TRANSPARENT))
1559 HDC hdc = (HDC)wParam;
1560 HBRUSH brush = CreateSolidBrush( comctl32_color.clrWindow );
1563 GetClipBox( hdc, &rect );
1564 FillRect( hdc, &rect, brush );
1565 DeleteObject( brush );
1573 DWORD mp = GetMessagePos();
1575 ht.pt.x = (short)LOWORD(mp);
1576 ht.pt.y = (short)HIWORD(mp);
1578 ScreenToClient(infoPtr->Self, &ht.pt);
1579 if(SYSLINK_HitTest (infoPtr, &ht))
1581 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_HAND));
1585 return DefWindowProcW(hwnd, message, wParam, lParam);
1591 if (GetClientRect(infoPtr->Self, &rcClient))
1593 HDC hdc = GetDC(infoPtr->Self);
1596 SYSLINK_Render(infoPtr, hdc, &rcClient);
1597 ReleaseDC(infoPtr->Self, hdc);
1604 return (LRESULT)infoPtr->Font;
1607 return (LRESULT)SYSLINK_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1610 SYSLINK_SetText(infoPtr, (LPWSTR)lParam);
1611 return DefWindowProcW(hwnd, message, wParam, lParam);
1613 case WM_LBUTTONDOWN:
1616 pt.x = (short)LOWORD(lParam);
1617 pt.y = (short)HIWORD(lParam);
1618 return SYSLINK_LButtonDown(infoPtr, &pt);
1623 pt.x = (short)LOWORD(lParam);
1624 pt.y = (short)HIWORD(lParam);
1625 return SYSLINK_LButtonUp(infoPtr, &pt);
1633 SYSLINK_OnEnter(infoPtr);
1637 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1638 SYSKEY_SelectNextPrevLink(infoPtr, shift);
1642 return DefWindowProcW(hwnd, message, wParam, lParam);
1648 LRESULT Ret = DLGC_HASSETSEL;
1649 int vk = (lParam != 0 ? (int)((LPMSG)lParam)->wParam : 0);
1653 Ret |= DLGC_WANTMESSAGE;
1657 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1658 if(!SYSLINK_NoNextLink(infoPtr, shift))
1660 Ret |= DLGC_WANTTAB;
1664 Ret |= DLGC_WANTCHARS;
1676 pt.x = (short)LOWORD(lParam);
1677 pt.y = (short)HIWORD(lParam);
1679 GetClientRect(infoPtr->Self, &rc);
1680 ScreenToClient(infoPtr->Self, &pt);
1681 if(pt.x < 0 || pt.y < 0 || pt.x > rc.right || pt.y > rc.bottom)
1686 if(SYSLINK_LinkAtPt(infoPtr, &pt, NULL, FALSE))
1691 return HTTRANSPARENT;
1695 return SYSLINK_HitTest(infoPtr, (PLHITTESTINFO)lParam);
1698 return SYSLINK_SetItem(infoPtr, (PLITEM)lParam);
1701 return SYSLINK_GetItem(infoPtr, (PLITEM)lParam);
1703 case LM_GETIDEALHEIGHT:
1706 /* LM_GETIDEALSIZE */
1707 SYSLINK_GetIdealSize(infoPtr, (int)wParam, (LPSIZE)lParam);
1709 return SYSLINK_GetIdealHeight(infoPtr);
1712 return SYSLINK_SetFocus(infoPtr);
1715 return SYSLINK_KillFocus(infoPtr);
1718 infoPtr->Style &= ~WS_DISABLED;
1719 infoPtr->Style |= (wParam ? 0 : WS_DISABLED);
1720 InvalidateRect (infoPtr->Self, NULL, FALSE);
1723 case WM_STYLECHANGED:
1724 if (wParam == GWL_STYLE)
1726 infoPtr->Style = ((LPSTYLESTRUCT)lParam)->styleNew;
1728 InvalidateRect(infoPtr->Self, NULL, TRUE);
1733 /* allocate memory for info struct */
1734 infoPtr = Alloc (sizeof(SYSLINK_INFO));
1735 if (!infoPtr) return -1;
1736 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1738 /* initialize the info struct */
1739 infoPtr->Self = hwnd;
1740 infoPtr->Notify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1741 infoPtr->Style = ((LPCREATESTRUCTW)lParam)->style;
1743 infoPtr->LinkFont = 0;
1744 infoPtr->Items = NULL;
1745 infoPtr->HasFocus = FALSE;
1746 infoPtr->MouseDownID = -1;
1747 infoPtr->TextColor = comctl32_color.clrWindowText;
1748 infoPtr->LinkColor = comctl32_color.clrHighlight;
1749 infoPtr->VisitedColor = comctl32_color.clrHighlight;
1750 infoPtr->BreakChar = ' ';
1751 infoPtr->IgnoreReturn = infoPtr->Style & LWS_IGNORERETURN;
1752 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd);
1753 SYSLINK_SetText(infoPtr, ((LPCREATESTRUCTW)lParam)->lpszName);
1757 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd);
1758 SYSLINK_ClearDoc(infoPtr);
1759 if(infoPtr->Font != 0) DeleteObject(infoPtr->Font);
1760 if(infoPtr->LinkFont != 0) DeleteObject(infoPtr->LinkFont);
1761 SetWindowLongPtrW(hwnd, 0, 0);
1765 case WM_SYSCOLORCHANGE:
1766 COMCTL32_RefreshSysColors();
1770 if ((message >= WM_USER) && (message < WM_APP) && !COMCTL32_IsReflectedMessage(message))
1772 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message, wParam, lParam );
1774 return DefWindowProcW(hwnd, message, wParam, lParam);
1779 /***********************************************************************
1780 * SYSLINK_Register [Internal]
1782 * Registers the SysLink window class.
1784 VOID SYSLINK_Register (void)
1788 ZeroMemory (&wndClass, sizeof(wndClass));
1789 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1790 wndClass.lpfnWndProc = SysLinkWindowProc;
1791 wndClass.cbClsExtra = 0;
1792 wndClass.cbWndExtra = sizeof (SYSLINK_INFO *);
1793 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1794 wndClass.lpszClassName = WC_LINK;
1796 RegisterClassW (&wndClass);
1800 /***********************************************************************
1801 * SYSLINK_Unregister [Internal]
1803 * Unregisters the SysLink window class.
1805 VOID SYSLINK_Unregister (void)
1807 UnregisterClassW (WC_LINK, NULL);