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 * Retreives a document link by it's 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 * Retreives 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)
886 hdc = BeginPaint (infoPtr->Self, &ps);
887 SYSLINK_Draw (infoPtr, hdc);
888 EndPaint (infoPtr->Self, &ps);
893 /***********************************************************************
895 * Set new Font for the SysLink control.
897 static HFONT SYSLINK_SetFont (SYSLINK_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
901 HFONT hOldFont = infoPtr->Font;
902 infoPtr->Font = hFont;
904 /* free the underline font */
905 if(infoPtr->LinkFont != NULL)
907 DeleteObject(infoPtr->LinkFont);
908 infoPtr->LinkFont = NULL;
911 /* Render text position and word wrapping in memory */
912 hdc = GetDC(infoPtr->Self);
915 /* create a new underline font */
916 if(GetObjectW(infoPtr->Font, sizeof(LOGFONTW), &lf))
918 lf.lfUnderline = TRUE;
919 infoPtr->LinkFont = CreateFontIndirectW(&lf);
923 ERR("Failed to create link font!\n");
926 SYSLINK_Render(infoPtr, hdc);
927 ReleaseDC(infoPtr->Self, hdc);
932 RedrawWindow(infoPtr->Self, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
938 /***********************************************************************
940 * Set new text for the SysLink control.
942 static LRESULT SYSLINK_SetText (SYSLINK_INFO *infoPtr, LPWSTR Text)
946 /* clear the document */
947 SYSLINK_ClearDoc(infoPtr);
949 textlen = lstrlenW(Text);
950 if(Text == NULL || textlen == 0)
955 /* let's parse the string and create a document */
956 if(SYSLINK_ParseText(infoPtr, Text) > 0)
958 /* Render text position and word wrapping in memory */
959 HDC hdc = GetDC(infoPtr->Self);
960 SYSLINK_Render(infoPtr, hdc);
961 SYSLINK_Draw(infoPtr, hdc);
962 ReleaseDC(infoPtr->Self, hdc);
968 /***********************************************************************
969 * SYSLINK_SetFocusLink
970 * Updates the focus status bits and focusses the specified link.
971 * If no document item is specified, the focus bit will be removed from all links.
972 * Returns the previous focused item.
974 static PDOC_ITEM SYSLINK_SetFocusLink (SYSLINK_INFO *infoPtr, PDOC_ITEM DocItem)
976 PDOC_ITEM Current, PrevFocus = NULL;
978 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
980 if(Current->Type == slLink)
982 if((PrevFocus == NULL) && (Current->u.Link.state & LIS_FOCUSED))
987 if(Current == DocItem)
989 Current->u.Link.state |= LIS_FOCUSED;
993 Current->u.Link.state &= ~LIS_FOCUSED;
1001 /***********************************************************************
1003 * Sets the states and attributes of a link item.
1005 static LRESULT SYSLINK_SetItem (SYSLINK_INFO *infoPtr, PLITEM Item)
1008 BOOL Repaint = FALSE;
1011 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1013 ERR("Invalid Flags!\n");
1017 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1020 ERR("Link %d couldn't be found\n", Item->iLink);
1024 if(Item->mask & LIF_STATE)
1026 UINT oldstate = di->u.Link.state;
1027 /* clear the masked bits */
1028 di->u.Link.state &= ~(Item->stateMask & LIS_MASK);
1030 di->u.Link.state |= (Item->state & Item->stateMask) & LIS_MASK;
1031 Repaint = (oldstate != di->u.Link.state);
1033 /* update the focus */
1034 SYSLINK_SetFocusLink(infoPtr, ((di->u.Link.state & LIS_FOCUSED) ? di : NULL));
1037 if(Item->mask & LIF_ITEMID)
1039 if(!di->u.Link.szID)
1041 di->u.Link.szID = SYSLINK_Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
1044 ERR("Unable to allocate memory for link id\n");
1050 lstrcpynW(di->u.Link.szID, Item->szID, MAX_LINKID_TEXT + 1);
1054 if(Item->mask & LIF_URL)
1056 if(!di->u.Link.szUrl)
1058 di->u.Link.szUrl = SYSLINK_Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
1061 ERR("Unable to allocate memory for link url\n");
1065 if(di->u.Link.szUrl)
1067 lstrcpynW(di->u.Link.szUrl, Item->szUrl, MAX_LINKID_TEXT + 1);
1073 SYSLINK_RepaintLink(infoPtr, di);
1079 /***********************************************************************
1081 * Retrieves the states and attributes of a link item.
1083 static LRESULT SYSLINK_GetItem (SYSLINK_INFO *infoPtr, PLITEM Item)
1087 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1089 ERR("Invalid Flags!\n");
1093 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1096 ERR("Link %d couldn't be found\n", Item->iLink);
1100 if(Item->mask & LIF_STATE)
1102 Item->state = (di->u.Link.state & Item->stateMask);
1103 if(!infoPtr->HasFocus)
1105 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1106 Item->state &= ~LIS_FOCUSED;
1110 if(Item->mask & LIF_ITEMID)
1114 lstrcpynW(Item->szID, di->u.Link.szID, MAX_LINKID_TEXT + 1);
1122 if(Item->mask & LIF_URL)
1124 if(di->u.Link.szUrl)
1126 lstrcpynW(Item->szUrl, di->u.Link.szUrl, L_MAX_URL_LENGTH + 1);
1137 /***********************************************************************
1139 * Determines the link the user clicked on.
1141 static LRESULT SYSLINK_HitTest (SYSLINK_INFO *infoPtr, PLHITTESTINFO HitTest)
1146 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1148 if(Current->Type == slLink)
1150 if((Current->u.Link.hRgn != NULL) &&
1151 PtInRegion(Current->u.Link.hRgn, HitTest->pt.x, HitTest->pt.y))
1153 HitTest->item.mask = 0;
1154 HitTest->item.iLink = id;
1155 HitTest->item.state = 0;
1156 HitTest->item.stateMask = 0;
1157 if(Current->u.Link.szID)
1159 lstrcpynW(HitTest->item.szID, Current->u.Link.szID, MAX_LINKID_TEXT + 1);
1163 HitTest->item.szID[0] = 0;
1165 if(Current->u.Link.szUrl)
1167 lstrcpynW(HitTest->item.szUrl, Current->u.Link.szUrl, L_MAX_URL_LENGTH + 1);
1171 HitTest->item.szUrl[0] = 0;
1182 /***********************************************************************
1183 * SYSLINK_GetIdealHeight
1184 * Returns the preferred height of a link at the current control's width.
1186 static LRESULT SYSLINK_GetIdealHeight (SYSLINK_INFO *infoPtr)
1188 HDC hdc = GetDC(infoPtr->Self);
1193 HGDIOBJ hOldFont = SelectObject(hdc, infoPtr->Font);
1195 if(GetTextMetricsW(hdc, &tm))
1197 height = tm.tmHeight;
1203 SelectObject(hdc, hOldFont);
1204 ReleaseDC(infoPtr->Self, hdc);
1211 /***********************************************************************
1212 * SYSLINK_SendParentNotify
1213 * Sends a WM_NOTIFY message to the parent window.
1215 static LRESULT SYSLINK_SendParentNotify (SYSLINK_INFO *infoPtr, UINT code, PDOC_ITEM Link, int iLink)
1219 nml.hdr.hwndFrom = infoPtr->Self;
1220 nml.hdr.idFrom = GetWindowLongPtrW(infoPtr->Self, GWLP_ID);
1221 nml.hdr.code = code;
1224 nml.item.iLink = iLink;
1226 nml.item.stateMask = 0;
1227 if(Link->u.Link.szID)
1229 lstrcpynW(nml.item.szID, Link->u.Link.szID, MAX_LINKID_TEXT + 1);
1233 nml.item.szID[0] = 0;
1235 if(Link->u.Link.szUrl)
1237 lstrcpynW(nml.item.szUrl, Link->u.Link.szUrl, L_MAX_URL_LENGTH + 1);
1241 nml.item.szUrl[0] = 0;
1244 return SendMessageW(GetParent(infoPtr->Self), WM_NOTIFY, (WPARAM)nml.hdr.idFrom, (LPARAM)&nml);
1247 /***********************************************************************
1249 * Handles receiving the input focus.
1251 static LRESULT SYSLINK_SetFocus (SYSLINK_INFO *infoPtr, HWND PrevFocusWindow)
1255 infoPtr->HasFocus = TRUE;
1258 /* FIXME - How to detect whether SHIFT+TAB or just TAB has been pressed?
1259 * The problem is we could get this message without keyboard input, too
1261 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1263 if(Focus == NULL && (Focus = SYSLINK_GetNextLink(infoPtr, NULL)))
1265 SYSLINK_SetFocusLink(infoPtr, Focus);
1268 /* This is a temporary hack since I'm not really sure how to detect which link to select.
1269 See message above! */
1270 Focus = SYSLINK_GetNextLink(infoPtr, NULL);
1273 SYSLINK_SetFocusLink(infoPtr, Focus);
1277 SYSLINK_RepaintLink(infoPtr, Focus);
1282 /***********************************************************************
1284 * Handles losing the input focus.
1286 static LRESULT SYSLINK_KillFocus (SYSLINK_INFO *infoPtr, HWND NewFocusWindow)
1290 infoPtr->HasFocus = FALSE;
1291 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1295 SYSLINK_RepaintLink(infoPtr, Focus);
1301 /***********************************************************************
1303 * Returns a link at the specified position
1305 static PDOC_ITEM SYSLINK_LinkAtPt (SYSLINK_INFO *infoPtr, POINT *pt, int *LinkId, BOOL MustBeEnabled)
1310 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1312 if((Current->Type == slLink) && (Current->u.Link.hRgn != NULL) &&
1313 PtInRegion(Current->u.Link.hRgn, pt->x, pt->y) &&
1314 (!MustBeEnabled || (MustBeEnabled && (Current->u.Link.state & LIS_ENABLED))))
1328 /***********************************************************************
1329 * SYSLINK_LButtonDown
1330 * Handles mouse clicks
1332 static LRESULT SYSLINK_LButtonDown (SYSLINK_INFO *infoPtr, DWORD Buttons, POINT *pt)
1334 PDOC_ITEM Current, Old;
1337 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1340 Old = SYSLINK_SetFocusLink(infoPtr, Current);
1341 if(Old != NULL && Old != Current)
1343 SYSLINK_RepaintLink(infoPtr, Old);
1345 infoPtr->MouseDownID = id;
1346 SYSLINK_RepaintLink(infoPtr, Current);
1347 SetFocus(infoPtr->Self);
1353 /***********************************************************************
1355 * Handles mouse clicks
1357 static LRESULT SYSLINK_LButtonUp (SYSLINK_INFO *infoPtr, DWORD Buttons, POINT *pt)
1359 if(infoPtr->MouseDownID > -1)
1364 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1365 if((Current != NULL) && (Current->u.Link.state & LIS_FOCUSED) && (infoPtr->MouseDownID == id))
1367 SYSLINK_SendParentNotify(infoPtr, NM_CLICK, Current, id);
1371 infoPtr->MouseDownID = -1;
1376 /***********************************************************************
1378 * Handles ENTER key events
1380 static BOOL SYSLINK_OnEnter (SYSLINK_INFO *infoPtr)
1382 if(infoPtr->HasFocus)
1387 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1390 SYSLINK_SendParentNotify(infoPtr, NM_RETURN, Focus, id);
1397 /***********************************************************************
1398 * SYSKEY_SelectNextPrevLink
1399 * Changes the currently focused link
1401 static BOOL SYSKEY_SelectNextPrevLink (SYSLINK_INFO *infoPtr, BOOL Prev)
1403 if(infoPtr->HasFocus)
1408 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1411 PDOC_ITEM NewFocus, OldFocus;
1414 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1416 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1418 if(NewFocus != NULL)
1420 OldFocus = SYSLINK_SetFocusLink(infoPtr, NewFocus);
1422 if(OldFocus != NewFocus)
1424 SYSLINK_RepaintLink(infoPtr, OldFocus);
1426 SYSLINK_RepaintLink(infoPtr, NewFocus);
1434 /***********************************************************************
1435 * SYSKEY_SelectNextPrevLink
1436 * Determines if there's a next or previous link to decide whether the control
1437 * should capture the tab key message
1439 static BOOL SYSLINK_NoNextLink (SYSLINK_INFO *infoPtr, BOOL Prev)
1441 PDOC_ITEM Focus, NewFocus;
1443 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1445 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1447 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1449 return NewFocus == NULL;
1452 /***********************************************************************
1455 static LRESULT WINAPI SysLinkWindowProc(HWND hwnd, UINT message,
1456 WPARAM wParam, LPARAM lParam)
1458 SYSLINK_INFO *infoPtr;
1460 TRACE("hwnd=%p msg=%04x wparam=%x lParam=%lx\n", hwnd, message, wParam, lParam);
1462 infoPtr = (SYSLINK_INFO *)GetWindowLongPtrW(hwnd, 0);
1464 if (!infoPtr && message != WM_CREATE)
1465 return DefWindowProcW( hwnd, message, wParam, lParam );
1469 return SYSLINK_Paint (infoPtr);
1474 DWORD mp = GetMessagePos();
1476 ht.pt.x = (short)LOWORD(mp);
1477 ht.pt.y = (short)HIWORD(mp);
1479 ScreenToClient(infoPtr->Self, &ht.pt);
1480 if(SYSLINK_HitTest (infoPtr, &ht))
1482 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_HAND));
1485 /* let the default window proc handle this message */
1486 return DefWindowProcW(hwnd, message, wParam, lParam);
1492 HDC hdc = GetDC(infoPtr->Self);
1495 SYSLINK_Render(infoPtr, hdc);
1496 ReleaseDC(infoPtr->Self, hdc);
1502 return (LRESULT)infoPtr->Font;
1505 return (LRESULT)SYSLINK_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1508 SYSLINK_SetText(infoPtr, (LPWSTR)lParam);
1509 return DefWindowProcW(hwnd, message, wParam, lParam);
1511 case WM_LBUTTONDOWN:
1514 pt.x = LOWORD(lParam);
1515 pt.y = HIWORD(lParam);
1516 return SYSLINK_LButtonDown(infoPtr, wParam, &pt);
1521 pt.x = LOWORD(lParam);
1522 pt.y = HIWORD(lParam);
1523 return SYSLINK_LButtonUp(infoPtr, wParam, &pt);
1531 SYSLINK_OnEnter(infoPtr);
1535 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1536 SYSKEY_SelectNextPrevLink(infoPtr, shift);
1540 return DefWindowProcW(hwnd, message, wParam, lParam);
1545 LRESULT Ret = DLGC_HASSETSEL;
1546 int vk = (lParam != 0 ? (int)((LPMSG)lParam)->wParam : 0);
1550 Ret |= DLGC_WANTMESSAGE;
1554 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1555 if(!SYSLINK_NoNextLink(infoPtr, shift))
1557 Ret |= DLGC_WANTTAB;
1561 Ret |= DLGC_WANTCHARS;
1573 pt.x = LOWORD(lParam);
1574 pt.y = HIWORD(lParam);
1576 GetClientRect(infoPtr->Self, &rc);
1577 ScreenToClient(infoPtr->Self, &pt);
1578 if(pt.x < 0 || pt.y < 0 || pt.x > rc.right || pt.y > rc.bottom)
1583 if(SYSLINK_LinkAtPt(infoPtr, &pt, NULL, FALSE))
1588 return HTTRANSPARENT;
1592 return SYSLINK_HitTest(infoPtr, (PLHITTESTINFO)lParam);
1595 return SYSLINK_SetItem(infoPtr, (PLITEM)lParam);
1598 return SYSLINK_GetItem(infoPtr, (PLITEM)lParam);
1600 case LM_GETIDEALHEIGHT:
1601 return SYSLINK_GetIdealHeight(infoPtr);
1604 return SYSLINK_SetFocus(infoPtr, (HWND)wParam);
1607 return SYSLINK_KillFocus(infoPtr, (HWND)wParam);
1610 /* allocate memory for info struct */
1611 infoPtr = (SYSLINK_INFO *)SYSLINK_Alloc (sizeof(SYSLINK_INFO));
1612 if (!infoPtr) return -1;
1613 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1615 /* initialize the info struct */
1616 infoPtr->Self = hwnd;
1618 infoPtr->LinkFont = 0;
1619 infoPtr->Items = NULL;
1620 infoPtr->HasFocus = FALSE;
1621 infoPtr->MouseDownID = -1;
1622 infoPtr->TextColor = GetSysColor(COLOR_WINDOWTEXT);
1623 infoPtr->LinkColor = GetSysColor(COLOR_HIGHLIGHT);
1624 infoPtr->VisitedColor = GetSysColor(COLOR_HIGHLIGHT);
1625 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd);
1626 lParam = (LPARAM)(((LPCREATESTRUCTW)lParam)->lpszName);
1627 SYSLINK_SetText(infoPtr, (LPWSTR)lParam);
1631 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd);
1632 SYSLINK_ClearDoc(infoPtr);
1633 if(infoPtr->Font != 0) DeleteObject(infoPtr->Font);
1634 if(infoPtr->LinkFont != 0) DeleteObject(infoPtr->LinkFont);
1635 SYSLINK_Free (infoPtr);
1636 SetWindowLongPtrW(hwnd, 0, 0);
1640 if ((message >= WM_USER) && (message < WM_APP))
1641 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam );
1642 return DefWindowProcW(hwnd, message, wParam, lParam);
1647 /***********************************************************************
1648 * SYSLINK_Register [Internal]
1650 * Registers the SysLink window class.
1652 VOID SYSLINK_Register (void)
1656 ZeroMemory (&wndClass, sizeof(wndClass));
1657 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1658 wndClass.lpfnWndProc = SysLinkWindowProc;
1659 wndClass.cbClsExtra = 0;
1660 wndClass.cbWndExtra = sizeof (SYSLINK_INFO *);
1661 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1662 wndClass.lpszClassName = WC_LINK;
1664 RegisterClassW (&wndClass);
1668 /***********************************************************************
1669 * SYSLINK_Unregister [Internal]
1671 * Unregisters the SysLink window class.
1673 VOID SYSLINK_Unregister (void)
1675 UnregisterClassW (WC_LINK, NULL);