Update of Greek translation
[geany-mirror.git] / plugins / classbuilder.c
blob980cfbdab781ae25c6013baf0e024ad06ea1d144
1 /*
2 * classbuilder.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007 Alexander Rodin <rodin(dot)alexander(at)gmail(dot)com>
5 * Copyright 2007-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
6 * Copyright 2007-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /* Class Builder - creates source files containing a new class interface and definition. */
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include "geanyplugin.h"
31 GeanyData *geany_data;
34 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
36 PLUGIN_SET_INFO(_("Class Builder"), _("Creates source files for new class types."), VERSION,
37 "Alexander Rodin, Ondrej Donek, the Geany developer team")
40 static GtkWidget *main_menu_item = NULL;
43 enum
45 GEANY_CLASS_TYPE_CPP,
46 GEANY_CLASS_TYPE_GTK,
47 GEANY_CLASS_TYPE_PHP
50 typedef struct _ClassInfo ClassInfo;
52 struct _ClassInfo
54 gint type;
55 gchar *namespace;
56 gchar *namespace_up;
57 gchar *namespace_low;
58 gchar *class_name;
59 gchar *class_name_up;
60 gchar *class_name_low;
61 gchar *base_name;
62 gchar *base_gtype;
63 gchar *header;
64 gchar *header_guard;
65 gchar *base_include;
66 gchar *base_decl;
67 gchar *constructor_decl;
68 gchar *destructor_decl;
69 gchar *source;
70 gchar *constructor_impl;
71 gchar *destructor_impl;
72 gchar *gtk_destructor_registration;
73 /* These are needed only for PHP classes */
74 gchar *namespace_decl;
75 gchar *implements_decl;
76 gchar *abstract_decl;
77 gchar *singleton_impl;
80 typedef struct _CreateClassDialog
82 gint class_type;
83 GtkWidget *dialog;
84 GtkWidget *class_name_entry;
85 GtkWidget *header_entry;
86 GtkWidget *source_entry;
87 GtkWidget *base_name_entry;
88 GtkWidget *base_header_entry;
89 GtkWidget *base_header_global_box;
90 GtkWidget *base_gtype_entry;
91 GtkWidget *create_constructor_box;
92 GtkWidget *create_destructor_box;
93 GtkWidget *gtk_constructor_type_entry;
94 /* These are needed only for PHP classes */
95 GtkWidget *class_namespace_entry;
96 GtkWidget *class_implements_entry;
97 GtkWidget *create_isabstract_box;
98 GtkWidget *create_issingleton_box;
99 } CreateClassDialog;
102 /* TODO make these templates configurable */
103 static const gchar templates_cpp_class_header[] = "{fileheader}\n\n\
104 #ifndef {header_guard}\n\
105 #define {header_guard}\n\
106 {base_include}\n\
107 class {class_name}{base_decl}\n\
108 {\n\
109 public:\n\
110 {constructor_decl}\
111 {destructor_decl}\
113 private:\n\
114 /* add your private declarations */\n\
115 };\n\
117 #endif /* {header_guard} */ \n\
120 static const gchar templates_cpp_class_source[] = "{fileheader}\n\n\
121 #include \"{header}\"\n\
123 {constructor_impl}\n\
124 {destructor_impl}\n\
127 static const gchar templates_gtk_class_header[] = "{fileheader}\n\n\
128 #ifndef {header_guard}_\n\
129 #define {header_guard}_ 1\n\
130 {base_include}\n\
131 G_BEGIN_DECLS\n\
132 \n\n\
133 #define {namespace_up}TYPE_{class_name_up} ({namespace_low}{class_name_low}_get_type ())\n\
134 #define {namespace_up}{class_name_up}(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), {namespace_up}TYPE_{class_name_up}, {namespace}{class_name}))\n\
135 #define {namespace_up}{class_name_up}_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), {namespace_up}TYPE_{class_name_up}, {namespace}{class_name}Class))\n\
136 #define {namespace_up}IS_{class_name_up}(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), {namespace_up}TYPE_{class_name_up}))\n\
137 #define {namespace_up}IS_{class_name_up}_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), {namespace_up}TYPE_{class_name_up}))\n\
138 #define {namespace_up}{class_name_up}_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), {namespace_up}TYPE_{class_name_up}, {namespace}{class_name}Class))\n\
140 typedef struct {namespace}{class_name}_ {namespace}{class_name};\n\
141 typedef struct {namespace}{class_name}Class_ {namespace}{class_name}Class;\n\
142 typedef struct {namespace}{class_name}Private_ {namespace}{class_name}Private;\n\
144 struct {namespace}{class_name}_\n\
145 {\n\
146 {base_name} parent;\n\
147 /* add your public declarations here */\n\
148 {namespace}{class_name}Private *priv;\n\
149 };\n\
151 struct {namespace}{class_name}Class_\n\
152 {\n\
153 {base_name}Class parent_class;\n\
154 };\n\
155 \n\n\
156 GType {namespace_low}{class_name_low}_get_type (void);\n\n\
157 {constructor_decl}\
158 \n\n\
159 G_END_DECLS\n\
161 #endif /* {header_guard}_ */\n\
164 static const gchar templates_gtk_class_source[] = "{fileheader}\n\
165 #include \"{header}\"\n\
167 struct {namespace}{class_name}Private_\n\
168 {\n\
169 /* add your private declarations here */\n\
170 gpointer delete_me;\n\
171 };\n\
173 {destructor_decl}\
175 G_DEFINE_TYPE ({namespace}{class_name}, {namespace_low}{class_name_low}, {base_gtype})\n\
176 \n\n\
177 static void\n\
178 {namespace_low}{class_name_low}_class_init ({namespace}{class_name}Class *klass)\n\
179 {\n\
180 {gtk_destructor_registration}\n\
181 g_type_class_add_private ((gpointer)klass, sizeof ({namespace}{class_name}Private));\n\
182 }\n\
184 {destructor_impl}\n\
186 static void\n\
187 {namespace_low}{class_name_low}_init ({namespace}{class_name} *self)\n\
188 {\n\
189 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, {namespace_up}TYPE_{class_name_up}, {namespace}{class_name}Private);\n\
190 }\n\
192 {constructor_impl}\n\
195 static const gchar templates_php_class_source[] = "<?php\n\
196 {fileheader}\n\
197 {namespace_decl}\n\
198 {base_include}\n\
199 {abstract_decl}class {class_name}{base_decl}{implements_decl}\n{\n\
200 {singleton_impl}\
201 {constructor_impl}\
202 {destructor_impl}\n\
203 // ...\n\n\
204 }\n\
208 static void cc_dlg_on_set_sensitive_toggled(GtkWidget *toggle_button, GtkWidget *target_widget);
209 static void cc_dlg_on_class_name_entry_changed(GtkWidget *entry, CreateClassDialog *cc_dlg);
210 static void cc_dlg_on_class_namespace_entry_changed(GtkWidget *entry, CreateClassDialog *cc_dlg);
211 static void cc_dlg_on_base_name_entry_changed(GtkWidget *entry, CreateClassDialog *cc_dlg);
212 static gboolean create_class(CreateClassDialog *cc_dlg);
215 /* The list must be ended with NULL as an extra check that arg_count is correct. */
216 static void
217 free_pointers(gsize arg_count, ...)
219 va_list a;
220 gsize i;
221 gpointer ptr;
223 va_start(a, arg_count);
224 for (i = 0; i < arg_count; i++)
226 ptr = va_arg(a, gpointer);
227 g_free(ptr);
229 ptr = va_arg(a, gpointer);
230 if (ptr)
231 g_warning("Wrong arg_count!");
232 va_end(a);
236 static gchar*
237 get_template_class_header(ClassInfo *class_info)
239 gchar *fileheader = NULL;
240 GString *template = NULL;
242 switch (class_info->type)
244 case GEANY_CLASS_TYPE_CPP:
245 fileheader = templates_get_template_fileheader(GEANY_FILETYPES_CPP, class_info->header);
246 template = g_string_new(templates_cpp_class_header);
247 utils_string_replace_all(template, "{fileheader}", fileheader);
248 utils_string_replace_all(template, "{header_guard}", class_info->header_guard);
249 utils_string_replace_all(template, "{base_include}", class_info->base_include);
250 utils_string_replace_all(template, "{class_name}", class_info->class_name);
251 utils_string_replace_all(template, "{base_decl}", class_info->base_decl);
252 utils_string_replace_all(template, "{constructor_decl}",
253 class_info->constructor_decl);
254 utils_string_replace_all(template, "{destructor_decl}",
255 class_info->destructor_decl);
256 break;
258 case GEANY_CLASS_TYPE_GTK:
259 fileheader = templates_get_template_fileheader(GEANY_FILETYPES_C, class_info->header);
260 template = g_string_new(templates_gtk_class_header);
261 utils_string_replace_all(template, "{fileheader}", fileheader);
262 utils_string_replace_all(template, "{header_guard}", class_info->header_guard);
263 utils_string_replace_all(template, "{base_include}", class_info->base_include);
264 utils_string_replace_all(template, "{namespace}", class_info->namespace);
265 utils_string_replace_all(template, "{namespace_up}", class_info->namespace_up);
266 utils_string_replace_all(template, "{namespace_low}", class_info->namespace_low);
267 utils_string_replace_all(template, "{class_name}", class_info->class_name);
268 utils_string_replace_all(template, "{class_name_up}", class_info->class_name_up);
269 utils_string_replace_all(template, "{class_name_low}", class_info->class_name_low);
270 utils_string_replace_all(template, "{base_name}", class_info->base_name);
271 utils_string_replace_all(template, "{constructor_decl}",
272 class_info->constructor_decl);
273 break;
276 g_free(fileheader);
278 if (template)
279 return g_string_free(template, FALSE);
280 else
281 return NULL;
285 static gchar*
286 get_template_class_source(ClassInfo *class_info)
288 gchar *fileheader = NULL;
289 GString *template = NULL;
291 switch (class_info->type)
293 case GEANY_CLASS_TYPE_CPP:
294 fileheader = templates_get_template_fileheader(GEANY_FILETYPES_CPP, class_info->source);
295 template = g_string_new(templates_cpp_class_source);
296 utils_string_replace_all(template, "{fileheader}", fileheader);
297 utils_string_replace_all(template, "{header}", class_info->header);
298 utils_string_replace_all(template, "{class_name}", class_info->class_name);
299 utils_string_replace_all(template, "{base_include}", class_info->base_include);
300 utils_string_replace_all(template, "{base_name}", class_info->base_name);
301 utils_string_replace_all(template, "{constructor_impl}",
302 class_info->constructor_impl);
303 utils_string_replace_all(template, "{destructor_impl}",
304 class_info->destructor_impl);
305 break;
307 case GEANY_CLASS_TYPE_GTK:
308 fileheader = templates_get_template_fileheader(GEANY_FILETYPES_C, class_info->source);
309 template = g_string_new(templates_gtk_class_source);
310 utils_string_replace_all(template, "{fileheader}", fileheader);
311 utils_string_replace_all(template, "{header}", class_info->header);
312 utils_string_replace_all(template, "{namespace}", class_info->namespace);
313 utils_string_replace_all(template, "{namespace_up}", class_info->namespace_up);
314 utils_string_replace_all(template, "{namespace_low}", class_info->namespace_low);
315 utils_string_replace_all(template, "{class_name}", class_info->class_name);
316 utils_string_replace_all(template, "{class_name_up}", class_info->class_name_up);
317 utils_string_replace_all(template, "{class_name_low}", class_info->class_name_low);
318 utils_string_replace_all(template, "{base_name}", class_info->base_name);
319 utils_string_replace_all(template, "{base_gtype}", class_info->base_gtype);
320 utils_string_replace_all(template, "{destructor_decl}", class_info->destructor_decl);
321 utils_string_replace_all(template, "{constructor_impl}",
322 class_info->constructor_impl);
323 utils_string_replace_all(template, "{destructor_impl}",
324 class_info->destructor_impl);
325 utils_string_replace_all(template, "{gtk_destructor_registration}",
326 class_info->gtk_destructor_registration);
327 break;
329 case GEANY_CLASS_TYPE_PHP:
330 fileheader = templates_get_template_fileheader(GEANY_FILETYPES_PHP, class_info->source);
331 template = g_string_new(templates_php_class_source);
332 utils_string_replace_all(template, "{fileheader}", fileheader);
333 utils_string_replace_all(template, "{namespace_decl}", class_info->namespace_decl);
334 utils_string_replace_all(template, "{base_include}", class_info->base_include);
335 utils_string_replace_all(template, "{abstract_decl}", class_info->abstract_decl);
336 utils_string_replace_all(template, "{class_name}", class_info->class_name);
337 utils_string_replace_all(template, "{base_decl}", class_info->base_decl);
338 utils_string_replace_all(template, "{implements_decl}", class_info->implements_decl);
339 utils_string_replace_all(template, "{constructor_impl}", class_info->constructor_impl);
340 utils_string_replace_all(template, "{destructor_impl}", class_info->destructor_impl);
341 utils_string_replace_all(template, "{singleton_impl}", class_info->singleton_impl);
342 break;
345 g_free(fileheader);
347 if (template)
348 return g_string_free(template, FALSE);
349 else
350 return NULL;
353 /* Creates a new option label, indented on the left */
354 static GtkWidget *cc_option_label_new(const gchar *text)
356 GtkWidget *align;
357 GtkWidget *label;
359 align = gtk_alignment_new(0.0, 0.5, 1.0, 1.0);
360 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 0, 0, 12, 0);
362 label = gtk_label_new(text);
363 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
364 gtk_container_add(GTK_CONTAINER(align), label);
366 return align;
369 /* Attaches a new section label at the specified table row, optionally
370 * padded at the top, and returns the new label. */
371 static GtkWidget *cc_table_attach_section_label(GtkWidget *table, const gchar *text,
372 guint row, gboolean top_padding)
374 gchar *markup;
375 GtkWidget *label, *align;
377 label = gtk_label_new(NULL);
378 markup = g_markup_printf_escaped("<b>%s</b>", text);
379 gtk_label_set_markup(GTK_LABEL(label), markup);
380 g_free(markup);
381 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
383 align = gtk_alignment_new(0.0, 0.5, 1.0, 1.0);
384 if (top_padding)
385 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 6, 0, 0, 0);
386 gtk_container_add(GTK_CONTAINER(align), label);
388 gtk_table_attach(GTK_TABLE(table), align,
389 0, 2, row, row+1,
390 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
392 return label;
395 /* Attach a new option label at the specified table row and returns
396 * the label */
397 static GtkWidget *cc_table_attach_option_label(GtkWidget *table, const gchar *text, guint row)
399 GtkWidget *opt_label = cc_option_label_new(text);
400 gtk_table_attach(GTK_TABLE(table), opt_label,
401 0, 1, row, row+1,
402 GTK_FILL|GTK_SHRINK, GTK_FILL|GTK_SHRINK, 0, 0);
403 return opt_label;
406 /* Attach an option label and entry to the table at the specified row.
407 * The label associated with the widget is set as data on the entry
408 * with the "label" key, if access to it is needed later. The entry
409 * widget is returned. */
410 static GtkWidget *cc_table_attach_option_entry(GtkWidget *table, const gchar *text, guint row)
412 GtkWidget *label;
413 GtkWidget *entry;
414 label = cc_table_attach_option_label(table, text, row);
415 entry = gtk_entry_new();
416 g_object_set_data(G_OBJECT(entry), "label", label);
417 gtk_table_attach(GTK_TABLE(table), entry,
418 1, 2, row, row+1,
419 GTK_EXPAND|GTK_FILL, GTK_FILL, 0, 0);
420 return entry;
423 static void show_dialog_create_class(gint type)
425 CreateClassDialog *cc_dlg;
426 GtkWidget *main_box, *table, *label, *hdr_hbox;
427 GtkWidget *opt_table, *align;
428 guint row;
430 cc_dlg = g_new0(CreateClassDialog, 1);
431 cc_dlg->class_type = type;
433 cc_dlg->dialog = gtk_dialog_new_with_buttons(_("Create Class"),
434 GTK_WINDOW(geany->main_widgets->window),
435 GTK_DIALOG_MODAL,
436 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
437 GTK_STOCK_OK, GTK_RESPONSE_OK,
438 NULL);
440 switch (type)
442 case GEANY_CLASS_TYPE_CPP:
443 gtk_window_set_title(GTK_WINDOW(cc_dlg->dialog), _("Create C++ Class"));
444 break;
445 case GEANY_CLASS_TYPE_GTK:
446 gtk_window_set_title(GTK_WINDOW(cc_dlg->dialog), _("Create GTK+ Class"));
447 break;
448 case GEANY_CLASS_TYPE_PHP:
449 gtk_window_set_title(GTK_WINDOW(cc_dlg->dialog), _("Create PHP Class"));
450 break;
453 g_signal_connect_swapped(cc_dlg->dialog, "destroy", G_CALLBACK(g_free), (gpointer)cc_dlg);
455 table = gtk_table_new(13, 2, FALSE);
456 gtk_table_set_col_spacings(GTK_TABLE(table), 6);
457 gtk_table_set_row_spacings(GTK_TABLE(table), 6);
459 main_box = ui_dialog_vbox_new(GTK_DIALOG(cc_dlg->dialog));
460 gtk_box_pack_start(GTK_BOX(main_box), table, TRUE, TRUE, 0);
462 row = 0;
464 if (type == GEANY_CLASS_TYPE_PHP || type == GEANY_CLASS_TYPE_GTK)
466 cc_table_attach_section_label(table, _("Namespace"), row++, FALSE);
467 cc_dlg->class_namespace_entry = cc_table_attach_option_entry(table, _("Name:"), row++);
468 g_signal_connect(cc_dlg->class_namespace_entry, "changed",
469 G_CALLBACK(cc_dlg_on_class_namespace_entry_changed), cc_dlg);
472 if (type == GEANY_CLASS_TYPE_PHP || type == GEANY_CLASS_TYPE_GTK)
473 cc_table_attach_section_label(table, _("Class"), row++, TRUE);
474 else
475 cc_table_attach_section_label(table, _("Class"), row++, FALSE);
477 cc_dlg->class_name_entry = cc_table_attach_option_entry(table, _("Name:"), row++);
478 g_signal_connect(cc_dlg->class_name_entry, "changed",
479 G_CALLBACK(cc_dlg_on_class_name_entry_changed), cc_dlg);
481 if (type != GEANY_CLASS_TYPE_PHP)
482 cc_dlg->header_entry = cc_table_attach_option_entry(table, _("Header file:"), row++);
484 cc_dlg->source_entry = cc_table_attach_option_entry(table, _("Source file:"), row++);
486 cc_table_attach_section_label(table, _("Inheritance"), row++, TRUE);
488 cc_dlg->base_name_entry = cc_table_attach_option_entry(table, _("Base class:"), row++);
490 if (type == GEANY_CLASS_TYPE_GTK)
491 gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_name_entry), "GObject");
492 g_signal_connect(cc_dlg->base_name_entry, "changed",
493 G_CALLBACK(cc_dlg_on_base_name_entry_changed), (gpointer)cc_dlg);
495 if (type == GEANY_CLASS_TYPE_PHP)
496 cc_dlg->base_header_entry = cc_table_attach_option_entry(table, _("Base source:"), row++);
497 else
499 hdr_hbox = gtk_hbox_new(FALSE, 6);
501 label = cc_table_attach_option_label(table, _("Base header:"), row);
503 cc_dlg->base_header_entry = gtk_entry_new();
504 g_object_set_data(G_OBJECT(cc_dlg->base_header_entry), "label", label);
505 gtk_box_pack_start(GTK_BOX(hdr_hbox),
506 cc_dlg->base_header_entry,
507 TRUE, TRUE, 0);
509 cc_dlg->base_header_global_box = gtk_check_button_new_with_label(_("Global"));
510 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cc_dlg->base_header_global_box), TRUE);
511 gtk_box_pack_start(GTK_BOX(hdr_hbox),
512 cc_dlg->base_header_global_box,
513 FALSE, TRUE, 0);
515 gtk_table_attach(GTK_TABLE(table), hdr_hbox,
516 1, 2, row, row+1,
517 GTK_FILL | GTK_EXPAND,
518 GTK_FILL | GTK_EXPAND,
519 0, 0);
520 row++;
523 if (type == GEANY_CLASS_TYPE_GTK)
524 gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_header_entry), "glib-object.h");
526 if (type == GEANY_CLASS_TYPE_GTK)
528 cc_dlg->base_gtype_entry = cc_table_attach_option_entry(table, _("Base GType:"), row++);
529 gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_gtype_entry), "G_TYPE_OBJECT");
532 if (type == GEANY_CLASS_TYPE_PHP)
533 cc_dlg->class_implements_entry = cc_table_attach_option_entry(table, _("Implements:"), row++);
535 cc_table_attach_section_label(table, _("Options"), row++, TRUE);
537 align = gtk_alignment_new(0.0, 0.5, 1.0, 1.0);
538 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 0, 0, 12, 0);
540 opt_table = gtk_table_new(1, 2, FALSE);
541 gtk_table_set_row_spacings(GTK_TABLE(opt_table), 6);
542 gtk_table_set_col_spacings(GTK_TABLE(opt_table), 6);
543 gtk_container_add(GTK_CONTAINER(align), opt_table);
545 gtk_table_attach(GTK_TABLE(table), align,
546 0, 2, row, row+1,
547 GTK_FILL|GTK_EXPAND,
548 GTK_FILL|GTK_EXPAND,
549 0, 0);
550 row++;
552 cc_dlg->create_constructor_box = gtk_check_button_new_with_label(_("Create constructor"));
553 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cc_dlg->create_constructor_box), TRUE);
554 gtk_table_attach(GTK_TABLE(opt_table), cc_dlg->create_constructor_box,
555 0, 1, 0, 1, GTK_FILL|GTK_SHRINK, GTK_FILL|GTK_SHRINK, 0, 0);
557 cc_dlg->create_destructor_box = gtk_check_button_new_with_label(_("Create destructor"));
558 gtk_table_attach(GTK_TABLE(opt_table), cc_dlg->create_destructor_box,
559 1, 2, 0, 1, GTK_FILL|GTK_SHRINK, GTK_FILL|GTK_SHRINK, 0, 0);
561 if (type == GEANY_CLASS_TYPE_PHP)
563 gtk_table_resize(GTK_TABLE(opt_table), 2, 2);
564 cc_dlg->create_isabstract_box = gtk_check_button_new_with_label(_("Is abstract"));
565 gtk_table_attach(GTK_TABLE(opt_table), cc_dlg->create_isabstract_box,
566 0, 1, 1, 2, GTK_FILL|GTK_SHRINK, GTK_FILL|GTK_SHRINK, 0, 0);
567 cc_dlg->create_issingleton_box = gtk_check_button_new_with_label(_("Is singleton"));
568 gtk_table_attach(GTK_TABLE(opt_table), cc_dlg->create_issingleton_box,
569 1, 2, 1, 2, GTK_FILL|GTK_SHRINK, GTK_FILL|GTK_SHRINK, 0, 0);
572 gtk_widget_show_all(align);
574 if (type == GEANY_CLASS_TYPE_GTK)
576 cc_dlg->gtk_constructor_type_entry = cc_table_attach_option_entry(table,
577 _("Constructor type:"), row++);
578 gtk_entry_set_text(GTK_ENTRY(cc_dlg->gtk_constructor_type_entry), "GObject");
579 g_signal_connect(cc_dlg->create_constructor_box, "toggled",
580 G_CALLBACK(cc_dlg_on_set_sensitive_toggled),
581 cc_dlg->gtk_constructor_type_entry);
583 else if (type == GEANY_CLASS_TYPE_PHP)
584 gtk_table_resize(GTK_TABLE(table), row, 2);
585 else if (type == GEANY_CLASS_TYPE_CPP)
586 gtk_table_resize(GTK_TABLE(table), row, 2);
588 gtk_widget_show_all(cc_dlg->dialog);
589 while (gtk_dialog_run(GTK_DIALOG(cc_dlg->dialog)) == GTK_RESPONSE_OK)
591 if (create_class(cc_dlg))
592 break;
593 else
594 gdk_beep();
596 gtk_widget_destroy(cc_dlg->dialog);
600 static void cc_dlg_on_set_sensitive_toggled(GtkWidget *toggle_button, GtkWidget *target_widget)
602 GtkWidget *label;
604 g_return_if_fail(toggle_button != NULL);
605 g_return_if_fail(GTK_IS_TOGGLE_BUTTON(toggle_button));
606 g_return_if_fail(target_widget != NULL);
607 g_return_if_fail(GTK_IS_WIDGET(target_widget));
609 label = g_object_get_data(G_OBJECT(target_widget), "label");
611 gtk_widget_set_sensitive(target_widget,
612 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle_button)));
613 gtk_widget_set_sensitive(label,
614 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle_button)));
618 static void cc_dlg_update_file_names(CreateClassDialog *cc_dlg)
620 gchar *class_name;
621 gchar *class_name_down;
622 gchar *class_header = NULL;
623 gchar *class_source = NULL;
625 g_return_if_fail(cc_dlg != NULL);
627 class_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->class_name_entry)));
628 class_name_down = g_ascii_strdown(class_name, -1);
629 switch (cc_dlg->class_type)
631 case GEANY_CLASS_TYPE_CPP:
633 class_header = g_strconcat(class_name_down, ".h", NULL);
634 class_source = g_strconcat(class_name_down, ".cpp", NULL);
635 break;
637 case GEANY_CLASS_TYPE_GTK:
639 const gchar *namespace;
640 gchar *namespace_down;
642 namespace = gtk_entry_get_text(GTK_ENTRY(cc_dlg->class_namespace_entry));
643 namespace_down = g_ascii_strdown(namespace, -1);
644 class_header = g_strconcat(namespace_down, class_name_down, ".h", NULL);
645 class_source = g_strconcat(namespace_down, class_name_down, ".c", NULL);
646 g_free(namespace_down);
647 break;
649 case GEANY_CLASS_TYPE_PHP:
651 class_header = NULL;
652 class_source = g_strconcat(class_name, ".php", NULL);
653 break;
657 if (cc_dlg->header_entry != NULL && class_header != NULL)
658 gtk_entry_set_text(GTK_ENTRY(cc_dlg->header_entry), class_header);
659 if (cc_dlg->source_entry != NULL && class_source != NULL)
660 gtk_entry_set_text(GTK_ENTRY(cc_dlg->source_entry), class_source);
662 g_free(class_name);
663 g_free(class_name_down);
664 g_free(class_header);
665 g_free(class_source);
669 static void cc_dlg_on_class_name_entry_changed(GtkWidget *entry, CreateClassDialog *cc_dlg)
671 cc_dlg_update_file_names(cc_dlg);
675 static void cc_dlg_on_class_namespace_entry_changed(GtkWidget *entry, CreateClassDialog *cc_dlg)
678 if (cc_dlg->class_type == GEANY_CLASS_TYPE_GTK)
679 cc_dlg_update_file_names(cc_dlg);
683 static gchar* str_case_split(const gchar *str, gchar splitter)
685 GString *result;
687 g_return_val_if_fail(str != NULL, NULL);
688 if (*str == '\0')
689 return g_strdup("");
691 result = g_string_new(NULL);
692 g_string_append_c(result, *str);
693 while (*(++str) != '\0')
695 if (g_ascii_isupper(*str) && g_ascii_islower(result->str[result->len - 1]))
696 g_string_append_c(result, splitter);
697 g_string_append_c(result, *str);
699 return g_string_free(result, FALSE);
703 static void cc_dlg_on_base_name_entry_changed(GtkWidget *entry, CreateClassDialog *cc_dlg)
705 gchar *base_name_splitted;
706 gchar *base_header;
707 gchar *tmp;
709 g_return_if_fail(entry != NULL);
710 g_return_if_fail(GTK_IS_ENTRY(entry));
711 g_return_if_fail(cc_dlg != NULL);
713 base_name_splitted = str_case_split(gtk_entry_get_text(GTK_ENTRY(entry)), '_');
714 if (! g_ascii_strncasecmp(gtk_entry_get_text(GTK_ENTRY(entry)), "gtk", 3))
715 /*tmp = g_strconcat("gtk/", gtk_entry_get_text(GTK_ENTRY(entry)), ".h", NULL);*/
716 /* With GTK 2.14 (and later GTK 3), single header includes are encouraged */
717 tmp = g_strdup("gtk/gtk.h");
718 else if (utils_str_equal(gtk_entry_get_text(GTK_ENTRY(entry)), "GObject"))
719 tmp = g_strdup("glib-object.h");
720 else if (cc_dlg->class_type == GEANY_CLASS_TYPE_PHP)
721 tmp = g_strconcat(gtk_entry_get_text(GTK_ENTRY(entry)), ".php", NULL);
722 else
723 tmp = g_strconcat(gtk_entry_get_text(GTK_ENTRY(entry)), ".h", NULL);
725 if (cc_dlg->class_type == GEANY_CLASS_TYPE_PHP)
726 base_header = g_strdup(tmp);
727 else
728 base_header = g_ascii_strdown(tmp, -1);
730 g_free(tmp);
732 gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_header_entry), base_header);
734 if (cc_dlg->class_type == GEANY_CLASS_TYPE_GTK)
736 gchar *base_gtype;
737 if (! g_ascii_strncasecmp(gtk_entry_get_text(GTK_ENTRY(entry)), "gtk", 3))
738 tmp = g_strdup_printf("%.3s_TYPE%s",
739 base_name_splitted,
740 base_name_splitted + 3);
741 else if (utils_str_equal(gtk_entry_get_text(GTK_ENTRY(entry)), "GObject"))
742 tmp = g_strdup("G_TYPE_OBJECT");
743 else
744 tmp = g_strconcat(base_name_splitted, "_TYPE", NULL);
745 base_gtype = g_ascii_strup(tmp, -1);
746 gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_gtype_entry), base_gtype);
748 g_free(base_gtype);
749 g_free(tmp);
752 g_free(base_name_splitted);
753 g_free(base_header);
757 static gboolean create_class(CreateClassDialog *cc_dlg)
759 ClassInfo *class_info;
760 GeanyDocument *doc;
761 gchar *text;
762 gchar *tmp;
764 g_return_val_if_fail(cc_dlg != NULL, FALSE);
766 if (utils_str_equal(gtk_entry_get_text(GTK_ENTRY(cc_dlg->class_name_entry)), ""))
767 return FALSE;
769 class_info = g_new0(ClassInfo, 1);
770 class_info->type = cc_dlg->class_type;
771 class_info->class_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->class_name_entry)));
772 tmp = str_case_split(class_info->class_name, '_');
773 class_info->class_name_up = g_ascii_strup(tmp, -1);
774 class_info->class_name_low = g_ascii_strdown(class_info->class_name_up, -1);
775 if (! utils_str_equal(gtk_entry_get_text(GTK_ENTRY(cc_dlg->base_name_entry)), ""))
777 class_info->base_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->base_name_entry)));
778 if (class_info->type != GEANY_CLASS_TYPE_PHP)
780 class_info->base_include = g_strdup_printf("\n#include %c%s%c\n",
781 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->base_header_global_box)) ?
782 '<' : '\"',
783 gtk_entry_get_text(GTK_ENTRY(cc_dlg->base_header_entry)),
784 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->base_header_global_box)) ?
785 '>' : '\"');
787 else
789 class_info->base_include = g_strdup_printf("\nrequire_once \"%s\";\n",
790 gtk_entry_get_text(GTK_ENTRY(cc_dlg->base_header_entry)));
791 class_info->base_decl = g_strdup_printf(" extends %s", class_info->base_name);
794 else
796 class_info->base_name = g_strdup("");
797 class_info->base_include = g_strdup("");
799 if (cc_dlg->header_entry != NULL)
801 class_info->header = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->header_entry)));
802 class_info->header_guard = g_ascii_strup(class_info->header, -1);
803 g_strdelimit(class_info->header_guard, ".-", '_');
805 switch (class_info->type)
807 case GEANY_CLASS_TYPE_CPP:
809 class_info->source = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->source_entry)));
810 if (! utils_str_equal(class_info->base_name, ""))
811 class_info->base_decl = g_strdup_printf(": public %s", class_info->base_name);
812 else
813 class_info->base_decl = g_strdup("");
814 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_constructor_box)))
816 gchar *base_constructor;
818 if (utils_str_equal(class_info->base_name, ""))
819 base_constructor = g_strdup("");
820 else
821 base_constructor = g_strdup_printf("\t: %s()\n", class_info->base_name);
822 class_info->constructor_decl = g_strdup_printf("%s();\n", class_info->class_name);
823 class_info->constructor_impl = g_strdup_printf("\n%s::%s()\n%s{\n\t\n}\n",
824 class_info->class_name, class_info->class_name, base_constructor);
825 g_free(base_constructor);
827 else
829 class_info->constructor_decl = g_strdup("");
830 class_info->constructor_impl = g_strdup("");
832 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_destructor_box)))
834 class_info->destructor_decl =
835 g_strdup_printf("virtual ~%s();\n", class_info->class_name);
836 class_info->destructor_impl = g_strdup_printf("\n%s::~%s()\n{\n\t\n}\n",
837 class_info->class_name, class_info->class_name);
839 else
841 class_info->destructor_decl = g_strdup("");
842 class_info->destructor_impl = g_strdup("");
844 break;
846 case GEANY_CLASS_TYPE_GTK:
848 class_info->namespace = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->class_namespace_entry)));
849 if (EMPTY(class_info->namespace))
851 class_info->namespace_up = g_strdup("");
852 class_info->namespace_low = g_strdup("");
854 else
856 gchar *tmp_namespace;
857 gchar *tmp_namespace_split;
859 tmp_namespace_split = str_case_split(class_info->namespace, '_');
860 tmp_namespace = g_strconcat(tmp_namespace_split, "_", NULL);
861 class_info->namespace_up = g_ascii_strup(tmp_namespace, -1);
862 class_info->namespace_low = g_ascii_strdown(class_info->namespace_up, -1);
863 g_free(tmp_namespace);
864 g_free(tmp_namespace_split);
866 class_info->base_gtype = g_strdup(gtk_entry_get_text(
867 GTK_ENTRY(cc_dlg->base_gtype_entry)));
868 class_info->source = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->source_entry)));
869 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_constructor_box)))
871 class_info->constructor_decl = g_strdup_printf("%s *%s%s_new (void);\n",
872 gtk_entry_get_text(GTK_ENTRY(cc_dlg->gtk_constructor_type_entry)),
873 class_info->namespace_low, class_info->class_name_low);
874 class_info->constructor_impl = g_strdup_printf("\n"
875 "%s *\n"
876 "%s%s_new (void)\n"
877 "{\n"
878 " return g_object_new (%sTYPE_%s, NULL);\n"
879 "}",
880 gtk_entry_get_text(GTK_ENTRY(cc_dlg->gtk_constructor_type_entry)),
881 class_info->namespace_low, class_info->class_name_low,
882 class_info->namespace_up, class_info->class_name_up);
884 else
886 class_info->constructor_decl = g_strdup("");
887 class_info->constructor_impl = g_strdup("");
889 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_destructor_box)))
891 class_info->gtk_destructor_registration =
892 g_strdup_printf("GObjectClass *g_object_class;\n\n"
893 " g_object_class = G_OBJECT_CLASS (klass);\n\n"
894 " g_object_class->finalize = %s%s_finalize;\n",
895 class_info->namespace_low, class_info->class_name_low);
896 class_info->destructor_decl =
897 g_strdup_printf("static void %s%s_finalize (GObject *object);\n",
898 class_info->namespace_low, class_info->class_name_low);
899 class_info->destructor_impl = g_strdup_printf("\n"
900 "static void\n"
901 "%s%s_finalize (GObject *object)\n"
902 "{\n"
903 " %s%s *self;\n\n"
904 " g_return_if_fail (%sIS_%s (object));\n\n"
905 " self = %s%s (object);\n\n"
906 " G_OBJECT_CLASS (%s%s_parent_class)->finalize (object);\n"
907 "}\n",
908 class_info->namespace_low, class_info->class_name_low,
909 class_info->namespace, class_info->class_name,
910 class_info->namespace_up, class_info->class_name_up,
911 class_info->namespace_up, class_info->class_name_up,
912 class_info->namespace_low, class_info->class_name_low);
914 else
916 class_info->gtk_destructor_registration = g_strdup("");
917 class_info->destructor_decl = g_strdup("");
918 class_info->destructor_impl = g_strdup("");
920 break;
922 case GEANY_CLASS_TYPE_PHP:
924 const gchar *tmp_str;
926 class_info->source = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->source_entry)));
928 tmp_str = gtk_entry_get_text(GTK_ENTRY(cc_dlg->class_namespace_entry));
929 if (! utils_str_equal(tmp_str, ""))
930 class_info->namespace_decl = g_strdup_printf("namespace %s;", tmp_str);
931 else
932 class_info->namespace_decl = g_strdup("");
934 tmp_str = gtk_entry_get_text(GTK_ENTRY(cc_dlg->class_implements_entry));
935 if (! utils_str_equal(tmp_str, ""))
936 class_info->implements_decl = g_strdup_printf(" implements %s", tmp_str);
937 else
938 class_info->implements_decl = g_strdup("");
940 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_constructor_box)) &&
941 ! gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_isabstract_box)))
943 class_info->constructor_impl = g_strdup_printf("\n"
944 "\t/**\n"
945 "\t * Constructor of class %s.\n"
946 "\t *\n"
947 "\t * @return void\n"
948 "\t */\n"
949 "\tpublic function __construct()\n"
950 "\t{\n"
951 "\t\t// ...\n"
952 "\t}\n",
953 class_info->class_name);
955 else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_constructor_box)) &&
956 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_isabstract_box)))
958 class_info->constructor_impl = g_strdup_printf("\n"
959 "\t/**\n"
960 "\t * Constructor of class %s.\n"
961 "\t *\n"
962 "\t * @return void\n"
963 "\t */\n"
964 "\tprotected function __construct()\n"
965 "\t{\n"
966 "\t\t// ...\n"
967 "\t}\n",
968 class_info->class_name);
970 else
971 class_info->constructor_impl = g_strdup("");
973 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_destructor_box)))
975 class_info->destructor_impl = g_strdup_printf("\n"
976 "\t/**\n"
977 "\t * Destructor of class %s.\n"
978 "\t *\n"
979 "\t * @return void\n"
980 "\t */\n"
981 "\tpublic function __destruct()\n"
982 "\t{\n"
983 "\t\t// ...\n"
984 "\t}\n",
985 class_info->class_name);
987 else
988 class_info->destructor_impl = g_strdup("");
990 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_isabstract_box)))
991 class_info->abstract_decl = g_strdup("abstract ");
992 else
993 class_info->abstract_decl = g_strdup("");
995 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_issingleton_box)))
997 class_info->singleton_impl = g_strdup_printf("\n"
998 "\t/**\n"
999 "\t * Holds instance of self.\n"
1000 "\t * \n"
1001 "\t * @var %s\n"
1002 "\t */\n"
1003 "\tprotected static $kInstance = null;\n\n"
1004 "\t/**\n"
1005 "\t * Returns instance of self.\n"
1006 "\t * \n"
1007 "\t * @return %s\n"
1008 "\t */\n"
1009 "\tpublic static function getInstance() {\n"
1010 "\t\tif(!(self::$kInstance instanceof %s)) {\n"
1011 "\t\t\tself::$kInstance = new self();\n"
1012 "\t\t}\n"
1013 "\t\treturn self::$kInstance;\n"
1014 "\t}\n",
1015 class_info->class_name,
1016 class_info->class_name,
1017 class_info->class_name);
1019 else
1020 class_info->singleton_impl = g_strdup("");
1021 break;
1025 /* only create the files if the filename is not empty */
1026 if (! utils_str_equal(class_info->source, ""))
1028 doc = document_new_file(class_info->source, NULL, NULL);
1029 text = get_template_class_source(class_info);
1030 editor_insert_text_block(doc->editor, text, 0, -1, 0, TRUE);
1031 g_free(text);
1032 sci_set_current_position(doc->editor->sci, 0, TRUE);
1035 if (! utils_str_equal(class_info->header, "") && class_info->type != GEANY_CLASS_TYPE_PHP)
1037 doc = document_new_file(class_info->header, NULL, NULL);
1038 text = get_template_class_header(class_info);
1039 editor_insert_text_block(doc->editor, text, 0, -1, 0, TRUE);
1040 g_free(text);
1041 sci_set_current_position(doc->editor->sci, 0, TRUE);
1044 free_pointers(24, tmp, class_info->namespace, class_info->namespace_up,
1045 class_info->namespace_low, class_info->class_name, class_info->class_name_up,
1046 class_info->base_name, class_info->class_name_low, class_info->base_include,
1047 class_info->header, class_info->header_guard, class_info->source, class_info->base_decl,
1048 class_info->constructor_decl, class_info->constructor_impl,
1049 class_info->gtk_destructor_registration, class_info->destructor_decl,
1050 class_info->destructor_impl, class_info->base_gtype,
1051 class_info->namespace_decl, class_info->implements_decl,
1052 class_info->abstract_decl, class_info->singleton_impl, class_info, NULL);
1053 return TRUE;
1057 static void
1058 on_menu_create_cpp_class_activate (GtkMenuItem *menuitem,
1059 gpointer user_data)
1061 show_dialog_create_class(GEANY_CLASS_TYPE_CPP);
1065 static void
1066 on_menu_create_gtk_class_activate (GtkMenuItem *menuitem,
1067 gpointer user_data)
1069 show_dialog_create_class(GEANY_CLASS_TYPE_GTK);
1073 static void
1074 on_menu_create_php_class_activate (GtkMenuItem *menuitem,
1075 gpointer user_data)
1077 show_dialog_create_class(GEANY_CLASS_TYPE_PHP);
1081 void plugin_init(GeanyData *data)
1083 GtkWidget *menu_create_class1;
1084 GtkWidget *menu_create_class1_menu;
1085 GtkWidget *menu_create_cpp_class;
1086 GtkWidget *menu_create_gtk_class;
1087 GtkWidget *menu_create_php_class;
1089 menu_create_class1 = ui_image_menu_item_new (GTK_STOCK_ADD, _("Create Cla_ss"));
1090 gtk_container_add (GTK_CONTAINER (geany->main_widgets->tools_menu), menu_create_class1);
1092 menu_create_class1_menu = gtk_menu_new ();
1093 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_create_class1), menu_create_class1_menu);
1095 menu_create_cpp_class = gtk_menu_item_new_with_mnemonic (_("_C++ Class..."));
1096 gtk_container_add (GTK_CONTAINER (menu_create_class1_menu), menu_create_cpp_class);
1098 menu_create_gtk_class = gtk_menu_item_new_with_mnemonic (_("_GTK+ Class..."));
1099 gtk_container_add (GTK_CONTAINER (menu_create_class1_menu), menu_create_gtk_class);
1101 menu_create_php_class = gtk_menu_item_new_with_mnemonic (_("_PHP Class..."));
1102 gtk_container_add (GTK_CONTAINER (menu_create_class1_menu), menu_create_php_class);
1104 g_signal_connect(menu_create_cpp_class, "activate",
1105 G_CALLBACK (on_menu_create_cpp_class_activate),
1106 NULL);
1107 g_signal_connect(menu_create_gtk_class, "activate",
1108 G_CALLBACK (on_menu_create_gtk_class_activate),
1109 NULL);
1110 g_signal_connect(menu_create_php_class, "activate",
1111 G_CALLBACK (on_menu_create_php_class_activate),
1112 NULL);
1114 gtk_widget_show_all(menu_create_class1);
1116 main_menu_item = menu_create_class1;
1120 void plugin_cleanup(void)
1122 gtk_widget_destroy(main_menu_item);