4 * Copyright 2004 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
21 * - Fix SHIFT+TAB and TAB issue (wrong link is selected when control gets the focus)
22 * - Better string parsing
23 * - Improve word wrapping
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(progress);
41 INT WINAPI StrCmpNIW(LPCWSTR,LPCWSTR,INT);
43 #define SYSLINK_Alloc(size) HeapAlloc(GetProcessHeap(), 0, (size))
44 #define SYSLINK_Free(ptr) HeapFree(GetProcessHeap(), 0, (ptr))
45 #define SYSLINK_ReAlloc(ptr, size) HeapReAlloc(GetProcessHeap(), 0, ptr, (size))
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 LPWSTR Text; /* Text of the document item */
66 UINT nText; /* Number of characters of the text */
67 SL_ITEM_TYPE Type; /* type of the item */
68 PDOC_TEXTBLOCK Blocks; /* Array of text blocks */
73 UINT state; /* Link state */
74 WCHAR *szID; /* Link ID string */
75 WCHAR *szUrl; /* Link URL string */
76 HRGN hRgn; /* Region of the link */
83 } DOC_ITEM, *PDOC_ITEM;
87 HWND Self; /* The window handle for this control */
88 PDOC_ITEM Items; /* Address to the first document item */
89 BOOL HasFocus; /* Whether the control has the input focus */
90 int MouseDownID; /* ID of the link that the mouse button first selected */
91 HFONT Font; /* Handle to the font for text */
92 HFONT LinkFont; /* Handle to the font for links */
93 COLORREF TextColor; /* Color of the text */
94 COLORREF LinkColor; /* Color of links */
95 COLORREF VisitedColor; /* Color of visited links */
98 static const WCHAR SL_LINKOPEN[] = { '<','a', 0 };
99 static const WCHAR SL_HREF[] = { 'h','r','e','f','=','\"',0 };
100 static const WCHAR SL_ID[] = { 'i','d','=','\"',0 };
101 static const WCHAR SL_LINKCLOSE[] = { '<','/','a','>',0 };
103 /* Control configuration constants */
105 #define SL_LEFTMARGIN (0)
106 #define SL_TOPMARGIN (0)
107 #define SL_RIGHTMARGIN (0)
108 #define SL_BOTTOMMARGIN (0)
110 /***********************************************************************
111 * SYSLINK_FreeDocItem
112 * Frees all data and gdi objects associated with a document item
114 static VOID SYSLINK_FreeDocItem (PDOC_ITEM DocItem)
116 if(DocItem->Type == slLink)
118 SYSLINK_Free(DocItem->u.Link.szID);
119 SYSLINK_Free(DocItem->u.Link.szUrl);
122 if(DocItem->Type == slLink && DocItem->u.Link.hRgn != NULL)
124 DeleteObject(DocItem->u.Link.hRgn);
127 /* we don't free Text because it's just a pointer to a character in the
128 entire window text string */
130 SYSLINK_Free(DocItem);
133 /***********************************************************************
134 * SYSLINK_AppendDocItem
135 * Create and append a new document item.
137 static PDOC_ITEM SYSLINK_AppendDocItem (SYSLINK_INFO *infoPtr, LPWSTR Text, UINT textlen,
138 SL_ITEM_TYPE type, PDOC_ITEM LastItem)
141 Item = SYSLINK_Alloc(sizeof(DOC_ITEM) + ((textlen + 1) * sizeof(WCHAR)));
144 ERR("Failed to alloc DOC_ITEM structure!\n");
147 textlen = min(textlen, lstrlenW(Text));
150 Item->Text = (LPWSTR)(Item + 1);
151 Item->nText = textlen;
157 LastItem->Next = Item;
161 infoPtr->Items = Item;
164 lstrcpynW(Item->Text, Text, textlen + 1);
165 Item->Text[textlen] = 0;
170 /***********************************************************************
172 * Clears the document tree
174 static VOID SYSLINK_ClearDoc (SYSLINK_INFO *infoPtr)
176 PDOC_ITEM Item, Next;
178 Item = infoPtr->Items;
182 SYSLINK_FreeDocItem(Item);
186 infoPtr->Items = NULL;
189 /***********************************************************************
191 * Parses the window text string and creates a document. Returns the
192 * number of document items created.
194 static UINT SYSLINK_ParseText (SYSLINK_INFO *infoPtr, LPWSTR Text)
196 WCHAR *current, *textstart, *linktext, *firsttag;
197 int taglen = 0, textlen, linklen, docitems = 0;
198 PDOC_ITEM Last = NULL;
199 SL_ITEM_TYPE CurrentType = slText;
204 Style = GetWindowLongW(infoPtr->Self, GWL_STYLE);
212 for(current = (WCHAR*)Text; *current != 0;)
216 if(!StrCmpNIW(current, SL_LINKOPEN, 2) && (CurrentType == slText))
218 BOOL ValidParam = FALSE, ValidLink = FALSE;
220 switch (*(current + 2))
223 /* we just have to deal with a <a> tag */
234 /* we expect parameters, parse them */
235 LPWSTR *CurrentParameter = NULL;
236 UINT *CurrentParameterLen = NULL;
240 tmp = current + taglen;
245 /* compare the current position with all known parameters */
246 if(!StrCmpNIW(tmp, SL_HREF, 6))
250 CurrentParameter = &lpUrl;
251 CurrentParameterLen = &lenUrl;
253 else if(!StrCmpNIW(tmp, SL_ID, 4))
257 CurrentParameter = &lpID;
258 CurrentParameterLen = &lenId;
267 /* we got a known parameter, now search until the next " character.
268 If we can't find a " character, there's a syntax error and we just assume it's text */
270 *CurrentParameter = current + taglen;
271 *CurrentParameterLen = 0;
273 for(tmp = *CurrentParameter; *tmp != 0; tmp++)
282 (*CurrentParameterLen)++;
287 /* we're done with this parameter, now there are only 2 possibilities:
288 * 1. another parameter is coming, so expect a ' ' (space) character
289 * 2. the tag is being closed, so expect a '<' character
294 /* we expect another parameter, do the whole thing again */
300 /* the tag is being closed, we're done */
314 if(ValidLink && ValidParam)
316 /* the <a ...> tag appears to be valid. save all information
317 so we can add the link if we find a valid </a> tag later */
318 CurrentType = slLink;
319 linktext = current + taglen;
328 if(textstart == NULL)
334 else if(!StrCmpNIW(current, SL_LINKCLOSE, 4) && (CurrentType == slLink) && firsttag)
336 /* there's a <a...> tag opened, first add the previous text, if present */
337 if(textstart != NULL && textlen > 0 && firsttag > textstart)
339 Last = SYSLINK_AppendDocItem(infoPtr, textstart, firsttag - textstart, slText, Last);
342 ERR("Unable to create new document item!\n");
350 /* now it's time to add the link to the document */
352 if(linktext != NULL && linklen > 0)
354 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slLink, Last);
357 ERR("Unable to create new document item!\n");
361 if(CurrentType == slLink)
365 if(!(Style & WS_DISABLED))
367 Last->u.Link.state |= LIS_ENABLED;
369 /* Copy the tag parameters */
372 nc = min(lenId, strlenW(lpID));
373 nc = min(nc, MAX_LINKID_TEXT);
374 Last->u.Link.szID = SYSLINK_Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
375 if(Last->u.Link.szID != NULL)
377 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
378 Last->u.Link.szID[nc] = 0;
382 Last->u.Link.szID = NULL;
385 nc = min(lenUrl, strlenW(lpUrl));
386 nc = min(nc, L_MAX_URL_LENGTH);
387 Last->u.Link.szUrl = SYSLINK_Alloc((L_MAX_URL_LENGTH + 1) * sizeof(WCHAR));
388 if(Last->u.Link.szUrl != NULL)
390 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
391 Last->u.Link.szUrl[nc] = 0;
395 Last->u.Link.szUrl = NULL;
399 CurrentType = slText;
406 /* we don't know what tag it is, so just continue */
409 if(CurrentType == slText && textstart == NULL)
423 /* save the pointer of the current text item if we couldn't find a tag */
424 if(textstart == NULL && CurrentType == slText)
433 if(textstart != NULL && textlen > 0)
435 Last = SYSLINK_AppendDocItem(infoPtr, textstart, textlen, CurrentType, Last);
438 ERR("Unable to create new document item!\n");
441 if(CurrentType == slLink)
445 if(!(Style & WS_DISABLED))
447 Last->u.Link.state |= LIS_ENABLED;
449 /* Copy the tag parameters */
452 nc = min(lenId, strlenW(lpID));
453 nc = min(nc, MAX_LINKID_TEXT);
454 Last->u.Link.szID = SYSLINK_Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
455 if(Last->u.Link.szID != NULL)
457 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
458 Last->u.Link.szID[nc] = 0;
462 Last->u.Link.szID = NULL;
465 nc = min(lenUrl, strlenW(lpUrl));
466 nc = min(nc, L_MAX_URL_LENGTH);
467 Last->u.Link.szUrl = SYSLINK_Alloc((L_MAX_URL_LENGTH + 1) * sizeof(WCHAR));
468 if(Last->u.Link.szUrl != NULL)
470 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
471 Last->u.Link.szUrl[nc] = 0;
475 Last->u.Link.szUrl = NULL;
480 if(linktext != NULL && linklen > 0)
482 /* we got a unclosed link, just display the text */
483 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slText, Last);
486 ERR("Unable to create new document item!\n");
495 /***********************************************************************
496 * SYSLINK_RepaintLink
499 static VOID SYSLINK_RepaintLink (SYSLINK_INFO *infoPtr, PDOC_ITEM DocItem)
501 if(DocItem->Type != slLink)
503 ERR("DocItem not a link!\n");
507 if(DocItem->u.Link.hRgn != NULL)
509 /* repaint the region */
510 RedrawWindow(infoPtr->Self, NULL, DocItem->u.Link.hRgn, RDW_INVALIDATE | RDW_UPDATENOW);
514 /***********************************************************************
515 * SYSLINK_GetLinkItemByIndex
516 * Retrieves a document link by its index
518 static PDOC_ITEM SYSLINK_GetLinkItemByIndex (SYSLINK_INFO *infoPtr, int iLink)
520 PDOC_ITEM Current = infoPtr->Items;
522 while(Current != NULL)
524 if((Current->Type == slLink) && (iLink-- <= 0))
528 Current = Current->Next;
533 /***********************************************************************
534 * SYSLINK_GetFocusLink
535 * Retrieves the link that has the LIS_FOCUSED bit
537 static PDOC_ITEM SYSLINK_GetFocusLink (SYSLINK_INFO *infoPtr, int *LinkId)
539 PDOC_ITEM Current = infoPtr->Items;
542 while(Current != NULL)
544 if((Current->Type == slLink))
546 if(Current->u.Link.state & LIS_FOCUSED)
554 Current = Current->Next;
559 /***********************************************************************
560 * SYSLINK_GetNextLink
563 static PDOC_ITEM SYSLINK_GetNextLink (SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
565 for(Current = (Current != NULL ? Current->Next : infoPtr->Items);
567 Current = Current->Next)
569 if(Current->Type == slLink)
577 /***********************************************************************
578 * SYSLINK_GetPrevLink
579 * Gets the previous link
581 static PDOC_ITEM SYSLINK_GetPrevLink (SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
585 /* returns the last link */
586 PDOC_ITEM Last = NULL;
588 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
590 if(Current->Type == slLink)
599 /* returns the previous link */
600 PDOC_ITEM Cur, Prev = NULL;
602 for(Cur = infoPtr->Items; Cur != NULL; Cur = Cur->Next)
608 if(Cur->Type == slLink)
617 /***********************************************************************
619 * Tries to wrap a line.
621 static BOOL SYSLINK_WrapLine (HDC hdc, LPWSTR Text, WCHAR BreakChar, int *LineLen, int nFit, LPSIZE Extent, int Width)
632 Current = Text + nFit;
634 /* check if we're in the middle of a word */
635 if((*Current) != BreakChar)
637 /* search for the beginning of the word */
638 while(Current > Text && (*(Current - 1)) != BreakChar)
655 /***********************************************************************
657 * Renders the document in memory
659 static VOID SYSLINK_Render (SYSLINK_INFO *infoPtr, HDC hdc)
664 int x, y, LineHeight;
667 GetClientRect(infoPtr->Self, &rc);
668 rc.right -= SL_RIGHTMARGIN;
669 rc.bottom -= SL_BOTTOMMARGIN;
671 if(rc.right - SL_LEFTMARGIN < 0 || rc.bottom - SL_TOPMARGIN < 0) return;
673 hOldFont = SelectObject(hdc, infoPtr->Font);
674 GetTextMetricsW(hdc, &tm);
680 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
684 PDOC_TEXTBLOCK bl, cbl;
688 if(Current->nText == 0)
690 ERR("DOC_ITEM with no text?!\n");
696 bl = Current->Blocks;
699 if(Current->Type == slText)
701 SelectObject(hdc, infoPtr->Font);
703 else if(Current->Type == slLink)
705 SelectObject(hdc, infoPtr->LinkFont);
710 if(GetTextExtentExPointW(hdc, tx, n, rc.right - x, &nFit, NULL, &szDim))
713 BOOL Wrap = SYSLINK_WrapLine(hdc, tx, tm.tmBreakChar, &LineLen, nFit, &szDim, rc.right - x);
717 if(x > SL_LEFTMARGIN)
719 /* move one line down, the word didn't fit into the line */
727 /* the word starts at the beginning of the line and doesn't
728 fit into the line, so break it at the last character that fits */
729 LineLen = max(nFit, 1);
735 GetTextExtentExPointW(hdc, tx, LineLen, rc.right - x, NULL, NULL, &szDim);
740 bl = SYSLINK_ReAlloc(bl, ++nBlocks * sizeof(DOC_TEXTBLOCK));
744 bl = SYSLINK_Alloc(++nBlocks * sizeof(DOC_TEXTBLOCK));
749 cbl = bl + nBlocks - 1;
751 cbl->nChars = LineLen;
754 cbl->rc.right = x + szDim.cx;
755 cbl->rc.bottom = y + szDim.cy;
758 LineHeight = max(LineHeight, szDim.cy);
760 /* (re)calculate the link's region */
761 if(Current->Type == slLink)
765 if(Current->u.Link.hRgn != NULL)
767 DeleteObject(Current->u.Link.hRgn);
769 /* initialize the link's hRgn */
770 Current->u.Link.hRgn = CreateRectRgnIndirect(&cbl->rc);
772 else if(Current->u.Link.hRgn != NULL)
775 hrgn = CreateRectRgnIndirect(&cbl->rc);
776 /* add the rectangle */
777 CombineRgn(Current->u.Link.hRgn, Current->u.Link.hRgn, hrgn, RGN_OR);
791 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
799 ERR("GetTextExtentExPoint() failed?!\n");
803 Current->Blocks = bl;
806 SelectObject(hdc, hOldFont);
809 /***********************************************************************
811 * Draws the SysLink control.
813 static LRESULT SYSLINK_Draw (SYSLINK_INFO *infoPtr, HDC hdc)
818 COLORREF OldTextColor, OldBkColor;
820 hOldFont = SelectObject(hdc, infoPtr->Font);
821 OldTextColor = SetTextColor(hdc, infoPtr->TextColor);
822 OldBkColor = SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
824 GetClientRect(infoPtr->Self, &rc);
825 rc.right -= SL_RIGHTMARGIN + SL_LEFTMARGIN;
826 rc.bottom -= SL_BOTTOMMARGIN + SL_TOPMARGIN;
828 if(rc.right < 0 || rc.bottom < 0) return 0;
830 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
836 bl = Current->Blocks;
842 if(Current->Type == slText)
844 SelectObject(hdc, infoPtr->Font);
845 SetTextColor(hdc, infoPtr->TextColor);
849 SelectObject(hdc, infoPtr->LinkFont);
850 SetTextColor(hdc, (!(Current->u.Link.state & LIS_VISITED) ? infoPtr->LinkColor : infoPtr->VisitedColor));
855 ExtTextOutW(hdc, bl->rc.left, bl->rc.top, ETO_OPAQUE | ETO_CLIPPED, &bl->rc, tx, bl->nChars, NULL);
856 if((Current->Type == slLink) && (Current->u.Link.state & LIS_FOCUSED) && infoPtr->HasFocus)
859 PrevColor = SetBkColor(hdc, OldBkColor);
860 DrawFocusRect(hdc, &bl->rc);
861 SetBkColor(hdc, PrevColor);
870 SetBkColor(hdc, OldBkColor);
871 SetTextColor(hdc, OldTextColor);
872 SelectObject(hdc, hOldFont);
878 /***********************************************************************
880 * Handles the WM_PAINT message.
882 static LRESULT SYSLINK_Paint (SYSLINK_INFO *infoPtr, HDC hdcParam)
887 hdc = hdcParam ? hdcParam : BeginPaint (infoPtr->Self, &ps);
888 SYSLINK_Draw (infoPtr, hdc);
889 if (!hdcParam) EndPaint (infoPtr->Self, &ps);
894 /***********************************************************************
896 * Set new Font for the SysLink control.
898 static HFONT SYSLINK_SetFont (SYSLINK_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
902 HFONT hOldFont = infoPtr->Font;
903 infoPtr->Font = hFont;
905 /* free the underline font */
906 if(infoPtr->LinkFont != NULL)
908 DeleteObject(infoPtr->LinkFont);
909 infoPtr->LinkFont = NULL;
912 /* Render text position and word wrapping in memory */
913 hdc = GetDC(infoPtr->Self);
916 /* create a new underline font */
917 if(GetObjectW(infoPtr->Font, sizeof(LOGFONTW), &lf))
919 lf.lfUnderline = TRUE;
920 infoPtr->LinkFont = CreateFontIndirectW(&lf);
924 ERR("Failed to create link font!\n");
927 SYSLINK_Render(infoPtr, hdc);
928 ReleaseDC(infoPtr->Self, hdc);
933 RedrawWindow(infoPtr->Self, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
939 /***********************************************************************
941 * Set new text for the SysLink control.
943 static LRESULT SYSLINK_SetText (SYSLINK_INFO *infoPtr, LPWSTR Text)
947 /* clear the document */
948 SYSLINK_ClearDoc(infoPtr);
950 textlen = lstrlenW(Text);
951 if(Text == NULL || textlen == 0)
956 /* let's parse the string and create a document */
957 if(SYSLINK_ParseText(infoPtr, Text) > 0)
959 /* Render text position and word wrapping in memory */
960 HDC hdc = GetDC(infoPtr->Self);
961 SYSLINK_Render(infoPtr, hdc);
962 SYSLINK_Draw(infoPtr, hdc);
963 ReleaseDC(infoPtr->Self, hdc);
969 /***********************************************************************
970 * SYSLINK_SetFocusLink
971 * Updates the focus status bits and focusses the specified link.
972 * If no document item is specified, the focus bit will be removed from all links.
973 * Returns the previous focused item.
975 static PDOC_ITEM SYSLINK_SetFocusLink (SYSLINK_INFO *infoPtr, PDOC_ITEM DocItem)
977 PDOC_ITEM Current, PrevFocus = NULL;
979 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
981 if(Current->Type == slLink)
983 if((PrevFocus == NULL) && (Current->u.Link.state & LIS_FOCUSED))
988 if(Current == DocItem)
990 Current->u.Link.state |= LIS_FOCUSED;
994 Current->u.Link.state &= ~LIS_FOCUSED;
1002 /***********************************************************************
1004 * Sets the states and attributes of a link item.
1006 static LRESULT SYSLINK_SetItem (SYSLINK_INFO *infoPtr, PLITEM Item)
1009 BOOL Repaint = FALSE;
1012 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1014 ERR("Invalid Flags!\n");
1018 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1021 ERR("Link %d couldn't be found\n", Item->iLink);
1025 if(Item->mask & LIF_STATE)
1027 UINT oldstate = di->u.Link.state;
1028 /* clear the masked bits */
1029 di->u.Link.state &= ~(Item->stateMask & LIS_MASK);
1031 di->u.Link.state |= (Item->state & Item->stateMask) & LIS_MASK;
1032 Repaint = (oldstate != di->u.Link.state);
1034 /* update the focus */
1035 SYSLINK_SetFocusLink(infoPtr, ((di->u.Link.state & LIS_FOCUSED) ? di : NULL));
1038 if(Item->mask & LIF_ITEMID)
1040 if(!di->u.Link.szID)
1042 di->u.Link.szID = SYSLINK_Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
1045 ERR("Unable to allocate memory for link id\n");
1051 lstrcpynW(di->u.Link.szID, Item->szID, MAX_LINKID_TEXT + 1);
1055 if(Item->mask & LIF_URL)
1057 if(!di->u.Link.szUrl)
1059 di->u.Link.szUrl = SYSLINK_Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
1062 ERR("Unable to allocate memory for link url\n");
1066 if(di->u.Link.szUrl)
1068 lstrcpynW(di->u.Link.szUrl, Item->szUrl, MAX_LINKID_TEXT + 1);
1074 SYSLINK_RepaintLink(infoPtr, di);
1080 /***********************************************************************
1082 * Retrieves the states and attributes of a link item.
1084 static LRESULT SYSLINK_GetItem (SYSLINK_INFO *infoPtr, PLITEM Item)
1088 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1090 ERR("Invalid Flags!\n");
1094 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1097 ERR("Link %d couldn't be found\n", Item->iLink);
1101 if(Item->mask & LIF_STATE)
1103 Item->state = (di->u.Link.state & Item->stateMask);
1104 if(!infoPtr->HasFocus)
1106 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1107 Item->state &= ~LIS_FOCUSED;
1111 if(Item->mask & LIF_ITEMID)
1115 lstrcpynW(Item->szID, di->u.Link.szID, MAX_LINKID_TEXT + 1);
1123 if(Item->mask & LIF_URL)
1125 if(di->u.Link.szUrl)
1127 lstrcpynW(Item->szUrl, di->u.Link.szUrl, L_MAX_URL_LENGTH + 1);
1138 /***********************************************************************
1140 * Determines the link the user clicked on.
1142 static LRESULT SYSLINK_HitTest (SYSLINK_INFO *infoPtr, PLHITTESTINFO HitTest)
1147 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1149 if(Current->Type == slLink)
1151 if((Current->u.Link.hRgn != NULL) &&
1152 PtInRegion(Current->u.Link.hRgn, HitTest->pt.x, HitTest->pt.y))
1154 HitTest->item.mask = 0;
1155 HitTest->item.iLink = id;
1156 HitTest->item.state = 0;
1157 HitTest->item.stateMask = 0;
1158 if(Current->u.Link.szID)
1160 lstrcpynW(HitTest->item.szID, Current->u.Link.szID, MAX_LINKID_TEXT + 1);
1164 HitTest->item.szID[0] = 0;
1166 if(Current->u.Link.szUrl)
1168 lstrcpynW(HitTest->item.szUrl, Current->u.Link.szUrl, L_MAX_URL_LENGTH + 1);
1172 HitTest->item.szUrl[0] = 0;
1183 /***********************************************************************
1184 * SYSLINK_GetIdealHeight
1185 * Returns the preferred height of a link at the current control's width.
1187 static LRESULT SYSLINK_GetIdealHeight (SYSLINK_INFO *infoPtr)
1189 HDC hdc = GetDC(infoPtr->Self);
1194 HGDIOBJ hOldFont = SelectObject(hdc, infoPtr->Font);
1196 if(GetTextMetricsW(hdc, &tm))
1198 height = tm.tmHeight;
1204 SelectObject(hdc, hOldFont);
1205 ReleaseDC(infoPtr->Self, hdc);
1212 /***********************************************************************
1213 * SYSLINK_SendParentNotify
1214 * Sends a WM_NOTIFY message to the parent window.
1216 static LRESULT SYSLINK_SendParentNotify (SYSLINK_INFO *infoPtr, UINT code, PDOC_ITEM Link, int iLink)
1220 nml.hdr.hwndFrom = infoPtr->Self;
1221 nml.hdr.idFrom = GetWindowLongPtrW(infoPtr->Self, GWLP_ID);
1222 nml.hdr.code = code;
1225 nml.item.iLink = iLink;
1227 nml.item.stateMask = 0;
1228 if(Link->u.Link.szID)
1230 lstrcpynW(nml.item.szID, Link->u.Link.szID, MAX_LINKID_TEXT + 1);
1234 nml.item.szID[0] = 0;
1236 if(Link->u.Link.szUrl)
1238 lstrcpynW(nml.item.szUrl, Link->u.Link.szUrl, L_MAX_URL_LENGTH + 1);
1242 nml.item.szUrl[0] = 0;
1245 return SendMessageW(GetParent(infoPtr->Self), WM_NOTIFY, (WPARAM)nml.hdr.idFrom, (LPARAM)&nml);
1248 /***********************************************************************
1250 * Handles receiving the input focus.
1252 static LRESULT SYSLINK_SetFocus (SYSLINK_INFO *infoPtr, HWND PrevFocusWindow)
1256 infoPtr->HasFocus = TRUE;
1259 /* FIXME - How to detect whether SHIFT+TAB or just TAB has been pressed?
1260 * The problem is we could get this message without keyboard input, too
1262 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1264 if(Focus == NULL && (Focus = SYSLINK_GetNextLink(infoPtr, NULL)))
1266 SYSLINK_SetFocusLink(infoPtr, Focus);
1269 /* This is a temporary hack since I'm not really sure how to detect which link to select.
1270 See message above! */
1271 Focus = SYSLINK_GetNextLink(infoPtr, NULL);
1274 SYSLINK_SetFocusLink(infoPtr, Focus);
1278 SYSLINK_RepaintLink(infoPtr, Focus);
1283 /***********************************************************************
1285 * Handles losing the input focus.
1287 static LRESULT SYSLINK_KillFocus (SYSLINK_INFO *infoPtr, HWND NewFocusWindow)
1291 infoPtr->HasFocus = FALSE;
1292 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1296 SYSLINK_RepaintLink(infoPtr, Focus);
1302 /***********************************************************************
1304 * Returns a link at the specified position
1306 static PDOC_ITEM SYSLINK_LinkAtPt (SYSLINK_INFO *infoPtr, POINT *pt, int *LinkId, BOOL MustBeEnabled)
1311 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1313 if((Current->Type == slLink) && (Current->u.Link.hRgn != NULL) &&
1314 PtInRegion(Current->u.Link.hRgn, pt->x, pt->y) &&
1315 (!MustBeEnabled || (MustBeEnabled && (Current->u.Link.state & LIS_ENABLED))))
1329 /***********************************************************************
1330 * SYSLINK_LButtonDown
1331 * Handles mouse clicks
1333 static LRESULT SYSLINK_LButtonDown (SYSLINK_INFO *infoPtr, DWORD Buttons, POINT *pt)
1335 PDOC_ITEM Current, Old;
1338 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1341 Old = SYSLINK_SetFocusLink(infoPtr, Current);
1342 if(Old != NULL && Old != Current)
1344 SYSLINK_RepaintLink(infoPtr, Old);
1346 infoPtr->MouseDownID = id;
1347 SYSLINK_RepaintLink(infoPtr, Current);
1348 SetFocus(infoPtr->Self);
1354 /***********************************************************************
1356 * Handles mouse clicks
1358 static LRESULT SYSLINK_LButtonUp (SYSLINK_INFO *infoPtr, DWORD Buttons, POINT *pt)
1360 if(infoPtr->MouseDownID > -1)
1365 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1366 if((Current != NULL) && (Current->u.Link.state & LIS_FOCUSED) && (infoPtr->MouseDownID == id))
1368 SYSLINK_SendParentNotify(infoPtr, NM_CLICK, Current, id);
1372 infoPtr->MouseDownID = -1;
1377 /***********************************************************************
1379 * Handles ENTER key events
1381 static BOOL SYSLINK_OnEnter (SYSLINK_INFO *infoPtr)
1383 if(infoPtr->HasFocus)
1388 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1391 SYSLINK_SendParentNotify(infoPtr, NM_RETURN, Focus, id);
1398 /***********************************************************************
1399 * SYSKEY_SelectNextPrevLink
1400 * Changes the currently focused link
1402 static BOOL SYSKEY_SelectNextPrevLink (SYSLINK_INFO *infoPtr, BOOL Prev)
1404 if(infoPtr->HasFocus)
1409 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1412 PDOC_ITEM NewFocus, OldFocus;
1415 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1417 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1419 if(NewFocus != NULL)
1421 OldFocus = SYSLINK_SetFocusLink(infoPtr, NewFocus);
1423 if(OldFocus != NewFocus)
1425 SYSLINK_RepaintLink(infoPtr, OldFocus);
1427 SYSLINK_RepaintLink(infoPtr, NewFocus);
1435 /***********************************************************************
1436 * SYSKEY_SelectNextPrevLink
1437 * Determines if there's a next or previous link to decide whether the control
1438 * should capture the tab key message
1440 static BOOL SYSLINK_NoNextLink (SYSLINK_INFO *infoPtr, BOOL Prev)
1442 PDOC_ITEM Focus, NewFocus;
1444 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1446 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1448 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1450 return NewFocus == NULL;
1453 /***********************************************************************
1456 static LRESULT WINAPI SysLinkWindowProc(HWND hwnd, UINT message,
1457 WPARAM wParam, LPARAM lParam)
1459 SYSLINK_INFO *infoPtr;
1461 TRACE("hwnd=%p msg=%04x wparam=%x lParam=%lx\n", hwnd, message, wParam, lParam);
1463 infoPtr = (SYSLINK_INFO *)GetWindowLongPtrW(hwnd, 0);
1465 if (!infoPtr && message != WM_CREATE)
1466 return DefWindowProcW( hwnd, message, wParam, lParam );
1470 return SYSLINK_Paint (infoPtr, (HDC)wParam);
1475 DWORD mp = GetMessagePos();
1477 ht.pt.x = (short)LOWORD(mp);
1478 ht.pt.y = (short)HIWORD(mp);
1480 ScreenToClient(infoPtr->Self, &ht.pt);
1481 if(SYSLINK_HitTest (infoPtr, &ht))
1483 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_HAND));
1486 /* let the default window proc handle this message */
1487 return DefWindowProcW(hwnd, message, wParam, lParam);
1493 HDC hdc = GetDC(infoPtr->Self);
1496 SYSLINK_Render(infoPtr, hdc);
1497 ReleaseDC(infoPtr->Self, hdc);
1503 return (LRESULT)infoPtr->Font;
1506 return (LRESULT)SYSLINK_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1509 SYSLINK_SetText(infoPtr, (LPWSTR)lParam);
1510 return DefWindowProcW(hwnd, message, wParam, lParam);
1512 case WM_LBUTTONDOWN:
1515 pt.x = LOWORD(lParam);
1516 pt.y = HIWORD(lParam);
1517 return SYSLINK_LButtonDown(infoPtr, wParam, &pt);
1522 pt.x = LOWORD(lParam);
1523 pt.y = HIWORD(lParam);
1524 return SYSLINK_LButtonUp(infoPtr, wParam, &pt);
1532 SYSLINK_OnEnter(infoPtr);
1536 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1537 SYSKEY_SelectNextPrevLink(infoPtr, shift);
1541 return DefWindowProcW(hwnd, message, wParam, lParam);
1546 LRESULT Ret = DLGC_HASSETSEL;
1547 int vk = (lParam != 0 ? (int)((LPMSG)lParam)->wParam : 0);
1551 Ret |= DLGC_WANTMESSAGE;
1555 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1556 if(!SYSLINK_NoNextLink(infoPtr, shift))
1558 Ret |= DLGC_WANTTAB;
1562 Ret |= DLGC_WANTCHARS;
1574 pt.x = LOWORD(lParam);
1575 pt.y = HIWORD(lParam);
1577 GetClientRect(infoPtr->Self, &rc);
1578 ScreenToClient(infoPtr->Self, &pt);
1579 if(pt.x < 0 || pt.y < 0 || pt.x > rc.right || pt.y > rc.bottom)
1584 if(SYSLINK_LinkAtPt(infoPtr, &pt, NULL, FALSE))
1589 return HTTRANSPARENT;
1593 return SYSLINK_HitTest(infoPtr, (PLHITTESTINFO)lParam);
1596 return SYSLINK_SetItem(infoPtr, (PLITEM)lParam);
1599 return SYSLINK_GetItem(infoPtr, (PLITEM)lParam);
1601 case LM_GETIDEALHEIGHT:
1602 return SYSLINK_GetIdealHeight(infoPtr);
1605 return SYSLINK_SetFocus(infoPtr, (HWND)wParam);
1608 return SYSLINK_KillFocus(infoPtr, (HWND)wParam);
1611 /* allocate memory for info struct */
1612 infoPtr = (SYSLINK_INFO *)SYSLINK_Alloc (sizeof(SYSLINK_INFO));
1613 if (!infoPtr) return -1;
1614 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1616 /* initialize the info struct */
1617 infoPtr->Self = hwnd;
1619 infoPtr->LinkFont = 0;
1620 infoPtr->Items = NULL;
1621 infoPtr->HasFocus = FALSE;
1622 infoPtr->MouseDownID = -1;
1623 infoPtr->TextColor = GetSysColor(COLOR_WINDOWTEXT);
1624 infoPtr->LinkColor = GetSysColor(COLOR_HIGHLIGHT);
1625 infoPtr->VisitedColor = GetSysColor(COLOR_HIGHLIGHT);
1626 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd);
1627 lParam = (LPARAM)(((LPCREATESTRUCTW)lParam)->lpszName);
1628 SYSLINK_SetText(infoPtr, (LPWSTR)lParam);
1632 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd);
1633 SYSLINK_ClearDoc(infoPtr);
1634 if(infoPtr->Font != 0) DeleteObject(infoPtr->Font);
1635 if(infoPtr->LinkFont != 0) DeleteObject(infoPtr->LinkFont);
1636 SYSLINK_Free (infoPtr);
1637 SetWindowLongPtrW(hwnd, 0, 0);
1641 if ((message >= WM_USER) && (message < WM_APP))
1642 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam );
1643 return DefWindowProcW(hwnd, message, wParam, lParam);
1648 /***********************************************************************
1649 * SYSLINK_Register [Internal]
1651 * Registers the SysLink window class.
1653 VOID SYSLINK_Register (void)
1657 ZeroMemory (&wndClass, sizeof(wndClass));
1658 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1659 wndClass.lpfnWndProc = SysLinkWindowProc;
1660 wndClass.cbClsExtra = 0;
1661 wndClass.cbWndExtra = sizeof (SYSLINK_INFO *);
1662 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1663 wndClass.lpszClassName = WC_LINK;
1665 RegisterClassW (&wndClass);
1669 /***********************************************************************
1670 * SYSLINK_Unregister [Internal]
1672 * Unregisters the SysLink window class.
1674 VOID SYSLINK_Unregister (void)
1676 UnregisterClassW (WC_LINK, NULL);