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