2 #include "tilda-window.h"
3 #include "tilda-window-dbus-glue.h"
6 * Find the TildaTerminal corresponding to the currently selected
7 * tab in self->notebook. This could go away if TildaTerminal were
8 * a proper subclass of GtkWidget.
10 static TildaTerminal *
11 tilda_window_find_current_terminal (TildaWindow *self)
15 gint current_page = gtk_notebook_get_current_page (GTK_NOTEBOOK(self->notebook));
16 GtkWidget *box = gtk_notebook_get_nth_page (GTK_NOTEBOOK(self->notebook), current_page);
18 for (i=0; i<self->terms->len; ++i)
20 ret = g_ptr_array_index (self->terms, i);
26 g_printerr ("FIXME: unable to find current terminal!\n");
31 tilda_window_find_next_free_terminal_number (TildaWindow *tw)
36 for (i=0; i<INT_MAX; ++i)
40 for (j=0; j<tw->terms->len; ++j)
42 TildaTerminal *tt = g_ptr_array_index (tw->terms, j);
59 tilda_window_add_term (TildaWindow *tw)
64 number = tilda_window_find_next_free_terminal_number (tw);
65 tt = g_object_new (TILDA_TYPE_TERMINAL,
67 "window-number", tw->number,
70 g_ptr_array_add (tw->terms, tt);
72 GtkWidget *label = gtk_label_new ("Tilda");
73 gint index = gtk_notebook_prepend_page (GTK_NOTEBOOK(tw->notebook), tt->hbox, label);
74 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK(tw->notebook), tt->hbox, TRUE, TRUE, GTK_PACK_END);
75 //gtk_notebook_set_current_page (GTK_NOTEBOOK(tw->notebook), index);
77 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK(tw->notebook)) > 1)
78 gtk_notebook_set_show_tabs (GTK_NOTEBOOK(tw->notebook), TRUE);
84 * Remove the TildaTerminal with the given number from the given
87 * Return: TRUE on success, FALSE otherwise.
90 tilda_window_remove_term (TildaWindow *tw, gint terminal_number)
94 for (i=0; i<tw->terms->len; ++i)
96 TildaTerminal *tt = g_ptr_array_index (tw->terms, i);
98 if (tt->number == terminal_number)
100 gint notebook_index = gtk_notebook_page_num (GTK_NOTEBOOK(tw->notebook), tt->hbox);
102 /* Make sure the index was valid */
103 if (notebook_index == -1)
105 g_printerr ("DEBUG ERROR: Bad Notebook Tab\n");
109 /* Actually remove the terminal */
110 gtk_notebook_remove_page (GTK_NOTEBOOK (tw->notebook), notebook_index);
112 /* We should hide the tabs if there is only one tab left */
113 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (tw->notebook)) == 1)
114 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (tw->notebook), FALSE);
117 // FIXME FIXME FIXME: need to actually do the stuff below
118 /* With no pages left, it's time to leave the program */
119 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (tw->notebook)) < 1)
123 /* Remove the term from our lists, then free it */
124 g_ptr_array_remove_fast (tw->terms, tt);
125 g_object_unref (G_OBJECT(tt));
127 /* Leave the loop, we're done */
136 * This sets up the given TildaWindow for the capability of real
137 * transparency, if the X server is capable of it. */
139 tilda_window_setup_real_transparency (TildaWindow *self)
142 GdkColormap *colormap;
144 screen = gtk_widget_get_screen (GTK_WIDGET(self->window));
145 colormap = gdk_screen_get_rgba_colormap (screen);
147 /* If possible, set the RGBA colormap so VTE can use real alpha
148 * channels for transparency. */
149 if (colormap != NULL && gdk_screen_is_composited (screen))
151 gtk_widget_set_colormap (GTK_WIDGET(self->window), colormap);
152 self->have_real_transparency = TRUE;
156 self->have_real_transparency = FALSE;
160 tilda_window_keybinding_cb (const gchar *keystr, gpointer data)
162 TildaWindow *self = TILDA_WINDOW(data);
165 // FIXME: this doesn't handle animation!
169 case WINDOW_UP: /* Pull the window up */
171 /* Bugfix: having this here keeps the tilda window from being
172 * hidden if you turn off "stick", pull it down on workspace 1,
173 * switch to workspace 2, then pull it up and back down. Without
174 * this, something in metacity (at least) hides the window. Stupid. */
175 gtk_window_deiconify (GTK_WINDOW(self->window));
177 /* Re-set the window properties that do not linger after hiding the
178 * window. I know this looks stupid, but it keeps all of the state-
179 * changing code in the place it belongs: the property-setting code. */
180 g_object_set (G_OBJECT(self),
181 "keep-above", self->keep_above,
182 "stick", self->stick,
184 gtk_window_present_with_time (GTK_WINDOW(self->window),
185 tomboy_keybinder_get_current_event_time());
187 /* Focusing the term here works perfectly, near as I can tell */
188 tt = tilda_window_find_current_terminal (self);
189 gtk_widget_grab_focus (GTK_WIDGET(tt->vte_term));
191 self->state = WINDOW_DOWN;
194 case WINDOW_DOWN: /* Pull the window up */
196 gtk_widget_hide (GTK_WIDGET(self->window));
198 self->state = WINDOW_UP;
202 g_printerr ("FIXME: the window is in a bad state!\n");
204 /* Pretend we're down, for good measure.... */
205 self->state = WINDOW_DOWN;
211 * Attempt to bind the new_key to show this window.
213 * Return: TRUE if successful, FALSE otherwise.
216 tilda_window_try_to_bind_key (TildaWindow *self, const gchar *new_key)
218 gboolean ret = FALSE;
220 /* Make sure the new key is not null in any way */
221 if (new_key == NULL || strcmp("", new_key) == 0)
224 /* Unbind if we were set */
226 tomboy_keybinder_unbind (self->key, tilda_window_keybinding_cb, self);
228 ret = tomboy_keybinder_bind (new_key, tilda_window_keybinding_cb, self);
230 /* If it was successful, update the self->key variable and be done with it */
234 self->key = g_strdup (new_key);
238 g_printerr ("Keybinding unsuccessful. Reverting to original key\n");
240 /* Not successful, so rebind the old key, and return FALSE */
241 if (self->key != NULL && strcmp("",self->key) != 0)
243 ret = tomboy_keybinder_bind (self->key, tilda_window_keybinding_cb, self);
245 /* Check that it went ok */
247 g_printerr ("Unable to bind original key as well! Oh shit...\n");
250 g_printerr ("No original key to revert to!\n");
256 tilda_window_dbus_register_object (TildaWindow *tw)
260 // Register this object with DBus
261 object_path = g_strdup_printf ("/net/sourceforge/Tilda/Window%d", tw->number);
262 dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(tw));
263 g_free (object_path);
266 /*******************************************************************************
267 * ALL GOBJECT STUFF BELOW PLEASE
268 ******************************************************************************/
270 static GObjectClass *parent_class = NULL;
272 enum tilda_window_properties {
273 TILDA_WINDOW_NUMBER = 1,
279 TILDA_WINDOW_X_POSITION,
280 TILDA_WINDOW_Y_POSITION,
282 TILDA_WINDOW_TAB_POSITION,
283 TILDA_WINDOW_ANIMATION_ORIENTATION,
284 TILDA_WINDOW_ANIMATION_DELAY,
286 TILDA_WINDOW_KEEP_ABOVE,
287 TILDA_WINDOW_SKIP_TASKBAR_HINT,
289 TILDA_WINDOW_HIDDEN_AT_START,
290 TILDA_WINDOW_CENTERED_HORIZONTALLY,
291 TILDA_WINDOW_CENTERED_VERTICALLY,
293 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
297 tilda_window_instance_init (GTypeInstance *instance,
300 TildaWindow *self = (TildaWindow *) instance;
301 self->dispose_has_run = FALSE;
303 /* Initialize all properties */
304 self->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
305 self->notebook = gtk_notebook_new ();
306 self->terms = g_ptr_array_new ();
308 /* Somewhat of a "poison" value, incase we don't set this */
309 self->number = 0xdeadbeef;
311 self->state = WINDOW_UP;
315 tilda_window_set_property (GObject *object,
320 TildaWindow *self = (TildaWindow *) object;
322 switch (property_id) {
324 case TILDA_WINDOW_NUMBER:
325 self->number = g_value_get_int (value);
326 g_print ("window number: %d\n", self->number);
329 case TILDA_WINDOW_KEY:
330 tilda_window_try_to_bind_key (self, g_value_get_string (value));
331 g_print ("window key: %s\n", self->key);
334 case TILDA_WINDOW_HEIGHT:
335 self->height = g_value_get_int (value);
336 gtk_widget_set_size_request (self->window, self->width, self->height);
337 gtk_window_resize (GTK_WINDOW(self->window), self->width, self->height);
338 g_print ("window height: %d\n", self->height);
341 case TILDA_WINDOW_WIDTH:
342 self->width = g_value_get_int (value);
343 gtk_widget_set_size_request (self->window, self->width, self->height);
344 gtk_window_resize (GTK_WINDOW(self->window), self->width, self->height);
345 g_print ("window width: %d\n", self->width);
348 case TILDA_WINDOW_X_POSITION:
349 self->x_position = g_value_get_int (value);
350 gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
351 g_print ("window x position: %d\n", self->x_position);
354 case TILDA_WINDOW_Y_POSITION:
355 self->y_position = g_value_get_int (value);
356 gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
357 g_print ("window y position: %d\n", self->y_position);
360 case TILDA_WINDOW_TAB_POSITION:
361 self->tab_position = g_value_get_int (value);
362 gtk_notebook_set_tab_pos (GTK_NOTEBOOK(self->notebook), self->tab_position);
363 g_print ("window tab position: %d\n", self->tab_position);
366 case TILDA_WINDOW_ANIMATION_ORIENTATION:
367 self->animation_orientation = g_value_get_int (value);
368 g_print ("window animation orientation: %d\n", self->animation_orientation);
371 case TILDA_WINDOW_ANIMATION_DELAY:
372 self->animation_delay = g_value_get_int (value);
373 g_print ("window animation delay: %d\n", self->animation_delay);
376 case TILDA_WINDOW_KEEP_ABOVE:
377 self->keep_above = g_value_get_boolean (value);
378 gtk_window_set_keep_above (GTK_WINDOW(self->window), self->keep_above);
379 g_print ("window keep above: %d\n", self->keep_above);
382 case TILDA_WINDOW_SKIP_TASKBAR_HINT:
383 self->skip_taskbar_hint = g_value_get_boolean (value);
384 gtk_window_set_skip_taskbar_hint (GTK_WINDOW(self->window), self->skip_taskbar_hint);
385 g_print ("window skip taskbar hint: %d\n", self->skip_taskbar_hint);
388 case TILDA_WINDOW_STICK:
389 self->stick = g_value_get_boolean (value);
391 /* This is moderately ugly, but GTK+ does it this way... */
392 self->stick ? gtk_window_stick (GTK_WINDOW(self->window))
393 : gtk_window_unstick (GTK_WINDOW(self->window));
394 g_print ("window stick: %d\n", self->stick);
397 case TILDA_WINDOW_HIDDEN_AT_START:
398 self->hidden_at_start = g_value_get_boolean (value);
399 g_print ("window hidden at start: %d\n", self->hidden_at_start);
402 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
403 self->centered_horizontally = g_value_get_boolean (value);
404 g_print ("window centered horizontally: %d\n", self->centered_horizontally);
407 case TILDA_WINDOW_CENTERED_VERTICALLY:
408 self->centered_vertically = g_value_get_boolean (value);
409 g_print ("window centered vertically: %d\n", self->centered_vertically);
412 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
413 self->have_real_transparency = g_value_get_boolean (value);
414 g_print ("window have real transp: %d\n", self->have_real_transparency);
418 /* We don't have this property */
419 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
425 tilda_window_get_property (GObject *object,
430 TildaWindow *self = (TildaWindow *) object;
432 switch (property_id) {
434 case TILDA_WINDOW_NUMBER:
435 g_value_set_int (value, self->number);
438 case TILDA_WINDOW_KEY:
439 g_value_set_string (value, self->key);
442 case TILDA_WINDOW_HEIGHT:
443 g_value_set_int (value, self->height);
446 case TILDA_WINDOW_WIDTH:
447 g_value_set_int (value, self->width);
450 case TILDA_WINDOW_X_POSITION:
451 g_value_set_int (value, self->x_position);
454 case TILDA_WINDOW_Y_POSITION:
455 g_value_set_int (value, self->y_position);
458 case TILDA_WINDOW_TAB_POSITION:
459 g_value_set_int (value, self->tab_position);
462 case TILDA_WINDOW_ANIMATION_ORIENTATION:
463 g_value_set_int (value, self->animation_orientation);
466 case TILDA_WINDOW_ANIMATION_DELAY:
467 g_value_set_int (value, self->animation_delay);
470 case TILDA_WINDOW_KEEP_ABOVE:
471 g_value_set_boolean (value, self->keep_above);
474 case TILDA_WINDOW_SKIP_TASKBAR_HINT:
475 g_value_set_boolean (value, self->skip_taskbar_hint);
478 case TILDA_WINDOW_STICK:
479 g_value_set_boolean (value, self->stick);
482 case TILDA_WINDOW_HIDDEN_AT_START:
483 g_value_set_boolean (value, self->hidden_at_start);
486 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
487 g_value_set_boolean (value, self->centered_horizontally);
490 case TILDA_WINDOW_CENTERED_VERTICALLY:
491 g_value_set_boolean (value, self->centered_vertically);
494 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
495 g_value_set_boolean (value, self->have_real_transparency);
499 /* We don't have this property */
500 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
506 tilda_window_constructor (GType type,
507 guint n_construct_properties,
508 GObjectConstructParam *construct_properties)
513 /* Invoke parent constructor */
514 TildaWindowClass *klass;
515 klass = TILDA_WINDOW_CLASS (g_type_class_peek (TILDA_TYPE_WINDOW));
516 obj = parent_class->constructor (type,
517 n_construct_properties,
518 construct_properties);
520 /* Do other stuff here. The object is ready to go now, and all
521 * ctor properties have been set.
523 * TODO: This is the place to do DBus-init */
524 self = TILDA_WINDOW(obj);
526 /* Register this object with DBus */
527 tilda_window_dbus_register_object (self);
529 /* Try to set up real transparency */
530 tilda_window_setup_real_transparency (self);
532 gtk_container_add (GTK_CONTAINER(self->window), self->notebook);
533 gtk_widget_show (self->notebook);
535 // FIXME: Remove these, and replace with reads from the config system
536 g_object_set (G_OBJECT(self), "key", "F2", NULL);
537 g_object_set (G_OBJECT(self), "x-position", 0, "y-position", 0, NULL);
538 g_object_set (G_OBJECT(self), "height", 400, "width", 1680, NULL);
539 g_object_set (G_OBJECT(self), "keep-above", TRUE, "stick", TRUE, NULL);
540 g_object_set (G_OBJECT(self), "hidden-at-start", FALSE, NULL);
542 gtk_window_set_decorated (GTK_WINDOW(self->window), FALSE);
544 // FIXME: It should be configurable how many terms we add at startup
545 tilda_window_add_term (self);
546 tilda_window_add_term (self);
548 /* Show us if we're ready. If not, just remain hidden. All sub-widgets must
549 * be gtk_widget_show()n by this point. */
550 if (!self->hidden_at_start)
552 gtk_widget_show (self->window);
553 self->state = WINDOW_DOWN;
556 self->state = WINDOW_UP;
562 my_unref (gpointer data, gpointer user_data)
564 g_object_unref (G_OBJECT(data));
568 tilda_window_dispose (GObject *obj)
570 TildaWindow *self = (TildaWindow *) obj;
572 /* We don't want to run dispose twice, so just return immediately */
573 if (self->dispose_has_run)
577 * In dispose, you are supposed to free all types referenced from this
578 * object which might themselves hold a reference to self. Generally,
579 * the most simple solution is to unref all members on which you own a
582 * NOTE: See the following for how to deal with GtkObject-derived things:
583 * http://library.gnome.org/devel/gtk/unstable/GtkObject.html
585 g_ptr_array_foreach (self->terms, my_unref, NULL);
586 gtk_widget_destroy (self->window);
588 /* Chain up to the parent class */
589 G_OBJECT_CLASS (parent_class)->dispose (obj);
593 tilda_window_finalize (GObject *obj)
595 TildaWindow *self = (TildaWindow *) obj;
598 * Here, complete the object's destruction.
599 * You might not need to do much...
601 // TODO: g_free() any primitives here
602 g_ptr_array_free (self->terms, TRUE);
605 /* Chain up to the parent class */
606 G_OBJECT_CLASS (parent_class)->finalize (obj);
610 tilda_window_class_init (gpointer g_class,
611 gpointer g_class_data)
613 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
614 TildaWindowClass *klass = TILDA_WINDOW_CLASS (g_class);
617 /* Hook our functions to this type */
618 gobject_class->set_property = tilda_window_set_property;
619 gobject_class->get_property = tilda_window_get_property;
620 gobject_class->dispose = tilda_window_dispose;
621 gobject_class->finalize = tilda_window_finalize;
622 gobject_class->constructor = tilda_window_constructor;
624 parent_class = g_type_class_peek_parent (klass);
626 /* Install all of the properties */
627 pspec = g_param_spec_int ("number",
629 "Set window's number",
631 INT_MAX, // max value
633 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
635 g_object_class_install_property (gobject_class,
639 pspec = g_param_spec_string ("key",
640 "Window's drop-down keybinding",
645 g_object_class_install_property (gobject_class,
649 pspec = g_param_spec_int ("height",
657 g_object_class_install_property (gobject_class,
661 pspec = g_param_spec_int ("width",
669 g_object_class_install_property (gobject_class,
673 pspec = g_param_spec_int ("x-position",
674 "Window's x position",
681 g_object_class_install_property (gobject_class,
682 TILDA_WINDOW_X_POSITION,
685 pspec = g_param_spec_int ("y-position",
686 "Window's y position",
693 g_object_class_install_property (gobject_class,
694 TILDA_WINDOW_Y_POSITION,
697 pspec = g_param_spec_int ("tab-position",
698 "Window's tab position",
705 g_object_class_install_property (gobject_class,
706 TILDA_WINDOW_TAB_POSITION,
709 pspec = g_param_spec_int ("animation-orientation",
710 "Window's animation orientation",
717 g_object_class_install_property (gobject_class,
718 TILDA_WINDOW_ANIMATION_ORIENTATION,
721 pspec = g_param_spec_int ("animation-delay",
722 "Amount of time in milliseconds between animation intervals",
729 g_object_class_install_property (gobject_class,
730 TILDA_WINDOW_ANIMATION_DELAY,
733 pspec = g_param_spec_boolean ("keep-above",
734 "Keep this window above all others",
739 g_object_class_install_property (gobject_class,
740 TILDA_WINDOW_KEEP_ABOVE,
743 pspec = g_param_spec_boolean ("skip-taskbar-hint",
744 "Hide this window in the taskbar if TRUE",
749 g_object_class_install_property (gobject_class,
750 TILDA_WINDOW_SKIP_TASKBAR_HINT,
753 pspec = g_param_spec_boolean ("stick",
754 "Display this window on all workspaces",
759 g_object_class_install_property (gobject_class,
763 pspec = g_param_spec_boolean ("hidden-at-start",
764 "Hide the window when it is first created",
769 g_object_class_install_property (gobject_class,
770 TILDA_WINDOW_HIDDEN_AT_START,
773 pspec = g_param_spec_boolean ("centered-horizontally",
774 "Center the window horizontally",
779 g_object_class_install_property (gobject_class,
780 TILDA_WINDOW_CENTERED_HORIZONTALLY,
783 pspec = g_param_spec_boolean ("centered-vertically",
784 "Center the window vertically",
789 g_object_class_install_property (gobject_class,
790 TILDA_WINDOW_CENTERED_VERTICALLY,
793 pspec = g_param_spec_boolean ("have-real-transparency",
794 NULL, NULL, FALSE, G_PARAM_READABLE);
796 g_object_class_install_property (gobject_class,
797 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
800 /* TODO: more properties */
802 /* Hook the TildaWindow type into DBus */
803 dbus_g_object_type_install_info (tilda_window_get_type(), &dbus_glib_tilda_window_object_info);
807 tilda_window_get_type (void)
809 static GType type = 0;
813 static const GTypeInfo info = {
814 sizeof (TildaWindowClass),
815 NULL, /* base_init */
816 NULL, /* base_finalize */
817 tilda_window_class_init, /* class_init */
818 NULL, /* class_finalize */
819 NULL, /* class_data */
820 sizeof (TildaWindow),
822 tilda_window_instance_init, /* instance_init */
825 type = g_type_register_static (G_TYPE_OBJECT,
836 int main (int argc, char *argv[])
839 gint test_number = INT_MIN;
841 /* Initialize the GObject type system */
843 gtk_init (&argc, &argv);
845 tw = g_object_new (TILDA_TYPE_WINDOW, "number", 10, NULL);
846 g_object_get (G_OBJECT (tw), "number", &test_number, NULL);
847 g_assert (test_number == 10);
849 g_object_unref (G_OBJECT (tw));
851 tw = g_object_new (TILDA_TYPE_WINDOW, "number", 22, NULL);
852 g_object_get (G_OBJECT (tw), "number", &test_number, NULL);
853 g_assert (test_number == 22);
857 g_object_unref (G_OBJECT (tw));
864 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */