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);
164 g_print ("tilda_window_keybinding_cb() called! -- window %d\n", self->number);
166 // FIXME: this doesn't handle animation!
172 tomboy_window_present_hardcore (GTK_WINDOW(self->window));
173 self->state = WINDOW_DOWN;
175 // Focusing the term here works perfectly, near as I can tell
176 tt = tilda_window_find_current_terminal (self);
177 gtk_widget_grab_focus (GTK_WIDGET(tt->vte_term));
182 gtk_widget_hide (GTK_WIDGET(self->window));
183 self->state = WINDOW_UP;
187 g_printerr ("FIXME: the window is in a bad state!\n");
189 /* Pretend we're down, for good measure.... */
190 self->state = WINDOW_DOWN;
196 * Attempt to bind the new_key to show this window.
198 * Return: TRUE if successful, FALSE otherwise.
201 tilda_window_try_to_bind_key (TildaWindow *self, const gchar *new_key)
203 gboolean ret = FALSE;
205 /* Make sure the new key is not null in any way */
206 if (new_key == NULL || strcmp("", new_key) == 0)
209 /* Unbind if we were set */
211 tomboy_keybinder_unbind (self->key, tilda_window_keybinding_cb, self);
213 ret = tomboy_keybinder_bind (new_key, tilda_window_keybinding_cb, self);
215 /* If it was successful, update the self->key variable and be done with it */
219 self->key = g_strdup (new_key);
223 g_printerr ("Keybinding unsuccessful. Reverting to original key\n");
225 /* Not successful, so rebind the old key, and return FALSE */
226 if (self->key != NULL && strcmp("",self->key) != 0)
228 ret = tomboy_keybinder_bind (self->key, tilda_window_keybinding_cb, self);
230 /* Check that it went ok */
232 g_printerr ("Unable to bind original key as well! Oh shit...\n");
235 g_printerr ("No original key to revert to!\n");
241 tilda_window_dbus_register_object (TildaWindow *tw)
245 // Register this object with DBus
246 object_path = g_strdup_printf ("/net/sourceforge/Tilda/Window%d", tw->number);
247 dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(tw));
248 g_free (object_path);
251 /*******************************************************************************
252 * ALL GOBJECT STUFF BELOW PLEASE
253 ******************************************************************************/
255 static GObjectClass *parent_class = NULL;
257 enum tilda_window_properties {
258 TILDA_WINDOW_NUMBER = 1,
262 TILDA_WINDOW_MAX_HEIGHT,
263 TILDA_WINDOW_MAX_WIDTH,
264 TILDA_WINDOW_X_POSITION,
265 TILDA_WINDOW_Y_POSITION,
267 TILDA_WINDOW_TAB_POSITION,
268 TILDA_WINDOW_ANIMATION_ORIENTATION,
269 TILDA_WINDOW_ANIMATION_DELAY,
271 TILDA_WINDOW_KEEP_ABOVE,
272 TILDA_WINDOW_SHOW_IN_TASKBAR,
274 TILDA_WINDOW_HIDDEN_AT_START,
275 TILDA_WINDOW_CENTERED_HORIZONTALLY,
276 TILDA_WINDOW_CENTERED_VERTICALLY,
278 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
282 tilda_window_instance_init (GTypeInstance *instance,
285 TildaWindow *self = (TildaWindow *) instance;
286 self->dispose_has_run = FALSE;
288 /* Initialize all properties */
289 self->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
290 self->notebook = gtk_notebook_new ();
291 self->terms = g_ptr_array_new ();
293 /* Somewhat of a "poison" value, incase we don't set this */
294 self->number = 0xdeadbeef;
296 self->state = WINDOW_UP;
300 tilda_window_set_property (GObject *object,
305 TildaWindow *self = (TildaWindow *) object;
307 switch (property_id) {
309 case TILDA_WINDOW_NUMBER:
310 self->number = g_value_get_int (value);
311 g_print ("window number: %d\n", self->number);
314 case TILDA_WINDOW_KEY:
315 tilda_window_try_to_bind_key (self, g_value_get_string (value));
316 g_print ("window key: %s\n", self->key);
319 case TILDA_WINDOW_MAX_HEIGHT:
320 self->max_height = g_value_get_int (value);
321 g_print ("window max height: %d\n", self->max_height);
324 case TILDA_WINDOW_MAX_WIDTH:
325 self->max_width = g_value_get_int (value);
326 g_print ("window max width: %d\n", self->max_width);
329 case TILDA_WINDOW_X_POSITION:
330 self->x_position = g_value_get_int (value);
331 gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
332 g_print ("window x position: %d\n", self->x_position);
335 case TILDA_WINDOW_Y_POSITION:
336 self->y_position = g_value_get_int (value);
337 gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
338 g_print ("window y position: %d\n", self->y_position);
341 case TILDA_WINDOW_TAB_POSITION:
342 self->tab_position = g_value_get_int (value);
343 g_print ("window tab position: %d\n", self->tab_position);
346 case TILDA_WINDOW_ANIMATION_ORIENTATION:
347 self->animation_orientation = g_value_get_int (value);
348 g_print ("window animation orientation: %d\n", self->animation_orientation);
351 case TILDA_WINDOW_ANIMATION_DELAY:
352 self->animation_delay = g_value_get_int (value);
353 g_print ("window animation delay: %d\n", self->animation_delay);
356 case TILDA_WINDOW_KEEP_ABOVE:
357 self->keep_above = g_value_get_boolean (value);
358 g_print ("window keep above: %d\n", self->keep_above);
361 case TILDA_WINDOW_SHOW_IN_TASKBAR:
362 self->show_in_taskbar = g_value_get_boolean (value);
363 g_print ("window show in taskbar: %d\n", self->show_in_taskbar);
366 case TILDA_WINDOW_PINNED:
367 self->pinned = g_value_get_boolean (value);
368 g_print ("window pinned: %d\n", self->pinned);
371 case TILDA_WINDOW_HIDDEN_AT_START:
372 self->hidden_at_start = g_value_get_boolean (value);
373 g_print ("window hidden at start: %d\n", self->hidden_at_start);
376 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
377 self->centered_horizontally = g_value_get_boolean (value);
378 g_print ("window centered horizontally: %d\n", self->centered_horizontally);
381 case TILDA_WINDOW_CENTERED_VERTICALLY:
382 self->centered_vertically = g_value_get_boolean (value);
383 g_print ("window centered vertically: %d\n", self->centered_vertically);
386 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
387 self->have_real_transparency = g_value_get_boolean (value);
388 g_print ("window have real transp: %d\n", self->have_real_transparency);
392 /* We don't have this property */
393 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
399 tilda_window_get_property (GObject *object,
404 TildaWindow *self = (TildaWindow *) object;
406 switch (property_id) {
408 case TILDA_WINDOW_NUMBER:
409 g_value_set_int (value, self->number);
412 case TILDA_WINDOW_KEY:
413 g_value_set_string (value, self->key);
416 case TILDA_WINDOW_MAX_HEIGHT:
417 g_value_set_int (value, self->max_height);
420 case TILDA_WINDOW_MAX_WIDTH:
421 g_value_set_int (value, self->max_width);
424 case TILDA_WINDOW_X_POSITION:
425 g_value_set_int (value, self->x_position);
428 case TILDA_WINDOW_Y_POSITION:
429 g_value_set_int (value, self->y_position);
432 case TILDA_WINDOW_TAB_POSITION:
433 g_value_set_int (value, self->tab_position);
436 case TILDA_WINDOW_ANIMATION_ORIENTATION:
437 g_value_set_int (value, self->animation_orientation);
440 case TILDA_WINDOW_ANIMATION_DELAY:
441 g_value_set_int (value, self->animation_delay);
444 case TILDA_WINDOW_KEEP_ABOVE:
445 g_value_set_boolean (value, self->keep_above);
448 case TILDA_WINDOW_SHOW_IN_TASKBAR:
449 g_value_set_boolean (value, self->show_in_taskbar);
452 case TILDA_WINDOW_PINNED:
453 g_value_set_boolean (value, self->pinned);
456 case TILDA_WINDOW_HIDDEN_AT_START:
457 g_value_set_boolean (value, self->hidden_at_start);
460 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
461 g_value_set_boolean (value, self->centered_horizontally);
464 case TILDA_WINDOW_CENTERED_VERTICALLY:
465 g_value_set_boolean (value, self->centered_vertically);
468 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
469 g_value_set_boolean (value, self->have_real_transparency);
473 /* We don't have this property */
474 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
480 tilda_window_constructor (GType type,
481 guint n_construct_properties,
482 GObjectConstructParam *construct_properties)
487 /* Invoke parent constructor */
488 TildaWindowClass *klass;
489 klass = TILDA_WINDOW_CLASS (g_type_class_peek (TILDA_TYPE_WINDOW));
490 obj = parent_class->constructor (type,
491 n_construct_properties,
492 construct_properties);
494 /* Do other stuff here. The object is ready to go now, and all
495 * ctor properties have been set.
497 * TODO: This is the place to do DBus-init */
498 self = TILDA_WINDOW(obj);
500 /* Register this object with DBus */
501 tilda_window_dbus_register_object (self);
503 /* Try to set up real transparency */
504 tilda_window_setup_real_transparency (self);
506 gtk_container_add (GTK_CONTAINER(self->window), self->notebook);
507 gtk_widget_show (self->notebook);
509 // FIXME: Remove these, and replace with reads from the config system
510 g_object_set (G_OBJECT(self), "key", "F2", NULL);
511 g_object_set (G_OBJECT(self), "x-position", 0, "y-position", 0, NULL);
513 gtk_window_set_decorated (GTK_WINDOW(self->window), FALSE);
515 tilda_window_add_term (self);
516 tilda_window_add_term (self);
517 gtk_widget_show_all (self->window);
518 self->state = WINDOW_DOWN;
524 my_unref (gpointer data, gpointer user_data)
526 g_object_unref (G_OBJECT(data));
530 tilda_window_dispose (GObject *obj)
532 TildaWindow *self = (TildaWindow *) obj;
534 /* We don't want to run dispose twice, so just return immediately */
535 if (self->dispose_has_run)
539 * In dispose, you are supposed to free all types referenced from this
540 * object which might themselves hold a reference to self. Generally,
541 * the most simple solution is to unref all members on which you own a
544 * NOTE: See the following for how to deal with GtkObject-derived things:
545 * http://library.gnome.org/devel/gtk/unstable/GtkObject.html
547 g_ptr_array_foreach (self->terms, my_unref, NULL);
548 gtk_widget_destroy (self->window);
550 /* Chain up to the parent class */
551 G_OBJECT_CLASS (parent_class)->dispose (obj);
555 tilda_window_finalize (GObject *obj)
557 TildaWindow *self = (TildaWindow *) obj;
560 * Here, complete the object's destruction.
561 * You might not need to do much...
563 // TODO: g_free() any primitives here
564 g_ptr_array_free (self->terms, TRUE);
567 /* Chain up to the parent class */
568 G_OBJECT_CLASS (parent_class)->finalize (obj);
572 tilda_window_class_init (gpointer g_class,
573 gpointer g_class_data)
575 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
576 TildaWindowClass *klass = TILDA_WINDOW_CLASS (g_class);
579 /* Hook our functions to this type */
580 gobject_class->set_property = tilda_window_set_property;
581 gobject_class->get_property = tilda_window_get_property;
582 gobject_class->dispose = tilda_window_dispose;
583 gobject_class->finalize = tilda_window_finalize;
584 gobject_class->constructor = tilda_window_constructor;
586 parent_class = g_type_class_peek_parent (klass);
588 /* Install all of the properties */
589 pspec = g_param_spec_int ("number",
591 "Set window's number",
593 INT_MAX, // max value
595 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
597 g_object_class_install_property (gobject_class,
601 pspec = g_param_spec_string ("key",
602 "Window's drop-down keybinding",
607 g_object_class_install_property (gobject_class,
611 pspec = g_param_spec_int ("max-height",
612 "Window's maximum height",
619 g_object_class_install_property (gobject_class,
620 TILDA_WINDOW_MAX_HEIGHT,
623 pspec = g_param_spec_int ("max-width",
624 "Window's maximum width",
631 g_object_class_install_property (gobject_class,
632 TILDA_WINDOW_MAX_WIDTH,
635 pspec = g_param_spec_int ("x-position",
636 "Window's x position",
643 g_object_class_install_property (gobject_class,
644 TILDA_WINDOW_X_POSITION,
647 pspec = g_param_spec_int ("y-position",
648 "Window's y position",
655 g_object_class_install_property (gobject_class,
656 TILDA_WINDOW_Y_POSITION,
659 pspec = g_param_spec_int ("tab-position",
660 "Window's tab position",
667 g_object_class_install_property (gobject_class,
668 TILDA_WINDOW_TAB_POSITION,
671 pspec = g_param_spec_int ("animation-orientation",
672 "Window's animation orientation",
679 g_object_class_install_property (gobject_class,
680 TILDA_WINDOW_ANIMATION_ORIENTATION,
683 pspec = g_param_spec_int ("animation-delay",
684 "Amount of time in milliseconds between animation intervals",
691 g_object_class_install_property (gobject_class,
692 TILDA_WINDOW_ANIMATION_DELAY,
695 pspec = g_param_spec_boolean ("keep-above",
696 "Keep this window above all others",
701 g_object_class_install_property (gobject_class,
702 TILDA_WINDOW_KEEP_ABOVE,
705 pspec = g_param_spec_boolean ("show-in-taskbar",
706 "Show this window in the taskbar",
711 g_object_class_install_property (gobject_class,
712 TILDA_WINDOW_SHOW_IN_TASKBAR,
715 pspec = g_param_spec_boolean ("pinned",
716 "Display this window on all workspaces",
721 g_object_class_install_property (gobject_class,
725 pspec = g_param_spec_boolean ("hidden-at-start",
726 "Hide the window when it is first created",
731 g_object_class_install_property (gobject_class,
732 TILDA_WINDOW_HIDDEN_AT_START,
735 pspec = g_param_spec_boolean ("centered-horizontally",
736 "Center the window horizontally",
741 g_object_class_install_property (gobject_class,
742 TILDA_WINDOW_CENTERED_HORIZONTALLY,
745 pspec = g_param_spec_boolean ("centered-vertically",
746 "Center the window vertically",
751 g_object_class_install_property (gobject_class,
752 TILDA_WINDOW_CENTERED_VERTICALLY,
755 pspec = g_param_spec_boolean ("have-real-transparency",
756 NULL, NULL, FALSE, G_PARAM_READABLE);
758 g_object_class_install_property (gobject_class,
759 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
762 /* TODO: more properties */
764 /* Hook the TildaWindow type into DBus */
765 dbus_g_object_type_install_info (tilda_window_get_type(), &dbus_glib_tilda_window_object_info);
769 tilda_window_get_type (void)
771 static GType type = 0;
775 static const GTypeInfo info = {
776 sizeof (TildaWindowClass),
777 NULL, /* base_init */
778 NULL, /* base_finalize */
779 tilda_window_class_init, /* class_init */
780 NULL, /* class_finalize */
781 NULL, /* class_data */
782 sizeof (TildaWindow),
784 tilda_window_instance_init, /* instance_init */
787 type = g_type_register_static (G_TYPE_OBJECT,
798 int main (int argc, char *argv[])
801 gint test_number = INT_MIN;
803 /* Initialize the GObject type system */
805 gtk_init (&argc, &argv);
807 tw = g_object_new (TILDA_TYPE_WINDOW, "number", 10, NULL);
808 g_object_get (G_OBJECT (tw), "number", &test_number, NULL);
809 g_assert (test_number == 10);
811 g_object_unref (G_OBJECT (tw));
813 tw = g_object_new (TILDA_TYPE_WINDOW, "number", 22, NULL);
814 g_object_get (G_OBJECT (tw), "number", &test_number, NULL);
815 g_assert (test_number == 22);
819 g_object_unref (G_OBJECT (tw));
826 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */