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 COLORREF BackColor; /* Background color, set on creation */
98 WCHAR BreakChar; /* Break Character for the current font */
99 BOOL IgnoreReturn; /* (infoPtr->Style & LWS_IGNORERETURN) on creation */
102 static const WCHAR SL_LINKOPEN[] = { '<','a', 0 };
103 static const WCHAR SL_HREF[] = { 'h','r','e','f','=','\"',0 };
104 static const WCHAR SL_ID[] = { 'i','d','=','\"',0 };
105 static const WCHAR SL_LINKCLOSE[] = { '<','/','a','>',0 };
107 /* Control configuration constants */
109 #define SL_LEFTMARGIN (0)
110 #define SL_TOPMARGIN (0)
111 #define SL_RIGHTMARGIN (0)
112 #define SL_BOTTOMMARGIN (0)
114 /***********************************************************************
115 * SYSLINK_FreeDocItem
116 * Frees all data and gdi objects associated with a document item
118 static VOID SYSLINK_FreeDocItem (PDOC_ITEM DocItem)
120 if(DocItem->Type == slLink)
122 Free(DocItem->u.Link.szID);
123 Free(DocItem->u.Link.szUrl);
126 /* we don't free Text because it's just a pointer to a character in the
127 entire window text string */
132 /***********************************************************************
133 * SYSLINK_AppendDocItem
134 * Create and append a new document item.
136 static PDOC_ITEM SYSLINK_AppendDocItem (SYSLINK_INFO *infoPtr, LPCWSTR Text, UINT textlen,
137 SL_ITEM_TYPE type, PDOC_ITEM LastItem)
141 textlen = min(textlen, strlenW(Text));
142 Item = Alloc(FIELD_OFFSET(DOC_ITEM, Text[textlen + 1]));
145 ERR("Failed to alloc DOC_ITEM structure!\n");
150 Item->nText = textlen;
156 LastItem->Next = Item;
160 infoPtr->Items = Item;
163 lstrcpynW(Item->Text, Text, textlen + 1);
168 /***********************************************************************
170 * Clears the document tree
172 static VOID SYSLINK_ClearDoc (SYSLINK_INFO *infoPtr)
174 PDOC_ITEM Item, Next;
176 Item = infoPtr->Items;
180 SYSLINK_FreeDocItem(Item);
184 infoPtr->Items = NULL;
187 /***********************************************************************
189 * Parses the window text string and creates a document. Returns the
190 * number of document items created.
192 static UINT SYSLINK_ParseText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
194 LPCWSTR current, textstart = NULL, linktext = NULL, firsttag = NULL;
195 int taglen = 0, textlen = 0, linklen = 0, docitems = 0;
196 PDOC_ITEM Last = NULL;
197 SL_ITEM_TYPE CurrentType = slText;
201 TRACE("(%p %s)\n", infoPtr, debugstr_w(Text));
203 for(current = Text; *current != 0;)
207 if(!StrCmpNIW(current, SL_LINKOPEN, 2) && (CurrentType == slText))
209 BOOL ValidParam = FALSE, ValidLink = FALSE;
211 if(*(current + 2) == '>')
213 /* we just have to deal with a <a> tag */
222 else if(*(current + 2) == infoPtr->BreakChar)
224 /* we expect parameters, parse them */
225 LPCWSTR *CurrentParameter = NULL, tmp;
226 UINT *CurrentParameterLen = NULL;
229 tmp = current + taglen;
234 /* compare the current position with all known parameters */
235 if(!StrCmpNIW(tmp, SL_HREF, 6))
239 CurrentParameter = &lpUrl;
240 CurrentParameterLen = &lenUrl;
242 else if(!StrCmpNIW(tmp, SL_ID, 4))
246 CurrentParameter = &lpID;
247 CurrentParameterLen = &lenId;
256 /* we got a known parameter, now search until the next " character.
257 If we can't find a " character, there's a syntax error and we just assume it's text */
259 *CurrentParameter = current + taglen;
260 *CurrentParameterLen = 0;
262 for(tmp = *CurrentParameter; *tmp != 0; tmp++)
271 (*CurrentParameterLen)++;
276 /* we're done with this parameter, now there are only 2 possibilities:
277 * 1. another parameter is coming, so expect a ' ' (space) character
278 * 2. the tag is being closed, so expect a '<' character
280 if(*tmp == infoPtr->BreakChar)
282 /* we expect another parameter, do the whole thing again */
289 /* the tag is being closed, we're done */
296 if(ValidLink && ValidParam)
298 /* the <a ...> tag appears to be valid. save all information
299 so we can add the link if we find a valid </a> tag later */
300 CurrentType = slLink;
301 linktext = current + taglen;
310 if(textstart == NULL)
316 else if(!StrCmpNIW(current, SL_LINKCLOSE, 4) && (CurrentType == slLink) && firsttag)
318 /* there's a <a...> tag opened, first add the previous text, if present */
319 if(textstart != NULL && textlen > 0 && firsttag > textstart)
321 Last = SYSLINK_AppendDocItem(infoPtr, textstart, firsttag - textstart, slText, Last);
324 ERR("Unable to create new document item!\n");
332 /* now it's time to add the link to the document */
334 if(linktext != NULL && linklen > 0)
336 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slLink, Last);
339 ERR("Unable to create new document item!\n");
343 if(CurrentType == slLink)
347 if(!(infoPtr->Style & WS_DISABLED))
349 Last->u.Link.state |= LIS_ENABLED;
351 /* Copy the tag parameters */
354 nc = min(lenId, strlenW(lpID));
355 nc = min(nc, MAX_LINKID_TEXT - 1);
356 Last->u.Link.szID = Alloc((nc + 1) * sizeof(WCHAR));
357 if(Last->u.Link.szID != NULL)
359 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
363 Last->u.Link.szID = NULL;
366 nc = min(lenUrl, strlenW(lpUrl));
367 nc = min(nc, L_MAX_URL_LENGTH - 1);
368 Last->u.Link.szUrl = Alloc((nc + 1) * sizeof(WCHAR));
369 if(Last->u.Link.szUrl != NULL)
371 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
375 Last->u.Link.szUrl = NULL;
379 CurrentType = slText;
386 /* we don't know what tag it is, so just continue */
389 if(CurrentType == slText && textstart == NULL)
403 /* save the pointer of the current text item if we couldn't find a tag */
404 if(textstart == NULL && CurrentType == slText)
413 if(textstart != NULL && textlen > 0)
415 Last = SYSLINK_AppendDocItem(infoPtr, textstart, textlen, CurrentType, Last);
418 ERR("Unable to create new document item!\n");
421 if(CurrentType == slLink)
425 if(!(infoPtr->Style & WS_DISABLED))
427 Last->u.Link.state |= LIS_ENABLED;
429 /* Copy the tag parameters */
432 nc = min(lenId, strlenW(lpID));
433 nc = min(nc, MAX_LINKID_TEXT - 1);
434 Last->u.Link.szID = Alloc((nc + 1) * sizeof(WCHAR));
435 if(Last->u.Link.szID != NULL)
437 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
441 Last->u.Link.szID = NULL;
444 nc = min(lenUrl, strlenW(lpUrl));
445 nc = min(nc, L_MAX_URL_LENGTH - 1);
446 Last->u.Link.szUrl = Alloc((nc + 1) * sizeof(WCHAR));
447 if(Last->u.Link.szUrl != NULL)
449 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
453 Last->u.Link.szUrl = NULL;
458 if(linktext != NULL && linklen > 0)
460 /* we got an unclosed link, just display the text */
461 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slText, Last);
464 ERR("Unable to create new document item!\n");
473 /***********************************************************************
474 * SYSLINK_RepaintLink
477 static VOID SYSLINK_RepaintLink (const SYSLINK_INFO *infoPtr, const DOC_ITEM *DocItem)
482 if(DocItem->Type != slLink)
484 ERR("DocItem not a link!\n");
488 bl = DocItem->Blocks;
495 InvalidateRect(infoPtr->Self, &bl->rc, TRUE);
496 n -= bl->nChars + bl->nSkip;
502 /***********************************************************************
503 * SYSLINK_GetLinkItemByIndex
504 * Retrieves a document link by its index
506 static PDOC_ITEM SYSLINK_GetLinkItemByIndex (const SYSLINK_INFO *infoPtr, int iLink)
508 PDOC_ITEM Current = infoPtr->Items;
510 while(Current != NULL)
512 if((Current->Type == slLink) && (iLink-- <= 0))
516 Current = Current->Next;
521 /***********************************************************************
522 * SYSLINK_GetFocusLink
523 * Retrieves the link that has the LIS_FOCUSED bit
525 static PDOC_ITEM SYSLINK_GetFocusLink (const SYSLINK_INFO *infoPtr, int *LinkId)
527 PDOC_ITEM Current = infoPtr->Items;
530 while(Current != NULL)
532 if(Current->Type == slLink)
534 if(Current->u.Link.state & LIS_FOCUSED)
542 Current = Current->Next;
547 /***********************************************************************
548 * SYSLINK_GetNextLink
551 static PDOC_ITEM SYSLINK_GetNextLink (const SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
553 for(Current = (Current != NULL ? Current->Next : infoPtr->Items);
555 Current = Current->Next)
557 if(Current->Type == slLink)
565 /***********************************************************************
566 * SYSLINK_GetPrevLink
567 * Gets the previous link
569 static PDOC_ITEM SYSLINK_GetPrevLink (const SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
573 /* returns the last link */
574 PDOC_ITEM Last = NULL;
576 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
578 if(Current->Type == slLink)
587 /* returns the previous link */
588 PDOC_ITEM Cur, Prev = NULL;
590 for(Cur = infoPtr->Items; Cur != NULL; Cur = Cur->Next)
596 if(Cur->Type == slLink)
605 /***********************************************************************
607 * Tries to wrap a line.
609 static BOOL SYSLINK_WrapLine (LPWSTR Text, WCHAR BreakChar, int *LineLen,
610 int nFit, LPSIZE Extent)
621 Current = Text + nFit;
623 /* check if we're in the middle of a word */
624 if((*Current) != BreakChar)
626 /* search for the beginning of the word */
627 while(Current > Text && (*(Current - 1)) != BreakChar)
644 /***********************************************************************
646 * Renders the document in memory
648 static VOID SYSLINK_Render (const SYSLINK_INFO *infoPtr, HDC hdc, PRECT pRect)
653 int x, y, LineHeight;
656 szDoc.cx = szDoc.cy = 0;
659 rc.right -= SL_RIGHTMARGIN;
660 rc.bottom -= SL_BOTTOMMARGIN;
662 if(rc.right - SL_LEFTMARGIN < 0)
664 if (rc.bottom - SL_TOPMARGIN < 0)
667 hOldFont = SelectObject(hdc, infoPtr->Font);
673 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
677 PDOC_TEXTBLOCK bl, cbl;
681 if(Current->nText == 0)
689 Free(Current->Blocks);
690 Current->Blocks = NULL;
694 if(Current->Type == slText)
696 SelectObject(hdc, infoPtr->Font);
698 else if(Current->Type == slLink)
700 SelectObject(hdc, infoPtr->LinkFont);
707 /* skip break characters unless they're the first of the doc item */
708 if(tx != Current->Text || x == SL_LEFTMARGIN)
710 while(n > 0 && (*tx) == infoPtr->BreakChar)
718 if((n == 0 && SkipChars != 0) ||
719 GetTextExtentExPointW(hdc, tx, n, rc.right - x, &nFit, NULL, &szDim))
727 Wrap = SYSLINK_WrapLine(tx, infoPtr->BreakChar, &LineLen, nFit, &szDim);
731 if(x > SL_LEFTMARGIN)
733 /* move one line down, the word didn't fit into the line */
741 /* the word starts at the beginning of the line and doesn't
742 fit into the line, so break it at the last character that fits */
743 LineLen = max(nFit, 1);
749 if(!GetTextExtentExPointW(hdc, tx, LineLen, rc.right - x, NULL, NULL, &szDim))
762 nbl = ReAlloc(bl, (nBlocks + 1) * sizeof(DOC_TEXTBLOCK));
768 cbl = bl + nBlocks - 1;
770 cbl->nChars = LineLen;
771 cbl->nSkip = SkipChars;
774 cbl->rc.right = x + szDim.cx;
775 cbl->rc.bottom = y + szDim.cy;
777 if (cbl->rc.right > szDoc.cx)
778 szDoc.cx = cbl->rc.right;
779 if (cbl->rc.bottom > szDoc.cy)
780 szDoc.cy = cbl->rc.bottom;
785 LineHeight = max(LineHeight, szDim.cy);
801 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
815 Current->Blocks = bl;
819 SelectObject(hdc, hOldFont);
821 pRect->right = pRect->left + szDoc.cx;
822 pRect->bottom = pRect->top + szDoc.cy;
825 /***********************************************************************
827 * Draws the SysLink control.
829 static LRESULT SYSLINK_Draw (const SYSLINK_INFO *infoPtr, HDC hdc)
834 COLORREF OldTextColor, OldBkColor;
836 hOldFont = SelectObject(hdc, infoPtr->Font);
837 OldTextColor = SetTextColor(hdc, infoPtr->TextColor);
838 OldBkColor = SetBkColor(hdc, infoPtr->BackColor);
840 GetClientRect(infoPtr->Self, &rc);
841 rc.right -= SL_RIGHTMARGIN + SL_LEFTMARGIN;
842 rc.bottom -= SL_BOTTOMMARGIN + SL_TOPMARGIN;
844 if(rc.right < 0 || rc.bottom < 0) return 0;
846 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
852 bl = Current->Blocks;
858 if(Current->Type == slText)
860 SelectObject(hdc, infoPtr->Font);
861 SetTextColor(hdc, infoPtr->TextColor);
865 SelectObject(hdc, infoPtr->LinkFont);
866 SetTextColor(hdc, (!(Current->u.Link.state & LIS_VISITED) ? infoPtr->LinkColor : infoPtr->VisitedColor));
872 ExtTextOutW(hdc, bl->rc.left, bl->rc.top, ETO_OPAQUE | ETO_CLIPPED, &bl->rc, tx, bl->nChars, NULL);
873 if((Current->Type == slLink) && (Current->u.Link.state & LIS_FOCUSED) && infoPtr->HasFocus)
875 COLORREF PrevTextColor;
876 PrevTextColor = SetTextColor(hdc, infoPtr->TextColor);
877 DrawFocusRect(hdc, &bl->rc);
878 SetTextColor(hdc, PrevTextColor);
881 n -= bl->nChars + bl->nSkip;
887 SetBkColor(hdc, OldBkColor);
888 SetTextColor(hdc, OldTextColor);
889 SelectObject(hdc, hOldFont);
895 /***********************************************************************
897 * Handles the WM_PAINT message.
899 static LRESULT SYSLINK_Paint (const SYSLINK_INFO *infoPtr, HDC hdcParam)
904 hdc = hdcParam ? hdcParam : BeginPaint (infoPtr->Self, &ps);
907 SYSLINK_Draw (infoPtr, hdc);
908 if (!hdcParam) EndPaint (infoPtr->Self, &ps);
913 /***********************************************************************
915 * Handles the WM_ERASEBKGND message.
917 static LRESULT SYSLINK_EraseBkgnd (const SYSLINK_INFO *infoPtr, HDC hdc)
922 GetClientRect(infoPtr->Self, &r);
923 hbr = CreateSolidBrush(infoPtr->BackColor);
924 FillRect(hdc, &r, hbr);
930 /***********************************************************************
932 * Set new Font for the SysLink control.
934 static HFONT SYSLINK_SetFont (SYSLINK_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
940 HFONT hOldFont = infoPtr->Font;
941 infoPtr->Font = hFont;
943 /* free the underline font */
944 if(infoPtr->LinkFont != NULL)
946 DeleteObject(infoPtr->LinkFont);
947 infoPtr->LinkFont = NULL;
950 /* Render text position and word wrapping in memory */
951 if (GetClientRect(infoPtr->Self, &rcClient))
953 hdc = GetDC(infoPtr->Self);
956 /* create a new underline font */
957 if(GetTextMetricsW(hdc, &tm) &&
958 GetObjectW(infoPtr->Font, sizeof(LOGFONTW), &lf))
960 lf.lfUnderline = TRUE;
961 infoPtr->LinkFont = CreateFontIndirectW(&lf);
962 infoPtr->BreakChar = tm.tmBreakChar;
966 ERR("Failed to create link font!\n");
969 SYSLINK_Render(infoPtr, hdc, &rcClient);
970 ReleaseDC(infoPtr->Self, hdc);
976 RedrawWindow(infoPtr->Self, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
982 /***********************************************************************
984 * Set new text for the SysLink control.
986 static LRESULT SYSLINK_SetText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
988 /* clear the document */
989 SYSLINK_ClearDoc(infoPtr);
991 if(Text == NULL || *Text == 0)
996 /* let's parse the string and create a document */
997 if(SYSLINK_ParseText(infoPtr, Text) > 0)
1001 /* Render text position and word wrapping in memory */
1002 if (GetClientRect(infoPtr->Self, &rcClient))
1004 HDC hdc = GetDC(infoPtr->Self);
1007 SYSLINK_Render(infoPtr, hdc, &rcClient);
1008 ReleaseDC(infoPtr->Self, hdc);
1010 InvalidateRect(infoPtr->Self, NULL, TRUE);
1018 /***********************************************************************
1019 * SYSLINK_SetFocusLink
1020 * Updates the focus status bits and focusses the specified link.
1021 * If no document item is specified, the focus bit will be removed from all links.
1022 * Returns the previous focused item.
1024 static PDOC_ITEM SYSLINK_SetFocusLink (const SYSLINK_INFO *infoPtr, const DOC_ITEM *DocItem)
1026 PDOC_ITEM Current, PrevFocus = NULL;
1028 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1030 if(Current->Type == slLink)
1032 if((PrevFocus == NULL) && (Current->u.Link.state & LIS_FOCUSED))
1034 PrevFocus = Current;
1037 if(Current == DocItem)
1039 Current->u.Link.state |= LIS_FOCUSED;
1043 Current->u.Link.state &= ~LIS_FOCUSED;
1051 /***********************************************************************
1053 * Sets the states and attributes of a link item.
1055 static LRESULT SYSLINK_SetItem (const SYSLINK_INFO *infoPtr, const LITEM *Item)
1061 BOOL Repaint = FALSE;
1063 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1065 ERR("Invalid Flags!\n");
1069 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1072 ERR("Link %d couldn't be found\n", Item->iLink);
1076 if(Item->mask & LIF_ITEMID)
1078 nc = min(lstrlenW(Item->szID), MAX_LINKID_TEXT - 1);
1079 szId = Alloc((nc + 1) * sizeof(WCHAR));
1082 lstrcpynW(szId, Item->szID, nc + 1);
1086 ERR("Unable to allocate memory for link id\n");
1091 if(Item->mask & LIF_URL)
1093 nc = min(lstrlenW(Item->szUrl), L_MAX_URL_LENGTH - 1);
1094 szUrl = Alloc((nc + 1) * sizeof(WCHAR));
1097 lstrcpynW(szUrl, Item->szUrl, nc + 1);
1103 ERR("Unable to allocate memory for link url\n");
1108 if(Item->mask & LIF_ITEMID)
1110 Free(di->u.Link.szID);
1111 di->u.Link.szID = szId;
1114 if(Item->mask & LIF_URL)
1116 Free(di->u.Link.szUrl);
1117 di->u.Link.szUrl = szUrl;
1120 if(Item->mask & LIF_STATE)
1122 UINT oldstate = di->u.Link.state;
1123 /* clear the masked bits */
1124 di->u.Link.state &= ~(Item->stateMask & LIS_MASK);
1126 di->u.Link.state |= (Item->state & Item->stateMask) & LIS_MASK;
1127 Repaint = (oldstate != di->u.Link.state);
1129 /* update the focus */
1130 SYSLINK_SetFocusLink(infoPtr, ((di->u.Link.state & LIS_FOCUSED) ? di : NULL));
1135 SYSLINK_RepaintLink(infoPtr, di);
1141 /***********************************************************************
1143 * Retrieves the states and attributes of a link item.
1145 static LRESULT SYSLINK_GetItem (const SYSLINK_INFO *infoPtr, PLITEM Item)
1149 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1151 ERR("Invalid Flags!\n");
1155 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1158 ERR("Link %d couldn't be found\n", Item->iLink);
1162 if(Item->mask & LIF_STATE)
1164 Item->state = (di->u.Link.state & Item->stateMask);
1165 if(!infoPtr->HasFocus)
1167 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1168 Item->state &= ~LIS_FOCUSED;
1172 if(Item->mask & LIF_ITEMID)
1176 lstrcpyW(Item->szID, di->u.Link.szID);
1184 if(Item->mask & LIF_URL)
1186 if(di->u.Link.szUrl)
1188 lstrcpyW(Item->szUrl, di->u.Link.szUrl);
1199 /***********************************************************************
1200 * SYSLINK_PtInDocItem
1201 * Determines if a point is in the region of a document item
1203 static BOOL SYSLINK_PtInDocItem (const DOC_ITEM *DocItem, POINT pt)
1208 bl = DocItem->Blocks;
1215 if (PtInRect(&bl->rc, pt))
1219 n -= bl->nChars + bl->nSkip;
1227 /***********************************************************************
1229 * Determines the link the user clicked on.
1231 static LRESULT SYSLINK_HitTest (const SYSLINK_INFO *infoPtr, PLHITTESTINFO HitTest)
1236 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1238 if(Current->Type == slLink)
1240 if(SYSLINK_PtInDocItem(Current, HitTest->pt))
1242 HitTest->item.mask = 0;
1243 HitTest->item.iLink = id;
1244 HitTest->item.state = 0;
1245 HitTest->item.stateMask = 0;
1246 if(Current->u.Link.szID)
1248 lstrcpyW(HitTest->item.szID, Current->u.Link.szID);
1252 HitTest->item.szID[0] = 0;
1254 if(Current->u.Link.szUrl)
1256 lstrcpyW(HitTest->item.szUrl, Current->u.Link.szUrl);
1260 HitTest->item.szUrl[0] = 0;
1271 /***********************************************************************
1272 * SYSLINK_GetIdealHeight
1273 * Returns the preferred height of a link at the current control's width.
1275 static LRESULT SYSLINK_GetIdealHeight (const SYSLINK_INFO *infoPtr)
1277 HDC hdc = GetDC(infoPtr->Self);
1282 HGDIOBJ hOldFont = SelectObject(hdc, infoPtr->Font);
1284 if(GetTextMetricsW(hdc, &tm))
1286 height = tm.tmHeight;
1292 SelectObject(hdc, hOldFont);
1293 ReleaseDC(infoPtr->Self, hdc);
1300 /***********************************************************************
1301 * SYSLINK_SendParentNotify
1302 * Sends a WM_NOTIFY message to the parent window.
1304 static LRESULT SYSLINK_SendParentNotify (const SYSLINK_INFO *infoPtr, UINT code, const DOC_ITEM *Link, int iLink)
1308 nml.hdr.hwndFrom = infoPtr->Self;
1309 nml.hdr.idFrom = GetWindowLongPtrW(infoPtr->Self, GWLP_ID);
1310 nml.hdr.code = code;
1313 nml.item.iLink = iLink;
1315 nml.item.stateMask = 0;
1316 if(Link->u.Link.szID)
1318 lstrcpyW(nml.item.szID, Link->u.Link.szID);
1322 nml.item.szID[0] = 0;
1324 if(Link->u.Link.szUrl)
1326 lstrcpyW(nml.item.szUrl, Link->u.Link.szUrl);
1330 nml.item.szUrl[0] = 0;
1333 return SendMessageW(infoPtr->Notify, WM_NOTIFY, nml.hdr.idFrom, (LPARAM)&nml);
1336 /***********************************************************************
1338 * Handles receiving the input focus.
1340 static LRESULT SYSLINK_SetFocus (SYSLINK_INFO *infoPtr)
1344 infoPtr->HasFocus = TRUE;
1346 /* We always select the first link, even if we activated the control using
1347 SHIFT+TAB. This is the default behavior */
1348 Focus = SYSLINK_GetNextLink(infoPtr, NULL);
1351 SYSLINK_SetFocusLink(infoPtr, Focus);
1352 SYSLINK_RepaintLink(infoPtr, Focus);
1357 /***********************************************************************
1359 * Handles losing the input focus.
1361 static LRESULT SYSLINK_KillFocus (SYSLINK_INFO *infoPtr)
1365 infoPtr->HasFocus = FALSE;
1366 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1370 SYSLINK_RepaintLink(infoPtr, Focus);
1376 /***********************************************************************
1378 * Returns a link at the specified position
1380 static PDOC_ITEM SYSLINK_LinkAtPt (const SYSLINK_INFO *infoPtr, const POINT *pt, int *LinkId, BOOL MustBeEnabled)
1385 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1387 if((Current->Type == slLink) && SYSLINK_PtInDocItem(Current, *pt) &&
1388 (!MustBeEnabled || (MustBeEnabled && (Current->u.Link.state & LIS_ENABLED))))
1402 /***********************************************************************
1403 * SYSLINK_LButtonDown
1404 * Handles mouse clicks
1406 static LRESULT SYSLINK_LButtonDown (SYSLINK_INFO *infoPtr, const POINT *pt)
1408 PDOC_ITEM Current, Old;
1411 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1414 SetFocus(infoPtr->Self);
1416 Old = SYSLINK_SetFocusLink(infoPtr, Current);
1417 if(Old != NULL && Old != Current)
1419 SYSLINK_RepaintLink(infoPtr, Old);
1421 infoPtr->MouseDownID = id;
1422 SYSLINK_RepaintLink(infoPtr, Current);
1428 /***********************************************************************
1430 * Handles mouse clicks
1432 static LRESULT SYSLINK_LButtonUp (SYSLINK_INFO *infoPtr, const POINT *pt)
1434 if(infoPtr->MouseDownID > -1)
1439 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1440 if((Current != NULL) && (Current->u.Link.state & LIS_FOCUSED) && (infoPtr->MouseDownID == id))
1442 SYSLINK_SendParentNotify(infoPtr, NM_CLICK, Current, id);
1446 infoPtr->MouseDownID = -1;
1451 /***********************************************************************
1453 * Handles ENTER key events
1455 static BOOL SYSLINK_OnEnter (const SYSLINK_INFO *infoPtr)
1457 if(infoPtr->HasFocus && !infoPtr->IgnoreReturn)
1462 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1465 SYSLINK_SendParentNotify(infoPtr, NM_RETURN, Focus, id);
1472 /***********************************************************************
1473 * SYSKEY_SelectNextPrevLink
1474 * Changes the currently focused link
1476 static BOOL SYSKEY_SelectNextPrevLink (const SYSLINK_INFO *infoPtr, BOOL Prev)
1478 if(infoPtr->HasFocus)
1483 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1486 PDOC_ITEM NewFocus, OldFocus;
1489 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1491 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1493 if(NewFocus != NULL)
1495 OldFocus = SYSLINK_SetFocusLink(infoPtr, NewFocus);
1497 if(OldFocus && OldFocus != NewFocus)
1499 SYSLINK_RepaintLink(infoPtr, OldFocus);
1501 SYSLINK_RepaintLink(infoPtr, NewFocus);
1509 /***********************************************************************
1510 * SYSKEY_SelectNextPrevLink
1511 * Determines if there's a next or previous link to decide whether the control
1512 * should capture the tab key message
1514 static BOOL SYSLINK_NoNextLink (const SYSLINK_INFO *infoPtr, BOOL Prev)
1516 PDOC_ITEM Focus, NewFocus;
1518 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1520 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1522 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1524 return NewFocus == NULL;
1527 /***********************************************************************
1528 * SYSLINK_GetIdealSize
1529 * Calculates the ideal size of a link control at a given maximum width.
1531 static VOID SYSLINK_GetIdealSize (const SYSLINK_INFO *infoPtr, int cxMaxWidth, LPSIZE lpSize)
1536 rc.left = rc.top = rc.bottom = 0;
1537 rc.right = cxMaxWidth;
1539 hdc = GetDC(infoPtr->Self);
1542 HGDIOBJ hOldFont = SelectObject(hdc, infoPtr->Font);
1544 SYSLINK_Render(infoPtr, hdc, &rc);
1546 SelectObject(hdc, hOldFont);
1547 ReleaseDC(infoPtr->Self, hdc);
1549 lpSize->cx = rc.right;
1550 lpSize->cy = rc.bottom;
1554 /***********************************************************************
1557 static LRESULT WINAPI SysLinkWindowProc(HWND hwnd, UINT message,
1558 WPARAM wParam, LPARAM lParam)
1560 SYSLINK_INFO *infoPtr;
1562 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd, message, wParam, lParam);
1564 infoPtr = (SYSLINK_INFO *)GetWindowLongPtrW(hwnd, 0);
1566 if (!infoPtr && message != WM_CREATE)
1567 return DefWindowProcW(hwnd, message, wParam, lParam);
1570 case WM_PRINTCLIENT:
1572 return SYSLINK_Paint (infoPtr, (HDC)wParam);
1575 return SYSLINK_EraseBkgnd(infoPtr, (HDC)wParam);
1580 DWORD mp = GetMessagePos();
1582 ht.pt.x = (short)LOWORD(mp);
1583 ht.pt.y = (short)HIWORD(mp);
1585 ScreenToClient(infoPtr->Self, &ht.pt);
1586 if(SYSLINK_HitTest (infoPtr, &ht))
1588 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_HAND));
1592 return DefWindowProcW(hwnd, message, wParam, lParam);
1598 if (GetClientRect(infoPtr->Self, &rcClient))
1600 HDC hdc = GetDC(infoPtr->Self);
1603 SYSLINK_Render(infoPtr, hdc, &rcClient);
1604 ReleaseDC(infoPtr->Self, hdc);
1611 return (LRESULT)infoPtr->Font;
1614 return (LRESULT)SYSLINK_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1617 SYSLINK_SetText(infoPtr, (LPWSTR)lParam);
1618 return DefWindowProcW(hwnd, message, wParam, lParam);
1620 case WM_LBUTTONDOWN:
1623 pt.x = (short)LOWORD(lParam);
1624 pt.y = (short)HIWORD(lParam);
1625 return SYSLINK_LButtonDown(infoPtr, &pt);
1630 pt.x = (short)LOWORD(lParam);
1631 pt.y = (short)HIWORD(lParam);
1632 return SYSLINK_LButtonUp(infoPtr, &pt);
1640 SYSLINK_OnEnter(infoPtr);
1644 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1645 SYSKEY_SelectNextPrevLink(infoPtr, shift);
1649 return DefWindowProcW(hwnd, message, wParam, lParam);
1655 LRESULT Ret = DLGC_HASSETSEL;
1656 int vk = (lParam != 0 ? (int)((LPMSG)lParam)->wParam : 0);
1660 Ret |= DLGC_WANTMESSAGE;
1664 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1665 if(!SYSLINK_NoNextLink(infoPtr, shift))
1667 Ret |= DLGC_WANTTAB;
1671 Ret |= DLGC_WANTCHARS;
1683 pt.x = (short)LOWORD(lParam);
1684 pt.y = (short)HIWORD(lParam);
1686 GetClientRect(infoPtr->Self, &rc);
1687 ScreenToClient(infoPtr->Self, &pt);
1688 if(pt.x < 0 || pt.y < 0 || pt.x > rc.right || pt.y > rc.bottom)
1693 if(SYSLINK_LinkAtPt(infoPtr, &pt, NULL, FALSE))
1698 return HTTRANSPARENT;
1702 return SYSLINK_HitTest(infoPtr, (PLHITTESTINFO)lParam);
1705 return SYSLINK_SetItem(infoPtr, (PLITEM)lParam);
1708 return SYSLINK_GetItem(infoPtr, (PLITEM)lParam);
1710 case LM_GETIDEALHEIGHT:
1713 /* LM_GETIDEALSIZE */
1714 SYSLINK_GetIdealSize(infoPtr, (int)wParam, (LPSIZE)lParam);
1716 return SYSLINK_GetIdealHeight(infoPtr);
1719 return SYSLINK_SetFocus(infoPtr);
1722 return SYSLINK_KillFocus(infoPtr);
1725 infoPtr->Style &= ~WS_DISABLED;
1726 infoPtr->Style |= (wParam ? 0 : WS_DISABLED);
1727 InvalidateRect (infoPtr->Self, NULL, FALSE);
1730 case WM_STYLECHANGED:
1731 if (wParam == GWL_STYLE)
1733 infoPtr->Style = ((LPSTYLESTRUCT)lParam)->styleNew;
1735 InvalidateRect(infoPtr->Self, NULL, TRUE);
1740 /* allocate memory for info struct */
1741 infoPtr = Alloc (sizeof(SYSLINK_INFO));
1742 if (!infoPtr) return -1;
1743 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1745 /* initialize the info struct */
1746 infoPtr->Self = hwnd;
1747 infoPtr->Notify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1748 infoPtr->Style = ((LPCREATESTRUCTW)lParam)->style;
1750 infoPtr->LinkFont = 0;
1751 infoPtr->Items = NULL;
1752 infoPtr->HasFocus = FALSE;
1753 infoPtr->MouseDownID = -1;
1754 infoPtr->TextColor = comctl32_color.clrWindowText;
1755 infoPtr->LinkColor = comctl32_color.clrHotTrackingColor;
1756 infoPtr->VisitedColor = comctl32_color.clrHighlight;
1757 infoPtr->BackColor = infoPtr->Style & LWS_TRANSPARENT ?
1758 comctl32_color.clrWindow : comctl32_color.clrBtnFace;
1759 infoPtr->BreakChar = ' ';
1760 infoPtr->IgnoreReturn = infoPtr->Style & LWS_IGNORERETURN;
1761 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd);
1762 SYSLINK_SetText(infoPtr, ((LPCREATESTRUCTW)lParam)->lpszName);
1766 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd);
1767 SYSLINK_ClearDoc(infoPtr);
1768 if(infoPtr->Font != 0) DeleteObject(infoPtr->Font);
1769 if(infoPtr->LinkFont != 0) DeleteObject(infoPtr->LinkFont);
1770 SetWindowLongPtrW(hwnd, 0, 0);
1774 case WM_SYSCOLORCHANGE:
1775 COMCTL32_RefreshSysColors();
1776 if (infoPtr->Style & LWS_TRANSPARENT)
1777 infoPtr->BackColor = comctl32_color.clrWindow;
1781 if ((message >= WM_USER) && (message < WM_APP) && !COMCTL32_IsReflectedMessage(message))
1783 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message, wParam, lParam );
1785 return DefWindowProcW(hwnd, message, wParam, lParam);
1790 /***********************************************************************
1791 * SYSLINK_Register [Internal]
1793 * Registers the SysLink window class.
1795 VOID SYSLINK_Register (void)
1799 ZeroMemory (&wndClass, sizeof(wndClass));
1800 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1801 wndClass.lpfnWndProc = SysLinkWindowProc;
1802 wndClass.cbClsExtra = 0;
1803 wndClass.cbWndExtra = sizeof (SYSLINK_INFO *);
1804 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1805 wndClass.lpszClassName = WC_LINK;
1807 RegisterClassW (&wndClass);
1811 /***********************************************************************
1812 * SYSLINK_Unregister [Internal]
1814 * Unregisters the SysLink window class.
1816 VOID SYSLINK_Unregister (void)
1818 UnregisterClassW (WC_LINK, NULL);