[134] | 1 | /* |
---|
| 2 | * Copyright (C) 2002 Anders Carlsson <andersca@gnu.org> |
---|
| 3 | * Copyright (C) 2003-2006 Vincent Untz |
---|
| 4 | * Copyright (C) 2006, 2007 Christian Persch |
---|
| 5 | * |
---|
| 6 | * This library is free software; you can redistribute it and/or |
---|
| 7 | * modify it under the terms of the GNU Lesser General Public |
---|
| 8 | * License as published by the Free Software Foundation; either |
---|
| 9 | * version 2 of the License, or (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This library is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 14 | * Lesser General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU Lesser General Public |
---|
| 17 | * License along with this library; if not, write to the |
---|
| 18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
| 19 | * Boston, MA 02111-1307, USA. |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include "config.h" |
---|
| 23 | |
---|
| 24 | #include <string.h> |
---|
| 25 | #include <stdio.h> |
---|
| 26 | #include <gtk/gtk.h> |
---|
| 27 | #include "na-tray-manager.h" |
---|
| 28 | #include "na-tray.h" |
---|
| 29 | |
---|
| 30 | #define NOTIFICATION_AREA_ICON "gnome-panel-notification-area" |
---|
| 31 | |
---|
| 32 | static guint n_windows = 0; |
---|
| 33 | |
---|
| 34 | typedef struct |
---|
| 35 | { |
---|
| 36 | GdkScreen *screen; |
---|
| 37 | guint screen_num; |
---|
| 38 | GtkWidget *window; |
---|
| 39 | NaTray *tray; |
---|
| 40 | GtkWidget *box; |
---|
| 41 | GtkLabel *count_label; |
---|
| 42 | } TrayData; |
---|
| 43 | |
---|
| 44 | static void |
---|
| 45 | do_add (GtkWidget *child, guint *n_children) |
---|
| 46 | { |
---|
| 47 | *n_children += 1; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | static void |
---|
| 51 | update_child_count (TrayData *data) |
---|
| 52 | { |
---|
| 53 | guint n_children = 0; |
---|
| 54 | char text[64]; |
---|
| 55 | |
---|
| 56 | if (!GTK_WIDGET_REALIZED (data->window)) |
---|
| 57 | return; |
---|
| 58 | |
---|
| 59 | gtk_container_foreach (GTK_CONTAINER (data->box), (GtkCallback) do_add, &n_children); |
---|
| 60 | |
---|
| 61 | g_snprintf (text, sizeof (text), "%u icons", n_children); |
---|
| 62 | gtk_label_set_text (data->count_label, text); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | static void |
---|
| 66 | tray_added_cb (GtkContainer *box, GtkWidget *icon, TrayData *data) |
---|
| 67 | { |
---|
| 68 | g_print ("[Screen %u tray %p] Child %p added to tray: \"%s\"\n", |
---|
| 69 | data->screen_num, data->tray, icon, "XXX");//na_tray_manager_get_child_title (manager, icon)); |
---|
| 70 | |
---|
| 71 | update_child_count (data); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | static void |
---|
| 75 | tray_removed_cb (GtkContainer *box, GtkWidget *icon, TrayData *data) |
---|
| 76 | { |
---|
| 77 | g_print ("[Screen %u tray %p] Child %p removed from tray\n", |
---|
| 78 | data->screen_num, data->tray, icon); |
---|
| 79 | |
---|
| 80 | update_child_count (data); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | static void orientation_changed_cb (GtkComboBox *combo, TrayData *data) |
---|
| 84 | { |
---|
| 85 | GtkOrientation orientation = (GtkOrientation) gtk_combo_box_get_active (combo); |
---|
| 86 | |
---|
| 87 | g_print ("[Screen %u tray %p] Setting orientation to \"%s\"\n", |
---|
| 88 | data->screen_num, data->tray, orientation == 0 ? "horizontal" : "vertical"); |
---|
| 89 | |
---|
| 90 | na_tray_set_orientation (data->tray, orientation); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | static void |
---|
| 94 | maybe_quit (gpointer data, |
---|
| 95 | GObject *zombie) |
---|
| 96 | { |
---|
| 97 | if (--n_windows == 0) { |
---|
| 98 | gtk_main_quit (); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | static TrayData *create_tray_on_screen (GdkScreen *screen, gboolean force); |
---|
| 103 | |
---|
| 104 | static void |
---|
| 105 | warning_dialog_response_cb (GtkWidget *dialog, |
---|
| 106 | gint response, |
---|
| 107 | GdkScreen *screen) |
---|
| 108 | { |
---|
| 109 | if (response == GTK_RESPONSE_YES) { |
---|
| 110 | create_tray_on_screen (screen, TRUE); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | gtk_widget_destroy (dialog); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | static void |
---|
| 117 | add_tray_cb (GtkWidget *button, TrayData *data) |
---|
| 118 | { |
---|
| 119 | create_tray_on_screen (data->screen, TRUE); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | static TrayData * |
---|
| 123 | create_tray_on_screen (GdkScreen *screen, |
---|
| 124 | gboolean force) |
---|
| 125 | { |
---|
| 126 | GtkWidget *window, *hbox, *vbox, *button, *combo, *label; |
---|
| 127 | TrayData *data; |
---|
| 128 | |
---|
| 129 | n_windows++; |
---|
| 130 | |
---|
| 131 | if (!force && na_tray_manager_check_running (screen)) { |
---|
| 132 | GtkWidget *dialog; |
---|
| 133 | |
---|
| 134 | dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_YES_NO, |
---|
| 135 | "Override tray manager?"); |
---|
| 136 | gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), |
---|
| 137 | "There is already a tray manager running on screen %d.", |
---|
| 138 | gdk_screen_get_number (screen)); |
---|
| 139 | gtk_window_set_screen (GTK_WINDOW (dialog), screen); |
---|
| 140 | g_signal_connect (dialog, "response", G_CALLBACK (warning_dialog_response_cb), screen); |
---|
| 141 | gtk_window_present (GTK_WINDOW (dialog)); |
---|
| 142 | g_object_weak_ref (G_OBJECT (dialog), (GWeakNotify) maybe_quit, NULL); |
---|
| 143 | return NULL; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | data = g_new0 (TrayData, 1); |
---|
| 147 | data->screen = screen; |
---|
| 148 | data->screen_num = gdk_screen_get_number (screen); |
---|
| 149 | |
---|
| 150 | data->window = window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
---|
| 151 | g_object_weak_ref (G_OBJECT (window), (GWeakNotify) maybe_quit, NULL); |
---|
| 152 | |
---|
| 153 | vbox = gtk_vbox_new (FALSE, 6); |
---|
| 154 | gtk_container_add (GTK_CONTAINER (window), vbox); |
---|
| 155 | |
---|
| 156 | button = gtk_button_new_with_mnemonic ("_Add another tray"); |
---|
| 157 | g_signal_connect (button, "clicked", G_CALLBACK (add_tray_cb), data); |
---|
| 158 | gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0); |
---|
| 159 | |
---|
| 160 | hbox = gtk_hbox_new (FALSE, 12); |
---|
| 161 | gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); |
---|
| 162 | label = gtk_label_new_with_mnemonic ("_Orientation:"); |
---|
| 163 | gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5); |
---|
| 164 | gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); |
---|
| 165 | combo = gtk_combo_box_new_text (); |
---|
| 166 | gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "Horizontal"); |
---|
| 167 | gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "Vertical"); |
---|
| 168 | g_signal_connect (combo, "changed", |
---|
| 169 | G_CALLBACK (orientation_changed_cb), data); |
---|
| 170 | gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0); |
---|
| 171 | |
---|
| 172 | label = gtk_label_new (NULL); |
---|
| 173 | data->count_label = GTK_LABEL (label); |
---|
| 174 | gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5); |
---|
| 175 | gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0); |
---|
| 176 | |
---|
| 177 | data->tray = na_tray_new_for_screen (screen, GTK_ORIENTATION_HORIZONTAL); |
---|
| 178 | gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (data->tray), TRUE, TRUE, 0); |
---|
| 179 | |
---|
| 180 | data->box = GTK_BIN (GTK_BIN (data->tray)->child)->child; |
---|
| 181 | g_signal_connect_after (data->box, "add", G_CALLBACK (tray_added_cb), data); |
---|
| 182 | g_signal_connect_after (data->box, "remove", G_CALLBACK (tray_removed_cb), data); |
---|
| 183 | |
---|
| 184 | gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0); |
---|
| 185 | |
---|
| 186 | gtk_window_set_screen (GTK_WINDOW (window), screen); |
---|
| 187 | gtk_window_set_default_size (GTK_WINDOW (window), -1, 200); |
---|
| 188 | |
---|
| 189 | /* gtk_window_set_resizable (GTK_WINDOW (window), FALSE); */ |
---|
| 190 | |
---|
| 191 | gtk_widget_show_all (window); |
---|
| 192 | |
---|
| 193 | update_child_count (data); |
---|
| 194 | |
---|
| 195 | return data; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | int |
---|
| 199 | main (int argc, char *argv[]) |
---|
| 200 | { |
---|
| 201 | GdkDisplay *display; |
---|
| 202 | GdkScreen *screen; |
---|
| 203 | int n_screens, i; |
---|
| 204 | |
---|
| 205 | gtk_init (&argc, &argv); |
---|
| 206 | |
---|
| 207 | gtk_window_set_default_icon_name (NOTIFICATION_AREA_ICON); |
---|
| 208 | |
---|
| 209 | display = gdk_display_get_default (); |
---|
| 210 | n_screens = gdk_display_get_n_screens (display); |
---|
| 211 | for (i = 0; i < n_screens; ++i) { |
---|
| 212 | screen = gdk_display_get_screen (display, i); |
---|
| 213 | |
---|
| 214 | create_tray_on_screen (screen, FALSE); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | gtk_main (); |
---|
| 218 | |
---|
| 219 | return 0; |
---|
| 220 | } |
---|