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 *tw)
40 debug_assert (TILDA_IS_WINDOW(tw));
45 for (i=0; i<INT_MAX; ++i)
49 for (j=0; j<tw->terms->len; ++j)
51 TildaTerminal *tt = g_ptr_array_index (tw->terms, j);
68 tilda_window_add_term (TildaWindow *tw)
71 debug_assert (TILDA_IS_WINDOW(tw));
76 number = tilda_window_find_next_free_terminal_number (tw);
77 tt = g_object_new (TILDA_TYPE_TERMINAL,
81 g_ptr_array_add (tw->terms, tt);
83 GtkWidget *label = gtk_label_new ("Tilda");
84 gint index = gtk_notebook_prepend_page (GTK_NOTEBOOK(tw->notebook), tt->hbox, label);
85 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK(tw->notebook), tt->hbox, TRUE, TRUE, GTK_PACK_END);
86 gtk_notebook_set_current_page (GTK_NOTEBOOK(tw->notebook), index);
88 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK(tw->notebook)) > 1)
89 gtk_notebook_set_show_tabs (GTK_NOTEBOOK(tw->notebook), TRUE);
95 * Remove the TildaTerminal with the given number from the given
98 * Return: TRUE on success, FALSE otherwise.
101 tilda_window_remove_term (TildaWindow *tw, gint terminal_number)
104 debug_assert (TILDA_IS_WINDOW(tw));
105 debug_assert (terminal_number >= 0);
109 for (i=0; i<tw->terms->len; ++i)
111 TildaTerminal *tt = g_ptr_array_index (tw->terms, i);
113 if (tt->number == terminal_number)
115 gint notebook_index = gtk_notebook_page_num (GTK_NOTEBOOK(tw->notebook), tt->hbox);
117 /* Make sure the index was valid */
118 if (notebook_index == -1)
120 debug_printf ("ERROR: Bad Notebook Tab\n");
124 /* Actually remove the terminal */
125 gtk_notebook_remove_page (GTK_NOTEBOOK (tw->notebook), notebook_index);
127 /* We should hide the tabs if there is only one tab left */
128 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (tw->notebook)) == 1)
129 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (tw->notebook), FALSE);
131 /* Remove the term from our lists, then free it */
132 g_ptr_array_remove_fast (tw->terms, tt);
133 g_object_unref (G_OBJECT(tt));
135 /* With no pages left, it's time to remove this window */
136 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (tw->notebook)) < 1)
138 debug_printf ("no terminals left, closing window %d\n", tw->number);
139 tilda_del_window (tw->number);
142 /* Leave the loop, we're done */
151 * This sets up the given TildaWindow for the capability of real
152 * transparency, if the X server is capable of it. */
154 tilda_window_setup_real_transparency (TildaWindow *self)
157 debug_assert (TILDA_IS_WINDOW(self));
160 GdkColormap *colormap;
162 screen = gtk_widget_get_screen (GTK_WIDGET(self->window));
163 colormap = gdk_screen_get_rgba_colormap (screen);
165 /* If possible, set the RGBA colormap so VTE can use real alpha
166 * channels for transparency. */
167 if (colormap != NULL && gdk_screen_is_composited (screen))
169 gtk_widget_set_colormap (GTK_WIDGET(self->window), colormap);
170 self->have_real_transparency = TRUE;
174 self->have_real_transparency = FALSE;
177 /* Center the given TildaWindow in the horizontal axis */
179 tilda_window_center_horizontally (TildaWindow *self)
182 debug_assert (TILDA_IS_WINDOW(self));
184 const gint screen_center = gdk_screen_width() / 2;
185 const gint tilda_center = self->width / 2;
186 const gint center_coord = screen_center - tilda_center;
188 g_object_set (G_OBJECT(self), "x-position", center_coord, NULL);
191 /* Center the given TildaWindow in the vertical axis */
193 tilda_window_center_vertically (TildaWindow *self)
196 debug_assert (TILDA_IS_WINDOW(self));
198 const gint screen_center = gdk_screen_height() / 2;
199 const gint tilda_center = self->height / 2;
200 const gint center_coord = screen_center - tilda_center;
202 g_object_set (G_OBJECT(self), "y-position", center_coord, NULL);
206 tilda_window_keybinding_cb (const gchar *keystr, gpointer data)
209 debug_assert (TILDA_IS_WINDOW(data));
211 TildaWindow *self = TILDA_WINDOW(data);
214 // FIXME: this doesn't handle animation!
218 case WINDOW_UP: /* Pull the window up */
220 /* Bugfix: having this here keeps the tilda window from being
221 * hidden if you turn off "stick", pull it down on workspace 1,
222 * switch to workspace 2, then pull it up and back down. Without
223 * this, something in metacity (at least) hides the window. Stupid. */
224 gtk_window_deiconify (GTK_WINDOW(self->window));
226 /* Re-set the window properties that do not linger after hiding the
227 * window. I know this looks stupid, but it keeps all of the state-
228 * changing code in the place it belongs: the property-setting code. */
229 g_object_set (G_OBJECT(self),
230 "keep-above", self->keep_above,
231 "stick", self->stick,
233 gtk_window_present_with_time (GTK_WINDOW(self->window),
234 tomboy_keybinder_get_current_event_time());
236 /* Focusing the term here works perfectly, near as I can tell */
237 tt = tilda_window_find_current_terminal (self);
238 gtk_widget_grab_focus (GTK_WIDGET(tt->vte_term));
240 self->state = WINDOW_DOWN;
243 case WINDOW_DOWN: /* Pull the window up */
245 gtk_widget_hide (GTK_WIDGET(self->window));
247 self->state = WINDOW_UP;
251 debug_printf ("ERROR: Window is in a bad state!\n");
253 /* Pretend we're down, for good measure.... */
254 self->state = WINDOW_DOWN;
260 * Attempt to bind the new_key to show this window.
262 * Return: TRUE if successful, FALSE otherwise.
265 tilda_window_try_to_bind_key (TildaWindow *self, const gchar *new_key)
268 debug_assert (TILDA_IS_WINDOW(self));
270 gboolean ret = FALSE;
272 /* Make sure the new key is not null in any way */
273 if (new_key == NULL || strcmp("", new_key) == 0)
276 /* Unbind if we were set */
278 tomboy_keybinder_unbind (self->key, tilda_window_keybinding_cb);
280 ret = tomboy_keybinder_bind (new_key, tilda_window_keybinding_cb, self);
282 /* If it was successful, update the self->key variable and be done with it */
286 self->key = g_strdup (new_key);
290 g_printerr (_("Bind key '%s' failed. Reverting to original keybinding\n"), self->key);
292 /* Not successful, so rebind the old key, and return FALSE */
293 if (self->key != NULL && strcmp("",self->key) != 0)
295 ret = tomboy_keybinder_bind (self->key, tilda_window_keybinding_cb, self);
297 /* Check that it went ok */
299 g_printerr (_("Unable to re-bind original key '%s'. Oh shit...\n"), self->key);
302 g_printerr (_("No original key to revert to!\n"));
308 tilda_window_dbus_register_object (TildaWindow *tw)
311 debug_assert (TILDA_IS_WINDOW(tw));
315 // Register this object with DBus
316 object_path = g_strdup_printf ("/net/sourceforge/Tilda/Window%d", tw->number);
317 dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(tw));
318 g_free (object_path);
321 /*******************************************************************************
322 * ALL GOBJECT STUFF BELOW PLEASE
323 ******************************************************************************/
325 static GObjectClass *parent_class = NULL;
327 enum tilda_window_properties {
328 TILDA_WINDOW_NUMBER = 1,
334 TILDA_WINDOW_X_POSITION,
335 TILDA_WINDOW_Y_POSITION,
337 TILDA_WINDOW_TAB_POSITION,
338 TILDA_WINDOW_ANIMATION_ORIENTATION,
339 TILDA_WINDOW_ANIMATION_DELAY,
341 TILDA_WINDOW_KEEP_ABOVE,
342 TILDA_WINDOW_SKIP_TASKBAR_HINT,
344 TILDA_WINDOW_HIDDEN_AT_START,
345 TILDA_WINDOW_CENTERED_HORIZONTALLY,
346 TILDA_WINDOW_CENTERED_VERTICALLY,
348 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
352 tilda_window_instance_init (GTypeInstance *instance,
357 TildaWindow *self = (TildaWindow *) instance;
358 self->dispose_has_run = FALSE;
360 /* Initialize all properties */
361 self->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
362 self->notebook = gtk_notebook_new ();
363 self->terms = g_ptr_array_new ();
365 /* Somewhat of a "poison" value, incase we don't set this */
366 self->number = 0xdeadbeef;
368 self->state = WINDOW_UP;
372 tilda_window_set_property (GObject *object,
377 TildaWindow *self = (TildaWindow *) object;
379 switch (property_id) {
381 case TILDA_WINDOW_NUMBER:
382 self->number = g_value_get_int (value);
383 debug_printf ("window number: %d\n", self->number);
386 case TILDA_WINDOW_KEY:
387 tilda_window_try_to_bind_key (self, g_value_get_string (value));
388 debug_printf ("window key %s\n", self->key);
391 case TILDA_WINDOW_HEIGHT:
392 self->height = g_value_get_int (value);
393 gtk_widget_set_size_request (self->window, self->width, self->height);
394 gtk_window_resize (GTK_WINDOW(self->window), self->width, self->height);
395 debug_printf ("window height: %d\n", self->height);
398 case TILDA_WINDOW_WIDTH:
399 self->width = g_value_get_int (value);
400 gtk_widget_set_size_request (self->window, self->width, self->height);
401 gtk_window_resize (GTK_WINDOW(self->window), self->width, self->height);
402 debug_printf ("window width: %d\n", self->width);
405 case TILDA_WINDOW_X_POSITION:
406 self->x_position = g_value_get_int (value);
407 gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
408 debug_printf ("window x position: %d\n", self->x_position);
411 case TILDA_WINDOW_Y_POSITION:
412 self->y_position = g_value_get_int (value);
413 gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
414 debug_printf ("window y position: %d\n", self->y_position);
417 case TILDA_WINDOW_TAB_POSITION:
418 self->tab_position = g_value_get_int (value);
419 gtk_notebook_set_tab_pos (GTK_NOTEBOOK(self->notebook), self->tab_position);
420 debug_printf ("window tab position: %d\n", self->tab_position);
423 case TILDA_WINDOW_ANIMATION_ORIENTATION:
424 self->animation_orientation = g_value_get_int (value);
425 debug_printf ("window animation orientation: %d\n", self->animation_orientation);
428 case TILDA_WINDOW_ANIMATION_DELAY:
429 self->animation_delay = g_value_get_int (value);
430 debug_printf ("window animation delay: %d\n", self->animation_delay);
433 case TILDA_WINDOW_KEEP_ABOVE:
434 self->keep_above = g_value_get_boolean (value);
435 gtk_window_set_keep_above (GTK_WINDOW(self->window), self->keep_above);
436 debug_printf ("window keep above: %d\n", self->keep_above);
439 case TILDA_WINDOW_SKIP_TASKBAR_HINT:
440 self->skip_taskbar_hint = g_value_get_boolean (value);
441 gtk_window_set_skip_taskbar_hint (GTK_WINDOW(self->window), self->skip_taskbar_hint);
442 debug_printf ("window skip taskbar hint: %d\n", self->skip_taskbar_hint);
445 case TILDA_WINDOW_STICK:
446 self->stick = g_value_get_boolean (value);
448 /* This is moderately ugly, but GTK+ does it this way... */
449 self->stick ? gtk_window_stick (GTK_WINDOW(self->window))
450 : gtk_window_unstick (GTK_WINDOW(self->window));
451 debug_printf ("window stick: %d\n", self->stick);
454 case TILDA_WINDOW_HIDDEN_AT_START:
455 self->hidden_at_start = g_value_get_boolean (value);
456 debug_printf ("window hidden at start: %d\n", self->hidden_at_start);
459 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
460 self->centered_horizontally = g_value_get_boolean (value);
461 if (self->centered_horizontally)
462 tilda_window_center_horizontally (self);
463 debug_printf ("window centered horizontally: %d\n", self->centered_horizontally);
466 case TILDA_WINDOW_CENTERED_VERTICALLY:
467 self->centered_vertically = g_value_get_boolean (value);
468 if (self->centered_vertically)
469 tilda_window_center_vertically (self);
470 debug_printf ("window centered vertically: %d\n", self->centered_vertically);
473 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
474 self->have_real_transparency = g_value_get_boolean (value);
475 debug_printf ("window have real transp: %d\n", self->have_real_transparency);
479 /* We don't have this property */
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
486 tilda_window_get_property (GObject *object,
491 TildaWindow *self = (TildaWindow *) object;
493 switch (property_id) {
495 case TILDA_WINDOW_NUMBER:
496 g_value_set_int (value, self->number);
499 case TILDA_WINDOW_KEY:
500 g_value_set_string (value, self->key);
503 case TILDA_WINDOW_HEIGHT:
504 g_value_set_int (value, self->height);
507 case TILDA_WINDOW_WIDTH:
508 g_value_set_int (value, self->width);
511 case TILDA_WINDOW_X_POSITION:
512 g_value_set_int (value, self->x_position);
515 case TILDA_WINDOW_Y_POSITION:
516 g_value_set_int (value, self->y_position);
519 case TILDA_WINDOW_TAB_POSITION:
520 g_value_set_int (value, self->tab_position);
523 case TILDA_WINDOW_ANIMATION_ORIENTATION:
524 g_value_set_int (value, self->animation_orientation);
527 case TILDA_WINDOW_ANIMATION_DELAY:
528 g_value_set_int (value, self->animation_delay);
531 case TILDA_WINDOW_KEEP_ABOVE:
532 g_value_set_boolean (value, self->keep_above);
535 case TILDA_WINDOW_SKIP_TASKBAR_HINT:
536 g_value_set_boolean (value, self->skip_taskbar_hint);
539 case TILDA_WINDOW_STICK:
540 g_value_set_boolean (value, self->stick);
543 case TILDA_WINDOW_HIDDEN_AT_START:
544 g_value_set_boolean (value, self->hidden_at_start);
547 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
548 g_value_set_boolean (value, self->centered_horizontally);
551 case TILDA_WINDOW_CENTERED_VERTICALLY:
552 g_value_set_boolean (value, self->centered_vertically);
555 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
556 g_value_set_boolean (value, self->have_real_transparency);
560 /* We don't have this property */
561 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
567 tilda_window_constructor (GType type,
568 guint n_construct_properties,
569 GObjectConstructParam *construct_properties)
576 /* Invoke parent constructor */
577 TildaWindowClass *klass;
578 klass = TILDA_WINDOW_CLASS (g_type_class_peek (TILDA_TYPE_WINDOW));
579 obj = parent_class->constructor (type,
580 n_construct_properties,
581 construct_properties);
583 /* Do other stuff here. The object is ready to go now, and all
584 * ctor properties have been set.
586 self = TILDA_WINDOW(obj);
588 /* Register this object with DBus */
589 tilda_window_dbus_register_object (self);
591 /* Try to set up real transparency */
592 tilda_window_setup_real_transparency (self);
594 gtk_container_add (GTK_CONTAINER(self->window), self->notebook);
595 gtk_widget_show (self->notebook);
597 // FIXME: Remove these, and replace with reads from the config system
598 g_object_set (G_OBJECT(self), "key", "F2", NULL);
599 g_object_set (G_OBJECT(self), "x-position", 0, "y-position", 0, NULL);
600 g_object_set (G_OBJECT(self), "height", 400, "width", 1680, NULL);
601 g_object_set (G_OBJECT(self), "keep-above", TRUE, "stick", TRUE, NULL);
602 g_object_set (G_OBJECT(self), "hidden-at-start", FALSE, NULL);
604 gtk_window_set_decorated (GTK_WINDOW(self->window), FALSE);
606 // FIXME: It should be configurable how many terms we add at startup
607 tilda_window_add_term (self);
608 tilda_window_add_term (self);
610 /* Show us if we're ready. If not, just remain hidden. All sub-widgets must
611 * be gtk_widget_show()n by this point. */
612 if (!self->hidden_at_start)
614 gtk_widget_show (self->window);
615 self->state = WINDOW_DOWN;
618 self->state = WINDOW_UP;
624 my_unref (gpointer data, gpointer user_data)
628 // FIXME: This function should probably be eliminated. It /is/ rather ugly
629 g_object_unref (G_OBJECT(data));
633 tilda_window_dispose (GObject *obj)
637 TildaWindow *self = (TildaWindow *) obj;
639 /* We don't want to run dispose twice, so just return immediately */
640 if (self->dispose_has_run)
644 * In dispose, you are supposed to free all types referenced from this
645 * object which might themselves hold a reference to self. Generally,
646 * the most simple solution is to unref all members on which you own a
649 * NOTE: See the following for how to deal with GtkObject-derived things:
650 * http://library.gnome.org/devel/gtk/unstable/GtkObject.html
652 g_ptr_array_foreach (self->terms, my_unref, NULL);
653 gtk_widget_destroy (self->window);
655 /* Chain up to the parent class */
656 G_OBJECT_CLASS (parent_class)->dispose (obj);
660 tilda_window_finalize (GObject *obj)
664 TildaWindow *self = (TildaWindow *) obj;
667 * Here, complete the object's destruction.
668 * You might not need to do much...
670 // TODO: g_free() any primitives here
671 g_ptr_array_free (self->terms, TRUE);
674 /* Chain up to the parent class */
675 G_OBJECT_CLASS (parent_class)->finalize (obj);
679 tilda_window_class_init (gpointer g_class,
680 gpointer g_class_data)
684 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
685 TildaWindowClass *klass = TILDA_WINDOW_CLASS (g_class);
688 /* Hook our functions to this type */
689 gobject_class->set_property = tilda_window_set_property;
690 gobject_class->get_property = tilda_window_get_property;
691 gobject_class->dispose = tilda_window_dispose;
692 gobject_class->finalize = tilda_window_finalize;
693 gobject_class->constructor = tilda_window_constructor;
695 parent_class = g_type_class_peek_parent (klass);
697 /* Install all of the properties */
698 pspec = g_param_spec_int ("number",
702 INT_MAX, // max value
704 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
706 g_object_class_install_property (gobject_class,
710 pspec = g_param_spec_string ("key",
711 _("Window's drop-down keybinding"),
716 g_object_class_install_property (gobject_class,
720 pspec = g_param_spec_int ("height",
721 _("Window's height"),
728 g_object_class_install_property (gobject_class,
732 pspec = g_param_spec_int ("width",
740 g_object_class_install_property (gobject_class,
744 pspec = g_param_spec_int ("x-position",
745 _("Window's x position"),
752 g_object_class_install_property (gobject_class,
753 TILDA_WINDOW_X_POSITION,
756 pspec = g_param_spec_int ("y-position",
757 _("Window's y position"),
764 g_object_class_install_property (gobject_class,
765 TILDA_WINDOW_Y_POSITION,
768 pspec = g_param_spec_int ("tab-position",
769 _("Position of window's tab bar"),
776 g_object_class_install_property (gobject_class,
777 TILDA_WINDOW_TAB_POSITION,
780 pspec = g_param_spec_int ("animation-orientation",
781 _("Window's animation orientation"),
788 g_object_class_install_property (gobject_class,
789 TILDA_WINDOW_ANIMATION_ORIENTATION,
792 pspec = g_param_spec_int ("animation-delay",
793 _("Amount of time in milliseconds between animation intervals"),
800 g_object_class_install_property (gobject_class,
801 TILDA_WINDOW_ANIMATION_DELAY,
804 pspec = g_param_spec_boolean ("keep-above",
805 _("Keep this window above all others"),
810 g_object_class_install_property (gobject_class,
811 TILDA_WINDOW_KEEP_ABOVE,
814 pspec = g_param_spec_boolean ("skip-taskbar-hint",
815 _("Hide this window in the taskbar if TRUE"),
820 g_object_class_install_property (gobject_class,
821 TILDA_WINDOW_SKIP_TASKBAR_HINT,
824 pspec = g_param_spec_boolean ("stick",
825 _("Display this window on all workspaces"),
830 g_object_class_install_property (gobject_class,
834 pspec = g_param_spec_boolean ("hidden-at-start",
835 _("Hide the window when it is first created"),
840 g_object_class_install_property (gobject_class,
841 TILDA_WINDOW_HIDDEN_AT_START,
844 pspec = g_param_spec_boolean ("centered-horizontally",
845 _("Center the window horizontally"),
850 g_object_class_install_property (gobject_class,
851 TILDA_WINDOW_CENTERED_HORIZONTALLY,
854 pspec = g_param_spec_boolean ("centered-vertically",
855 _("Center the window vertically"),
860 g_object_class_install_property (gobject_class,
861 TILDA_WINDOW_CENTERED_VERTICALLY,
864 pspec = g_param_spec_boolean ("have-real-transparency",
865 NULL, NULL, FALSE, G_PARAM_READABLE);
867 g_object_class_install_property (gobject_class,
868 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
871 /* Hook the TildaWindow type into DBus */
872 dbus_g_object_type_install_info (tilda_window_get_type(), &dbus_glib_tilda_window_object_info);
876 tilda_window_get_type (void)
878 static GType type = 0;
882 static const GTypeInfo info = {
883 sizeof (TildaWindowClass),
884 NULL, /* base_init */
885 NULL, /* base_finalize */
886 tilda_window_class_init, /* class_init */
887 NULL, /* class_finalize */
888 NULL, /* class_data */
889 sizeof (TildaWindow),
891 tilda_window_instance_init, /* instance_init */
894 type = g_type_register_static (G_TYPE_OBJECT,
903 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */