<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martijn's C# Programming Blog &#187; GTK</title>
	<atom:link href="http://www.dijksterhuis.org/category/csharp/gtk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Showing Gnome/Ubuntu LibNotify Notifications from Mono and C#</title>
		<link>http://www.dijksterhuis.org/showing-gnomeubuntu-libnotify-notifications-mono/</link>
		<comments>http://www.dijksterhuis.org/showing-gnomeubuntu-libnotify-notifications-mono/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 03:40:04 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[GTK]]></category>
		<category><![CDATA[Learn C#]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=445</guid>
		<description><![CDATA[
In an earlier post I looked at how I could add my own icon to the Gnome Notification area from my Mono C# applications.  The next thing I wanted to do was to show a notification in the taskbar to the user when something truly important happens. If you are running Ubuntu Intrepid (8.10) [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2009/01/notification_sample.png" alt="Gnome / Ubuntu Notification displayed using Mono / C#" title="Gnome / Ubuntu Notification displayed using Mono / C#" width="316" height="102" class="alignnone size-full wp-image-444" /></p>
<p><em><a href="http://www.dijksterhuis.org/creating-gtk-notification-area-icon-mono/">In an earlier post</a> I looked at how I could add my own icon to the Gnome Notification area from my Mono C# applications.  The next thing I wanted to do was to show a notification in the taskbar to the user when something truly important happens. If you are running Ubuntu Intrepid (8.10) then its easy to create them from Mono. As I found out, Hoary (8.04) doesn&#8217;t include the required libraries.</em></p>
<p><span id="more-445"></span></p>
<p>The standard Mono distribution does not include support for showing Notifications as this functionality is  not part of the Gnome standard distribution. Instead Ubuntu and others depend on <em>DBUS</em> and <em>libnotify</em> to deliver their messages. Sebastian Dröge has created the necessary bindings for Mono/C#  in <em>libnotify-sharp</em> and these are contained in the Debian/Ubuntu library <em>libnotify0.4-cil</em>.</p>
<p>To install them run: </p>
<pre class="brush: c#">
sudo apt-get install libnotify0.4-cil
</pre>
<p>There is also a documentation package called <em>monodoc-notify-sharp-manual</em> which you could install it is however completely empty.</p>
<p>To run the demo code (below) simply create a new empty GTK solution in MonoDevelop. You will need to include a reference to libnotify-sharp. In the &#8220;Solution&#8221; pane, right click &#8220;References&#8221; and add the &#8220;notify-sharp&#8221; binding as shown below.</p>
<p><a href="http://www.dijksterhuis.org/wp-content/uploads/2009/01/notification_sample_2.png"><img src="http://www.dijksterhuis.org/wp-content/uploads/2009/01/notification_sample_2-499x424.png" alt="Referencing Notify-Sharp from MonoDevelop" title="Referencing Notify-Sharp from MonoDevelop" width="499" height="424" class="alignnone size-medium wp-image-446" /></a></p>
<p>Creating a simple notification is straightforward:</p>
<pre class="brush: c#">
        using Notifications;
        ....
        Notification myNote = new Notification();

        // Use a stock icon, and set the title (summary) and message (body)
        myNote.IconName = Stock.Harddisk;
        myNote.Summary  = &quot;Harddisk fatal error&quot;;
        myNote.Body        = &quot;Internal moist levels critical, please man the pumps!&quot;;
</pre>
<p>Each notification has a title (called the summary) and a body. We can also optionally specify an icon and in the code above I have used a stock Gnome icon. </p>
<p>The &#8220;AddAction&#8221; method allows you to specify one or more actions for a user to click on when the notification is displayed.  As the notifications are only displayed for a very short period of time this is probably not such a good place if the required action is truly important.</p>
<pre class="brush: c#">
        // Add an action the user can take to remedy the situation
        myNote.AddAction(&quot;DoPump&quot;,&quot;Start Pump&quot;,OnDoSomething);
        myNote.AddAction(&quot;DoRun&quot;,&quot;Run!&quot;,OnDoSomething);
</pre>
<p>The first parameter is passed to the user specified handling function. The second is the name of the action as displayed on the notification.</p>
<p>There are three levels of urgency for our notifications: Low, Normal and Critical. The difference to your user is probably just in the color of the notification. </p>
<pre class="brush: c#">
        // The color of the notification changes depending on urgency
        myNote.Urgency = Urgency.Critical;
</pre>
<p>Finally we need to show the notification:</p>
<pre class="brush: c#">
        // Show the notification on the desktop
        myNote.Show();
</pre>
<p><strong>Wrapping up</strong></p>
<p>Unrelated to displaying notification messages from Mono I came across <a href="http://dambalah.com/2008/11/23/ubuntu-notifications-using-libnotify-bin/">a post by Luc Castera</a> showing how you can simply show these notifications from a shell script:</p>
<ul>
<li>Install libnotify-bin: sudo aptitude install libnotify-bin</li>
<li>From a script, call it: notify-send &#8216;title of notification&#8217; &#8216;body of notification&#8217;</li>
</ul>
<p><strong>Source Code</strong></p>
<p>If you created an empty GTK project from MonoDevelop then the following should replace MainWindow.CS:</p>
<pre class="brush: c#">
// MainWindow.cs created with MonoDevelop
// User: martijn at 10:04 AM 1/5/2009
//
// To change standard headers go to Edit-&gt;Preferences-&gt;Coding-&gt;Standard Headers
//
using System;
using Gtk;
using Notifications;

public partial class MainWindow: Gtk.Window
{
    public MainWindow (): base (Gtk.WindowType.Toplevel)
    {
        Build ();

        Button myButton = new Button(&quot;Send Notification&quot;);
        myButton.Clicked += OnButtonClickedEvent;
        myButton.Show();

        Add(myButton);

    }

    protected void OnDeleteEvent (object sender, DeleteEventArgs a)
    {
        Application.Quit ();
        a.RetVal = true;
    }

    protected void OnButtonClickedEvent (object sender, EventArgs a)
    {
        Notification myNote = new Notification();

        // Use a stock icon, and set the title (summary) and message (body)
        myNote.IconName = Stock.Harddisk;
        myNote.Summary  = &quot;Harddisk fatal error&quot;;
        myNote.Body     = &quot;Internal moist levels critical, please man the pumps!&quot;;

        // Add an action the user can take to remedy the situation
        myNote.AddAction(&quot;DoPump&quot;,&quot;Start Pump&quot;,OnDoSomething);
        myNote.AddAction(&quot;DoRun&quot;,&quot;Run!&quot;,OnDoSomething);

        // The color of the notification changes depending on urgency
        myNote.Urgency = Urgency.Critical;  

        // Show the notification on the desktop
        myNote.Show();

    }

    protected void OnDoSomething( object sender, ActionArgs args )
    {
        string TheResult = &quot;&quot;;

        switch((string)args.Action)
        {
            case &quot;DoPump&quot; : TheResult = &quot;Pumping failed, head for the hills!&quot;; break;
            case &quot;DoRun&quot;  : TheResult = &quot;Hiding is fine, come back later!&quot;; break;
            default:         TheResult = &quot;Not supposed to happen!&quot;; break;
        }

        MessageDialog myDialog = new MessageDialog(this,
                                                   DialogFlags.DestroyWithParent,
                                                   MessageType.Error,
                                                   ButtonsType.Ok,
                                                   TheResult );

        myDialog.Run();
        myDialog.Destroy();
    }
}
</pre>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/showing-gnomeubuntu-libnotify-notifications-mono/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating a GTK notification area icon using Mono and C#</title>
		<link>http://www.dijksterhuis.org/creating-gtk-notification-area-icon-mono/</link>
		<comments>http://www.dijksterhuis.org/creating-gtk-notification-area-icon-mono/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 08:14:59 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[GTK]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[StatusIcon]]></category>
		<category><![CDATA[Tooltip]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=435</guid>
		<description><![CDATA[
Cool (Linux) applications have their own icon in the notification area. To add my own icon in the Gnome notification area using Mono only took a few lines of code. In the post below I show how you can add your icon and associate a tooltip and right click menu to it.

GTK provides the StatusIcon [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.dijksterhuis.org/wp-content/uploads/2009/01/tooltip.png"><img src="http://www.dijksterhuis.org/wp-content/uploads/2009/01/tooltip.png" alt="Adding a Gnome tooltip to your application with C# and Mono" title="Adding a Gnome tooltip to your application with C# and Mono" width="500" height="57" class="alignnone size-full wp-image-436" /></a></p>
<p><em>Cool (Linux) applications have their own icon in the notification area. To add my own icon in the Gnome notification area using Mono only took a few lines of code. In the post below I show how you can add your icon and associate a tooltip and right click menu to it.</em></p>
<p><span id="more-435"></span></p>
<p>GTK provides the <em>StatusIcon</em> class to register your own icon in the notification area.</p>
<pre class="brush: c#">
    StatusIcon  myStatusIcon;
</pre>
<p>You can provide and load your own icon, but for this example I used one of the GTK stock icons.</p>
<pre class="brush: c#">
     myStatusIcon = StatusIcon.NewFromStock(Stock.Harddisk);  // I use a stock icon from Stock.*
                                                                                          // to avoid having to include an icon file
     myStatusIcon.Visible = true;                                             // Make sure we are displayed
</pre>
<p>Adding or changing a tooltip is as straighforward as assigning it:</p>
<pre class="brush: c#">
      myStatusIcon.Tooltip = &quot;Hello World ToolTip&quot;;                      // The message to show when the mouse hovers over the icon
</pre>
<p>To create a rightclick menu we need to register the PopupMenu event handler:</p>
<pre class="brush: c#">
     myStatusIcon.PopupMenu += OnStatusIconPopupMenu;         // Link in the right click popup menu
     ...
     ...

    /* The Status Popup menu is shown when the user right clicks on the toolbar icon */

    protected void OnStatusIconPopupMenu(object sender, EventArgs e)
    {
        Menu popupMenu = new Menu();

        MenuItem helloItem = new MenuItem(&quot;About Hello World&quot;);
        helloItem.Show();
        helloItem.Activated += new EventHandler(OnHelloAboutActivated);

        popupMenu.Append(helloItem);
        popupMenu.Popup(null,null,null,3,Gtk.Global.CurrentEventTime);
    }
</pre>
<p>And that is really all there is to it. I have included the complete code below. If you create a new GTK solution in MonoDevelop you will end up with two files, Main.cs and MainWindows.Cs. Replace the MainWindow.CS file with the content below: </p>
<pre class="brush: c#">

using System;
using Gtk;

public partial class MainWindow: Gtk.Window
{
	StatusIcon  myStatusIcon;
	AboutDialog aboutDialog;

	public MainWindow (): base (Gtk.WindowType.Toplevel)
	{

		/* The following lines create the notification area status icon */
		/* Make it visible and link in the right click popup menu       */ 

		myStatusIcon = StatusIcon.NewFromStock(Stock.Harddisk);  // I use a stock icon from Stock.*
		                                                         // to avoid having to include an icon file
		myStatusIcon.Tooltip = &quot;Hello World ToolTip&quot;;            // The message to show when the mouse hovers over the icon
		myStatusIcon.Visible = true;                             // Make sure we are displayed
		myStatusIcon.PopupMenu += OnStatusIconPopupMenu;         // Link in the right click popup menu 

		/* Not necessary */
		Label AppWindowLabel = new Label(&quot;Hello World&quot;);
		this.Add(AppWindowLabel);

		Build ();
	}

	/* Called when the main application closes */

	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}

	/* The Status Popup menu is shown when the user right clicks on the toolbar icon */

	protected void OnStatusIconPopupMenu(object sender, EventArgs e)
	{
		Menu popupMenu = new Menu();

		MenuItem helloItem = new MenuItem(&quot;About Hello World&quot;);
		helloItem.Show();
		helloItem.Activated += new EventHandler(OnHelloAboutActivated);

		popupMenu.Append(helloItem);
		popupMenu.Popup(null,null,null,3,Gtk.Global.CurrentEventTime);
	}

	/* If the user select the &quot;About Hello World&quot; menu option, we show the about dialog */

	protected void OnHelloAboutActivated(object sender, EventArgs e)
	{
		aboutDialog = new AboutDialog();

		aboutDialog.ProgramName = &quot;The About Dialog Popup Demo&quot;;
		aboutDialog.Version = &quot;1.0beta&quot;;
		aboutDialog.Comments = &quot;The best things in life are simple!&quot;;
		aboutDialog.License = &quot;Creative Commons&quot;;
		aboutDialog.Authors = new string[] { &quot;Martijn Dijksterhuis&quot; };
		aboutDialog.Website = &quot;http://www.dijksterhuis.org&quot;;
		aboutDialog.Response += new ResponseHandler(OnHelloAboutClose);

		aboutDialog.Run();
	}

	/* Catch the &quot;Close&quot; and &quot;X&quot; button event from the about dialog box */

	protected void OnHelloAboutClose(object sender, ResponseArgs e)
	{
		if (e.ResponseId==ResponseType.Cancel || e.ResponseId==ResponseType.DeleteEvent)
		 aboutDialog.Destroy();
	}

}
</pre>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/creating-gtk-notification-area-icon-mono/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
