Skip to content

Commit 6f78232

Browse files
committed
Add basic HelpTopic support
1 parent 0a799aa commit 6f78232

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

Source/ExcelDna.IntelliSense.Host/MyFunctions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ namespace ExcelDna.CustomAddin
88
// These functions - are just here for testing...
99
public class MyFunctions
1010
{
11-
[ExcelFunction(Description = "Returns the sum of two particular numbers that are given\r\n(As a test, of course)")]
11+
[ExcelFunction(Description = "Returns the sum of two particular numbers that are given\r\n(As a test, of course)",
12+
HelpTopic = "http://www.google.com")]
1213
public static double AddThem(
1314
[ExcelArgument(Name = "Augend", Description = "is the first number, to which will be added")] double v1,
1415
[ExcelArgument(Name = "Addend", Description = "is the second number that will be added")] double v2)
1516
{
1617
return v1 + v2;
1718
}
1819

19-
[ExcelFunction(Description = "--------------------")]
20+
[ExcelFunction(Description = "--------------------",
21+
HelpTopic = "MyFile.chm!100")]
2022
public static double AdxThem(
2123
[ExcelArgument(Name = "tag", Description = "is the first number, to which will be added")] double v1,
2224
[ExcelArgument(Name = "Addend", Description = "is the second number that will be added")] double v2)

Source/ExcelDna.IntelliSense/FormattedText.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ class TextRun
7070
{
7171
public string Text { get; set; }
7272
public FontStyle Style { get; set; }
73-
// CONSIDER: Maybe allow links?
73+
public string LinkAddress { get; set; }
74+
public bool IsLink { get { return !string.IsNullOrEmpty(LinkAddress); } }
7475
}
7576

7677
}

Source/ExcelDna.IntelliSense/IntelliSenseDisplay.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ IEnumerable<TextLine> GetFunctionDescriptionOrNull(FunctionInfo functionInfo)
344344

345345
FormattedText GetFunctionIntelliSense(FunctionInfo functionInfo, int currentArgIndex)
346346
{
347-
var nameLine = new TextLine { new TextRun { Text = functionInfo.Name + "(" } };
347+
var nameLine = new TextLine { new TextRun { Text = functionInfo.Name, LinkAddress = FixHelpTopic(functionInfo.HelpTopic) } };
348+
nameLine.Add(new TextRun { Text = "(" });
348349
if (functionInfo.ArgumentList.Count > 0)
349350
{
350351
var argNames = functionInfo.ArgumentList.Take(currentArgIndex).Select(arg => arg.Name).ToArray();
@@ -427,6 +428,14 @@ public void Dispose()
427428
_syncContextMain = null;
428429
}
429430

431+
// Removes the !0 that we add to make Excel happy
432+
string FixHelpTopic(string helpTopic)
433+
{
434+
if (helpTopic != null && helpTopic.EndsWith("!0"))
435+
return helpTopic.Substring(0, helpTopic.Length - 2);
436+
return helpTopic;
437+
}
438+
430439
// TODO: Think about case again
431440
// TODO: Consider locking...
432441
public void RegisterFunctionInfo(FunctionInfo functionInfo)

Source/ExcelDna.IntelliSense/ToolTipForm.cs

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace ExcelDna.IntelliSense
1010
{
11+
// CONSIDER: Maybe some ideas from here: http://codereview.stackexchange.com/questions/55916/lightweight-rich-link-label
1112

1213
// TODO: Drop shadow: http://stackoverflow.com/questions/16493698/drop-shadow-on-a-borderless-winform
1314
class ToolTipForm : Form
@@ -19,10 +20,14 @@ class ToolTipForm : Form
1920
int _left;
2021
int _top;
2122
Brush _textBrush;
23+
Brush _linkBrush;
2224
Pen _borderPen;
2325
Pen _borderLightPen;
24-
private ToolTip tipDna;
26+
ToolTip tipDna;
2527
Dictionary<FontStyle, Font> _fonts;
28+
Rectangle _linkRect;
29+
bool _linkActive;
30+
string _linkAddress;
2631

2732
public ToolTipForm(IntPtr hwndOwner)
2833
{
@@ -40,6 +45,7 @@ public ToolTipForm(IntPtr hwndOwner)
4045

4146
};
4247
_textBrush = new SolidBrush(Color.FromArgb(68, 68, 68));
48+
_linkBrush = new SolidBrush(Color.Blue);
4349
_borderPen = new Pen(Color.FromArgb(195, 195, 195));
4450
_borderLightPen = new Pen(Color.FromArgb(225, 225, 225));
4551
//Win32Helper.SetParent(this.Handle, hwndOwner);
@@ -144,6 +150,7 @@ protected override bool ShowWithoutActivation
144150

145151
// Sometimes has Invalid Handle error when calling base.CreateParams (called from Invalidate() for some reason)
146152
CreateParams _createParams;
153+
147154
protected override CreateParams CreateParams
148155
{
149156
get
@@ -199,11 +206,32 @@ protected override void OnPaint(PaintEventArgs e)
199206
int lineHeight = 16;
200207
foreach (var run in line)
201208
{
202-
var font = _fonts[run.Style];
209+
// We support only a single link, for now
210+
211+
Font font;
212+
Brush brush;
213+
if (run.IsLink && _linkActive)
214+
{
215+
font = _fonts[FontStyle.Underline];
216+
brush = _linkBrush;
217+
}
218+
else
219+
{
220+
font = _fonts[run.Style];
221+
brush = _textBrush;
222+
}
223+
203224
// TODO: Empty strings are a problem....
204225
var text = run.Text == "" ? " " : run.Text;
205226

206-
DrawString(e.Graphics, _textBrush, ref layoutRect, out textSize, format, text, font);
227+
DrawString(e.Graphics, brush, ref layoutRect, out textSize, format, text, font);
228+
229+
if (run.IsLink)
230+
{
231+
_linkRect = new Rectangle(layoutRect.X - textSize.Width, layoutRect.Y, textSize.Width, textSize.Height);
232+
_linkAddress = run.LinkAddress;
233+
}
234+
207235
totalWidth += textSize.Width;
208236
lineHeight = Math.Max(lineHeight, textSize.Height);
209237
}
@@ -291,6 +319,8 @@ void InitializeComponent()
291319
this.Name = "ToolTipForm";
292320
this.ShowInTaskbar = false;
293321
this.tipDna.SetToolTip(this, "IntelliSense by Excel-DNA");
322+
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.ToolTipForm_MouseClick);
323+
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.ToolTipForm_MouseMove);
294324
this.ResumeLayout(false);
295325

296326
}
@@ -308,5 +338,36 @@ public Win32Window(IntPtr handle)
308338
Handle = handle;
309339
}
310340
}
341+
342+
void ToolTipForm_MouseMove(object sender, MouseEventArgs e)
343+
{
344+
var inLink = _linkRect.Contains(e.Location);
345+
if ((inLink && !_linkActive) ||
346+
(!inLink && _linkActive))
347+
{
348+
_linkActive = !_linkActive;
349+
Invalidate();
350+
}
351+
}
352+
353+
void ToolTipForm_MouseClick(object sender, MouseEventArgs e)
354+
{
355+
var inLink = _linkRect.Contains(e.Location);
356+
if (inLink)
357+
{
358+
LaunchLink(_linkAddress);
359+
}
360+
}
361+
362+
void LaunchLink(string address)
363+
{
364+
// Check :
365+
// http://stackoverflow.com/questions/11076952/open-chm-file-at-specific-page-topic-using-command-line-arguments
366+
// http://stackoverflow.com/questions/27556808/excel-2010-help-on-this-function-does-not-launch-hh-exe-with-mapid-parameter/27571805#27571805
367+
// http://www.help-info.de/en/Help_Info_HTMLHelp/hh_command.htm
368+
// https://support.microsoft.com/en-us/kb/224816
369+
370+
Process.Start(address);
371+
}
311372
}
312373
}

0 commit comments

Comments
 (0)