File: gwymodule-cmap.c

package info (click to toggle)
gwyddion 2.62-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 51,952 kB
  • sloc: ansic: 398,486; python: 7,877; sh: 5,492; makefile: 4,723; xml: 3,883; cpp: 1,969; pascal: 418; perl: 154; ruby: 130
file content (382 lines) | stat: -rw-r--r-- 11,818 bytes parent folder | download | duplicates (2)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 *  $Id: gwymodule-cmap.c 23987 2021-08-16 13:25:48Z yeti-dn $
 *  Copyright (C) 2021 David Necas (Yeti).
 *  E-mail: yeti@gwyddion.net.
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
 *  License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any
 *  later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 *  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 *  details.
 *
 *  You should have received a copy of the GNU General Public License along with this program; if not, write to the
 *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include <string.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwyutils.h>
#include <libgwyddion/gwycontainer.h>
#include <libgwymodule/gwymodule-cmap.h>
#include "gwymoduleinternal.h"

/* The curve map function information */
typedef struct {
    const gchar *name;
    const gchar *menu_path;
    const gchar *stock_id;
    const gchar *tooltip;
    GwyRunType run;
    guint sens_mask;
    GwyCurveMapFunc func;
} GwyCurveMapFuncInfo;

/* Auxiliary structure to pass both user callback function and data to g_hash_table_foreach() lambda argument in
 * gwy_curve_map_func_foreach() */
typedef struct {
    GFunc function;
    gpointer user_data;
} ProcFuncForeachData;

static GHashTable *cmap_funcs = NULL;
static GPtrArray *call_stack = NULL;

/**
 * gwy_curve_map_func_register:
 * @name: Name of function to register.  It should be a valid identifier and if a module registers only one function,
 *        module and function names should be the same.
 * @func: The function itself.
 * @menu_path: Menu path under Curve Map menu.  The menu path should be marked translatabe, but passed untranslated
 *             (to allow merging of translated and untranslated submenus).
 * @stock_id: Stock icon id for toolbar.
 * @run: Supported run modes.  Curve map data processing functions can have two run modes: %GWY_RUN_IMMEDIATE (no
 *       questions asked) and %GWY_RUN_INTERACTIVE (a modal dialog with parameters).
 * @sens_mask: Sensitivity mask (a combination of #GwyMenuSensFlags flags). Usually it contains
 *             #GWY_MENU_FLAG_CURVE_MAP, possibly other requirements.
 * @tooltip: Tooltip for this function.
 *
 * Registers a curve map data processing function.
 *
 * Note: the string arguments are not copied as modules are not expected to vanish.  If they are constructed
 * (non-constant) strings, do not free them. Should modules ever become unloadable they will get a chance to clean-up.
 *
 * Returns: Normally %TRUE; %FALSE on failure.
 *
 * Since: 2.60
 **/
gboolean
gwy_curve_map_func_register(const gchar *name,
                            GwyCurveMapFunc func,
                            const gchar *menu_path,
                            const gchar *stock_id,
                            GwyRunType run,
                            guint sens_mask,
                            const gchar *tooltip)
{
    GwyCurveMapFuncInfo *func_info;

    g_return_val_if_fail(name, FALSE);
    g_return_val_if_fail(func, FALSE);
    g_return_val_if_fail(menu_path, FALSE);
    g_return_val_if_fail(run & GWY_RUN_MASK, FALSE);
    gwy_debug("name = %s, menu path = %s, run = %d, func = %p", name, menu_path, run, func);

    if (!cmap_funcs) {
        gwy_debug("Initializing...");
        cmap_funcs = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
        call_stack = g_ptr_array_new();
    }

    if (!gwy_strisident(name, "_-", NULL))
        g_warning("Function name `%s' is not a valid identifier. It may be rejected in future.", name);
    if (g_hash_table_lookup(cmap_funcs, name)) {
        g_warning("Duplicate function `%s', keeping only first", name);
        return FALSE;
    }

    func_info = g_new0(GwyCurveMapFuncInfo, 1);
    func_info->name = name;
    func_info->func = func;
    func_info->menu_path = menu_path;
    func_info->stock_id = stock_id;
    func_info->tooltip = tooltip;
    func_info->run = run;
    func_info->sens_mask = sens_mask;

    g_hash_table_insert(cmap_funcs, (gpointer)func_info->name, func_info);
    if (!_gwy_module_add_registered_function(GWY_MODULE_PREFIX_CMAP, name)) {
        g_hash_table_remove(cmap_funcs, func_info->name);
        return FALSE;
    }

    return TRUE;
}

/**
 * gwy_curve_map_func_run:
 * @name: Curve map data processing function name.
 * @data: Data (a #GwyContainer).
 * @run: How the function should be run.
 *
 * Runs a curve map processing function identified by @name.
 *
 * Since: 2.60
 **/
void
gwy_curve_map_func_run(const gchar *name,
                       GwyContainer *data,
                       GwyRunType run)
{
    GwyCurveMapFuncInfo *func_info;

    func_info = g_hash_table_lookup(cmap_funcs, name);
    g_return_if_fail(func_info);
    g_return_if_fail(run & func_info->run);
    g_ptr_array_add(call_stack, func_info);
    func_info->func(data, run, name);
    g_return_if_fail(call_stack->len);
    g_ptr_array_set_size(call_stack, call_stack->len-1);
}

static void
gwy_curve_map_func_user_cb(gpointer key,
                           G_GNUC_UNUSED gpointer value,
                           gpointer user_data)
{
    ProcFuncForeachData *pffd = (ProcFuncForeachData*)user_data;

    pffd->function(key, pffd->user_data);
}

/**
 * gwy_curve_map_func_foreach:
 * @function: Function to run for each volume function.  It will get function name (constant string owned by module
 *            system) as its first argument, @user_data as the second argument.
 * @user_data: Data to pass to @function.
 *
 * Calls a function for each volume function.
 *
 * Since: 2.60
 **/
void
gwy_curve_map_func_foreach(GFunc function,
                           gpointer user_data)
{
    ProcFuncForeachData pffd;

    if (!cmap_funcs)
        return;

    pffd.user_data = user_data;
    pffd.function = function;
    g_hash_table_foreach(cmap_funcs, gwy_curve_map_func_user_cb, &pffd);
}

/**
 * gwy_curve_map_func_exists:
 * @name: Curve map data processing function name.
 *
 * Checks whether a curve map processing function exists.
 *
 * Returns: %TRUE if function @name exists, %FALSE otherwise.
 *
 * Since: 2.60
 **/
gboolean
gwy_curve_map_func_exists(const gchar *name)
{
    return cmap_funcs && g_hash_table_lookup(cmap_funcs, name);
}

/**
 * gwy_curve_map_func_get_run_types:
 * @name: Curve map data processing function name.
 *
 * Returns run modes supported by a curve map processing function.
 *
 * Returns: The run mode bit mask.
 *
 * Since: 2.60
 **/
GwyRunType
gwy_curve_map_func_get_run_types(const gchar *name)
{
    GwyCurveMapFuncInfo *func_info;

    func_info = g_hash_table_lookup(cmap_funcs, name);
    g_return_val_if_fail(func_info, 0);

    return func_info->run;
}

/**
 * gwy_curve_map_func_get_menu_path:
 * @name: Curve map data processing function name.
 *
 * Returns the menu path of a curve map processing function.
 *
 * The returned menu path is only the tail part registered by the function, i.e., without any leading "/Curve Map".
 *
 * Returns: The menu path.  The returned string is owned by the module.
 *
 * Since: 2.60
 **/
const gchar*
gwy_curve_map_func_get_menu_path(const gchar *name)
{
    GwyCurveMapFuncInfo *func_info;

    func_info = g_hash_table_lookup(cmap_funcs, name);
    g_return_val_if_fail(func_info, 0);

    return func_info->menu_path;
}

/**
 * gwy_curve_map_func_get_stock_id:
 * @name: Curve map data processing function name.
 *
 * Gets stock icon id of a curve map processing  function.
 *
 * Returns: The stock icon id.  The returned string is owned by the module.
 *
 * Since: 2.60
 **/
const gchar*
gwy_curve_map_func_get_stock_id(const gchar *name)
{
    GwyCurveMapFuncInfo *func_info;

    g_return_val_if_fail(cmap_funcs, NULL);
    func_info = g_hash_table_lookup(cmap_funcs, name);
    g_return_val_if_fail(func_info, NULL);

    return func_info->stock_id;
}

/**
 * gwy_curve_map_func_get_tooltip:
 * @name: Curve map data processing function name.
 *
 * Gets tooltip for a curve map processing function.
 *
 * Returns: The tooltip.  The returned string is owned by the module.
 *
 * Since: 2.60
 **/
const gchar*
gwy_curve_map_func_get_tooltip(const gchar *name)
{
    GwyCurveMapFuncInfo *func_info;

    g_return_val_if_fail(cmap_funcs, NULL);
    func_info = g_hash_table_lookup(cmap_funcs, name);
    g_return_val_if_fail(func_info, NULL);

    return func_info->tooltip;
}

/**
 * gwy_curve_map_func_get_sensitivity_mask:
 * @name: Curve map data processing function name.
 *
 * Gets menu sensititivy mask for a curve map processing function.
 *
 * Returns: The menu item sensitivity mask (a combination of #GwyMenuSensFlags flags).
 *
 * Since: 2.60
 **/
guint
gwy_curve_map_func_get_sensitivity_mask(const gchar *name)
{
    GwyCurveMapFuncInfo *func_info;

    func_info = g_hash_table_lookup(cmap_funcs, name);
    g_return_val_if_fail(func_info, 0);

    return func_info->sens_mask;
}

/**
 * gwy_curve_map_func_current:
 *
 * Obtains the name of currently running curve map processing function.
 *
 * If no curve map processing function is currently running, %NULL is returned.
 *
 * If multiple nested functions are running (which is not usual but technically possible), the innermost function name
 * is returned.
 *
 * Returns: The name of currently running curve map processing function or %NULL.
 *
 * Since: 2.60
 **/
const gchar*
gwy_curve_map_func_current(void)
{
    GwyCurveMapFuncInfo *func_info;

    if (!call_stack || !call_stack->len)
        return NULL;

    func_info = (GwyCurveMapFuncInfo*)g_ptr_array_index(call_stack, call_stack->len-1);
    return func_info->name;
}

gboolean
_gwy_cmap_func_remove(const gchar *name)
{
    gwy_debug("%s", name);
    if (!g_hash_table_remove(cmap_funcs, name)) {
        g_warning("Cannot remove function %s", name);
        return FALSE;
    }
    return TRUE;
}

/************************** Documentation ****************************/

/**
 * SECTION:gwymodule-cmap
 * @title: gwymodule-cmap
 * @short_description: Curve map data processing modules
 *
 * Curve map data processing modules implement function processing curve map data represented with #GwyLawn.  They
 * reigster functions that get a #GwyContainer with data and either modify it or create a new data from it. In this
 * regard, they are quite similar to regular (two-dimensional) data processing functions but they live in separate
 * menus, toolbars, etc.
 *
 * Curve map data processing functions were introduced in version 2.60.
 **/

/**
 * GwyCurveMapFuncInfo:
 * @name: An unique curve map data processing function name.
 * @menu_path: A path under "/Curve Map" where the function should appear. It must start with "/".
 * @cmap: The function itself.
 * @run: Possible run-modes for this function.
 * @sens_flags: Sensitivity flags.  Curve map data processing function should include, in general,
 *              %GWY_MENU_FLAG_CURVE_MAP.  Functions constructing synthetic data from nothing do not have to specify
 *              even %GWY_MENU_FLAG_CURVE_MAP.
 *
 * Information about one curve map data processing function.
 *
 * Since: 2.60
 **/

/**
 * GwyCurveMapFunc:
 * @data: The data container to operate on.
 * @run: Run mode.
 * @name: Function name from as registered with gwy_curve_map_func_register() (single-function modules can safely
 *        ignore this argument).
 *
 * The type of curve map data processing function.
 *
 * Since: 2.60
 **/

/* vim: set cin columns=120 tw=118 et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */