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