File: gwymodule-xyz.c

package info (click to toggle)
gwyddion 2.52-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 46,588 kB
  • sloc: ansic: 367,740; python: 7,788; sh: 5,245; makefile: 4,317; xml: 3,631; cpp: 2,550; pascal: 418; perl: 154; ruby: 130
file content (403 lines) | stat: -rw-r--r-- 11,936 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 *  $Id: gwymodule-xyz.c 20677 2017-12-18 18:22:52Z yeti-dn $
 *  Copyright (C) 2003-2004,2013,2016 David Necas (Yeti), Petr Klapetek.
 *  E-mail: yeti@gwyddion.net, klapetek@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-xyz.h>
#include "gwymoduleinternal.h"

/* The surface function information */
typedef struct {
    const gchar *name;
    const gchar *menu_path;
    const gchar *stock_id;
    const gchar *tooltip;
    GwyRunType run;
    guint sens_mask;
    GwyXYZFunc func;
} GwyXYZFuncInfo;

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

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

/**
 * gwy_xyz_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 XYZ Data 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.  XYZ surface 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_XYZ, possibly other
 *             requirements.
 * @tooltip: Tooltip for this function.
 *
 * Registers a surface 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.45
 **/
gboolean
gwy_xyz_func_register(const gchar *name,
                      GwyXYZFunc func,
                      const gchar *menu_path,
                      const gchar *stock_id,
                      GwyRunType run,
                      guint sens_mask,
                      const gchar *tooltip)
{
    GwyXYZFuncInfo *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 (!surface_funcs) {
        gwy_debug("Initializing...");
        surface_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(surface_funcs, name)) {
        g_warning("Duplicate function `%s', keeping only first", name);
        return FALSE;
    }

    func_info = g_new0(GwyXYZFuncInfo, 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(surface_funcs, (gpointer)func_info->name, func_info);
    if (!_gwy_module_add_registered_function(GWY_MODULE_PREFIX_XYZ, name)) {
        g_hash_table_remove(surface_funcs, func_info->name);
        return FALSE;
    }

    return TRUE;
}

/**
 * gwy_xyz_func_run:
 * @name: XYZ surface data processing function name.
 * @data: Data (a #GwyContainer).
 * @run: How the function should be run.
 *
 * Runs a surface data processing function identified by @name.
 *
 * Since: 2.45
 **/
void
gwy_xyz_func_run(const gchar *name,
                 GwyContainer *data,
                 GwyRunType run)
{
    GwyXYZFuncInfo *func_info;

    func_info = g_hash_table_lookup(surface_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_xyz_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_xyz_func_foreach:
 * @function: Function to run for each surface 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 surface function.
 *
 * Since: 2.45
 **/
void
gwy_xyz_func_foreach(GFunc function,
                     gpointer user_data)
{
    ProcFuncForeachData pffd;

    if (!surface_funcs)
        return;

    pffd.user_data = user_data;
    pffd.function = function;
    g_hash_table_foreach(surface_funcs, gwy_xyz_func_user_cb, &pffd);
}

/**
 * gwy_xyz_func_exists:
 * @name: XYZ surface data processing function name.
 *
 * Checks whether a surface data processing function exists.
 *
 * Returns: %TRUE if function @name exists, %FALSE otherwise.
 *
 * Since: 2.45
 **/
gboolean
gwy_xyz_func_exists(const gchar *name)
{
    return surface_funcs && g_hash_table_lookup(surface_funcs, name);
}

/**
 * gwy_xyz_func_get_run_types:
 * @name: XYZ surface data processing function name.
 *
 * Returns run modes supported by a surface data processing function.
 *
 * Returns: The run mode bit mask.
 *
 * Since: 2.45
 **/
GwyRunType
gwy_xyz_func_get_run_types(const gchar *name)
{
    GwyXYZFuncInfo *func_info;

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

    return func_info->run;
}

/**
 * gwy_xyz_func_get_menu_path:
 * @name: XYZ surface data processing function name.
 *
 * Returns the menu path of a surface data processing function.
 *
 * The returned menu path is only the tail part registered by the function,
 * i.e., without any leading "/XYZ Data".
 *
 * Returns: The menu path.  The returned string is owned by the module.
 *
 * Since: 2.45
 **/
const gchar*
gwy_xyz_func_get_menu_path(const gchar *name)
{
    GwyXYZFuncInfo *func_info;

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

    return func_info->menu_path;
}

/**
 * gwy_xyz_func_get_stock_id:
 * @name: XYZ surface data processing function name.
 *
 * Gets stock icon id of a surface data processing  function.
 *
 * Returns: The stock icon id.  The returned string is owned by the module.
 *
 * Since: 2.45
 **/
const gchar*
gwy_xyz_func_get_stock_id(const gchar *name)
{
    GwyXYZFuncInfo *func_info;

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

    return func_info->stock_id;
}

/**
 * gwy_xyz_func_get_tooltip:
 * @name: XYZ surface data processing function name.
 *
 * Gets tooltip for a surface data processing function.
 *
 * Returns: The tooltip.  The returned string is owned by the module.
 *
 * Since: 2.45
 **/
const gchar*
gwy_xyz_func_get_tooltip(const gchar *name)
{
    GwyXYZFuncInfo *func_info;

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

    return func_info->tooltip;
}

/**
 * gwy_xyz_func_get_sensitivity_mask:
 * @name: XYZ surface data processing function name.
 *
 * Gets menu sensititivy mask for a surface data processing function.
 *
 * Returns: The menu item sensitivity mask (a combination of #GwyMenuSensFlags
 *          flags).
 *
 * Since: 2.45
 **/
guint
gwy_xyz_func_get_sensitivity_mask(const gchar *name)
{
    GwyXYZFuncInfo *func_info;

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

    return func_info->sens_mask;
}

/**
 * gwy_xyz_func_current:
 *
 * Obtains the name of currently running surface data processing function.
 *
 * If no surface data 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 surface data processing function or
 *          %NULL.
 *
 * Since: 2.38
 **/
const gchar*
gwy_xyz_func_current(void)
{
    GwyXYZFuncInfo *func_info;

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

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

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

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

/**
 * SECTION:gwymodule-surface
 * @title: gwymodule-surface
 * @short_description: XYZ surface data processing modules
 *
 * XYZ surface data processing modules implement function processing surface
 * data represented with #GwySurface.  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.
 *
 * XYZ surface data processing functions were introduced in version 2.32.
 **/

/**
 * GwyXYZFuncInfo:
 * @name: An unique surface data processing function name.
 * @menu_path: A path under "/XYZ Data" where the function should appear.
 *             It must start with "/".
 * @surface: The function itself.
 * @run: Possible run-modes for this function.
 * @sens_flags: Sensitivity flags.  XYZ surface data processing function should
 *              include, in general, %GWY_MENU_FLAG_XYZ.  Functions
 *              constructing synthetic data from nothing do not have to specify
 *              even %GWY_MENU_FLAG_XYZ.
 *
 * Information about one surface data processing function.
 *
 * Since: 2.45
 **/

/**
 * GwyXYZFunc:
 * @data: The data container to operate on.
 * @run: Run mode.
 * @name: Function name from as registered with gwy_xyz_func_register()
 *        (single-function modules can safely ignore this argument).
 *
 * The type of surface data processing function.
 *
 * Since: 2.45
 **/

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