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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 if (DocItem->u.Link.szID != NULL)
122 Free(DocItem->u.Link.szID);
124 if (DocItem->u.Link.szUrl != NULL)
126 Free(DocItem->u.Link.szUrl);
130 /* we don't free Text because it's just a pointer to a character in the
131 entire window text string */
136 /***********************************************************************
137 * SYSLINK_AppendDocItem
138 * Create and append a new document item.
140 static PDOC_ITEM SYSLINK_AppendDocItem (SYSLINK_INFO *infoPtr, LPCWSTR Text, UINT textlen,
141 SL_ITEM_TYPE type, PDOC_ITEM LastItem)
145 textlen = min(textlen, lstrlenW(Text));
146 Item = Alloc(FIELD_OFFSET(DOC_ITEM, Text[textlen + 1]));
149 ERR("Failed to alloc DOC_ITEM structure!\n");
154 Item->nText = textlen;
160 LastItem->Next = Item;
164 infoPtr->Items = Item;
167 lstrcpynW(Item->Text, Text, textlen + 1);
172 /***********************************************************************
174 * Clears the document tree
176 static VOID SYSLINK_ClearDoc (SYSLINK_INFO *infoPtr)
178 PDOC_ITEM Item, Next;
180 Item = infoPtr->Items;
184 SYSLINK_FreeDocItem(Item);
188 infoPtr->Items = NULL;
191 /***********************************************************************
193 * Parses the window text string and creates a document. Returns the
194 * number of document items created.
196 static UINT SYSLINK_ParseText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
198 LPCWSTR current, textstart = NULL, linktext = NULL, firsttag = NULL;
199 int taglen = 0, textlen = 0, linklen = 0, docitems = 0;
200 PDOC_ITEM Last = NULL;
201 SL_ITEM_TYPE CurrentType = slText;
205 for(current = Text; *current != 0;)
209 if(!StrCmpNIW(current, SL_LINKOPEN, 2) && (CurrentType == slText))
211 BOOL ValidParam = FALSE, ValidLink = FALSE;
213 if(*(current + 2) == '>')
215 /* we just have to deal with a <a> tag */
224 else if(*(current + 2) == infoPtr->BreakChar)
226 /* we expect parameters, parse them */
227 LPCWSTR *CurrentParameter = NULL, tmp;
228 UINT *CurrentParameterLen = NULL;
231 tmp = current + taglen;
236 /* compare the current position with all known parameters */
237 if(!StrCmpNIW(tmp, SL_HREF, 6))
241 CurrentParameter = &lpUrl;
242 CurrentParameterLen = &lenUrl;
244 else if(!StrCmpNIW(tmp, SL_ID, 4))
248 CurrentParameter = &lpID;
249 CurrentParameterLen = &lenId;
258 /* we got a known parameter, now search until the next " character.
259 If we can't find a " character, there's a syntax error and we just assume it's text */
261 *CurrentParameter = current + taglen;
262 *CurrentParameterLen = 0;
264 for(tmp = *CurrentParameter; *tmp != 0; tmp++)
273 (*CurrentParameterLen)++;
278 /* we're done with this parameter, now there are only 2 possibilities:
279 * 1. another parameter is coming, so expect a ' ' (space) character
280 * 2. the tag is being closed, so expect a '<' character
282 if(*tmp == infoPtr->BreakChar)
284 /* we expect another parameter, do the whole thing again */
291 /* the tag is being closed, we're done */
300 if(ValidLink && ValidParam)
302 /* the <a ...> tag appears to be valid. save all information
303 so we can add the link if we find a valid </a> tag later */
304 CurrentType = slLink;
305 linktext = current + taglen;
314 if(textstart == NULL)
320 else if(!StrCmpNIW(current, SL_LINKCLOSE, 4) && (CurrentType == slLink) && firsttag)
322 /* there's a <a...> tag opened, first add the previous text, if present */
323 if(textstart != NULL && textlen > 0 && firsttag > textstart)
325 Last = SYSLINK_AppendDocItem(infoPtr, textstart, firsttag - textstart, slText, Last);
328 ERR("Unable to create new document item!\n");
336 /* now it's time to add the link to the document */
338 if(linktext != NULL && linklen > 0)
340 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slLink, Last);
343 ERR("Unable to create new document item!\n");
347 if(CurrentType == slLink)
351 if(!(infoPtr->Style & WS_DISABLED))
353 Last->u.Link.state |= LIS_ENABLED;
355 /* Copy the tag parameters */
358 nc = min(lenId, strlenW(lpID));
359 nc = min(nc, MAX_LINKID_TEXT - 1);
360 Last->u.Link.szID = Alloc((nc + 1) * sizeof(WCHAR));
361 if(Last->u.Link.szID != NULL)
363 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
367 Last->u.Link.szID = NULL;
370 nc = min(lenUrl, strlenW(lpUrl));
371 nc = min(nc, L_MAX_URL_LENGTH - 1);
372 Last->u.Link.szUrl = Alloc((nc + 1) * sizeof(WCHAR));
373 if(Last->u.Link.szUrl != NULL)
375 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
379 Last->u.Link.szUrl = NULL;
383 CurrentType = slText;
390 /* we don't know what tag it is, so just continue */
393 if(CurrentType == slText && textstart == NULL)
407 /* save the pointer of the current text item if we couldn't find a tag */
408 if(textstart == NULL && CurrentType == slText)
417 if(textstart != NULL && textlen > 0)
419 Last = SYSLINK_AppendDocItem(infoPtr, textstart, textlen, CurrentType, Last);
422 ERR("Unable to create new document item!\n");
425 if(CurrentType == slLink)
429 if(!(infoPtr->Style & WS_DISABLED))
431 Last->u.Link.state |= LIS_ENABLED;
433 /* Copy the tag parameters */
436 nc = min(lenId, strlenW(lpID));
437 nc = min(nc, MAX_LINKID_TEXT - 1);
438 Last->u.Link.szID = Alloc((nc + 1) * sizeof(WCHAR));
439 if(Last->u.Link.szID != NULL)
441 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
445 Last->u.Link.szID = NULL;
448 nc = min(lenUrl, strlenW(lpUrl));
449 nc = min(nc, L_MAX_URL_LENGTH - 1);
450 Last->u.Link.szUrl = Alloc((nc + 1) * sizeof(WCHAR));
451 if(Last->u.Link.szUrl != NULL)
453 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
457 Last->u.Link.szUrl = NULL;
462 if(linktext != NULL && linklen > 0)
464 /* we got an unclosed link, just display the text */
465 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slText, Last);
468 ERR("Unable to create new document item!\n");
477 /***********************************************************************
478 * SYSLINK_RepaintLink
481 static VOID SYSLINK_RepaintLink (SYSLINK_INFO *infoPtr, PDOC_ITEM DocItem)
486 if(DocItem->Type != slLink)
488 ERR("DocItem not a link!\n");
492 bl = DocItem->Blocks;
499 InvalidateRect(infoPtr->Self, &bl->rc, TRUE);
500 n -= bl->nChars + bl->nSkip;
506 /***********************************************************************
507 * SYSLINK_GetLinkItemByIndex
508 * Retrieves a document link by its index
510 static PDOC_ITEM SYSLINK_GetLinkItemByIndex (SYSLINK_INFO *infoPtr, int iLink)
512 PDOC_ITEM Current = infoPtr->Items;
514 while(Current != NULL)
516 if((Current->Type == slLink) && (iLink-- <= 0))
520 Current = Current->Next;
525 /***********************************************************************
526 * SYSLINK_GetFocusLink
527 * Retrieves the link that has the LIS_FOCUSED bit
529 static PDOC_ITEM SYSLINK_GetFocusLink (SYSLINK_INFO *infoPtr, int *LinkId)
531 PDOC_ITEM Current = infoPtr->Items;
534 while(Current != NULL)
536 if((Current->Type == slLink))
538 if(Current->u.Link.state & LIS_FOCUSED)
546 Current = Current->Next;
551 /***********************************************************************
552 * SYSLINK_GetNextLink
555 static PDOC_ITEM SYSLINK_GetNextLink (SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
557 for(Current = (Current != NULL ? Current->Next : infoPtr->Items);
559 Current = Current->Next)
561 if(Current->Type == slLink)
569 /***********************************************************************
570 * SYSLINK_GetPrevLink
571 * Gets the previous link
573 static PDOC_ITEM SYSLINK_GetPrevLink (SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
577 /* returns the last link */
578 PDOC_ITEM Last = NULL;
580 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
582 if(Current->Type == slLink)
591 /* returns the previous link */
592 PDOC_ITEM Cur, Prev = NULL;
594 for(Cur = infoPtr->Items; Cur != NULL; Cur = Cur->Next)
600 if(Cur->Type == slLink)
609 /***********************************************************************
611 * Tries to wrap a line.
613 static BOOL SYSLINK_WrapLine (HDC hdc, LPWSTR Text, WCHAR BreakChar, int *LineLen,
614 int nFit, LPSIZE Extent, int Width)
625 Current = Text + nFit;
627 /* check if we're in the middle of a word */
628 if((*Current) != BreakChar)
630 /* search for the beginning of the word */
631 while(Current > Text && (*(Current - 1)) != BreakChar)
648 /***********************************************************************
650 * Renders the document in memory
652 static VOID SYSLINK_Render (SYSLINK_INFO *infoPtr, HDC hdc)
657 int x, y, LineHeight;
659 GetClientRect(infoPtr->Self, &rc);
660 rc.right -= SL_RIGHTMARGIN;
661 rc.bottom -= SL_BOTTOMMARGIN;
663 if(rc.right - SL_LEFTMARGIN < 0 || rc.bottom - SL_TOPMARGIN < 0) return;
665 hOldFont = SelectObject(hdc, infoPtr->Font);
671 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
675 PDOC_TEXTBLOCK bl, cbl;
679 if(Current->nText == 0)
687 if (Current->Blocks != NULL)
689 Free(Current->Blocks);
690 Current->Blocks = NULL;
695 if(Current->Type == slText)
697 SelectObject(hdc, infoPtr->Font);
699 else if(Current->Type == slLink)
701 SelectObject(hdc, infoPtr->LinkFont);
708 /* skip break characters unless they're the first of the doc item */
709 if(tx != Current->Text || x == SL_LEFTMARGIN)
711 while(n > 0 && (*tx) == infoPtr->BreakChar)
719 if((n == 0 && SkipChars != 0) ||
720 GetTextExtentExPointW(hdc, tx, n, rc.right - x, &nFit, NULL, &szDim))
727 Wrap = SYSLINK_WrapLine(hdc, tx, infoPtr->BreakChar, &LineLen, nFit, &szDim, rc.right - x);
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))
764 PDOC_TEXTBLOCK nbl = ReAlloc(bl, (nBlocks + 1) * sizeof(DOC_TEXTBLOCK));
779 bl = Alloc(sizeof(DOC_TEXTBLOCK));
786 cbl = bl + nBlocks - 1;
788 cbl->nChars = LineLen;
789 cbl->nSkip = SkipChars;
792 cbl->rc.right = x + szDim.cx;
793 cbl->rc.bottom = y + szDim.cy;
798 LineHeight = max(LineHeight, szDim.cy);
810 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
824 Current->Blocks = bl;
828 SelectObject(hdc, hOldFont);
831 /***********************************************************************
833 * Draws the SysLink control.
835 static LRESULT SYSLINK_Draw (SYSLINK_INFO *infoPtr, HDC hdc)
840 COLORREF OldTextColor, OldBkColor;
842 hOldFont = SelectObject(hdc, infoPtr->Font);
843 OldTextColor = SetTextColor(hdc, infoPtr->TextColor);
844 OldBkColor = SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
846 GetClientRect(infoPtr->Self, &rc);
847 rc.right -= SL_RIGHTMARGIN + SL_LEFTMARGIN;
848 rc.bottom -= SL_BOTTOMMARGIN + SL_TOPMARGIN;
850 if(rc.right < 0 || rc.bottom < 0) return 0;
852 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
858 bl = Current->Blocks;
864 if(Current->Type == slText)
866 SelectObject(hdc, infoPtr->Font);
867 SetTextColor(hdc, infoPtr->TextColor);
871 SelectObject(hdc, infoPtr->LinkFont);
872 SetTextColor(hdc, (!(Current->u.Link.state & LIS_VISITED) ? infoPtr->LinkColor : infoPtr->VisitedColor));
878 ExtTextOutW(hdc, bl->rc.left, bl->rc.top, ETO_OPAQUE | ETO_CLIPPED, &bl->rc, tx, bl->nChars, NULL);
879 if((Current->Type == slLink) && (Current->u.Link.state & LIS_FOCUSED) && infoPtr->HasFocus)
881 COLORREF PrevTextColor;
882 PrevTextColor = SetTextColor(hdc, infoPtr->TextColor);
883 DrawFocusRect(hdc, &bl->rc);
884 SetTextColor(hdc, PrevTextColor);
887 n -= bl->nChars + bl->nSkip;
893 SetBkColor(hdc, OldBkColor);
894 SetTextColor(hdc, OldTextColor);
895 SelectObject(hdc, hOldFont);
901 /***********************************************************************
903 * Handles the WM_PAINT message.
905 static LRESULT SYSLINK_Paint (SYSLINK_INFO *infoPtr, HDC hdcParam)
910 hdc = hdcParam ? hdcParam : BeginPaint (infoPtr->Self, &ps);
913 SYSLINK_Draw (infoPtr, hdc);
914 if (!hdcParam) EndPaint (infoPtr->Self, &ps);
920 /***********************************************************************
922 * Set new Font for the SysLink control.
924 static HFONT SYSLINK_SetFont (SYSLINK_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
929 HFONT hOldFont = infoPtr->Font;
930 infoPtr->Font = hFont;
932 /* free the underline font */
933 if(infoPtr->LinkFont != NULL)
935 DeleteObject(infoPtr->LinkFont);
936 infoPtr->LinkFont = NULL;
939 /* Render text position and word wrapping in memory */
940 hdc = GetDC(infoPtr->Self);
943 /* create a new underline font */
944 if(GetTextMetricsW(hdc, &tm) &&
945 GetObjectW(infoPtr->Font, sizeof(LOGFONTW), &lf))
947 lf.lfUnderline = TRUE;
948 infoPtr->LinkFont = CreateFontIndirectW(&lf);
949 infoPtr->BreakChar = tm.tmBreakChar;
953 ERR("Failed to create link font!\n");
956 SYSLINK_Render(infoPtr, hdc);
957 ReleaseDC(infoPtr->Self, hdc);
962 RedrawWindow(infoPtr->Self, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
968 /***********************************************************************
970 * Set new text for the SysLink control.
972 static LRESULT SYSLINK_SetText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
976 /* clear the document */
977 SYSLINK_ClearDoc(infoPtr);
979 if(Text == NULL || (textlen = lstrlenW(Text)) == 0)
984 /* let's parse the string and create a document */
985 if(SYSLINK_ParseText(infoPtr, Text) > 0)
987 /* Render text position and word wrapping in memory */
988 HDC hdc = GetDC(infoPtr->Self);
991 SYSLINK_Render(infoPtr, hdc);
992 ReleaseDC(infoPtr->Self, hdc);
994 InvalidateRect(infoPtr->Self, NULL, TRUE);
1001 /***********************************************************************
1002 * SYSLINK_SetFocusLink
1003 * Updates the focus status bits and focusses the specified link.
1004 * If no document item is specified, the focus bit will be removed from all links.
1005 * Returns the previous focused item.
1007 static PDOC_ITEM SYSLINK_SetFocusLink (SYSLINK_INFO *infoPtr, PDOC_ITEM DocItem)
1009 PDOC_ITEM Current, PrevFocus = NULL;
1011 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1013 if(Current->Type == slLink)
1015 if((PrevFocus == NULL) && (Current->u.Link.state & LIS_FOCUSED))
1017 PrevFocus = Current;
1020 if(Current == DocItem)
1022 Current->u.Link.state |= LIS_FOCUSED;
1026 Current->u.Link.state &= ~LIS_FOCUSED;
1034 /***********************************************************************
1036 * Sets the states and attributes of a link item.
1038 static LRESULT SYSLINK_SetItem (SYSLINK_INFO *infoPtr, PLITEM Item)
1044 BOOL Repaint = FALSE;
1046 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1048 ERR("Invalid Flags!\n");
1052 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1055 ERR("Link %d couldn't be found\n", Item->iLink);
1059 if(Item->mask & LIF_ITEMID)
1061 nc = min(lstrlenW(Item->szID), MAX_LINKID_TEXT - 1);
1062 szId = Alloc((nc + 1) * sizeof(WCHAR));
1065 lstrcpynW(szId, Item->szID, nc + 1);
1069 ERR("Unable to allocate memory for link id\n");
1074 if(Item->mask & LIF_URL)
1076 nc = min(lstrlenW(Item->szUrl), L_MAX_URL_LENGTH - 1);
1077 szUrl = Alloc((nc + 1) * sizeof(WCHAR));
1080 lstrcpynW(szUrl, Item->szUrl, nc + 1);
1089 ERR("Unable to allocate memory for link url\n");
1094 if(Item->mask & LIF_ITEMID)
1098 Free(di->u.Link.szID);
1100 di->u.Link.szID = szId;
1103 if(Item->mask & LIF_URL)
1105 if(di->u.Link.szUrl)
1107 Free(di->u.Link.szUrl);
1109 di->u.Link.szUrl = szUrl;
1112 if(Item->mask & LIF_STATE)
1114 UINT oldstate = di->u.Link.state;
1115 /* clear the masked bits */
1116 di->u.Link.state &= ~(Item->stateMask & LIS_MASK);
1118 di->u.Link.state |= (Item->state & Item->stateMask) & LIS_MASK;
1119 Repaint = (oldstate != di->u.Link.state);
1121 /* update the focus */
1122 SYSLINK_SetFocusLink(infoPtr, ((di->u.Link.state & LIS_FOCUSED) ? di : NULL));
1127 SYSLINK_RepaintLink(infoPtr, di);
1133 /***********************************************************************
1135 * Retrieves the states and attributes of a link item.
1137 static LRESULT SYSLINK_GetItem (SYSLINK_INFO *infoPtr, PLITEM Item)
1141 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1143 ERR("Invalid Flags!\n");
1147 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1150 ERR("Link %d couldn't be found\n", Item->iLink);
1154 if(Item->mask & LIF_STATE)
1156 Item->state = (di->u.Link.state & Item->stateMask);
1157 if(!infoPtr->HasFocus)
1159 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1160 Item->state &= ~LIS_FOCUSED;
1164 if(Item->mask & LIF_ITEMID)
1168 lstrcpyW(Item->szID, di->u.Link.szID);
1176 if(Item->mask & LIF_URL)
1178 if(di->u.Link.szUrl)
1180 lstrcpyW(Item->szUrl, di->u.Link.szUrl);
1191 /***********************************************************************
1192 * SYSLINK_PtInDocItem
1193 * Determines if a point is in the region of a document item
1195 static BOOL SYSLINK_PtInDocItem (PDOC_ITEM DocItem, POINT pt)
1200 bl = DocItem->Blocks;
1207 if (PtInRect(&bl->rc, pt))
1211 n -= bl->nChars + bl->nSkip;
1219 /***********************************************************************
1221 * Determines the link the user clicked on.
1223 static LRESULT SYSLINK_HitTest (SYSLINK_INFO *infoPtr, PLHITTESTINFO HitTest)
1228 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1230 if(Current->Type == slLink)
1232 if(SYSLINK_PtInDocItem(Current, HitTest->pt))
1234 HitTest->item.mask = 0;
1235 HitTest->item.iLink = id;
1236 HitTest->item.state = 0;
1237 HitTest->item.stateMask = 0;
1238 if(Current->u.Link.szID)
1240 lstrcpyW(HitTest->item.szID, Current->u.Link.szID);
1244 HitTest->item.szID[0] = 0;
1246 if(Current->u.Link.szUrl)
1248 lstrcpyW(HitTest->item.szUrl, Current->u.Link.szUrl);
1252 HitTest->item.szUrl[0] = 0;
1263 /***********************************************************************
1264 * SYSLINK_GetIdealHeight
1265 * Returns the preferred height of a link at the current control's width.
1267 static LRESULT SYSLINK_GetIdealHeight (SYSLINK_INFO *infoPtr)
1269 HDC hdc = GetDC(infoPtr->Self);
1274 HGDIOBJ hOldFont = SelectObject(hdc, infoPtr->Font);
1276 if(GetTextMetricsW(hdc, &tm))
1278 height = tm.tmHeight;
1284 SelectObject(hdc, hOldFont);
1285 ReleaseDC(infoPtr->Self, hdc);
1292 /***********************************************************************
1293 * SYSLINK_SendParentNotify
1294 * Sends a WM_NOTIFY message to the parent window.
1296 static LRESULT SYSLINK_SendParentNotify (SYSLINK_INFO *infoPtr, UINT code, PDOC_ITEM Link, int iLink)
1300 nml.hdr.hwndFrom = infoPtr->Self;
1301 nml.hdr.idFrom = GetWindowLongPtrW(infoPtr->Self, GWLP_ID);
1302 nml.hdr.code = code;
1305 nml.item.iLink = iLink;
1307 nml.item.stateMask = 0;
1308 if(Link->u.Link.szID)
1310 lstrcpyW(nml.item.szID, Link->u.Link.szID);
1314 nml.item.szID[0] = 0;
1316 if(Link->u.Link.szUrl)
1318 lstrcpyW(nml.item.szUrl, Link->u.Link.szUrl);
1322 nml.item.szUrl[0] = 0;
1325 return SendMessageW(infoPtr->Notify, WM_NOTIFY, (WPARAM)nml.hdr.idFrom, (LPARAM)&nml);
1328 /***********************************************************************
1330 * Handles receiving the input focus.
1332 static LRESULT SYSLINK_SetFocus (SYSLINK_INFO *infoPtr, HWND PrevFocusWindow)
1336 infoPtr->HasFocus = TRUE;
1338 /* We always select the first link, even if we activated the control using
1339 SHIFT+TAB. This is the default behavior */
1340 Focus = SYSLINK_GetNextLink(infoPtr, NULL);
1343 SYSLINK_SetFocusLink(infoPtr, Focus);
1346 SYSLINK_RepaintLink(infoPtr, Focus);
1351 /***********************************************************************
1353 * Handles losing the input focus.
1355 static LRESULT SYSLINK_KillFocus (SYSLINK_INFO *infoPtr, HWND NewFocusWindow)
1359 infoPtr->HasFocus = FALSE;
1360 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1364 SYSLINK_RepaintLink(infoPtr, Focus);
1370 /***********************************************************************
1372 * Returns a link at the specified position
1374 static PDOC_ITEM SYSLINK_LinkAtPt (SYSLINK_INFO *infoPtr, POINT *pt, int *LinkId, BOOL MustBeEnabled)
1379 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1381 if((Current->Type == slLink) && SYSLINK_PtInDocItem(Current, *pt) &&
1382 (!MustBeEnabled || (MustBeEnabled && (Current->u.Link.state & LIS_ENABLED))))
1396 /***********************************************************************
1397 * SYSLINK_LButtonDown
1398 * Handles mouse clicks
1400 static LRESULT SYSLINK_LButtonDown (SYSLINK_INFO *infoPtr, DWORD Buttons, POINT *pt)
1402 PDOC_ITEM Current, Old;
1405 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1408 SetFocus(infoPtr->Self);
1410 Old = SYSLINK_SetFocusLink(infoPtr, Current);
1411 if(Old != NULL && Old != Current)
1413 SYSLINK_RepaintLink(infoPtr, Old);
1415 infoPtr->MouseDownID = id;
1416 SYSLINK_RepaintLink(infoPtr, Current);
1422 /***********************************************************************
1424 * Handles mouse clicks
1426 static LRESULT SYSLINK_LButtonUp (SYSLINK_INFO *infoPtr, DWORD Buttons, POINT *pt)
1428 if(infoPtr->MouseDownID > -1)
1433 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1434 if((Current != NULL) && (Current->u.Link.state & LIS_FOCUSED) && (infoPtr->MouseDownID == id))
1436 SYSLINK_SendParentNotify(infoPtr, NM_CLICK, Current, id);
1440 infoPtr->MouseDownID = -1;
1445 /***********************************************************************
1447 * Handles ENTER key events
1449 static BOOL SYSLINK_OnEnter (SYSLINK_INFO *infoPtr)
1451 if(infoPtr->HasFocus)
1456 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1459 SYSLINK_SendParentNotify(infoPtr, NM_RETURN, Focus, id);
1466 /***********************************************************************
1467 * SYSKEY_SelectNextPrevLink
1468 * Changes the currently focused link
1470 static BOOL SYSKEY_SelectNextPrevLink (SYSLINK_INFO *infoPtr, BOOL Prev)
1472 if(infoPtr->HasFocus)
1477 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1480 PDOC_ITEM NewFocus, OldFocus;
1483 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1485 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1487 if(NewFocus != NULL)
1489 OldFocus = SYSLINK_SetFocusLink(infoPtr, NewFocus);
1491 if(OldFocus != NewFocus)
1493 SYSLINK_RepaintLink(infoPtr, OldFocus);
1495 SYSLINK_RepaintLink(infoPtr, NewFocus);
1503 /***********************************************************************
1504 * SYSKEY_SelectNextPrevLink
1505 * Determines if there's a next or previous link to decide whether the control
1506 * should capture the tab key message
1508 static BOOL SYSLINK_NoNextLink (SYSLINK_INFO *infoPtr, BOOL Prev)
1510 PDOC_ITEM Focus, NewFocus;
1512 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1514 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1516 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1518 return NewFocus == NULL;
1521 /***********************************************************************
1524 static LRESULT WINAPI SysLinkWindowProc(HWND hwnd, UINT message,
1525 WPARAM wParam, LPARAM lParam)
1527 SYSLINK_INFO *infoPtr;
1529 TRACE("hwnd=%p msg=%04x wparam=%x lParam=%lx\n", hwnd, message, wParam, lParam);
1531 infoPtr = (SYSLINK_INFO *)GetWindowLongPtrW(hwnd, 0);
1533 if (!infoPtr && message != WM_CREATE)
1534 goto HandleDefaultMessage;
1537 case WM_PRINTCLIENT:
1539 return SYSLINK_Paint (infoPtr, (HDC)wParam);
1544 DWORD mp = GetMessagePos();
1546 ht.pt.x = (short)LOWORD(mp);
1547 ht.pt.y = (short)HIWORD(mp);
1549 ScreenToClient(infoPtr->Self, &ht.pt);
1550 if(SYSLINK_HitTest (infoPtr, &ht))
1552 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_HAND));
1555 /* let the default window proc handle this message */
1556 goto HandleDefaultMessage;
1561 HDC hdc = GetDC(infoPtr->Self);
1564 SYSLINK_Render(infoPtr, hdc);
1565 ReleaseDC(infoPtr->Self, hdc);
1571 return (LRESULT)infoPtr->Font;
1574 return (LRESULT)SYSLINK_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1577 SYSLINK_SetText(infoPtr, (LPWSTR)lParam);
1578 goto HandleDefaultMessage;
1580 case WM_LBUTTONDOWN:
1583 pt.x = LOWORD(lParam);
1584 pt.y = HIWORD(lParam);
1585 return SYSLINK_LButtonDown(infoPtr, wParam, &pt);
1590 pt.x = LOWORD(lParam);
1591 pt.y = HIWORD(lParam);
1592 return SYSLINK_LButtonUp(infoPtr, wParam, &pt);
1600 SYSLINK_OnEnter(infoPtr);
1604 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1605 SYSKEY_SelectNextPrevLink(infoPtr, shift);
1609 goto HandleDefaultMessage;
1614 LRESULT Ret = DLGC_HASSETSEL;
1615 int vk = (lParam != 0 ? (int)((LPMSG)lParam)->wParam : 0);
1619 Ret |= DLGC_WANTMESSAGE;
1623 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1624 if(!SYSLINK_NoNextLink(infoPtr, shift))
1626 Ret |= DLGC_WANTTAB;
1630 Ret |= DLGC_WANTCHARS;
1642 pt.x = LOWORD(lParam);
1643 pt.y = HIWORD(lParam);
1645 GetClientRect(infoPtr->Self, &rc);
1646 ScreenToClient(infoPtr->Self, &pt);
1647 if(pt.x < 0 || pt.y < 0 || pt.x > rc.right || pt.y > rc.bottom)
1652 if(SYSLINK_LinkAtPt(infoPtr, &pt, NULL, FALSE))
1657 return HTTRANSPARENT;
1661 return SYSLINK_HitTest(infoPtr, (PLHITTESTINFO)lParam);
1664 return SYSLINK_SetItem(infoPtr, (PLITEM)lParam);
1667 return SYSLINK_GetItem(infoPtr, (PLITEM)lParam);
1669 case LM_GETIDEALHEIGHT:
1670 return SYSLINK_GetIdealHeight(infoPtr);
1673 return SYSLINK_SetFocus(infoPtr, (HWND)wParam);
1676 return SYSLINK_KillFocus(infoPtr, (HWND)wParam);
1679 infoPtr->Style &= ~WS_DISABLED;
1680 infoPtr->Style |= (wParam ? 0 : WS_DISABLED);
1681 InvalidateRect (infoPtr->Self, NULL, FALSE);
1684 case WM_STYLECHANGED:
1685 if (wParam == GWL_STYLE)
1687 infoPtr->Style = ((LPSTYLESTRUCT)lParam)->styleNew;
1689 InvalidateRect(infoPtr->Self, NULL, TRUE);
1694 /* allocate memory for info struct */
1695 infoPtr = Alloc (sizeof(SYSLINK_INFO));
1696 if (!infoPtr) return -1;
1697 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1699 /* initialize the info struct */
1700 infoPtr->Self = hwnd;
1701 infoPtr->Notify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1702 infoPtr->Style = ((LPCREATESTRUCTW)lParam)->style;
1704 infoPtr->LinkFont = 0;
1705 infoPtr->Items = NULL;
1706 infoPtr->HasFocus = FALSE;
1707 infoPtr->MouseDownID = -1;
1708 infoPtr->TextColor = GetSysColor(COLOR_WINDOWTEXT);
1709 infoPtr->LinkColor = GetSysColor(COLOR_HIGHLIGHT);
1710 infoPtr->VisitedColor = GetSysColor(COLOR_HIGHLIGHT);
1711 infoPtr->BreakChar = ' ';
1712 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd);
1713 SYSLINK_SetText(infoPtr, ((LPCREATESTRUCTW)lParam)->lpszName);
1717 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd);
1718 SYSLINK_ClearDoc(infoPtr);
1719 if(infoPtr->Font != 0) DeleteObject(infoPtr->Font);
1720 if(infoPtr->LinkFont != 0) DeleteObject(infoPtr->LinkFont);
1721 SetWindowLongPtrW(hwnd, 0, 0);
1726 HandleDefaultMessage:
1727 if ((message >= WM_USER) && (message < WM_APP))
1729 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam );
1731 return DefWindowProcW(hwnd, message, wParam, lParam);
1736 /***********************************************************************
1737 * SYSLINK_Register [Internal]
1739 * Registers the SysLink window class.
1741 VOID SYSLINK_Register (void)
1745 ZeroMemory (&wndClass, sizeof(wndClass));
1746 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1747 wndClass.lpfnWndProc = SysLinkWindowProc;
1748 wndClass.cbClsExtra = 0;
1749 wndClass.cbWndExtra = sizeof (SYSLINK_INFO *);
1750 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1751 wndClass.lpszClassName = WC_LINK;
1753 RegisterClassW (&wndClass);
1757 /***********************************************************************
1758 * SYSLINK_Unregister [Internal]
1760 * Unregisters the SysLink window class.
1762 VOID SYSLINK_Unregister (void)
1764 UnregisterClassW (WC_LINK, NULL);