summaryrefslogtreecommitdiff
path: root/tests/teststatusicon.c
blob: 467baf5dd5f70e624d3edb7ddb20925f76f4cc1f (plain)
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
/* gtkstatusicon-x11.c:
 *
 * Copyright (C) 2003 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors:
 *	Mark McLoughlin <mark@skynet.ie>
 */

#include <gtk/gtk.h>

typedef enum
{
  TEST_STATUS_FILE,
  TEST_STATUS_DIRECTORY
} TestStatus;

static TestStatus status = TEST_STATUS_FILE;
static gint timeout = 0;

static void
update_icon (GtkStatusIcon *status_icon)
{
  gchar *icon_name;
  gchar *tooltip;

  if (status == TEST_STATUS_FILE)
    {
      icon_name = "gnome-fs-regular";
      tooltip = "Regular File";
    }
  else
    {
      icon_name = "gnome-fs-directory";
      tooltip = "Directory";
    }

  gtk_status_icon_set_from_icon_name (status_icon, icon_name);
  gtk_status_icon_set_tooltip (status_icon, tooltip);
}

static gboolean
timeout_handler (gpointer data)
{
  GtkStatusIcon *icon = GTK_STATUS_ICON (data);

  if (status == TEST_STATUS_FILE)
    status = TEST_STATUS_DIRECTORY;
  else
    status = TEST_STATUS_FILE;

  update_icon (icon);

  return TRUE;
}

static void
blink_toggle_toggled (GtkToggleButton *toggle,
		      GtkStatusIcon   *icon)
{
  gtk_status_icon_set_blinking (icon, 
				gtk_toggle_button_get_active (toggle));
}

static void
visible_toggle_toggled (GtkToggleButton *toggle,
			GtkStatusIcon   *icon)
{
  gtk_status_icon_set_visible (icon, 
			       gtk_toggle_button_get_active (toggle));
}

static void
timeout_toggle_toggled (GtkToggleButton *toggle,
			GtkStatusIcon   *icon)
{
  if (timeout)
    {
      g_source_remove (timeout);
      timeout = 0;
    }
  else
    {
      timeout = g_timeout_add (2000, timeout_handler, icon);
    }
}

static void
icon_activated (GtkStatusIcon *icon)
{
  GtkWidget *dialog;
  GtkWidget *toggle;

  dialog = g_object_get_data (G_OBJECT (icon), "test-status-icon-dialog");
  if (dialog == NULL)
    {
      dialog = gtk_message_dialog_new (NULL, 0,
				       GTK_MESSAGE_QUESTION,
				       GTK_BUTTONS_CLOSE,
				       "You wanna test the status icon ?");

      g_object_set_data_full (G_OBJECT (icon), "test-status-icon-dialog",
			      dialog, (GDestroyNotify) gtk_widget_destroy);

      g_signal_connect (dialog, "response", 
			G_CALLBACK (gtk_widget_hide), NULL);
      g_signal_connect (dialog, "delete_event", 
			G_CALLBACK (gtk_widget_hide_on_delete), NULL);

      toggle = gtk_toggle_button_new_with_mnemonic ("_Show the icon");
      gtk_box_pack_end (GTK_BOX (GTK_DIALOG (dialog)->vbox), toggle, TRUE, TRUE, 6);
      gtk_widget_show (toggle);

      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
				    gtk_status_icon_get_visible (icon));
      g_signal_connect (toggle, "toggled", 
			G_CALLBACK (visible_toggle_toggled), icon);

      toggle = gtk_toggle_button_new_with_mnemonic ("_Blink the icon");
      gtk_box_pack_end (GTK_BOX (GTK_DIALOG (dialog)->vbox), toggle, TRUE, TRUE, 6);
      gtk_widget_show (toggle);

      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
				    gtk_status_icon_get_blinking (icon));
      g_signal_connect (toggle, "toggled", 
			G_CALLBACK (blink_toggle_toggled), icon);

      toggle = gtk_toggle_button_new_with_mnemonic ("_Change images");
      gtk_box_pack_end (GTK_BOX (GTK_DIALOG (dialog)->vbox), toggle, TRUE, TRUE, 6);
      gtk_widget_show (toggle);

      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
				    timeout != 0);
      g_signal_connect (toggle, "toggled", 
			G_CALLBACK (timeout_toggle_toggled), icon);
    }

  gtk_window_present (GTK_WINDOW (dialog));
}

static void
check_activated (GtkCheckMenuItem *item,
		 GtkStatusIcon    *icon)
{
  gtk_status_icon_set_blinking (icon, 
				gtk_check_menu_item_get_active (item));
}

static void 
popup_menu (GtkStatusIcon *icon,
	    guint          button,
	    guint32        activate_time)
{
  GtkWidget *menu, *menuitem;

  menu = gtk_menu_new ();

  menuitem = gtk_check_menu_item_new_with_label ("Blink");
  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), 
				  gtk_status_icon_get_blinking (icon));
  g_signal_connect (menuitem, "activate", G_CALLBACK (check_activated), icon);

  gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);

  gtk_widget_show (menuitem);

  gtk_menu_popup (GTK_MENU (menu), 
		  NULL, NULL, NULL, NULL, 
		  button, activate_time);
}

int
main (int argc, char **argv)
{
  GtkStatusIcon *icon;

  gtk_init (&argc, &argv);

  icon = gtk_status_icon_new ();
  update_icon (icon);

  gtk_status_icon_set_blinking (GTK_STATUS_ICON (icon), TRUE);

  g_signal_connect (icon, "activate",
		    G_CALLBACK (icon_activated), NULL);

  g_signal_connect (icon, "popup-menu",
		    G_CALLBACK (popup_menu), NULL);
 
  timeout = g_timeout_add (2000, timeout_handler, icon);

  gtk_main ();

  return 0;
}