[dia] choosers: continue the great modernisation



commit 0e35178788831d18cb1d819c6aa65a9ff56fcba9
Author: Zander Brown <zbrown gnome org>
Date:   Thu Sep 23 00:09:18 2021 +0100

    choosers: continue the great modernisation
    
    Split the previews to their own files, in a magic future gtk4 world
    these will replace the cell renderers

 app/toolbox.c                                  |   4 +-
 lib/{diaarrowchooser.c => dia-arrow-chooser.c} | 242 +++--------------
 lib/{diaarrowchooser.h => dia-arrow-chooser.h} |  38 +--
 lib/dia-arrow-preview.c                        | 220 +++++++++++++++
 lib/dia-arrow-preview.h                        |  40 +++
 lib/dia-line-chooser.c                         | 229 ++++++++++++++++
 lib/dia-line-chooser.h                         |  45 ++++
 lib/dia-line-preview.c                         | 158 +++++++++++
 lib/dia-line-preview.h                         |  37 +++
 lib/dialinechooser.c                           | 354 -------------------------
 lib/dialinechooser.h                           | 100 -------
 lib/libdia.def                                 |  15 +-
 lib/meson.build                                |  10 +-
 lib/prop_attr.c                                |   1 -
 lib/utils.c                                    |  13 +-
 lib/utils.h                                    |  15 +-
 16 files changed, 798 insertions(+), 723 deletions(-)
---
diff --git a/app/toolbox.c b/app/toolbox.c
index c3a4d49d2..ef6f6f2c8 100644
--- a/app/toolbox.c
+++ b/app/toolbox.c
@@ -22,12 +22,12 @@
 #include "gtkwrapbox.h"
 #include "gtkhwrapbox.h"
 
-#include "diaarrowchooser.h"
-#include "dialinechooser.h"
 #include "attributes.h"
 #include "sheet.h"
+#include "dia-arrow-chooser.h"
 #include "dia-colour-area.h"
 #include "dia-line-width-area.h"
+#include "dia-line-chooser.h"
 #include "intl.h"
 #include "message.h"
 #include "object.h"
diff --git a/lib/diaarrowchooser.c b/lib/dia-arrow-chooser.c
similarity index 61%
rename from lib/diaarrowchooser.c
rename to lib/dia-arrow-chooser.c
index bcfe1b81c..3f8a2385c 100644
--- a/lib/diaarrowchooser.c
+++ b/lib/dia-arrow-chooser.c
@@ -17,212 +17,38 @@
  * 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
  */
 
-#include <config.h>
+#include "config.h"
 
+#include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
-#include "intl.h"
+
 #include "dia-arrow-selector.h"
-#include "diaarrowchooser.h"
-#include "renderer/diacairo.h"
+#include "dia-arrow-chooser.h"
+#include "dia-arrow-preview.h"
+#include "utils.h"
 
 /**
- * SECTION:diaarrowchooser
+ * DiaArrowChooser:
  *
  * A widget to choose arrowhead. This only select arrowhead, not width and height.
  */
+struct _DiaArrowChooser {
+  GtkButton button;
+  DiaArrowPreview *preview;
+  Arrow arrow;
+  gboolean left;
 
-static const char *menuitem_enum_key = "dia-menuitem-value";
-
-
-/* --------------- DiaArrowPreview -------------------------------- */
-
-static int  dia_arrow_preview_expose     (GtkWidget             *widget,
-                                          GdkEventExpose        *event);
-
-
-G_DEFINE_TYPE (DiaArrowPreview, dia_arrow_preview, GTK_TYPE_MISC)
-
-
-static void
-dia_arrow_preview_class_init (DiaArrowPreviewClass *class)
-{
-  GtkWidgetClass *widget_class;
-
-  widget_class = GTK_WIDGET_CLASS (class);
-  widget_class->expose_event = dia_arrow_preview_expose;
-}
-
-
-static void
-dia_arrow_preview_init (DiaArrowPreview *arrow)
-{
-  int xpad, ypad;
-
-  gtk_widget_set_has_window (GTK_WIDGET (arrow), FALSE);
-
-  gtk_misc_get_padding (GTK_MISC (arrow), &xpad, &ypad);
-
-  gtk_widget_set_size_request (GTK_WIDGET (arrow),
-                               40 + xpad * 2,
-                               20 + ypad * 2);
-
-  arrow->atype = ARROW_NONE;
-  arrow->left = TRUE;
-}
-
-
-/**
- * dia_arrow_preview_new:
- * @atype: The type of arrow to start out selected with.
- * @left: If %TRUE, this preview will point to the left.
- *
- * Create a new arrow preview widget.
- *
- * Returns: A new widget.
- */
-GtkWidget *
-dia_arrow_preview_new (ArrowType atype, gboolean left)
-{
-  DiaArrowPreview *arrow = g_object_new (DIA_TYPE_ARROW_PREVIEW, NULL);
-
-  arrow->atype = atype;
-  arrow->left = left;
-
-  return GTK_WIDGET (arrow);
-}
-
-
-/**
- * dia_arrow_preview_set:
- * @arrow: Preview widget to change.
- * @atype: New arrow type to use.
- * @left: If %TRUE, the preview should point to the left.
- *
- * Set the values shown by an arrow preview widget.
- *
- * Since: dawn-of-time
- */
-static void
-dia_arrow_preview_set (DiaArrowPreview *arrow, ArrowType atype, gboolean left)
-{
-  if (arrow->atype != atype || arrow->left != left) {
-    arrow->atype = atype;
-    arrow->left = left;
-    if (gtk_widget_is_drawable (GTK_WIDGET (arrow))) {
-      gtk_widget_queue_draw (GTK_WIDGET (arrow));
-    }
-  }
-}
-
-
-/**
- * dia_arrow_preview_expose:
- * @widget: The widget to display.
- * @event: The event that caused the call.
- *
- * Expose handle for the arrow preview widget.
- *
- * The expose handler gets called when the Arrow needs to be drawn.
- *
- * Returns: %TRUE always.
- *
- * Since: dawn-of-time
- */
-static int
-dia_arrow_preview_expose (GtkWidget *widget, GdkEventExpose *event)
-{
-  if (gtk_widget_is_drawable (widget)) {
-    Point from, to;
-    Point move_arrow, move_line, arrow_head;
-    DiaCairoRenderer *renderer;
-    DiaArrowPreview *arrow = DIA_ARROW_PREVIEW(widget);
-    Arrow arrow_type;
-    GtkMisc *misc = GTK_MISC (widget);
-    int width, height;
-    int x, y;
-    GdkWindow *win;
-    int linewidth = 2;
-    cairo_surface_t *surface;
-    cairo_t *ctx;
-    GtkAllocation alloc;
-    int xpad, ypad;
-
-    gtk_widget_get_allocation (widget, &alloc);
-    gtk_misc_get_padding (misc, &xpad, &ypad);
-
-    width = alloc.width - xpad * 2;
-    height = alloc.height - ypad * 2;
-    x = (alloc.x + xpad);
-    y = (alloc.y + ypad);
-
-    win = gtk_widget_get_window (widget);
-
-    to.y = from.y = height/2;
-    if (arrow->left) {
-      from.x = width-linewidth;
-      to.x = 0;
-    } else {
-      from.x = 0;
-      to.x = width-linewidth;
-    }
-
-    /* here we must do some acrobaticts and construct Arrow type
-     * variable
-     */
-    arrow_type.type = arrow->atype;
-    arrow_type.length = .75 * ((double) height-linewidth);
-    arrow_type.width = .75 * ((double) height-linewidth);
-
-    /* and here we calculate new arrow start and end of line points */
-    calculate_arrow_point(&arrow_type, &from, &to,
-                          &move_arrow, &move_line,
-                         linewidth);
-    arrow_head = to;
-    point_add(&arrow_head, &move_arrow);
-    point_add(&to, &move_line);
-
-    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
-
-    renderer = g_object_new (DIA_CAIRO_TYPE_RENDERER, NULL);
-    renderer->with_alpha = TRUE;
-    renderer->surface = cairo_surface_reference (surface);
-
-    dia_renderer_begin_render (DIA_RENDERER (renderer), NULL);
-    dia_renderer_set_linewidth (DIA_RENDERER (renderer), linewidth);
-    {
-      Color colour_bg, colour_fg;
-      GtkStyle *style = gtk_widget_get_style (widget);
-      /* the text colors are the best approximation to what we had */
-      GdkColor bg = style->base[gtk_widget_get_state (widget)];
-      GdkColor fg = style->text[gtk_widget_get_state (widget)];
-
-      GDK_COLOR_TO_DIA (bg, colour_bg);
-      GDK_COLOR_TO_DIA (fg, colour_fg);
-
-      dia_renderer_draw_line (DIA_RENDERER (renderer), &from, &to, &colour_fg);
-      dia_arrow_draw (&arrow_type,
-                      DIA_RENDERER (renderer),
-                      &arrow_head,
-                      &from,
-                      linewidth,
-                      &colour_fg,
-                      &colour_bg);
-    }
-    dia_renderer_end_render (DIA_RENDERER (renderer));
-    g_clear_object (&renderer);
-
-    ctx = gdk_cairo_create (win);
-    cairo_set_source_surface (ctx, surface, x, y);
-    cairo_paint (ctx);
-  }
-
-  return TRUE;
-}
-
+  DiaChangeArrowCallback callback;
+  gpointer user_data;
 
-/* ------- Code for DiaArrowChooser ----------------------- */
+  GtkWidget *menu;
+  GtkWidget *dialog;
+  DiaArrowSelector *selector;
+};
 
 G_DEFINE_TYPE (DiaArrowChooser, dia_arrow_chooser, GTK_TYPE_BUTTON)
 
@@ -271,10 +97,10 @@ dia_arrow_chooser_button_press_event (GtkWidget *widget, GdkEventButton *event)
 
 
 static void
-dia_arrow_chooser_class_init (DiaArrowChooserClass *class)
+dia_arrow_chooser_class_init (DiaArrowChooserClass *klass)
 {
-  GObjectClass *object_class = G_OBJECT_CLASS (class);
-  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
   object_class->dispose = dia_arrow_chooser_dispose;
 
@@ -293,7 +119,7 @@ dia_arrow_chooser_init (DiaArrowChooser *arrow)
   arrow->arrow.width = DEFAULT_ARROW_WIDTH;
 
   wid = dia_arrow_preview_new (ARROW_NONE, arrow->left);
-  gtk_container_add(GTK_CONTAINER (arrow), wid);
+  gtk_container_add (GTK_CONTAINER (arrow), wid);
   gtk_widget_show (wid);
   arrow->preview = DIA_ARROW_PREVIEW (wid);
 
@@ -321,7 +147,7 @@ dia_arrow_chooser_dialog_response (GtkWidget       *dialog,
         new_arrow.length != chooser->arrow.length ||
         new_arrow.width  != chooser->arrow.width) {
       chooser->arrow = new_arrow;
-      dia_arrow_preview_set (chooser->preview, new_arrow.type, chooser->left);
+      dia_arrow_preview_set_arrow (chooser->preview, new_arrow.type, chooser->left);
       if (chooser->callback) {
         (* chooser->callback) (chooser->arrow, chooser->user_data);
       }
@@ -329,6 +155,7 @@ dia_arrow_chooser_dialog_response (GtkWidget       *dialog,
   } else {
     dia_arrow_selector_set_arrow (chooser->selector, chooser->arrow);
   }
+
   gtk_widget_hide (chooser->dialog);
 }
 
@@ -409,8 +236,8 @@ static void
 dia_arrow_chooser_change_arrow_type (GtkMenuItem     *mi,
                                      DiaArrowChooser *chooser)
 {
-  ArrowType atype = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (mi),
-                                                        menuitem_enum_key));
+  ArrowType atype = GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (mi),
+                                                         dia_menuitem_key_quark ()));
   Arrow arrow;
   arrow.width = chooser->arrow.width;
   arrow.length = chooser->arrow.length;
@@ -442,7 +269,9 @@ dia_arrow_chooser_new (gboolean               left,
   int i;
 
   chooser->left = left;
-  dia_arrow_preview_set (chooser->preview, chooser->preview->atype, left);
+  dia_arrow_preview_set_arrow (chooser->preview,
+                               dia_arrow_preview_get_arrow (chooser->preview),
+                               left);
   chooser->callback = callback;
   chooser->user_data = user_data;
 
@@ -452,9 +281,9 @@ dia_arrow_chooser_new (gboolean               left,
   for (i = ARROW_NONE; i < MAX_ARROW_TYPE; ++i) {
     ArrowType arrow_type = arrow_type_from_index (i);
     mi = gtk_menu_item_new ();
-    g_object_set_data (G_OBJECT (mi),
-                       menuitem_enum_key,
-                       GINT_TO_POINTER (arrow_type));
+    g_object_set_qdata (G_OBJECT (mi),
+                        dia_menuitem_key_quark (),
+                        GINT_TO_POINTER (arrow_type));
     gtk_widget_set_tooltip_text (mi,
                                  gettext (arrow_get_name_from_type (arrow_type)));
     ar = dia_arrow_preview_new (arrow_type, left);
@@ -496,7 +325,7 @@ void
 dia_arrow_chooser_set_arrow (DiaArrowChooser *chooser, Arrow *arrow)
 {
   if (chooser->arrow.type != arrow->type) {
-    dia_arrow_preview_set (chooser->preview, arrow->type, chooser->left);
+    dia_arrow_preview_set_arrow (chooser->preview, arrow->type, chooser->left);
     chooser->arrow.type = arrow->type;
     if (chooser->dialog != NULL) {
       dia_arrow_selector_set_arrow (chooser->selector, chooser->arrow);
@@ -525,4 +354,3 @@ dia_arrow_chooser_get_arrow_type (DiaArrowChooser *arrow)
 {
   return arrow->arrow.type;
 }
-
diff --git a/lib/diaarrowchooser.h b/lib/dia-arrow-chooser.h
similarity index 69%
rename from lib/diaarrowchooser.h
rename to lib/dia-arrow-chooser.h
index 19991afe9..1f4e49bf6 100644
--- a/lib/diaarrowchooser.h
+++ b/lib/dia-arrow-chooser.h
@@ -17,6 +17,8 @@
  * 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
  */
 
 #pragma once
@@ -24,52 +26,16 @@
 #include <gtk/gtk.h>
 
 #include "arrows.h"
-#include "diatypes.h"
 #include "dia-autoptr.h"
 #include "dia-arrow-selector.h"
 
 G_BEGIN_DECLS
 
-/* --------------- DiaArrowPreview -------------------------------- */
-
-#define DIA_TYPE_ARROW_PREVIEW dia_arrow_preview_get_type ()
-G_DECLARE_FINAL_TYPE (DiaArrowPreview, dia_arrow_preview, DIA, ARROW_PREVIEW, GtkMisc)
-
-
-struct _DiaArrowPreview {
-  GtkMisc misc;
-  ArrowType atype;
-  gboolean left;
-};
-
-
-GtkWidget *dia_arrow_preview_new            (ArrowType               atype,
-                                             gboolean                left);
-
-
-/* ------- Code for DiaArrowChooser ----------------------- */
-
 #define DIA_TYPE_ARROW_CHOOSER dia_arrow_chooser_get_type ()
 G_DECLARE_FINAL_TYPE (DiaArrowChooser, dia_arrow_chooser, DIA, ARROW_CHOOSER, GtkButton)
 
-
 typedef void (*DiaChangeArrowCallback) (Arrow atype, gpointer user_data);
 
-struct _DiaArrowChooser {
-  GtkButton button;
-  DiaArrowPreview *preview;
-  Arrow arrow;
-  gboolean left;
-
-  DiaChangeArrowCallback callback;
-  gpointer user_data;
-
-  GtkWidget *menu;
-  GtkWidget *dialog;
-  DiaArrowSelector *selector;
-};
-
-
 GtkWidget *dia_arrow_chooser_new            (gboolean                left,
                                              DiaChangeArrowCallback  callback,
                                              gpointer                user_data);
diff --git a/lib/dia-arrow-preview.c b/lib/dia-arrow-preview.c
new file mode 100644
index 000000000..ebcc3349b
--- /dev/null
+++ b/lib/dia-arrow-preview.c
@@ -0,0 +1,220 @@
+/* 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
+ */
+
+#include "dia-arrow-preview.h"
+#include "renderer/diacairo.h"
+
+struct _DiaArrowPreview {
+  GtkMisc misc;
+  ArrowType atype;
+  gboolean left;
+};
+
+G_DEFINE_TYPE (DiaArrowPreview, dia_arrow_preview, GTK_TYPE_MISC)
+
+
+/**
+ * dia_arrow_preview_expose:
+ * @widget: The widget to display.
+ * @event: The event that caused the call.
+ *
+ * Expose handle for the arrow preview widget.
+ *
+ * The expose handler gets called when the Arrow needs to be drawn.
+ *
+ * Returns: %TRUE always.
+ *
+ * Since: dawn-of-time
+ */
+static int
+dia_arrow_preview_expose (GtkWidget *widget, GdkEventExpose *event)
+{
+  if (gtk_widget_is_drawable (widget)) {
+    Point from, to;
+    Point move_arrow, move_line, arrow_head;
+    DiaCairoRenderer *renderer;
+    DiaArrowPreview *arrow = DIA_ARROW_PREVIEW (widget);
+    Arrow arrow_type;
+    GtkMisc *misc = GTK_MISC (widget);
+    int width, height;
+    int x, y;
+    GdkWindow *win;
+    int linewidth = 2;
+    cairo_surface_t *surface;
+    cairo_t *ctx;
+    GtkAllocation alloc;
+    int xpad, ypad;
+
+    gtk_widget_get_allocation (widget, &alloc);
+    gtk_misc_get_padding (misc, &xpad, &ypad);
+
+    width = alloc.width - xpad * 2;
+    height = alloc.height - ypad * 2;
+    x = (alloc.x + xpad);
+    y = (alloc.y + ypad);
+
+    win = gtk_widget_get_window (widget);
+
+    to.y = from.y = height / 2;
+    if (arrow->left) {
+      from.x = width - linewidth;
+      to.x = 0;
+    } else {
+      from.x = 0;
+      to.x = width - linewidth;
+    }
+
+    /* here we must do some acrobaticts and construct Arrow type
+     * variable
+     */
+    arrow_type.type = arrow->atype;
+    arrow_type.length = .75 * ((double) height - linewidth);
+    arrow_type.width = .75 * ((double) height - linewidth);
+
+    /* and here we calculate new arrow start and end of line points */
+    calculate_arrow_point (&arrow_type,
+                           &from,
+                           &to,
+                           &move_arrow,
+                           &move_line,
+                           linewidth);
+    arrow_head = to;
+    point_add (&arrow_head, &move_arrow);
+    point_add (&to, &move_line);
+
+    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
+
+    renderer = g_object_new (DIA_CAIRO_TYPE_RENDERER, NULL);
+    renderer->with_alpha = TRUE;
+    renderer->surface = cairo_surface_reference (surface);
+
+    dia_renderer_begin_render (DIA_RENDERER (renderer), NULL);
+    dia_renderer_set_linewidth (DIA_RENDERER (renderer), linewidth);
+    {
+      Color colour_bg, colour_fg;
+      GtkStyle *style = gtk_widget_get_style (widget);
+      /* the text colors are the best approximation to what we had */
+      GdkColor bg = style->base[gtk_widget_get_state (widget)];
+      GdkColor fg = style->text[gtk_widget_get_state (widget)];
+
+      GDK_COLOR_TO_DIA (bg, colour_bg);
+      GDK_COLOR_TO_DIA (fg, colour_fg);
+
+      dia_renderer_draw_line (DIA_RENDERER (renderer), &from, &to, &colour_fg);
+      dia_arrow_draw (&arrow_type,
+                      DIA_RENDERER (renderer),
+                      &arrow_head,
+                      &from,
+                      linewidth,
+                      &colour_fg,
+                      &colour_bg);
+    }
+    dia_renderer_end_render (DIA_RENDERER (renderer));
+    g_clear_object (&renderer);
+
+    ctx = gdk_cairo_create (win);
+    cairo_set_source_surface (ctx, surface, x, y);
+    cairo_paint (ctx);
+  }
+
+  return TRUE;
+}
+
+
+static void
+dia_arrow_preview_class_init (DiaArrowPreviewClass *class)
+{
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
+
+  widget_class->expose_event = dia_arrow_preview_expose;
+}
+
+
+static void
+dia_arrow_preview_init (DiaArrowPreview *arrow)
+{
+  int xpad, ypad;
+
+  gtk_widget_set_has_window (GTK_WIDGET (arrow), FALSE);
+
+  gtk_misc_get_padding (GTK_MISC (arrow), &xpad, &ypad);
+
+  gtk_widget_set_size_request (GTK_WIDGET (arrow),
+                               40 + xpad * 2,
+                               20 + ypad * 2);
+
+  arrow->atype = ARROW_NONE;
+  arrow->left = TRUE;
+}
+
+
+/**
+ * dia_arrow_preview_new:
+ * @atype: The type of arrow to start out selected with.
+ * @left: If %TRUE, this preview will point to the left.
+ *
+ * Create a new arrow preview widget.
+ *
+ * Returns: A new widget.
+ */
+GtkWidget *
+dia_arrow_preview_new (ArrowType atype, gboolean left)
+{
+  DiaArrowPreview *arrow = g_object_new (DIA_TYPE_ARROW_PREVIEW, NULL);
+
+  arrow->atype = atype;
+  arrow->left = left;
+
+  return GTK_WIDGET (arrow);
+}
+
+
+/**
+ * dia_arrow_preview_set_arrow:
+ * @arrow: Preview widget to change.
+ * @atype: New arrow type to use.
+ * @left: If %TRUE, the preview should point to the left.
+ *
+ * Set the values shown by an arrow preview widget.
+ *
+ * Since: dawn-of-time
+ */
+void
+dia_arrow_preview_set_arrow (DiaArrowPreview *arrow,
+                             ArrowType        atype,
+                             gboolean         left)
+{
+  if (arrow->atype != atype || arrow->left != left) {
+    arrow->atype = atype;
+    arrow->left = left;
+    if (gtk_widget_is_drawable (GTK_WIDGET (arrow))) {
+      gtk_widget_queue_draw (GTK_WIDGET (arrow));
+    }
+  }
+}
+
+
+ArrowType
+dia_arrow_preview_get_arrow (DiaArrowPreview *self)
+{
+  g_return_val_if_fail (self, ARROW_NONE);
+
+  return self->atype;
+}
diff --git a/lib/dia-arrow-preview.h b/lib/dia-arrow-preview.h
new file mode 100644
index 000000000..5eec7c567
--- /dev/null
+++ b/lib/dia-arrow-preview.h
@@ -0,0 +1,40 @@
+/* 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
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+#include "dia-autoptr.h"
+#include "arrows.h"
+
+G_BEGIN_DECLS
+
+#define DIA_TYPE_ARROW_PREVIEW dia_arrow_preview_get_type ()
+G_DECLARE_FINAL_TYPE (DiaArrowPreview, dia_arrow_preview, DIA, ARROW_PREVIEW, GtkMisc)
+
+GtkWidget *dia_arrow_preview_new            (ArrowType               atype,
+                                             gboolean                left);
+void       dia_arrow_preview_set_arrow      (DiaArrowPreview        *arrow,
+                                             ArrowType               atype,
+                                             gboolean                left);
+ArrowType  dia_arrow_preview_get_arrow      (DiaArrowPreview        *self);
+
+G_END_DECLS
diff --git a/lib/dia-line-chooser.c b/lib/dia-line-chooser.c
new file mode 100644
index 000000000..81cfedf6f
--- /dev/null
+++ b/lib/dia-line-chooser.c
@@ -0,0 +1,229 @@
+/* Dia -- an diagram creation/manipulation program
+ * Copyright (C) 1998 Alexander Larsson
+ *
+ * dialinechooser.c -- Copyright (C) 1999 James Henstridge.
+ *                     Copyright (C) 2004 Hubert Figuiere
+ *
+ * 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
+ */
+
+#include "config.h"
+
+#include <glib/gi18n-lib.h>
+
+#include "dia-line-style-selector.h"
+#include "dia-line-chooser.h"
+#include "dia-line-preview.h"
+#include "utils.h"
+
+struct _DiaLineChooser {
+  GtkButton button;
+  DiaLinePreview *preview;
+  LineStyle lstyle;
+  double dash_length;
+
+  GtkMenu *menu;
+
+  DiaChangeLineCallback callback;
+  gpointer user_data;
+
+  GtkWidget *dialog;
+  DiaLineStyleSelector *selector;
+};
+
+G_DEFINE_TYPE (DiaLineChooser, dia_line_chooser, GTK_TYPE_BUTTON)
+
+
+static void
+dia_line_chooser_dispose (GObject *object)
+{
+  DiaLineChooser *self = DIA_LINE_CHOOSER (object);
+
+  g_clear_object (&self->menu);
+
+  G_OBJECT_CLASS (dia_line_chooser_parent_class)->dispose (object);
+}
+
+
+static int
+dia_line_chooser_button_press_event (GtkWidget *widget, GdkEventButton *event)
+{
+  DiaLineChooser *self = DIA_LINE_CHOOSER (widget);
+
+  if (event->button == 1) {
+    gtk_menu_popup (self->menu,
+                    NULL,
+                    NULL,
+                    NULL,
+                    NULL,
+                    event->button,
+                    event->time);
+
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+
+static void
+dia_line_chooser_class_init (DiaLineChooserClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->dispose = dia_line_chooser_dispose;
+
+  widget_class->button_press_event = dia_line_chooser_button_press_event;
+}
+
+
+static void
+dia_line_chooser_dialog_response (GtkWidget      *dialog,
+                                  int             response_id,
+                                  DiaLineChooser *lchooser)
+{
+  LineStyle new_style;
+  double new_dash;
+
+  if (response_id == GTK_RESPONSE_OK) {
+    dia_line_style_selector_get_linestyle (lchooser->selector,
+                                           &new_style,
+                                           &new_dash);
+
+    if (new_style != lchooser->lstyle || new_dash != lchooser->dash_length) {
+      lchooser->lstyle = new_style;
+      lchooser->dash_length = new_dash;
+      dia_line_preview_set_style (lchooser->preview, new_style);
+
+      if (lchooser->callback)
+        (* lchooser->callback) (new_style, new_dash, lchooser->user_data);
+    }
+  } else {
+    dia_line_style_selector_set_linestyle (lchooser->selector,
+                                           lchooser->lstyle,
+                                           lchooser->dash_length);
+  }
+
+  gtk_widget_hide (lchooser->dialog);
+}
+
+
+static void
+dia_line_chooser_change_line_style (GtkMenuItem *mi, DiaLineChooser *lchooser)
+{
+  LineStyle lstyle = GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (mi),
+                                                          dia_menuitem_key_quark ()));
+
+  dia_line_chooser_set_line_style (lchooser, lstyle, lchooser->dash_length);
+}
+
+
+void
+dia_line_chooser_set_line_style (DiaLineChooser *lchooser,
+                                 LineStyle       lstyle,
+                                 double          dashlength)
+{
+  if (lstyle != lchooser->lstyle) {
+    dia_line_preview_set_style (lchooser->preview, lstyle);
+    lchooser->lstyle = lstyle;
+    dia_line_style_selector_set_linestyle (lchooser->selector,
+                                           lchooser->lstyle,
+                                           lchooser->dash_length);
+  }
+
+  lchooser->dash_length = dashlength;
+
+  if (lchooser->callback)
+    (* lchooser->callback) (lchooser->lstyle,
+                            lchooser->dash_length,
+                            lchooser->user_data);
+}
+
+
+static void
+dia_line_chooser_init (DiaLineChooser *lchooser)
+{
+  GtkWidget *wid;
+  GtkWidget *mi, *ln;
+  int i;
+
+  lchooser->lstyle = LINESTYLE_SOLID;
+  lchooser->dash_length = DEFAULT_LINESTYLE_DASHLEN;
+
+  wid = dia_line_preview_new (LINESTYLE_SOLID);
+  gtk_container_add (GTK_CONTAINER (lchooser), wid);
+  gtk_widget_show (wid);
+  lchooser->preview = DIA_LINE_PREVIEW (wid);
+
+  lchooser->dialog = gtk_dialog_new_with_buttons (_("Line Style Properties"),
+                                                  NULL,
+                                                  GTK_DIALOG_NO_SEPARATOR,
+                                                  _("_Cancel"), GTK_RESPONSE_CANCEL,
+                                                  _("_OK"), GTK_RESPONSE_OK,
+                                                  NULL);
+  gtk_dialog_set_default_response (GTK_DIALOG (lchooser->dialog),
+                                   GTK_RESPONSE_OK);
+  g_signal_connect (G_OBJECT (lchooser->dialog),
+                    "response", G_CALLBACK (dia_line_chooser_dialog_response),
+                    lchooser);
+
+  wid = dia_line_style_selector_new ();
+  gtk_container_set_border_width (GTK_CONTAINER (wid), 5);
+  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG(lchooser->dialog))),
+                      wid,
+                      TRUE,
+                      TRUE,
+                      0);
+  gtk_widget_show (wid);
+  lchooser->selector = DIA_LINE_STYLE_SELECTOR (wid);
+
+  lchooser->menu = GTK_MENU (g_object_ref_sink (gtk_menu_new ()));
+  for (i = 0; i <= LINESTYLE_DOTTED; i++) {
+    mi = gtk_menu_item_new ();
+    g_object_set_qdata (G_OBJECT (mi),
+                        dia_menuitem_key_quark (),
+                        GINT_TO_POINTER (i));
+    ln = dia_line_preview_new (i);
+    gtk_container_add (GTK_CONTAINER (mi), ln);
+    gtk_widget_show (ln);
+    g_signal_connect (G_OBJECT (mi),
+                      "activate", G_CALLBACK (dia_line_chooser_change_line_style),
+                      lchooser);
+    gtk_menu_shell_append (GTK_MENU_SHELL (lchooser->menu), mi);
+    gtk_widget_show (mi);
+  }
+  mi = gtk_menu_item_new_with_label (_("Details…"));
+  g_signal_connect_swapped (G_OBJECT (mi),
+                            "activate", G_CALLBACK (gtk_widget_show),
+                            lchooser->dialog);
+  gtk_menu_shell_append (GTK_MENU_SHELL (lchooser->menu), mi);
+  gtk_widget_show (mi);
+}
+
+
+GtkWidget *
+dia_line_chooser_new (DiaChangeLineCallback callback,
+                      gpointer              user_data)
+{
+  DiaLineChooser *chooser = g_object_new (DIA_TYPE_LINE_CHOOSER, NULL);
+
+  chooser->callback = callback;
+  chooser->user_data = user_data;
+
+  return GTK_WIDGET (chooser);
+}
diff --git a/lib/dia-line-chooser.h b/lib/dia-line-chooser.h
new file mode 100644
index 000000000..77a5eaa42
--- /dev/null
+++ b/lib/dia-line-chooser.h
@@ -0,0 +1,45 @@
+/* Dia -- an diagram creation/manipulation program
+ * Copyright (C) 1998 Alexander Larsson
+ *
+ * dialinechooser.h -- Copyright (C) 1999 James Henstridge.
+ *                     Copyright (C) 2004 Hubert Figuiere
+ *
+ * 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
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+#include "dia-line-style-selector.h"
+
+G_BEGIN_DECLS
+
+#define DIA_TYPE_LINE_CHOOSER dia_line_chooser_get_type ()
+G_DECLARE_FINAL_TYPE (DiaLineChooser, dia_line_chooser, DIA, LINE_CHOOSER, GtkButton)
+
+typedef void (*DiaChangeLineCallback) (LineStyle lstyle,
+                                       double    dash_length,
+                                       gpointer  user_data);
+
+GtkWidget *dia_line_chooser_new                  (DiaChangeLineCallback  callback,
+                                                  gpointer               user_data);
+void       dia_line_chooser_set_line_style       (DiaLineChooser        *lchooser,
+                                                  LineStyle              style,
+                                                  double                 dashlength);
+
+G_END_DECLS
diff --git a/lib/dia-line-preview.c b/lib/dia-line-preview.c
new file mode 100644
index 000000000..7da55ff4d
--- /dev/null
+++ b/lib/dia-line-preview.c
@@ -0,0 +1,158 @@
+/* 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
+ */
+
+#include "dia-line-preview.h"
+
+struct _DiaLinePreview {
+  GtkMisc misc;
+  LineStyle lstyle;
+};
+
+G_DEFINE_TYPE (DiaLinePreview, dia_line_preview, GTK_TYPE_MISC)
+
+
+static int
+dia_line_preview_expose (GtkWidget *widget, GdkEventExpose *event)
+{
+  DiaLinePreview *line = DIA_LINE_PREVIEW (widget);
+  GtkMisc *misc = GTK_MISC(widget);
+  int width, height;
+  int x, y;
+  GdkWindow *win;
+  double dash_list[6];
+  GtkStyle *style;
+  GdkColor fg;
+  cairo_t *ctx;
+
+  if (gtk_widget_is_drawable (widget)) {
+    GtkAllocation alloc;
+    int xpad, ypad;
+
+    gtk_widget_get_allocation (widget, &alloc);
+    gtk_misc_get_padding (misc, &xpad, &ypad);
+
+    width = alloc.width - xpad * 2;
+    height = alloc.height - ypad * 2;
+    x = (alloc.x + xpad);
+    y = (alloc.y + ypad);
+
+    win = gtk_widget_get_window (widget);
+    style = gtk_widget_get_style (widget);
+    fg = style->text[gtk_widget_get_state(widget)];
+
+    ctx = gdk_cairo_create (win);
+    gdk_cairo_set_source_color (ctx, &fg);
+    cairo_set_line_width (ctx, 2);
+    cairo_set_line_cap (ctx, CAIRO_LINE_CAP_BUTT);
+    cairo_set_line_join (ctx, CAIRO_LINE_JOIN_MITER);
+
+    switch (line->lstyle) {
+      case LINESTYLE_DEFAULT:
+      case LINESTYLE_SOLID:
+        cairo_set_dash (ctx, dash_list, 0, 0);
+        break;
+      case LINESTYLE_DASHED:
+        dash_list[0] = 10;
+        dash_list[1] = 10;
+        cairo_set_dash (ctx, dash_list, 2, 0);
+        break;
+      case LINESTYLE_DASH_DOT:
+        dash_list[0] = 10;
+        dash_list[1] = 4;
+        dash_list[2] = 2;
+        dash_list[3] = 4;
+        cairo_set_dash (ctx, dash_list, 4, 0);
+        break;
+      case LINESTYLE_DASH_DOT_DOT:
+        dash_list[0] = 10;
+        dash_list[1] = 2;
+        dash_list[2] = 2;
+        dash_list[3] = 2;
+        dash_list[4] = 2;
+        dash_list[5] = 2;
+        cairo_set_dash (ctx, dash_list, 6, 0);
+        break;
+      case LINESTYLE_DOTTED:
+        dash_list[0] = 2;
+        dash_list[1] = 2;
+        cairo_set_dash (ctx, dash_list, 2, 0);
+        break;
+      default:
+        g_return_val_if_reached (FALSE);
+    }
+    cairo_move_to (ctx, x, y + height / 2);
+    cairo_line_to (ctx, x + width, y + height / 2);
+    cairo_stroke (ctx);
+  }
+  return TRUE;
+}
+
+
+static void
+dia_line_preview_class_init (DiaLinePreviewClass *klass)
+{
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  widget_class->expose_event = dia_line_preview_expose;
+}
+
+
+static void
+dia_line_preview_init (DiaLinePreview *line)
+{
+  int xpad, ypad;
+
+  gtk_widget_set_has_window (GTK_WIDGET (line), FALSE);
+
+  gtk_misc_get_padding (GTK_MISC (line), &xpad, &ypad);
+
+  gtk_widget_set_size_request (GTK_WIDGET (line),
+                               30 + xpad * 2,
+                               15 + ypad * 2);
+
+  line->lstyle = LINESTYLE_SOLID;
+}
+
+
+GtkWidget *
+dia_line_preview_new (LineStyle lstyle)
+{
+  DiaLinePreview *line = g_object_new (DIA_TYPE_LINE_PREVIEW, NULL);
+
+  line->lstyle = lstyle;
+
+  return GTK_WIDGET (line);
+}
+
+
+void
+dia_line_preview_set_style (DiaLinePreview *line, LineStyle lstyle)
+{
+  if (line->lstyle == lstyle) {
+    return;
+  }
+
+  line->lstyle = lstyle;
+
+  if (gtk_widget_is_drawable (GTK_WIDGET (line))) {
+    gtk_widget_queue_draw (GTK_WIDGET (line));
+  }
+}
+
diff --git a/lib/dia-line-preview.h b/lib/dia-line-preview.h
new file mode 100644
index 000000000..a98d37f0f
--- /dev/null
+++ b/lib/dia-line-preview.h
@@ -0,0 +1,37 @@
+/* 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
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+#include "dia-autoptr.h"
+#include "dia-enums.h"
+
+G_BEGIN_DECLS
+
+#define DIA_TYPE_LINE_PREVIEW dia_line_preview_get_type ()
+G_DECLARE_FINAL_TYPE (DiaLinePreview, dia_line_preview, DIA, LINE_PREVIEW, GtkMisc)
+
+GtkWidget *dia_line_preview_new                  (LineStyle             lstyle);
+void       dia_line_preview_set_style            (DiaLinePreview       *line,
+                                                  LineStyle             lstyle);
+
+G_END_DECLS
diff --git a/lib/libdia.def b/lib/libdia.def
index 11e3c9d6e..3d6f1db11 100644
--- a/lib/libdia.def
+++ b/lib/libdia.def
@@ -190,18 +190,27 @@ EXPORTS
  dia_asin
  dia_acos
 
+ dia_menuitem_key_quark
+
  dia_alignment_selector_get_alignment
  dia_alignment_selector_new
  dia_alignment_selector_set_alignment
+
  dia_arrow_chooser_new
  dia_arrow_chooser_set_arrow
  dia_arrow_chooser_get_arrow_type
  dia_arrow_chooser_get_type
+
  dia_arrow_selector_get_arrow
  dia_arrow_selector_get_type
  dia_arrow_selector_new
  dia_arrow_selector_set_arrow
 
+ dia_arrow_preview_new
+ dia_arrow_preview_get_type
+ dia_arrow_preview_set_arrow
+ dia_arrow_preview_get_arrow
+
  dia_arrow_cell_renderer_new
 
  dia_assert_true
@@ -312,16 +321,20 @@ EXPORTS
  dialog_make
  dialog_add_spinbutton
 
- dia_line_chooser_new
 
  dia_line_style_selector_get_linestyle
  dia_line_style_selector_get_type
  dia_line_style_selector_new
  dia_line_style_selector_set_linestyle
 
+ dia_line_chooser_new
  dia_line_chooser_get_type
  dia_line_chooser_set_line_style
 
+ dia_line_preview_new
+ dia_line_preview_get_type
+ dia_line_preview_set_style
+
  dia_line_cell_renderer_new
 
  dia_list_plugins
diff --git a/lib/meson.build b/lib/meson.build
index 62dedb57f..107c4d6dc 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -142,12 +142,15 @@ libdia_sources = stdprop_sources + [
     'dia_svg.c',
     'autoroute.c',
     'parent.c',
-    'diaarrowchooser.c',
     'dia-autoptr.h',
     'dia-arrow-cell-renderer.c',
     'dia-arrow-cell-renderer.h',
+    'dia-arrow-chooser.c',
+    'dia-arrow-chooser.h',
     'dia-arrow-selector.c',
     'dia-arrow-selector.h',
+    'dia-arrow-preview.c',
+    'dia-arrow-preview.h',
     'dia-colour-cell-renderer.c',
     'dia-colour-cell-renderer.h',
     'dia-colour-selector.c',
@@ -162,15 +165,18 @@ libdia_sources = stdprop_sources + [
     'dia-handle.c',
     'dia-line-cell-renderer.c',
     'dia-line-cell-renderer.h',
+    'dia-line-chooser.c',
+    'dia-line-chooser.h',
     'dia-line-style-selector.c',
     'dia-line-style-selector.h',
+    'dia-line-preview.c',
+    'dia-line-preview.h',
     'dia-simple-list.c',
     'dia-simple-list.h',
     'dia-size-selector.c',
     'dia-size-selector.h',
     'dia-unit-spinner.c',
     'dia-unit-spinner.h',
-    'dialinechooser.c',
     'persistence.c',
     'diaerror.c',
     'debug.c',
diff --git a/lib/prop_attr.c b/lib/prop_attr.c
index 030f92bc5..b071dcbf1 100644
--- a/lib/prop_attr.c
+++ b/lib/prop_attr.c
@@ -28,7 +28,6 @@
 #include "dia_xml.h"
 #include "properties.h"
 #include "propinternals.h"
-#include "diaarrowchooser.h"
 #include "dia-arrow-selector.h"
 #include "dia-colour-selector.h"
 #include "dia-font-selector.h"
diff --git a/lib/utils.c b/lib/utils.c
index e358bf63c..796e77dda 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -18,22 +18,15 @@
 
 /* This file contains some code from glib.
   Here is the copyright notice:
-  
+
  * GLIB - Library of useful routines for C programming
  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
  */
 
-
-#include <config.h>
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-#include <glib.h>
-
 #include "utils.h"
 
+G_DEFINE_QUARK (dia-menuitem-key, dia_menuitem_key)
+
 int
 nearest_pow (int num)
 {
diff --git a/lib/utils.h b/lib/utils.h
index 93252dbec..762abc14f 100644
--- a/lib/utils.h
+++ b/lib/utils.h
@@ -15,20 +15,15 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
-#ifndef UTILS_H
-#define UTILS_H
 
-#include <stdarg.h>
+#pragma once
+
+#include <glib-object.h>
 
 G_BEGIN_DECLS
 
+GQuark dia_menuitem_key_quark (void);
+
 int nearest_pow (int num);
 
 G_END_DECLS
-
-#endif /* UTILS_H */
-
-
-
-
-


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