teststatusicon.c 6.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21
 *
 * Authors:
 *	Mark McLoughlin <mark@skynet.ie>
 */

22
#define GDK_DISABLE_DEPRECATION_WARNINGS
23
#include <gtk/gtk.h>
24 25
#include <stdlib.h>

26 27
typedef enum
{
28 29
  TEST_STATUS_INFO,
  TEST_STATUS_QUESTION
30 31
} TestStatus;

32
static TestStatus status = TEST_STATUS_INFO;
33
static gint timeout = 0;
34
static GSList *icons = NULL;
35

Matthias Clasen's avatar
Matthias Clasen committed
36
static gboolean
37 38 39 40
size_changed_cb (GtkStatusIcon *icon,
		 int size)
{
  g_print ("status icon %p size-changed size = %d\n", icon, size);
Matthias Clasen's avatar
Matthias Clasen committed
41 42

  return FALSE;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
}

static void
embedded_changed_cb (GtkStatusIcon *icon)
{
  g_print ("status icon %p embedded changed to %d\n", icon,
	   gtk_status_icon_is_embedded (icon));
}

static void
orientation_changed_cb (GtkStatusIcon *icon)
{
  GtkOrientation orientation;

  g_object_get (icon, "orientation", &orientation, NULL);
  g_print ("status icon %p orientation changed to %d\n", icon, orientation);
}

61 62 63 64 65 66 67
static void
screen_changed_cb (GtkStatusIcon *icon)
{
  g_print ("status icon %p screen changed to %p\n", icon,
	   gtk_status_icon_get_screen (icon));
}

68
static void
69
update_icon (void)
70
{
71
  GSList *l;
72 73 74
  gchar *icon_name;
  gchar *tooltip;

75
  if (status == TEST_STATUS_INFO)
76
    {
77
      icon_name = "dialog-information";
Matthias Clasen's avatar
Matthias Clasen committed
78
      tooltip = "Some Information ...";
79 80 81
    }
  else
    {
82
      icon_name = "dialog-question";
83
      tooltip = "Some Question ...";
84 85
    }

86 87 88 89 90
  for (l = icons; l; l = l->next)
    {
      GtkStatusIcon *status_icon = l->data;

      gtk_status_icon_set_from_icon_name (status_icon, icon_name);
91
      gtk_status_icon_set_tooltip_text (status_icon, tooltip);
92
    }
93 94 95 96 97
}

static gboolean
timeout_handler (gpointer data)
{
98 99
  if (status == TEST_STATUS_INFO)
    status = TEST_STATUS_QUESTION;
100
  else
101
    status = TEST_STATUS_INFO;
102

103
  update_icon ();
104 105 106 107 108

  return TRUE;
}

static void
109
visible_toggle_toggled (GtkToggleButton *toggle)
110
{
111 112 113 114 115
  GSList *l;

  for (l = icons; l; l = l->next)
    gtk_status_icon_set_visible (GTK_STATUS_ICON (l->data),
                                 gtk_toggle_button_get_active (toggle));
116 117 118
}

static void
119
timeout_toggle_toggled (GtkToggleButton *toggle)
120 121 122 123 124 125 126 127
{
  if (timeout)
    {
      g_source_remove (timeout);
      timeout = 0;
    }
  else
    {
128
      timeout = gdk_threads_add_timeout (2000, timeout_handler, NULL);
129 130 131 132 133 134
    }
}

static void
icon_activated (GtkStatusIcon *icon)
{
135
  GtkWidget *content_area;
136 137 138 139 140 141 142 143 144 145 146
  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 ?");

147 148
      gtk_window_set_screen (GTK_WINDOW (dialog), gtk_status_icon_get_screen (icon));
      gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
149

150 151 152 153 154 155 156 157
      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);

158 159
      content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));

160
      toggle = gtk_toggle_button_new_with_mnemonic ("_Show the icon");
161
      gtk_box_pack_end (GTK_BOX (content_area), toggle, TRUE, TRUE, 6);
162 163 164 165 166
      gtk_widget_show (toggle);

      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
				    gtk_status_icon_get_visible (icon));
      g_signal_connect (toggle, "toggled", 
167
			G_CALLBACK (visible_toggle_toggled), NULL);
168 169

      toggle = gtk_toggle_button_new_with_mnemonic ("_Change images");
170
      gtk_box_pack_end (GTK_BOX (content_area), toggle, TRUE, TRUE, 6);
171 172 173 174 175
      gtk_widget_show (toggle);

      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
				    timeout != 0);
      g_signal_connect (toggle, "toggled", 
176
			G_CALLBACK (timeout_toggle_toggled), NULL);
177 178 179 180 181
    }

  gtk_window_present (GTK_WINDOW (dialog));
}

182
static void
183
do_quit (GtkMenuItem *item)
184
{
185 186 187 188 189 190 191 192 193 194 195 196 197
  GSList *l;

  for (l = icons; l; l = l->next)
    {
      GtkStatusIcon *icon = l->data;

      gtk_status_icon_set_visible (icon, FALSE);
      g_object_unref (icon);
    }

  g_slist_free (icons);
  icons = NULL;

198 199 200
  gtk_main_quit ();
}

201 202 203 204 205 206
static void
do_exit (GtkMenuItem *item)
{
  exit (0);
}

207 208 209 210 211 212 213 214 215
static void 
popup_menu (GtkStatusIcon *icon,
	    guint          button,
	    guint32        activate_time)
{
  GtkWidget *menu, *menuitem;

  menu = gtk_menu_new ();

216 217 218
  gtk_menu_set_screen (GTK_MENU (menu),
                       gtk_status_icon_get_screen (icon));

219
  menuitem = gtk_menu_item_new_with_label ("Quit");
220
  g_signal_connect (menuitem, "activate", G_CALLBACK (do_quit), NULL);
221 222 223 224 225

  gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);

  gtk_widget_show (menuitem);

226 227 228 229 230 231 232
  menuitem = gtk_menu_item_new_with_label ("Exit abruptly");
  g_signal_connect (menuitem, "activate", G_CALLBACK (do_exit), NULL);

  gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);

  gtk_widget_show (menuitem);

233
  gtk_menu_popup (GTK_MENU (menu), 
234 235
		  NULL, NULL,
		  gtk_status_icon_position_menu, icon,
236 237 238 239 240 241
		  button, activate_time);
}

int
main (int argc, char **argv)
{
242
  GtkStatusIcon *icon;
243 244 245

  gtk_init (&argc, &argv);

246
  icon = gtk_status_icon_new ();
247

248 249 250 251 252
  g_signal_connect (icon, "size-changed", G_CALLBACK (size_changed_cb), NULL);
  g_signal_connect (icon, "notify::embedded", G_CALLBACK (embedded_changed_cb), NULL);
  g_signal_connect (icon, "notify::orientation", G_CALLBACK (orientation_changed_cb), NULL);
  g_signal_connect (icon, "notify::screen", G_CALLBACK (screen_changed_cb), NULL);
  g_print ("icon size %d\n", gtk_status_icon_get_size (icon));
253

254 255
  g_signal_connect (icon, "activate",
                    G_CALLBACK (icon_activated), NULL);
256

257 258
  g_signal_connect (icon, "popup-menu",
                    G_CALLBACK (popup_menu), NULL);
259

260
  icons = g_slist_append (icons, icon);
261

262
  update_icon ();
263

264
  timeout = gdk_threads_add_timeout (2000, timeout_handler, icon);
265 266 267 268 269

  gtk_main ();

  return 0;
}