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,
79 "window-number", tw->number,
82 g_ptr_array_add (tw->terms, tt);
84 GtkWidget *label = gtk_label_new ("Tilda");
85 gint index = gtk_notebook_prepend_page (GTK_NOTEBOOK(tw->notebook), tt->hbox, label);
86 gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK(tw->notebook), tt->hbox, TRUE, TRUE, GTK_PACK_END);
87 gtk_notebook_set_current_page (GTK_NOTEBOOK(tw->notebook), index);
89 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK(tw->notebook)) > 1)
90 gtk_notebook_set_show_tabs (GTK_NOTEBOOK(tw->notebook), TRUE);
96 * Remove the TildaTerminal with the given number from the given
99 * Return: TRUE on success, FALSE otherwise.
102 tilda_window_remove_term (TildaWindow *tw, gint terminal_number)
105 debug_assert (TILDA_IS_WINDOW(tw));
106 debug_assert (terminal_number >= 0);
110 for (i=0; i<tw->terms->len; ++i)
112 TildaTerminal *tt = g_ptr_array_index (tw->terms, i);
114 if (tt->number == terminal_number)
116 gint notebook_index = gtk_notebook_page_num (GTK_NOTEBOOK(tw->notebook), tt->hbox);
118 /* Make sure the index was valid */
119 if (notebook_index == -1)
121 debug_printf ("ERROR: Bad Notebook Tab\n");
125 /* Actually remove the terminal */
126 gtk_notebook_remove_page (GTK_NOTEBOOK (tw->notebook), notebook_index);
128 /* We should hide the tabs if there is only one tab left */
129 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (tw->notebook)) == 1)
130 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (tw->notebook), FALSE);
132 /* Remove the term from our lists, then free it */
133 g_ptr_array_remove_fast (tw->terms, tt);
134 g_object_unref (G_OBJECT(tt));
136 /* With no pages left, it's time to remove this window */
137 if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (tw->notebook)) < 1)
139 debug_printf ("no terminals left, closing window %d\n", tw->number);
140 tilda_del_window (tw->number);
143 /* Leave the loop, we're done */
152 * This sets up the given TildaWindow for the capability of real
153 * transparency, if the X server is capable of it. */
155 tilda_window_setup_real_transparency (TildaWindow *self)
158 debug_assert (TILDA_IS_WINDOW(self));
161 GdkColormap *colormap;
163 screen = gtk_widget_get_screen (GTK_WIDGET(self->window));
164 colormap = gdk_screen_get_rgba_colormap (screen);
166 /* If possible, set the RGBA colormap so VTE can use real alpha
167 * channels for transparency. */
168 if (colormap != NULL && gdk_screen_is_composited (screen))
170 gtk_widget_set_colormap (GTK_WIDGET(self->window), colormap);
171 self->have_real_transparency = TRUE;
175 self->have_real_transparency = FALSE;
178 /* Center the given TildaWindow in the horizontal axis */
180 tilda_window_center_horizontally (TildaWindow *self)
183 debug_assert (TILDA_IS_WINDOW(self));
185 const gint screen_center = gdk_screen_width() / 2;
186 const gint tilda_center = self->width / 2;
187 const gint center_coord = screen_center - tilda_center;
189 g_object_set (G_OBJECT(self), "x-position", center_coord, NULL);
192 /* Center the given TildaWindow in the vertical axis */
194 tilda_window_center_vertically (TildaWindow *self)
197 debug_assert (TILDA_IS_WINDOW(self));
199 const gint screen_center = gdk_screen_height() / 2;
200 const gint tilda_center = self->height / 2;
201 const gint center_coord = screen_center - tilda_center;
203 g_object_set (G_OBJECT(self), "y-position", center_coord, NULL);
207 tilda_window_keybinding_cb (const gchar *keystr, gpointer data)
210 debug_assert (TILDA_IS_WINDOW(data));
212 TildaWindow *self = TILDA_WINDOW(data);
215 // FIXME: this doesn't handle animation!
219 case WINDOW_UP: /* Pull the window up */
221 /* Bugfix: having this here keeps the tilda window from being
222 * hidden if you turn off "stick", pull it down on workspace 1,
223 * switch to workspace 2, then pull it up and back down. Without
224 * this, something in metacity (at least) hides the window. Stupid. */
225 gtk_window_deiconify (GTK_WINDOW(self->window));
227 /* Re-set the window properties that do not linger after hiding the
228 * window. I know this looks stupid, but it keeps all of the state-
229 * changing code in the place it belongs: the property-setting code. */
230 g_object_set (G_OBJECT(self),
231 "keep-above", self->keep_above,
232 "stick", self->stick,
234 gtk_window_present_with_time (GTK_WINDOW(self->window),
235 tomboy_keybinder_get_current_event_time());
237 /* Focusing the term here works perfectly, near as I can tell */
238 tt = tilda_window_find_current_terminal (self);
239 gtk_widget_grab_focus (GTK_WIDGET(tt->vte_term));
241 self->state = WINDOW_DOWN;
244 case WINDOW_DOWN: /* Pull the window up */
246 gtk_widget_hide (GTK_WIDGET(self->window));
248 self->state = WINDOW_UP;
252 debug_printf ("ERROR: Window is in a bad state!\n");
254 /* Pretend we're down, for good measure.... */
255 self->state = WINDOW_DOWN;
261 * Attempt to bind the new_key to show this window.
263 * Return: TRUE if successful, FALSE otherwise.
266 tilda_window_try_to_bind_key (TildaWindow *self, const gchar *new_key)
269 debug_assert (TILDA_IS_WINDOW(self));
271 gboolean ret = FALSE;
273 /* Make sure the new key is not null in any way */
274 if (new_key == NULL || strcmp("", new_key) == 0)
277 /* Unbind if we were set */
279 tomboy_keybinder_unbind (self->key, tilda_window_keybinding_cb);
281 ret = tomboy_keybinder_bind (new_key, tilda_window_keybinding_cb, self);
283 /* If it was successful, update the self->key variable and be done with it */
287 self->key = g_strdup (new_key);
291 g_printerr ("Keybinding unsuccessful. Reverting to original key\n");
293 /* Not successful, so rebind the old key, and return FALSE */
294 if (self->key != NULL && strcmp("",self->key) != 0)
296 ret = tomboy_keybinder_bind (self->key, tilda_window_keybinding_cb, self);
298 /* Check that it went ok */
300 g_printerr ("Unable to bind original key as well! Oh shit...\n");
303 g_printerr ("No original key to revert to!\n");
309 tilda_window_dbus_register_object (TildaWindow *tw)
312 debug_assert (TILDA_IS_WINDOW(tw));
316 // Register this object with DBus
317 object_path = g_strdup_printf ("/net/sourceforge/Tilda/Window%d", tw->number);
318 dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(tw));
319 g_free (object_path);
322 /*******************************************************************************
323 * ALL GOBJECT STUFF BELOW PLEASE
324 ******************************************************************************/
326 static GObjectClass *parent_class = NULL;
328 enum tilda_window_properties {
329 TILDA_WINDOW_NUMBER = 1,
335 TILDA_WINDOW_X_POSITION,
336 TILDA_WINDOW_Y_POSITION,
338 TILDA_WINDOW_TAB_POSITION,
339 TILDA_WINDOW_ANIMATION_ORIENTATION,
340 TILDA_WINDOW_ANIMATION_DELAY,
342 TILDA_WINDOW_KEEP_ABOVE,
343 TILDA_WINDOW_SKIP_TASKBAR_HINT,
345 TILDA_WINDOW_HIDDEN_AT_START,
346 TILDA_WINDOW_CENTERED_HORIZONTALLY,
347 TILDA_WINDOW_CENTERED_VERTICALLY,
349 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
353 tilda_window_instance_init (GTypeInstance *instance,
358 TildaWindow *self = (TildaWindow *) instance;
359 self->dispose_has_run = FALSE;
361 /* Initialize all properties */
362 self->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
363 self->notebook = gtk_notebook_new ();
364 self->terms = g_ptr_array_new ();
366 /* Somewhat of a "poison" value, incase we don't set this */
367 self->number = 0xdeadbeef;
369 self->state = WINDOW_UP;
373 tilda_window_set_property (GObject *object,
378 TildaWindow *self = (TildaWindow *) object;
380 switch (property_id) {
382 case TILDA_WINDOW_NUMBER:
383 self->number = g_value_get_int (value);
384 debug_printf ("window number: %d\n", self->number);
387 case TILDA_WINDOW_KEY:
388 tilda_window_try_to_bind_key (self, g_value_get_string (value));
389 debug_printf ("window key %s\n", self->key);
392 case TILDA_WINDOW_HEIGHT:
393 self->height = g_value_get_int (value);
394 gtk_widget_set_size_request (self->window, self->width, self->height);
395 gtk_window_resize (GTK_WINDOW(self->window), self->width, self->height);
396 debug_printf ("window height: %d\n", self->height);
399 case TILDA_WINDOW_WIDTH:
400 self->width = g_value_get_int (value);
401 gtk_widget_set_size_request (self->window, self->width, self->height);
402 gtk_window_resize (GTK_WINDOW(self->window), self->width, self->height);
403 debug_printf ("window width: %d\n", self->width);
406 case TILDA_WINDOW_X_POSITION:
407 self->x_position = g_value_get_int (value);
408 gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
409 debug_printf ("window x position: %d\n", self->x_position);
412 case TILDA_WINDOW_Y_POSITION:
413 self->y_position = g_value_get_int (value);
414 gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
415 debug_printf ("window y position: %d\n", self->y_position);
418 case TILDA_WINDOW_TAB_POSITION:
419 self->tab_position = g_value_get_int (value);
420 gtk_notebook_set_tab_pos (GTK_NOTEBOOK(self->notebook), self->tab_position);
421 debug_printf ("window tab position: %d\n", self->tab_position);
424 case TILDA_WINDOW_ANIMATION_ORIENTATION:
425 self->animation_orientation = g_value_get_int (value);
426 debug_printf ("window animation orientation: %d\n", self->animation_orientation);
429 case TILDA_WINDOW_ANIMATION_DELAY:
430 self->animation_delay = g_value_get_int (value);
431 debug_printf ("window animation delay: %d\n", self->animation_delay);
434 case TILDA_WINDOW_KEEP_ABOVE:
435 self->keep_above = g_value_get_boolean (value);
436 gtk_window_set_keep_above (GTK_WINDOW(self->window), self->keep_above);
437 debug_printf ("window keep above: %d\n", self->keep_above);
440 case TILDA_WINDOW_SKIP_TASKBAR_HINT:
441 self->skip_taskbar_hint = g_value_get_boolean (value);
442 gtk_window_set_skip_taskbar_hint (GTK_WINDOW(self->window), self->skip_taskbar_hint);
443 debug_printf ("window skip taskbar hint: %d\n", self->skip_taskbar_hint);
446 case TILDA_WINDOW_STICK:
447 self->stick = g_value_get_boolean (value);
449 /* This is moderately ugly, but GTK+ does it this way... */
450 self->stick ? gtk_window_stick (GTK_WINDOW(self->window))
451 : gtk_window_unstick (GTK_WINDOW(self->window));
452 debug_printf ("window stick: %d\n", self->stick);
455 case TILDA_WINDOW_HIDDEN_AT_START:
456 self->hidden_at_start = g_value_get_boolean (value);
457 debug_printf ("window hidden at start: %d\n", self->hidden_at_start);
460 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
461 self->centered_horizontally = g_value_get_boolean (value);
462 if (self->centered_horizontally)
463 tilda_window_center_horizontally (self);
464 debug_printf ("window centered horizontally: %d\n", self->centered_horizontally);
467 case TILDA_WINDOW_CENTERED_VERTICALLY:
468 self->centered_vertically = g_value_get_boolean (value);
469 if (self->centered_vertically)
470 tilda_window_center_vertically (self);
471 debug_printf ("window centered vertically: %d\n", self->centered_vertically);
474 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
475 self->have_real_transparency = g_value_get_boolean (value);
476 debug_printf ("window have real transp: %d\n", self->have_real_transparency);
480 /* We don't have this property */
481 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
487 tilda_window_get_property (GObject *object,
492 TildaWindow *self = (TildaWindow *) object;
494 switch (property_id) {
496 case TILDA_WINDOW_NUMBER:
497 g_value_set_int (value, self->number);
500 case TILDA_WINDOW_KEY:
501 g_value_set_string (value, self->key);
504 case TILDA_WINDOW_HEIGHT:
505 g_value_set_int (value, self->height);
508 case TILDA_WINDOW_WIDTH:
509 g_value_set_int (value, self->width);
512 case TILDA_WINDOW_X_POSITION:
513 g_value_set_int (value, self->x_position);
516 case TILDA_WINDOW_Y_POSITION:
517 g_value_set_int (value, self->y_position);
520 case TILDA_WINDOW_TAB_POSITION:
521 g_value_set_int (value, self->tab_position);
524 case TILDA_WINDOW_ANIMATION_ORIENTATION:
525 g_value_set_int (value, self->animation_orientation);
528 case TILDA_WINDOW_ANIMATION_DELAY:
529 g_value_set_int (value, self->animation_delay);
532 case TILDA_WINDOW_KEEP_ABOVE:
533 g_value_set_boolean (value, self->keep_above);
536 case TILDA_WINDOW_SKIP_TASKBAR_HINT:
537 g_value_set_boolean (value, self->skip_taskbar_hint);
540 case TILDA_WINDOW_STICK:
541 g_value_set_boolean (value, self->stick);
544 case TILDA_WINDOW_HIDDEN_AT_START:
545 g_value_set_boolean (value, self->hidden_at_start);
548 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
549 g_value_set_boolean (value, self->centered_horizontally);
552 case TILDA_WINDOW_CENTERED_VERTICALLY:
553 g_value_set_boolean (value, self->centered_vertically);
556 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
557 g_value_set_boolean (value, self->have_real_transparency);
561 /* We don't have this property */
562 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
568 tilda_window_constructor (GType type,
569 guint n_construct_properties,
570 GObjectConstructParam *construct_properties)
577 /* Invoke parent constructor */
578 TildaWindowClass *klass;
579 klass = TILDA_WINDOW_CLASS (g_type_class_peek (TILDA_TYPE_WINDOW));
580 obj = parent_class->constructor (type,
581 n_construct_properties,
582 construct_properties);
584 /* Do other stuff here. The object is ready to go now, and all
585 * ctor properties have been set.
587 * TODO: This is the place to do DBus-init */
588 self = TILDA_WINDOW(obj);
590 /* Register this object with DBus */
591 tilda_window_dbus_register_object (self);
593 /* Try to set up real transparency */
594 tilda_window_setup_real_transparency (self);
596 gtk_container_add (GTK_CONTAINER(self->window), self->notebook);
597 gtk_widget_show (self->notebook);
599 // FIXME: Remove these, and replace with reads from the config system
600 g_object_set (G_OBJECT(self), "key", "F2", NULL);
601 g_object_set (G_OBJECT(self), "x-position", 0, "y-position", 0, NULL);
602 g_object_set (G_OBJECT(self), "height", 400, "width", 1680, NULL);
603 g_object_set (G_OBJECT(self), "keep-above", TRUE, "stick", TRUE, NULL);
604 g_object_set (G_OBJECT(self), "hidden-at-start", FALSE, NULL);
606 gtk_window_set_decorated (GTK_WINDOW(self->window), FALSE);
608 // FIXME: It should be configurable how many terms we add at startup
609 tilda_window_add_term (self);
610 tilda_window_add_term (self);
612 /* Show us if we're ready. If not, just remain hidden. All sub-widgets must
613 * be gtk_widget_show()n by this point. */
614 if (!self->hidden_at_start)
616 gtk_widget_show (self->window);
617 self->state = WINDOW_DOWN;
620 self->state = WINDOW_UP;
626 my_unref (gpointer data, gpointer user_data)
630 // FIXME: This function should probably be eliminated. It /is/ rather ugly
631 g_object_unref (G_OBJECT(data));
635 tilda_window_dispose (GObject *obj)
639 TildaWindow *self = (TildaWindow *) obj;
641 /* We don't want to run dispose twice, so just return immediately */
642 if (self->dispose_has_run)
646 * In dispose, you are supposed to free all types referenced from this
647 * object which might themselves hold a reference to self. Generally,
648 * the most simple solution is to unref all members on which you own a
651 * NOTE: See the following for how to deal with GtkObject-derived things:
652 * http://library.gnome.org/devel/gtk/unstable/GtkObject.html
654 g_ptr_array_foreach (self->terms, my_unref, NULL);
655 gtk_widget_destroy (self->window);
657 /* Chain up to the parent class */
658 G_OBJECT_CLASS (parent_class)->dispose (obj);
662 tilda_window_finalize (GObject *obj)
666 TildaWindow *self = (TildaWindow *) obj;
669 * Here, complete the object's destruction.
670 * You might not need to do much...
672 // TODO: g_free() any primitives here
673 g_ptr_array_free (self->terms, TRUE);
676 /* Chain up to the parent class */
677 G_OBJECT_CLASS (parent_class)->finalize (obj);
681 tilda_window_class_init (gpointer g_class,
682 gpointer g_class_data)
686 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
687 TildaWindowClass *klass = TILDA_WINDOW_CLASS (g_class);
690 /* Hook our functions to this type */
691 gobject_class->set_property = tilda_window_set_property;
692 gobject_class->get_property = tilda_window_get_property;
693 gobject_class->dispose = tilda_window_dispose;
694 gobject_class->finalize = tilda_window_finalize;
695 gobject_class->constructor = tilda_window_constructor;
697 parent_class = g_type_class_peek_parent (klass);
699 /* Install all of the properties */
700 pspec = g_param_spec_int ("number",
702 "Set window's number",
704 INT_MAX, // max value
706 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
708 g_object_class_install_property (gobject_class,
712 pspec = g_param_spec_string ("key",
713 "Window's drop-down keybinding",
718 g_object_class_install_property (gobject_class,
722 pspec = g_param_spec_int ("height",
730 g_object_class_install_property (gobject_class,
734 pspec = g_param_spec_int ("width",
742 g_object_class_install_property (gobject_class,
746 pspec = g_param_spec_int ("x-position",
747 "Window's x position",
754 g_object_class_install_property (gobject_class,
755 TILDA_WINDOW_X_POSITION,
758 pspec = g_param_spec_int ("y-position",
759 "Window's y position",
766 g_object_class_install_property (gobject_class,
767 TILDA_WINDOW_Y_POSITION,
770 pspec = g_param_spec_int ("tab-position",
771 "Window's tab position",
778 g_object_class_install_property (gobject_class,
779 TILDA_WINDOW_TAB_POSITION,
782 pspec = g_param_spec_int ("animation-orientation",
783 "Window's animation orientation",
790 g_object_class_install_property (gobject_class,
791 TILDA_WINDOW_ANIMATION_ORIENTATION,
794 pspec = g_param_spec_int ("animation-delay",
795 "Amount of time in milliseconds between animation intervals",
802 g_object_class_install_property (gobject_class,
803 TILDA_WINDOW_ANIMATION_DELAY,
806 pspec = g_param_spec_boolean ("keep-above",
807 "Keep this window above all others",
812 g_object_class_install_property (gobject_class,
813 TILDA_WINDOW_KEEP_ABOVE,
816 pspec = g_param_spec_boolean ("skip-taskbar-hint",
817 "Hide this window in the taskbar if TRUE",
822 g_object_class_install_property (gobject_class,
823 TILDA_WINDOW_SKIP_TASKBAR_HINT,
826 pspec = g_param_spec_boolean ("stick",
827 "Display this window on all workspaces",
832 g_object_class_install_property (gobject_class,
836 pspec = g_param_spec_boolean ("hidden-at-start",
837 "Hide the window when it is first created",
842 g_object_class_install_property (gobject_class,
843 TILDA_WINDOW_HIDDEN_AT_START,
846 pspec = g_param_spec_boolean ("centered-horizontally",
847 "Center the window horizontally",
852 g_object_class_install_property (gobject_class,
853 TILDA_WINDOW_CENTERED_HORIZONTALLY,
856 pspec = g_param_spec_boolean ("centered-vertically",
857 "Center the window vertically",
862 g_object_class_install_property (gobject_class,
863 TILDA_WINDOW_CENTERED_VERTICALLY,
866 pspec = g_param_spec_boolean ("have-real-transparency",
867 NULL, NULL, FALSE, G_PARAM_READABLE);
869 g_object_class_install_property (gobject_class,
870 TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
873 /* Hook the TildaWindow type into DBus */
874 dbus_g_object_type_install_info (tilda_window_get_type(), &dbus_glib_tilda_window_object_info);
878 tilda_window_get_type (void)
880 static GType type = 0;
884 static const GTypeInfo info = {
885 sizeof (TildaWindowClass),
886 NULL, /* base_init */
887 NULL, /* base_finalize */
888 tilda_window_class_init, /* class_init */
889 NULL, /* class_finalize */
890 NULL, /* class_data */
891 sizeof (TildaWindow),
893 tilda_window_instance_init, /* instance_init */
896 type = g_type_register_static (G_TYPE_OBJECT,
905 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */