/*
* =====================================================================================
*
* Filename: ee_tip.cpp
*
* Description: 延时5秒提醒
*
* Version: 1.0
* Created: 2007年12月21日 11时11分53秒 CST
* Revision: none
* Compiler: gcc
*
* Author: wind (xihe), xihels@gmail.com
* Company: cyclone
*
* =====================================================================================
*/
#include <gtkmm.h>
#include <iostream>
using namespace std;
class MainWnd : public Gtk::Window {
public:
MainWnd(Glib::RefPtr<Gtk::StatusIcon> stat_,
int timeout,
Glib::ustring show_text_,
Glib::ustring image_file_path_) ;
bool on_stat_size_channged(int size);
bool on_button_press_event (GdkEventButton* event);
bool on_timeout();
private:
Glib::RefPtr<Gdk::Screen> screen;
Glib::RefPtr<Gtk::StatusIcon> stat;
Glib::ustring show_text;
Glib::ustring image_file_path;
};
MainWnd::MainWnd(Glib::RefPtr<Gtk::StatusIcon> stat_,
int timeout,
Glib::ustring show_text_,
Glib::ustring image_file_path_)
: Gtk::Window(Gtk::WINDOW_POPUP),
show_text(show_text_),
image_file_path(image_file_path_)
{
stat = stat_;
stat->signal_size_changed().connect(sigc::mem_fun(this, &MainWnd::on_stat_size_channged));
Gtk::HBox* hbox= Gtk::manage(new Gtk::HBox);
this->add(*hbox);
Gtk::Image* image;
if (image_file_path.empty())
image = Gtk::manage(
new Gtk::Image(Gtk::Stock::DIALOG_INFO,
Gtk::ICON_SIZE_DIALOG));
else
image = Gtk::manage(
new Gtk::Image(
Gdk::Pixbuf::create_from_file(
image_file_path)));
hbox->pack_start(*image, true, true);
//Glib::ustring text_str(" <span size='large' color='red' weight='bold'> " + show_text + " </span>");
Gtk::Label* text = Gtk::manage( new Gtk::Label(show_text));
text->set_use_markup(true);
hbox->pack_end(*text, true,true);
this->show_all();
Glib::signal_timeout().connect(sigc::mem_fun(*this, &MainWnd::on_timeout), timeout);
this->add_events(Gdk::BUTTON_PRESS_MASK);
}
bool MainWnd::on_button_press_event(GdkEventButton*)
{
Gtk::Main::quit();
return true;
}
bool MainWnd::on_stat_size_channged(int size)
{
//return false;
Gdk::Rectangle rect;
Gtk::Orientation orie;
int wx = 0;
int wy = 0;
int ww = 0;
int wh = 0;
this->get_size(ww, wh);
if (stat->get_geometry(screen, rect, orie)) {
if (orie == Gtk::ORIENTATION_HORIZONTAL) { // 面板是水平的
if (rect.get_y() < screen->get_height()/2) // 面板在上面
wy = rect.get_y() + size;
else
wy = rect.get_y() - wh;
wx = rect.get_x() - size;
if ((wx + ww) > screen->get_width())
wx -= wx + ww - screen->get_width();
if (wx < 0)
wx = 0;
} else {
if (rect.get_x() < screen->get_width()/2) // 面板在左边
wx = rect.get_x() + size;
else
wx = rect.get_x() - ww;
wy = rect.get_y() - size;
}
this->move(wx, wy);
}
return false;
}
bool MainWnd::on_timeout()
{
Gtk::Main::quit();
return true;
}
void on_active()
{
Gtk::Main::quit();
}
int main(int argc, char* argv[])
{
//-title -text -img
Gtk::Main kit(argc, argv);
string text("写点什么?");
string image;
char* pstr;
int timeout = 5000;
for(int i = 1; i < argc; i++) {
if (!strcmp("-text", argv[i])) {
pstr = argv[++i];
if (!pstr)
break;
text.assign(pstr);
} else if (!strcmp("-image", argv[i])) {
pstr = argv[++i];
if (!pstr)
break;
image.assign(pstr);
} else if (!strcmp("-timeout", argv[i])) {
pstr = argv[++i];
if (!pstr)
break;
timeout = atoi(pstr);
timeout = timeout > 0 ? timeout : 5000;
} else {
cout << argv[0] << " " << endl;
cout << "-text 提示内容" << endl;
cout << "-image 图标全路径" << endl;
cout << "-timeout 延时的毫秒数" << endl;
exit(127);
}
}
try {
Glib::RefPtr<Gtk::StatusIcon> stat;
if (image.empty())
stat =
Gtk::StatusIcon::create(Gtk::Stock::DIALOG_INFO);
else
stat =
Gtk::StatusIcon::create(
Gdk::Pixbuf::create_from_file(image));
stat->set_blinking();
stat->signal_activate().connect(sigc::ptr_fun(on_active));
MainWnd * main_wnd = new MainWnd(stat, timeout, text, image);
kit.run(*main_wnd);
} catch (const Glib::Exception& e) {
cout << e.what() << endl;
exit(127);
}
return 0;
}