Drop unused context argument.
[geda-gaf/berndj.git] / gschem / src / g_funcs.c
blobc207499d8b69abfb747ad7132758ba84a4ed608a
1 /* gEDA - GPL Electronic Design Automation
2 * gschem - gEDA Schematic Capture
3 * Copyright (C) 1998-2007 Ales Hvezda
4 * Copyright (C) 1998-2007 gEDA Contributors (see ChangeLog for details)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
20 #include <config.h>
22 #include <stdio.h>
23 #include <sys/stat.h>
24 #include <ctype.h>
25 #ifdef HAVE_STRING_H
26 #include <string.h>
27 #endif
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
35 #include "gschem.h"
37 #ifdef HAVE_LIBDMALLOC
38 #include <dmalloc.h>
39 #endif
41 /*! \todo Finish function documentation!!!
42 * \brief
43 * \par Function Description
46 SCM g_funcs_print(SCM filename)
48 SCM_ASSERT (scm_is_string (filename), filename,
49 SCM_ARG1, "gschem-print");
51 if (output_filename) {
52 if (f_print_file (global_window_current->toplevel, output_filename))
53 return SCM_BOOL_F;
54 } else {
55 char *filename_chars;
57 scm_dynwind_begin(0);
59 filename_chars = scm_to_locale_string(filename);
60 scm_dynwind_free(filename_chars);
62 if (f_print_file(global_window_current->toplevel, filename_chars)) {
63 scm_dynwind_end();
64 return SCM_BOOL_F;
66 scm_dynwind_end();
69 return SCM_BOOL_T;
72 /*! \todo Finish function documentation!!!
73 * \brief
74 * \par Function Description
77 SCM g_funcs_postscript(SCM filename)
79 SCM_ASSERT (scm_is_string (filename), filename,
80 SCM_ARG1, "gschem-postscript");
82 if (output_filename) {
83 if (f_print_file (global_window_current->toplevel, output_filename))
84 return SCM_BOOL_F;
85 } else {
86 char *filename_chars;
88 scm_dynwind_begin(0);
90 filename_chars = scm_to_locale_string(filename);
91 scm_dynwind_free(filename_chars);
93 if (f_print_file(global_window_current->toplevel, filename_chars)) {
94 scm_dynwind_end();
95 return SCM_BOOL_F;
97 scm_dynwind_end();
100 return SCM_BOOL_T;
103 /*! \todo Finish function documentation!!!
104 * \brief
105 * \par Function Description
108 SCM g_funcs_image(SCM filename)
110 SCM_ASSERT (scm_is_string (filename), filename,
111 SCM_ARG1, "gschem-image");
113 if (output_filename) {
114 x_image_lowlevel (global_window_current, output_filename,
115 global_window_current->image_width,
116 global_window_current->image_height,
117 g_strdup("png"));
118 } else {
119 char *filename_chars;
121 scm_dynwind_begin(0);
122 filename_chars = scm_to_locale_string(filename);
123 scm_dynwind_free(filename_chars);
125 x_image_lowlevel(global_window_current, filename_chars,
126 global_window_current->image_width,
127 global_window_current->image_height,
128 g_strdup("png"));
130 scm_dynwind_end();
133 return SCM_BOOL_T;
136 /*! \todo Finish function documentation!!!
137 * \brief
138 * \par Function Description
141 SCM g_funcs_exit(void)
143 exit(0);
146 /*! \todo Finish function documentation!!!
147 * \brief
148 * \par Function Description
151 SCM g_funcs_log(SCM msg)
153 char *msg_chars;
155 SCM_ASSERT (scm_is_string (msg), msg,
156 SCM_ARG1, "gschem-log");
158 scm_dynwind_begin(0);
159 msg_chars = scm_to_locale_string(msg);
160 scm_dynwind_free(msg_chars);
162 s_log_message("%s", msg_chars);
164 scm_dynwind_end();
165 return SCM_BOOL_T;
168 /*! \todo Finish function documentation!!!
169 * \brief
170 * \par Function Description
173 SCM g_funcs_msg(SCM msg)
175 char *msg_chars;
177 SCM_ASSERT (scm_is_string (msg), msg,
178 SCM_ARG1, "gschem-msg");
180 scm_dynwind_begin(0);
181 msg_chars = scm_to_locale_string(msg);
182 scm_dynwind_free(msg_chars);
184 generic_msg_dialog(msg_chars);
186 scm_dynwind_end();
187 return SCM_BOOL_T;
190 /*! \todo Finish function documentation!!!
191 * \brief
192 * \par Function Description
195 SCM g_funcs_confirm(SCM msg)
197 char *msg_chars;
198 int r;
200 SCM_ASSERT (scm_is_string (msg), msg,
201 SCM_ARG1, "gschem-msg");
203 scm_dynwind_begin(0);
204 msg_chars = scm_to_locale_string(msg);
205 scm_dynwind_free(msg_chars);
207 r = generic_confirm_dialog(msg_chars);
209 scm_dynwind_end();
211 if (r)
212 return SCM_BOOL_T;
213 else
214 return SCM_BOOL_F;
217 /*! \todo Finish function documentation!!!
218 * \brief
219 * \par Function Description
222 SCM g_funcs_filesel(SCM msg, SCM templ, SCM flags)
224 char *msg_chars, *templ_chars;
225 int c_flags;
226 char * r;
227 SCM v;
229 SCM_ASSERT (scm_is_string (msg), msg,
230 SCM_ARG1, "gschem-filesel");
232 SCM_ASSERT (scm_is_string (templ), templ,
233 SCM_ARG2, "gschem-filesel");
235 /*! \bug FIXME -- figure out the magic SCM_ASSERT for the flags */
237 /*! \bug FIXME -- how to deal with conflicting flags?
238 * Should I throw a scheme error? Just deal in the c code?
240 for (c_flags = 0; scm_pair_p (flags) == SCM_BOOL_T; flags = SCM_CDR (flags)) {
241 SCM f = SCM_CAR (flags);
242 char *f_chars;
244 scm_dynwind_begin(0);
245 f_chars = scm_to_locale_string(f);
246 scm_dynwind_free(f_chars);
248 if (strcmp(f_chars, "may_exist") == 0) {
249 c_flags |= FSB_MAY_EXIST;
250 } else if (strcmp(f_chars, "must_exist") == 0) {
251 c_flags |= FSB_MUST_EXIST;
252 } else if (strcmp(f_chars, "must_not_exist") == 0) {
253 c_flags |= FSB_SHOULD_NOT_EXIST;
254 } else if (strcmp(f_chars, "save") == 0) {
255 c_flags |= FSB_SAVE;
256 } else if (strcmp(f_chars, "open") == 0) {
257 c_flags |= FSB_LOAD;
258 } else {
259 scm_wrong_type_arg ("gschem-filesel", 1, f);
262 scm_dynwind_end();
265 scm_dynwind_begin(0);
266 msg_chars = scm_to_locale_string(msg);
267 templ_chars = scm_to_locale_string(templ);
268 scm_dynwind_free(msg_chars);
269 scm_dynwind_free(templ_chars);
271 r = generic_filesel_dialog(msg_chars, templ_chars, c_flags);
273 scm_dynwind_end();
275 v = r ? scm_from_locale_string(r) : SCM_BOOL_F;
276 g_free (r);
278 return v;
281 /*! \todo Finish function documentation!!!
282 * \brief
283 * \par Function Description
286 SCM g_funcs_use_rc_values(void)
288 i_vars_freestrings(global_window_current);
289 i_vars_set(global_window_current);
290 return SCM_BOOL_T;
293 /*! \todo Finish function documentation!!!
294 * \brief
295 * \par Function Description
299 * Gets names from all objects of current page which selected-flags are true.
301 /* all of the declaration part is copied from some other c-code of
302 * gEDA gschem.
303 * I don't really know, whether this all are necessary or not, but
304 * it works :-). */
305 static void
306 hash_table_2_list (gpointer key,
307 gpointer value,
308 gpointer user_data)
310 SCM *plist = (SCM*)user_data;
311 SCM value_scm;
313 if (value) {
314 value_scm = scm_from_locale_string((char const *) value);
315 } else {
316 value_scm = SCM_BOOL_F;
318 *plist = scm_cons(value_scm, *plist);
321 /*! \todo Finish function documentation!!!
322 * \brief
323 * \par Function Description
326 SCM get_selected_component_attributes(GSCHEM_TOPLEVEL *w_current)
328 SCM list = SCM_EOL;
329 OBJECT *obj;
330 GHashTable *ht;
332 /* build a hash table */
333 ht = g_hash_table_new (g_str_hash, g_str_equal);
334 for (obj = w_current->toplevel->page_current->object_head; obj != NULL;
335 obj = obj->next) {
336 if (obj->selected && obj->type == OBJ_TEXT) {
337 const gchar *str = o_text_get_string(obj);
338 if (str == NULL) continue;
339 /* add text string in the hash table */
340 g_hash_table_insert (ht, (gchar *) str, (gchar *) str);
343 /* now create a scheme list of the entries in the hash table */
344 g_hash_table_foreach (ht, hash_table_2_list, &list);
345 /* and get ride of the hast table */
346 g_hash_table_destroy (ht);
348 return list;
351 /*! \todo Finish function documentation!!!
352 * \brief Get selected filename of current schematic.
353 * \par Function Description
354 * This function gets the whole filename of the current schematic.
355 * Specifically, the <B>page_filename</B> of the current page.
357 * \param [in] w_current The GSCHEM_TOPLEVEL object to get filename from.
358 * \return whole filename of current schematic.
360 SCM get_selected_filename(GSCHEM_TOPLEVEL *w_current)
362 SCM return_value;
364 exit_if_null(w_current);
366 return_value = scm_from_locale_string(w_current->toplevel->page_current->page_filename);
368 return(return_value);
371 /*! \brief Use gschemdoc to open a browser to a specific wiki page
373 * \param [in] wikiname the name of the wiki page
375 * \par Function Description
376 * Invokes gschemdoc with its -w switch to open a browser to the wiki
377 * page specified by wikiname. If wikiname is empty or not a string,
378 * will browse to the main wiki page.
380 SCM g_funcs_browse_wiki(SCM wikiname)
382 char *wikistr;
383 int pid;
385 /* Extract wiki name string from Scheme value structure.
386 * If not a string, use the empty string */
387 if (scm_is_string (wikiname)) {
388 /* XXX No need or opportunity to free(3) this due to the exec below. */
389 wikistr = scm_to_locale_string(wikiname);
390 } else {
391 wikistr = "";
394 #ifndef __MINGW32__
396 pid = fork();
398 if (pid < 0) {
399 /* Fork failed. Still in parent process, so can use the log
400 * window */
401 s_log_message(_("Could not fork\n"));
402 return SCM_BOOL_F;
403 } else if (pid > 0) {
404 /* Parent process, we're finished here */
405 return SCM_BOOL_T;
408 /* begin daughter process stuff */
410 /* assume gschemdoc is part of path */
411 char *gschemdoc = "gschemdoc";
412 char *wikiarg = "-w";
414 execlp(gschemdoc, gschemdoc, wikiarg, wikistr, NULL);
416 /* if we return, then nothing happened */
417 fprintf(stderr, _("Could not invoke %s\n"), gschemdoc);
418 _exit(0);
420 /* end daughter process stuff */
422 #else /* __MINGW32__ */
423 s_log_message(_("Documentation commands not supported under MinGW.\n"));
424 return SCM_BOOL_F;
425 #endif /* __MINGW32__ */
428 SCM g_funcs_get_coords(SCM prompt, SCM continuation)
430 GtkComboBox *promptbox;
431 GtkListStore *model;
432 GtkTreeIter iter;
433 char *prompt_c;
434 SCM *t;
436 SCM_ASSERT(scm_is_string(prompt), prompt,
437 SCM_ARG1, "gschem-get-coord");
438 SCM_ASSERT(scm_is_true(scm_procedure_p(continuation)), continuation,
439 SCM_ARG2, "gschem-get-coord");
441 scm_gc_protect_object(continuation);
443 t = g_new(SCM, 1);
444 *t = continuation;
446 promptbox = GTK_COMBO_BOX(global_window_current->scm_prompts);
447 model = GTK_LIST_STORE(gtk_combo_box_get_model(promptbox));
449 prompt_c = scm_to_locale_string(prompt);
450 gtk_list_store_append(model, &iter);
451 gtk_list_store_set(model, &iter,
452 SCM_PROMPT_MESSAGE, prompt_c,
453 SCM_PROMPT_CONTINUATION, t,
454 -1);
455 free(prompt_c);
457 return SCM_BOOL_T;