![]() |
![]() |
![]() |
Gwyddion Module Library Reference Manual | ![]() |
---|
Beyond the Minimal ModuleBeyond the Minimal Module — Common module idioms |
Often one wants to implement a group of closely related functions that could share quite a bit of code. There are several posibilities how to do it.
The naive approach would be to put the code of all the modules to
a one file and share what is shareable. But alas! there can be only one
GWY_MODULE_QUERY
per file, thus a one file can register only a one
module and this approach would not work.
The prefered solution is to register more than function for the module.
This is as simple as it sounds. One just has to call
gwy_process_func_register()
(or other feature registration method)
several times with different functions. It is even possible to register
functions of different kind in a one module, but usually there is no
reason to do this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
static gboolean module_register(void) { gwy_process_func_register("sobel_horizontal", (GwyProcessFunc)&gradient_sobel_horizontal, N_("/_Presentation/_Gradient/_Sobel (horizontal)"), NULL, GRADIENT_RUN_MODES, GWY_MENU_FLAG_DATA, N_("Horizontal Sobel gradient presentation")); gwy_process_func_register("sobel_vertical", (GwyProcessFunc)&gradient_sobel_vertical, N_("/_Presentation/_Gradient/_Sobel (vertical)"), NULL, GRADIENT_RUN_MODES, GWY_MENU_FLAG_DATA, N_("Vertical Sobel gradient presentation")); return TRUE; } |
1 2 3 4 5 6 7 8 |
GwyContainer *settings; settings = gwy_app_settings_get(); ratio = 1.61803; gwy_container_gis_double_by_name(settings, "/module/my_module/ratio", &ratio); ratio = CLAMP(ratio, 1.0, 2.0); |
1 2 3 4 5 6 |
GwyContainer *settings; settings = gwy_app_settings_get(); gwy_container_set_double_by_name(settings, "/module/my_module/ratio", ratio); |
1 2 3 4 5 6 7 |
GwyContainer *container; container = gwy_container_new(); /* Fill data with something meaningful */ ... gwy_app_data_browser_add(container); g_object_unref(container); |
1 2 |
gwy_container_set_object_by_name(container, "/12345/data", data_field); g_object_unref(data_field); |
1 2 3 |
new_id = gwy_app_data_browser_add_data_field(container, data_field); g_object_unref(data_field); gwy_app_set_data_field_title(container, new_id, _("Bogosity")); |
1 2 3 4 5 6 |
gwy_app_sync_data_items(container, container, old_id, new_id, FALSE, GWY_DATA_ITEM_GRADIENT, GWY_DATA_ITEM_MASK_COLOR, 0); |
1 |
gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD_ID, &old_id, 0); |
1 2 |
new_id = gwy_app_data_browser_add_graph_model(container, graph_model);
g_object_unref(graph_model); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
static gboolean slope_dialog(gdouble *amount) { GtkWidget *dialog, *table, *spin; GtkObject *adjust; gint response; /* Create the dialog */ dialog = gtk_dialog_new_with_buttons(_("Frobnicate"), NULL, 0, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL); gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE); gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK); /* Create the parameter controls */ table = gtk_table_new(1, 3, FALSE); gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), table, FALSE, FALSE, 4); adjust = gtk_adjustment_new(*amount, 0, 100, 1, 10, 0); spin = gwy_table_attach_hscale(table, 0, _("Amount:"), "percent", adjust, 0); /* Run the dialog and respond to user actions */ gtk_widget_show_all(dialog); response = gtk_dialog_run(GTK_DIALOG(dialog)); switch (response) { case GTK_RESPONSE_CANCEL: case GTK_RESPONSE_DELETE_EVENT: gtk_widget_destroy(dialog); case GTK_RESPONSE_NONE: break; case GTK_RESPONSE_OK: *amount = gtk_adjustment_get_value(GTK_ADJUSTMENT(adjust)); gtk_widget_destroy(dialog); break; } return response == GTK_RESPONSE_OK; } |