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