[dia] units: becomes GEnum with "instance" methods



commit 1bbc0e4faf799d85fe987af893c918abad4f08fc
Author: Zander Brown <zbrown gnome org>
Date:   Fri May 1 03:09:53 2020 +0100

    units: becomes GEnum with "instance" methods

 lib/diacolorselector.c      |  15 ++--
 lib/units.c                 | 177 ++++++++++++++++++++++++++++++++++++++++----
 lib/units.h                 |  54 ++++++++------
 lib/widgets.c               | 135 +++++++++++++++++++--------------
 objects/Misc/measure.c      |   4 +-
 objects/custom/shape_info.c |  30 ++++----
 6 files changed, 297 insertions(+), 118 deletions(-)
---
diff --git a/lib/diacolorselector.c b/lib/diacolorselector.c
index 88848f36..8700cf9b 100644
--- a/lib/diacolorselector.c
+++ b/lib/diacolorselector.c
@@ -42,8 +42,7 @@ enum {
   N_COL
 };
 
-struct _DiaColorSelector
-{
+struct _DiaColorSelector {
   GtkHBox         hbox;
 
   gboolean        use_alpha;
@@ -100,12 +99,12 @@ dia_color_selector_class_init (DiaColorSelectorClass *klass)
   object_class->finalize = dia_color_selector_finalize;
 
   dia_colorsel_signals[DIA_COLORSEL_VALUE_CHANGED]
-      = g_signal_new("value_changed",
-                    G_TYPE_FROM_CLASS(klass),
-                    G_SIGNAL_RUN_FIRST,
-                    0, NULL, NULL,
-                    g_cclosure_marshal_VOID__VOID,
-                    G_TYPE_NONE, 0);
+      = g_signal_new ("value_changed",
+                      G_TYPE_FROM_CLASS (klass),
+                      G_SIGNAL_RUN_FIRST,
+                      0, NULL, NULL,
+                      g_cclosure_marshal_VOID__VOID,
+                      G_TYPE_NONE, 0);
 }
 
 
diff --git a/lib/units.c b/lib/units.c
index 49804095..8069ae1f 100644
--- a/lib/units.c
+++ b/lib/units.c
@@ -1,19 +1,166 @@
+/* Dia -- an diagram creation/manipulation program
+ * Copyright (C) 1998 Alexander Larsson
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Copyright © 2020 Zander Brown <zbrown gnome org>
+ */
 
+#include "config.h"
 #include "units.h"
 
-/* from gnome-libs/libgnome/gnome-paper.c */
-const DIAVAR DiaUnitDef units[] =
+#include <glib/gi18n-lib.h>
+
+
+/**
+ * dia_unit_get_name:
+ * @self: the #DiaUnit
+ *
+ * Get the human readable localised name for @self
+ *
+ * Since: 0.98
+ */
+const char *
+dia_unit_get_name (DiaUnit self)
+{
+  switch (self) {
+    case DIA_UNIT_CENTIMETER:
+      return _("Centimeter");
+    case DIA_UNIT_DECIMETER:
+      return _("Decimeter");
+    case DIA_UNIT_FEET:
+      return _("Feet");
+    case DIA_UNIT_INCH:
+      return _("Inch");
+    case DIA_UNIT_METER:
+      return _("Meter");
+    case DIA_UNIT_MILLIMETER:
+      return _("Millimeter");
+    case DIA_UNIT_POINT:
+      return _("Point");
+    case DIA_UNIT_PICA:
+      return _("Pica");
+    case DIA_LAST_UNIT:
+    default:
+      g_return_val_if_reached (NULL);
+  }
+}
+
+
+/**
+ * dia_unit_get_symbol:
+ * @self: the #DiaUnit
+ *
+ * Get the symbol for @self, e.g. cm
+ *
+ * Since: 0.98
+ */
+const char *
+dia_unit_get_symbol (DiaUnit self)
+{
+  switch (self) {
+    case DIA_UNIT_CENTIMETER:
+      return "cm";
+    case DIA_UNIT_DECIMETER:
+      return "dm";
+    case DIA_UNIT_FEET:
+      return "ft";
+    case DIA_UNIT_INCH:
+      return "in";
+    case DIA_UNIT_METER:
+      return "m";
+    case DIA_UNIT_MILLIMETER:
+      return "mm";
+    case DIA_UNIT_POINT:
+      return "pt";
+    case DIA_UNIT_PICA:
+      return "pi";
+    case DIA_LAST_UNIT:
+    default:
+      g_return_val_if_reached (NULL);
+  }
+}
+
+
+/**
+ * dia_unit_get_factor:
+ * @self: the #DiaUnit
+ *
+ * The number of %DIA_UNIT_POINT per @self
+ *
+ * Since: 0.98
+ */
+double
+dia_unit_get_factor (DiaUnit self)
+{
+  switch (self) {
+    case DIA_UNIT_CENTIMETER:
+      return 28.346457;
+    case DIA_UNIT_DECIMETER:
+      return 283.46457;
+    case DIA_UNIT_FEET:
+      return 864;
+    case DIA_UNIT_INCH:
+      return 72;
+    case DIA_UNIT_METER:
+      return 2834.6457;
+    case DIA_UNIT_MILLIMETER:
+      return 2.8346457;
+    case DIA_UNIT_POINT:
+      return 1;
+    case DIA_UNIT_PICA:
+      return 12;
+    case DIA_LAST_UNIT:
+    default:
+      g_return_val_if_reached (-1);
+  }
+}
+
+
+/**
+ * dia_unit_get_digits:
+ * @self: the #DiaUnit
+ *
+ * Number of digits after the decimal separator
+ *
+ * Since: 0.98
+ */
+int
+dia_unit_get_digits (DiaUnit self)
 {
-  /* XXX does anyone *really* measure paper size in feet?  meters? */
-
-  /* human name, abreviation, points per unit */
-  { "Centimeter", "cm", 28.346457, 2 },
-  { "Decimeter",  "dm", 283.46457, 3 },
-  { "Feet",       "ft", 864, 4 },
-  { "Inch",       "in", 72, 3 },
-  { "Meter",      "m",  2834.6457, 4 },
-  { "Millimeter", "mm", 2.8346457, 2 },
-  { "Point",      "pt", 1, 2 },
-  { "Pica",       "pi", 12, 2 },
-  { 0 }
-};
+  switch (self) {
+    case DIA_UNIT_CENTIMETER:
+      return 2;
+    case DIA_UNIT_DECIMETER:
+      return 3;
+    case DIA_UNIT_FEET:
+      return 4;
+    case DIA_UNIT_INCH:
+      return 3;
+    case DIA_UNIT_METER:
+      return 4;
+    case DIA_UNIT_MILLIMETER:
+      return 2;
+    case DIA_UNIT_POINT:
+      return 2;
+    case DIA_UNIT_PICA:
+      return 2;
+    case DIA_LAST_UNIT:
+    default:
+      g_return_val_if_reached (-1);
+  }
+}
diff --git a/lib/units.h b/lib/units.h
index 49f1db6a..5943b931 100644
--- a/lib/units.h
+++ b/lib/units.h
@@ -15,31 +15,39 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
-#ifndef UNITS_H
-#define UNITS_H
 
-#include <config.h>
-#include "diavar.h"
+#pragma once
 
-typedef enum {
-  DIA_UNIT_CENTIMETER,
-  DIA_UNIT_DECIMETER,
-  DIA_UNIT_FEET,
-  DIA_UNIT_INCH,
-  DIA_UNIT_METER,
-  DIA_UNIT_MILLIMETER,
-  DIA_UNIT_POINT,
-  DIA_UNIT_PICA,
-} DiaUnit;
+#include "dia-lib-enums.h"
 
-typedef struct _DiaUnitDef DiaUnitDef;
-struct _DiaUnitDef {
-  char* name;
-  char* unit;
-  float factor;
-  int digits; /** Number of digits after the decimal separator */
-};
+/**
+ * DiaUnit:
+ * @DIA_UNIT_CENTIMETER: 0.01 metre
+ * @DIA_UNIT_DECIMETER: 0.1 metre
+ * @DIA_UNIT_FEET: 0.3048 metre (12 inch)
+ * @DIA_UNIT_INCH: 0.0254 metre
+ * @DIA_UNIT_METER: 1 metre
+ * @DIA_UNIT_MILLIMETER: 0.001 metre
+ * @DIA_UNIT_POINT: 1 pixel
+ * @DIA_UNIT_PICA: 12 pixels
+ * @DIA_LAST_UNIT: Dummy value indicating the number of units
+ *
+ * Since: dawn-of-time
+ */
+typedef enum /*< enum,prefix=DIA >*/ {
+  DIA_UNIT_CENTIMETER, /*< nick=centimetre >*/
+  DIA_UNIT_DECIMETER,  /*< nick=decimetre >*/
+  DIA_UNIT_FEET,       /*< nick=feet >*/
+  DIA_UNIT_INCH,       /*< nick=inch >*/
+  DIA_UNIT_METER,      /*< nick=metre >*/
+  DIA_UNIT_MILLIMETER, /*< nick=millimetre >*/
+  DIA_UNIT_POINT,      /*< nick=point >*/
+  DIA_UNIT_PICA,       /*< nick=pica >*/
+  DIA_LAST_UNIT        /*< skip >*/
+} DiaUnit;
 
-extern const DIAVAR DiaUnitDef units[];
 
-#endif /* UNITS_H */
+const char *dia_unit_get_name   (DiaUnit self);
+const char *dia_unit_get_symbol (DiaUnit self);
+double      dia_unit_get_factor (DiaUnit self);
+int         dia_unit_get_digits (DiaUnit self);
diff --git a/lib/widgets.c b/lib/widgets.c
index 68f036e4..c9cc3d95 100644
--- a/lib/widgets.c
+++ b/lib/widgets.c
@@ -520,10 +520,15 @@ dia_file_selector_get_file(DiaFileSelector *fs)
 
 /************* DiaUnitSpinner: ***************/
 
-/** A Spinner that allows a 'favored' unit to display in.  External access
- *  to the value still happens in cm, but display is in the favored unit.
- *  Internally, the value is kept in the favored unit to a) allow proper
- *  limits, and b) avoid rounding problems while editing.
+/**
+ * DiaUnitSpinner:
+ *
+ * A Spinner that allows a 'favoured' unit to display in. External access
+ * to the value still happens in cm, but display is in the favored unit.
+ * Internally, the value is kept in the favored unit to a) allow proper
+ * limits, and b) avoid rounding problems while editing.
+ *
+ * Since: dawn-of-time
  */
 
 static void dia_unit_spinner_init(DiaUnitSpinner *self);
@@ -580,8 +585,8 @@ dia_unit_spinner_new(GtkAdjustment *adjustment, DiaUnit adj_unit)
   gtk_entry_set_activates_default(GTK_ENTRY(self), TRUE);
   self->unit_num = adj_unit;
 
-  gtk_spin_button_configure(GTK_SPIN_BUTTON(self),
-                            adjustment, 0.0, units[adj_unit].digits);
+  gtk_spin_button_configure (GTK_SPIN_BUTTON (self),
+                             adjustment, 0.0, dia_unit_get_digits (adj_unit));
 
   g_signal_connect(GTK_SPIN_BUTTON(self), "output",
                    G_CALLBACK(dia_unit_spinner_output),
@@ -597,22 +602,26 @@ dia_unit_spinner_new(GtkAdjustment *adjustment, DiaUnit adj_unit)
 static gboolean
 dia_unit_spinner_input (DiaUnitSpinner *self, double *value)
 {
-  float val, factor = 1.0;
+  double val, factor = 1.0;
   char *extra = NULL;
 
-  val = g_strtod(gtk_entry_get_text(GTK_ENTRY(self)), &extra);
+  val = g_strtod (gtk_entry_get_text (GTK_ENTRY (self)), &extra);
 
   /* get rid of extra white space after number */
-  while (*extra && g_ascii_isspace(*extra)) extra++;
-  if (*extra) {
-    int i;
+  while (*extra && g_ascii_isspace (*extra)) {
+    extra++;
+  }
 
-    for (i = 0; units[i].name != NULL; i++)
-      if (!g_ascii_strcasecmp(units[i].unit, extra)) {
-       factor = units[i].factor / units[self->unit_num].factor;
-       break;
+  if (*extra) {
+    for (int i = 0; i < DIA_LAST_UNIT; i++) {
+      if (!g_ascii_strcasecmp (dia_unit_get_symbol (i), extra)) {
+        factor = dia_unit_get_factor (i) /
+                  dia_unit_get_factor (self->unit_num);
+        break;
       }
+    }
   }
+
   /* convert to prefered units */
   val *= factor;
 
@@ -623,70 +632,88 @@ dia_unit_spinner_input (DiaUnitSpinner *self, double *value)
   return TRUE;
 }
 
-static gboolean dia_unit_spinner_output(DiaUnitSpinner *self)
+
+static gboolean
+dia_unit_spinner_output (DiaUnitSpinner *self)
 {
   char buf[256];
+  GtkSpinButton *sbutton = GTK_SPIN_BUTTON (self);
+  GtkAdjustment *adjustment = gtk_spin_button_get_adjustment (sbutton);
 
-  GtkSpinButton *sbutton = GTK_SPIN_BUTTON(self);
-  GtkAdjustment *adjustment = gtk_spin_button_get_adjustment(sbutton);
-
-  g_snprintf(buf, sizeof(buf), "%0.*f %s",
-             gtk_spin_button_get_digits(sbutton),
-             gtk_adjustment_get_value(adjustment),
-             units[self->unit_num].unit);
-  gtk_entry_set_text(GTK_ENTRY(self), buf);
+  g_snprintf (buf,
+              sizeof(buf),
+              "%0.*f %s",
+              gtk_spin_button_get_digits(sbutton),
+              gtk_adjustment_get_value(adjustment),
+              dia_unit_get_symbol (self->unit_num));
+  gtk_entry_set_text (GTK_ENTRY(self), buf);
 
   /* Return true, so that the default output function is not invoked. */
   return TRUE;
 }
 
-/** Set the value (in cm).
- * */
+
+/**
+ * dia_unit_spinner_set_value:
+ * @self: the DiaUnitSpinner
+ * @val: value in %DIA_UNIT_CENTIMETER
+ *
+ * Set the value (in cm).
+ *
+ * Since: dawn-of-time
+ */
 void
-dia_unit_spinner_set_value(DiaUnitSpinner *self, gdouble val)
+dia_unit_spinner_set_value (DiaUnitSpinner *self, double val)
 {
   GtkSpinButton *sbutton = GTK_SPIN_BUTTON(self);
 
-  gtk_spin_button_set_value(sbutton,
-                            val /
-                            (units[self->unit_num].factor /
-                             units[DIA_UNIT_CENTIMETER].factor));
+  gtk_spin_button_set_value (sbutton,
+                             val /
+                             (dia_unit_get_factor (self->unit_num) /
+                              (dia_unit_get_factor (DIA_UNIT_CENTIMETER))));
 }
 
-/** Get the value (in cm) */
-gdouble
-dia_unit_spinner_get_value(DiaUnitSpinner *self)
+
+/**
+ * dia_unit_spinner_get_value:
+ * @self: the DiaUnitSpinner
+ *
+ * Get the value (in cm)
+ *
+ * Returns: The value in %DIA_UNIT_CENTIMETER
+ *
+ * Since: dawn-of-time
+ */
+double
+dia_unit_spinner_get_value (DiaUnitSpinner *self)
 {
   GtkSpinButton *sbutton = GTK_SPIN_BUTTON(self);
 
-  return gtk_spin_button_get_value(sbutton) *
-      (units[self->unit_num].factor / units[DIA_UNIT_CENTIMETER].factor);
+  return gtk_spin_button_get_value (sbutton) *
+                    (dia_unit_get_factor (self->unit_num) /
+                     dia_unit_get_factor (DIA_UNIT_CENTIMETER));
 }
 
-/* Must manipulate the limit values through this to also consider unit.
+
+/**
+ * dia_unit_spinner_set_upper:
+ * @self: the DiaUnitSpinner
+ * @val: value in %DIA_UNIT_CENTIMETER
+ *
+ * Must manipulate the limit values through this to also consider unit.
+ *
  * Given value is in centimeter.
+ *
+ * Since: dawn-of-time
  */
 void
-dia_unit_spinner_set_upper (DiaUnitSpinner *self, gdouble val)
+dia_unit_spinner_set_upper (DiaUnitSpinner *self, double val)
 {
-  val /= (units[self->unit_num].factor / units[DIA_UNIT_CENTIMETER].factor);
+  val /= (dia_unit_get_factor (self->unit_num) /
+          dia_unit_get_factor (DIA_UNIT_CENTIMETER));
 
   gtk_adjustment_set_upper (
-    gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(self)), val);
-}
-GList *
-get_units_name_list(void)
-{
-  int i;
-  static GList *name_list = NULL;
-
-  if (name_list == NULL) {
-    for (i = 0; units[i].name != NULL; i++) {
-      name_list = g_list_append(name_list, units[i].name);
-    }
-  }
-
-  return name_list;
+    gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (self)), val);
 }
 
 
diff --git a/objects/Misc/measure.c b/objects/Misc/measure.c
index 56f1a77d..1305bf15 100644
--- a/objects/Misc/measure.c
+++ b/objects/Misc/measure.c
@@ -229,8 +229,8 @@ measure_update_data (Measure *measure)
   g_clear_pointer (&measure->name, g_free);
   value = distance_point_point (&ends[0], &ends[1]);
   value *= measure->scale;
-  value *= (28.346457 / units[measure->unit].factor);
-  measure->name = g_strdup_printf ("%.*g %s", measure->precision, value, units[measure->unit].unit);
+  value *= (28.346457 / dia_unit_get_factor (measure->unit));
+  measure->name = g_strdup_printf ("%.*g %s", measure->precision, value, dia_unit_get_symbol 
(measure->unit));
 
   ascent = dia_font_ascent (measure->name, measure->font, measure->font_height);
   width = dia_font_string_width (measure->name, measure->font, measure->font_height);
diff --git a/objects/custom/shape_info.c b/objects/custom/shape_info.c
index a3359c95..5549e39e 100644
--- a/objects/custom/shape_info.c
+++ b/objects/custom/shape_info.c
@@ -896,35 +896,33 @@ load_shape_info (const gchar *filename, ShapeInfo *preload)
        }
        xmlFree(tmp);
       }
-    } else if (node->ns == shape_ns && (!xmlStrcmp(node->name, (const xmlChar *)"default-width") || 
!xmlStrcmp(node->name, (const xmlChar *)"default-height"))) {
-
-      int j = 0;
-      DiaUnitDef ud;
-
-      gdouble val = 0.0;
-
+    } else if (node->ns == shape_ns && (!xmlStrcmp(node->name, (const xmlChar *)"default-width") || 
!xmlStrcmp(node->name, (const xmlChar *) "default-height"))) {
+      double val = 0.0;
       int unit_ssize = 0;
       int ssize = 0;
-      tmp = (gchar *) xmlNodeGetContent(node);
-      ssize = strlen(tmp);
 
-      val = g_ascii_strtod(tmp, NULL);
+      tmp = (char *) xmlNodeGetContent (node);
+      ssize = strlen (tmp);
+
+      val = g_ascii_strtod (tmp, NULL);
 
-      for (ud = units[j]; ud.name; ud = units[++j]) {
-        unit_ssize = strlen(ud.unit);
-        if (ssize > unit_ssize && !strcmp(tmp+(ssize-unit_ssize), ud.unit)) {
-          val *= (ud.factor / 28.346457);
+      for (int j = 0; j < DIA_LAST_UNIT; j++) {
+        unit_ssize = strlen (dia_unit_get_symbol (j));
+        if (ssize > unit_ssize &&
+            !g_strcmp0 (tmp + (ssize - unit_ssize),
+                        dia_unit_get_symbol (j))) {
+          val *= (dia_unit_get_factor (j) / 28.346457);
           break;
         }
       }
 
-      if (!xmlStrcmp(node->name, (const xmlChar *)"default-width")) {
+      if (!xmlStrcmp (node->name, (const xmlChar *) "default-width")) {
         info->default_width = val;
       } else {
         info->default_height = val;
       }
 
-      xmlFree(tmp);
+      xmlFree (tmp);
     } else if (node->ns == svg_ns && !xmlStrcmp(node->name, (const xmlChar *)"svg")) {
       DiaSvgStyle s = {
        1.0, DIA_SVG_COLOUR_FOREGROUND, 1.0, DIA_SVG_COLOUR_NONE, 1.0,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]