Updated copyright text/header in most source files.
[geda-gaf/peter-b.git] / gschem / src / i_basic.c
blob2bdfbde63dddf76eca31c74ba32ab55a9ef6d017
1 /* gEDA - GPL Electronic Design Automation
2 * gschem - gEDA Schematic Capture
3 * Copyright (C) 1998-2010 Ales Hvezda
4 * Copyright (C) 1998-2010 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 #ifdef HAVE_STRING_H
23 #include <string.h>
24 #endif
26 #include "gschem.h"
28 #ifdef HAVE_LIBDMALLOC
29 #include <dmalloc.h>
30 #endif
32 /*! \brief Update status bar string
34 * \par Function Description
35 * This function actually updates the status bar
36 * widget with the new string.
38 * \param [in] w_current GSCHEM_TOPLEVEL structure
39 * \param [in] string The new string to be shown in the status bar
41 static void i_update_status(GSCHEM_TOPLEVEL *w_current, const char *string)
43 if (!w_current->status_label)
44 return;
46 if (string) {
47 /* NOTE: consider optimizing this if same label */
48 gtk_label_set(GTK_LABEL(w_current->status_label),
49 (char *) string);
53 /*! \brief Get string corresponding to the currently selected mode
55 * \par Function Description
56 * Returns a string describing the currently
57 * selected mode.
59 * \param [in] w_current GSCHEM_TOPLEVEL structure
60 * \returns a string that will only last until the next time
61 * the function is called (which is probably just fine, really)
62 * *EK* Egil Kvaleberg
64 static const char *i_status_string(GSCHEM_TOPLEVEL *w_current)
66 static char *buf = 0;
68 switch ( w_current->event_state ) {
69 case NONE:
70 case STARTROUTENET: /*! \todo */
71 case ENDROUTENET: /*! \todo */
72 return "";
73 case STARTSELECT:
74 case SELECT:
75 case SBOX:
76 case GRIPS:
77 return _("Select Mode");
78 case ENDCOMP:
79 return _("Component Mode"); /*EK* new */
80 case ENDTEXT:
81 return _("Text Mode"); /*EK* new */
82 case STARTCOPY:
83 case ENDCOPY:
84 return _("Copy Mode");
85 case STARTMOVE:
86 case ENDMOVE:
87 return _("Move Mode");
88 case ENDROTATEP:
89 return _("Rotate Mode");
90 case ENDMIRROR:
91 return _("Mirror Mode");
92 case ZOOM:
93 case ZOOMBOXEND:
94 case ZOOMBOXSTART:
95 return _("Zoom Box");
96 case STARTPAN:
97 case PAN:
98 case MOUSEPAN:
99 return _("Pan Mode");
100 case STARTPASTE:
101 case ENDPASTE:
102 g_free(buf);
103 buf = g_strdup_printf(_("Paste %d Mode"), w_current->buffer_number+1);
104 return buf;
105 case STARTDRAWNET:
106 case DRAWNET:
107 case NETCONT:
108 if (w_current->magneticnet_mode)
109 return _("Magnetic Net Mode");
110 else
111 return _("Net Mode");
112 case STARTDRAWBUS:
113 case DRAWBUS:
114 case BUSCONT:
115 return _("Bus Mode");
116 case DRAWLINE:
117 case ENDLINE:
118 return _("Line Mode");
119 case DRAWBOX:
120 case ENDBOX:
121 return _("Box Mode");
122 case DRAWPICTURE:
123 case ENDPICTURE:
124 return _("Picture Mode");
125 case DRAWCIRCLE:
126 case ENDCIRCLE:
127 return _("Circle Mode");
128 case DRAWARC:
129 case ENDARC:
130 return _("Arc Mode");
131 case DRAWPIN:
132 case ENDPIN:
133 return _("Pin Mode");
134 case COPY:
135 return _("Copy");
136 case MOVE:
137 return _("Move");
138 case MCOPY:
139 return _("Multiple Copy");
140 case STARTMCOPY:
141 case ENDMCOPY:
142 return _("Multiple Copy Mode");
144 g_assert_not_reached();
145 return ""; /* should not happen */
148 /*! \brief Show state field
150 * \par Function Description
151 * Show state field on screen, possibly with the
152 * addition of an extra message
154 * \param [in] w_current GSCHEM_TOPLEVEL structure
155 * \param [in] message The string to be displayed
157 void i_show_state(GSCHEM_TOPLEVEL *w_current, const char *message)
159 TOPLEVEL *toplevel = w_current->toplevel;
160 gchar *what_to_say;
161 const gchar *array[5] = { NULL };
162 int i = 3; /* array[4] must be NULL */
164 /* Fill in the string array */
165 array[i--] = i_status_string(w_current);
167 if(toplevel->show_hidden_text)
168 array[i--] = _("Show Hidden");
170 if(toplevel->snap == SNAP_OFF)
171 array[i--] = _("Snap Off");
172 else if (toplevel->snap == SNAP_RESNAP)
173 array[i--] = _("Resnap Active");
175 if(message && message[0])
176 array[i] = message;
178 /* Skip over NULLs */
179 while(array[i] == NULL)
180 i++;
182 what_to_say = g_strjoinv(" - ", (gchar **) array + i);
184 if(w_current->keyaccel_string) {
185 gchar *p = what_to_say;
187 what_to_say = g_strdup_printf("%s \t\t %s", w_current->keyaccel_string,
188 what_to_say);
189 g_free(p);
192 i_update_status(w_current, what_to_say);
193 g_free(what_to_say);
196 /*! \brief Set new state, then show state field
198 * \par Function Description
199 * Set new state, then show state field.
201 * \param [in] w_current GSCHEM_TOPLEVEL structure
202 * \param [in] newstate The new state
203 * *EK* Egil Kvaleberg
205 void i_set_state(GSCHEM_TOPLEVEL *w_current, enum x_states newstate)
207 i_set_state_msg(w_current, newstate, NULL);
210 /*! \brief Set new state, then show state field including some
211 * message
213 * \par Function Description
214 * Set new state, then show state field including some
215 * message.
217 * \param [in] w_current GSCHEM_TOPLEVEL structure
218 * \param [in] newstate The new state
219 * \param [in] message Message to be shown
220 * *EK* Egil Kvaleberg
222 void i_set_state_msg(GSCHEM_TOPLEVEL *w_current, enum x_states newstate,
223 const char *message)
225 w_current->event_state = newstate;
226 i_show_state(w_current, message);
229 /*! \todo Finish function documentation!!!
230 * \brief
231 * \par Function Description
234 void i_update_middle_button(GSCHEM_TOPLEVEL *w_current,
235 void (*func_ptr)(),
236 const char *string)
238 char *temp_string;
240 if (func_ptr == NULL)
241 return;
243 if (string == NULL)
244 return;
246 if (!w_current->middle_label)
247 return;
249 switch(w_current->middle_button) {
251 /* remove this case eventually and make it a null case */
252 case(ACTION):
253 gtk_label_set(GTK_LABEL(w_current->middle_label),
254 _("Action"));
255 break;
257 #ifdef HAVE_LIBSTROKE
258 case(STROKE):
259 gtk_label_set(GTK_LABEL(w_current->middle_label),
260 _("Stroke"));
261 break;
262 #else
263 /* remove this case eventually and make it a null case */
264 case(STROKE):
265 gtk_label_set(GTK_LABEL(w_current->middle_label),
266 _("none"));
267 break;
268 #endif
270 case(REPEAT):
271 temp_string = g_strconcat (_("Repeat/"), string, NULL);
273 gtk_label_set(GTK_LABEL(w_current->middle_label),
274 temp_string);
275 w_current->last_callback = func_ptr;
276 g_free(temp_string);
277 break;
282 /*! \todo Finish function documentation!!!
283 * \brief
284 * \param [in] w_current GSCHEM_TOPLEVEL structure
287 void i_update_toolbar(GSCHEM_TOPLEVEL *w_current)
289 if (!w_current->toolbars)
290 return;
292 switch(w_current->event_state) {
293 case(NONE):
294 case(SELECT):
295 case(STARTSELECT):
296 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
297 w_current->toolbar_select), TRUE);
298 break;
300 case(DRAWNET):
301 case(STARTDRAWNET):
302 case(NETCONT):
303 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
304 w_current->toolbar_net), TRUE);
305 break;
307 case(DRAWBUS):
308 case(STARTDRAWBUS):
309 case(BUSCONT):
310 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
311 w_current->toolbar_bus), TRUE);
312 break;
314 case(DRAWLINE): /*! \todo */
315 case(DRAWBOX): /*! \todo */
316 case(DRAWPICTURE): /*! \todo */
317 case(DRAWPIN): /*! \todo */
318 case(DRAWCIRCLE): /*! \todo */
319 case(DRAWARC): /*! \todo */
320 case(MOVE): /*! \todo */
321 case(COPY): /*! \todo */
322 case(ZOOM): /*! \todo */
323 case(PAN): /*! \todo */
324 case(STARTPAN): /*! \todo */
325 case(STARTCOPY): /*! \todo */
326 case(STARTMOVE): /*! \todo */
327 case(ENDCOPY): /*! \todo */
328 case(ENDMOVE): /*! \todo */
329 case(ENDLINE): /*! \todo */
330 case(ENDBOX): /*! \todo */
331 case(ENDPICTURE): /*! \todo */
332 case(ENDCIRCLE): /*! \todo */
333 case(ENDARC): /*! \todo */
334 case(ENDPIN): /*! \todo */
335 case(ENDCOMP): /*! \todo */
336 case(ENDTEXT): /*! \todo */
337 case(ENDROTATEP): /*! \todo */
338 case(ENDMIRROR): /*! \todo */
339 case(ZOOMBOXSTART): /*! \todo */
340 case(ZOOMBOXEND): /*! \todo */
341 case(STARTROUTENET): /*! \todo */
342 case(ENDROUTENET): /*! \todo */
343 case(MOUSEPAN): /*! \todo */
344 case(STARTPASTE): /*! \todo */
345 case(ENDPASTE): /*! \todo */
346 case(GRIPS): /*! \todo */
347 case(MCOPY): /*! \todo */
348 case(STARTMCOPY): /*! \todo */
349 case(ENDMCOPY): /*! \todo */
350 default:
351 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
352 w_current->toolbar_select), TRUE);
353 break;
358 /*! \brief Update sensitivity of the Edit/Paste menu item
360 * \par Function Description
361 * Asynchronous callback to update sensitivity of the Edit/Paste
362 * menu item.
364 static void clipboard_usable_cb (int usable, void *userdata)
366 GSCHEM_TOPLEVEL *w_current = userdata;
367 x_menus_sensitivity (w_current, "_Edit/_Paste", usable);
371 /*! \brief Update sensitivity of relevant menu items
373 * \par Function Description
374 * Update sensitivity of relevant menu items.
376 * \param [in] w_current GSCHEM_TOPLEVEL structure
378 void i_update_menus(GSCHEM_TOPLEVEL *w_current)
380 TOPLEVEL *toplevel = w_current->toplevel;
382 * This is very simplistic. Right now it just disables all menu
383 * items which get greyed out when a component is not selected.
384 * Eventually what gets enabled/disabled
385 * should be based on what is in the selection list
388 g_assert(w_current != NULL);
389 g_assert(toplevel->page_current != NULL);
391 x_clipboard_query_usable (w_current, clipboard_usable_cb, w_current);
393 if (o_select_selected (w_current)) {
394 /* since one or more things are selected, we set these TRUE */
395 /* These strings should NOT be internationalized */
396 x_menus_sensitivity(w_current, "_Edit/Cu_t", TRUE);
397 x_menus_sensitivity(w_current, "_Edit/_Copy", TRUE);
398 x_menus_sensitivity(w_current, "_Edit/_Delete", TRUE);
399 x_menus_sensitivity(w_current, "_Edit/Copy Mode", TRUE);
400 x_menus_sensitivity(w_current, "_Edit/Multiple Copy Mode", TRUE);
401 x_menus_sensitivity(w_current, "_Edit/Move Mode", TRUE);
402 x_menus_sensitivity(w_current, "_Edit/Rotate 90 Mode", TRUE);
403 x_menus_sensitivity(w_current, "_Edit/Mirror Mode", TRUE);
404 x_menus_sensitivity(w_current, "_Edit/Edit...", TRUE);
405 x_menus_sensitivity(w_current, "_Edit/Edit Text...", TRUE);
406 x_menus_sensitivity(w_current, "_Edit/Slot...", TRUE);
407 x_menus_sensitivity(w_current, "_Edit/Color...", TRUE);
408 x_menus_sensitivity(w_current, "_Edit/Lock", TRUE);
409 x_menus_sensitivity(w_current, "_Edit/Unlock", TRUE);
410 x_menus_sensitivity(w_current, "_Edit/Line Width & Type...", TRUE);
411 x_menus_sensitivity(w_current, "_Edit/Fill Type...", TRUE);
412 x_menus_sensitivity(w_current, "_Edit/Embed Component/Picture", TRUE);
413 x_menus_sensitivity(w_current, "_Edit/Unembed Component/Picture", TRUE);
414 x_menus_sensitivity(w_current, "_Edit/Update Component", TRUE);
415 x_menus_sensitivity(w_current, "_Buffer/Copy into 1", TRUE);
416 x_menus_sensitivity(w_current, "_Buffer/Copy into 2", TRUE);
417 x_menus_sensitivity(w_current, "_Buffer/Copy into 3", TRUE);
418 x_menus_sensitivity(w_current, "_Buffer/Copy into 4", TRUE);
419 x_menus_sensitivity(w_current, "_Buffer/Copy into 5", TRUE);
420 x_menus_sensitivity(w_current, "_Buffer/Cut into 1", TRUE);
421 x_menus_sensitivity(w_current, "_Buffer/Cut into 2", TRUE);
422 x_menus_sensitivity(w_current, "_Buffer/Cut into 3", TRUE);
423 x_menus_sensitivity(w_current, "_Buffer/Cut into 4", TRUE);
424 x_menus_sensitivity(w_current, "_Buffer/Cut into 5", TRUE);
425 x_menus_sensitivity(w_current, "Hie_rarchy/_Down Schematic", TRUE);
426 x_menus_sensitivity(w_current, "Hie_rarchy/Down _Symbol", TRUE);
427 x_menus_sensitivity(w_current, "Hie_rarchy/D_ocumentation...", TRUE);
428 x_menus_sensitivity(w_current, "A_ttributes/_Attach", TRUE);
429 x_menus_sensitivity(w_current, "A_ttributes/_Detach", TRUE);
430 x_menus_sensitivity(w_current, "A_ttributes/Show _Value", TRUE);
431 x_menus_sensitivity(w_current, "A_ttributes/Show _Name", TRUE);
432 x_menus_sensitivity(w_current, "A_ttributes/Show _Both", TRUE);
433 x_menus_sensitivity(w_current, "A_ttributes/_Toggle Visibility", TRUE);
435 /* Menu items for hierarchy added by SDB 1.9.2005. */
436 x_menus_popup_sensitivity(w_current, "/Down Schematic", TRUE);
437 x_menus_popup_sensitivity(w_current, "/Down Symbol", TRUE);
438 /* x_menus_popup_sensitivity(w_current, "/Up", TRUE); */
440 } else {
441 /* Nothing is selected, grey these out */
442 /* These strings should NOT be internationalized */
443 x_menus_sensitivity(w_current, "_Edit/Cu_t", FALSE);
444 x_menus_sensitivity(w_current, "_Edit/_Copy", FALSE);
445 x_menus_sensitivity(w_current, "_Edit/_Delete", FALSE);
446 x_menus_sensitivity(w_current, "_Edit/Copy Mode", FALSE);
447 x_menus_sensitivity(w_current, "_Edit/Multiple Copy Mode", FALSE);
448 x_menus_sensitivity(w_current, "_Edit/Move Mode", FALSE);
449 x_menus_sensitivity(w_current, "_Edit/Rotate 90 Mode", FALSE);
450 x_menus_sensitivity(w_current, "_Edit/Mirror Mode", FALSE);
451 x_menus_sensitivity(w_current, "_Edit/Edit...", FALSE);
452 x_menus_sensitivity(w_current, "_Edit/Edit Text...", FALSE);
453 x_menus_sensitivity(w_current, "_Edit/Slot...", FALSE);
454 x_menus_sensitivity(w_current, "_Edit/Color...", FALSE);
455 x_menus_sensitivity(w_current, "_Edit/Lock", FALSE);
456 x_menus_sensitivity(w_current, "_Edit/Unlock", FALSE);
457 x_menus_sensitivity(w_current, "_Edit/Line Width & Type...", FALSE);
458 x_menus_sensitivity(w_current, "_Edit/Fill Type...", FALSE);
459 x_menus_sensitivity(w_current, "_Edit/Embed Component/Picture", FALSE);
460 x_menus_sensitivity(w_current, "_Edit/Unembed Component/Picture", FALSE);
461 x_menus_sensitivity(w_current, "_Edit/Update Component", FALSE);
462 x_menus_sensitivity(w_current, "_Buffer/Copy into 1", FALSE);
463 x_menus_sensitivity(w_current, "_Buffer/Copy into 2", FALSE);
464 x_menus_sensitivity(w_current, "_Buffer/Copy into 3", FALSE);
465 x_menus_sensitivity(w_current, "_Buffer/Copy into 4", FALSE);
466 x_menus_sensitivity(w_current, "_Buffer/Copy into 5", FALSE);
467 x_menus_sensitivity(w_current, "_Buffer/Cut into 1", FALSE);
468 x_menus_sensitivity(w_current, "_Buffer/Cut into 2", FALSE);
469 x_menus_sensitivity(w_current, "_Buffer/Cut into 3", FALSE);
470 x_menus_sensitivity(w_current, "_Buffer/Cut into 4", FALSE);
471 x_menus_sensitivity(w_current, "_Buffer/Cut into 5", FALSE);
472 x_menus_sensitivity(w_current, "Hie_rarchy/_Down Schematic", FALSE);
473 x_menus_sensitivity(w_current, "Hie_rarchy/Down _Symbol", FALSE);
474 x_menus_sensitivity(w_current, "Hie_rarchy/D_ocumentation...", FALSE);
475 x_menus_sensitivity(w_current, "A_ttributes/_Attach", FALSE);
476 x_menus_sensitivity(w_current, "A_ttributes/_Detach", FALSE);
477 x_menus_sensitivity(w_current, "A_ttributes/Show _Value", FALSE);
478 x_menus_sensitivity(w_current, "A_ttributes/Show _Name", FALSE);
479 x_menus_sensitivity(w_current, "A_ttributes/Show _Both", FALSE);
480 x_menus_sensitivity(w_current, "A_ttributes/_Toggle Visibility", FALSE);
482 /* Menu items for hierarchy added by SDB 1.9.2005. */
483 x_menus_popup_sensitivity(w_current, "/Down Schematic", FALSE);
484 x_menus_popup_sensitivity(w_current, "/Down Symbol", FALSE);
485 /* x_menus_popup_sensitivity(w_current, "/Up", FALSE); */
488 x_menus_sensitivity(w_current, "_Buffer/Paste from 1", (object_buffer[0] != NULL));
489 x_menus_sensitivity(w_current, "_Buffer/Paste from 2", (object_buffer[1] != NULL));
490 x_menus_sensitivity(w_current, "_Buffer/Paste from 3", (object_buffer[2] != NULL));
491 x_menus_sensitivity(w_current, "_Buffer/Paste from 4", (object_buffer[3] != NULL));
492 x_menus_sensitivity(w_current, "_Buffer/Paste from 5", (object_buffer[4] != NULL));
496 /*! \brief Set filename as gschem window title
498 * \par Function Description
499 * Set filename as gschem window title using
500 * the gnome HID format style.
502 * \param [in] w_current GSCHEM_TOPLEVEL structure
503 * \param [in] string The filename
505 void i_set_filename(GSCHEM_TOPLEVEL *w_current, const gchar *string)
507 gchar *print_string=NULL;
508 gchar *filename=NULL;
510 if (!w_current->main_window)
511 return;
512 if (string == NULL)
513 return;
515 filename = g_path_get_basename(string);
517 print_string = g_strdup_printf("%s - gschem", filename);
519 gtk_window_set_title(GTK_WINDOW(w_current->main_window),
520 print_string);
522 g_free(print_string);
523 g_free(filename);
526 /*! \brief Write the grid settings to the gschem status bar
528 * \par Function Description
529 * Write the grid settings to the gschem status bar.
530 * The function takes the current grid paramters of gschem
531 * and prints it to the status bar.
532 * The format is "Grid([SnapGridSize],[CurrentGridSize])"
534 * \param [in] w_current GSCHEM_TOPLEVEL structure
535 * \param [in] visible_grid Visible grid size
537 void i_update_grid_info (GSCHEM_TOPLEVEL *w_current)
539 TOPLEVEL *toplevel = w_current->toplevel;
540 gchar *print_string=NULL;
541 gchar *snap=NULL;
542 gchar *grid=NULL;
544 if (!w_current->grid_label)
545 return;
547 switch (toplevel->snap) {
548 case SNAP_OFF:
549 snap = g_strdup(_("OFF"));
550 break;
551 case SNAP_GRID:
552 snap = g_strdup_printf("%d", toplevel->snap_size);
553 break;
554 case SNAP_RESNAP:
555 snap = g_strdup_printf("%dR", toplevel->snap_size);
556 break;
557 default:
558 g_critical("i_set_grid: toplevel->snap out of range: %d\n",
559 toplevel->snap);
562 if (w_current->grid == GRID_NONE) {
563 grid = g_strdup(_("OFF"));
564 } else {
565 int visible_grid = x_grid_query_drawn_spacing (w_current);
566 if (visible_grid == -1)
567 grid = g_strdup (_("NONE"));
568 else
569 grid = g_strdup_printf("%d", visible_grid);
572 print_string = g_strdup_printf(_("Grid(%s, %s)"), snap, grid);
573 gtk_label_set(GTK_LABEL(w_current->grid_label), print_string);
575 g_free(print_string);
576 g_free(grid);
577 g_free(snap);