Bump gEDA version
[geda-gaf.git] / gschem / src / gschem_options.c
blob75073ade97496bd139ecae274ea4aa6536ce75e6
1 /* gEDA - GPL Electronic Design Automation
2 * gschem - gEDA Schematic Capture
3 * Copyright (C) 1998-2010 Ales Hvezda
4 * Copyright (C) 1998-2020 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 /*!
21 * \file gschem_options.c
23 * \brief
26 #include <config.h>
28 #include <stdio.h>
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32 #ifdef HAVE_STRING_H
33 #include <string.h>
34 #endif
36 #include "gschem.h"
37 #include "actions.decl.x"
41 /*! \private
43 enum
45 PROP_GSCHEM_TOPLEVEL = 1,
46 PROP_GRID_MODE,
47 PROP_SHOW_ORIGIN,
48 PROP_MAGNETIC_NET_MODE,
49 PROP_NET_RUBBER_BAND_MODE,
50 PROP_SNAP_MODE,
51 PROP_SNAP_SIZE
56 static void
57 class_init (GschemOptionsClass *klass);
59 static void
60 get_property (GObject *object, guint param_id, GValue *value, GParamSpec *pspec);
62 static void
63 instance_init (GschemOptions *adapter);
65 static void
66 set_property (GObject *object, guint param_id, const GValue *value, GParamSpec *pspec);
70 /*! \brief Cycle grid mode to the next option
72 * \param options These options
74 void
75 gschem_options_cycle_grid_mode (GschemOptions *options)
77 GRID_MODE next_grid_mode;
79 g_return_if_fail (options != NULL);
81 next_grid_mode = (options->grid_mode + 1) % GRID_MODE_COUNT;
83 gschem_options_set_grid_mode (options, next_grid_mode);
88 /*! \brief Cycle show origin to the next option
90 * \param options These options
92 void
93 gschem_options_cycle_show_origin (GschemOptions *options)
95 gboolean next_show_origin;
97 g_return_if_fail (options != NULL);
99 next_show_origin = !options->show_origin;
101 gschem_options_set_show_origin (options, next_show_origin);
106 /*! \brief Cycle magnetic net mode to the next option
108 * \param options These options
110 void
111 gschem_options_cycle_magnetic_net_mode (GschemOptions *options)
113 gboolean next_magnetic_net_mode;
115 g_return_if_fail (options != NULL);
117 next_magnetic_net_mode = !options->magnetic_net_mode;
119 gschem_options_set_magnetic_net_mode (options, next_magnetic_net_mode);
124 /*! \brief Cycle net rubber band mode to the next option
126 * \param options These options
128 void
129 gschem_options_cycle_net_rubber_band_mode (GschemOptions *options)
131 gboolean next_net_rubber_band_mode;
133 g_return_if_fail (options != NULL);
135 next_net_rubber_band_mode = !options->net_rubber_band_mode;
137 gschem_options_set_net_rubber_band_mode (options, next_net_rubber_band_mode);
142 /*! \brief Cycle snap mode to the next option
144 * \param options These options
146 void
147 gschem_options_cycle_snap_mode (GschemOptions *options)
149 SNAP_STATE next_snap_mode;
151 g_return_if_fail (options != NULL);
153 /* toggle to the next snap state */
154 next_snap_mode = (options->snap_mode + 1) % SNAP_STATE_COUNT;
156 gschem_options_set_snap_mode (options, next_snap_mode);
161 /*! \brief Get the grid mode
163 * \param [in] options These options
164 * \return The grid mode
166 GRID_MODE
167 gschem_options_get_grid_mode (GschemOptions *options)
169 g_return_val_if_fail (options != NULL, GRID_MODE_MESH);
171 return options->grid_mode;
176 /*! \brief Get whether to show origin
178 * \param [in] options These options
179 * \return Whether to show the origin
181 gboolean
182 gschem_options_get_show_origin (GschemOptions *options)
184 g_return_val_if_fail (options != NULL, DEFAULT_SHOW_ORIGIN);
186 return options->show_origin;
191 /*! \brief Get the magnetic net mode
193 * \param [in] options These options
194 * \return The magnetic net mode
196 gboolean
197 gschem_options_get_magnetic_net_mode (GschemOptions *options)
199 g_return_val_if_fail (options != NULL, DEFAULT_MAGNETIC_NET_MODE);
201 return options->magnetic_net_mode;
206 /*! \brief Get the net rubber band mode
208 * \param [in] options These options
209 * \return The net rubber band mode
211 gboolean
212 gschem_options_get_net_rubber_band_mode (GschemOptions *options)
214 g_return_val_if_fail (options != NULL, DEFAULT_NET_RUBBER_BAND_MODE);
216 return options->net_rubber_band_mode;
221 /*! \brief Get the snap mode
223 * \param [in] options These options
224 * \return The snap mode
226 SNAP_STATE
227 gschem_options_get_snap_mode (GschemOptions *options)
229 g_return_val_if_fail (options != NULL, SNAP_GRID);
231 return options->snap_mode;
236 /*! \brief Get the snap size
238 * \param [in] options These options
239 * \return The snap size
242 gschem_options_get_snap_size (GschemOptions *options)
244 g_return_val_if_fail (options != NULL, DEFAULT_SNAP_SIZE);
246 return options->snap_size;
251 /*! \brief Get/register GschemSelection type.
253 GType
254 gschem_options_get_type ()
256 static GType type = 0;
258 if (type == 0) {
259 static const GTypeInfo info = {
260 sizeof(GschemOptionsClass),
261 NULL, /* base_init */
262 NULL, /* base_finalize */
263 (GClassInitFunc) class_init,
264 NULL, /* class_finalize */
265 NULL, /* class_data */
266 sizeof(GschemOptions),
267 0, /* n_preallocs */
268 (GInstanceInitFunc) instance_init,
271 type = g_type_register_static (G_TYPE_OBJECT, "GschemOptions", &info, 0);
274 return type;
279 /*! \brief Create a new gschem options
281 * \returns A new set of options
283 GschemOptions*
284 gschem_options_new (GschemToplevel *w_current)
286 return g_object_new (GSCHEM_TYPE_OPTIONS,
287 "gschem-toplevel", w_current,
288 NULL);
292 /*! \brief Scale the snap size down
294 * \param [in] options These options
296 void
297 gschem_options_scale_snap_down (GschemOptions *options)
299 g_return_if_fail (options != NULL);
301 if ((options->snap_size % 2) == 0) {
302 gschem_options_set_snap_size (options, options->snap_size / 2);
308 /*! \brief Scale the snap size up
310 * \param [in] options These options
312 void
313 gschem_options_scale_snap_up (GschemOptions *options)
315 g_return_if_fail (options != NULL);
317 gschem_options_set_snap_size (options, options->snap_size * 2);
322 /*! \brief Set the grid mode
324 * If the grid mode is invalid the default grid mode is set.
326 * \param [in] options These options
327 * \param [in] grid_mode The grid mode
329 void
330 gschem_options_set_grid_mode (GschemOptions *options, GRID_MODE grid_mode)
332 g_return_if_fail (options != NULL);
334 switch (grid_mode) {
335 case GRID_MODE_NONE:
336 case GRID_MODE_DOTS:
337 case GRID_MODE_MESH:
338 options->grid_mode = grid_mode;
339 break;
340 default:
341 options->grid_mode = default_grid_mode;
342 break;
345 gschem_action_set_active (action_options_grid_none,
346 options->grid_mode == GRID_MODE_NONE,
347 options->w_current);
348 gschem_action_set_active (action_options_grid_dots,
349 options->grid_mode == GRID_MODE_DOTS,
350 options->w_current);
351 gschem_action_set_active (action_options_grid_mesh,
352 options->grid_mode == GRID_MODE_MESH,
353 options->w_current);
355 g_object_notify (G_OBJECT (options), "grid-mode");
360 /*! \brief Set whether to show origin
362 * \param [in] options These options
363 * \param [in] show_origin Whether to show the origin
365 void
366 gschem_options_set_show_origin (GschemOptions *options, gboolean show_origin)
368 g_return_if_fail (options != NULL);
370 options->show_origin = show_origin;
372 gschem_action_set_active (action_view_show_origin,
373 options->show_origin,
374 options->w_current);
376 g_object_notify (G_OBJECT (options), "show-origin");
381 /*! \brief Set the magnetic net mode
383 * \param [in] options These options
384 * \param [in] enabled Magnetic net mode
386 void
387 gschem_options_set_magnetic_net_mode (GschemOptions *options, gboolean enabled)
389 g_return_if_fail (options != NULL);
391 options->magnetic_net_mode = enabled;
393 gschem_action_set_active (action_options_magneticnet,
394 options->magnetic_net_mode,
395 options->w_current);
397 g_object_notify (G_OBJECT (options), "magnetic-net-mode");
402 /*! \brief Set the net rubber band mode
404 * Sets whether nets rubberband as you move them (or connecting comps)
406 * \param [in] options These options
407 * \param [in] enabled Net rubber band mode
409 void
410 gschem_options_set_net_rubber_band_mode (GschemOptions *options, gboolean enabled)
412 g_return_if_fail (options != NULL);
414 options->net_rubber_band_mode = enabled;
416 gschem_action_set_active (action_options_rubberband,
417 options->net_rubber_band_mode,
418 options->w_current);
420 g_object_notify (G_OBJECT (options), "net-rubber-band-mode");
425 /*! \brief Set the snap mode
427 * \param [in] options These options
428 * \param [in] snap_mode The snap mode
430 void
431 gschem_options_set_snap_mode (GschemOptions *options, SNAP_STATE snap_mode)
433 g_return_if_fail (options != NULL);
435 options->snap_mode = snap_mode;
437 gschem_action_set_active (action_options_snap_off,
438 options->snap_mode == SNAP_OFF,
439 options->w_current);
440 gschem_action_set_active (action_options_snap_grid,
441 options->snap_mode == SNAP_GRID,
442 options->w_current);
443 gschem_action_set_active (action_options_snap_resnap,
444 options->snap_mode == SNAP_RESNAP,
445 options->w_current);
447 g_object_notify (G_OBJECT (options), "snap-mode");
452 /*! \brief Set the snap size
454 * If the snap size is outside the range of valid values, the value will be
455 * assigned to the closest limit.
457 * \param [in] options These options
458 * \param [in] snap_size The snap size
460 void
461 gschem_options_set_snap_size (GschemOptions *options, int snap_size)
463 g_return_if_fail (options != NULL);
465 if (snap_size < MINIMUM_SNAP_SIZE) {
466 snap_size = MINIMUM_SNAP_SIZE;
469 if (snap_size > MAXIMUM_SNAP_SIZE) {
470 snap_size = MAXIMUM_SNAP_SIZE;
473 options->snap_size = snap_size;
475 g_object_notify (G_OBJECT (options), "snap-size");
480 /*! \private
481 * \brief Initialize gschem options class
483 * \param [in] klass The class for the gschem options
485 static void
486 class_init (GschemOptionsClass *klass)
488 G_OBJECT_CLASS (klass)->get_property = get_property;
489 G_OBJECT_CLASS (klass)->set_property = set_property;
491 g_object_class_install_property (G_OBJECT_CLASS (klass),
492 PROP_GSCHEM_TOPLEVEL,
493 g_param_spec_pointer ("gschem-toplevel",
494 "gschem Toplevel",
495 "gschem Toplevel",
496 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
498 g_object_class_install_property (G_OBJECT_CLASS (klass),
499 PROP_GRID_MODE,
500 g_param_spec_int ("grid-mode",
501 "Grid Mode",
502 "Grid Mode",
504 (GRID_MODE_COUNT - 1),
505 DEFAULT_GRID_MODE,
506 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
508 g_object_class_install_property (G_OBJECT_CLASS (klass),
509 PROP_SHOW_ORIGIN,
510 g_param_spec_boolean ("show-origin",
511 "Show Origin",
512 "Show Origin",
513 DEFAULT_SHOW_ORIGIN,
514 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
516 g_object_class_install_property (G_OBJECT_CLASS (klass),
517 PROP_MAGNETIC_NET_MODE,
518 g_param_spec_boolean ("magnetic-net-mode",
519 "Magnetic Net Mode",
520 "magnetic Net Mode",
521 DEFAULT_MAGNETIC_NET_MODE,
522 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
524 g_object_class_install_property (G_OBJECT_CLASS (klass),
525 PROP_NET_RUBBER_BAND_MODE,
526 g_param_spec_boolean ("net-rubber-band-mode",
527 "Net Rubber Band Mode",
528 "Net Rubber Band Mode",
529 DEFAULT_NET_RUBBER_BAND_MODE,
530 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
532 g_object_class_install_property (G_OBJECT_CLASS (klass),
533 PROP_SNAP_MODE,
534 g_param_spec_int ("snap-mode",
535 "Snap Mode",
536 "Snap Mode",
538 SNAP_STATE_COUNT - 1,
539 SNAP_GRID,
540 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
542 g_object_class_install_property (G_OBJECT_CLASS (klass),
543 PROP_SNAP_SIZE,
544 g_param_spec_int ("snap-size",
545 "Snap Size",
546 "Snap Size",
547 MINIMUM_SNAP_SIZE,
548 MAXIMUM_SNAP_SIZE,
549 DEFAULT_SNAP_SIZE,
550 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
555 /*! \private
556 * \brief Get a property
558 * \param [in] object
559 * \param [in] param_id
560 * \param [in,out] value
561 * \param [in] pspec
563 static void
564 get_property (GObject *object, guint param_id, GValue *value, GParamSpec *pspec)
566 GschemOptions *options = GSCHEM_OPTIONS (object);
568 switch (param_id) {
569 case PROP_GSCHEM_TOPLEVEL:
570 g_value_set_pointer (value, options->w_current);
571 break;
573 case PROP_GRID_MODE:
574 g_value_set_int (value, gschem_options_get_grid_mode (options));
575 break;
577 case PROP_SHOW_ORIGIN:
578 g_value_set_boolean (value, gschem_options_get_show_origin (options));
579 break;
581 case PROP_MAGNETIC_NET_MODE:
582 g_value_set_boolean (value, gschem_options_get_magnetic_net_mode (options));
583 break;
585 case PROP_NET_RUBBER_BAND_MODE:
586 g_value_set_boolean (value, gschem_options_get_net_rubber_band_mode (options));
587 break;
589 case PROP_SNAP_MODE:
590 g_value_set_int (value, gschem_options_get_snap_mode (options));
591 break;
593 case PROP_SNAP_SIZE:
594 g_value_set_int (value, gschem_options_get_snap_size (options));
595 break;
597 default:
598 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
604 /*! \brief Initialize GschemOptions instance
606 * \param [in,out] selection
608 static void
609 instance_init (GschemOptions *options)
615 /*! \brief Set a property
617 * \param [in,out] object
618 * \param [in] param_id
619 * \param [in] value
620 * \param [in] pspec
622 static void
623 set_property (GObject *object, guint param_id, const GValue *value, GParamSpec *pspec)
625 GschemOptions *options = GSCHEM_OPTIONS (object);
627 switch (param_id) {
628 case PROP_GSCHEM_TOPLEVEL:
629 options->w_current = GSCHEM_TOPLEVEL (g_value_get_pointer (value));
630 break;
632 case PROP_GRID_MODE:
633 gschem_options_set_grid_mode (options, g_value_get_int (value));
634 break;
636 case PROP_SHOW_ORIGIN:
637 gschem_options_set_show_origin (options, g_value_get_boolean (value));
638 break;
640 case PROP_MAGNETIC_NET_MODE:
641 gschem_options_set_magnetic_net_mode (options, g_value_get_boolean (value));
642 break;
644 case PROP_NET_RUBBER_BAND_MODE:
645 gschem_options_set_net_rubber_band_mode (options, g_value_get_boolean (value));
646 break;
648 case PROP_SNAP_MODE:
649 gschem_options_set_snap_mode (options, g_value_get_int (value));
650 break;
652 case PROP_SNAP_SIZE:
653 gschem_options_set_snap_size (options, g_value_get_int (value));
654 break;
656 default:
657 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);