1 #include <string.h> /* for strcmp() */
2 #include <gdk/gdkx.h> /* for gdk_x11_window_set_user_time() */
5 #include "tilda-window.h"
6 #include "tilda-window-dbus-glue.h"
7 #include "tomboykeybinder.h"
10 * Find the TildaTerminal corresponding to the currently selected
11 * tab in self->notebook. This could go away if TildaTerminal were
12 * a proper subclass of GtkWidget.
14 static TildaTerminal *
15 tilda_window_find_current_terminal (TildaWindow *self)
18 debug_assert (TILDA_IS_WINDOW(self));
22 gint current_page = gtk_notebook_get_current_page (GTK_NOTEBOOK(self->notebook));
23 GtkWidget *box = gtk_notebook_get_nth_page (GTK_NOTEBOOK(self->notebook), current_page);
25 for (i=0; i<self->terms->len; ++i)
27 ret = g_ptr_array_index (self->terms, i);
33 debug_printf ("ERROR: unable to find current terminal!\n");
38 tilda_window_find_next_free_terminal_number (TildaWindow *self)
41 debug_assert (TILDA_IS_WINDOW(self));
46 for (i=0; i<INT_MAX; ++i)
50 for (j=0; j<self->terms->len; ++j)
52 TildaTerminal *tt = g_ptr_array_index (self->terms, j);
69 * Clean up and remove self completely from the program
71 * Should only be used by DBus...
74 tilda_window_close (TildaWindow *self)
77 debug_assert (TILDA_IS_WINDOW(self));
79 tilda_del_window (self->number);
85 tilda_window_add_terminal (TildaWindow *self)
88 debug_assert (TILDA_IS_WINDOW(self));
93 number = tilda_window_find_next_free_terminal_number (self);
94 tt = g_object_new (TILDA_TYPE_TERMINAL,
96 "parent-window", self,
98 g_ptr_array_add (self->terms, tt);
100 GtkWidget *label = gtk_label_new ("Tilda");
101 gint index = gtk_notebook_prepend_page (GTK_NOTEBOOK(self->notebook), tt->hbox, label);
102 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK(self->notebook), tt->hbox, TRUE, TRUE, GTK_PACK_END);
103 gtk_notebook_set_current_page (GTK_NOTEBOOK(self->notebook), index);
105 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK(self->notebook)) > 1)
106 gtk_notebook_set_show_tabs (GTK_NOTEBOOK(self->notebook), TRUE);
108 /* Focus the VTE Terminal */
109 gtk_widget_grab_focus (tt->vte_term);
115 * Remove the TildaTerminal with the given number from the given
118 * Return: TRUE on success, FALSE otherwise.
121 tilda_window_remove_terminal (TildaWindow *self, gint terminal_number)
124 debug_assert (TILDA_IS_WINDOW(self));
125 debug_assert (terminal_number >= 0);
129 for (i=0; i<self->terms->len; ++i)
131 TildaTerminal *tt = g_ptr_array_index (self->terms, i);
133 if (tt->number == terminal_number)
135 gint notebook_index = gtk_notebook_page_num (GTK_NOTEBOOK(self->notebook), tt->hbox);
137 /* Make sure the index was valid */
138 if (notebook_index == -1)
140 debug_printf ("ERROR: Bad Notebook Tab\n");
144 /* Actually remove the terminal */
145 gtk_notebook_remove_page (GTK_NOTEBOOK (self->notebook), notebook_index);
147 /* We should hide the tabs if there is only one tab left */
148 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (self->notebook)) == 1)
149 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (self->notebook), FALSE);
151 /* Remove the term from our lists, then free it */
152 g_ptr_array_remove_fast (self->terms, tt);
153 g_object_unref (G_OBJECT(tt));
155 /* With no pages left, it's time to remove this window */
156 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (self->notebook)) < 1)
158 debug_printf ("no terminals left, closing window %d\n", self->number);
159 tilda_del_window (self->number);
162 /* Leave the loop, we're done */
171 * This sets up the given TildaWindow for the capability of real
172 * transparency, if the X server is capable of it. */
174 tilda_window_setup_real_transparency (TildaWindow *self)
177 debug_assert (TILDA_IS_WINDOW(self));
180 GdkColormap *colormap;
182 screen = gtk_widget_get_screen (GTK_WIDGET(self->window));
183 colormap = gdk_screen_get_rgba_colormap (screen);
185 /* If possible, set the RGBA colormap so VTE can use real alpha
186 * channels for transparency. */
187 if (colormap != NULL && gdk_screen_is_composited (screen))
189 gtk_widget_set_colormap (GTK_WIDGET(self->window), colormap);
190 self->have_real_transparency = TRUE;
194 self->have_real_transparency = FALSE;
197 /* Center the given TildaWindow in the horizontal axis */
199 tilda_window_center_horizontally (TildaWindow *self)
202 debug_assert (TILDA_IS_WINDOW(self));
204 const gint screen_center = gdk_screen_width() / 2;
205 const gint tilda_center = self->width / 2;
206 const gint center_coord = screen_center - tilda_center;
208 g_object_set (G_OBJECT(self), "x-position", center_coord, NULL);
211 /* Center the given TildaWindow in the vertical axis */
213 tilda_window_center_vertically (TildaWindow *self)
216 debug_assert (TILDA_IS_WINDOW(self));
218 const gint screen_center = gdk_screen_height() / 2;
219 const gint tilda_center = self->height / 2;
220 const gint center_coord = screen_center - tilda_center;
222 g_object_set (G_OBJECT(self), "y-position", center_coord, NULL);
226 tilda_window_keybinding_cb (const gchar *keystr, gpointer data)
229 debug_assert (TILDA_IS_WINDOW(data));
231 TildaWindow *self = TILDA_WINDOW(data);
234 /* This call sets the X11 window property _NET_WM_USER_TIME, which GTK+ normally
235 * sets for us. However, because this callback is activated via a global keybinding,
236 * we see the event before GDK / GTK+ does. Therefore, to get the focus, we must
237 * set the property ourselves. */
238 gdk_x11_window_set_user_time (GTK_WIDGET(self->window)->window,
239 tomboy_keybinder_get_current_event_time());
243 case WINDOW_UP: /* Pull the window up */
245 /* Bugfix: having this here keeps the tilda window from being
246 * hidden if you turn off "stick", pull it down on workspace 1,
247 * switch to workspace 2, then pull it up and back down. Without
248 * this, something in metacity (at least) hides the window. Stupid. */
249 gtk_window_deiconify (GTK_WINDOW(self->window));
251 /* Re-set the window properties that do not linger after hiding the
252 * window. I know this looks stupid, but it keeps all of the state-
253 * changing code in the place it belongs: the property-setting code. */
254 g_object_set (G_OBJECT(self),
255 "keep-above", self->keep_above,
256 "stick", self->stick,
258 gtk_widget_show (GTK_WIDGET(self->window));
260 /* Focusing the term here works perfectly, near as I can tell */
261 tt = tilda_window_find_current_terminal (self);
262 gtk_widget_grab_focus (GTK_WIDGET(tt->vte_term));
264 self->state = WINDOW_DOWN;
267 case WINDOW_DOWN: /* Pull the window up */
269 gtk_widget_hide (GTK_WIDGET(self->window));
271 self->state = WINDOW_UP;
275 debug_printf ("ERROR: Window is in a bad state!\n");
277 /* Pretend we're down, for good measure.... */
278 self->state = WINDOW_DOWN;
284 * Attempt to bind the new_key to show this window.
286 * Return: TRUE if successful, FALSE otherwise.
289 tilda_window_try_to_bind_key (TildaWindow *self, const gchar *new_key)
292 debug_assert (TILDA_IS_WINDOW(self));
294 gboolean ret = FALSE;
296 /* Make sure the new key is not null in any way */
297 if (new_key == NULL || strcmp("", new_key) == 0)
300 /* Unbind if we were set */
302 tomboy_keybinder_unbind (self->key, tilda_window_keybinding_cb);
304 ret = tomboy_keybinder_bind (new_key, tilda_window_keybinding_cb, self);
306 /* If it was successful, update the self->key variable and be done with it */
310 self->key = g_strdup (new_key);
314 g_printerr (_("Bind key '%s' failed. Reverting to original keybinding\n"), self->key);
316 /* Not successful, so rebind the old key, and return FALSE */
317 if (self->key != NULL && strcmp("",self->key) != 0)
319 ret = tomboy_keybinder_bind (self->key, tilda_window_keybinding_cb, self);
321 /* Check that it went ok */
323 g_printerr (_("Unable to re-bind original key '%s'. Oh shit...\n"), self->key);
326 g_printerr (_("No original key to revert to!\n"));
332 tilda_window_dbus_register_object (TildaWindow *self)
335 debug_assert (TILDA_IS_WINDOW(self));
339 // Register this object with DBus
340 object_path = g_strdup_printf ("/net/sourceforge/Tilda/Window%d", self->number);
341 dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(self));
342 g_free (object_path);
345 /*******************************************************************************
346 * ALL GOBJECT STUFF BELOW PLEASE
347 ******************************************************************************/
349 static GObjectClass *parent_class = NULL;
351 enum tilda_window_properties {
352 TILDA_WINDOW_NUMBER = 1,
358 TILDA_WINDOW_X_POSITION,
359 TILDA_WINDOW_Y_POSITION,
361 TILDA_WINDOW_TAB_POSITION,
362 TILDA_WINDOW_ANIMATION_ORIENTATION,
363 TILDA_WINDOW_ANIMATION_DELAY,
365 TILDA_WINDOW_KEEP_ABOVE,
366 TILDA_WINDOW_SKIP_TASKBAR_HINT,
368 TILDA_WINDOW_HIDDEN_AT_START,
369 TILDA_WINDOW_CENTERED_HORIZONTALLY,
370 TILDA_WINDOW_CENTERED_VERTICALLY,
372 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
376 tilda_window_instance_init (GTypeInstance *instance,
381 TildaWindow *self = (TildaWindow *) instance;
382 self->dispose_has_run = FALSE;
384 /* Initialize all properties */
385 self->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
386 self->notebook = gtk_notebook_new ();
387 self->terms = g_ptr_array_new ();
389 /* Somewhat of a "poison" value, incase we don't set this */
390 self->number = 0xdeadbeef;
392 self->state = WINDOW_UP;
396 tilda_window_set_property (GObject *object,
401 TildaWindow *self = (TildaWindow *) object;
403 switch (property_id) {
405 case TILDA_WINDOW_NUMBER:
406 self->number = g_value_get_int (value);
407 debug_printf ("window number: %d\n", self->number);
410 case TILDA_WINDOW_KEY:
411 tilda_window_try_to_bind_key (self, g_value_get_string (value));
412 debug_printf ("window key %s\n", self->key);
415 case TILDA_WINDOW_HEIGHT:
416 self->height = g_value_get_int (value);
417 gtk_widget_set_size_request (self->window, self->width, self->height);
418 gtk_window_resize (GTK_WINDOW(self->window), self->width, self->height);
419 debug_printf ("window height: %d\n", self->height);
422 case TILDA_WINDOW_WIDTH:
423 self->width = g_value_get_int (value);
424 gtk_widget_set_size_request (self->window, self->width, self->height);
425 gtk_window_resize (GTK_WINDOW(self->window), self->width, self->height);
426 debug_printf ("window width: %d\n", self->width);
429 case TILDA_WINDOW_X_POSITION:
430 self->x_position = g_value_get_int (value);
431 gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
432 debug_printf ("window x position: %d\n", self->x_position);
435 case TILDA_WINDOW_Y_POSITION:
436 self->y_position = g_value_get_int (value);
437 gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
438 debug_printf ("window y position: %d\n", self->y_position);
441 case TILDA_WINDOW_TAB_POSITION:
442 self->tab_position = g_value_get_int (value);
443 gtk_notebook_set_tab_pos (GTK_NOTEBOOK(self->notebook), self->tab_position);
444 debug_printf ("window tab position: %d\n", self->tab_position);
447 case TILDA_WINDOW_ANIMATION_ORIENTATION:
448 self->animation_orientation = g_value_get_int (value);
449 debug_printf ("window animation orientation: %d\n", self->animation_orientation);
452 case TILDA_WINDOW_ANIMATION_DELAY:
453 self->animation_delay = g_value_get_int (value);
454 debug_printf ("window animation delay: %d\n", self->animation_delay);
457 case TILDA_WINDOW_KEEP_ABOVE:
458 self->keep_above = g_value_get_boolean (value);
459 gtk_window_set_keep_above (GTK_WINDOW(self->window), self->keep_above);
460 debug_printf ("window keep above: %d\n", self->keep_above);
463 case TILDA_WINDOW_SKIP_TASKBAR_HINT:
464 self->skip_taskbar_hint = g_value_get_boolean (value);
465 gtk_window_set_skip_taskbar_hint (GTK_WINDOW(self->window), self->skip_taskbar_hint);
466 debug_printf ("window skip taskbar hint: %d\n", self->skip_taskbar_hint);
469 case TILDA_WINDOW_STICK:
470 self->stick = g_value_get_boolean (value);
472 /* This is moderately ugly, but GTK+ does it this way... */
473 self->stick ? gtk_window_stick (GTK_WINDOW(self->window))
474 : gtk_window_unstick (GTK_WINDOW(self->window));
475 debug_printf ("window stick: %d\n", self->stick);
478 case TILDA_WINDOW_HIDDEN_AT_START:
479 self->hidden_at_start = g_value_get_boolean (value);
480 debug_printf ("window hidden at start: %d\n", self->hidden_at_start);
483 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
484 self->centered_horizontally = g_value_get_boolean (value);
485 if (self->centered_horizontally)
486 tilda_window_center_horizontally (self);
487 debug_printf ("window centered horizontally: %d\n", self->centered_horizontally);
490 case TILDA_WINDOW_CENTERED_VERTICALLY:
491 self->centered_vertically = g_value_get_boolean (value);
492 if (self->centered_vertically)
493 tilda_window_center_vertically (self);
494 debug_printf ("window centered vertically: %d\n", self->centered_vertically);
497 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
498 self->have_real_transparency = g_value_get_boolean (value);
499 debug_printf ("window have real transp: %d\n", self->have_real_transparency);
503 /* We don't have this property */
504 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
510 tilda_window_get_property (GObject *object,
515 TildaWindow *self = (TildaWindow *) object;
517 switch (property_id) {
519 case TILDA_WINDOW_NUMBER:
520 g_value_set_int (value, self->number);
523 case TILDA_WINDOW_KEY:
524 g_value_set_string (value, self->key);
527 case TILDA_WINDOW_HEIGHT:
528 g_value_set_int (value, self->height);
531 case TILDA_WINDOW_WIDTH:
532 g_value_set_int (value, self->width);
535 case TILDA_WINDOW_X_POSITION:
536 g_value_set_int (value, self->x_position);
539 case TILDA_WINDOW_Y_POSITION:
540 g_value_set_int (value, self->y_position);
543 case TILDA_WINDOW_TAB_POSITION:
544 g_value_set_int (value, self->tab_position);
547 case TILDA_WINDOW_ANIMATION_ORIENTATION:
548 g_value_set_int (value, self->animation_orientation);
551 case TILDA_WINDOW_ANIMATION_DELAY:
552 g_value_set_int (value, self->animation_delay);
555 case TILDA_WINDOW_KEEP_ABOVE:
556 g_value_set_boolean (value, self->keep_above);
559 case TILDA_WINDOW_SKIP_TASKBAR_HINT:
560 g_value_set_boolean (value, self->skip_taskbar_hint);
563 case TILDA_WINDOW_STICK:
564 g_value_set_boolean (value, self->stick);
567 case TILDA_WINDOW_HIDDEN_AT_START:
568 g_value_set_boolean (value, self->hidden_at_start);
571 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
572 g_value_set_boolean (value, self->centered_horizontally);
575 case TILDA_WINDOW_CENTERED_VERTICALLY:
576 g_value_set_boolean (value, self->centered_vertically);
579 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
580 g_value_set_boolean (value, self->have_real_transparency);
584 /* We don't have this property */
585 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
591 tilda_window_constructor (GType type,
592 guint n_construct_properties,
593 GObjectConstructParam *construct_properties)
600 /* Invoke parent constructor */
601 TildaWindowClass *klass;
602 klass = TILDA_WINDOW_CLASS (g_type_class_peek (TILDA_TYPE_WINDOW));
603 obj = parent_class->constructor (type,
604 n_construct_properties,
605 construct_properties);
607 /* Do other stuff here. The object is ready to go now, and all
608 * ctor properties have been set.
610 self = TILDA_WINDOW(obj);
612 /* Register this object with DBus */
613 tilda_window_dbus_register_object (self);
615 /* Try to set up real transparency */
616 tilda_window_setup_real_transparency (self);
618 gtk_container_add (GTK_CONTAINER(self->window), self->notebook);
619 g_object_set (G_OBJECT(self->notebook), "can-focus", FALSE, NULL);
620 gtk_widget_show (self->notebook);
622 // FIXME: Remove these, and replace with reads from the config system
623 g_object_set (G_OBJECT(self), "key", "F2", NULL);
624 g_object_set (G_OBJECT(self), "x-position", 0, "y-position", 0, NULL);
625 g_object_set (G_OBJECT(self), "height", 400, "width", 1680, NULL);
626 g_object_set (G_OBJECT(self), "keep-above", TRUE, "stick", TRUE, NULL);
627 g_object_set (G_OBJECT(self), "hidden-at-start", FALSE, NULL);
629 gtk_window_set_decorated (GTK_WINDOW(self->window), FALSE);
631 // FIXME: It should be configurable how many terms we add at startup
632 tilda_window_add_terminal (self);
633 tilda_window_add_terminal (self);
635 /* Show us if we're ready. If not, just remain hidden. All sub-widgets must
636 * be gtk_widget_show()n by this point. */
637 if (!self->hidden_at_start)
639 gtk_widget_show (self->window);
640 self->state = WINDOW_DOWN;
643 self->state = WINDOW_UP;
649 my_unref (gpointer data, gpointer user_data)
653 // FIXME: This function should probably be eliminated. It /is/ rather ugly
654 g_object_unref (G_OBJECT(data));
658 tilda_window_dispose (GObject *obj)
662 TildaWindow *self = (TildaWindow *) obj;
664 /* We don't want to run dispose twice, so just return immediately */
665 if (self->dispose_has_run)
669 * In dispose, you are supposed to free all types referenced from this
670 * object which might themselves hold a reference to self. Generally,
671 * the most simple solution is to unref all members on which you own a
674 * NOTE: See the following for how to deal with GtkObject-derived things:
675 * http://library.gnome.org/devel/gtk/unstable/GtkObject.html
677 g_ptr_array_foreach (self->terms, my_unref, NULL);
678 gtk_widget_destroy (self->window);
680 /* Chain up to the parent class */
681 G_OBJECT_CLASS (parent_class)->dispose (obj);
685 tilda_window_finalize (GObject *obj)
689 TildaWindow *self = (TildaWindow *) obj;
692 * Here, complete the object's destruction.
693 * You might not need to do much...
695 // TODO: g_free() any primitives here
696 g_ptr_array_free (self->terms, TRUE);
699 /* Chain up to the parent class */
700 G_OBJECT_CLASS (parent_class)->finalize (obj);
704 tilda_window_class_init (gpointer g_class,
705 gpointer g_class_data)
709 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
710 TildaWindowClass *klass = TILDA_WINDOW_CLASS (g_class);
713 /* Hook our functions to this type */
714 gobject_class->set_property = tilda_window_set_property;
715 gobject_class->get_property = tilda_window_get_property;
716 gobject_class->dispose = tilda_window_dispose;
717 gobject_class->finalize = tilda_window_finalize;
718 gobject_class->constructor = tilda_window_constructor;
720 parent_class = g_type_class_peek_parent (klass);
722 /* Install all of the properties */
723 pspec = g_param_spec_int ("number",
727 INT_MAX, // max value
729 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
731 g_object_class_install_property (gobject_class,
735 pspec = g_param_spec_string ("key",
736 _("Window's drop-down keybinding"),
741 g_object_class_install_property (gobject_class,
745 pspec = g_param_spec_int ("height",
746 _("Window's height"),
753 g_object_class_install_property (gobject_class,
757 pspec = g_param_spec_int ("width",
765 g_object_class_install_property (gobject_class,
769 pspec = g_param_spec_int ("x-position",
770 _("Window's x position"),
777 g_object_class_install_property (gobject_class,
778 TILDA_WINDOW_X_POSITION,
781 pspec = g_param_spec_int ("y-position",
782 _("Window's y position"),
789 g_object_class_install_property (gobject_class,
790 TILDA_WINDOW_Y_POSITION,
793 pspec = g_param_spec_int ("tab-position",
794 _("Position of window's tab bar"),
801 g_object_class_install_property (gobject_class,
802 TILDA_WINDOW_TAB_POSITION,
805 pspec = g_param_spec_int ("animation-orientation",
806 _("Window's animation orientation"),
813 g_object_class_install_property (gobject_class,
814 TILDA_WINDOW_ANIMATION_ORIENTATION,
817 pspec = g_param_spec_int ("animation-delay",
818 _("Amount of time in milliseconds between animation intervals"),
825 g_object_class_install_property (gobject_class,
826 TILDA_WINDOW_ANIMATION_DELAY,
829 pspec = g_param_spec_boolean ("keep-above",
830 _("Keep this window above all others"),
835 g_object_class_install_property (gobject_class,
836 TILDA_WINDOW_KEEP_ABOVE,
839 pspec = g_param_spec_boolean ("skip-taskbar-hint",
840 _("Hide this window in the taskbar if TRUE"),
845 g_object_class_install_property (gobject_class,
846 TILDA_WINDOW_SKIP_TASKBAR_HINT,
849 pspec = g_param_spec_boolean ("stick",
850 _("Display this window on all workspaces"),
855 g_object_class_install_property (gobject_class,
859 pspec = g_param_spec_boolean ("hidden-at-start",
860 _("Hide the window when it is first created"),
865 g_object_class_install_property (gobject_class,
866 TILDA_WINDOW_HIDDEN_AT_START,
869 pspec = g_param_spec_boolean ("centered-horizontally",
870 _("Center the window horizontally"),
875 g_object_class_install_property (gobject_class,
876 TILDA_WINDOW_CENTERED_HORIZONTALLY,
879 pspec = g_param_spec_boolean ("centered-vertically",
880 _("Center the window vertically"),
885 g_object_class_install_property (gobject_class,
886 TILDA_WINDOW_CENTERED_VERTICALLY,
889 pspec = g_param_spec_boolean ("have-real-transparency",
890 NULL, NULL, FALSE, G_PARAM_READABLE);
892 g_object_class_install_property (gobject_class,
893 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
896 /* Hook the TildaWindow type into DBus */
897 dbus_g_object_type_install_info (tilda_window_get_type(), &dbus_glib_tilda_window_object_info);
901 tilda_window_get_type (void)
903 static GType type = 0;
907 static const GTypeInfo info = {
908 sizeof (TildaWindowClass),
909 NULL, /* base_init */
910 NULL, /* base_finalize */
911 tilda_window_class_init, /* class_init */
912 NULL, /* class_finalize */
913 NULL, /* class_data */
914 sizeof (TildaWindow),
916 tilda_window_instance_init, /* instance_init */
919 type = g_type_register_static (G_TYPE_OBJECT,
928 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */