Home About

January 2nd, 2009

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

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: , , , ,

2 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

Leave a Reply


Recent Comments
  • Hakbor: This works fine. But to get the NUnit to use my current tests (and not the old ones) , it is not sufficient...
  • Alberto: Your plugin is very useful; I installed it on several different blogs I manage and I’m very happy with...
  • Nelson: Saved me from doing it myself. Good article.
  • andy: i am currently playing taiwanese server wow in 奈辛瓦里(PVP) and i would like to realm transfer to...
  • berties: any english speaking playing on a taiwanese server?