Posted by: ilaksh | January 10, 2010

SharpDevelop hack to insert reddit (or any other web page) into a method definition

Practically speaking this currently is just kind of a novelty, due to the fact that the WPF frame uses IE (yuck) to display web pages.  But I think that it still demonstrates a more general and very interesting feature of Avalon Edit, which is the text editor component in SharpDevelop 4.

Daniel Grunwald is awesome for a lot of reasons, one of which is that he has set up his editor component so that you can insert any WPF control into the middle of your source code.

Here is a link to the page where he explains how that works: http://danielgrunwald.de/coding/AvalonEdit/rendering.php

Just as a test, I downloaded SharpDevelop 4 from SVN and modified this method definition from Libraries | ICSharpCode.AvalonEdit | Rendering | LinkElementGenerator.cs:


public override VisualLineElement ConstructElement(int offset)
 {
     Match m = GetMatch(offset);
     if (m.Success && m.Index == 0) {
         Uri uri = GetUriFromMatch(m);
         if (uri == null)
             return null;
         System.Windows.Controls.Frame frame = new System.Windows.Controls.Frame();
         frame.Width = 700;
         frame.Height = 300;
         frame.Source = uri;

         //VisualLineLinkText linkText = new VisualLineLinkText(CurrentContext.VisualLine, m.Length);
         //linkText.NavigateUri = uri;
         //linkText.RequireControlModifierForClick = this.RequireControlModifierForClick;
         el = new InlineObjectElement(30, frame);
         return el;
         //return linkText;
     } else {
         return null;
     }
 }

Now, when the editor sees a url, such as http://www.reddit.com, instead of just displaying it as an underlined link, it opens up a WPF frame pointed to that URL.  For example, a method definition might look completely normal when collapsed:

— but when you expand it:

This would work a lot better if we had something like a WebKit component instead of the WPF frame using IE.  That is actually possible, because you can put any WPF component in there.

What I really am thinking about are possibilities like displaying math equations in actual math notation, or editing dictionaries in table form, or formatting the text in comments, or quite a lot of other options.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: