GNOME Notification Area with C++
posted on: 2010-05-24 20:34:34
In this tutorial I will show you how to use the GNOME notification area in C++
The GNOME notification window is a powerful usability feature that allows developers to deliver subtle and effective notifications to the user. This feature is one that is utilised most often by messaging clients, especially the native GNOME ones such as pidgin and evolution. However some developers seem not to be taking advantage of it.
Though not all applications require instant user notification, and let's face it. How annoying would it be if all your applications were constantly bombarding you with notices. As the purpose of the notification feature is to be able to get the attention of the user, when they may be busy doing other things. And the main point being that you can give the user information and let them make the decision of what to do about it, without forcing them to make a decision when they may be preoccupied.
NOTE: when compiling the source code don't forget the -lnotify flag!!
The contents of notifyexample.h
#include <gtkmm.h>
#include <libnotify/notify.h>
class NotifyExample : public Gtk::Window
{
public:
NotifyExample();
protected:
void on_button_click();
private:
NotifyNotification * m_notification;
Gtk::VBox m_vBox;
Gtk::TextView m_textView;
Gtk::Button m_buttonBox;
Gtk::Button m_button;
};
The contents of notifyexample.cc
#include "notifyexample.h"
NotifyExample::NotifyExample() : m_button("Notify")
{
set_title("Dave Nicholas - Notification Example");
set_border_width(5);
set_default_size(400, 200);
this->add(m_vBox);
this->m_vBox.pack_start(m_textView);
this->m_vBox.pack_start(m_button, Gtk::PACK_SHRINK);
this->m_button.signal_clicked().connect(sigc::mem_fun(*this, &NotifyExample::on_button_click) );
show_all_children();
}
void NotifyExample::on_button_click()
{
notify_init("Notify Example");
Glib::RefPtr<Gtk::TextBuffer> refTextBuffer = this->m_textView.get_buffer();
Glib::ustring gString = refTextBuffer->get_text();
this->m_notification = notify_notification_new ("Notify Example", gString.c_str(), NULL, NULL);
notify_notification_set_timeout (m_notification, 2000);
notify_notification_show (m_notification, NULL);
g_object_unref(G_OBJECT(m_notification));
}
Here is the source code as usual. I hope you have found this tutorial useful as usual please forward me with any questions.















