From 363690e9d4e6060eca6bfefff901b5046119f4f7 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Thu, 14 Jun 2007 13:10:51 +0000 Subject: [PATCH] Apply class builder patch from Alexander Rodin (thanks). git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@1616 ea778897-0a13-0410-b9d1-a72fbfd435f5 --- ChangeLog | 7 + THANKS | 3 +- geany.glade | 43 +++++ src/callbacks.c | 16 ++ src/callbacks.h | 8 + src/interface.c | 35 ++++ src/templates.c | 221 +++++++++++++++++++++++++ src/templates.h | 32 +++- src/tools.c | 422 +++++++++++++++++++++++++++++++++++++++++++++++- src/tools.h | 2 + 10 files changed, 786 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index ca43916f..4de602f7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2007-06-14 Nick Treleaven + + * src/templates.c, src/interface.c, src/templates.h, src/tools.c, + src/tools.h, src/callbacks.c, src/callbacks.h, geany.glade, THANKS: + Apply class builder patch from Alexander Rodin (thanks). + + 2007-06-13 Nick Treleaven * src/build.c, src/utils.c: diff --git a/THANKS b/THANKS index 63b8bfdb..95cb7cfd 100644 --- a/THANKS +++ b/THANKS @@ -20,7 +20,7 @@ Kevin Ellwood Stefan Oltmanns - escape sequences patch Bob Doan - some patches Rob van der Linde - fixed wrong vte height on some systems -Josef Whiter - parse 'Entering directory' build messages. +Josef Whiter - parse 'Entering directory' build messages Jeff Pohlmeyer - "Allow DnD", "Use tabs" and other great patches Tomás Vírseda - sort open files patch Dirk Weber @@ -31,6 +31,7 @@ Guillaume Hoffmann - Haskell filetype patch Peter Strand - Haskell CTags patch Dave Moore - code navigation patch François Cami , Guillaume Duviol - tab replacement patch +Alexander Rodin - class builder patch Translators: ---------------------------------- diff --git a/geany.glade b/geany.glade index 84177bea..91814f01 100644 --- a/geany.glade +++ b/geany.glade @@ -1376,6 +1376,49 @@ + + + + True + Create cla_ss + True + + + + True + gtk-add + 1 + 0.5 + 0.5 + 0 + 0 + + + + + + + + + True + C++ class + True + + + + + + + True + Gtk+ class + True + + + + + + + diff --git a/src/callbacks.c b/src/callbacks.c index 88b37e77..9d36bb05 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -2294,3 +2294,19 @@ gboolean on_motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer user return FALSE; } + +void +on_menu_create_cpp_class_activate (GtkMenuItem *menuitem, + gpointer user_data) +{ + tools_show_dialog_create_class(GEANY_CLASS_TYPE_CPP); +} + + +void +on_menu_create_gtk_class_activate (GtkMenuItem *menuitem, + gpointer user_data) +{ + tools_show_dialog_create_class(GEANY_CLASS_TYPE_GTK); +} + diff --git a/src/callbacks.h b/src/callbacks.h index ba4718b5..3eaab66a 100644 --- a/src/callbacks.h +++ b/src/callbacks.h @@ -616,3 +616,11 @@ gboolean on_motion_event (GtkWidget *widget, GdkEventMotion *event, gpointer user_data); + +void +on_menu_create_cpp_class_activate (GtkMenuItem *menuitem, + gpointer user_data); + +void +on_menu_create_gtk_class_activate (GtkMenuItem *menuitem, + gpointer user_data); diff --git a/src/interface.c b/src/interface.c index efad1208..70f4a357 100644 --- a/src/interface.c +++ b/src/interface.c @@ -193,6 +193,11 @@ create_window1 (void) GtkWidget *image1792; GtkWidget *menu_count_words1; GtkWidget *menu_insert_special_chars1; + GtkWidget *menu_create_class1; + GtkWidget *image1861; + GtkWidget *menu_create_class1_menu; + GtkWidget *menu_create_cpp_class; + GtkWidget *menu_create_gtk_class; GtkWidget *menu_help1; GtkWidget *menu_item5_menu; GtkWidget *help1; @@ -955,6 +960,25 @@ create_window1 (void) gtk_widget_show (menu_insert_special_chars1); gtk_container_add (GTK_CONTAINER (tools1_menu), menu_insert_special_chars1); + menu_create_class1 = gtk_image_menu_item_new_with_mnemonic (_("Create cla_ss")); + gtk_widget_show (menu_create_class1); + gtk_container_add (GTK_CONTAINER (tools1_menu), menu_create_class1); + + image1861 = gtk_image_new_from_stock ("gtk-add", GTK_ICON_SIZE_MENU); + gtk_widget_show (image1861); + gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_create_class1), image1861); + + menu_create_class1_menu = gtk_menu_new (); + gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_create_class1), menu_create_class1_menu); + + menu_create_cpp_class = gtk_menu_item_new_with_mnemonic (_("C++ class")); + gtk_widget_show (menu_create_cpp_class); + gtk_container_add (GTK_CONTAINER (menu_create_class1_menu), menu_create_cpp_class); + + menu_create_gtk_class = gtk_menu_item_new_with_mnemonic (_("Gtk+ class")); + gtk_widget_show (menu_create_gtk_class); + gtk_container_add (GTK_CONTAINER (menu_create_class1_menu), menu_create_gtk_class); + menu_help1 = gtk_menu_item_new_with_mnemonic (_("_Help")); gtk_widget_show (menu_help1); gtk_container_add (GTK_CONTAINER (menubar1), menu_help1); @@ -1508,6 +1532,12 @@ create_window1 (void) g_signal_connect ((gpointer) menu_insert_special_chars1, "activate", G_CALLBACK (on_menu_insert_special_chars1_activate), NULL); + g_signal_connect ((gpointer) menu_create_cpp_class, "activate", + G_CALLBACK (on_menu_create_cpp_class_activate), + NULL); + g_signal_connect ((gpointer) menu_create_gtk_class, "activate", + G_CALLBACK (on_menu_create_gtk_class_activate), + NULL); g_signal_connect ((gpointer) help1, "activate", G_CALLBACK (on_help1_activate), NULL); @@ -1760,6 +1790,11 @@ create_window1 (void) GLADE_HOOKUP_OBJECT (window1, image1792, "image1792"); GLADE_HOOKUP_OBJECT (window1, menu_count_words1, "menu_count_words1"); GLADE_HOOKUP_OBJECT (window1, menu_insert_special_chars1, "menu_insert_special_chars1"); + GLADE_HOOKUP_OBJECT (window1, menu_create_class1, "menu_create_class1"); + GLADE_HOOKUP_OBJECT (window1, image1861, "image1861"); + GLADE_HOOKUP_OBJECT (window1, menu_create_class1_menu, "menu_create_class1_menu"); + GLADE_HOOKUP_OBJECT (window1, menu_create_cpp_class, "menu_create_cpp_class"); + GLADE_HOOKUP_OBJECT (window1, menu_create_gtk_class, "menu_create_gtk_class"); GLADE_HOOKUP_OBJECT (window1, menu_help1, "menu_help1"); GLADE_HOOKUP_OBJECT (window1, menu_item5_menu, "menu_item5_menu"); GLADE_HOOKUP_OBJECT (window1, help1, "help1"); diff --git a/src/templates.c b/src/templates.c index 41ff4ba7..0f0812c4 100644 --- a/src/templates.c +++ b/src/templates.c @@ -206,6 +206,127 @@ end\n\ x = StdClass.new\n\ "; +static const gchar templates_cpp_class_header[] = "{fileheader}\n\n\ +#ifndef {header_guard}\n\ +#define {header_guard}\n\ +{base_include}\n\ +class {class_name}{base_decl}\n\ +{\n\ + public:\n\ + {constructor_decl}\ + {destructor_decl}\ + \n\ + private:\n\ + // add your private declarations\n\ +};\n\ +\n\ +#endif // {header_guard}\n\ +"; + +static const gchar templates_cpp_class_source[] = "{fileheader}\n\n\ +#include \"{header}\"\n\ +\n\ +{constructor_impl}\n\ +{destructor_impl}\n\ +"; + +static const gchar templates_gtk_class_header[] = "{fileheader}\n\n\ +#ifndef __{header_guard}__\n\ +#define __{header_guard}__\n\ +{base_include}\n\ +G_BEGIN_DECLS\n\ +\n\ +#define {class_name_up}_TYPE ({class_name_low}_get_type())\n\ +#define {class_name_up}(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\\\n\ + {class_name_up}_TYPE, {class_name}))\n\ +#define {class_name_up}_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),\\\n\ + {class_name_up}_TYPE, {class_name}Class))\n\ +#define IS_{class_name_up}(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),\\\n\ + {class_name_up}_TYPE))\n\ +#define IS_{class_name_up}_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),\\\n\ + {class_name_up}_TYPE))\n\ +\n\ +typedef struct _{class_name} {class_name};\n\ +typedef struct _{class_name}Class {class_name}Class;\n\ +\n\ +struct _{class_name}\n\ +{\n\ + {base_name} parent;\n\ + /* add your public declarations here */\n\ +};\n\ +\n\ +struct _{class_name}Class\n\ +{\n\ + {base_name}Class parent_class;\n\ +};\n\ +\n\ +GType {class_name_low}_get_type (void);\n\ +{constructor_decl}\ +\n\ +G_END_DECLS\n\ +\n\ +#endif /* __{header_guard}__ */\n\ +"; + +static const gchar templates_gtk_class_source[] = "{fileheader}\n\ +#include \"{header}\"\n\ +\n\ +typedef struct _{class_name}Private {class_name}Private;\n\ +\n\ +#define {class_name_up}_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj),\\\n\ + {class_name_up}_TYPE, {class_name}Private))\n\ +\n\ +struct _{class_name}Private\n\ +{\n\ + /* add your private declarations here */\n\ +};\n\ +\n\ +static void {class_name_low}_class_init ({class_name}Class *klass);\n\ +static void {class_name_low}_init ({class_name} *self);\n\ +{destructor_decl}\ +\n\ +/* Local data */\n\ +static {base_name}Class *parent_class = NULL;\n\ +\n\ +GType {class_name_low}_get_type(void)\n\ +{\n\ + static GType self_type = 0;\n\ + if (! self_type)\n\ + {\n\ + static const GTypeInfo self_info = \n\ + {\n\ + sizeof({class_name}Class),\n\ + NULL, /* base_init */\n\ + NULL, /* base_finalize */\n\ + (GClassInitFunc){class_name_low}_class_init,\n\ + NULL, /* class_finalize */\n\ + NULL, /* class_data */\n\ + sizeof({class_name}),\n\ + 0,\n\ + (GInstanceInitFunc){class_name_low}_init,\n\ + };\n\ + \n\ + self_type = g_type_register_static({base_gtype}, \"{class_name}\", &self_info, 0);\ + }\n\ + \n\ + return self_type;\n\ +}\n\ +\n\ +static void {class_name_low}_class_init({class_name}Class *klass)\n\ +{\n\ + {gtk_destructor_registration}\n\ + parent_class = gtk_type_class({base_gtype});\n\ + g_type_class_add_private((gpointer)klass, sizeof({class_name}Private));\n\ +}\n\ +\n\ +static void {class_name_low}_init({class_name} *self)\n\ +{\n\ + \n\ +}\n\ +\n\ +{constructor_impl}\n\ +{destructor_impl}\n\ +"; static gchar *templates[GEANY_MAX_TEMPLATES]; static gchar *ft_templates[GEANY_MAX_FILE_TYPES] = {NULL}; @@ -638,3 +759,103 @@ static gchar *templates_replace_all(gchar *text, const gchar *year, const gchar return text; } + +gchar* templates_get_template_class_header(ClassInfo *class_info) +{ + switch (class_info->type) + { + case GEANY_CLASS_TYPE_CPP: + { + gchar *fileheader = NULL; + gchar *template; + + fileheader = get_file_header(filetypes[GEANY_FILETYPES_C], class_info->header); + template = g_strdup(templates_cpp_class_header); + template = utils_str_replace(template, "{fileheader}", fileheader); + template = utils_str_replace(template, "{header_guard}", class_info->header_guard); + template = utils_str_replace(template, "{base_include}", class_info->base_include); + template = utils_str_replace(template, "{class_name}", class_info->class_name); + template = utils_str_replace(template, "{base_decl}", class_info->base_decl); + template = utils_str_replace(template, "{constructor_decl}", + class_info->constructor_decl); + template = utils_str_replace(template, "{destructor_decl}", + class_info->destructor_decl); + + return template; + } + case GEANY_CLASS_TYPE_GTK: + { + gchar *fileheader = NULL; + gchar *template; + + fileheader = get_file_header(filetypes[GEANY_FILETYPES_C], class_info->header); + template = g_strdup(templates_gtk_class_header); + template = utils_str_replace(template, "{fileheader}", fileheader); + template = utils_str_replace(template, "{header_guard}", class_info->header_guard); + template = utils_str_replace(template, "{base_include}", class_info->base_include); + template = utils_str_replace(template, "{class_name}", class_info->class_name); + template = utils_str_replace(template, "{class_name_up}", class_info->class_name_up); + template = utils_str_replace(template, "{class_name_low}", class_info->class_name_low); + template = utils_str_replace(template, "{base_name}", class_info->base_name); + template = utils_str_replace(template, "{constructor_decl}", + class_info->constructor_decl); + + return template; + } + } + + return NULL; +} + +gchar* templates_get_template_class_source(ClassInfo *class_info) +{ + switch (class_info->type) + { + case GEANY_CLASS_TYPE_CPP: + { + gchar *fileheader = NULL; + gchar *template; + + fileheader = get_file_header(filetypes[GEANY_FILETYPES_C], class_info->source); + template = g_strdup(templates_cpp_class_source); + template = utils_str_replace(template, "{fileheader}", fileheader); + template = utils_str_replace(template, "{header}", class_info->header); + template = utils_str_replace(template, "{class_name}", class_info->class_name); + template = utils_str_replace(template, "{base_include}", class_info->base_include); + template = utils_str_replace(template, "{base_name}", class_info->base_name); + template = utils_str_replace(template, "{constructor_impl}", + class_info->constructor_impl); + template = utils_str_replace(template, "{destructor_impl}", + class_info->destructor_impl); + + return template; + } + case GEANY_CLASS_TYPE_GTK: + { + gchar *fileheader = NULL; + gchar *template; + + fileheader = get_file_header(filetypes[GEANY_FILETYPES_C], class_info->source); + template = g_strdup(templates_gtk_class_source); + template = utils_str_replace(template, "{fileheader}", fileheader); + template = utils_str_replace(template, "{header}", class_info->header); + template = utils_str_replace(template, "{class_name}", class_info->class_name); + template = utils_str_replace(template, "{class_name_up}", class_info->class_name_up); + template = utils_str_replace(template, "{class_name_low}", class_info->class_name_low); + template = utils_str_replace(template, "{base_name}", class_info->base_name); + template = utils_str_replace(template, "{base_gtype}", class_info->base_gtype); + template = utils_str_replace(template, "{destructor_decl}", class_info->destructor_decl); + template = utils_str_replace(template, "{constructor_impl}", + class_info->constructor_impl); + template = utils_str_replace(template, "{destructor_impl}", + class_info->destructor_impl); + template = utils_str_replace(template, "{gtk_destructor_registration}", + class_info->gtk_destructor_registration); + + return template; + } + } + + return NULL; +} + diff --git a/src/templates.h b/src/templates.h index d1d5a1f8..4f1ad0dc 100644 --- a/src/templates.h +++ b/src/templates.h @@ -26,6 +26,8 @@ #include "filetypes.h" +typedef struct _ClassInfo ClassInfo; + void templates_init(void); gchar *templates_get_template_fileheader(gint idx); @@ -40,8 +42,11 @@ gchar *templates_get_template_function(gint filetype_idx, const gchar *func_name gchar *templates_get_template_licence(gint filetype_idx, gint licence_type); -void templates_free_templates(void); +gchar *templates_get_template_class_header(ClassInfo *class_info); +gchar *templates_get_template_class_source(ClassInfo *class_info); + +void templates_free_templates(void); enum { @@ -53,5 +58,30 @@ enum GEANY_MAX_TEMPLATES }; +enum +{ + GEANY_CLASS_TYPE_CPP, + GEANY_CLASS_TYPE_GTK +}; + +struct _ClassInfo +{ + gint type; + gchar *class_name; + gchar *class_name_up; + gchar *class_name_low; + gchar *base_name; + gchar *base_gtype; + gchar *header; + gchar *header_guard; + gchar *base_include; + gchar *base_decl; + gchar *constructor_decl; + gchar *destructor_decl; + gchar *source; + gchar *constructor_impl; + gchar *destructor_impl; + gchar *gtk_destructor_registration; +}; #endif diff --git a/src/tools.c b/src/tools.c index 52d945bf..682ce269 100644 --- a/src/tools.c +++ b/src/tools.c @@ -46,6 +46,7 @@ #include "ui_utils.h" #include "msgwindow.h" #include "keybindings.h" +#include "templates.h" enum @@ -55,6 +56,23 @@ enum N_COLUMNS }; +typedef struct _CreateClassDialog +{ + gint class_type; + GtkWidget *dialog; + GtkWidget *class_name_entry; + GtkWidget *header_entry; + GtkWidget *source_entry; + GtkWidget *base_name_entry; + GtkWidget *base_header_entry; + GtkWidget *base_header_global_box; + GtkWidget *base_gtype_entry; + GtkWidget *create_constructor_box; + GtkWidget *create_destructor_box; + GtkWidget *gtk_constructor_type_entry; +} +CreateClassDialog; + static GtkWidget *sc_dialog = NULL; static GtkTreeStore *sc_store = NULL; static GtkTreeView *sc_tree = NULL; @@ -66,7 +84,10 @@ static void sc_on_tree_row_activated static void sc_fill_store(GtkTreeStore *store); static gboolean sc_insert(GtkTreeModel *model, GtkTreeIter *iter); - +static void on_set_sensetive_toggled(GtkWidget *toggle_button, GtkWidget *target_widget); +static void on_class_name_entry_changed(GtkWidget *entry, CreateClassDialog *cc_dlg); +static void on_base_name_entry_changed(GtkWidget *entry, CreateClassDialog *cc_dlg); +static void on_create_class(CreateClassDialog *cc_dlg); void tools_show_dialog_insert_special_chars() { @@ -900,4 +921,403 @@ void tools_create_insert_custom_command_menu_items() } +void tools_show_dialog_create_class(gint type) +{ + CreateClassDialog *cc_dlg; + GtkWidget *main_box; + GtkWidget *frame; + GtkWidget *vbox; + GtkWidget *hbox; + GtkWidget *label; + GtkWidget *action_area; + GtkWidget *cancel_button; + GtkWidget *ok_button; + + cc_dlg = g_malloc(sizeof(CreateClassDialog)); + cc_dlg->class_type = type; + + cc_dlg->dialog = gtk_dialog_new(); + gtk_window_set_title(GTK_WINDOW(cc_dlg->dialog), _("Create class")); + gtk_container_set_border_width(GTK_CONTAINER(cc_dlg->dialog), 5); + g_signal_connect_swapped(G_OBJECT(cc_dlg->dialog), "destroy", + G_CALLBACK(g_free), (gpointer)cc_dlg); + + main_box = GTK_DIALOG(cc_dlg->dialog)->vbox; + gtk_box_set_spacing(GTK_BOX(main_box), 10); + + frame = gtk_frame_new(_("Class")); + gtk_container_add(GTK_CONTAINER(main_box), frame); + gtk_widget_show(frame); + + vbox = gtk_vbox_new(FALSE, 10); + gtk_container_set_border_width(GTK_CONTAINER(vbox), 5); + gtk_container_add(GTK_CONTAINER(frame), vbox); + gtk_widget_show(vbox); + + hbox = gtk_hbox_new(FALSE, 10); + gtk_container_add(GTK_CONTAINER(vbox), hbox); + gtk_widget_show(hbox); + + label = gtk_label_new(_("Class name:")); + gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); + gtk_widget_show(label); + + cc_dlg->class_name_entry = gtk_entry_new(); + gtk_box_pack_start(GTK_BOX(hbox), cc_dlg->class_name_entry, TRUE, TRUE, 0); + gtk_widget_show(cc_dlg->class_name_entry); + g_signal_connect(G_OBJECT(cc_dlg->class_name_entry), "changed", + G_CALLBACK(on_class_name_entry_changed), cc_dlg); + + hbox = gtk_hbox_new(FALSE, 10); + gtk_container_add(GTK_CONTAINER(vbox), hbox); + gtk_widget_show(hbox); + + label = gtk_label_new(_("Header file:")); + gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); + gtk_widget_show(label); + + cc_dlg->header_entry = gtk_entry_new(); + gtk_container_add(GTK_CONTAINER(hbox), cc_dlg->header_entry); + gtk_widget_show(cc_dlg->header_entry); + + hbox = gtk_hbox_new(FALSE, 10); + gtk_container_add(GTK_CONTAINER(vbox), hbox); + gtk_widget_show(hbox); + + label = gtk_label_new(_("Source file:")); + gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); + gtk_widget_show(label); + + cc_dlg->source_entry = gtk_entry_new(); + gtk_container_add(GTK_CONTAINER(hbox), cc_dlg->source_entry); + gtk_widget_show(cc_dlg->source_entry); + + frame = gtk_frame_new(_("Inheritance")); + gtk_container_add(GTK_CONTAINER(main_box), frame); + gtk_widget_show(frame); + + vbox = gtk_vbox_new(FALSE, 10); + gtk_container_set_border_width(GTK_CONTAINER(vbox), 5); + gtk_container_add(GTK_CONTAINER(frame), vbox); + gtk_widget_show(vbox); + + hbox = gtk_hbox_new(FALSE, 10); + gtk_container_add(GTK_CONTAINER(vbox), hbox); + gtk_widget_show(hbox); + + label = gtk_label_new(_("Base class:")); + gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); + gtk_widget_show(label); + + cc_dlg->base_name_entry = gtk_entry_new(); + if (type == GEANY_CLASS_TYPE_GTK) + gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_name_entry), "GtkObject"); + gtk_container_add(GTK_CONTAINER(hbox), cc_dlg->base_name_entry); + gtk_widget_show(cc_dlg->base_name_entry); + g_signal_connect(G_OBJECT(cc_dlg->base_name_entry), "changed", + G_CALLBACK(on_base_name_entry_changed), (gpointer)cc_dlg); + + hbox = gtk_hbox_new(FALSE, 10); + gtk_container_add(GTK_CONTAINER(vbox), hbox); + gtk_widget_show(hbox); + + label = gtk_label_new(_("Base header:")); + gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); + gtk_widget_show(label); + + cc_dlg->base_header_entry = gtk_entry_new(); + if (type == GEANY_CLASS_TYPE_GTK) + gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_header_entry), "gtk/gtkobject.h"); + gtk_container_add(GTK_CONTAINER(hbox), cc_dlg->base_header_entry); + gtk_widget_show(cc_dlg->base_header_entry); + + cc_dlg->base_header_global_box = gtk_check_button_new_with_label(_("Global")); + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cc_dlg->base_header_global_box), TRUE); + gtk_box_pack_end(GTK_BOX(hbox), cc_dlg->base_header_global_box, FALSE, FALSE, 0); + gtk_widget_show(cc_dlg->base_header_global_box); + + if (type == GEANY_CLASS_TYPE_GTK) + { + hbox = gtk_hbox_new(FALSE, 10); + gtk_container_add(GTK_CONTAINER(vbox), hbox); + gtk_widget_show(hbox); + + label = gtk_label_new(_("Base GType:")); + gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); + gtk_widget_show(label); + + cc_dlg->base_gtype_entry = gtk_entry_new(); + gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_gtype_entry), "GTK_TYPE_OBJECT"); + gtk_container_add(GTK_CONTAINER(hbox), cc_dlg->base_gtype_entry); + gtk_widget_show(cc_dlg->base_gtype_entry); + } + + frame = gtk_frame_new(_("Options")); + gtk_container_add(GTK_CONTAINER(main_box), frame); + gtk_widget_show(frame); + + vbox = gtk_vbox_new(FALSE, 10); + gtk_container_add(GTK_CONTAINER(frame), vbox); + gtk_widget_show(vbox); + + hbox = gtk_hbox_new(FALSE, 10); + gtk_container_add(GTK_CONTAINER(vbox), hbox); + gtk_widget_show(hbox); + + cc_dlg->create_constructor_box = gtk_check_button_new_with_label(_("Create constructor")); + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cc_dlg->create_constructor_box), TRUE); + gtk_container_add(GTK_CONTAINER(hbox), cc_dlg->create_constructor_box); + gtk_widget_show(cc_dlg->create_constructor_box); + + cc_dlg->create_destructor_box = gtk_check_button_new_with_label(_("Create destructor")); + gtk_container_add(GTK_CONTAINER(hbox), cc_dlg->create_destructor_box); + gtk_widget_show(cc_dlg->create_destructor_box); + + if (type == GEANY_CLASS_TYPE_GTK) + { + hbox = gtk_hbox_new(FALSE, 10); + gtk_container_add(GTK_CONTAINER(vbox), hbox); + gtk_widget_show(hbox); + g_signal_connect(G_OBJECT(cc_dlg->create_constructor_box), "toggled", + G_CALLBACK(on_set_sensetive_toggled), (gpointer)hbox); + + label = gtk_label_new(_("GTK+ constructor type")); + gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); + gtk_widget_show(label); + + cc_dlg->gtk_constructor_type_entry = gtk_entry_new(); + gtk_entry_set_text(GTK_ENTRY(cc_dlg->gtk_constructor_type_entry), "GtkObject"); + gtk_container_add(GTK_CONTAINER(hbox), cc_dlg->gtk_constructor_type_entry); + gtk_widget_show(cc_dlg->gtk_constructor_type_entry); + } + + action_area = GTK_DIALOG(cc_dlg->dialog)->action_area; + + cancel_button = gtk_button_new_from_stock(GTK_STOCK_CANCEL); + gtk_container_add(GTK_CONTAINER(action_area), cancel_button); + gtk_widget_show(cancel_button); + + g_signal_connect_swapped(G_OBJECT(cancel_button), "clicked", + G_CALLBACK(gtk_object_destroy), (gpointer)cc_dlg->dialog); + + ok_button = gtk_button_new_from_stock(GTK_STOCK_OK); + gtk_container_add(GTK_CONTAINER(action_area), ok_button); + gtk_widget_show(ok_button); + g_signal_connect_swapped(G_OBJECT(ok_button), "clicked", + G_CALLBACK(on_create_class), (gpointer)cc_dlg); + + gtk_widget_show(cc_dlg->dialog); +} + +static void on_set_sensetive_toggled(GtkWidget *toggle_button, GtkWidget *target_widget) +{ + g_return_if_fail(toggle_button != NULL); + g_return_if_fail(GTK_IS_TOGGLE_BUTTON(toggle_button)); + g_return_if_fail(target_widget != NULL); + g_return_if_fail(GTK_IS_WIDGET(target_widget)); + + gtk_widget_set_sensitive(target_widget, + gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle_button))); +} + +static void on_class_name_entry_changed(GtkWidget *entry, CreateClassDialog *cc_dlg) +{ + gchar *class_name_down; + gchar *class_header; + gchar *class_source; + + g_return_if_fail(entry != NULL); + g_return_if_fail(GTK_IS_ENTRY(entry)); + g_return_if_fail(cc_dlg != NULL); + + class_name_down = g_ascii_strdown(gtk_entry_get_text(GTK_ENTRY(entry)), -1); + class_header = g_strconcat(class_name_down, ".h", NULL); + if (cc_dlg->class_type == GEANY_CLASS_TYPE_CPP) + class_source = g_strconcat(class_name_down, ".cpp", NULL); + else + class_source = g_strconcat(class_name_down, ".c", NULL); + + gtk_entry_set_text(GTK_ENTRY(cc_dlg->header_entry), class_header); + gtk_entry_set_text(GTK_ENTRY(cc_dlg->source_entry), class_source); + + g_free(class_name_down); + g_free(class_header); + g_free(class_source); +} + +static gchar* str_case_split(const gchar *str, char splitter) +{ + GString *result; + + g_return_val_if_fail(str != NULL, NULL); + if (*str == '\0') + return g_strdup(""); + + result = g_string_new(NULL); + g_string_append_c(result, *str); + while (*(++str) != '\0') + { + if (g_ascii_isupper(*str) && g_ascii_islower(result->str[result->len - 1])) + g_string_append_c(result, splitter); + g_string_append_c(result, *str); + } + return g_string_free(result, FALSE); +} + +static void on_base_name_entry_changed(GtkWidget *entry, CreateClassDialog *cc_dlg) +{ + gchar *base_name_splitted; + gchar *base_header; + gchar *base_gtype = NULL; + + g_return_if_fail(entry != NULL); + g_return_if_fail(GTK_IS_ENTRY(entry)); + g_return_if_fail(cc_dlg != NULL); + + base_name_splitted = str_case_split(gtk_entry_get_text(GTK_ENTRY(entry)), '_'); + base_header = g_strdup_printf("%s%s.h", + g_ascii_strncasecmp(gtk_entry_get_text(GTK_ENTRY(entry)), "gtk", 3) ? "": "gtk/", + gtk_entry_get_text(GTK_ENTRY(entry))); + g_strdown(base_header); + + gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_header_entry), base_header); + + if (cc_dlg->class_type == GEANY_CLASS_TYPE_GTK) + { + if (! g_ascii_strncasecmp(gtk_entry_get_text(GTK_ENTRY(entry)), "gtk", 3)) + { + base_gtype = g_strdup_printf("%.3s_TYPE%s", + base_name_splitted, + base_name_splitted + 3); + } + else + base_gtype = g_strconcat(base_name_splitted, "_TYPE", NULL); + g_strup(base_gtype); + gtk_entry_set_text(GTK_ENTRY(cc_dlg->base_gtype_entry), base_gtype); + } + + g_free(base_name_splitted); + g_free(base_header); + g_free(base_gtype); +} + +static void on_create_class(CreateClassDialog *cc_dlg) +{ + ClassInfo *class_info; + gint idx; + gchar *text; + + g_return_if_fail(cc_dlg != NULL); + + class_info = g_malloc0(sizeof(ClassInfo)); + class_info->type = cc_dlg->class_type; + class_info->class_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->class_name_entry))); + class_info->class_name_up = str_case_split(class_info->class_name, '_'); + g_strup(class_info->class_name_up); + class_info->class_name_low = g_ascii_strdown(class_info->class_name_up, -1); + if (*gtk_entry_get_text(GTK_ENTRY(cc_dlg->base_name_entry)) != '\0') + { + class_info->base_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->base_name_entry))); + class_info->base_include = g_strdup_printf("\n#include %c%s%c\n", + gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->base_header_global_box)) ? + '<' : '\"', + gtk_entry_get_text(GTK_ENTRY(cc_dlg->base_header_entry)), + gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->base_header_global_box)) ? + '>' : '\"'); + } + class_info->header = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->header_entry))); + class_info->header_guard = g_ascii_strup(class_info->header, -1); + g_strdelimit(class_info->header_guard, ".", '_'); + switch (class_info->type) + { + case GEANY_CLASS_TYPE_CPP: + { + class_info->source = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->source_entry))); + if (class_info->base_name != NULL) + class_info->base_decl = g_strdup_printf(": public %s", class_info->base_name); + if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_constructor_box))) + { + gchar *base_constructor; + + if (class_info->base_name == NULL) + base_constructor = g_strdup(""); + else + base_constructor = g_strdup_printf("\t: %s()\n", class_info->base_name); + class_info->constructor_decl = g_strdup_printf("%s();\n", class_info->class_name); + class_info->constructor_impl = g_strdup_printf("\n%s::%s()\n%s{\n\t\n}\n", + class_info->class_name, class_info->class_name, base_constructor); + g_free(base_constructor); + } + if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_destructor_box))) + { + class_info->destructor_decl = + g_strdup_printf("virtual ~%s();\n", class_info->class_name); + class_info->destructor_impl = g_strdup_printf("\n%s::~%s()\n{\n\t\n}\n", + class_info->class_name, class_info->class_name); + } + break; + } + case GEANY_CLASS_TYPE_GTK: + { + class_info->base_gtype = g_strdup(gtk_entry_get_text( + GTK_ENTRY(cc_dlg->base_gtype_entry))); + class_info->source = g_strdup(gtk_entry_get_text(GTK_ENTRY(cc_dlg->source_entry))); + if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_constructor_box))) + { + class_info->constructor_decl = g_strdup_printf("%s*\t%s_new\t\t\t(void);\n", + gtk_entry_get_text(GTK_ENTRY(cc_dlg->gtk_constructor_type_entry)), + class_info->class_name_low); + class_info->constructor_impl = g_strdup_printf("\n" + "%s* %s_new(void)\n" + "{\n" + "\treturn (%s*)g_object_new(%s_TYPE, NULL);\n" + "}\n", + gtk_entry_get_text(GTK_ENTRY(cc_dlg->gtk_constructor_type_entry)), + class_info->class_name_low, + gtk_entry_get_text(GTK_ENTRY(cc_dlg->gtk_constructor_type_entry)), + class_info->class_name_up); + } + if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cc_dlg->create_destructor_box))) + { + class_info->gtk_destructor_registration = + g_strdup_printf("GtkObjectClass *object_class;\n\n" + "\tobject_class = (GtkObjectClass*)klass;\n\n" + "\tobject_class->destroy = %s_destroy;\n", + class_info->class_name_low); + class_info->destructor_decl = + g_strdup_printf("static void %s_destroy\t\t\t(GtkObject *object);\n", + class_info->class_name_low); + class_info->destructor_impl = g_strdup_printf("\n" + "void %s_destroy(GtkObject *object)\n" + "{\n" + "\t%s *self;\n\n" + "\tg_return_if_fail(object != NULL);\n" + "\tg_return_if_fail(IS_%s(object));\n\n" + "\tself = %s(object);\n\n" + "\tif (GTK_OBJECT_CLASS(parent_class)->destroy)\n" + "\t\t(* GTK_OBJECT_CLASS(parent_class)->destroy)(object);\n" + "}\n", + class_info->class_name_low, + class_info->class_name, + class_info->class_name_up, + class_info->class_name_up); + } + break; + } + } + + text = templates_get_template_class_source(class_info); + idx = document_new_file(class_info->source, NULL); + sci_set_text(doc_list[idx].sci, text); + g_free(text); + + text = templates_get_template_class_header(class_info); + idx = document_new_file(class_info->header, NULL); + sci_set_text(doc_list[idx].sci, text); + g_free(text); + + gtk_object_destroy(GTK_OBJECT(cc_dlg->dialog)); +} + + diff --git a/src/tools.h b/src/tools.h index 2a84b6e5..993b3a11 100644 --- a/src/tools.h +++ b/src/tools.h @@ -31,4 +31,6 @@ void tools_create_insert_custom_command_menu_items(); void tools_execute_custom_command(gint idx, const gchar *command); +void tools_show_dialog_create_class(gint type); + #endif