Home About

January 2nd, 2009

Creating a GTK notification area icon using Mono and C# - 3

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Adding a Gnome tooltip to your application with C# and Mono

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 class to register your own icon in the notification area.

    StatusIcon  myStatusIcon;

You can provide and load your own icon, but for this example I used one of the GTK stock icons.

     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

Adding or changing a tooltip is as straighforward as assigning it:

      myStatusIcon.Tooltip = "Hello World ToolTip";                      // The message to show when the mouse hovers over the icon

To create a rightclick menu we need to register the PopupMenu event handler:

     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("About Hello World");
        helloItem.Show();
        helloItem.Activated += new EventHandler(OnHelloAboutActivated);

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

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:


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 = "Hello World ToolTip";            // 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("Hello World");
		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("About Hello World");
		helloItem.Show();
		helloItem.Activated += new EventHandler(OnHelloAboutActivated);

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

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

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

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

		aboutDialog.Run();
	}

	/* Catch the "Close" and "X" 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();
	}

}
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google
  • Reddit

Tags: , , , ,

3 Responses to “Creating a GTK notification area icon using Mono and C#”

  1. Ralf Kistner Says:

    Thanks for the tutorial.

    However, I would like to add my own icon. How would this be accomplished? What format should the icon be in? Can the icon be stored inside the exe file?

  2. Martijn Says:

    Hi Ralf,

    You can use a Gdk.Pixbuf if you would just like to load an external image (Pixbuf will accept (png, tiff, jpg, gif, xpm, pcx, ico, xpm, xbm)

    using Gdk;
    myStatusIcon = new StatusIcon(new Gdk.Pixbuf(“/home/martijn/Desktop/images.jpeg”));

    I haven’t yet tried to distribute this in an exe file, so I can’t help you there.

    Cheers,
    Martijn

  3. Anthony Says:

    Hi Martin,

    On Windows the notification icon hangs around after the application quits, at least until you mouse over it. You can fix this behaviour with the following small change to your DeleteEvent handler:

    protected void OnDeleteEvent(object sender, DeleteEventArgs a)
    {
    myStatusIcon.Visible = false; // <– hide icon before quitting
    Application.Quit();
    a.RetVal = true;
    }

    Kind regards,
    Anthony.

Leave a Reply


Recent Comments
  • Ales: Hi, Thanks for the code… I must say I did not experience any errors decrypting any of my messages. I even...
  • JC: Thanks very useful and well explained
  • Thomas: This is a public static class written in the C# language that does not save state. You can call into the...
  • Simon: Thank you very much for this post! It helped me essentially to overcome obstacels to work with mono!
  • Graham: This is a good research for keyboard shortcuts! Some shortcuts are also compatible for Windows OS. I have...