Take page as input and take advantage of s_visit_page.
[geda-gaf/berndj.git] / gschem / src / x_autonumber.c
blob2cc184811754d8da63dc7bf987e730e1016b2a21
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
21 #include <config.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #ifdef HAVE_STDLIB_H
26 #include <stdlib.h>
27 #endif
28 #ifdef HAVE_STRING_H
29 #include <string.h>
30 #endif
32 #include "gschem.h"
33 #include <gdk/gdkkeysyms.h>
35 #include "support.h"
37 #ifdef HAVE_LIBDMALLOC
38 #include <dmalloc.h>
39 #endif
41 #define GLADE_HOOKUP_OBJECT(component,widget,name) \
42 g_object_set_data_full (G_OBJECT (component), name, \
43 gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)
45 /** @brief How many entries to keep in the "Search text" combo box. */
46 #define HISTORY_LENGTH 15
48 /* autonumber text structs and enums */
49 enum {
50 AUTONUMBER_SORT_DIAGONAL,
51 AUTONUMBER_SORT_YX,
52 AUTONUMBER_SORT_YX_REV,
53 AUTONUMBER_SORT_XY,
54 AUTONUMBER_SORT_XY_REV,
55 AUTONUMBER_SORT_FILE
58 enum {
59 AUTONUMBER_IGNORE,
60 AUTONUMBER_RENUMBER,
61 AUTONUMBER_RESPECT
64 enum {
65 SCOPE_SELECTED,
66 SCOPE_PAGE,
67 SCOPE_HIERARCHY
70 typedef struct autonumber_text_t AUTONUMBER_TEXT;
72 /** @brief Stored state of the autonumber text dialog */
73 struct autonumber_text_t {
74 /** @brief Search text history */
75 GList *scope_text;
77 /** @brief Scope for searching existing numbers */
78 gint scope_skip;
80 /** @brief Scope for autonumbering text */
81 gint scope_number;
83 /** @brief Overwrite existing numbers in scope */
84 gboolean scope_overwrite;
86 /** @brief Sort order */
87 gint order;
89 /** @brief Starting number for automatic numbering */
90 gint startnum;
92 /** @brief Remove numbers instead of automatic numbering */
93 gboolean removenum;
95 /** @brief Automatic assignments of slots */
96 gboolean slotting;
98 /** @brief Pointer to the dialog */
99 GtkWidget *dialog;
101 /** @brief Pointer to the GSCHEM_TOPLEVEL struct */
102 GSCHEM_TOPLEVEL *w_current;
104 /* variables used while autonumbering */
105 gchar * current_searchtext;
106 gint root_page; /* flag whether its the root page or not */
107 GList *used_numbers; /* list of used numbers */
108 GList *free_slots; /* list of FREE_SLOT objects */
109 GList *used_slots; /* list of USED_SLOT objects */
112 typedef struct autonumber_slot_t AUTONUMBER_SLOT;
114 struct autonumber_slot_t {
115 gchar *symbolname; /* or should I use the device name? (Werner) */
116 gint number; /* usually this is the refdes number */
117 gint slotnr; /* just the number of the free slot */
122 /* ***** BACK-END CODE ***************************************************** */
124 /********** compare functions for g_list_sort, ... ***********************/
125 /*! \brief GCompareFunc function to sort a list with g_list_sort().
126 * \par Function Description
127 * Compares the integer values of the gconstpointers a and b.
128 * \return -1 if a<b, 1 if a>b, 0 if a==b
130 gint autonumber_sort_numbers(gconstpointer a, gconstpointer b) {
131 if (GPOINTER_TO_INT(a) < GPOINTER_TO_INT(b))
132 return -1;
133 if (GPOINTER_TO_INT(a) > GPOINTER_TO_INT(b))
134 return 1;
135 return 0;
138 /*! \brief GCompareFunc function to sort text objects by there location
139 * \par Function Description
140 * This Function takes two <B>OBJECT*</B> arguments and compares the
141 * location of the two text objects. The first sort criteria is the x location,
142 * the second sort criteria is the y location.
143 * The Function is used as GCompareFunc by g_list_sort().
145 gint autonumber_sort_xy(gconstpointer a, gconstpointer b) {
146 OBJECT const *aa, *bb;
147 aa=a; bb=b;
148 if (aa->text->x < bb->text->x)
149 return -1;
150 if (aa->text->x > bb->text->x)
151 return 1;
152 /* x == x */
153 if (aa->text->y > bb->text->y)
154 return -1;
155 if (aa->text->y < bb->text->y)
156 return 1;
157 return 0;
160 /*! \brief GCompareFunc function to sort text objects by there location
161 * \par Function Description
162 * This function takes two <B>OBJECT*</B> arguments and compares the
163 * location of the two text objects. The first sort criteria is the x location,
164 * the second sort criteria is the y location.
165 * This function sorts the objects in reverse order.
166 * The function is used as GCompareFunc by g_list_sort().
168 gint autonumber_sort_xy_rev(gconstpointer a, gconstpointer b) {
169 OBJECT const *aa, *bb;
170 aa=a; bb=b;
171 if (aa->text->x < bb->text->x)
172 return 1;
173 if (aa->text->x > bb->text->x)
174 return -1;
175 /* x == x */
176 if (aa->text->y < bb->text->y)
177 return 1;
178 if (aa->text->y > bb->text->y)
179 return -1;
180 return 0;
183 /*! \brief GCompareFunc function to sort text objects by there location
184 * \par Function Description
185 * This function takes two <B>OBJECT*</B> arguments and compares the
186 * location of the two text objects. The first sort criteria is the y location,
187 * the second sort criteria is the x location.
188 * The function is used as GCompareFunc by g_list_sort().
190 int autonumber_sort_yx(gconstpointer a, gconstpointer b) {
191 OBJECT const *aa, *bb;
192 aa=a; bb=b;
193 if (aa->text->y > bb->text->y)
194 return -1;
195 if (aa->text->y < bb->text->y)
196 return 1;
197 /* y == y */
198 if (aa->text->x < bb->text->x)
199 return -1;
200 if (aa->text->x > bb->text->x)
201 return 1;
202 return 0;
205 /*! \brief GCompareFunc function to sort text objects by there location
206 * \par Function Description
207 * This Function takes two <B>OBJECT*</B> arguments and compares the
208 * location of the two text objects. The first sort criteria is the y location,
209 * the second sort criteria is the x location.
210 * This function sorts the objects in reverse order.
211 * The function is used as GCompareFunc by g_list_sort().
213 int autonumber_sort_yx_rev(gconstpointer a, gconstpointer b) {
214 OBJECT const *aa, *bb;
215 aa=a; bb=b;
216 if (aa->text->y > bb->text->y)
217 return 1;
218 if (aa->text->y < bb->text->y)
219 return -1;
220 /* y == y */
221 if (aa->text->x > bb->text->x)
222 return 1;
223 if (aa->text->x < bb->text->x)
224 return -1;
225 return 0;
228 /*! \brief GCompareFunc function to sort text objects by there location
229 * \par Function Description
230 * This Function takes two <B>OBJECT*</B> arguments and compares the
231 * location of the two text objects. The sort criteria is the combined x- and the
232 * y-location. The function sorts from top left to bottom right.
233 * The function is used as GCompareFunc by g_list_sort().
235 int autonumber_sort_diagonal(gconstpointer a, gconstpointer b) {
236 OBJECT const *aa, *bb;
237 aa=a; bb=b;
238 if (aa->text->x - aa->text->y < bb->text->x - bb->text->y)
239 return -1;
240 if (aa->text->x - aa->text->y > bb->text->x - bb->text->y)
241 return 1;
242 return 0;
245 /*! \brief GCompareFunc function to access <B>AUTONUMBER_SLOT</B> object in a GList
246 * \par Function Description
247 * This Function takes two <B>AUTONUMBER_SLOT*</B> arguments and compares them.
248 * Sorting criteria is are the AUTONUMBER_SLOT members: first the symbolname, than the
249 * number and last the slotnr.
250 * If the number or the slotnr is set to zero it acts as a wildcard.
251 * The function is used as GCompareFunc by GList functions.
253 gint freeslot_compare(gconstpointer a, gconstpointer b)
255 AUTONUMBER_SLOT const *aa, *bb;
256 gint res;
257 aa = a; bb = b;
259 if ((res = strcmp(aa->symbolname, bb->symbolname)) != 0)
260 return res;
262 /* aa->symbolname == bb->symbolname */
263 if (aa->number == 0 || bb->number == 0)
264 return 0;
265 if (aa->number > bb->number)
266 return 1;
267 if (aa->number < bb->number)
268 return -1;
270 /* aa->number == bb->number */
271 if (aa->slotnr == 0 || bb->slotnr == 0)
272 return 0;
273 if (aa->slotnr > bb->slotnr)
274 return 1;
275 if (aa->slotnr < bb->slotnr)
276 return -1;
278 return 0;
281 /*! \brief Prints a <B>GList</B> of <B>AUTONUMBER_SLOT</B> elements
282 * \par Function Description
283 * This function prints the elements of a GList that contains <B>AUTONUMBER_SLOT</B> elements
284 * It is only used for debugging purposes.
286 void freeslot_print(GList *list) {
287 GList *item;
288 AUTONUMBER_SLOT *fs;
290 printf("freeslot_print(): symname, number, slot\n");
291 for (item = list; item != NULL; item = g_list_next(item)) {
292 fs = item ->data;
293 printf(" %s, %d, %d\n",fs->symbolname, fs->number, fs->slotnr);
298 /*! \brief Function to clear the databases of used parts
299 * \par Function Descriptions
300 * Just remove the list of used numbers, used slots and free slots.
302 void autonumber_clear_database (AUTONUMBER_TEXT *autotext)
304 /* cleanup everything for the next searchtext */
305 if (autotext->used_numbers != NULL) {
306 g_list_free(autotext->used_numbers);
307 autotext->used_numbers = NULL;
309 if (autotext->free_slots != NULL) {
310 g_list_foreach(autotext->free_slots, (GFunc) g_free, NULL);
311 g_list_free(autotext->free_slots);
312 autotext->free_slots = NULL;
314 if (autotext->used_slots != NULL) {
315 g_list_foreach(autotext->used_slots, (GFunc) g_free, NULL);
316 g_list_free(autotext->used_slots);
317 autotext->used_slots = NULL;
321 /*! \brief Function to test, whether the OBJECT matches the autotext criteria
322 * \par Function Description
323 * The criteria are those of the autonumber text dialog. The function decides
324 * whether the <B>OBJECT</B> has to be renumberd, ignored or taken care of when
325 * renumbering all other objects.
326 * \return one of these integer values: <B>AUTONUMBER_IGNORE</B>,
327 * <B>AUTONUMBER_RESPECT</B> or <B>AUTONUMBER_RENUMBER</B> and the current number
328 * of the text object in <B>*number</B>.
330 gint autonumber_match(AUTONUMBER_TEXT *autotext, OBJECT *o_current, gint *number)
332 gint i, len, isnumbered=1;
333 const gchar *str = NULL;
335 len = strlen(autotext->current_searchtext);
336 /* first find out whether we can ignore that object */
337 if (o_current->type != OBJ_TEXT) /* text object */
338 return AUTONUMBER_IGNORE;
340 str = o_text_get_string(o_current);
342 if (!(strlen(str) - len > 0)
343 || !g_str_has_prefix(str, autotext->current_searchtext))
344 return AUTONUMBER_IGNORE;
346 /* the string object matches with its leading characters to the searchtext */
347 /* now look for the extension, either a number or the "?" */
348 if (g_str_has_suffix (str,"?")) {
349 isnumbered = 0;
350 /* There must not be any character between the "?" and the searchtext */
351 if (strlen(str) != len+1)
352 return AUTONUMBER_IGNORE;
354 else {
355 if (!isdigit( (int) (str[len]) )) /* has at least one digit */
356 return AUTONUMBER_IGNORE;
358 for (i=len+1; str[i]; i++) /* and only digits */
359 if (!isdigit( (int) (str[i]) ))
360 return AUTONUMBER_IGNORE;
363 /* we have six cases, 3 from focus multiplied by 2 selection cases */
364 if ((autotext->root_page || autotext->scope_number == SCOPE_HIERARCHY)
365 && (o_current->selected
366 || autotext->scope_number == SCOPE_HIERARCHY || autotext->scope_number == SCOPE_PAGE)
367 && (!isnumbered || (autotext->scope_overwrite)))
368 return AUTONUMBER_RENUMBER;
370 if (isnumbered
371 && !(autotext->scope_skip == SCOPE_SELECTED
372 && !(o_current->selected) && autotext->root_page)) {
373 sscanf(&(str[len])," %d", number);
374 return AUTONUMBER_RESPECT; /* numbered objects which we don't renumber */
376 else
377 return AUTONUMBER_IGNORE; /* unnumbered objects outside the focus */
381 /*! \brief Creates a list of already numbered objects and slots
382 * \par Function Description
383 * This function collects the used numbers of a single schematic page.
384 * The used element numbers are stored in a GList container
385 * inside the <B>AUTONUMBER_TEXT</B> struct.
386 * The slotting container is a little bit different. It stores free slots of
387 * multislotted symbols, that were used only partially.
388 * The criteria are derived from the autonumber dialog entries.
390 void autonumber_get_used(GSCHEM_TOPLEVEL *w_current, AUTONUMBER_TEXT *autotext)
392 gint number, numslots, slotnr, i;
393 OBJECT *o_current, *o_parent, *o_numslots;
394 AUTONUMBER_SLOT *slot;
395 GList *slot_item;
396 char *numslot_str, *slot_str;
398 for (o_current = w_current->toplevel->page_current->object_head; o_current != NULL;
399 o_current = o_current->next) {
400 if (autonumber_match(autotext, o_current, &number) == AUTONUMBER_RESPECT) {
401 /* check slot and maybe add it to the lists */
402 o_parent = o_current->attached_to;
403 if (autotext->slotting && o_parent != NULL) {
404 /* check for slotted symbol */
405 if ((numslot_str = o_attrib_search_numslots(o_parent, &o_numslots)) != NULL) {
406 sscanf(numslot_str," %d",&numslots);
407 g_free(numslot_str);
409 if (numslots > 0) {
410 slot_str=o_attrib_search_attrib_name(o_parent->attribs,"slot",0);
411 if (slot_str == NULL) {
412 s_log_message(_("slotted object without slot attribute may cause "
413 "problems when autonumbering slots\n"));
415 else {
416 sscanf(slot_str, " %d", &slotnr);
417 slot = g_new(AUTONUMBER_SLOT,1);
418 slot->number = number;
419 slot->slotnr = slotnr;
420 slot->symbolname = o_parent->complex_basename;
423 slot_item = g_list_find_custom(autotext->used_slots,
424 slot,
425 (GCompareFunc) freeslot_compare);
426 if (slot_item != NULL) { /* duplicate slot in used_slots */
427 s_log_message(_("duplicate slot may cause problems: "
428 "[symbolname=%s, number=%d, slot=%d]\n"),
429 slot->symbolname, slot->number, slot->slotnr);
430 g_free(slot);
432 else {
433 autotext->used_slots = g_list_insert_sorted(autotext->used_slots,
434 slot,
435 (GCompareFunc) freeslot_compare);
437 slot_item = g_list_find_custom(autotext->free_slots,
438 slot,
439 (GCompareFunc) freeslot_compare);
440 if (slot_item == NULL) {
441 /* insert all slots to the list, except of the current one */
442 for (i=1; i <= numslots; i++) {
443 if (i != slotnr) {
444 slot = g_memdup(slot, sizeof(AUTONUMBER_SLOT));
445 slot->slotnr = i;
446 autotext->free_slots = g_list_insert_sorted(autotext->free_slots,
447 slot,
448 (GCompareFunc) freeslot_compare);
452 else {
453 g_free(slot_item->data);
454 autotext->free_slots = g_list_delete_link(autotext->free_slots, slot_item);
461 /* put number into the used list */
462 autotext->used_numbers = g_list_insert_sorted(autotext->used_numbers,
463 GINT_TO_POINTER(number),
464 (GCompareFunc) autonumber_sort_numbers);
470 /*! \brief Gets or generates free numbers for the autonumbering process.
471 * \par Function Description
472 * This function gets or generates new numbers for the <B>OBJECT o_current</B>.
473 * It uses the element numbers <B>used_numbers</B> and the list of the free slots
474 * <B>free_slots</B> of the <B>AUTONUMBER_TEXT</B> struct.
475 * \return
476 * The new number is returned into the <B>number</B> parameter.
477 * <B>slot</B> is set if autoslotting is active, else it is set to zero.
479 void autonumber_get_new_numbers(AUTONUMBER_TEXT *autotext, OBJECT *o_current,
480 gint *number, gint *slot)
482 GList *item;
483 gint new_number, numslots, i;
484 AUTONUMBER_SLOT *freeslot;
485 OBJECT *o_parent = NULL, *o_numslots;
486 GList *freeslot_item;
487 gchar *numslot_str;
489 new_number = autotext->startnum;
491 /* Check for slots first */
492 /* 1. are there any unused slots in the database? */
493 o_parent = o_current->attached_to;
494 if (autotext->slotting && o_parent != NULL) {
495 freeslot = g_new(AUTONUMBER_SLOT,1);
496 freeslot->symbolname = o_parent->complex_basename;
497 freeslot->number = 0;
498 freeslot->slotnr = 0;
499 freeslot_item = g_list_find_custom(autotext->free_slots,
500 freeslot,
501 (GCompareFunc) freeslot_compare);
502 g_free(freeslot);
503 /* Yes! -> remove from database, apply it */
504 if (freeslot_item != NULL) {
505 freeslot = freeslot_item->data;
506 *number = freeslot->number;
507 *slot = freeslot->slotnr;
508 g_free(freeslot);
509 autotext->free_slots = g_list_delete_link(autotext->free_slots, freeslot_item);
511 return;
515 /* get a new number */
516 item = autotext->used_numbers;
517 while (1) {
518 while (item != NULL && GPOINTER_TO_INT(item->data) < new_number)
519 item = g_list_next(item);
521 if (item == NULL || GPOINTER_TO_INT(item->data) > new_number)
522 break;
523 else /* new_number == item->data */
524 new_number++;
526 *number = new_number;
527 *slot = 0;
529 /* insert the new number to the used list */
530 autotext->used_numbers = g_list_insert_sorted(autotext->used_numbers,
531 GINT_TO_POINTER(new_number),
532 (GCompareFunc) autonumber_sort_numbers);
534 /* 3. is o_current a slotted object ? */
535 if ((autotext->slotting) && o_parent != NULL) {
536 if ((numslot_str = o_attrib_search_numslots(o_parent, &o_numslots)) != NULL) {
537 sscanf(numslot_str," %d",&numslots);
538 g_free(numslot_str);
539 if (numslots > 0) {
540 /* Yes! -> new number and slot=1; add the other slots to the database */
541 *slot = 1;
542 for (i=2; i <=numslots; i++) {
543 freeslot = g_new(AUTONUMBER_SLOT,1);
544 freeslot->symbolname = o_parent->complex_basename;
545 freeslot->number = new_number;
546 freeslot->slotnr = i;
547 autotext->free_slots = g_list_insert_sorted(autotext->free_slots,
548 freeslot,
549 (GCompareFunc) freeslot_compare);
556 /** @brief Removes the number from the element.
558 * This function updates the text content of the \a o_current object.
560 * @param autotext Pointer to the state structure
561 * @param o_current Pointer to the object from which to remove the number
564 void autonumber_remove_number(AUTONUMBER_TEXT * autotext, OBJECT *o_current)
566 OBJECT *o_parent, *o_slot;
567 gchar *slot_str;
568 gchar *str = NULL;
570 /* replace old text */
571 str = g_strdup_printf("%s?", autotext->current_searchtext);
572 o_text_set_string(o_current, str);
573 g_free (str);
575 /* redraw the text */
576 o_erase_single(autotext->w_current, o_current);
577 o_text_recreate(o_current);
578 o_text_draw(autotext->w_current, o_current);
580 /* remove the slot attribute if slotting is active */
581 if (autotext->slotting) {
582 /* get the slot attribute */
583 o_parent = o_current->attached_to;
584 if (o_parent != NULL) {
585 slot_str = o_attrib_search_slot(o_parent, &o_slot);
586 if (slot_str != NULL && o_slot != NULL) {
587 g_free(slot_str);
588 /* delete the slot attribute */
589 o_selection_remove (autotext->w_current->toplevel->page_current->selection_list, o_slot);
590 o_delete (autotext->w_current, o_slot);
594 autotext->w_current->toplevel->page_current->CHANGED = 1;
597 /*! \brief Changes the number <B>OBJECT</B> element. Changes the slot attribute.
598 * \par Function Description
599 * This function updates the text content of the <B>o_current</B> object.
600 * If the <B>slot</B> value is not zero. It updates the slot attribute of the
601 * complex element that is also the parent object of the o_current element.
603 void autonumber_apply_new_text(AUTONUMBER_TEXT * autotext, OBJECT *o_current,
604 gint number, gint slot)
606 OBJECT *o_parent, *o_slot;
607 gchar *slot_str;
608 gchar *str = NULL;
610 /* add the slot as attribute to the object */
611 o_parent = o_current->attached_to;
612 if (slot != 0 && o_parent != NULL) {
613 slot_str = o_attrib_search_slot(o_parent, &o_slot);
614 if (slot_str != NULL) {
615 /* update the slot attribute */
616 g_free(slot_str);
617 slot_str = g_strdup_printf("slot=%d",slot);
618 o_text_set_string(o_slot, slot_str);
619 g_free (slot_str);
620 o_erase_single(autotext->w_current, o_slot);
621 o_text_recreate(o_slot);
622 o_text_draw(autotext->w_current, o_slot);
624 else {
625 /* create a new attribute and attach it */
626 o_attrib_add_attrib(autotext->w_current,
627 g_strdup_printf("slot=%d",slot),
628 INVISIBLE, SHOW_NAME_VALUE,
629 o_parent);
631 o_attrib_slot_update(autotext->w_current->toplevel, o_parent);
634 /* replace old text */
635 str = g_strdup_printf("%s%d", autotext->current_searchtext, number);
636 o_text_set_string(o_current, str);
637 g_free (str);
639 /* redraw the text */
640 o_erase_single(autotext->w_current, o_current);
641 o_text_recreate(o_current);
642 o_text_draw(autotext->w_current, o_current);
643 autotext->w_current->toplevel->page_current->CHANGED = 1;
647 /*! \brief Handles all the options of the autonumber text dialog
648 * \par Function Description
649 * This function is the master of all autonumber code. It receives the options of
650 * the the autonumber text dialog in an <B>AUTONUMBER_TEXT</B> structure.
651 * First it collects all pages of a hierarchical schematic.
652 * Second it gets all matching text elements for the searchtext.
653 * Then it renumbers all text elements of all schematic pages. The renumbering
654 * follows the rules of the parameters given in the autonumber text dialog.
656 void autonumber_text_autonumber(AUTONUMBER_TEXT *autotext)
658 GList *pages;
659 GList *searchtext_list=NULL;
660 GList *text_item, *obj_item, *page_item;
661 OBJECT *o_current;
662 GSCHEM_TOPLEVEL *w_current;
663 gchar *searchtext;
664 gchar *scope_text;
665 gchar *new_searchtext;
666 gint i, number, slot;
667 GList *o_list = NULL;
669 w_current = autotext->w_current;
670 autotext->current_searchtext = NULL;
671 autotext->root_page = 1;
672 autotext->used_numbers = NULL;
673 autotext->free_slots = NULL;
674 autotext->used_slots = NULL;
676 scope_text = g_list_first(autotext->scope_text)->data;
678 /* Step1: get all pages of the hierarchy */
679 pages = s_hierarchy_traversepages(w_current->toplevel,
680 w_current->toplevel->page_current,
681 HIERARCHY_NODUPS);
683 /* g_list_foreach(pages, (GFunc) s_hierarchy_print_page, NULL); */
685 /* Step2: if searchtext has an asterisk at the end we have to find
686 all matching searchtextes.
688 Example: "refdes=*" will match each text that starts with "refdes="
689 and has a trailing "?" or a trailing number if the "all"-option is set.
690 We get a list of possible prefixes: refdes=R, refdes=C.
692 If there is only one search pattern, it becomes a single item
693 in the searchtext list */
695 if (strlen(scope_text) == 0) {
696 s_log_message(_("No searchstring given in autonumber text.\n"));
697 return; /* error */
699 else if (g_str_has_suffix(scope_text,"?") == TRUE) {
700 /* single searchtext, strip of the "?" */
701 searchtext = g_strndup(scope_text, strlen(scope_text)-1);
702 searchtext_list=g_list_append (searchtext_list, searchtext);
704 else if (g_str_has_suffix(scope_text,"*") == TRUE) {
705 /* strip of the "*" */
706 searchtext = g_strndup(scope_text, strlen(scope_text)-1);
707 /* collect all the possible searchtexts in all pages of the hierarchy */
708 for (page_item = pages; page_item != NULL; page_item = g_list_next(page_item)) {
709 s_toplevel_goto_page(w_current->toplevel, page_item->data);
710 /* iterate over all objects an look for matching searchtext's */
711 for (o_current = w_current->toplevel->page_current->object_head; o_current != NULL;
712 o_current = o_current->next) {
713 if (o_current->type == OBJ_TEXT) {
714 if (autotext->scope_number == SCOPE_HIERARCHY
715 || autotext->scope_number == SCOPE_PAGE
716 || ((autotext->scope_number == SCOPE_SELECTED) && (o_current->selected))) {
717 const gchar *str = o_text_get_string(o_current);
718 if (g_str_has_prefix (str, searchtext)) {
719 /* the beginnig of the current text matches with the searchtext now */
720 /* strip of the trailing [0-9?] chars and add it too the searchtext */
721 for (i = strlen (str)-1;
722 (i >= strlen(searchtext))
723 && (str[i] == '?'
724 || isdigit( (int) (str[i]) ));
725 i--)
726 ; /* void */
728 new_searchtext = g_strndup (str, i+1);
729 if (g_list_find_custom(searchtext_list, new_searchtext,
730 (GCompareFunc) strcmp) == NULL ) {
731 searchtext_list = g_list_append(searchtext_list, new_searchtext);
733 else {
734 g_free(new_searchtext);
740 if (autotext->scope_number == SCOPE_SELECTED || autotext->scope_number == SCOPE_PAGE)
741 break; /* search only in the first page */
743 g_free(searchtext);
745 else {
746 s_log_message(_("No '*' or '?' given at the end of the autonumber text.\n"));
747 return;
750 /* Step3: iterate over the search items in the list */
751 for (text_item=searchtext_list; text_item !=NULL; text_item=g_list_next(text_item)) {
752 autotext->current_searchtext = text_item->data;
753 /* printf("autonumber_text_autonumber: searchtext %s\n", autotext->current_searchtext); */
754 /* decide whether to renumber page by page or get a global used-list */
755 if (autotext->scope_skip == SCOPE_HIERARCHY) { /* whole hierarchy database */
756 /* renumbering all means that no db is required */
757 if (!(autotext->scope_number == SCOPE_HIERARCHY
758 && autotext->scope_overwrite)) {
759 for (page_item = pages; page_item != NULL; page_item = g_list_next(page_item)) {
760 autotext->root_page = (pages->data == page_item->data);
761 s_toplevel_goto_page(w_current->toplevel, page_item->data);
762 autonumber_get_used(w_current, autotext);
767 /* renumber the elements */
768 for (page_item = pages; page_item != NULL; page_item = g_list_next(page_item)) {
769 s_toplevel_goto_page(w_current->toplevel, page_item->data);
770 autotext->root_page = (pages->data == page_item->data);
771 /* build a page database if we're numbering pagebypage or selection only*/
772 if (autotext->scope_skip == SCOPE_PAGE || autotext->scope_skip == SCOPE_SELECTED) {
773 autonumber_get_used(w_current, autotext);
776 /* RENUMBER CODE FOR ONE PAGE AND ONE SEARCHTEXT*/
777 /* 1. get objects to renumber */
778 for (o_current = w_current->toplevel->page_current->object_head; o_current != NULL;
779 o_current = o_current->next) {
780 if (autonumber_match(autotext, o_current, &number) == AUTONUMBER_RENUMBER) {
781 /* put number into the used list */
782 o_list = g_list_append(o_list, o_current);
786 /* 2. sort object list */
787 switch (autotext->order) {
788 case AUTONUMBER_SORT_YX:
789 o_list=g_list_sort(o_list, autonumber_sort_yx);
790 break;
791 case AUTONUMBER_SORT_YX_REV:
792 o_list=g_list_sort(o_list, autonumber_sort_yx_rev);
793 break;
794 case AUTONUMBER_SORT_XY:
795 o_list=g_list_sort(o_list, autonumber_sort_xy);
796 break;
797 case AUTONUMBER_SORT_XY_REV:
798 o_list=g_list_sort(o_list, autonumber_sort_xy_rev);
799 break;
800 case AUTONUMBER_SORT_DIAGONAL:
801 o_list=g_list_sort(o_list, autonumber_sort_diagonal);
802 break;
803 default:
804 ; /* unsorted file order */
807 /* 3. renumber/reslot the objects */
808 for(obj_item=o_list; obj_item != NULL; obj_item=g_list_next(obj_item)) {
809 o_current= obj_item->data;
810 if(autotext->removenum) {
811 autonumber_remove_number(autotext, o_current);
812 } else {
813 /* get valid numbers from the database */
814 autonumber_get_new_numbers(autotext, o_current, &number, &slot);
815 /* and apply it. TODO: join these two functions */
816 autonumber_apply_new_text(autotext, o_current, number, slot);
819 g_list_free(o_list);
820 o_list = NULL;
822 /* destroy the page database */
823 if (autotext->scope_skip == SCOPE_PAGE
824 || autotext->scope_skip == SCOPE_SELECTED)
825 autonumber_clear_database(autotext);
827 if (autotext->scope_number == SCOPE_SELECTED
828 || autotext->scope_number == SCOPE_PAGE)
829 break; /* only renumber the parent page (the first page) */
831 autonumber_clear_database(autotext); /* cleanup */
834 /* cleanup and redraw all*/
835 g_list_foreach(searchtext_list, (GFunc) g_free, NULL);
836 g_list_free(searchtext_list);
837 /* go back to the root page */
838 s_toplevel_goto_page(w_current->toplevel, pages->data);
839 o_redraw_all(w_current);
840 g_list_free(pages);
841 o_undo_savestate(w_current, UNDO_ALL);
844 /* ***** UTILITY GUI FUNCTIONS (move to a separate file in the future?) **** */
846 /*! \brief Put the icons and the text into the sortorder combobox
847 * \par Function Description
848 * Load all bitmaps for the combobox and store them together with the label
849 * in a GtkListStore.
851 void autonumber_sortorder_create(GSCHEM_TOPLEVEL *w_current, GtkWidget *sort_order)
853 GtkListStore *store;
854 GtkTreeIter iter;
855 GtkCellRenderer *renderer;
856 GdkPixbuf *pixbuf;
857 gchar *path;
858 GError *error=NULL;
860 gchar *filenames[] = {"gschem-diagonal.png",
861 "gschem-top2bottom.png", "gschem-bottom2top.png",
862 "gschem-left2right.png", "gschem-right2left.png",
863 "gschem-fileorder.png",
864 NULL};
865 gchar *names[] = {N_("Diagonal"),
866 N_("Top to bottom"), N_("Bottom to top"),
867 N_("Left to right"), N_("Right to left"),
868 N_("File order"),
869 NULL};
870 gint i;
872 store = gtk_list_store_new(2, G_TYPE_STRING, GDK_TYPE_PIXBUF);
874 for (i=0; filenames[i] != NULL; i++) {
875 path=g_build_filename(w_current->toplevel->bitmap_directory,
876 filenames[i], NULL);
877 pixbuf = gdk_pixbuf_new_from_file(path, &error);
878 g_free(path);
879 gtk_list_store_append(store, &iter);
880 gtk_list_store_set(store, &iter,
881 0, _(names[i]),
882 1, pixbuf,
883 -1);
886 gtk_combo_box_set_model(GTK_COMBO_BOX(sort_order), GTK_TREE_MODEL(store));
887 renderer = gtk_cell_renderer_text_new ();
889 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (sort_order),
890 renderer, TRUE);
891 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (sort_order),
892 renderer, "text", 0, NULL);
893 renderer = gtk_cell_renderer_pixbuf_new();
894 g_object_set(G_OBJECT(renderer), "xpad", 5, "ypad", 5, NULL);
896 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (sort_order),
897 renderer, FALSE);
898 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (sort_order),
899 renderer, "pixbuf", 1, NULL);
902 /* ***** STATE STRUCT HANDLING (interface between GUI and backend code) **** */
904 /** @brief Adds a line to the search text history list
906 * Function makes sure that: 1) There are no duplicates in the list and 2) the
907 * last search text is always at the top of the list.
909 GList *autonumber_history_add(GList *history, gchar *text)
911 /* Search for this text in history and delete it (so we don't have
912 * duplicate entries) */
914 GList *cur;
916 cur=history;
917 while(cur!=NULL) {
918 if(!strcmp(text, cur->data)) {
919 history=g_list_remove_link(history, cur);
921 g_free(cur->data);
922 g_list_free(cur);
923 break;
925 cur=g_list_next(cur);
928 /* Add the new text at the beginning of the list */
930 history=g_list_prepend(history, text);
932 /* Truncate history */
933 while(g_list_length(history) > HISTORY_LENGTH) {
934 GList *last = g_list_last(history);
936 history = g_list_remove_link(history, last);
938 g_free(last->data);
939 g_list_free(last);
942 return history;
945 /** @brief Allocate and initialize the state structure
947 * @return Pointer to the allocated structure or NULL on error.
949 AUTONUMBER_TEXT *autonumber_init_state()
951 AUTONUMBER_TEXT *autotext;
953 /* Default contents of the combo box history */
954 gchar *default_text[] = {
955 "refdes=*",
956 "refdes=C?",
957 "refdes=D?",
958 "refdes=I?",
959 "refdes=L?",
960 "refdes=Q?",
961 "refdes=R?",
962 "refdes=T?",
963 "refdes=U?",
964 "refdes=X?",
965 "netname=*",
966 "netname=A?",
967 "netname=D?",
968 NULL
970 gchar **t;
972 autotext = g_new(AUTONUMBER_TEXT, 1);
974 if(autotext==NULL) return NULL;
976 autotext->scope_text = NULL;
977 t=default_text;
978 while(*t!=NULL) {
979 autotext->scope_text=g_list_append(autotext->scope_text,
980 g_strdup(*t));
981 t++;
984 autotext->scope_skip = SCOPE_PAGE;
985 autotext->scope_number = SCOPE_SELECTED;
987 autotext->scope_overwrite = 0;
988 autotext->order = AUTONUMBER_SORT_DIAGONAL;
990 autotext->startnum=1;
992 autotext->removenum=0;
993 autotext->slotting=0;
995 autotext->dialog = NULL;
997 return autotext;
1000 /** @brief Restore the settings for the autonumber text dialog
1002 * @param autotext Pointer to the state struct.
1004 void autonumber_set_state(AUTONUMBER_TEXT *autotext)
1006 GtkWidget *widget;
1007 GtkTreeModel *model;
1008 GList *el;
1009 /* Scope */
1011 /* Search text history */
1012 widget = lookup_widget(autotext->dialog, "scope_text");
1014 /* Simple way to clear the ComboBox. Owen from #gtk+ says:
1016 * Yeah, it's just slightly "shady" ... if you want to stick to fully
1017 * advertised API, you need to remember how many rows you added and
1018 * use gtk_combo_box_remove_text() */
1020 model = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
1021 gtk_list_store_clear(GTK_LIST_STORE(model));
1023 for (el= autotext->scope_text; el != NULL; el=g_list_next(el)) {
1024 gtk_combo_box_append_text(GTK_COMBO_BOX(widget), el->data);
1027 widget = gtk_bin_get_child(GTK_BIN(widget));
1028 gtk_entry_set_text(GTK_ENTRY(widget), g_list_first(autotext->scope_text)->data);
1030 widget = lookup_widget(autotext->dialog, "scope_skip");
1031 gtk_combo_box_set_active(GTK_COMBO_BOX(widget),
1032 autotext->scope_skip);
1034 widget = lookup_widget(autotext->dialog, "scope_number");
1035 gtk_combo_box_set_active(GTK_COMBO_BOX(widget),
1036 autotext->scope_number);
1038 widget = lookup_widget(autotext->dialog, "scope_overwrite");
1039 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget),
1040 autotext->scope_overwrite);
1042 /* Options */
1043 widget = lookup_widget(autotext->dialog, "opt_startnum");
1044 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget),
1045 autotext->startnum);
1047 widget = lookup_widget(autotext->dialog, "sort_order");
1048 gtk_combo_box_set_active(GTK_COMBO_BOX(widget), autotext->order);
1050 widget = lookup_widget(autotext->dialog, "opt_removenum");
1051 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget),
1052 autotext->removenum);
1054 widget = lookup_widget(autotext->dialog, "opt_slotting");
1055 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget),
1056 autotext->slotting);
1059 /** @brief Get the settings from the autonumber text dialog
1061 * Get the settings from the autonumber text dialog and store it in the
1062 * <B>AUTONUMBER_TEXT</B> structure.
1064 * @param autotext Pointer to the state struct.
1066 void autonumber_get_state(AUTONUMBER_TEXT *autotext)
1068 GtkWidget *widget;
1069 gchar *text;
1071 /* Scope */
1073 /* Search text history */
1074 widget = lookup_widget(autotext->dialog, "scope_text");
1075 widget = gtk_bin_get_child(GTK_BIN(widget));
1076 text = g_strdup(gtk_entry_get_text( GTK_ENTRY(widget)));
1078 autotext->scope_text=autonumber_history_add(autotext->scope_text, text);
1080 widget = lookup_widget(autotext->dialog, "scope_skip");
1081 autotext->scope_skip = gtk_combo_box_get_active( GTK_COMBO_BOX(widget) );
1083 widget = lookup_widget(autotext->dialog, "scope_number");
1084 autotext->scope_number = gtk_combo_box_get_active(GTK_COMBO_BOX(widget) );
1086 widget = lookup_widget(autotext->dialog, "scope_overwrite");
1087 autotext->scope_overwrite = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
1089 /* Sort order */
1090 widget = lookup_widget(autotext->dialog, "sort_order");
1091 autotext->order= gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
1093 /* Options */
1094 widget = lookup_widget(autotext->dialog, "opt_startnum");
1095 autotext->startnum=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
1097 widget = lookup_widget(autotext->dialog, "opt_removenum");
1098 autotext->removenum = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
1100 widget = lookup_widget(autotext->dialog, "opt_slotting");
1101 autotext->slotting = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
1104 /* ***** CALLBACKS (functions that get called directly from the GTK) ******* */
1106 /*! \brief response callback for the autonumber text dialog
1107 * \par Function Description
1108 * The function just closes the dialog if the close button is pressed or the
1109 * user closes the dialog window.
1110 * Triggering the apply button will call the autonumber action functions.
1112 void autonumber_text_response(GtkWidget * widget, gint response,
1113 AUTONUMBER_TEXT *autotext)
1115 switch (response) {
1116 case GTK_RESPONSE_ACCEPT:
1117 autonumber_get_state(autotext);
1118 if (autotext->removenum == TRUE && autotext->scope_overwrite == FALSE) {
1119 /* temporarly set the overwrite flag */
1120 autotext->scope_overwrite = TRUE;
1121 autonumber_text_autonumber(autotext);
1122 autotext->scope_overwrite = FALSE;
1124 else {
1125 autonumber_text_autonumber(autotext);
1127 break;
1128 case GTK_RESPONSE_REJECT:
1129 case GTK_RESPONSE_DELETE_EVENT:
1130 gtk_widget_destroy(autotext->dialog);
1131 autotext->dialog = NULL;
1132 break;
1133 default:
1134 printf("ERROR: autonumber_text_response(): strange signal %d\n",response);
1139 /** @brief Callback that activates or deactivates "overwrite existing numbers"
1140 * check box.
1142 * This gets called each time "remove numbers" check box gets clicked.
1144 void autonumber_removenum_toggled(GtkWidget * opt_removenum,
1145 AUTONUMBER_TEXT *autotext)
1147 GtkWidget *scope_overwrite;
1149 scope_overwrite=lookup_widget(autotext->dialog, "scope_overwrite");
1151 /* toggle activity of scope overwrite with respect to removenum */
1152 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(opt_removenum))) {
1153 gtk_widget_set_sensitive(scope_overwrite, 0);
1154 } else {
1155 gtk_widget_set_sensitive(scope_overwrite, 1);
1160 /* ***** DIALOG SET-UP ***************************************************** */
1162 /** @brief Creates the autonumber text dialog.
1164 * Dialog is not shown. No callbacks are registered. This is basically
1165 * unmodified code returned by Glade.
1167 * Only modification was the following substitution:
1169 * %s/create_pixmap (autonumber_text, "\(.*\)")/autonumber_create_pixmap("gschem-\1", w_current)/
1171 * and addition of the "w_current" parameter.
1173 * @param w_current Pointer to the top level struct.
1174 * @return Pointer to the dialog window.
1176 GtkWidget* autonumber_create_dialog(GSCHEM_TOPLEVEL *w_current)
1178 GtkWidget *autonumber_text;
1179 GtkWidget *vbox1;
1180 GtkWidget *alignment1;
1181 GtkWidget *vbox3;
1182 GtkWidget *table1;
1183 GtkWidget *label4;
1184 GtkWidget *scope_text;
1185 GtkWidget *label8;
1186 GtkWidget *label6;
1187 GtkWidget *scope_number;
1188 GtkWidget *scope_skip;
1189 GtkWidget *scope_overwrite;
1190 GtkWidget *label1;
1191 GtkWidget *alignment3;
1192 GtkWidget *vbox4;
1193 GtkWidget *table3;
1194 GtkWidget *label12;
1195 GtkWidget *label13;
1196 GtkObject *opt_startnum_adj;
1197 GtkWidget *opt_startnum;
1198 GtkWidget *sort_order;
1199 GtkWidget *opt_removenum;
1200 GtkWidget *opt_slotting;
1201 GtkWidget *label3;
1204 autonumber_text = gschem_dialog_new_with_buttons(_("Autonumber text"),
1205 GTK_WINDOW(w_current->main_window),
1206 0, /* not modal */
1207 "autonumber", w_current,
1208 GTK_STOCK_CLOSE,
1209 GTK_RESPONSE_REJECT,
1210 GTK_STOCK_APPLY,
1211 GTK_RESPONSE_ACCEPT,
1212 NULL);
1213 /* Set the alternative button order (ok, cancel, help) for other systems */
1214 gtk_dialog_set_alternative_button_order(GTK_DIALOG(autonumber_text),
1215 GTK_RESPONSE_ACCEPT,
1216 GTK_RESPONSE_REJECT,
1217 -1);
1219 gtk_window_position (GTK_WINDOW (autonumber_text),
1220 GTK_WIN_POS_MOUSE);
1222 gtk_container_border_width(GTK_CONTAINER(autonumber_text),
1223 DIALOG_BORDER_SPACING);
1224 vbox1 = GTK_DIALOG(autonumber_text)->vbox;
1225 gtk_box_set_spacing(GTK_BOX(vbox1), DIALOG_V_SPACING);
1227 /* scope section */
1228 label1 = gtk_label_new (_("<b>Scope</b>"));
1229 gtk_label_set_use_markup (GTK_LABEL (label1), TRUE);
1230 gtk_misc_set_alignment (GTK_MISC(label1), 0, 0);
1231 gtk_box_pack_start (GTK_BOX(vbox1), label1, TRUE, TRUE, 0);
1232 gtk_widget_show (label1);
1234 alignment1 = gtk_alignment_new (0, 0, 1, 1);
1235 gtk_widget_show (alignment1);
1236 gtk_box_pack_start (GTK_BOX (vbox1), alignment1, TRUE, TRUE, 0);
1237 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment1),
1238 0, 0, DIALOG_INDENTATION, 0);
1240 vbox3 = gtk_vbox_new (FALSE, 0);
1241 gtk_widget_show (vbox3);
1242 gtk_container_add (GTK_CONTAINER (alignment1), vbox3);
1244 table1 = gtk_table_new (3, 2, FALSE);
1245 gtk_widget_show (table1);
1246 gtk_box_pack_start (GTK_BOX (vbox3), table1, TRUE, TRUE, 0);
1247 gtk_table_set_row_spacings (GTK_TABLE (table1), DIALOG_V_SPACING);
1248 gtk_table_set_col_spacings (GTK_TABLE (table1), DIALOG_H_SPACING);
1250 label4 = gtk_label_new (_("Search for:"));
1251 gtk_widget_show (label4);
1252 gtk_table_attach (GTK_TABLE (table1), label4, 0, 1, 0, 1,
1253 (GtkAttachOptions) (GTK_FILL),
1254 (GtkAttachOptions) (0), 0, 0);
1255 gtk_misc_set_alignment (GTK_MISC (label4), 0, 0.5);
1257 scope_text = gtk_combo_box_entry_new_text ();
1258 gtk_entry_set_activates_default(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(scope_text))), TRUE);
1259 gtk_widget_show (scope_text);
1260 gtk_table_attach (GTK_TABLE (table1), scope_text, 1, 2, 0, 1,
1261 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1262 (GtkAttachOptions) (GTK_FILL), 0, 0);
1264 label8 = gtk_label_new (_("Autonumber text in:"));
1265 gtk_widget_show (label8);
1266 gtk_table_attach (GTK_TABLE (table1), label8, 0, 1, 1, 2,
1267 (GtkAttachOptions) (GTK_FILL),
1268 (GtkAttachOptions) (0), 0, 0);
1269 gtk_misc_set_alignment (GTK_MISC (label8), 0, 0.5);
1271 label6 = gtk_label_new (_("Skip numbers found in:"));
1272 gtk_widget_show (label6);
1273 gtk_table_attach (GTK_TABLE (table1), label6, 0, 1, 2, 3,
1274 (GtkAttachOptions) (GTK_FILL),
1275 (GtkAttachOptions) (0), 0, 0);
1276 gtk_misc_set_alignment (GTK_MISC (label6), 0, 0.5);
1278 scope_number = gtk_combo_box_new_text ();
1279 gtk_widget_show (scope_number);
1280 gtk_table_attach (GTK_TABLE (table1), scope_number, 1, 2, 1, 2,
1281 (GtkAttachOptions) (GTK_FILL),
1282 (GtkAttachOptions) (GTK_FILL), 0, 0);
1283 gtk_combo_box_append_text (GTK_COMBO_BOX (scope_number), _("Selected objects"));
1284 gtk_combo_box_append_text (GTK_COMBO_BOX (scope_number), _("Current page"));
1285 gtk_combo_box_append_text (GTK_COMBO_BOX (scope_number), _("Whole hierarchy"));
1287 scope_skip = gtk_combo_box_new_text ();
1288 gtk_widget_show (scope_skip);
1289 gtk_table_attach (GTK_TABLE (table1), scope_skip, 1, 2, 2, 3,
1290 (GtkAttachOptions) (GTK_FILL),
1291 (GtkAttachOptions) (GTK_FILL), 0, 0);
1292 gtk_combo_box_append_text (GTK_COMBO_BOX (scope_skip), _("Selected objects"));
1293 gtk_combo_box_append_text (GTK_COMBO_BOX (scope_skip), _("Current page"));
1294 gtk_combo_box_append_text (GTK_COMBO_BOX (scope_skip), _("Whole hierarchy"));
1296 scope_overwrite = gtk_check_button_new_with_mnemonic (_("Overwrite existing numbers"));
1297 gtk_widget_show (scope_overwrite);
1298 gtk_box_pack_start (GTK_BOX (vbox3), scope_overwrite, FALSE, FALSE, 6);
1300 /* Options section */
1301 label3 = gtk_label_new (_("<b>Options</b>"));
1302 gtk_label_set_use_markup (GTK_LABEL (label3), TRUE);
1303 gtk_misc_set_alignment(GTK_MISC(label3), 0, 0);
1304 gtk_widget_show (label3);
1305 gtk_box_pack_start(GTK_BOX(vbox1), label3, TRUE, TRUE, 0);
1307 alignment3 = gtk_alignment_new (0, 0, 1, 1);
1308 gtk_widget_show (alignment3);
1309 gtk_box_pack_start(GTK_BOX(vbox1), alignment3, TRUE, TRUE, 0);
1310 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment3),
1311 0, 0, DIALOG_INDENTATION, 0);
1313 vbox4 = gtk_vbox_new (FALSE, 3);
1314 gtk_widget_show (vbox4);
1315 gtk_container_add (GTK_CONTAINER (alignment3), vbox4);
1317 table3 = gtk_table_new (2, 2, FALSE);
1318 gtk_widget_show (table3);
1319 gtk_box_pack_start (GTK_BOX (vbox4), table3, TRUE, TRUE, 0);
1320 gtk_table_set_row_spacings (GTK_TABLE (table3), DIALOG_V_SPACING);
1321 gtk_table_set_col_spacings (GTK_TABLE (table3), DIALOG_H_SPACING);
1323 label12 = gtk_label_new (_("Starting number:"));
1324 gtk_widget_show (label12);
1325 gtk_table_attach (GTK_TABLE (table3), label12, 0, 1, 0, 1,
1326 (GtkAttachOptions) (GTK_FILL),
1327 (GtkAttachOptions) (0), 0, 0);
1328 gtk_misc_set_alignment (GTK_MISC (label12), 0, 0.5);
1330 label13 = gtk_label_new (_("Sort order:"));
1331 gtk_widget_show (label13);
1332 gtk_table_attach (GTK_TABLE (table3), label13, 0, 1, 1, 2,
1333 (GtkAttachOptions) (GTK_FILL),
1334 (GtkAttachOptions) (0), 0, 0);
1335 gtk_misc_set_alignment (GTK_MISC (label13), 0, 0.5);
1337 opt_startnum_adj = gtk_adjustment_new (1, 0, 10000, 1, 10, 10);
1338 opt_startnum = gtk_spin_button_new (GTK_ADJUSTMENT (opt_startnum_adj), 1, 0);
1339 gtk_entry_set_activates_default(GTK_ENTRY(opt_startnum), TRUE);
1340 gtk_widget_show (opt_startnum);
1341 gtk_table_attach (GTK_TABLE (table3), opt_startnum, 1, 2, 0, 1,
1342 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
1343 (GtkAttachOptions) (0), 0, 0);
1345 sort_order = gtk_combo_box_new();
1346 gtk_widget_show (sort_order);
1347 gtk_table_attach (GTK_TABLE (table3), sort_order, 1, 2, 1, 2,
1348 (GtkAttachOptions) (GTK_FILL),
1349 (GtkAttachOptions) (GTK_FILL), 0, 0);
1351 opt_removenum = gtk_check_button_new_with_mnemonic (_("Remove numbers"));
1352 gtk_widget_show (opt_removenum);
1353 gtk_box_pack_start (GTK_BOX (vbox4), opt_removenum, FALSE, FALSE, 0);
1355 opt_slotting = gtk_check_button_new_with_mnemonic (_("Automatic slotting"));
1356 gtk_widget_show (opt_slotting);
1357 gtk_box_pack_start (GTK_BOX (vbox4), opt_slotting, FALSE, FALSE, 0);
1359 /* Store pointers to all widgets, for use by lookup_widget(). */
1360 GLADE_HOOKUP_OBJECT (autonumber_text, scope_text, "scope_text");
1361 GLADE_HOOKUP_OBJECT (autonumber_text, scope_number, "scope_number");
1362 GLADE_HOOKUP_OBJECT (autonumber_text, scope_skip, "scope_skip");
1363 GLADE_HOOKUP_OBJECT (autonumber_text, scope_overwrite, "scope_overwrite");
1364 GLADE_HOOKUP_OBJECT (autonumber_text, opt_startnum, "opt_startnum");
1365 GLADE_HOOKUP_OBJECT (autonumber_text, sort_order, "sort_order");
1366 GLADE_HOOKUP_OBJECT (autonumber_text, opt_removenum, "opt_removenum");
1367 GLADE_HOOKUP_OBJECT (autonumber_text, opt_slotting, "opt_slotting");
1369 return autonumber_text;
1372 /*! \brief Create or restore the autonumber text dialog
1374 * If the function is called the first time the dialog is created.
1375 * If the dialog is only in background it is moved to the foreground.
1377 * @param w_current Pointer to the top level struct
1379 void autonumber_text_dialog(GSCHEM_TOPLEVEL *w_current)
1381 static AUTONUMBER_TEXT *autotext = NULL;
1383 GtkWidget *opt_removenum = NULL;
1384 GtkWidget *sort_order = NULL;
1386 if(autotext == NULL) {
1387 /* first call of this function, init dialog structure */
1388 autotext=autonumber_init_state();
1391 /* set the GSCHEM_TOPLEVEL always. Can it be changed between the calls??? */
1392 autotext->w_current = w_current;
1394 if(autotext->dialog == NULL) {
1395 /* Dialog is not currently displayed - create it */
1397 autotext->dialog = autonumber_create_dialog(w_current);
1399 opt_removenum = lookup_widget(autotext->dialog, "opt_removenum");
1400 sort_order = lookup_widget(autotext->dialog, "sort_order");
1402 autonumber_sortorder_create(w_current, sort_order);
1404 gtk_dialog_set_default_response (GTK_DIALOG (autotext->dialog),
1405 GTK_RESPONSE_ACCEPT);
1407 gtk_signal_connect(GTK_OBJECT(autotext->dialog), "response",
1408 GTK_SIGNAL_FUNC(autonumber_text_response),
1409 autotext);
1411 gtk_signal_connect(GTK_OBJECT(opt_removenum),
1412 "clicked",
1413 GTK_SIGNAL_FUNC(autonumber_removenum_toggled),
1414 autotext);
1416 autonumber_set_state(autotext);
1418 gtk_widget_show_all(autotext->dialog);
1421 /* if the dialog is in the background or minimized: show it */
1422 gtk_window_present(GTK_WINDOW(autotext->dialog));