[Terminal] Add enum types
[tilda-gobject.git] / tilda-terminal.c
1 #include "tilda-terminal.h"
2
3 static GObjectClass *parent_class = NULL;
4
5 /* API */
6
7 /*
8  * All GObject stuff is below. You probably don't need to change this...
9  */
10
11 enum tilda_terminal_properties {
12         TILDA_TERMINAL_NUMBER = 1,
13
14         /* All non-constructor-only properties */
15         TILDA_TERMINAL_BACKGROUND_IMAGE,
16         TILDA_TERMINAL_SHELL,
17         TILDA_TERMINAL_FONT,
18         TILDA_TERMINAL_TITLE,
19         TILDA_TERMINAL_WORKING_DIRECTORY,
20
21         TILDA_TERMINAL_SCROLLBACK_LINES,
22         TILDA_TERMINAL_TRANSPARENCY_PERCENT,
23
24         TILDA_TERMINAL_BACKSPACE_BINDING,
25         TILDA_TERMINAL_DELETE_BINDING,
26         TILDA_TERMINAL_DYNAMIC_TITLE,
27         TILDA_TERMINAL_EXIT_ACTION,
28 };
29
30 static void
31 tilda_terminal_instance_init (GTypeInstance *instance,
32                                                           gpointer       g_class)
33 {
34         TildaTerminal *self = (TildaTerminal *) instance;
35
36         /* Initialize instance members and allocate any necessary memory here.
37          * NOTE: any constructor-time values will be set later. */
38         self->dispose_has_run = FALSE;
39         self->number = 0;
40 }
41
42 static void
43 tilda_terminal_set_property (GObject      *object,
44                                                          guint         property_id,
45                                                          const GValue *value,
46                                                          GParamSpec   *pspec)
47 {
48         TildaTerminal *self = (TildaTerminal *) object;
49
50         switch (property_id) {
51
52                 case TILDA_TERMINAL_NUMBER:
53                         self->number = g_value_get_int (value);
54                         g_print ("terminal number: %d\n", self->number);
55                         break;
56
57                 case TILDA_TERMINAL_BACKGROUND_IMAGE:
58                         g_free (self->background_image);
59                         self->background_image = g_value_dup_string (value);
60                         // TODO: Actually set it in self->vte_term
61                         g_print ("terminal back img: %s\n", self->background_image);
62                         break;
63
64                 case TILDA_TERMINAL_SHELL:
65                         g_free (self->shell);
66                         self->shell = g_value_dup_string (value);
67                         g_print ("terminal shell: %s\n", self->shell);
68                         break;
69
70                 case TILDA_TERMINAL_FONT:
71                         g_free (self->font);
72                         self->font = g_value_dup_string (value);
73                         g_print ("terminal font: %s\n", self->font);
74                         break;
75
76                 case TILDA_TERMINAL_TITLE:
77                         g_free (self->title);
78                         self->title = g_value_dup_string (value);
79                         g_print ("terminal title: %s\n", self->title);
80                         break;
81
82                 case TILDA_TERMINAL_WORKING_DIRECTORY:
83                         g_free (self->working_directory);
84                         self->working_directory = g_value_dup_string (value);
85                         g_print ("terminal wrk dir: %s\n", self->working_directory);
86                         break;
87
88                 case TILDA_TERMINAL_SCROLLBACK_LINES:
89                         self->scrollback_lines = g_value_get_int (value);
90                         g_print ("terminal scrollback lines: %d\n", self->scrollback_lines);
91                         break;
92
93                 case TILDA_TERMINAL_TRANSPARENCY_PERCENT:
94                         self->transparency_percent = g_value_get_int (value);
95                         g_print ("terminal transp percent: %d\n", self->transparency_percent);
96                         break;
97
98                 case TILDA_TERMINAL_BACKSPACE_BINDING:
99                         self->backspace_binding = g_value_get_int (value);
100                         //vte_terminal_set_backspace_binding (VTE_TERMINAL(self->vte_term), self->backspace_binding);
101                         g_print ("terminal backspace key: %d\n", self->backspace_binding);
102                         break;
103
104                 case TILDA_TERMINAL_DELETE_BINDING:
105                         self->delete_binding = g_value_get_int (value);
106                         //vte_terminal_set_delete_binding (VTE_TERMINAL(self->vte_term), self->delete_binding);
107                         g_print ("terminal delete key: %d\n", self->delete_binding);
108                         break;
109
110                 case TILDA_TERMINAL_DYNAMIC_TITLE:
111                         self->dynamic_title = g_value_get_int (value);
112                         g_print ("terminal dynamic title: %d\n", self->dynamic_title);
113                         break;
114
115                 case TILDA_TERMINAL_EXIT_ACTION:
116                         self->exit_action = g_value_get_int (value);
117                         g_print ("terminal exit action: %d\n", self->exit_action);
118                         break;
119
120                 default:
121                         /* We don't have this property... */
122                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
123                         break;
124         }
125 }
126
127 static void
128 tilda_terminal_get_property (GObject    *object,
129                                                          guint       property_id,
130                                                          GValue     *value,
131                                                          GParamSpec *pspec)
132 {
133         TildaTerminal *self = (TildaTerminal *) object;
134
135         switch (property_id) {
136
137                 case TILDA_TERMINAL_NUMBER:
138                         g_value_set_int (value, self->number);
139                         break;
140
141                 case TILDA_TERMINAL_BACKGROUND_IMAGE:
142                         g_value_set_string (value, self->background_image);
143                         break;
144
145                 case TILDA_TERMINAL_SHELL:
146                         g_value_set_string (value, self->shell);
147                         break;
148
149                 case TILDA_TERMINAL_FONT:
150                         g_value_set_string (value, self->font);
151                         break;
152
153                 case TILDA_TERMINAL_TITLE:
154                         g_value_set_string (value, self->title);
155                         break;
156
157                 case TILDA_TERMINAL_WORKING_DIRECTORY:
158                         g_value_set_string (value, self->working_directory);
159                         break;
160
161                 case TILDA_TERMINAL_SCROLLBACK_LINES:
162                         g_value_set_int (value, self->scrollback_lines);
163                         break;
164
165                 case TILDA_TERMINAL_TRANSPARENCY_PERCENT:
166                         g_value_set_int (value, self->transparency_percent);
167                         break;
168
169                 default:
170                         /* We don't have this property... */
171                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
172                         break;
173         }
174 }
175
176 static GObject *
177 tilda_terminal_constructor (GType                  type,
178                                                         guint                  n_construct_properties,
179                                                         GObjectConstructParam *construct_properties)
180 {
181         GObject *obj;
182
183         /* Invoke parent constructor */
184         TildaTerminalClass *klass;
185         klass = TILDA_TERMINAL_CLASS (g_type_class_peek (TILDA_TYPE_TERMINAL));
186         obj = parent_class->constructor (type,
187                                                                          n_construct_properties,
188                                                                          construct_properties);
189
190         /* Do other stuff here. The object is ready to go now, and all
191          * ctor properties have been set.
192          *
193          * TODO: This is the place to do DBus-init */
194
195
196         return obj;
197 }
198
199 static void
200 tilda_terminal_dispose (GObject *obj)
201 {
202         TildaTerminal *self = (TildaTerminal *) obj;
203
204         /* We don't want to run dispose twice, so just return immediately */
205         if (self->dispose_has_run)
206                 return;
207
208         self->dispose_has_run = TRUE;
209
210         /*
211          * In dispose, you are supposed to free all types referenced from this
212          * object which might themselves hold a reference to self. Generally,
213          * the most simple solution is to unref all members on which you own a
214          * reference.
215          */
216
217         /* Chain up to the parent class */
218         G_OBJECT_CLASS (parent_class)->dispose (obj);
219 }
220
221 static void
222 tilda_terminal_finalize (GObject *obj)
223 {
224         TildaTerminal *self = (TildaTerminal *) obj;
225
226         /*
227          * Here, complete object destruction.
228          * You might not need to do much...
229          */
230
231         // TODO: g_free() any primitives here
232         g_free (self->background_image);
233         g_free (self->shell);
234         g_free (self->font);
235         g_free (self->title);
236         g_free (self->working_directory);
237
238
239         /* Chain up to the parent class */
240         G_OBJECT_CLASS (parent_class)->finalize (obj);
241 }
242
243 static void
244 tilda_terminal_class_init (gpointer g_class,
245                                                    gpointer g_class_data)
246 {
247         GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
248         TildaTerminalClass *klass = TILDA_TERMINAL_CLASS (g_class);
249         GParamSpec *pspec;
250
251         /* Hook our functions to this type */
252         gobject_class->set_property = tilda_terminal_set_property;
253         gobject_class->get_property = tilda_terminal_get_property;
254         gobject_class->dispose = tilda_terminal_dispose;
255         gobject_class->finalize = tilda_terminal_finalize;
256         gobject_class->constructor = tilda_terminal_constructor;
257
258         parent_class = g_type_class_peek_parent (klass);
259
260         /* Install all of the properties */
261         pspec = g_param_spec_int ("number",
262                                                           "Terminal number",
263                                                           "Set terminal's number",
264                                                           0,            // min value
265                                                           INT_MAX,      // max value
266                                                           0,            // def value
267                                                           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
268
269         g_object_class_install_property (gobject_class,
270                                                                          TILDA_TERMINAL_NUMBER,
271                                                                          pspec);
272
273         pspec = g_param_spec_string ("background-image",
274                                                                  "Terminal's background image",
275                                                                  "Get/Set terminal's background image",
276                                                                  NULL,
277                                                                  G_PARAM_READWRITE);
278
279         g_object_class_install_property (gobject_class,
280                                                                          TILDA_TERMINAL_BACKGROUND_IMAGE,
281                                                                          pspec);
282
283         pspec = g_param_spec_string ("shell",
284                                                                  "Terminal's shell",
285                                                                  "Get/Set terminal's shell",
286                                                                  NULL,
287                                                                  G_PARAM_READWRITE);
288
289         g_object_class_install_property (gobject_class,
290                                                                          TILDA_TERMINAL_SHELL,
291                                                                          pspec);
292
293         pspec = g_param_spec_string ("font",
294                                                                  "Terminal's font",
295                                                                  "Get/Set terminal's font",
296                                                                  NULL,
297                                                                  G_PARAM_READWRITE);
298
299         g_object_class_install_property (gobject_class,
300                                                                          TILDA_TERMINAL_FONT,
301                                                                          pspec);
302
303         pspec = g_param_spec_string ("title",
304                                                                  "Terminal's title",
305                                                                  "Get/Set terminal's title",
306                                                                  NULL,
307                                                                  G_PARAM_READWRITE);
308
309         g_object_class_install_property (gobject_class,
310                                                                          TILDA_TERMINAL_TITLE,
311                                                                          pspec);
312
313         pspec = g_param_spec_string ("working-directory",
314                                                                  "Terminal's initial working directory",
315                                                                  "Get/Set terminal's initial working directory",
316                                                                  NULL,
317                                                                  G_PARAM_READWRITE);
318
319         g_object_class_install_property (gobject_class,
320                                                                          TILDA_TERMINAL_WORKING_DIRECTORY,
321                                                                          pspec);
322
323         pspec = g_param_spec_int ("scrollback-lines",
324                                                           "Terminal's scrollback amount (lines)",
325                                                           "Get/Set terminal's scrollback amount",
326                                                           0,
327                                                           INT_MAX, // TODO: artificially limit this?
328                                                           1000,
329                                                           G_PARAM_READWRITE);
330
331         g_object_class_install_property (gobject_class,
332                                                                          TILDA_TERMINAL_SCROLLBACK_LINES,
333                                                                          pspec);
334
335         pspec = g_param_spec_int ("transparency-percent",
336                                                           "Terminal's transparency (percent)",
337                                                           "Get/Set terminal's transparency",
338                                                           0,
339                                                           100,
340                                                           0,
341                                                           G_PARAM_READWRITE);
342
343         g_object_class_install_property (gobject_class,
344                                                                          TILDA_TERMINAL_TRANSPARENCY_PERCENT,
345                                                                          pspec);
346 }
347
348 GType
349 tilda_terminal_get_type (void)
350 {
351         static GType type = 0;
352
353         if (type == 0)
354         {
355                 static const GTypeInfo info = {
356                         sizeof (TildaTerminalClass),
357                         NULL,   /* base_init */
358                         NULL,   /* base_finalize */
359                         tilda_terminal_class_init,      /* class_init */
360                         NULL,   /* class_finalize */
361                         NULL,   /* class_data */
362                         sizeof (TildaTerminal),
363                         0,              /* n_preallocs */
364                         tilda_terminal_instance_init,   /* instance_init */
365                 };
366
367                 type = g_type_register_static (G_TYPE_OBJECT,
368                                                                            "TildaTerminalType",
369                                                                            &info,
370                                                                            0);
371         }
372
373         return type;
374 }
375
376 int main (int argc, char *argv[])
377 {
378         GObject *tt;
379         gint test_number = INT_MIN;
380         gchar *test_string = NULL;
381
382         /* Initialize the GObject type system */
383         g_type_init ();
384
385         tt = g_object_new (TILDA_TYPE_TERMINAL, "number", 10, NULL);
386         g_object_get (G_OBJECT (tt), "number", &test_number, NULL);
387         g_assert (test_number == 10);
388
389         g_object_unref (G_OBJECT (tt));
390
391         tt = g_object_new (TILDA_TYPE_TERMINAL, "number", 22, NULL);
392         g_object_get (G_OBJECT (tt), "number", &test_number, NULL);
393         g_assert (test_number == 22);
394
395         g_object_set (G_OBJECT (tt), "font", "hello I'm a font");
396         g_object_set (G_OBJECT (tt), "font", "Bitstream Vera Sans Mono 13");
397
398         g_object_get (G_OBJECT (tt), "font", &test_string, NULL);
399         g_print ("Read Font: %s\n", test_string);
400         // NOTE: you MUST free the string!!!!
401         g_free (test_string);
402
403         g_object_set (G_OBJECT (tt), "transparency-percent", 50);
404
405         g_object_unref (G_OBJECT (tt));
406
407         return 0;
408 }
409
410 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */