/* OpenCOBOL GTK+ 2.0 wrapper */ /* Tectonics: cobc -c `pkg-config --cflags gtk+-2.0` ocgtk.c */ #include #include #include #include #include #include "ocgtk.h" /* Initialize the toolkit, abends if not possible */ void CBL_OC_GTK_INIT(int argc, char *argv[]) { gtk_init(&argc, &argv); } /* Initialize the toolkit, return false if not possible */ /* Need pointers to argc and argv here */ int CBL_OC_GTK_INIT_CHECK() { gboolean gres = gtk_init_check(0, NULL); return (gres == TRUE) ? 0 : -1; } /* Create new window */ GtkWidget* CBL_OC_GTK_WINDOW_NEW() { return gtk_window_new(GTK_WINDOW_TOPLEVEL); } /* set the title */ void CBL_OC_GTK_WINDOW_SET_TITLE(void *window, char *title) { struct cob_module *module; cob_field *title_field; char *cstr; /* Error conditions simply return, doing nothing */ if (cob_get_global_ptr()->cob_call_params < 2) { return; } module = cob_get_global_ptr()->cob_current_module; if (module == NULL) { //cob_runtime_error("No module!"); cob_stop_run(1); } title_field = module->cob_procedure_parameters[1]; if (!title_field) { return; } cstr = (char *)malloc(title_field->size + 1); if (!cstr) { return; } memcpy(cstr, title_field->data, title_field->size); cstr[title_field->size] = '\0'; gtk_window_set_title(GTK_WINDOW(window), cstr); free(cstr); } /* Widget sizing */ void CBL_OC_GTK_WIDGET_SET_SIZE_REQUEST(void *widget, int x, int y) { gtk_widget_set_size_request(GTK_WIDGET(widget), x, y); } /* Set border width */ void CBL_OC_GTK_CONTAINER_SET_BORDER_WIDTH(void *window, int pixels) { gtk_container_set_border_width(GTK_CONTAINER(window), pixels); } /* New vertical box */ GtkWidget* CBL_OC_GTK_VBOX_NEW(int homogeneous, int spacing) { return gtk_vbox_new((gboolean)homogeneous, (gint)spacing); } /* New horizontal box */ GtkWidget* CBL_OC_GTK_HBOX_NEW(int homogeneous, int spacing) { return gtk_hbox_new((gboolean)homogeneous, (gint)spacing); } /* packing boxes */ void CBL_OC_GTK_BOX_PACK_START(void *gcont, void *gobj, int expand, int fill, int padding) { gtk_box_pack_start(GTK_BOX(gcont), gobj, (gboolean)expand, (gboolean)fill, (guint)padding); } /* menus */ GtkWidget* CBL_OC_GTK_MENU_BAR_NEW() { return gtk_menu_bar_new(); } GtkWidget* CBL_OC_GTK_MENU_NEW() { return gtk_menu_new(); } GtkWidget* CBL_OC_GTK_MENU_ITEM_NEW_WITH_LABEL(char *label) { struct cob_module *module; cob_field *title_field; char *cstr; GtkWidget *item; /* Error conditions simply return, doing nothing */ if (cob_get_global_ptr()->cob_call_params < 1) { return; } module = cob_get_global_ptr()->cob_current_module; if (module == NULL) { //cob_runtime_error("No module!"); cob_stop_run(1); } title_field = module->cob_procedure_parameters[0]; if (!title_field) { return; } cstr = (char *)malloc(title_field->size + 1); if (!cstr) { return; } memcpy(cstr, title_field->data, title_field->size); cstr[title_field->size] = '\0'; item = gtk_menu_item_new_with_label(cstr); gtk_widget_set_tooltip_text(item, (gchar *)cstr); free(cstr); return item; } void CBL_OC_GTK_MENU_ITEM_SET_SUBMENU(void *item, void *menu) { gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu); return; } void CBL_OC_GTK_MENU_SHELL_APPEND(void *menu, void *item) { gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); return; } /* New button */ GtkWidget* CBL_OC_GTK_BUTTON_NEW_WITH_LABEL(char *label) { GtkWidget *button; button = gtk_button_new_with_label(label); if (button) { gtk_widget_set_tooltip_text(button, (gchar *)label); } return button; } /* New text entry */ GtkWidget* CBL_OC_GTK_ENTRY_NEW() { return gtk_entry_new(); } /* Set text in entry */ void CBL_OC_GTK_ENTRY_SET_TEXT(void *entry, char *text) { gtk_entry_set_text(GTK_ENTRY(entry), text); return; } /* Get the text in an entry */ int CBL_OC_GTK_ENTRY_GET_TEXT(void *entry, char *text) { struct cob_module *module; cob_field *text_field; size_t text_length; module = cob_get_global_ptr()->cob_current_module; text_field = module->cob_procedure_parameters[1]; const gchar *entry_text; entry_text = gtk_entry_get_text(GTK_ENTRY(entry)); text_length = entry_text ? strlen(entry_text) : 0; text_length = (text_length > text_field->size) ? text_field->size : text_length; memset(text_field->data, ' ', text_field->size); memcpy(text_field->data, entry_text, text_length); return (int)text_length; } /* connect event to callback */ void CBL_OC_G_SIGNAL_CONNECT(void *gobj, char *sgn, void (cb)(void *, void *), void *parm) { g_signal_connect(G_OBJECT(gobj), sgn, G_CALLBACK(cb), parm); } /* add object to container */ void CBL_OC_GTK_CONTAINER_ADD(void *window, void *gobj) { gtk_container_add(GTK_CONTAINER(window), gobj); } /* tell gtk that object is now ready */ void CBL_OC_GTK_WIDGET_SHOW(void *gobj) { gtk_widget_show(gobj); } /* tell gtk to ready all the wdigets */ void CBL_OC_GTK_WIDGET_SHOW_ALL(void *window) { gtk_widget_show_all(window); } /* Some dialogs */ GtkWidget* CBL_OC_GTK_FILE_SELECTION_NEW(char *title) { return gtk_file_selection_new(title); } /* the event loop */ void CBL_OC_GTK_MAIN() { gtk_main(); } /* stop the gui */ void CBL_OC_GTK_MAIN_QUIT() { gtk_main_quit(); }