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
|
/*
* $Id: gwymodule-graph.c 20678 2017-12-18 18:26:55Z yeti-dn $
* Copyright (C) 2003,2004 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 <libgwydgets/gwygraph.h>
#include <libgwymodule/gwymodule-graph.h>
#include "gwymoduleinternal.h"
/* The graph function information */
typedef struct {
const gchar *name;
const gchar *menu_path;
const gchar *stock_id;
const gchar *tooltip;
guint sens_mask;
GwyGraphFunc func;
} GwyGraphFuncInfo;
/* Auxiliary structure to pass both user callback function and data to
* g_hash_table_foreach() lambda argument in gwy_graph_func_foreach() */
typedef struct {
GFunc function;
gpointer user_data;
} FuncForeachData;
static GHashTable *graph_funcs = NULL;
static GPtrArray *call_stack = NULL;
/**
* gwy_graph_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 Graph 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.
* @sens_mask: Sensitivity mask (a combination of #GwyMenuSensFlags flags).
* Usually it is equal to #GWY_MENU_FLAG_GRAPH, but it's
* possible to set other requirements.
* @tooltip: Tooltip for this function.
*
* Registers a graph 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 chance to clean-up.
*
* Returns: Normally %TRUE; %FALSE on failure.
**/
gboolean
gwy_graph_func_register(const gchar *name,
GwyGraphFunc func,
const gchar *menu_path,
const gchar *stock_id,
guint sens_mask,
const gchar *tooltip)
{
GwyGraphFuncInfo *func_info;
g_return_val_if_fail(name, FALSE);
g_return_val_if_fail(func, FALSE);
g_return_val_if_fail(menu_path, FALSE);
gwy_debug("name = %s, menu path = %s, func = %p", name, menu_path, func);
if (!graph_funcs) {
gwy_debug("initializing...");
graph_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(graph_funcs, name)) {
g_warning("Duplicate function %s, keeping only first", name);
return FALSE;
}
func_info = g_new0(GwyGraphFuncInfo, 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->sens_mask = sens_mask;
g_hash_table_insert(graph_funcs, (gpointer)func_info->name, func_info);
if (!_gwy_module_add_registered_function(GWY_MODULE_PREFIX_GRAPH, name)) {
g_hash_table_remove(graph_funcs, func_info->name);
return FALSE;
}
return TRUE;
}
/**
* gwy_graph_func_run:
* @name: Graph function name.
* @graph: Graph (a #GwyGraph).
*
* Runs a graph function identified by @name.
**/
void
gwy_graph_func_run(const gchar *name,
GwyGraph *graph)
{
GwyGraphFuncInfo *func_info;
func_info = g_hash_table_lookup(graph_funcs, name);
g_return_if_fail(func_info);
g_return_if_fail(GWY_IS_GRAPH(graph));
g_ptr_array_add(call_stack, func_info);
func_info->func(graph, name);
g_return_if_fail(call_stack->len);
g_ptr_array_set_size(call_stack, call_stack->len-1);
}
static void
gwy_graph_func_user_cb(gpointer key,
G_GNUC_UNUSED gpointer value,
gpointer user_data)
{
FuncForeachData *ffd = (FuncForeachData*)user_data;
ffd->function(key, ffd->user_data);
}
/**
* gwy_graph_func_foreach:
* @function: Function to run for each graph 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 graph function.
**/
void
gwy_graph_func_foreach(GFunc function,
gpointer user_data)
{
FuncForeachData ffd;
if (!graph_funcs)
return;
ffd.user_data = user_data;
ffd.function = function;
g_hash_table_foreach(graph_funcs, gwy_graph_func_user_cb, &ffd);
}
/**
* gwy_graph_func_exists:
* @name: Graph function name.
*
* Checks whether a graph function exists.
*
* Returns: %TRUE if function @name exists, %FALSE otherwise.
**/
gboolean
gwy_graph_func_exists(const gchar *name)
{
return graph_funcs && g_hash_table_lookup(graph_funcs, name);
}
/**
* gwy_graph_func_get_menu_path:
* @name: Graph function name.
*
* Gets menu path of a graph function.
*
* The returned menu path is only the tail part registered by the function,
* i.e., without any leading "/Graph".
*
* Returns: The menu path. The returned string is owned by the module.
**/
const gchar*
gwy_graph_func_get_menu_path(const gchar *name)
{
GwyGraphFuncInfo *func_info;
g_return_val_if_fail(graph_funcs, NULL);
func_info = g_hash_table_lookup(graph_funcs, name);
g_return_val_if_fail(func_info, NULL);
return func_info->menu_path;
}
/**
* gwy_graph_func_get_stock_id:
* @name: Graph function name.
*
* Gets stock icon id of a graph function.
*
* Returns: The stock icon id. The returned string is owned by the module.
**/
const gchar*
gwy_graph_func_get_stock_id(const gchar *name)
{
GwyGraphFuncInfo *func_info;
g_return_val_if_fail(graph_funcs, NULL);
func_info = g_hash_table_lookup(graph_funcs, name);
g_return_val_if_fail(func_info, NULL);
return func_info->stock_id;
}
/**
* gwy_graph_func_get_tooltip:
* @name: Graph function name.
*
* Gets tooltip for a graph function.
*
* Returns: The tooltip. The returned string is owned by the module.
**/
const gchar*
gwy_graph_func_get_tooltip(const gchar *name)
{
GwyGraphFuncInfo *func_info;
g_return_val_if_fail(graph_funcs, NULL);
func_info = g_hash_table_lookup(graph_funcs, name);
g_return_val_if_fail(func_info, NULL);
return func_info->tooltip;
}
/**
* gwy_graph_func_get_sensitivity_mask:
* @name: Graph function name.
*
* Gets menu sensititivy mask for a graph function.
*
* Returns: The menu item sensitivity mask (a combination of #GwyMenuSensFlags
* flags).
**/
guint
gwy_graph_func_get_sensitivity_mask(const gchar *name)
{
GwyGraphFuncInfo *func_info;
func_info = g_hash_table_lookup(graph_funcs, name);
g_return_val_if_fail(func_info, 0);
return func_info->sens_mask;
}
/**
* gwy_graph_func_current:
*
* Obtains the name of currently running graph function.
*
* If no graph 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 graph function or %NULL.
*
* Since: 2.38
**/
const gchar*
gwy_graph_func_current(void)
{
GwyGraphFuncInfo *func_info;
if (!call_stack || !call_stack->len)
return NULL;
func_info = (GwyGraphFuncInfo*)g_ptr_array_index(call_stack,
call_stack->len-1);
return func_info->name;
}
gboolean
_gwy_graph_func_remove(const gchar *name)
{
gwy_debug("%s", name);
if (!g_hash_table_remove(graph_funcs, name)) {
g_warning("Cannot remove function %s", name);
return FALSE;
}
return TRUE;
}
/************************** Documentation ****************************/
/**
* SECTION:gwymodule-graph
* @title: gwymodule-graph
* @short_description: Graph modules
*
* Graph modules implement operations on graphs, e.g., curve fitting.
**/
/**
* GwyGraphFunc:
* @graph: Graph (a #GwyGraph) to operate on.
* @name: Function name from as registered with gwy_graph_func_register()
* (single-function modules can safely ignore this argument).
*
* The type of graph function.
**/
/* 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 : */
|