1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
|
/*
Gtk2 SystemTray NotificationIcon starter example
Copyright (C) 2006 James Scott, Jr. <skoona@users.sourceforge.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* Name of package */
#define PACKAGE "gtkstatusicon-starter-program"
/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT "James Scott Jr. skoona@users.sourceforge.net"
/* Define to the full name of this package. */
#define PACKAGE_NAME "GtkStatusIcon Starter Program"
/* Define to the full name and version of this package. */
#define PACKAGE_STRING "GtkStatusIcon Starter Program 0.1.0"
/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "gtkstatusicon-starter-program"
/* Define to the version of this package. */
#define PACKAGE_VERSION "0.1.0"
/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1
/* Version number of package */
#define VERSION "0.1.0"
/* Enable GNU extensions on systems that have them. */
#ifndef _GNU_SOURCE
# define _GNU_SOURCE 1
#endif
#include <glib.h>
#include <gtk/gtk.h>
#include <stdlib.h> /* EXIT_SUCCESS EXIT_FAILURE */
#define GSI_WINDOW_TITLE "gsi: GtkStatusIcon Example"
#define GSI_MONITOR_MAX 8
#define GSI_MAX_ARRAY 512
#define GSI_REG_STRING 256
#define GSI_ICON_SIZE 24
/*
* Command line params
*/
static gint gd_b_debug = 0;
static gint gd_b_version = 0;
/*
* For Logging info messages
* GtkTextView widget
*/
GtkWidget *g_text_view = NULL;
typedef enum _State_Icons_IDs {
GSI_DEFAULT,
GSI_SKOONADEVEL,
GSI_N_ICONS
} GSI_IconType;
typedef struct _System_Control_Data {
guint cb_id;
GtkWindow *window; /* Main Window */
GtkWidget *evbox; /* used to share popups on main window */
gboolean b_visible; /* is the info window visible */
gboolean b_use_systray; /* should we use the tray icon */
GtkStatusIcon *tray_icon; /* the status icon */
gboolean tray_visible_flag; /* is the icon is visible */
gboolean tray_embedded_flag; /* is the icon viewable by user ? */
GtkWidget *tray_menu; /* Popup Menu */
gint tray_icon_size; /* pixel size of icon (normally 24) */
GdkPixbuf *my_icons[GSI_N_ICONS];
guint i_icon_index[GSI_MONITOR_MAX];
gint i_old_icon_index[GSI_MONITOR_MAX];
} GSI_CONFIG, *PGSI_CONFIG;
/*
* Start Local Functions
*/
static void cb_systray_popup_menu_response_exit (GtkWidget * widget, gpointer gp);
static void cb_systray_popup_menu_response_about (GtkWidget * widget, gpointer gp);
static void cb_systray_popup_menu_response_change (GtkWidget * widget, gpointer gp);
static gboolean gsi_systray_popup_menu_create (PGSI_CONFIG pgsi);
static void cb_systray_icon_handle_activate (GtkStatusIcon *status_icon, gpointer gp);
static void cb_systray_icon_handle_popup (GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer gp);
static void cb_systray_icon_handle_notify (GObject *gobject, GParamSpec *arg1, gpointer gp);
static gboolean gsi_systray_icon_create (gpointer gp);
static gboolean gsi_systray_icon_remove (gpointer gp);
static void cb_main_interface_controls_clicked (GtkButton *button, PGSI_CONFIG pgsi);
static void cb_main_interface_show (GtkWidget * widget, PGSI_CONFIG pgsi);
static void cb_main_interface_hide (GtkWidget * widget, PGSI_CONFIG pgsi);
static gboolean cb_main_interface_clicked (GtkWidget * widget, GdkEventButton * event, gpointer gp);
static gboolean cb_main_interface_delete_event (GtkWidget * w, GdkEvent * event, gpointer gp);
static void cb_main_interface_destroy (GtkWidget * widget, PGSI_CONFIG pgsi);
static gint gsi_main_interface_parse_args (gint argc, gchar ** argv,GOptionContext **context);
static GtkWindow *gsi_main_interface_create (PGSI_CONFIG pgsi);
static GtkWidget *utils_scrolled_textview(GtkWidget *box, GtkPolicyType h_policy, GtkPolicyType v_policy);
static void utils_textview_append(GtkWidget *view, gchar *log_message);
static void utils_log_msg (gchar * pch_func, gchar * pch_topic, gchar * pch_emsg);
static gboolean utils_icons_load (PGSI_CONFIG pgsi);
extern int main (int argc, char *argv[]);
/*
* End local Functions
*/
static void cb_main_interface_controls_clicked (GtkButton *button, PGSI_CONFIG pgsi)
{
GtkWidget *widget = NULL;
gchar *pch = NULL;
g_return_if_fail (pgsi != NULL);
widget = GTK_WIDGET(g_object_get_data(G_OBJECT(button), "B-PEER"));
pch = (gchar *)g_object_get_data(G_OBJECT(button), "B-ACTION");
/*
* Manage the Icons Visibility
*/
if ( pgsi->tray_visible_flag ) {
gsi_systray_icon_remove (pgsi);
if ( g_str_equal(pch, "Remove")) {
gtk_widget_set_sensitive(widget, TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE);
} else {
gtk_widget_set_sensitive(widget, FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(button), TRUE);
}
} else {
gsi_systray_icon_create (pgsi);
if ( g_str_equal(pch, "Add")) {
gtk_widget_set_sensitive(widget, TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE);
} else {
gtk_widget_set_sensitive(widget, FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(button), TRUE);
}
}
utils_log_msg ((gchar *)__func__, "Button Clicked", (gchar *)g_object_get_data(G_OBJECT(button), "B-ACTION") );
return;
}
/*
* Menu Callback : Process PopUp menu commands
*/
static void cb_systray_popup_menu_response_change (GtkWidget * widget, gpointer gp)
{
PGSI_CONFIG pgsi = gp;
g_return_if_fail (gp != NULL);
pgsi->b_use_systray = !pgsi->b_use_systray;
if ( pgsi->b_use_systray ) {
gsi_systray_icon_remove (pgsi);
} else {
gsi_systray_icon_create (pgsi);
}
utils_log_msg ("cb_systray_popup_menu_response_change", "Changed Host Value", pgsi->b_use_systray ? "Destroyed" : "Created" );
return;
}
static void cb_systray_popup_menu_response_exit (GtkWidget * widget, gpointer gp)
{
PGSI_CONFIG pgsi = gp;
g_return_if_fail (gp != NULL);
utils_log_msg ("cb_systray_popup_menu_response_exit", "Enter", "top");
gtk_widget_destroy (GTK_WIDGET (pgsi->window)); /* this will cause everything to shutdown */
return;
}
static void cb_systray_popup_menu_response_about (GtkWidget * widget, gpointer gp)
{
gchar *website = "http://mysite.verizon.net/ressgdw8/";
gchar *website_label = "Skoona's Development Resources";
gchar *authors[] = {
"James Scott, Jr. <skoona@users.sourceforge.net>",
NULL
};
g_return_if_fail (gp != NULL);
utils_log_msg ("cb_systray_popup_menu_response_about", "Enter", "top");
gtk_show_about_dialog (NULL,
"name", "About",
"program-name", GSI_WINDOW_TITLE,
"version", PACKAGE_VERSION,
"copyright", "\xC2\xA9 2006 James Scott,Jr. (GPL)",
"comments", "System Tray Notification Icons Example",
"authors", authors,
"website", website,
"website-label", website_label,
"logo", ((PGSI_CONFIG)gp)->my_icons[GSI_SKOONADEVEL],
NULL);
return;
}
static gboolean gsi_systray_popup_menu_create (PGSI_CONFIG pgsi)
{
GtkWidget *menu = NULL, *menu_item = NULL;
g_return_val_if_fail (pgsi != NULL, FALSE);
utils_log_msg ("gsi_systray_popup_menu_create", "Enter", "top");
if (pgsi->tray_menu) {
return TRUE;
}
pgsi->tray_menu = menu = gtk_menu_new ();
menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_ABOUT, NULL);
g_signal_connect (G_OBJECT (menu_item), "activate",
G_CALLBACK (cb_systray_popup_menu_response_about), pgsi);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
gtk_widget_show (menu_item);
menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_APPLY, NULL);
g_signal_connect (G_OBJECT (menu_item), "activate",
G_CALLBACK (cb_systray_popup_menu_response_change), pgsi);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
gtk_widget_show (menu_item);
menu_item = gtk_separator_menu_item_new ();
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
gtk_widget_show (menu_item);
menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_QUIT, NULL);
g_signal_connect (G_OBJECT (menu_item), "activate",
G_CALLBACK (cb_systray_popup_menu_response_exit), pgsi);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
gtk_widget_show (menu_item);
gtk_widget_show (menu);
return TRUE;
}
/*
* GtkStatusIcon Callbacks
*/
/*
* activate
*/
static void cb_systray_icon_handle_activate (GtkStatusIcon *status_icon, gpointer gp)
{
PGSI_CONFIG pgsi = gp;
gchar ch_buf[256];
g_return_if_fail (pgsi != NULL);
g_snprintf(ch_buf, sizeof(ch_buf), "gobject => %p", (gpointer)status_icon);
utils_log_msg ("cb_systray_icon_handle_activate", "Enter", ch_buf);
if (pgsi->window == NULL)
{
return;
}
if (pgsi->b_visible)
{
gtk_widget_hide (GTK_WIDGET(pgsi->window));
}
else
{
gtk_widget_show_all (GTK_WIDGET(pgsi->window));
gtk_window_present (pgsi->window);
}
return;
}
/*
* popup-menu
*/
static void cb_systray_icon_handle_popup (GtkStatusIcon *status_icon, guint button, guint activate_time, gpointer gp)
{
PGSI_CONFIG pgsi = gp;
gchar ch_buf[256];
g_return_if_fail (pgsi != NULL);
g_snprintf(ch_buf, sizeof(ch_buf), "gobject => %p", (gpointer)status_icon);
utils_log_msg ("cb_systray_icon_handle_popup", "Enter", ch_buf);
if (pgsi->window == NULL)
{
return;
}
if (pgsi->tray_menu != NULL)
{
gtk_menu_popup (GTK_MENU (pgsi->tray_menu), NULL, NULL, NULL, gp, button, activate_time);
}
return;
}
/*
* notify
* ::size
* ::embedded
* ::visible
*/
static void cb_systray_icon_handle_notify (GObject *gobject, GParamSpec *arg1, gpointer gp)
{
PGSI_CONFIG pgsi = gp;
gboolean b_value = FALSE;
gchar ch_buf[256];
g_return_if_fail (pgsi != NULL);
utils_log_msg ("cb_systray_icon_handle_notify", "Notify", arg1->name );
if ( g_str_equal(arg1->name,"size") )
{
pgsi->tray_icon_size = gtk_status_icon_get_size(pgsi->tray_icon);
g_snprintf(ch_buf, sizeof(ch_buf), "%p=>%d", (gpointer)gobject, pgsi->tray_icon_size );
}
if ( g_str_equal(arg1->name,"embedded") )
{
pgsi->tray_embedded_flag = b_value = gtk_status_icon_is_embedded(pgsi->tray_icon);
if (b_value)
pgsi->tray_visible_flag = TRUE;
g_snprintf(ch_buf, sizeof(ch_buf), "%p=>%s", (gpointer)gobject, b_value ? "True" : "False");
}
if ( g_str_equal(arg1->name,"visible") )
{
pgsi->tray_visible_flag = b_value = gtk_status_icon_get_visible(pgsi->tray_icon);
g_snprintf(ch_buf, sizeof(ch_buf), "%p=>%s", (gpointer)gobject, b_value ? "True" : "False");
}
utils_log_msg ("cb_systray_icon_handle_notify", arg1->name, ch_buf );
return;
}
static gboolean gsi_systray_icon_create (gpointer gp)
{
PGSI_CONFIG pgsi = gp;
gchar *pch_title = NULL;
g_return_val_if_fail (pgsi != NULL, FALSE);
utils_log_msg ("gsi_systray_icon_create", "Enter", "top");
/* Don't create it if it should not be on
*/
if (!pgsi->b_use_systray)
{
return FALSE;
}
/* If it exist just make it visible
*/
if (pgsi->tray_icon != NULL)
{
gtk_status_icon_set_visible(pgsi->tray_icon, TRUE);
return TRUE;
}
/* Else finally create a new one
*/
pgsi->tray_icon = gtk_status_icon_new_from_pixbuf(pgsi->my_icons[GSI_SKOONADEVEL]);
g_return_val_if_fail (pgsi->tray_icon != NULL, FALSE);
/* sink its ref so we can delete it later
*/
g_object_ref_sink (G_OBJECT(pgsi->tray_icon));
/* setup the signals to track it
*/
g_signal_connect (pgsi->tray_icon, "activate",
G_CALLBACK (cb_systray_icon_handle_activate), gp);
g_signal_connect (pgsi->tray_icon, "popup-menu",
G_CALLBACK (cb_systray_icon_handle_popup), gp);
g_signal_connect (pgsi->tray_icon, "notify::embedded",
G_CALLBACK (cb_systray_icon_handle_notify), gp);
g_signal_connect (pgsi->tray_icon, "notify::visible",
G_CALLBACK (cb_systray_icon_handle_notify), gp);
g_signal_connect (pgsi->tray_icon, "notify::size",
G_CALLBACK (cb_systray_icon_handle_notify), gp);
/* Attach a popup menu
*/
gsi_systray_popup_menu_create (pgsi);
/* Attach a new ToolTip message
*/
pch_title = g_strdup_printf ("I am SystemTray GtkStatusIcon Instance[%d]", pgsi->cb_id);
gtk_status_icon_set_tooltip(pgsi->tray_icon, pch_title);
g_free (pch_title);
return TRUE;
}
static gboolean gsi_systray_icon_remove (gpointer gp)
{
PGSI_CONFIG pgsi = gp;
g_return_val_if_fail (pgsi != NULL, FALSE);
utils_log_msg ("gsi_systray_icon_remove", "Enter", "top");
if (pgsi->tray_icon == NULL)
return FALSE;
/* Make it invisible anref it to destroy it
*/
gtk_status_icon_set_visible(pgsi->tray_icon, FALSE);
g_object_unref ( G_OBJECT(pgsi->tray_icon) );
pgsi->tray_icon = NULL;
return TRUE;
}
/*
* Manage Main window visibilty
*/
static void cb_main_interface_show (GtkWidget * widget, PGSI_CONFIG pgsi)
{
g_return_if_fail (pgsi != NULL);
utils_log_msg ("cb_main_interface_show", "Enter", "top");
pgsi->b_visible = TRUE;
}
static void cb_main_interface_hide (GtkWidget * widget, PGSI_CONFIG pgsi)
{
g_return_if_fail (pgsi != NULL);
utils_log_msg ("cb_main_interface_hide", "Enter", "top");
pgsi->b_visible = FALSE;
}
/*
* Prevent the normal window exit, just hide the window
*/
static gboolean cb_main_interface_delete_event (GtkWidget * w, GdkEvent * event,
gpointer gp)
{
PGSI_CONFIG pgsi = gp;
g_return_val_if_fail (pgsi, FALSE); /* error exit */
utils_log_msg ("cb_main_interface_delete_event", "Enter", "top");
if (pgsi->tray_visible_flag)
return gtk_widget_hide_on_delete (w);
return FALSE;
}
/*
* Handles cleanup for main window
*/
static void cb_main_interface_destroy (GtkWidget * widget, PGSI_CONFIG pgsi)
{
gint x = 0;
g_return_if_fail (pgsi != NULL);
utils_log_msg ("cb_main_interface_destroy", "Enter", "top");
gsi_systray_icon_remove (pgsi);
for (x = 0; x < GSI_N_ICONS; x++)
{
g_object_unref (pgsi->my_icons[x]);
}
gtk_main_quit ();
return;
}
/* Some of the main window core logic
*/
static gboolean cb_main_interface_clicked (GtkWidget * widget, GdkEventButton * event, gpointer gp)
{
PGSI_CONFIG pgsi = gp;
gchar *pch = NULL;
g_return_val_if_fail (pgsi != NULL, FALSE);
g_return_val_if_fail (widget != NULL, FALSE);
pch = g_strdup_printf ("button: %d", event->button);
utils_log_msg ("cb_main_interface_clicked", "Enter", pch);
g_free (pch);
if (pgsi->window == NULL)
{
return FALSE;
}
if (event->type != GDK_BUTTON_PRESS)
{
return FALSE;
}
switch (event->button)
{
case 1:
if ( widget != pgsi->evbox) {
if (pgsi->b_visible)
{
gtk_widget_hide (GTK_WIDGET(pgsi->window));
}
else
{
gtk_widget_show_all (GTK_WIDGET(pgsi->window));
gtk_window_present (pgsi->window);
}
}
return TRUE;
break;
case 2:
case 3:
if (pgsi->tray_menu != NULL)
{
gtk_menu_popup (GTK_MENU (pgsi->tray_menu), NULL, NULL, NULL, NULL,
event->button, event->time);
}
return TRUE;
break;
default:
return FALSE;
}
return FALSE;
}
/*
* Create a logging view
*/
static GtkWidget *utils_scrolled_textview(GtkWidget *box, GtkPolicyType h_policy, GtkPolicyType v_policy)
{
GtkWidget *scrolled = NULL,
*view = NULL;
scrolled = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), h_policy, v_policy);
gtk_container_add(GTK_CONTAINER(box), scrolled );
gtk_widget_show (scrolled);
view = gtk_text_view_new();
gtk_text_view_set_editable(GTK_TEXT_VIEW(view), TRUE);
gtk_container_add(GTK_CONTAINER(scrolled), view);
gtk_widget_show (view);
return view;
}
static void utils_textview_append(GtkWidget *view, gchar *log_message)
{
GtkTextIter iter;
GtkTextBuffer *buffer;
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
gtk_text_buffer_get_end_iter(buffer, &iter);
gtk_text_buffer_insert(buffer, &iter, log_message, -1);
}
/*
* Creates the main application window
*/
static GtkWindow *gsi_main_interface_create (PGSI_CONFIG pgsi)
{
GtkWidget *widget = NULL, *box = NULL, *button = NULL, *frame = NULL, *vbox =NULL;
GtkWindow *window = NULL;
GdkScreen *screen = NULL;
gchar *pch = NULL;
gint i_x = 0;
gint i_y = 0;
g_return_val_if_fail (pgsi != NULL, NULL);
pgsi->b_visible = FALSE;
pgsi->b_use_systray = TRUE;
pch = g_strdup_printf ("use_systray=%d", pgsi->b_use_systray);
utils_log_msg ("gsi_main_interface_create", "Enter", pch);
g_free (pch);
pch = NULL;
/* *********************************************************
* Create main window and display
*/
pgsi->window = window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
gtk_container_set_border_width (GTK_CONTAINER (window), 0);
gtk_window_set_title (GTK_WINDOW (window), GSI_WINDOW_TITLE);
/* gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK); */
gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_NONE);
gtk_window_set_destroy_with_parent (GTK_WINDOW (window), TRUE);
g_signal_connect (window, "destroy", G_CALLBACK (cb_main_interface_destroy), pgsi);
g_signal_connect (window, "delete-event", G_CALLBACK (cb_main_interface_delete_event), pgsi);
g_signal_connect (window, "show", G_CALLBACK (cb_main_interface_show), pgsi);
g_signal_connect (window, "hide", G_CALLBACK (cb_main_interface_hide), pgsi);
[cut at 20 KB] |