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
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 if(DocItem->u.Link.szID != NULL)
120 SYSLINK_Free(DocItem->u.Link.szID);
122 if(DocItem->u.Link.szUrl != NULL)
124 SYSLINK_Free(DocItem->u.Link.szUrl);
128 if(DocItem->Type == slLink && DocItem->u.Link.hRgn != NULL)
130 DeleteObject(DocItem->u.Link.hRgn);
133 /* we don't free Text because it's just a pointer to a character in the
134 entire window text string */
136 SYSLINK_Free(DocItem);
139 /***********************************************************************
140 * SYSLINK_AppendDocItem
141 * Create and append a new document item.
143 static PDOC_ITEM SYSLINK_AppendDocItem (SYSLINK_INFO *infoPtr, LPWSTR Text, UINT textlen,
144 SL_ITEM_TYPE type, PDOC_ITEM LastItem)
147 Item = SYSLINK_Alloc(sizeof(DOC_ITEM) + ((textlen + 1) * sizeof(WCHAR)));
150 ERR("Failed to alloc DOC_ITEM structure!\n");
153 textlen = min(textlen, lstrlenW(Text));
156 Item->Text = (LPWSTR)(Item + 1);
157 Item->nText = textlen;
163 LastItem->Next = Item;
167 infoPtr->Items = Item;
170 lstrcpynW(Item->Text, Text, textlen + 1);
171 Item->Text[textlen] = 0;
176 /***********************************************************************
178 * Clears the document tree
180 static VOID SYSLINK_ClearDoc (SYSLINK_INFO *infoPtr)
182 PDOC_ITEM Item, Next;
184 Item = infoPtr->Items;
188 SYSLINK_FreeDocItem(Item);
192 infoPtr->Items = NULL;
195 /***********************************************************************
197 * Parses the window text string and creates a document. Returns the
198 * number of document items created.
200 static UINT SYSLINK_ParseText (SYSLINK_INFO *infoPtr, LPWSTR Text)
202 WCHAR *current, *textstart, *linktext, *firsttag;
203 int taglen = 0, textlen, linklen, docitems = 0;
204 PDOC_ITEM Last = NULL;
205 SL_ITEM_TYPE CurrentType = slText;
210 Style = GetWindowLongW(infoPtr->Self, GWL_STYLE);
218 for(current = (WCHAR*)Text; *current != 0;)
222 if(!StrCmpNIW(current, SL_LINKOPEN, 2) && (CurrentType == slText))
224 BOOL ValidParam = FALSE, ValidLink = FALSE;
226 switch (*(current + 2))
229 /* we just have to deal with a <a> tag */
240 /* we expect parameters, parse them */
241 LPWSTR *CurrentParameter = NULL;
242 UINT *CurrentParameterLen = NULL;
246 tmp = current + taglen;
251 /* compare the current position with all known parameters */
252 if(!StrCmpNIW(tmp, SL_HREF, 6))
256 CurrentParameter = &lpUrl;
257 CurrentParameterLen = &lenUrl;
259 else if(!StrCmpNIW(tmp, SL_ID, 4))
263 CurrentParameter = &lpID;
264 CurrentParameterLen = &lenId;
273 /* we got a known parameter, now search until the next " character.
274 If we can't find a " character, there's a syntax error and we just assume it's text */
276 *CurrentParameter = current + taglen;
277 *CurrentParameterLen = 0;
279 for(tmp = *CurrentParameter; *tmp != 0; tmp++)
288 (*CurrentParameterLen)++;
293 /* we're done with this parameter, now there are only 2 possibilities:
294 * 1. another parameter is coming, so expect a ' ' (space) character
295 * 2. the tag is being closed, so expect a '<' character
300 /* we expect another parameter, do the whole thing again */
306 /* the tag is being closed, we're done */
320 if(ValidLink && ValidParam)
322 /* the <a ...> tag appears to be valid. save all information
323 so we can add the link if we find a valid </a> tag later */
324 CurrentType = slLink;
325 linktext = current + taglen;
334 if(textstart == NULL)
340 else if(!StrCmpNIW(current, SL_LINKCLOSE, 4) && (CurrentType == slLink) && firsttag)
342 /* there's a <a...> tag opened, first add the previous text, if present */
343 if(textstart != NULL && textlen > 0 && firsttag > textstart)
345 Last = SYSLINK_AppendDocItem(infoPtr, textstart, firsttag - textstart, slText, Last);
348 ERR("Unable to create new document item!\n");
356 /* now it's time to add the link to the document */
358 if(linktext != NULL && linklen > 0)
360 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slLink, Last);
363 ERR("Unable to create new document item!\n");
367 if(CurrentType == slLink)
371 if(!(Style & WS_DISABLED))
373 Last->u.Link.state |= LIS_ENABLED;
375 /* Copy the tag parameters */
378 nc = min(lenId, lstrlenW(lpID));
379 nc = min(nc, MAX_LINKID_TEXT);
380 Last->u.Link.szID = SYSLINK_Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
381 if(Last->u.Link.szID != NULL)
383 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
384 Last->u.Link.szID[nc] = 0;
388 Last->u.Link.szID = NULL;
391 nc = min(lenUrl, lstrlenW(lpUrl));
392 nc = min(nc, L_MAX_URL_LENGTH);
393 Last->u.Link.szUrl = SYSLINK_Alloc((L_MAX_URL_LENGTH + 1) * sizeof(WCHAR));
394 if(Last->u.Link.szUrl != NULL)
396 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
397 Last->u.Link.szUrl[nc] = 0;
401 Last->u.Link.szUrl = NULL;
405 CurrentType = slText;
412 /* we don't know what tag it is, so just continue */
415 if(CurrentType == slText && textstart == NULL)
429 /* save the pointer of the current text item if we couldn't find a tag */
430 if(textstart == NULL && CurrentType == slText)
439 if(textstart != NULL && textlen > 0)
441 Last = SYSLINK_AppendDocItem(infoPtr, textstart, textlen, CurrentType, Last);
444 ERR("Unable to create new document item!\n");
447 if(CurrentType == slLink)
451 if(!(Style & WS_DISABLED))
453 Last->u.Link.state |= LIS_ENABLED;
455 /* Copy the tag parameters */
458 nc = min(lenId, lstrlenW(lpID));
459 nc = min(nc, MAX_LINKID_TEXT);
460 Last->u.Link.szID = SYSLINK_Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
461 if(Last->u.Link.szID != NULL)
463 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
464 Last->u.Link.szID[nc] = 0;
468 Last->u.Link.szID = NULL;
471 nc = min(lenUrl, lstrlenW(lpUrl));
472 nc = min(nc, L_MAX_URL_LENGTH);
473 Last->u.Link.szUrl = SYSLINK_Alloc((L_MAX_URL_LENGTH + 1) * sizeof(WCHAR));
474 if(Last->u.Link.szUrl != NULL)
476 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
477 Last->u.Link.szUrl[nc] = 0;
481 Last->u.Link.szUrl = NULL;
486 if(linktext != NULL && linklen > 0)
488 /* we got a unclosed link, just display the text */
489 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slText, Last);
492 ERR("Unable to create new document item!\n");
501 /***********************************************************************
502 * SYSLINK_RepaintLink
505 static VOID SYSLINK_RepaintLink (SYSLINK_INFO *infoPtr, PDOC_ITEM DocItem)
507 if(DocItem->Type != slLink)
509 ERR("DocItem not a link!\n");
513 if(DocItem->u.Link.hRgn != NULL)
515 /* repaint the region */
516 RedrawWindow(infoPtr->Self, NULL, DocItem->u.Link.hRgn, RDW_INVALIDATE | RDW_UPDATENOW);
520 /***********************************************************************
521 * SYSLINK_GetLinkItemByIndex
522 * Retreives a document link by it's index
524 static PDOC_ITEM SYSLINK_GetLinkItemByIndex (SYSLINK_INFO *infoPtr, int iLink)
526 PDOC_ITEM Current = infoPtr->Items;
528 while(Current != NULL)
530 if((Current->Type == slLink) && (iLink-- <= 0))
534 Current = Current->Next;
539 /***********************************************************************
540 * SYSLINK_GetFocusLink
541 * Retreives the link that has the LIS_FOCUSED bit
543 static PDOC_ITEM SYSLINK_GetFocusLink (SYSLINK_INFO *infoPtr, int *LinkId)
545 PDOC_ITEM Current = infoPtr->Items;
548 while(Current != NULL)
550 if((Current->Type == slLink))
552 if(Current->u.Link.state & LIS_FOCUSED)
560 Current = Current->Next;
565 /***********************************************************************
566 * SYSLINK_GetNextLink
569 static PDOC_ITEM SYSLINK_GetNextLink (SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
571 for(Current = (Current != NULL ? Current->Next : infoPtr->Items);
573 Current = Current->Next)
575 if(Current->Type == slLink)
583 /***********************************************************************
584 * SYSLINK_GetPrevLink
585 * Gets the previous link
587 static PDOC_ITEM SYSLINK_GetPrevLink (SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
591 /* returns the last link */
592 PDOC_ITEM Last = NULL;
594 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
596 if(Current->Type == slLink)
605 /* returns the previous link */
606 PDOC_ITEM Cur, Prev = NULL;
608 for(Cur = infoPtr->Items; Cur != NULL; Cur = Cur->Next)
614 if(Cur->Type == slLink)
623 /***********************************************************************
625 * Tries to wrap a line.
627 static BOOL SYSLINK_WrapLine (HDC hdc, LPWSTR Text, WCHAR BreakChar, int *LineLen, int nFit, LPSIZE Extent, int Width)
638 Current = Text + nFit;
640 /* check if we're in the middle of a word */
641 if((*Current) != BreakChar)
643 /* search for the beginning of the word */
644 while(Current > Text && (*(Current - 1)) != BreakChar)
661 /***********************************************************************
663 * Renders the document in memory
665 static VOID SYSLINK_Render (SYSLINK_INFO *infoPtr, HDC hdc)
670 int x, y, LineHeight;
673 GetClientRect(infoPtr->Self, &rc);
674 rc.right -= SL_RIGHTMARGIN;
675 rc.bottom -= SL_BOTTOMMARGIN;
677 if(rc.right - SL_LEFTMARGIN < 0 || rc.bottom - SL_TOPMARGIN < 0) return;
679 hOldFont = SelectObject(hdc, infoPtr->Font);
680 GetTextMetricsW(hdc, &tm);
686 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
690 PDOC_TEXTBLOCK bl, cbl;
694 if(Current->nText == 0)
696 ERR("DOC_ITEM with no text?!\n");
702 bl = Current->Blocks;
705 if(Current->Type == slText)
707 SelectObject(hdc, infoPtr->Font);
709 else if(Current->Type == slLink)
711 SelectObject(hdc, infoPtr->LinkFont);
716 if(GetTextExtentExPointW(hdc, tx, n, rc.right - x, &nFit, NULL, &szDim))
719 BOOL Wrap = SYSLINK_WrapLine(hdc, tx, tm.tmBreakChar, &LineLen, nFit, &szDim, rc.right - x);
723 if(x > SL_LEFTMARGIN)
725 /* move one line down, the word didn't fit into the line */
733 /* the word starts at the beginning of the line and doesn't
734 fit into the line, so break it at the last character that fits */
735 LineLen = max(nFit, 1);
741 GetTextExtentExPointW(hdc, tx, LineLen, rc.right - x, NULL, NULL, &szDim);
746 bl = SYSLINK_ReAlloc(bl, ++nBlocks * sizeof(DOC_TEXTBLOCK));
750 bl = SYSLINK_Alloc(++nBlocks * sizeof(DOC_TEXTBLOCK));
755 cbl = bl + nBlocks - 1;
757 cbl->nChars = LineLen;
760 cbl->rc.right = x + szDim.cx;
761 cbl->rc.bottom = y + szDim.cy;
764 LineHeight = max(LineHeight, szDim.cy);
766 /* (re)calculate the link's region */
767 if(Current->Type == slLink)
771 if(Current->u.Link.hRgn != NULL)
773 DeleteObject(Current->u.Link.hRgn);
775 /* initialize the link's hRgn */
776 Current->u.Link.hRgn = CreateRectRgnIndirect(&cbl->rc);
778 else if(Current->u.Link.hRgn != NULL)
781 hrgn = CreateRectRgnIndirect(&cbl->rc);
782 /* add the rectangle */
783 CombineRgn(Current->u.Link.hRgn, Current->u.Link.hRgn, hrgn, RGN_OR);
797 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
805 ERR("GetTextExtentExPoint() failed?!\n");
809 Current->Blocks = bl;
812 SelectObject(hdc, hOldFont);
815 /***********************************************************************
817 * Draws the SysLink control.
819 static LRESULT SYSLINK_Draw (SYSLINK_INFO *infoPtr, HDC hdc)
824 COLORREF OldTextColor, OldBkColor;
826 hOldFont = SelectObject(hdc, infoPtr->Font);
827 OldTextColor = SetTextColor(hdc, infoPtr->TextColor);
828 OldBkColor = SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
830 GetClientRect(infoPtr->Self, &rc);
831 rc.right -= SL_RIGHTMARGIN + SL_LEFTMARGIN;
832 rc.bottom -= SL_BOTTOMMARGIN + SL_TOPMARGIN;
834 if(rc.right < 0 || rc.bottom < 0) return 0;
836 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
842 bl = Current->Blocks;
848 if(Current->Type == slText)
850 SelectObject(hdc, infoPtr->Font);
851 SetTextColor(hdc, infoPtr->TextColor);
855 SelectObject(hdc, infoPtr->LinkFont);
856 SetTextColor(hdc, (!(Current->u.Link.state & LIS_VISITED) ? infoPtr->LinkColor : infoPtr->VisitedColor));
861 ExtTextOutW(hdc, bl->rc.left, bl->rc.top, ETO_OPAQUE | ETO_CLIPPED, &bl->rc, tx, bl->nChars, NULL);
862 if((Current->Type == slLink) && (Current->u.Link.state & LIS_FOCUSED) && infoPtr->HasFocus)
865 PrevColor = SetBkColor(hdc, OldBkColor);
866 DrawFocusRect(hdc, &bl->rc);
867 SetBkColor(hdc, PrevColor);
876 SetBkColor(hdc, OldBkColor);
877 SetTextColor(hdc, OldTextColor);
878 SelectObject(hdc, hOldFont);
884 /***********************************************************************
886 * Handles the WM_PAINT message.
888 static LRESULT SYSLINK_Paint (SYSLINK_INFO *infoPtr)
892 hdc = BeginPaint (infoPtr->Self, &ps);
893 SYSLINK_Draw (infoPtr, hdc);
894 EndPaint (infoPtr->Self, &ps);
899 /***********************************************************************
901 * Set new Font for the SysLink control.
903 static HFONT SYSLINK_SetFont (SYSLINK_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
907 HFONT hOldFont = infoPtr->Font;
908 infoPtr->Font = hFont;
910 /* free the underline font */
911 if(infoPtr->LinkFont != NULL)
913 DeleteObject(infoPtr->LinkFont);
914 infoPtr->LinkFont = NULL;
917 /* Render text position and word wrapping in memory */
918 hdc = GetDC(infoPtr->Self);
921 /* create a new underline font */
922 if(GetObjectW(infoPtr->Font, sizeof(LOGFONTW), &lf))
924 lf.lfUnderline = TRUE;
925 infoPtr->LinkFont = CreateFontIndirectW(&lf);
929 ERR("Failed to create link font!\n");
932 SYSLINK_Render(infoPtr, hdc);
933 ReleaseDC(infoPtr->Self, hdc);
938 RedrawWindow(infoPtr->Self, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
944 /***********************************************************************
946 * Set new text for the SysLink control.
948 static LRESULT SYSLINK_SetText (SYSLINK_INFO *infoPtr, LPWSTR Text)
952 /* clear the document */
953 SYSLINK_ClearDoc(infoPtr);
955 textlen = lstrlenW(Text);
956 if(Text == NULL || textlen == 0)
961 /* let's parse the string and create a document */
962 if(SYSLINK_ParseText(infoPtr, Text) > 0)
964 /* Render text position and word wrapping in memory */
965 HDC hdc = GetDC(infoPtr->Self);
966 SYSLINK_Render(infoPtr, hdc);
967 SYSLINK_Draw(infoPtr, hdc);
968 ReleaseDC(infoPtr->Self, hdc);
974 /***********************************************************************
975 * SYSLINK_SetFocusLink
976 * Updates the focus status bits and focusses the specified link.
977 * If no document item is specified, the focus bit will be removed from all links.
978 * Returns the previous focused item.
980 static PDOC_ITEM SYSLINK_SetFocusLink (SYSLINK_INFO *infoPtr, PDOC_ITEM DocItem)
982 PDOC_ITEM Current, PrevFocus = NULL;
984 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
986 if(Current->Type == slLink)
988 if((PrevFocus == NULL) && (Current->u.Link.state & LIS_FOCUSED))
993 if(Current == DocItem)
995 Current->u.Link.state |= LIS_FOCUSED;
999 Current->u.Link.state &= ~LIS_FOCUSED;
1007 /***********************************************************************
1009 * Sets the states and attributes of a link item.
1011 static LRESULT SYSLINK_SetItem (SYSLINK_INFO *infoPtr, PLITEM Item)
1014 BOOL Repaint = FALSE;
1017 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1019 ERR("Invalid Flags!\n");
1023 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1026 ERR("Link %d couldn't be found\n", Item->iLink);
1030 if(Item->mask & LIF_STATE)
1032 UINT oldstate = di->u.Link.state;
1033 /* clear the masked bits */
1034 di->u.Link.state &= ~(Item->stateMask & LIS_MASK);
1036 di->u.Link.state |= (Item->state & Item->stateMask) & LIS_MASK;
1037 Repaint = (oldstate != di->u.Link.state);
1039 /* update the focus */
1040 SYSLINK_SetFocusLink(infoPtr, ((di->u.Link.state & LIS_FOCUSED) ? di : NULL));
1043 if(Item->mask & LIF_ITEMID)
1045 if(!di->u.Link.szID)
1047 di->u.Link.szID = SYSLINK_Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
1050 ERR("Unable to allocate memory for link id\n");
1056 lstrcpynW(di->u.Link.szID, Item->szID, MAX_LINKID_TEXT + 1);
1060 if(Item->mask & LIF_URL)
1062 if(!di->u.Link.szUrl)
1064 di->u.Link.szUrl = SYSLINK_Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
1067 ERR("Unable to allocate memory for link url\n");
1071 if(di->u.Link.szUrl)
1073 lstrcpynW(di->u.Link.szUrl, Item->szUrl, MAX_LINKID_TEXT + 1);
1079 SYSLINK_RepaintLink(infoPtr, di);
1085 /***********************************************************************
1087 * Retrieves the states and attributes of a link item.
1089 static LRESULT SYSLINK_GetItem (SYSLINK_INFO *infoPtr, PLITEM Item)
1093 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1095 ERR("Invalid Flags!\n");
1099 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1102 ERR("Link %d couldn't be found\n", Item->iLink);
1106 if(Item->mask & LIF_STATE)
1108 Item->state = (di->u.Link.state & Item->stateMask);
1109 if(!infoPtr->HasFocus)
1111 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1112 Item->state &= ~LIS_FOCUSED;
1116 if(Item->mask & LIF_ITEMID)
1120 lstrcpynW(Item->szID, di->u.Link.szID, MAX_LINKID_TEXT + 1);
1128 if(Item->mask & LIF_URL)
1130 if(di->u.Link.szUrl)
1132 lstrcpynW(Item->szUrl, di->u.Link.szUrl, L_MAX_URL_LENGTH + 1);
1143 /***********************************************************************
1145 * Determines the link the user clicked on.
1147 static LRESULT SYSLINK_HitTest (SYSLINK_INFO *infoPtr, PLHITTESTINFO HitTest)
1152 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1154 if(Current->Type == slLink)
1156 if((Current->u.Link.hRgn != NULL) &&
1157 PtInRegion(Current->u.Link.hRgn, HitTest->pt.x, HitTest->pt.y))
1159 HitTest->item.mask = 0;
1160 HitTest->item.iLink = id;
1161 HitTest->item.state = 0;
1162 HitTest->item.stateMask = 0;
1163 if(Current->u.Link.szID)
1165 lstrcpynW(HitTest->item.szID, Current->u.Link.szID, MAX_LINKID_TEXT + 1);
1169 HitTest->item.szID[0] = 0;
1171 if(Current->u.Link.szUrl)
1173 lstrcpynW(HitTest->item.szUrl, Current->u.Link.szUrl, L_MAX_URL_LENGTH + 1);
1177 HitTest->item.szUrl[0] = 0;
1188 /***********************************************************************
1189 * SYSLINK_GetIdealHeight
1190 * Returns the preferred height of a link at the current control's width.
1192 static LRESULT SYSLINK_GetIdealHeight (SYSLINK_INFO *infoPtr)
1194 HDC hdc = GetDC(infoPtr->Self);
1199 HGDIOBJ hOldFont = SelectObject(hdc, infoPtr->Font);
1201 if(GetTextMetricsW(hdc, &tm))
1203 height = tm.tmHeight;
1209 SelectObject(hdc, hOldFont);
1210 ReleaseDC(infoPtr->Self, hdc);
1217 /***********************************************************************
1218 * SYSLINK_SendParentNotify
1219 * Sends a WM_NOTIFY message to the parent window.
1221 static LRESULT SYSLINK_SendParentNotify (SYSLINK_INFO *infoPtr, UINT code, PDOC_ITEM Link, int iLink)
1225 nml.hdr.hwndFrom = infoPtr->Self;
1226 nml.hdr.idFrom = GetWindowLongW(infoPtr->Self, GWL_ID);
1227 nml.hdr.code = code;
1230 nml.item.iLink = iLink;
1232 nml.item.stateMask = 0;
1233 if(Link->u.Link.szID)
1235 lstrcpynW(nml.item.szID, Link->u.Link.szID, MAX_LINKID_TEXT + 1);
1239 nml.item.szID[0] = 0;
1241 if(Link->u.Link.szUrl)
1243 lstrcpynW(nml.item.szUrl, Link->u.Link.szUrl, L_MAX_URL_LENGTH + 1);
1247 nml.item.szUrl[0] = 0;
1250 return SendMessageW(GetParent(infoPtr->Self), WM_NOTIFY, (WPARAM)nml.hdr.idFrom, (LPARAM)&nml);
1253 /***********************************************************************
1255 * Handles receiving the input focus.
1257 static LRESULT SYSLINK_SetFocus (SYSLINK_INFO *infoPtr, HWND PrevFocusWindow)
1261 infoPtr->HasFocus = TRUE;
1264 /* FIXME - How to detect whether SHIFT+TAB or just TAB has been pressed?
1265 * The problem is we could get this message without keyboard input, too
1267 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1269 if(Focus == NULL && (Focus = SYSLINK_GetNextLink(infoPtr, NULL)))
1271 SYSLINK_SetFocusLink(infoPtr, Focus);
1274 /* This is a temporary hack since I'm not really sure how to detect which link to select.
1275 See message above! */
1276 Focus = SYSLINK_GetNextLink(infoPtr, NULL);
1279 SYSLINK_SetFocusLink(infoPtr, Focus);
1283 SYSLINK_RepaintLink(infoPtr, Focus);
1288 /***********************************************************************
1290 * Handles losing the input focus.
1292 static LRESULT SYSLINK_KillFocus (SYSLINK_INFO *infoPtr, HWND NewFocusWindow)
1296 infoPtr->HasFocus = FALSE;
1297 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1301 SYSLINK_RepaintLink(infoPtr, Focus);
1307 /***********************************************************************
1309 * Returns a link at the specified position
1311 static PDOC_ITEM SYSLINK_LinkAtPt (SYSLINK_INFO *infoPtr, POINT *pt, int *LinkId, BOOL MustBeEnabled)
1316 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1318 if((Current->Type == slLink) && (Current->u.Link.hRgn != NULL) &&
1319 PtInRegion(Current->u.Link.hRgn, pt->x, pt->y) &&
1320 (!MustBeEnabled || (MustBeEnabled && (Current->u.Link.state & LIS_ENABLED))))
1334 /***********************************************************************
1335 * SYSLINK_LButtonDown
1336 * Handles mouse clicks
1338 static LRESULT SYSLINK_LButtonDown (SYSLINK_INFO *infoPtr, DWORD Buttons, POINT *pt)
1340 PDOC_ITEM Current, Old;
1343 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1346 Old = SYSLINK_SetFocusLink(infoPtr, Current);
1347 if(Old != NULL && Old != Current)
1349 SYSLINK_RepaintLink(infoPtr, Old);
1351 infoPtr->MouseDownID = id;
1352 SYSLINK_RepaintLink(infoPtr, Current);
1353 SetFocus(infoPtr->Self);
1359 /***********************************************************************
1361 * Handles mouse clicks
1363 static LRESULT SYSLINK_LButtonUp (SYSLINK_INFO *infoPtr, DWORD Buttons, POINT *pt)
1365 if(infoPtr->MouseDownID > -1)
1370 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1371 if((Current != NULL) && (Current->u.Link.state & LIS_FOCUSED) && (infoPtr->MouseDownID == id))
1373 SYSLINK_SendParentNotify(infoPtr, NM_CLICK, Current, id);
1377 infoPtr->MouseDownID = -1;
1382 /***********************************************************************
1384 * Handles ENTER key events
1386 static BOOL SYSLINK_OnEnter (SYSLINK_INFO *infoPtr)
1388 if(infoPtr->HasFocus)
1393 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1396 SYSLINK_SendParentNotify(infoPtr, NM_RETURN, Focus, id);
1403 /***********************************************************************
1404 * SYSKEY_SelectNextPrevLink
1405 * Changes the currently focused link
1407 static BOOL SYSKEY_SelectNextPrevLink (SYSLINK_INFO *infoPtr, BOOL Prev)
1409 if(infoPtr->HasFocus)
1414 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1417 PDOC_ITEM NewFocus, OldFocus;
1420 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1422 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1424 if(NewFocus != NULL)
1426 OldFocus = SYSLINK_SetFocusLink(infoPtr, NewFocus);
1428 if(OldFocus != NewFocus)
1430 SYSLINK_RepaintLink(infoPtr, OldFocus);
1432 SYSLINK_RepaintLink(infoPtr, NewFocus);
1440 /***********************************************************************
1441 * SYSKEY_SelectNextPrevLink
1442 * Determines if there's a next or previous link to decide whether the control
1443 * should capture the tab key message
1445 static BOOL SYSLINK_NoNextLink (SYSLINK_INFO *infoPtr, BOOL Prev)
1447 PDOC_ITEM Focus, NewFocus;
1449 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1451 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1453 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1455 return NewFocus == NULL;
1458 /***********************************************************************
1461 static LRESULT WINAPI SysLinkWindowProc(HWND hwnd, UINT message,
1462 WPARAM wParam, LPARAM lParam)
1464 SYSLINK_INFO *infoPtr;
1466 TRACE("hwnd=%p msg=%04x wparam=%x lParam=%lx\n", hwnd, message, wParam, lParam);
1468 infoPtr = (SYSLINK_INFO *)GetWindowLongW(hwnd, 0);
1470 if (!infoPtr && message != WM_CREATE)
1471 return DefWindowProcW( hwnd, message, wParam, lParam );
1475 return SYSLINK_Paint (infoPtr);
1481 DWORD mp = GetMessagePos();
1483 pt = MAKEPOINTS(mp);
1487 ScreenToClient(infoPtr->Self, &ht.pt);
1488 if(SYSLINK_HitTest (infoPtr, &ht))
1490 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_HAND));
1493 /* let the default window proc handle this message */
1494 return DefWindowProcW(hwnd, message, wParam, lParam);
1500 HDC hdc = GetDC(infoPtr->Self);
1503 SYSLINK_Render(infoPtr, hdc);
1504 ReleaseDC(infoPtr->Self, hdc);
1510 return (LRESULT)infoPtr->Font;
1513 return (LRESULT)SYSLINK_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1516 SYSLINK_SetText(infoPtr, (LPWSTR)lParam);
1517 return DefWindowProcW(hwnd, message, wParam, lParam);
1519 case WM_LBUTTONDOWN:
1522 pt.x = LOWORD(lParam);
1523 pt.y = HIWORD(lParam);
1524 return SYSLINK_LButtonDown(infoPtr, wParam, &pt);
1529 pt.x = LOWORD(lParam);
1530 pt.y = HIWORD(lParam);
1531 return SYSLINK_LButtonUp(infoPtr, wParam, &pt);
1539 SYSLINK_OnEnter(infoPtr);
1543 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1544 SYSKEY_SelectNextPrevLink(infoPtr, shift);
1548 return DefWindowProcW(hwnd, message, wParam, lParam);
1553 LRESULT Ret = DLGC_HASSETSEL;
1554 int vk = (lParam != 0 ? (int)((LPMSG)lParam)->wParam : 0);
1558 Ret |= DLGC_WANTMESSAGE;
1562 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1563 if(!SYSLINK_NoNextLink(infoPtr, shift))
1565 Ret |= DLGC_WANTTAB;
1569 Ret |= DLGC_WANTCHARS;
1581 pt.x = LOWORD(lParam);
1582 pt.y = HIWORD(lParam);
1584 GetClientRect(infoPtr->Self, &rc);
1585 ScreenToClient(infoPtr->Self, &pt);
1586 if(pt.x < 0 || pt.y < 0 || pt.x > rc.right || pt.y > rc.bottom)
1591 if(SYSLINK_LinkAtPt(infoPtr, &pt, NULL, FALSE))
1596 return HTTRANSPARENT;
1600 return SYSLINK_HitTest(infoPtr, (PLHITTESTINFO)lParam);
1603 return SYSLINK_SetItem(infoPtr, (PLITEM)lParam);
1606 return SYSLINK_GetItem(infoPtr, (PLITEM)lParam);
1608 case LM_GETIDEALHEIGHT:
1609 return SYSLINK_GetIdealHeight(infoPtr);
1612 return SYSLINK_SetFocus(infoPtr, (HWND)wParam);
1615 return SYSLINK_KillFocus(infoPtr, (HWND)wParam);
1618 /* allocate memory for info struct */
1619 infoPtr = (SYSLINK_INFO *)SYSLINK_Alloc (sizeof(SYSLINK_INFO));
1620 if (!infoPtr) return -1;
1621 SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
1623 /* initialize the info struct */
1624 infoPtr->Self = hwnd;
1626 infoPtr->LinkFont = 0;
1627 infoPtr->Items = NULL;
1628 infoPtr->HasFocus = FALSE;
1629 infoPtr->MouseDownID = -1;
1630 infoPtr->TextColor = GetSysColor(COLOR_WINDOWTEXT);
1631 infoPtr->LinkColor = GetSysColor(COLOR_HIGHLIGHT);
1632 infoPtr->VisitedColor = GetSysColor(COLOR_HIGHLIGHT);
1633 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd);
1634 lParam = (LPARAM)(((LPCREATESTRUCTW)lParam)->lpszName);
1635 SYSLINK_SetText(infoPtr, (LPWSTR)lParam);
1639 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd);
1640 SYSLINK_ClearDoc(infoPtr);
1641 SYSLINK_Free (infoPtr);
1642 SetWindowLongW(hwnd, 0, 0);
1646 if ((message >= WM_USER) && (message < WM_APP))
1647 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam );
1648 return DefWindowProcW(hwnd, message, wParam, lParam);
1653 /***********************************************************************
1654 * SYSLINK_Register [Internal]
1656 * Registers the SysLink window class.
1658 VOID SYSLINK_Register (void)
1662 ZeroMemory (&wndClass, sizeof(wndClass));
1663 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1664 wndClass.lpfnWndProc = (WNDPROC)SysLinkWindowProc;
1665 wndClass.cbClsExtra = 0;
1666 wndClass.cbWndExtra = sizeof (SYSLINK_INFO *);
1667 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1668 wndClass.lpszClassName = WC_LINK;
1670 RegisterClassW (&wndClass);
1674 /***********************************************************************
1675 * SYSLINK_Unregister [Internal]
1677 * Unregisters the SysLink window class.
1679 VOID SYSLINK_Unregister (void)
1681 UnregisterClassW (WC_LINK, NULL);