[Window] Fix shadowed variable "index"
[tilda-gobject.git] / tilda-window.c
1 #include <gdk/gdkx.h> /* for gdk_x11_window_set_user_time() */
2 #include <stdlib.h>
3
4 #include "tilda.h"
5 #include "tilda-config.h"
6 #include "tilda-controller.h"
7 #include "tilda-window.h"
8 #include "tilda-window-dbus-glue.h"
9 #include "tomboykeybinder.h"
10
11 /**
12  * Find the TildaTerminal corresponding to the currently selected
13  * tab in self->notebook. This could go away if TildaTerminal were
14  * a proper subclass of GtkWidget.
15  */
16 static TildaTerminal *
17 tilda_window_find_current_terminal (TildaWindow *self)
18 {
19         debug_enter();
20         debug_assert (TILDA_IS_WINDOW(self));
21
22         gint i;
23         TildaTerminal *ret;
24         gint current_page = gtk_notebook_get_current_page (GTK_NOTEBOOK(self->notebook));
25         GtkWidget *box = gtk_notebook_get_nth_page (GTK_NOTEBOOK(self->notebook), current_page);
26
27         for (i=0; i<self->terms->len; ++i)
28         {
29                 ret = g_ptr_array_index (self->terms, i);
30
31                 if (ret->hbox == box)
32                         return ret;
33         }
34
35         debug_printf ("ERROR: unable to find current terminal!\n");
36         return NULL;
37 }
38
39 static gint
40 tilda_window_find_next_free_terminal_number (TildaWindow *self)
41 {
42         debug_enter ();
43         debug_assert (TILDA_IS_WINDOW(self));
44
45         gint i, j;
46         gboolean found;
47
48         for (i=0; i<INT_MAX; ++i)
49         {
50                 found = FALSE;
51
52                 for (j=0; j<self->terms->len; ++j)
53                 {
54                         TildaTerminal *tt = g_ptr_array_index (self->terms, j);
55
56                         if (tt->number == i)
57                         {
58                                 found = TRUE;
59                                 break;
60                         }
61                 }
62
63                 if (!found)
64                         return i;
65         }
66
67         return 0;
68 }
69
70 /**
71  * Clean up and remove self completely from the program
72  *
73  * Should only be used by DBus...
74  */
75 gboolean
76 tilda_window_close (TildaWindow *self)
77 {
78         debug_enter  ();
79         debug_assert (TILDA_IS_WINDOW(self));
80
81         tilda_controller_remove_window (TILDA_CONTROLLER(self->controller), self->number);
82
83         return TRUE;
84 }
85
86 gboolean
87 tilda_window_add_terminal (TildaWindow *self)
88 {
89         debug_enter ();
90         debug_assert (TILDA_IS_WINDOW(self));
91
92         gint number;
93         TildaTerminal *tt;
94         GtkWidget *label;
95         gint notebook_index;
96
97         number = tilda_window_find_next_free_terminal_number (self);
98         tt = g_object_new (TILDA_TYPE_TERMINAL,
99                                            "number", number,
100                                            "parent-window", self,
101                                            NULL);
102         g_ptr_array_add (self->terms, tt);
103
104         label = gtk_label_new ("Tilda");
105         notebook_index = gtk_notebook_prepend_page (GTK_NOTEBOOK(self->notebook), tt->hbox, label);
106         gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK(self->notebook), tt->hbox, TRUE, TRUE, GTK_PACK_END);
107         gtk_notebook_set_current_page (GTK_NOTEBOOK(self->notebook), notebook_index);
108
109         if (gtk_notebook_get_n_pages (GTK_NOTEBOOK(self->notebook)) > 1)
110                 gtk_notebook_set_show_tabs (GTK_NOTEBOOK(self->notebook), TRUE);
111
112         /* Focus the VTE Terminal */
113         gtk_widget_grab_focus (tt->vte_term);
114
115         return TRUE;
116 }
117
118 /**
119  * Remove the TildaTerminal with the given number from the given
120  * TildaWindow.
121  *
122  * Return: TRUE on success, FALSE otherwise.
123  */
124 gboolean
125 tilda_window_remove_terminal (TildaWindow *self, gint terminal_number)
126 {
127         debug_enter  ();
128         debug_assert (TILDA_IS_WINDOW(self));
129         debug_assert (terminal_number >= 0);
130
131         gint i;
132
133         for (i=0; i<self->terms->len; ++i)
134         {
135                 TildaTerminal *tt = g_ptr_array_index (self->terms, i);
136
137                 if (tt->number == terminal_number)
138                 {
139                         gint notebook_index = gtk_notebook_page_num (GTK_NOTEBOOK(self->notebook), tt->hbox);
140
141                         /* Make sure the index was valid */
142                         if (notebook_index == -1)
143                         {
144                                 debug_printf ("ERROR: Bad Notebook Tab\n");
145                                 return FALSE;
146                         }
147
148                         /* Actually remove the terminal */
149                         gtk_notebook_remove_page (GTK_NOTEBOOK (self->notebook), notebook_index);
150
151                         /* We should hide the tabs if there is only one tab left */
152                         if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (self->notebook)) == 1)
153                                 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (self->notebook), FALSE);
154
155                         /* Remove the term from our lists, then free it */
156                         g_ptr_array_remove_fast (self->terms, tt);
157                         g_object_unref (G_OBJECT(tt));
158
159                         /* With no pages left, it's time to remove this window */
160                         if (gtk_notebook_get_n_pages (GTK_NOTEBOOK (self->notebook)) < 1)
161                         {
162                                 debug_printf ("no terminals left, closing window %d\n", self->number);
163                                 tilda_controller_remove_window (TILDA_CONTROLLER(self->controller), self->number);
164                         }
165
166                         /* Leave the loop, we're done */
167                         break;
168                 }
169         }
170
171         return TRUE;
172 }
173
174 /**
175  * This sets up the given TildaWindow for the capability of real
176  * transparency, if the X server is capable of it. */
177 static void
178 tilda_window_setup_real_transparency (TildaWindow *self)
179 {
180         debug_enter  ();
181         debug_assert (TILDA_IS_WINDOW(self));
182
183         GdkScreen *screen;
184         GdkColormap *colormap;
185
186         screen = gtk_widget_get_screen (GTK_WIDGET(self->window));
187         colormap = gdk_screen_get_rgba_colormap (screen);
188
189         /* If possible, set the RGBA colormap so VTE can use real alpha
190          * channels for transparency. */
191         if (colormap != NULL && gdk_screen_is_composited (screen))
192         {
193                 gtk_widget_set_colormap (GTK_WIDGET(self->window), colormap);
194                 self->have_real_transparency = TRUE;
195                 return;
196         }
197
198         self->have_real_transparency = FALSE;
199 }
200
201 /* Center the given TildaWindow in the horizontal axis */
202 static void
203 tilda_window_center_horizontally (TildaWindow *self)
204 {
205         debug_enter  ();
206         debug_assert (TILDA_IS_WINDOW(self));
207
208         const gint screen_center = gdk_screen_width() / 2;
209         const gint tilda_center  = self->width / 2;
210         const gint center_coord  = screen_center - tilda_center;
211
212         g_object_set (G_OBJECT(self), "x-position", center_coord, NULL);
213 }
214
215 /* Center the given TildaWindow in the vertical axis */
216 static void
217 tilda_window_center_vertically (TildaWindow *self)
218 {
219         debug_enter  ();
220         debug_assert (TILDA_IS_WINDOW(self));
221
222         const gint screen_center = gdk_screen_height() / 2;
223         const gint tilda_center  = self->height / 2;
224         const gint center_coord  = screen_center - tilda_center;
225
226         g_object_set (G_OBJECT(self), "y-position", center_coord, NULL);
227 }
228
229 static void
230 tilda_window_keybinding_cb (const gchar *keystr, gpointer data)
231 {
232         debug_enter  ();
233         debug_assert (TILDA_IS_WINDOW(data));
234
235         TildaWindow *self = TILDA_WINDOW(data);
236         TildaTerminal *tt;
237
238         /* This call sets the X11 window property _NET_WM_USER_TIME, which GTK+ normally
239          * sets for us. However, because this callback is activated via a global keybinding,
240          * we see the event before GDK / GTK+ does. Therefore, to get the focus, we must
241          * set the property ourselves. */
242         gdk_x11_window_set_user_time (GTK_WIDGET(self->window)->window,
243                                                                   tomboy_keybinder_get_current_event_time());
244
245         switch (self->state)
246         {
247                 case WINDOW_UP: /* Pull the window up */
248
249                         /* Bugfix: having this here keeps the tilda window from being
250                          * hidden if you turn off "stick", pull it down on workspace 1,
251                          * switch to workspace 2, then pull it up and back down. Without
252                          * this, something in metacity (at least) hides the window. Stupid. */
253                         gtk_window_deiconify (GTK_WINDOW(self->window));
254
255                         /* Re-set the window properties that do not linger after hiding the
256                          * window. I know this looks stupid, but it keeps all of the state-
257                          * changing code in the place it belongs: the property-setting code. */
258                         g_object_set (G_OBJECT(self),
259                                         "keep-above", self->keep_above,
260                                         "stick", self->stick,
261                                         NULL);
262                         gtk_widget_show (GTK_WIDGET(self->window));
263
264                         /* Focusing the term here works perfectly, near as I can tell */
265                         tt = tilda_window_find_current_terminal (self);
266                         gtk_widget_grab_focus (GTK_WIDGET(tt->vte_term));
267
268                         self->state = WINDOW_DOWN;
269                         break;
270
271                 case WINDOW_DOWN: /* Pull the window up */
272
273                         gtk_widget_hide (GTK_WIDGET(self->window));
274
275                         self->state = WINDOW_UP;
276                         break;
277
278                 default:
279                         debug_printf ("ERROR: Window is in a bad state!\n");
280
281                         /* Pretend we're down, for good measure.... */
282                         self->state = WINDOW_DOWN;
283                         break;
284         }
285 }
286
287 /**
288  * Attempt to bind the new_key to show this window.
289  *
290  * Return: TRUE if successful, FALSE otherwise.
291  */
292 static gboolean
293 tilda_window_try_to_bind_key (TildaWindow *self, const gchar *new_key)
294 {
295         debug_enter  ();
296         debug_assert (TILDA_IS_WINDOW(self));
297
298         gboolean ret = FALSE;
299
300         /* Make sure the new key is not null in any way */
301         if (new_key == NULL || g_ascii_strcasecmp("", new_key) == 0)
302                 return FALSE;
303
304         /* Check that no other windows are using the key */
305         // FIXME: there should be a hidden option to disable this. Maybe some people want
306         // to have logs in two Tildas, and just show them with one key. Crazy...
307         if (tilda_controller_global_key_in_use(TILDA_CONTROLLER(self->controller), new_key))
308                 return FALSE;
309
310         /* Unbind if we were set */
311         if (self->key)
312                 tomboy_keybinder_unbind (self->key, tilda_window_keybinding_cb);
313
314         ret = tomboy_keybinder_bind (new_key, tilda_window_keybinding_cb, self);
315
316         /* If it was successful, update the self->key variable and be done with it */
317         if (ret)
318         {
319                 g_free (self->key);
320                 self->key = g_strdup (new_key);
321                 return TRUE;
322         }
323
324         g_printerr (_("Bind key '%s' failed. Reverting to original keybinding\n"), self->key);
325
326         /* Not successful, so rebind the old key, and return FALSE */
327         if (self->key != NULL && g_ascii_strcasecmp("",self->key) != 0)
328         {
329                 ret = tomboy_keybinder_bind (self->key, tilda_window_keybinding_cb, self);
330
331                 /* Check that it went ok */
332                 if (!ret)
333                         g_printerr (_("Unable to re-bind original key '%s'. Oh shit...\n"), self->key);
334         }
335         else
336                 g_printerr (_("No original key to revert to!\n"));
337
338         return FALSE;
339 }
340
341 static void
342 tilda_window_dbus_register_object (TildaWindow *self)
343 {
344         debug_enter  ();
345         debug_assert (TILDA_IS_WINDOW(self));
346
347         gchar *object_path;
348
349         // Register this object with DBus
350         object_path = g_strdup_printf ("/net/sourceforge/Tilda/Window%d", self->number);
351         dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(self));
352         g_free (object_path);
353 }
354
355 /*******************************************************************************
356  * ALL GOBJECT STUFF BELOW PLEASE
357  ******************************************************************************/
358
359 static GObjectClass *parent_class = NULL;
360
361 enum tilda_window_properties {
362         TILDA_WINDOW_NUMBER = 1,
363         TILDA_WINDOW_CONTROLLER,
364
365         TILDA_WINDOW_KEY,
366
367         TILDA_WINDOW_HEIGHT,
368         TILDA_WINDOW_WIDTH,
369         TILDA_WINDOW_X_POSITION,
370         TILDA_WINDOW_Y_POSITION,
371         TILDA_WINDOW_INITIAL_TERMINALS,
372
373         TILDA_WINDOW_TAB_POSITION,
374         TILDA_WINDOW_ANIMATION_ORIENTATION,
375         TILDA_WINDOW_ANIMATION_DELAY,
376
377         TILDA_WINDOW_KEEP_ABOVE,
378         TILDA_WINDOW_SKIP_TASKBAR_HINT,
379         TILDA_WINDOW_STICK,
380         TILDA_WINDOW_HIDDEN_AT_START,
381         TILDA_WINDOW_CENTERED_HORIZONTALLY,
382         TILDA_WINDOW_CENTERED_VERTICALLY,
383
384         TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
385 };
386
387 static void
388 tilda_window_instance_init (GTypeInstance *instance,
389                                                         gpointer       g_class)
390 {
391         debug_enter ();
392
393         TildaWindow *self = (TildaWindow *) instance;
394         self->dispose_has_run = FALSE;
395
396         /* Initialize all properties */
397         self->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
398         self->notebook = gtk_notebook_new ();
399         self->terms = g_ptr_array_new ();
400
401         /* Somewhat of a "poison" value, incase we don't set this */
402         self->number = 0xdeadbeef;
403         self->controller = NULL;
404
405         self->state = WINDOW_UP;
406 }
407
408 static void
409 tilda_window_set_property (GObject      *object,
410                                                    guint         property_id,
411                                                    const GValue *value,
412                                                    GParamSpec   *pspec)
413 {
414         TildaWindow *self = (TildaWindow *) object;
415
416         switch (property_id) {
417
418                 case TILDA_WINDOW_NUMBER:
419                         self->number = g_value_get_int (value);
420                         debug_printf ("window number: %d\n", self->number);
421                         break;
422
423                 case TILDA_WINDOW_CONTROLLER:
424                         self->controller = g_value_get_pointer (value);
425                         debug_printf ("window controller: 0x%x\n", self->controller);
426                         break;
427
428                 case TILDA_WINDOW_KEY:
429                         tilda_window_try_to_bind_key (self, g_value_get_string (value));
430                         debug_printf ("window key %s\n", self->key);
431                         break;
432
433                 case TILDA_WINDOW_HEIGHT:
434                         self->height = g_value_get_int (value);
435                         gtk_widget_set_size_request (self->window, self->width, self->height);
436                         gtk_window_resize (GTK_WINDOW(self->window), self->width, self->height);
437                         debug_printf ("window height: %d\n", self->height);
438                         break;
439
440                 case TILDA_WINDOW_WIDTH:
441                         self->width = g_value_get_int (value);
442                         gtk_widget_set_size_request (self->window, self->width, self->height);
443                         gtk_window_resize (GTK_WINDOW(self->window), self->width, self->height);
444                         debug_printf ("window width: %d\n", self->width);
445                         break;
446
447                 case TILDA_WINDOW_X_POSITION:
448                         self->x_position = g_value_get_int (value);
449                         gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
450                         debug_printf ("window x position: %d\n", self->x_position);
451                         break;
452
453                 case TILDA_WINDOW_Y_POSITION:
454                         self->y_position = g_value_get_int (value);
455                         gtk_window_move (GTK_WINDOW(self->window), self->x_position, self->y_position);
456                         debug_printf ("window y position: %d\n", self->y_position);
457                         break;
458
459                 case TILDA_WINDOW_INITIAL_TERMINALS:
460                         self->initial_terminals = g_value_get_int (value);
461                         debug_printf ("window initial terminals: %d\n", self->initial_terminals);
462                         break;
463
464                 case TILDA_WINDOW_TAB_POSITION:
465                         self->tab_position = g_value_get_enum (value);
466                         gtk_notebook_set_tab_pos (GTK_NOTEBOOK(self->notebook), self->tab_position);
467                         debug_printf ("window tab position: %d\n", self->tab_position);
468                         break;
469
470                 case TILDA_WINDOW_ANIMATION_ORIENTATION:
471                         self->animation_orientation = g_value_get_enum (value);
472                         debug_printf ("window animation orientation: %d\n", self->animation_orientation);
473                         break;
474
475                 case TILDA_WINDOW_ANIMATION_DELAY:
476                         self->animation_delay = g_value_get_int (value);
477                         debug_printf ("window animation delay: %d\n", self->animation_delay);
478                         break;
479
480                 case TILDA_WINDOW_KEEP_ABOVE:
481                         self->keep_above = g_value_get_boolean (value);
482                         gtk_window_set_keep_above (GTK_WINDOW(self->window), self->keep_above);
483                         debug_printf ("window keep above: %d\n", self->keep_above);
484                         break;
485
486                 case TILDA_WINDOW_SKIP_TASKBAR_HINT:
487                         self->skip_taskbar_hint = g_value_get_boolean (value);
488                         gtk_window_set_skip_taskbar_hint (GTK_WINDOW(self->window), self->skip_taskbar_hint);
489                         debug_printf ("window skip taskbar hint: %d\n", self->skip_taskbar_hint);
490                         break;
491
492                 case TILDA_WINDOW_STICK:
493                         self->stick = g_value_get_boolean (value);
494
495                         /* This is moderately ugly, but GTK+ does it this way... */
496                         self->stick ? gtk_window_stick (GTK_WINDOW(self->window))
497                                                 : gtk_window_unstick (GTK_WINDOW(self->window));
498                         debug_printf ("window stick: %d\n", self->stick);
499                         break;
500
501                 case TILDA_WINDOW_HIDDEN_AT_START:
502                         self->hidden_at_start = g_value_get_boolean (value);
503                         debug_printf ("window hidden at start: %d\n", self->hidden_at_start);
504                         break;
505
506                 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
507                         self->centered_horizontally = g_value_get_boolean (value);
508                         if (self->centered_horizontally)
509                                 tilda_window_center_horizontally (self);
510                         debug_printf ("window centered horizontally: %d\n", self->centered_horizontally);
511                         break;
512
513                 case TILDA_WINDOW_CENTERED_VERTICALLY:
514                         self->centered_vertically = g_value_get_boolean (value);
515                         if (self->centered_vertically)
516                                 tilda_window_center_vertically (self);
517                         debug_printf ("window centered vertically: %d\n", self->centered_vertically);
518                         break;
519
520                 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
521                         self->have_real_transparency = g_value_get_boolean (value);
522                         debug_printf ("window have real transp: %d\n", self->have_real_transparency);
523                         break;
524
525                 default:
526                         /* We don't have this property */
527                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
528                         break;
529         }
530 }
531
532 static void
533 tilda_window_get_property (GObject    *object,
534                                                    guint       property_id,
535                                                    GValue     *value,
536                                                    GParamSpec *pspec)
537 {
538         TildaWindow *self = (TildaWindow *) object;
539
540         switch (property_id) {
541
542                 case TILDA_WINDOW_NUMBER:
543                         g_value_set_int (value, self->number);
544                         break;
545
546                 case TILDA_WINDOW_CONTROLLER:
547                         g_value_set_pointer (value, self->controller);
548                         break;
549
550                 case TILDA_WINDOW_KEY:
551                         g_value_set_string (value, self->key);
552                         break;
553
554                 case TILDA_WINDOW_HEIGHT:
555                         g_value_set_int (value, self->height);
556                         break;
557
558                 case TILDA_WINDOW_WIDTH:
559                         g_value_set_int (value, self->width);
560                         break;
561
562                 case TILDA_WINDOW_X_POSITION:
563                         g_value_set_int (value, self->x_position);
564                         break;
565
566                 case TILDA_WINDOW_Y_POSITION:
567                         g_value_set_int (value, self->y_position);
568                         break;
569
570                 case TILDA_WINDOW_INITIAL_TERMINALS:
571                         g_value_set_int (value, self->initial_terminals);
572                         break;
573
574                 case TILDA_WINDOW_TAB_POSITION:
575                         g_value_set_enum (value, self->tab_position);
576                         break;
577
578                 case TILDA_WINDOW_ANIMATION_ORIENTATION:
579                         g_value_set_enum (value, self->animation_orientation);
580                         break;
581
582                 case TILDA_WINDOW_ANIMATION_DELAY:
583                         g_value_set_int (value, self->animation_delay);
584                         break;
585
586                 case TILDA_WINDOW_KEEP_ABOVE:
587                         g_value_set_boolean (value, self->keep_above);
588                         break;
589
590                 case TILDA_WINDOW_SKIP_TASKBAR_HINT:
591                         g_value_set_boolean (value, self->skip_taskbar_hint);
592                         break;
593
594                 case TILDA_WINDOW_STICK:
595                         g_value_set_boolean (value, self->stick);
596                         break;
597
598                 case TILDA_WINDOW_HIDDEN_AT_START:
599                         g_value_set_boolean (value, self->hidden_at_start);
600                         break;
601
602                 case TILDA_WINDOW_CENTERED_HORIZONTALLY:
603                         g_value_set_boolean (value, self->centered_horizontally);
604                         break;
605
606                 case TILDA_WINDOW_CENTERED_VERTICALLY:
607                         g_value_set_boolean (value, self->centered_vertically);
608                         break;
609
610                 case TILDA_WINDOW_HAVE_REAL_TRANSPARENCY:
611                         g_value_set_boolean (value, self->have_real_transparency);
612                         break;
613
614                 default:
615                         /* We don't have this property */
616                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
617                         break;
618         }
619 }
620
621 static GObject *
622 tilda_window_constructor (GType                  type,
623                                                   guint                  n_construct_properties,
624                                                   GObjectConstructParam *construct_properties)
625 {
626         debug_enter ();
627
628         GObject *obj;
629         TildaWindow *self;
630         gint i;
631
632         /* Invoke parent constructor */
633         TildaWindowClass *klass;
634         klass = TILDA_WINDOW_CLASS (g_type_class_peek (TILDA_TYPE_WINDOW));
635         obj = parent_class->constructor (type,
636                                                                          n_construct_properties,
637                                                                          construct_properties);
638
639         /* Do other stuff here. The object is ready to go now, and all
640          * ctor properties have been set.
641          */
642         self = TILDA_WINDOW(obj);
643
644         /* Try to set up real transparency */
645         tilda_window_setup_real_transparency (self);
646
647         gtk_container_add (GTK_CONTAINER(self->window), self->notebook);
648         g_object_set (G_OBJECT(self->notebook), "can-focus", FALSE, NULL);
649         gtk_widget_show (self->notebook);
650
651         /* Tilda is never decorated */
652         gtk_window_set_decorated (GTK_WINDOW(self->window), FALSE);
653
654         /* Set all of the properties out of the config file */
655         tilda_window_set_property_from_config (self, "key");
656
657         // FIXME: hack -- start the wizard in this case :)
658         if (!self->key)
659         {
660                 gchar *key = g_strdup_printf ("F%d", self->number+3);
661                 g_object_set (G_OBJECT(self), "key", key, NULL);
662                 g_free (key);
663
664                 g_critical ("HACK: start the wizard here\n");
665         }
666
667         tilda_window_set_property_from_config (self, "height");
668         tilda_window_set_property_from_config (self, "width");
669         tilda_window_set_property_from_config (self, "x-position");
670         tilda_window_set_property_from_config (self, "y-position");
671         tilda_window_set_property_from_config (self, "initial-terminals");
672         tilda_window_set_property_from_config (self, "animation-delay");
673
674         tilda_window_set_property_from_config (self, "tab-position");
675         tilda_window_set_property_from_config (self, "animation-orientation");
676
677         tilda_window_set_property_from_config (self, "keep-above");
678         tilda_window_set_property_from_config (self, "skip-taskbar-hint");
679         tilda_window_set_property_from_config (self, "stick");
680         tilda_window_set_property_from_config (self, "hidden-at-start");
681         tilda_window_set_property_from_config (self, "centered-horizontally");
682         tilda_window_set_property_from_config (self, "centered-vertically");
683
684         /* Add the initial terminal(s) */
685         for (i=0; i<self->initial_terminals; ++i)
686                 tilda_window_add_terminal (self);
687
688         /* Show us if we're ready. If not, just remain hidden. All sub-widgets must
689          * be gtk_widget_show()n by this point. */
690         if (!self->hidden_at_start)
691         {
692                 gtk_widget_show (self->window);
693                 self->state = WINDOW_DOWN;
694         }
695         else
696                 self->state = WINDOW_UP;
697
698         /* Register this object with DBus */
699         tilda_window_dbus_register_object (self);
700
701         return obj;
702 }
703
704 static void
705 tilda_window_dispose (GObject *obj)
706 {
707         debug_enter ();
708
709         TildaWindow *self = (TildaWindow *) obj;
710
711         /* We don't want to run dispose twice, so just return immediately */
712         if (self->dispose_has_run)
713                 return;
714
715         /*
716          * In dispose, you are supposed to free all types referenced from this
717          * object which might themselves hold a reference to self. Generally,
718          * the most simple solution is to unref all members on which you own a
719          * reference.
720          *
721          * NOTE: See the following for how to deal with GtkObject-derived things:
722          * http://library.gnome.org/devel/gtk/unstable/GtkObject.html
723          */
724         g_ptr_array_foreach (self->terms, g_object_unref, NULL);
725         gtk_widget_destroy (self->window);
726
727         /* Unbind if we were set */
728         if (self->key)
729                 tomboy_keybinder_unbind (self->key, tilda_window_keybinding_cb);
730
731         /* Chain up to the parent class */
732         G_OBJECT_CLASS (parent_class)->dispose (obj);
733 }
734
735 static void
736 tilda_window_finalize (GObject *obj)
737 {
738         debug_enter ();
739
740         TildaWindow *self = (TildaWindow *) obj;
741
742         /*
743          * Here, complete the object's destruction.
744          * You might not need to do much...
745          */
746         // TODO: g_free() any primitives here
747         g_ptr_array_free (self->terms, TRUE);
748
749
750         /* Chain up to the parent class */
751         G_OBJECT_CLASS (parent_class)->finalize (obj);
752 }
753
754 static void
755 tilda_window_class_init (gpointer g_class,
756                                                  gpointer g_class_data)
757 {
758         debug_enter ();
759
760         GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
761         TildaWindowClass *klass = TILDA_WINDOW_CLASS (g_class);
762         GParamSpec *pspec;
763
764         /* Hook our functions to this type */
765         gobject_class->set_property = tilda_window_set_property;
766         gobject_class->get_property = tilda_window_get_property;
767         gobject_class->dispose = tilda_window_dispose;
768         gobject_class->finalize = tilda_window_finalize;
769         gobject_class->constructor = tilda_window_constructor;
770
771         parent_class = g_type_class_peek_parent (klass);
772
773         /* Install all of the properties */
774         pspec = g_param_spec_int ("number",
775                                                           _("Window number"),
776                                                           NULL,
777                                                           0,            // min value
778                                                           INT_MAX,      // max value
779                                                           0,            // def value
780                                                           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
781
782         g_object_class_install_property (gobject_class,
783                                                                          TILDA_WINDOW_NUMBER,
784                                                                          pspec);
785
786         pspec = g_param_spec_pointer ("controller",
787                                                                   _("Pointer to window's controlling TildaController"),
788                                                                   NULL,
789                                                                   G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
790
791         g_object_class_install_property (gobject_class,
792                                                                          TILDA_WINDOW_CONTROLLER,
793                                                                          pspec);
794
795         pspec = g_param_spec_string ("key",
796                                                                  _("Window's drop-down keybinding"),
797                                                                  NULL,
798                                                                  NULL,
799                                                                  G_PARAM_READWRITE);
800
801         g_object_class_install_property (gobject_class,
802                                                                          TILDA_WINDOW_KEY,
803                                                                          pspec);
804
805         pspec = g_param_spec_int ("height",
806                                                           _("Window's height"),
807                                                           NULL,
808                                                           0,
809                                                           INT_MAX,
810                                                           0,
811                                                           G_PARAM_READWRITE);
812
813         g_object_class_install_property (gobject_class,
814                                                                          TILDA_WINDOW_HEIGHT,
815                                                                          pspec);
816
817         pspec = g_param_spec_int ("width",
818                                                           _("Window's width"),
819                                                           NULL,
820                                                           0,
821                                                           INT_MAX,
822                                                           0,
823                                                           G_PARAM_READWRITE);
824
825         g_object_class_install_property (gobject_class,
826                                                                          TILDA_WINDOW_WIDTH,
827                                                                          pspec);
828
829         pspec = g_param_spec_int ("x-position",
830                                                           _("Window's x position"),
831                                                           NULL,
832                                                           0,
833                                                           INT_MAX,
834                                                           0,
835                                                           G_PARAM_READWRITE);
836
837         g_object_class_install_property (gobject_class,
838                                                                          TILDA_WINDOW_X_POSITION,
839                                                                          pspec);
840
841         pspec = g_param_spec_int ("y-position",
842                                                           _("Window's y position"),
843                                                           NULL,
844                                                           0,
845                                                           INT_MAX,
846                                                           0,
847                                                           G_PARAM_READWRITE);
848
849         g_object_class_install_property (gobject_class,
850                                                                          TILDA_WINDOW_Y_POSITION,
851                                                                          pspec);
852
853         pspec = g_param_spec_int ("initial-terminals",
854                                                           _("Window's inital number of terminals"),
855                                                           NULL,
856                                                           1,
857                                                           INT_MAX,
858                                                           1,
859                                                           G_PARAM_READWRITE);
860
861         g_object_class_install_property (gobject_class,
862                                                                          TILDA_WINDOW_INITIAL_TERMINALS,
863                                                                          pspec);
864
865         pspec = g_param_spec_enum ("tab-position",
866                                                            _("Position of window's tab bar"),
867                                                            NULL,
868                                                            gtk_position_type_get_type(),
869                                                            GTK_POS_TOP,
870                                                            G_PARAM_READWRITE);
871
872         g_object_class_install_property (gobject_class,
873                                                                          TILDA_WINDOW_TAB_POSITION,
874                                                                          pspec);
875
876         pspec = g_param_spec_enum ("animation-orientation",
877                                                            _("Window's animation orientation"),
878                                                            NULL,
879                                                            gtk_position_type_get_type(),
880                                                            GTK_POS_TOP,
881                                                            G_PARAM_READWRITE);
882
883         g_object_class_install_property (gobject_class,
884                                                                          TILDA_WINDOW_ANIMATION_ORIENTATION,
885                                                                          pspec);
886
887         pspec = g_param_spec_int ("animation-delay",
888                                                           _("Amount of time in milliseconds between animation intervals"),
889                                                           NULL,
890                                                           0,
891                                                           INT_MAX,
892                                                           0,
893                                                           G_PARAM_READWRITE);
894
895         g_object_class_install_property (gobject_class,
896                                                                          TILDA_WINDOW_ANIMATION_DELAY,
897                                                                          pspec);
898
899         pspec = g_param_spec_boolean ("keep-above",
900                                                                   _("Keep this window above all others"),
901                                                                   NULL,
902                                                                   FALSE,
903                                                                   G_PARAM_READWRITE);
904
905         g_object_class_install_property (gobject_class,
906                                                                          TILDA_WINDOW_KEEP_ABOVE,
907                                                                          pspec);
908
909         pspec = g_param_spec_boolean ("skip-taskbar-hint",
910                                                                   _("Hide this window in the taskbar if TRUE"),
911                                                                   NULL,
912                                                                   FALSE,
913                                                                   G_PARAM_READWRITE);
914
915         g_object_class_install_property (gobject_class,
916                                                                          TILDA_WINDOW_SKIP_TASKBAR_HINT,
917                                                                          pspec);
918
919         pspec = g_param_spec_boolean ("stick",
920                                                                   _("Display this window on all workspaces"),
921                                                                   NULL,
922                                                                   FALSE,
923                                                                   G_PARAM_READWRITE);
924
925         g_object_class_install_property (gobject_class,
926                                                                          TILDA_WINDOW_STICK,
927                                                                          pspec);
928
929         pspec = g_param_spec_boolean ("hidden-at-start",
930                                                                   _("Hide the window when it is first created"),
931                                                                   NULL,
932                                                                   FALSE,
933                                                                   G_PARAM_READWRITE);
934
935         g_object_class_install_property (gobject_class,
936                                                                          TILDA_WINDOW_HIDDEN_AT_START,
937                                                                          pspec);
938
939         pspec = g_param_spec_boolean ("centered-horizontally",
940                                                                   _("Center the window horizontally"),
941                                                                   NULL,
942                                                                   FALSE,
943                                                                   G_PARAM_READWRITE);
944
945         g_object_class_install_property (gobject_class,
946                                                                          TILDA_WINDOW_CENTERED_HORIZONTALLY,
947                                                                          pspec);
948
949         pspec = g_param_spec_boolean ("centered-vertically",
950                                                                   _("Center the window vertically"),
951                                                                   NULL,
952                                                                   FALSE,
953                                                                   G_PARAM_READWRITE);
954
955         g_object_class_install_property (gobject_class,
956                                                                          TILDA_WINDOW_CENTERED_VERTICALLY,
957                                                                          pspec);
958
959         pspec = g_param_spec_boolean ("have-real-transparency",
960                                                                   NULL, NULL, FALSE, G_PARAM_READABLE);
961
962         g_object_class_install_property (gobject_class,
963                                                                          TILDA_WINDOW_HAVE_REAL_TRANSPARENCY,
964                                                                          pspec);
965
966         /* Hook the TildaWindow type into DBus */
967         dbus_g_object_type_install_info (tilda_window_get_type(), &dbus_glib_tilda_window_object_info);
968 }
969
970 GType
971 tilda_window_get_type (void)
972 {
973         static GType type = 0;
974
975         if (type == 0)
976         {
977                 static const GTypeInfo info = {
978                         sizeof (TildaWindowClass),
979                         NULL,   /* base_init */
980                         NULL,   /* base_finalize */
981                         tilda_window_class_init,        /* class_init */
982                         NULL,   /* class_finalize */
983                         NULL,   /* class_data */
984                         sizeof (TildaWindow),
985                         0,              /* n_preallocs */
986                         tilda_window_instance_init,     /* instance_init */
987                 };
988
989                 type = g_type_register_static (G_TYPE_OBJECT,
990                                                                            "TildaWindowType",
991                                                                            &info,
992                                                                            0);
993         }
994
995         return type;
996 }
997
998 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */