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. */
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
;
50 typedef struct _ClassInfo ClassInfo
;
60 gchar
*class_name_low
;
67 gchar
*constructor_decl
;
68 gchar
*destructor_decl
;
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
;
77 gchar
*singleton_impl
;
80 typedef struct _CreateClassDialog
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
;
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\
107 class {class_name}{base_decl}\n\
114 /* add your private declarations */\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\
127 static const gchar templates_gtk_class_header
[] = "{fileheader}\n\n\
128 #ifndef {header_guard}_\n\
129 #define {header_guard}_ 1\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\
146 {base_name} parent;\n\
147 /* add your public declarations here */\n\
148 {namespace}{class_name}Private *priv;\n\
151 struct {namespace}{class_name}Class_\n\
153 {base_name}Class parent_class;\n\
156 GType {namespace_low}{class_name_low}_get_type (void);\n\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\
169 /* add your private declarations here */\n\
170 gpointer delete_me;\n\
175 G_DEFINE_TYPE ({namespace}{class_name}, {namespace_low}{class_name_low}, {base_gtype})\n\
178 {namespace_low}{class_name_low}_class_init ({namespace}{class_name}Class *klass)\n\
180 {gtk_destructor_registration}\n\
181 g_type_class_add_private ((gpointer)klass, sizeof ({namespace}{class_name}Private));\n\
187 {namespace_low}{class_name_low}_init ({namespace}{class_name} *self)\n\
189 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, {namespace_up}TYPE_{class_name_up}, {namespace}{class_name}Private);\n\
192 {constructor_impl}\n\
195 static const gchar templates_php_class_source
[] = "<?php\n\
199 {abstract_decl}class {class_name}{base_decl}{implements_decl}\n{\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. */
217 free_pointers(gsize arg_count
, ...)
223 va_start(a
, arg_count
);
224 for (i
= 0; i
< arg_count
; i
++)
226 ptr
= va_arg(a
, gpointer
);
229 ptr
= va_arg(a
, gpointer
);
231 g_warning("Wrong arg_count!");
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
);
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
);
279 return g_string_free(template, FALSE
);
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
);
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
);
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
);
348 return g_string_free(template, FALSE
);
353 /* Creates a new option label, indented on the left */
354 static GtkWidget
*cc_option_label_new(const gchar
*text
)
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
);
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
)
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
);
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);
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
,
390 GTK_EXPAND
| GTK_FILL
, GTK_FILL
, 0, 0);
395 /* Attach a new option label at the specified table row and returns
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
,
402 GTK_FILL
|GTK_SHRINK
, GTK_FILL
|GTK_SHRINK
, 0, 0);
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
)
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
,
419 GTK_EXPAND
|GTK_FILL
, GTK_FILL
, 0, 0);
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
;
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
),
436 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
437 GTK_STOCK_OK
, GTK_RESPONSE_OK
,
442 case GEANY_CLASS_TYPE_CPP
:
443 gtk_window_set_title(GTK_WINDOW(cc_dlg
->dialog
), _("Create C++ Class"));
445 case GEANY_CLASS_TYPE_GTK
:
446 gtk_window_set_title(GTK_WINDOW(cc_dlg
->dialog
), _("Create GTK+ Class"));
448 case GEANY_CLASS_TYPE_PHP
:
449 gtk_window_set_title(GTK_WINDOW(cc_dlg
->dialog
), _("Create PHP Class"));
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);
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
);
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
++);
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
,
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
,
515 gtk_table_attach(GTK_TABLE(table
), hdr_hbox
,
517 GTK_FILL
| GTK_EXPAND
,
518 GTK_FILL
| GTK_EXPAND
,
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
,
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
))
596 gtk_widget_destroy(cc_dlg
->dialog
);
600 static void cc_dlg_on_set_sensitive_toggled(GtkWidget
*toggle_button
, GtkWidget
*target_widget
)
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
)
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
);
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
);
649 case GEANY_CLASS_TYPE_PHP
:
652 class_source
= g_strconcat(class_name
, ".php", NULL
);
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
);
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
)
687 g_return_val_if_fail(str
!= NULL
, NULL
);
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
;
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
);
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
);
728 base_header
= g_ascii_strdown(tmp
, -1);
732 gtk_entry_set_text(GTK_ENTRY(cc_dlg
->base_header_entry
), base_header
);
734 if (cc_dlg
->class_type
== GEANY_CLASS_TYPE_GTK
)
737 if (! g_ascii_strncasecmp(gtk_entry_get_text(GTK_ENTRY(entry
)), "gtk", 3))
738 tmp
= g_strdup_printf("%.3s_TYPE%s",
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");
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
);
752 g_free(base_name_splitted
);
757 static gboolean
create_class(CreateClassDialog
*cc_dlg
)
759 ClassInfo
*class_info
;
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
)), ""))
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
)) ?
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
)) ?
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
);
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
);
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("");
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
);
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
);
841 class_info
->destructor_decl
= g_strdup("");
842 class_info
->destructor_impl
= g_strdup("");
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("");
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"
878 " return g_object_new (%sTYPE_%s, NULL);\n"
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
);
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"
901 "%s%s_finalize (GObject *object)\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"
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
);
916 class_info
->gtk_destructor_registration
= g_strdup("");
917 class_info
->destructor_decl
= g_strdup("");
918 class_info
->destructor_impl
= g_strdup("");
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
);
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
);
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"
945 "\t * Constructor of class %s.\n"
947 "\t * @return void\n"
949 "\tpublic function __construct()\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"
960 "\t * Constructor of class %s.\n"
962 "\t * @return void\n"
964 "\tprotected function __construct()\n"
968 class_info
->class_name
);
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"
977 "\t * Destructor of class %s.\n"
979 "\t * @return void\n"
981 "\tpublic function __destruct()\n"
985 class_info
->class_name
);
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 ");
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"
999 "\t * Holds instance of self.\n"
1003 "\tprotected static $kInstance = null;\n\n"
1005 "\t * Returns instance of self.\n"
1009 "\tpublic static function getInstance() {\n"
1010 "\t\tif(!(self::$kInstance instanceof %s)) {\n"
1011 "\t\t\tself::$kInstance = new self();\n"
1013 "\t\treturn self::$kInstance;\n"
1015 class_info
->class_name
,
1016 class_info
->class_name
,
1017 class_info
->class_name
);
1020 class_info
->singleton_impl
= g_strdup("");
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
);
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
);
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
);
1058 on_menu_create_cpp_class_activate (GtkMenuItem
*menuitem
,
1061 show_dialog_create_class(GEANY_CLASS_TYPE_CPP
);
1066 on_menu_create_gtk_class_activate (GtkMenuItem
*menuitem
,
1069 show_dialog_create_class(GEANY_CLASS_TYPE_GTK
);
1074 on_menu_create_php_class_activate (GtkMenuItem
*menuitem
,
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
),
1107 g_signal_connect(menu_create_gtk_class
, "activate",
1108 G_CALLBACK (on_menu_create_gtk_class_activate
),
1110 g_signal_connect(menu_create_php_class
, "activate",
1111 G_CALLBACK (on_menu_create_php_class_activate
),
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
);