4 * Copyright 2004, 2005 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 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 */
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 Free(DocItem->u.Link.szID);
121 Free(DocItem->u.Link.szUrl);
124 /* we don't free Text because it's just a pointer to a character in the
125 entire window text string */
130 /***********************************************************************
131 * SYSLINK_AppendDocItem
132 * Create and append a new document item.
134 static PDOC_ITEM SYSLINK_AppendDocItem (SYSLINK_INFO *infoPtr, LPCWSTR Text, UINT textlen,
135 SL_ITEM_TYPE type, PDOC_ITEM LastItem)
138 Item = Alloc(sizeof(DOC_ITEM) + ((textlen + 1) * sizeof(WCHAR)));
141 ERR("Failed to alloc DOC_ITEM structure!\n");
144 textlen = min(textlen, lstrlenW(Text));
147 Item->Text = (LPWSTR)(Item + 1);
148 Item->nText = textlen;
154 LastItem->Next = Item;
158 infoPtr->Items = Item;
161 lstrcpynW(Item->Text, Text, textlen + 1);
162 Item->Text[textlen] = 0;
167 /***********************************************************************
169 * Clears the document tree
171 static VOID SYSLINK_ClearDoc (SYSLINK_INFO *infoPtr)
173 PDOC_ITEM Item, Next;
175 Item = infoPtr->Items;
179 SYSLINK_FreeDocItem(Item);
183 infoPtr->Items = NULL;
186 /***********************************************************************
188 * Parses the window text string and creates a document. Returns the
189 * number of document items created.
191 static UINT SYSLINK_ParseText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
193 LPCWSTR current, textstart = NULL, linktext = NULL, firsttag = NULL;
194 int taglen = 0, textlen = 0, linklen = 0, docitems = 0;
195 PDOC_ITEM Last = NULL;
196 SL_ITEM_TYPE CurrentType = slText;
200 for(current = Text; *current != 0;)
204 if(!StrCmpNIW(current, SL_LINKOPEN, 2) && (CurrentType == slText))
206 BOOL ValidParam = FALSE, ValidLink = FALSE;
208 if(*(current + 2) == '>')
210 /* we just have to deal with a <a> tag */
219 else if(*(current + 2) == infoPtr->BreakChar)
221 /* we expect parameters, parse them */
222 LPCWSTR *CurrentParameter = NULL, tmp;
223 UINT *CurrentParameterLen = NULL;
226 tmp = current + taglen;
231 /* compare the current position with all known parameters */
232 if(!StrCmpNIW(tmp, SL_HREF, 6))
236 CurrentParameter = &lpUrl;
237 CurrentParameterLen = &lenUrl;
239 else if(!StrCmpNIW(tmp, SL_ID, 4))
243 CurrentParameter = &lpID;
244 CurrentParameterLen = &lenId;
253 /* we got a known parameter, now search until the next " character.
254 If we can't find a " character, there's a syntax error and we just assume it's text */
256 *CurrentParameter = current + taglen;
257 *CurrentParameterLen = 0;
259 for(tmp = *CurrentParameter; *tmp != 0; tmp++)
268 (*CurrentParameterLen)++;
273 /* we're done with this parameter, now there are only 2 possibilities:
274 * 1. another parameter is coming, so expect a ' ' (space) character
275 * 2. the tag is being closed, so expect a '<' character
277 if(*tmp == infoPtr->BreakChar)
279 /* we expect another parameter, do the whole thing again */
286 /* the tag is being closed, we're done */
295 if(ValidLink && ValidParam)
297 /* the <a ...> tag appears to be valid. save all information
298 so we can add the link if we find a valid </a> tag later */
299 CurrentType = slLink;
300 linktext = current + taglen;
309 if(textstart == NULL)
315 else if(!StrCmpNIW(current, SL_LINKCLOSE, 4) && (CurrentType == slLink) && firsttag)
317 /* there's a <a...> tag opened, first add the previous text, if present */
318 if(textstart != NULL && textlen > 0 && firsttag > textstart)
320 Last = SYSLINK_AppendDocItem(infoPtr, textstart, firsttag - textstart, slText, Last);
323 ERR("Unable to create new document item!\n");
331 /* now it's time to add the link to the document */
333 if(linktext != NULL && linklen > 0)
335 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slLink, Last);
338 ERR("Unable to create new document item!\n");
342 if(CurrentType == slLink)
346 if(!(infoPtr->Style & WS_DISABLED))
348 Last->u.Link.state |= LIS_ENABLED;
350 /* Copy the tag parameters */
353 nc = min(lenId, strlenW(lpID));
354 nc = min(nc, MAX_LINKID_TEXT);
355 Last->u.Link.szID = Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
356 if(Last->u.Link.szID != NULL)
358 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
359 Last->u.Link.szID[nc] = 0;
363 Last->u.Link.szID = NULL;
366 nc = min(lenUrl, strlenW(lpUrl));
367 nc = min(nc, L_MAX_URL_LENGTH);
368 Last->u.Link.szUrl = Alloc((L_MAX_URL_LENGTH + 1) * sizeof(WCHAR));
369 if(Last->u.Link.szUrl != NULL)
371 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
372 Last->u.Link.szUrl[nc] = 0;
376 Last->u.Link.szUrl = NULL;
380 CurrentType = slText;
387 /* we don't know what tag it is, so just continue */
390 if(CurrentType == slText && textstart == NULL)
404 /* save the pointer of the current text item if we couldn't find a tag */
405 if(textstart == NULL && CurrentType == slText)
414 if(textstart != NULL && textlen > 0)
416 Last = SYSLINK_AppendDocItem(infoPtr, textstart, textlen, CurrentType, Last);
419 ERR("Unable to create new document item!\n");
422 if(CurrentType == slLink)
426 if(!(infoPtr->Style & WS_DISABLED))
428 Last->u.Link.state |= LIS_ENABLED;
430 /* Copy the tag parameters */
433 nc = min(lenId, strlenW(lpID));
434 nc = min(nc, MAX_LINKID_TEXT);
435 Last->u.Link.szID = Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
436 if(Last->u.Link.szID != NULL)
438 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
439 Last->u.Link.szID[nc] = 0;
443 Last->u.Link.szID = NULL;
446 nc = min(lenUrl, strlenW(lpUrl));
447 nc = min(nc, L_MAX_URL_LENGTH);
448 Last->u.Link.szUrl = Alloc((L_MAX_URL_LENGTH + 1) * sizeof(WCHAR));
449 if(Last->u.Link.szUrl != NULL)
451 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
452 Last->u.Link.szUrl[nc] = 0;
456 Last->u.Link.szUrl = NULL;
461 if(linktext != NULL && linklen > 0)
463 /* we got an unclosed link, just display the text */
464 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slText, Last);
467 ERR("Unable to create new document item!\n");
476 /***********************************************************************
477 * SYSLINK_RepaintLink
480 static VOID SYSLINK_RepaintLink (SYSLINK_INFO *infoPtr, PDOC_ITEM DocItem)
485 if(DocItem->Type != slLink)
487 ERR("DocItem not a link!\n");
491 bl = DocItem->Blocks;
498 InvalidateRect(infoPtr->Self, &bl->rc, TRUE);
499 n -= bl->nChars + bl->nSkip;
505 /***********************************************************************
506 * SYSLINK_GetLinkItemByIndex
507 * Retrieves a document link by its index
509 static PDOC_ITEM SYSLINK_GetLinkItemByIndex (SYSLINK_INFO *infoPtr, int iLink)
511 PDOC_ITEM Current = infoPtr->Items;
513 while(Current != NULL)
515 if((Current->Type == slLink) && (iLink-- <= 0))
519 Current = Current->Next;
524 /***********************************************************************
525 * SYSLINK_GetFocusLink
526 * Retrieves the link that has the LIS_FOCUSED bit
528 static PDOC_ITEM SYSLINK_GetFocusLink (SYSLINK_INFO *infoPtr, int *LinkId)
530 PDOC_ITEM Current = infoPtr->Items;
533 while(Current != NULL)
535 if((Current->Type == slLink))
537 if(Current->u.Link.state & LIS_FOCUSED)
545 Current = Current->Next;
550 /***********************************************************************
551 * SYSLINK_GetNextLink
554 static PDOC_ITEM SYSLINK_GetNextLink (SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
556 for(Current = (Current != NULL ? Current->Next : infoPtr->Items);
558 Current = Current->Next)
560 if(Current->Type == slLink)
568 /***********************************************************************
569 * SYSLINK_GetPrevLink
570 * Gets the previous link
572 static PDOC_ITEM SYSLINK_GetPrevLink (SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
576 /* returns the last link */
577 PDOC_ITEM Last = NULL;
579 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
581 if(Current->Type == slLink)
590 /* returns the previous link */
591 PDOC_ITEM Cur, Prev = NULL;
593 for(Cur = infoPtr->Items; Cur != NULL; Cur = Cur->Next)
599 if(Cur->Type == slLink)
608 /***********************************************************************
610 * Tries to wrap a line.
612 static BOOL SYSLINK_WrapLine (HDC hdc, LPWSTR Text, WCHAR BreakChar, int *LineLen,
613 int nFit, LPSIZE Extent, int Width)
624 Current = Text + nFit;
626 /* check if we're in the middle of a word */
627 if((*Current) != BreakChar)
629 /* search for the beginning of the word */
630 while(Current > Text && (*(Current - 1)) != BreakChar)
647 /***********************************************************************
649 * Renders the document in memory
651 static VOID SYSLINK_Render (SYSLINK_INFO *infoPtr, HDC hdc)
656 int x, y, LineHeight;
658 GetClientRect(infoPtr->Self, &rc);
659 rc.right -= SL_RIGHTMARGIN;
660 rc.bottom -= SL_BOTTOMMARGIN;
662 if(rc.right - SL_LEFTMARGIN < 0 || rc.bottom - SL_TOPMARGIN < 0) return;
664 hOldFont = SelectObject(hdc, infoPtr->Font);
670 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
674 PDOC_TEXTBLOCK bl, cbl;
678 if(Current->nText == 0)
686 if (Current->Blocks != NULL)
688 Free(Current->Blocks);
689 Current->Blocks = NULL;
694 if(Current->Type == slText)
696 SelectObject(hdc, infoPtr->Font);
698 else if(Current->Type == slLink)
700 SelectObject(hdc, infoPtr->LinkFont);
707 /* skip break characters unless they're the first of the doc item */
708 if(tx != Current->Text || x == SL_LEFTMARGIN)
710 while(n > 0 && (*tx) == infoPtr->BreakChar)
718 if((n == 0 && SkipChars != 0) ||
719 GetTextExtentExPointW(hdc, tx, n, rc.right - x, &nFit, NULL, &szDim))
726 Wrap = SYSLINK_WrapLine(hdc, tx, infoPtr->BreakChar, &LineLen, nFit, &szDim, rc.right - x);
730 if(x > SL_LEFTMARGIN)
732 /* move one line down, the word didn't fit into the line */
740 /* the word starts at the beginning of the line and doesn't
741 fit into the line, so break it at the last character that fits */
742 LineLen = max(nFit, 1);
748 if(!GetTextExtentExPointW(hdc, tx, LineLen, rc.right - x, NULL, NULL, &szDim))
762 PDOC_TEXTBLOCK nbl = ReAlloc(bl, (nBlocks + 1) * sizeof(DOC_TEXTBLOCK));
776 bl = Alloc((nBlocks + 1) * sizeof(DOC_TEXTBLOCK));
783 cbl = bl + nBlocks - 1;
785 cbl->nChars = LineLen;
786 cbl->nSkip = SkipChars;
789 cbl->rc.right = x + szDim.cx;
790 cbl->rc.bottom = y + szDim.cy;
795 LineHeight = max(LineHeight, szDim.cy);
807 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
821 Current->Blocks = bl;
824 Current->Blocks = NULL;
827 SelectObject(hdc, hOldFont);
830 /***********************************************************************
832 * Draws the SysLink control.
834 static LRESULT SYSLINK_Draw (SYSLINK_INFO *infoPtr, HDC hdc)
839 COLORREF OldTextColor, OldBkColor;
841 hOldFont = SelectObject(hdc, infoPtr->Font);
842 OldTextColor = SetTextColor(hdc, infoPtr->TextColor);
843 OldBkColor = SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
845 GetClientRect(infoPtr->Self, &rc);
846 rc.right -= SL_RIGHTMARGIN + SL_LEFTMARGIN;
847 rc.bottom -= SL_BOTTOMMARGIN + SL_TOPMARGIN;
849 if(rc.right < 0 || rc.bottom < 0) return 0;
851 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
857 bl = Current->Blocks;
863 if(Current->Type == slText)
865 SelectObject(hdc, infoPtr->Font);
866 SetTextColor(hdc, infoPtr->TextColor);
870 SelectObject(hdc, infoPtr->LinkFont);
871 SetTextColor(hdc, (!(Current->u.Link.state & LIS_VISITED) ? infoPtr->LinkColor : infoPtr->VisitedColor));
877 ExtTextOutW(hdc, bl->rc.left, bl->rc.top, ETO_OPAQUE | ETO_CLIPPED, &bl->rc, tx, bl->nChars, NULL);
878 if((Current->Type == slLink) && (Current->u.Link.state & LIS_FOCUSED) && infoPtr->HasFocus)
881 PrevColor = SetBkColor(hdc, OldBkColor);
882 DrawFocusRect(hdc, &bl->rc);
883 SetBkColor(hdc, PrevColor);
886 n -= bl->nChars + bl->nSkip;
892 SetBkColor(hdc, OldBkColor);
893 SetTextColor(hdc, OldTextColor);
894 SelectObject(hdc, hOldFont);
900 /***********************************************************************
902 * Handles the WM_PAINT message.
904 static LRESULT SYSLINK_Paint (SYSLINK_INFO *infoPtr, HDC hdcParam)
909 hdc = hdcParam ? hdcParam : BeginPaint (infoPtr->Self, &ps);
910 SYSLINK_Draw (infoPtr, hdc);
911 if (!hdcParam) EndPaint (infoPtr->Self, &ps);
916 /***********************************************************************
918 * Set new Font for the SysLink control.
920 static HFONT SYSLINK_SetFont (SYSLINK_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
925 HFONT hOldFont = infoPtr->Font;
926 infoPtr->Font = hFont;
928 /* free the underline font */
929 if(infoPtr->LinkFont != NULL)
931 DeleteObject(infoPtr->LinkFont);
932 infoPtr->LinkFont = NULL;
935 /* Render text position and word wrapping in memory */
936 hdc = GetDC(infoPtr->Self);
939 /* create a new underline font */
940 if(GetTextMetricsW(hdc, &tm) &&
941 GetObjectW(infoPtr->Font, sizeof(LOGFONTW), &lf))
943 lf.lfUnderline = TRUE;
944 infoPtr->LinkFont = CreateFontIndirectW(&lf);
945 infoPtr->BreakChar = tm.tmBreakChar;
949 ERR("Failed to create link font!\n");
952 SYSLINK_Render(infoPtr, hdc);
953 ReleaseDC(infoPtr->Self, hdc);
958 RedrawWindow(infoPtr->Self, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
964 /***********************************************************************
966 * Set new text for the SysLink control.
968 static LRESULT SYSLINK_SetText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
972 /* clear the document */
973 SYSLINK_ClearDoc(infoPtr);
975 textlen = lstrlenW(Text);
976 if(Text == NULL || textlen == 0)
981 /* let's parse the string and create a document */
982 if(SYSLINK_ParseText(infoPtr, Text) > 0)
984 /* Render text position and word wrapping in memory */
985 HDC hdc = GetDC(infoPtr->Self);
986 SYSLINK_Render(infoPtr, hdc);
987 SYSLINK_Draw(infoPtr, hdc);
988 ReleaseDC(infoPtr->Self, hdc);
994 /***********************************************************************
995 * SYSLINK_SetFocusLink
996 * Updates the focus status bits and focusses the specified link.
997 * If no document item is specified, the focus bit will be removed from all links.
998 * Returns the previous focused item.
1000 static PDOC_ITEM SYSLINK_SetFocusLink (SYSLINK_INFO *infoPtr, PDOC_ITEM DocItem)
1002 PDOC_ITEM Current, PrevFocus = NULL;
1004 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1006 if(Current->Type == slLink)
1008 if((PrevFocus == NULL) && (Current->u.Link.state & LIS_FOCUSED))
1010 PrevFocus = Current;
1013 if(Current == DocItem)
1015 Current->u.Link.state |= LIS_FOCUSED;
1019 Current->u.Link.state &= ~LIS_FOCUSED;
1027 /***********************************************************************
1029 * Sets the states and attributes of a link item.
1031 static LRESULT SYSLINK_SetItem (SYSLINK_INFO *infoPtr, PLITEM Item)
1034 BOOL Repaint = FALSE;
1037 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1039 ERR("Invalid Flags!\n");
1043 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1046 ERR("Link %d couldn't be found\n", Item->iLink);
1050 if(Item->mask & LIF_STATE)
1052 UINT oldstate = di->u.Link.state;
1053 /* clear the masked bits */
1054 di->u.Link.state &= ~(Item->stateMask & LIS_MASK);
1056 di->u.Link.state |= (Item->state & Item->stateMask) & LIS_MASK;
1057 Repaint = (oldstate != di->u.Link.state);
1059 /* update the focus */
1060 SYSLINK_SetFocusLink(infoPtr, ((di->u.Link.state & LIS_FOCUSED) ? di : NULL));
1063 if(Item->mask & LIF_ITEMID)
1065 if(!di->u.Link.szID)
1067 di->u.Link.szID = Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
1070 ERR("Unable to allocate memory for link id\n");
1076 lstrcpynW(di->u.Link.szID, Item->szID, MAX_LINKID_TEXT + 1);
1080 if(Item->mask & LIF_URL)
1082 if(!di->u.Link.szUrl)
1084 di->u.Link.szUrl = Alloc((MAX_LINKID_TEXT + 1) * sizeof(WCHAR));
1087 ERR("Unable to allocate memory for link url\n");
1091 if(di->u.Link.szUrl)
1093 lstrcpynW(di->u.Link.szUrl, Item->szUrl, MAX_LINKID_TEXT + 1);
1099 SYSLINK_RepaintLink(infoPtr, di);
1105 /***********************************************************************
1107 * Retrieves the states and attributes of a link item.
1109 static LRESULT SYSLINK_GetItem (SYSLINK_INFO *infoPtr, PLITEM Item)
1113 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1115 ERR("Invalid Flags!\n");
1119 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1122 ERR("Link %d couldn't be found\n", Item->iLink);
1126 if(Item->mask & LIF_STATE)
1128 Item->state = (di->u.Link.state & Item->stateMask);
1129 if(!infoPtr->HasFocus)
1131 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1132 Item->state &= ~LIS_FOCUSED;
1136 if(Item->mask & LIF_ITEMID)
1140 lstrcpynW(Item->szID, di->u.Link.szID, MAX_LINKID_TEXT + 1);
1148 if(Item->mask & LIF_URL)
1150 if(di->u.Link.szUrl)
1152 lstrcpynW(Item->szUrl, di->u.Link.szUrl, L_MAX_URL_LENGTH + 1);
1163 /***********************************************************************
1164 * SYSLINK_PtInDocItem
1165 * Determines if a point is in the region of a document item
1167 static BOOL SYSLINK_PtInDocItem (PDOC_ITEM DocItem, POINT pt)
1172 bl = DocItem->Blocks;
1179 if (PtInRect(&bl->rc, pt))
1183 n -= bl->nChars + bl->nSkip;
1191 /***********************************************************************
1193 * Determines the link the user clicked on.
1195 static LRESULT SYSLINK_HitTest (SYSLINK_INFO *infoPtr, PLHITTESTINFO HitTest)
1200 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1202 if(Current->Type == slLink)
1204 if(SYSLINK_PtInDocItem(Current, HitTest->pt))
1206 HitTest->item.mask = 0;
1207 HitTest->item.iLink = id;
1208 HitTest->item.state = 0;
1209 HitTest->item.stateMask = 0;
1210 if(Current->u.Link.szID)
1212 lstrcpynW(HitTest->item.szID, Current->u.Link.szID, MAX_LINKID_TEXT + 1);
1216 HitTest->item.szID[0] = 0;
1218 if(Current->u.Link.szUrl)
1220 lstrcpynW(HitTest->item.szUrl, Current->u.Link.szUrl, L_MAX_URL_LENGTH + 1);
1224 HitTest->item.szUrl[0] = 0;
1235 /***********************************************************************
1236 * SYSLINK_GetIdealHeight
1237 * Returns the preferred height of a link at the current control's width.
1239 static LRESULT SYSLINK_GetIdealHeight (SYSLINK_INFO *infoPtr)
1241 HDC hdc = GetDC(infoPtr->Self);
1246 HGDIOBJ hOldFont = SelectObject(hdc, infoPtr->Font);
1248 if(GetTextMetricsW(hdc, &tm))
1250 height = tm.tmHeight;
1256 SelectObject(hdc, hOldFont);
1257 ReleaseDC(infoPtr->Self, hdc);
1264 /***********************************************************************
1265 * SYSLINK_SendParentNotify
1266 * Sends a WM_NOTIFY message to the parent window.
1268 static LRESULT SYSLINK_SendParentNotify (SYSLINK_INFO *infoPtr, UINT code, PDOC_ITEM Link, int iLink)
1272 nml.hdr.hwndFrom = infoPtr->Self;
1273 nml.hdr.idFrom = GetWindowLongPtrW(infoPtr->Self, GWLP_ID);
1274 nml.hdr.code = code;
1277 nml.item.iLink = iLink;
1279 nml.item.stateMask = 0;
1280 if(Link->u.Link.szID)
1282 lstrcpynW(nml.item.szID, Link->u.Link.szID, MAX_LINKID_TEXT + 1);
1286 nml.item.szID[0] = 0;
1288 if(Link->u.Link.szUrl)
1290 lstrcpynW(nml.item.szUrl, Link->u.Link.szUrl, L_MAX_URL_LENGTH + 1);
1294 nml.item.szUrl[0] = 0;
1297 return SendMessageW(infoPtr->Notify, WM_NOTIFY, (WPARAM)nml.hdr.idFrom, (LPARAM)&nml);
1300 /***********************************************************************
1302 * Handles receiving the input focus.
1304 static LRESULT SYSLINK_SetFocus (SYSLINK_INFO *infoPtr, HWND PrevFocusWindow)
1308 infoPtr->HasFocus = TRUE;
1310 /* We always select the first link, even if we activated the control using
1311 SHIFT+TAB. This is the default behavior */
1312 Focus = SYSLINK_GetNextLink(infoPtr, NULL);
1315 SYSLINK_SetFocusLink(infoPtr, Focus);
1318 SYSLINK_RepaintLink(infoPtr, Focus);
1323 /***********************************************************************
1325 * Handles losing the input focus.
1327 static LRESULT SYSLINK_KillFocus (SYSLINK_INFO *infoPtr, HWND NewFocusWindow)
1331 infoPtr->HasFocus = FALSE;
1332 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1336 SYSLINK_RepaintLink(infoPtr, Focus);
1342 /***********************************************************************
1344 * Returns a link at the specified position
1346 static PDOC_ITEM SYSLINK_LinkAtPt (SYSLINK_INFO *infoPtr, POINT *pt, int *LinkId, BOOL MustBeEnabled)
1351 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1353 if((Current->Type == slLink) && SYSLINK_PtInDocItem(Current, *pt) &&
1354 (!MustBeEnabled || (MustBeEnabled && (Current->u.Link.state & LIS_ENABLED))))
1368 /***********************************************************************
1369 * SYSLINK_LButtonDown
1370 * Handles mouse clicks
1372 static LRESULT SYSLINK_LButtonDown (SYSLINK_INFO *infoPtr, DWORD Buttons, POINT *pt)
1374 PDOC_ITEM Current, Old;
1377 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1380 SetFocus(infoPtr->Self);
1382 Old = SYSLINK_SetFocusLink(infoPtr, Current);
1383 if(Old != NULL && Old != Current)
1385 SYSLINK_RepaintLink(infoPtr, Old);
1387 infoPtr->MouseDownID = id;
1388 SYSLINK_RepaintLink(infoPtr, Current);
1394 /***********************************************************************
1396 * Handles mouse clicks
1398 static LRESULT SYSLINK_LButtonUp (SYSLINK_INFO *infoPtr, DWORD Buttons, POINT *pt)
1400 if(infoPtr->MouseDownID > -1)
1405 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1406 if((Current != NULL) && (Current->u.Link.state & LIS_FOCUSED) && (infoPtr->MouseDownID == id))
1408 SYSLINK_SendParentNotify(infoPtr, NM_CLICK, Current, id);
1412 infoPtr->MouseDownID = -1;
1417 /***********************************************************************
1419 * Handles ENTER key events
1421 static BOOL SYSLINK_OnEnter (SYSLINK_INFO *infoPtr)
1423 if(infoPtr->HasFocus)
1428 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1431 SYSLINK_SendParentNotify(infoPtr, NM_RETURN, Focus, id);
1438 /***********************************************************************
1439 * SYSKEY_SelectNextPrevLink
1440 * Changes the currently focused link
1442 static BOOL SYSKEY_SelectNextPrevLink (SYSLINK_INFO *infoPtr, BOOL Prev)
1444 if(infoPtr->HasFocus)
1449 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1452 PDOC_ITEM NewFocus, OldFocus;
1455 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1457 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1459 if(NewFocus != NULL)
1461 OldFocus = SYSLINK_SetFocusLink(infoPtr, NewFocus);
1463 if(OldFocus != NewFocus)
1465 SYSLINK_RepaintLink(infoPtr, OldFocus);
1467 SYSLINK_RepaintLink(infoPtr, NewFocus);
1475 /***********************************************************************
1476 * SYSKEY_SelectNextPrevLink
1477 * Determines if there's a next or previous link to decide whether the control
1478 * should capture the tab key message
1480 static BOOL SYSLINK_NoNextLink (SYSLINK_INFO *infoPtr, BOOL Prev)
1482 PDOC_ITEM Focus, NewFocus;
1484 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1486 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1488 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1490 return NewFocus == NULL;
1493 /***********************************************************************
1496 static LRESULT WINAPI SysLinkWindowProc(HWND hwnd, UINT message,
1497 WPARAM wParam, LPARAM lParam)
1499 SYSLINK_INFO *infoPtr;
1501 TRACE("hwnd=%p msg=%04x wparam=%x lParam=%lx\n", hwnd, message, wParam, lParam);
1503 infoPtr = (SYSLINK_INFO *)GetWindowLongPtrW(hwnd, 0);
1505 if (!infoPtr && message != WM_CREATE)
1506 goto HandleDefaultMessage;
1509 case WM_PRINTCLIENT:
1511 return SYSLINK_Paint (infoPtr, (HDC)wParam);
1516 DWORD mp = GetMessagePos();
1518 ht.pt.x = (short)LOWORD(mp);
1519 ht.pt.y = (short)HIWORD(mp);
1521 ScreenToClient(infoPtr->Self, &ht.pt);
1522 if(SYSLINK_HitTest (infoPtr, &ht))
1524 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_HAND));
1527 /* let the default window proc handle this message */
1528 goto HandleDefaultMessage;
1533 HDC hdc = GetDC(infoPtr->Self);
1536 SYSLINK_Render(infoPtr, hdc);
1537 ReleaseDC(infoPtr->Self, hdc);
1543 return (LRESULT)infoPtr->Font;
1546 return (LRESULT)SYSLINK_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1549 SYSLINK_SetText(infoPtr, (LPWSTR)lParam);
1550 goto HandleDefaultMessage;
1552 case WM_LBUTTONDOWN:
1555 pt.x = LOWORD(lParam);
1556 pt.y = HIWORD(lParam);
1557 return SYSLINK_LButtonDown(infoPtr, wParam, &pt);
1562 pt.x = LOWORD(lParam);
1563 pt.y = HIWORD(lParam);
1564 return SYSLINK_LButtonUp(infoPtr, wParam, &pt);
1572 SYSLINK_OnEnter(infoPtr);
1576 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1577 SYSKEY_SelectNextPrevLink(infoPtr, shift);
1581 goto HandleDefaultMessage;
1586 LRESULT Ret = DLGC_HASSETSEL;
1587 int vk = (lParam != 0 ? (int)((LPMSG)lParam)->wParam : 0);
1591 Ret |= DLGC_WANTMESSAGE;
1595 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1596 if(!SYSLINK_NoNextLink(infoPtr, shift))
1598 Ret |= DLGC_WANTTAB;
1602 Ret |= DLGC_WANTCHARS;
1614 pt.x = LOWORD(lParam);
1615 pt.y = HIWORD(lParam);
1617 GetClientRect(infoPtr->Self, &rc);
1618 ScreenToClient(infoPtr->Self, &pt);
1619 if(pt.x < 0 || pt.y < 0 || pt.x > rc.right || pt.y > rc.bottom)
1624 if(SYSLINK_LinkAtPt(infoPtr, &pt, NULL, FALSE))
1629 return HTTRANSPARENT;
1633 return SYSLINK_HitTest(infoPtr, (PLHITTESTINFO)lParam);
1636 return SYSLINK_SetItem(infoPtr, (PLITEM)lParam);
1639 return SYSLINK_GetItem(infoPtr, (PLITEM)lParam);
1641 case LM_GETIDEALHEIGHT:
1642 return SYSLINK_GetIdealHeight(infoPtr);
1645 return SYSLINK_SetFocus(infoPtr, (HWND)wParam);
1648 return SYSLINK_KillFocus(infoPtr, (HWND)wParam);
1651 infoPtr->Style &= ~WS_DISABLED;
1652 infoPtr->Style |= (wParam ? 0 : WS_DISABLED);
1653 InvalidateRect (infoPtr->Self, NULL, FALSE);
1656 case WM_STYLECHANGED:
1657 if (wParam == GWL_STYLE)
1659 infoPtr->Style = ((LPSTYLESTRUCT)lParam)->styleNew;
1661 InvalidateRect(infoPtr->Self, NULL, TRUE);
1666 /* allocate memory for info struct */
1667 infoPtr = Alloc (sizeof(SYSLINK_INFO));
1668 if (!infoPtr) return -1;
1669 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1671 /* initialize the info struct */
1672 infoPtr->Self = hwnd;
1673 infoPtr->Notify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1674 infoPtr->Style = ((LPCREATESTRUCTW)lParam)->style;
1676 infoPtr->LinkFont = 0;
1677 infoPtr->Items = NULL;
1678 infoPtr->HasFocus = FALSE;
1679 infoPtr->MouseDownID = -1;
1680 infoPtr->TextColor = GetSysColor(COLOR_WINDOWTEXT);
1681 infoPtr->LinkColor = GetSysColor(COLOR_HIGHLIGHT);
1682 infoPtr->VisitedColor = GetSysColor(COLOR_HIGHLIGHT);
1683 infoPtr->BreakChar = ' ';
1684 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd);
1685 SYSLINK_SetText(infoPtr, ((LPCREATESTRUCTW)lParam)->lpszName);
1689 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd);
1690 SYSLINK_ClearDoc(infoPtr);
1691 if(infoPtr->Font != 0) DeleteObject(infoPtr->Font);
1692 if(infoPtr->LinkFont != 0) DeleteObject(infoPtr->LinkFont);
1693 SetWindowLongPtrW(hwnd, 0, 0);
1698 HandleDefaultMessage:
1699 if ((message >= WM_USER) && (message < WM_APP))
1701 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam );
1703 return DefWindowProcW(hwnd, message, wParam, lParam);
1708 /***********************************************************************
1709 * SYSLINK_Register [Internal]
1711 * Registers the SysLink window class.
1713 VOID SYSLINK_Register (void)
1717 ZeroMemory (&wndClass, sizeof(wndClass));
1718 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1719 wndClass.lpfnWndProc = SysLinkWindowProc;
1720 wndClass.cbClsExtra = 0;
1721 wndClass.cbWndExtra = sizeof (SYSLINK_INFO *);
1722 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1723 wndClass.lpszClassName = WC_LINK;
1725 RegisterClassW (&wndClass);
1729 /***********************************************************************
1730 * SYSLINK_Unregister [Internal]
1732 * Unregisters the SysLink window class.
1734 VOID SYSLINK_Unregister (void)
1736 UnregisterClassW (WC_LINK, NULL);