aboutsummaryrefslogtreecommitdiff
path: root/src/lib/trayicon.c
blob: 200c9068073a2e7c6b8c67c637a57ca4b54a5685 (plain) (blame)
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
/*
     This file is part of GNUnet.
     (C) 2010, 2011 Christian Grothoff (and other contributing authors)

     GNUnet 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, or (at your
     option) any later version.

     GNUnet 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 GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file src/lib/trayicon.c
 * @brief trayicon support
 * @author Christian Grothoff
 */
#include "gnunet_gtk_config.h"
#include "gnunet_gtk.h"


/**
 * Our tray icon.
 */
static GtkStatusIcon *tray_icon;

/**
 * The main window.
 */
static GtkWindow *main_window;


/**
 * We got a click on our tray icon. Toggle visibility of the main
 * window.
 */
static void 
tray_icon_on_click(GtkStatusIcon *status_icon, 
		   gpointer user_data)
{
  if (gtk_window_is_active (main_window))
    gtk_widget_hide (GTK_WIDGET (main_window));
  else
    gtk_window_present (main_window);
}


/**
 * We got a right-click on the tray icon. Display the context
 * menu (which should have a 'quit' button).
 */
static int 
tray_icon_on_menu(GtkWidget *widget, 
		  GdkEvent *event,
		  gpointer user_data)
{
  GtkMenu *tray_menu;
  GdkEventButton *event_button;
  GtkBuilder *builder;
 
  if (event->type == GDK_BUTTON_PRESS)
    {
      event_button = (GdkEventButton *) event;      
      if (event_button->button == 3)
	{
	  builder = GNUNET_GTK_get_new_builder ("gnunet_gtk_status_bar_menu.glade");
	  tray_menu = GTK_MENU (gtk_builder_get_object (builder,
							"GNUNET_GTK_status_bar_popup_menu"));
	  gtk_menu_popup (tray_menu, NULL, NULL, NULL, NULL,
			  event_button->button, event_button->time);
	  g_object_unref (builder);
	}
    }
  return FALSE;
}


/**
 * Create our tray icon.
 *
 * @param main handle to the main window (show or hide)
 * @param icon_name name of the tray icon file
 * @param tooltip tooltip for the tray icon
 */
void
GNUNET_GTK_tray_icon_create (GtkWindow *main,
			     const char *icon_name,
			     const char *tooltip)
{
  if (NULL != tray_icon)
    {
      GNUNET_break (0);
      return;
    }
  main_window = main;
  tray_icon = gtk_status_icon_new();
  g_signal_connect(G_OBJECT(tray_icon), "activate", 
		   G_CALLBACK(tray_icon_on_click), NULL);
  g_signal_connect (G_OBJECT(tray_icon), 
		    "button_press_event",
		    G_CALLBACK(tray_icon_on_menu), 
		    tray_icon);
  gtk_status_icon_set_from_icon_name (tray_icon, 
				      icon_name);
  gtk_status_icon_set_tooltip(tray_icon, 
			      tooltip);
  gtk_status_icon_set_visible(tray_icon, TRUE); 
}


/**
 * Destroy the tray icon.
 */
void 
GNUNET_GTK_tray_icon_destroy ()
{
  if (tray_icon == NULL)
    return;
  g_object_unref (G_OBJECT (tray_icon));
  tray_icon = NULL;
  main_window = NULL;
}

/* end of trayicon.c */