4 #include "tilda-window.h"
5 #include "tilda-window-dbus-glue.h"
6 #include "tomboykeybinder.h"
9 * Find the TildaTerminal corresponding to the currently selected
10 * tab in self->notebook. This could go away if TildaTerminal were
11 * a proper subclass of GtkWidget.
13 static TildaTerminal *
14 tilda_window_find_current_terminal (TildaWindow *self)
17 debug_assert (TILDA_IS_WINDOW(self));
21 gint current_page = gtk_notebook_get_current_page (GTK_NOTEBOOK(self->notebook));
22 GtkWidget *box = gtk_notebook_get_nth_page (GTK_NOTEBOOK(self->notebook), current_page);
24 for (i=0; i<self->terms->len; ++i)
26 ret = g_ptr_array_index (self->terms, i);
32 debug_printf ("ERROR: unable to find current terminal!\n");
37 tilda_window_find_next_free_terminal_number (TildaWindow *self)
40 debug_assert (TILDA_IS_WINDOW(self));
45 for (i=0; i<INT_MAX; ++i)
49 for (j=0; j<self->terms->len; ++j)
51 TildaTerminal *tt = g_ptr_array_index (self->terms, j);
68 * Clean up and remove self completely from the program
70 * Should only be used by DBus...
73 tilda_window_close (TildaWindow *self)
76 debug_assert (TILDA_IS_WINDOW(self));
78 tilda_del_window (self->number);
84 tilda_window_add_terminal (TildaWindow *self)
87 debug_assert (TILDA_IS_WINDOW(self));
92 number = tilda_window_find_next_free_terminal_number (self);
93 tt = g_object_new (TILDA_TYPE_TERMINAL,
95 "parent-window", self,
97 g_ptr_array_add (self->terms, tt);
99 GtkWidget *label = gtk_label_new ("Tilda");
100 gint index = gtk_notebook_prepend_page (GTK_NOTEBOOK(self->notebook), tt->hbox, label);
101 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK(self->notebook), tt->hbox, TRUE, TRUE, GTK_PACK_END);
102 gtk_notebook_set_current_page (GTK_NOTEBOOK(self->notebook), index);
104 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK(self->notebook)) > 1)
105 gtk_notebook_set_show_tabs (GTK_NOTEBOOK(self->notebook), TRUE);
111 * Remove the TildaTerminal with the given number from the given
114 * Return: TRUE on success, FALSE otherwise.
117 tilda_window_remove_terminal (TildaWindow *self, gint terminal_number)
120 debug_assert (TILDA_IS_WINDOW(self));
121 debug_assert (terminal_number >= 0);
125 for (i=0; i<self->terms->len; ++i)
127 TildaTerminal *tt = g_ptr_array_index (self->terms, i);
129 if (tt->number == terminal_number)
131 gint notebook_index = gtk_notebook_page_num (GTK_NOTEBOOK(self->notebook), tt->hbox);
133 /* Make sure the index was valid */
134 if (notebook_index == -1)
136 debug_printf ("ERROR: Bad Notebook Tab\n");
140 /* Actually remove the terminal */
141 gtk_notebook_remove_page (GTK_NOTEBOOK (self->notebook), notebook_index);
143 /* We should hide the tabs if there is only one tab left */
144 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (self->notebook)) == 1)
145 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (self->notebook), FALSE);
147 /* Remove the term from our lists, then free it */
148 g_ptr_array_remove_fast (self->terms, tt);
149 g_object_unref (G_OBJECT(tt));
151 /* With no pages left, it's time to remove this window */
152 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (self->notebook)) < 1)
154 debug_printf ("no terminals left, closing window %d\n", self->number);
155 tilda_del_window (self->number);
158 /* Leave the loop, we're done */
167 * This sets up the given TildaWindow for the capability of real
168 * transparency, if the X server is capable of it. */
170 tilda_window_setup_real_transparency (TildaWindow *self)
173 debug_assert (TILDA_IS_WINDOW(self));
176 GdkColormap *colormap;
178 screen = gtk_widget_get_screen (GTK_WIDGET(self->window));
179 colormap = gdk_screen_get_rgba_colormap (screen);
181 /* If possible, set the RGBA colormap so VTE can use real alpha
182 * channels for transparency. */
183 if (colormap != NULL && gdk_screen_is_composited (screen))
185 gtk_widget_set_colormap (GTK_WIDGET(self->window), colormap);
186 self->have_real_transparency = TRUE;
190 self->have_real_transparency = FALSE;
193 /* Center the given TildaWindow in the horizontal axis */
195 tilda_window_center_horizontally (TildaWindow *self)
198 debug_assert (TILDA_IS_WINDOW(self));
200 const gint screen_center = gdk_screen_width() / 2;
201 const gint tilda_center = self->width / 2;
202 const gint center_coord = screen_center - tilda_center;
204 g_object_set (G_OBJECT(self), "x-position", center_coord, NULL);
207 /* Center the given TildaWindow in the vertical axis */
209 tilda_window_center_vertically (TildaWindow *self)
212 debug_assert (TILDA_IS_WINDOW(self));
214 const gint screen_center = gdk_screen_height() / 2;
215 const gint tilda_center = self->height / 2;
216 const gint center_coord = screen_center - tilda_center;
218 g_object_set (G_OBJECT(self), "y-position", center_coord, NULL);
222 tilda_window_keybinding_cb (const gchar *keystr, gpointer data)
225 debug_assert (TILDA_IS_WINDOW(data));
227 TildaWindow *self = TILDA_WINDOW(data);
230 // FIXME: this doesn't handle animation!
234 case WINDOW_UP: /* Pull the window up */
236 /* Bugfix: having this here keeps the tilda window from being
237 * hidden if you turn off "stick", pull it down on workspace 1,
238 * switch to workspace 2, then pull it up and back down. Without
239 * this, something in metacity (at least) hides the window. Stupid. */
240 gtk_window_deiconify (GTK_WINDOW(self->window));
242 /* Re-set the window properties that do not linger after hiding the
243 * window. I know this looks stupid, but it keeps all of the state-
244 * changing code in the place it belongs: the property-setting code. */
245 g_object_set (G_OBJECT(self),
246 "keep-above", self->keep_above,
247 "stick", self->stick,
249 gtk_window_present_with_time (GTK_WINDOW(self->window),
250 tomboy_keybinder_get_current_event_time());
252 /* Focusing the term here works perfectly, near as I can tell */
253 tt = tilda_window_find_current_terminal (self);
254 gtk_widget_grab_focus (GTK_WIDGET(tt->vte_term));
256 self->state = WINDOW_DOWN;
259 case WINDOW_DOWN: /* Pull the window up */
261 gtk_widget_hide (GTK_WIDGET(self->window));
263 self->state = WINDOW_UP;
267 debug_printf ("ERROR: Window is in a bad state!\n");
269 /* Pretend we're down, for good measure.... */
270 self->state = WINDOW_DOWN;
276 * Attempt to bind the new_key to show this window.
278 * Return: TRUE if successful, FALSE otherwise.
281 tilda_window_try_to_bind_key (TildaWindow *self, const gchar *new_key)
284 debug_assert (TILDA_IS_WINDOW(self));
286 gboolean ret = FALSE;
288 /* Make sure the new key is not null in any way */
289 if (new_key == NULL || strcmp("", new_key) == 0)
292 /* Unbind if we were set */
294 tomboy_keybinder_unbind (self->key, tilda_window_keybinding_cb);
296 ret = tomboy_keybinder_bind (new_key, tilda_window_keybinding_cb, self);
298 /* If it was successful, update the self->key variable and be done with it */
302 self->key = g_strdup (new_key);
306 g_printerr (_("Bind key '%s' failed. Reverting to original keybinding\n"), self->key);
308 /* Not successful, so rebind the old key, and return FALSE */
309 if (self->key != NULL && strcmp("",self->key) != 0)
311 ret = tomboy_keybinder_bind (self->key, tilda_window_keybinding_cb, self);
313 /* Check that it went ok */
315 g_printerr (_("Unable to re-bind original key '%s'. Oh shit...\n"), self->key);
318 g_printerr (_("No original key to revert to!\n"));
324 tilda_window_dbus_register_object (TildaWindow *self)
327 debug_assert (TILDA_IS_WINDOW(self));
331 // Register this object with DBus
332 object_path = g_strdup_printf ("/net/sourceforge/Tilda/Window%d", self->number);
333 dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(self));
334 g_free (object_path);
337 /*******************************************************************************
338 * ALL GOBJECT STUFF BELOW PLEASE
339 ******************************************************************************/
341 static GObjectClass *parent_class = NULL;
343 enum tilda_window_properties {
344 TILDA_WINDOW_NUMBER = 1,
350 TILDA_WINDOW_X_POSITION,
351 TILDA_WINDOW_Y_POSITION,
353 TILDA_WINDOW_TAB_POSITION,
354 TILDA_WINDOW_ANIMATION_ORIENTATION,
355 TILDA_WINDOW_ANIMATION_DELAY,
357 TILDA_WINDOW_KEEP_ABOVE,
358 TILDA_WINDOW_SKIP_TASKBAR_HINT,
360 TILDA_WINDOW_HIDDEN_AT_START,
361 TILDA_WINDOW_CENTERED_HORIZONTALLY,
362 TILDA_WINDOW_CENTERED_VERTICALLY,
364 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
368 tilda_window_instance_init (GTypeInstance *instance,
373 TildaWindow *self = (TildaWindow *) instance;
374 self->dispose_has_run = FALSE;
376 /* Initialize all properties */
377 self->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
378 self->notebook = gtk_notebook_new ();
379 self->terms = g_ptr_array_new ();
381 /* Somewhat of a "poison" value, incase we don't set this */
382 self->number = 0xdeadbeef;
384 self->state = WINDOW_UP;
388 tilda_window_set_property (GObject *object,
393 TildaWindow *self = (TildaWindow *) object;
395 switch (property_id) {
397 case TILDA_WINDOW_NUMBER:
398 self->number = g_value_get_int (value);
399 debug_printf ("window number: %d\n", self->number);
402 case TILDA_WINDOW_KEY:
403 tilda_window_try_to_bind_key (self, g_value_get_string (value));
404 debug_printf ("window key %s\n", self->key);
407 case TILDA_WINDOW_HEIGHT:
408 self->height = g_value_get_int (value);
409 gtk_widget_set_size_request (self->window, self->width, self->height);
410 gtk_window_resize (GTK_WINDOW(self->window), self->width, self->height);
411 debug_printf ("window height: %d\n", self->height);
414 case TILDA_WINDOW_WIDTH:
415 self->width = g_value_get_int (value);
416 gtk_widget_set_size_request (self->window, self->width, self->height);
417 gtk_window_resize (GTK_WINDOW(self->window), self->width, self->height);
418 debug_printf ("window width: %d\n", self->width);
421 case TILDA_WINDOW_X_POSITION:
422 self->x_position = g_value_get_int (value);
423 gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
424 debug_printf ("window x position: %d\n", self->x_position);
427 case TILDA_WINDOW_Y_POSITION:
428 self->y_position = g_value_get_int (value);
429 gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
430 debug_printf ("window y position: %d\n", self->y_position);
433 case TILDA_WINDOW_TAB_POSITION:
434 self->tab_position = g_value_get_int (value);
435 gtk_notebook_set_tab_pos (GTK_NOTEBOOK(self->notebook), self->tab_position);
436 debug_printf ("window tab position: %d\n", self->tab_position);
439 case TILDA_WINDOW_ANIMATION_ORIENTATION:
440 self->animation_orientation = g_value_get_int (value);
441 debug_printf ("window animation orientation: %d\n", self->animation_orientation);
444 case TILDA_WINDOW_ANIMATION_DELAY:
445 self->animation_delay = g_value_get_int (value);
446 debug_printf ("window animation delay: %d\n", self->animation_delay);
449 case TILDA_WINDOW_KEEP_ABOVE:
450 self->keep_above = g_value_get_boolean (value);
451 gtk_window_set_keep_above (GTK_WINDOW(self->window), self->keep_above);
452 debug_printf ("window keep above: %d\n", self->keep_above);
455 case TILDA_WINDOW_SKIP_TASKBAR_HINT:
456 self->skip_taskbar_hint = g_value_get_boolean (value);
457 gtk_window_set_skip_taskbar_hint (GTK_WINDOW(self->window), self->skip_taskbar_hint);
458 debug_printf ("window skip taskbar hint: %d\n", self->skip_taskbar_hint);
461 case TILDA_WINDOW_STICK:
462 self->stick = g_value_get_boolean (value);
464 /* This is moderately ugly, but GTK+ does it this way... */
465 self->stick ? gtk_window_stick (GTK_WINDOW(self->window))
466 : gtk_window_unstick (GTK_WINDOW(self->window));
467 debug_printf ("window stick: %d\n", self->stick);
470 case TILDA_WINDOW_HIDDEN_AT_START:
471 self->hidden_at_start = g_value_get_boolean (value);
472 debug_printf ("window hidden at start: %d\n", self->hidden_at_start);
475 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
476 self->centered_horizontally = g_value_get_boolean (value);
477 if (self->centered_horizontally)
478 tilda_window_center_horizontally (self);
479 debug_printf ("window centered horizontally: %d\n", self->centered_horizontally);
482 case TILDA_WINDOW_CENTERED_VERTICALLY:
483 self->centered_vertically = g_value_get_boolean (value);
484 if (self->centered_vertically)
485 tilda_window_center_vertically (self);
486 debug_printf ("window centered vertically: %d\n", self->centered_vertically);
489 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
490 self->have_real_transparency = g_value_get_boolean (value);
491 debug_printf ("window have real transp: %d\n", self->have_real_transparency);
495 /* We don't have this property */
496 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
502 tilda_window_get_property (GObject *object,
507 TildaWindow *self = (TildaWindow *) object;
509 switch (property_id) {
511 case TILDA_WINDOW_NUMBER:
512 g_value_set_int (value, self->number);
515 case TILDA_WINDOW_KEY:
516 g_value_set_string (value, self->key);
519 case TILDA_WINDOW_HEIGHT:
520 g_value_set_int (value, self->height);
523 case TILDA_WINDOW_WIDTH:
524 g_value_set_int (value, self->width);
527 case TILDA_WINDOW_X_POSITION:
528 g_value_set_int (value, self->x_position);
531 case TILDA_WINDOW_Y_POSITION:
532 g_value_set_int (value, self->y_position);
535 case TILDA_WINDOW_TAB_POSITION:
536 g_value_set_int (value, self->tab_position);
539 case TILDA_WINDOW_ANIMATION_ORIENTATION:
540 g_value_set_int (value, self->animation_orientation);
543 case TILDA_WINDOW_ANIMATION_DELAY:
544 g_value_set_int (value, self->animation_delay);
547 case TILDA_WINDOW_KEEP_ABOVE:
548 g_value_set_boolean (value, self->keep_above);
551 case TILDA_WINDOW_SKIP_TASKBAR_HINT:
552 g_value_set_boolean (value, self->skip_taskbar_hint);
555 case TILDA_WINDOW_STICK:
556 g_value_set_boolean (value, self->stick);
559 case TILDA_WINDOW_HIDDEN_AT_START:
560 g_value_set_boolean (value, self->hidden_at_start);
563 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
564 g_value_set_boolean (value, self->centered_horizontally);
567 case TILDA_WINDOW_CENTERED_VERTICALLY:
568 g_value_set_boolean (value, self->centered_vertically);
571 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
572 g_value_set_boolean (value, self->have_real_transparency);
576 /* We don't have this property */
577 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
583 tilda_window_constructor (GType type,
584 guint n_construct_properties,
585 GObjectConstructParam *construct_properties)
592 /* Invoke parent constructor */
593 TildaWindowClass *klass;
594 klass = TILDA_WINDOW_CLASS (g_type_class_peek (TILDA_TYPE_WINDOW));
595 obj = parent_class->constructor (type,
596 n_construct_properties,
597 construct_properties);
599 /* Do other stuff here. The object is ready to go now, and all
600 * ctor properties have been set.
602 self = TILDA_WINDOW(obj);
604 /* Register this object with DBus */
605 tilda_window_dbus_register_object (self);
607 /* Try to set up real transparency */
608 tilda_window_setup_real_transparency (self);
610 gtk_container_add (GTK_CONTAINER(self->window), self->notebook);
611 gtk_widget_show (self->notebook);
613 // FIXME: Remove these, and replace with reads from the config system
614 g_object_set (G_OBJECT(self), "key", "F2", NULL);
615 g_object_set (G_OBJECT(self), "x-position", 0, "y-position", 0, NULL);
616 g_object_set (G_OBJECT(self), "height", 400, "width", 1680, NULL);
617 g_object_set (G_OBJECT(self), "keep-above", TRUE, "stick", TRUE, NULL);
618 g_object_set (G_OBJECT(self), "hidden-at-start", FALSE, NULL);
620 gtk_window_set_decorated (GTK_WINDOW(self->window), FALSE);
622 // FIXME: It should be configurable how many terms we add at startup
623 tilda_window_add_terminal (self);
624 tilda_window_add_terminal (self);
626 /* Show us if we're ready. If not, just remain hidden. All sub-widgets must
627 * be gtk_widget_show()n by this point. */
628 if (!self->hidden_at_start)
630 gtk_widget_show (self->window);
631 self->state = WINDOW_DOWN;
634 self->state = WINDOW_UP;
640 my_unref (gpointer data, gpointer user_data)
644 // FIXME: This function should probably be eliminated. It /is/ rather ugly
645 g_object_unref (G_OBJECT(data));
649 tilda_window_dispose (GObject *obj)
653 TildaWindow *self = (TildaWindow *) obj;
655 /* We don't want to run dispose twice, so just return immediately */
656 if (self->dispose_has_run)
660 * In dispose, you are supposed to free all types referenced from this
661 * object which might themselves hold a reference to self. Generally,
662 * the most simple solution is to unref all members on which you own a
665 * NOTE: See the following for how to deal with GtkObject-derived things:
666 * http://library.gnome.org/devel/gtk/unstable/GtkObject.html
668 g_ptr_array_foreach (self->terms, my_unref, NULL);
669 gtk_widget_destroy (self->window);
671 /* Chain up to the parent class */
672 G_OBJECT_CLASS (parent_class)->dispose (obj);
676 tilda_window_finalize (GObject *obj)
680 TildaWindow *self = (TildaWindow *) obj;
683 * Here, complete the object's destruction.
684 * You might not need to do much...
686 // TODO: g_free() any primitives here
687 g_ptr_array_free (self->terms, TRUE);
690 /* Chain up to the parent class */
691 G_OBJECT_CLASS (parent_class)->finalize (obj);
695 tilda_window_class_init (gpointer g_class,
696 gpointer g_class_data)
700 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
701 TildaWindowClass *klass = TILDA_WINDOW_CLASS (g_class);
704 /* Hook our functions to this type */
705 gobject_class->set_property = tilda_window_set_property;
706 gobject_class->get_property = tilda_window_get_property;
707 gobject_class->dispose = tilda_window_dispose;
708 gobject_class->finalize = tilda_window_finalize;
709 gobject_class->constructor = tilda_window_constructor;
711 parent_class = g_type_class_peek_parent (klass);
713 /* Install all of the properties */
714 pspec = g_param_spec_int ("number",
718 INT_MAX, // max value
720 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
722 g_object_class_install_property (gobject_class,
726 pspec = g_param_spec_string ("key",
727 _("Window's drop-down keybinding"),
732 g_object_class_install_property (gobject_class,
736 pspec = g_param_spec_int ("height",
737 _("Window's height"),
744 g_object_class_install_property (gobject_class,
748 pspec = g_param_spec_int ("width",
756 g_object_class_install_property (gobject_class,
760 pspec = g_param_spec_int ("x-position",
761 _("Window's x position"),
768 g_object_class_install_property (gobject_class,
769 TILDA_WINDOW_X_POSITION,
772 pspec = g_param_spec_int ("y-position",
773 _("Window's y position"),
780 g_object_class_install_property (gobject_class,
781 TILDA_WINDOW_Y_POSITION,
784 pspec = g_param_spec_int ("tab-position",
785 _("Position of window's tab bar"),
792 g_object_class_install_property (gobject_class,
793 TILDA_WINDOW_TAB_POSITION,
796 pspec = g_param_spec_int ("animation-orientation",
797 _("Window's animation orientation"),
804 g_object_class_install_property (gobject_class,
805 TILDA_WINDOW_ANIMATION_ORIENTATION,
808 pspec = g_param_spec_int ("animation-delay",
809 _("Amount of time in milliseconds between animation intervals"),
816 g_object_class_install_property (gobject_class,
817 TILDA_WINDOW_ANIMATION_DELAY,
820 pspec = g_param_spec_boolean ("keep-above",
821 _("Keep this window above all others"),
826 g_object_class_install_property (gobject_class,
827 TILDA_WINDOW_KEEP_ABOVE,
830 pspec = g_param_spec_boolean ("skip-taskbar-hint",
831 _("Hide this window in the taskbar if TRUE"),
836 g_object_class_install_property (gobject_class,
837 TILDA_WINDOW_SKIP_TASKBAR_HINT,
840 pspec = g_param_spec_boolean ("stick",
841 _("Display this window on all workspaces"),
846 g_object_class_install_property (gobject_class,
850 pspec = g_param_spec_boolean ("hidden-at-start",
851 _("Hide the window when it is first created"),
856 g_object_class_install_property (gobject_class,
857 TILDA_WINDOW_HIDDEN_AT_START,
860 pspec = g_param_spec_boolean ("centered-horizontally",
861 _("Center the window horizontally"),
866 g_object_class_install_property (gobject_class,
867 TILDA_WINDOW_CENTERED_HORIZONTALLY,
870 pspec = g_param_spec_boolean ("centered-vertically",
871 _("Center the window vertically"),
876 g_object_class_install_property (gobject_class,
877 TILDA_WINDOW_CENTERED_VERTICALLY,
880 pspec = g_param_spec_boolean ("have-real-transparency",
881 NULL, NULL, FALSE, G_PARAM_READABLE);
883 g_object_class_install_property (gobject_class,
884 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
887 /* Hook the TildaWindow type into DBus */
888 dbus_g_object_type_install_info (tilda_window_get_type(), &dbus_glib_tilda_window_object_info);
892 tilda_window_get_type (void)
894 static GType type = 0;
898 static const GTypeInfo info = {
899 sizeof (TildaWindowClass),
900 NULL, /* base_init */
901 NULL, /* base_finalize */
902 tilda_window_class_init, /* class_init */
903 NULL, /* class_finalize */
904 NULL, /* class_data */
905 sizeof (TildaWindow),
907 tilda_window_instance_init, /* instance_init */
910 type = g_type_register_static (G_TYPE_OBJECT,
919 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */