teststatusicon.c 9.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* 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>
25 26
#include <stdlib.h>

27
#include "prop-editor.h"
28 29 30

typedef enum
{
31 32
  TEST_STATUS_INFO,
  TEST_STATUS_QUESTION
33 34
} TestStatus;

35
static TestStatus status = TEST_STATUS_INFO;
36
static gint timeout = 0;
37
static GSList *icons = NULL;
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
static void
size_changed_cb (GtkStatusIcon *icon,
		 int size)
{
  g_print ("status icon %p size-changed size = %d\n", icon, size);
}

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);
}

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

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

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

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

      gtk_status_icon_set_from_icon_name (status_icon, icon_name);
      gtk_status_icon_set_tooltip (status_icon, tooltip);
    }
94 95 96 97 98
}

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

104
  update_icons ();
105 106 107 108 109

  return TRUE;
}

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

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

static void
120
visible_toggle_toggled (GtkToggleButton *toggle)
121
{
122 123 124 125 126
  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));
127 128 129
}

static void
130
timeout_toggle_toggled (GtkToggleButton *toggle)
131 132 133 134 135 136 137 138
{
  if (timeout)
    {
      g_source_remove (timeout);
      timeout = 0;
    }
  else
    {
139
      timeout = gdk_threads_add_timeout (2000, timeout_handler, NULL);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    }
}

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 ?");

157 158
      gtk_window_set_screen (GTK_WINDOW (dialog), gtk_status_icon_get_screen (icon));
      gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
159

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
      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", 
175
			G_CALLBACK (visible_toggle_toggled), NULL);
176 177 178 179 180 181 182 183

      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", 
184
			G_CALLBACK (blink_toggle_toggled), NULL);
185 186 187 188 189 190 191 192

      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", 
193
			G_CALLBACK (timeout_toggle_toggled), NULL);
194 195 196 197 198 199
    }

  gtk_window_present (GTK_WINDOW (dialog));
}

static void
200
check_activated (GtkCheckMenuItem *item)
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
  GSList *l;
  GdkScreen *screen;

  screen = NULL;

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

      orig_screen = gtk_status_icon_get_screen (icon);

      if (screen != NULL)
        gtk_status_icon_set_screen (icon, screen);

      screen = orig_screen;

      gtk_status_icon_set_blinking (icon,
                                    gtk_check_menu_item_get_active (item));
    }

  g_assert (screen != NULL);

  gtk_status_icon_set_screen (GTK_STATUS_ICON (icons->data), screen);
226 227
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241
static void
do_properties (GtkMenuItem   *item,
	       GtkStatusIcon *icon)
{
	static GtkWidget *editor = NULL;

	if (editor == NULL) {
		editor = create_prop_editor (G_OBJECT (icon), GTK_TYPE_STATUS_ICON);
		g_signal_connect (editor, "destroy", G_CALLBACK (gtk_widget_destroyed), &editor);
	}

	gtk_window_present (GTK_WINDOW (editor));
}

242
static void
243
do_quit (GtkMenuItem *item)
244
{
245 246 247 248 249 250 251 252 253 254 255 256 257
  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;

258 259 260
  gtk_main_quit ();
}

261 262 263 264 265 266
static void
do_exit (GtkMenuItem *item)
{
  exit (0);
}

267 268 269 270 271 272 273 274 275
static void 
popup_menu (GtkStatusIcon *icon,
	    guint          button,
	    guint32        activate_time)
{
  GtkWidget *menu, *menuitem;

  menu = gtk_menu_new ();

276 277 278
  gtk_menu_set_screen (GTK_MENU (menu),
                       gtk_status_icon_get_screen (icon));

279 280 281
  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));
282
  g_signal_connect (menuitem, "activate", G_CALLBACK (check_activated), NULL);
283 284 285 286 287

  gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);

  gtk_widget_show (menuitem);

288 289 290 291 292 293 294
  menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_PROPERTIES, NULL);
  g_signal_connect (menuitem, "activate", G_CALLBACK (do_properties), icon);

  gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);

  gtk_widget_show (menuitem);

295
  menuitem = gtk_menu_item_new_with_label ("Quit");
296
  g_signal_connect (menuitem, "activate", G_CALLBACK (do_quit), NULL);
297 298 299 300 301

  gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);

  gtk_widget_show (menuitem);

302 303 304 305 306 307 308
  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);

309
  gtk_menu_popup (GTK_MENU (menu), 
310 311
		  NULL, NULL,
		  gtk_status_icon_position_menu, icon,
312 313 314 315 316 317
		  button, activate_time);
}

int
main (int argc, char **argv)
{
318 319
  GdkDisplay *display;
  guint n_screens, i;
320 321 322

  gtk_init (&argc, &argv);

323
  display = gdk_display_get_default ();
324

325
  n_screens = gdk_display_get_n_screens (display);
326

327 328 329 330 331 332 333 334
  for (i = 0; i < n_screens; i++)
    {
      GtkStatusIcon *icon;

      icon = gtk_status_icon_new ();
      gtk_status_icon_set_screen (icon, gdk_display_get_screen (display, i));
      update_icons ();

335 336 337
      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);
338
      g_signal_connect (icon, "notify::screen", G_CALLBACK (screen_changed_cb), NULL);
339 340
      g_print ("icon size %d\n", gtk_status_icon_get_size (icon));
      gtk_status_icon_set_blinking (GTK_STATUS_ICON (icon), FALSE);
341 342 343

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

345 346 347 348
      g_signal_connect (icon, "popup-menu",
                        G_CALLBACK (popup_menu), NULL);

      icons = g_slist_append (icons, icon);
349
 
350 351
      update_icons ();

352
      timeout = gdk_threads_add_timeout (2000, timeout_handler, icon);
353
    }
354 355 356 357 358

  gtk_main ();

  return 0;
}