summaryrefslogtreecommitdiffstats
path: root/app/widgets/gimpsymmetryeditor.c
blob: f0b7c631f86d55513e8272a685c9b4f0ee69500d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpsymmetryeditor.c
 * Copyright (C) 2015 Jehan <jehan@girinstud.io>
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "propgui/propgui-types.h" /* ugly, but what the heck */

#include "core/gimpimage.h"
#include "core/gimpimage-symmetry.h"
#include "core/gimpsymmetry.h"

#include "propgui/gimppropgui.h"

#include "gimpmenufactory.h"
#include "gimpsymmetryeditor.h"

#include "gimp-intl.h"


struct _GimpSymmetryEditorPrivate
{
  GimpContext *context;

  GtkWidget   *menu;
  GtkWidget   *options_vbox;
};


static void   gimp_symmetry_editor_set_image        (GimpImageEditor    *editor,
                                                     GimpImage          *image);

/* Signal handlers on the contextual image. */
static void   gimp_symmetry_editor_symmetry_notify  (GimpImage          *image,
                                                     GParamSpec         *pspec,
                                                     GimpSymmetryEditor *editor);

/* Signal handlers on the symmetry. */
static void   gimp_symmetry_editor_symmetry_updated (GimpSymmetry       *symmetry,
                                                     GimpImage          *image,
                                                     GimpSymmetryEditor *editor);

/* Private functions. */
static void   gimp_symmetry_editor_set_options      (GimpSymmetryEditor *editor,
                                                     GimpSymmetry       *symmetry);


G_DEFINE_TYPE_WITH_PRIVATE (GimpSymmetryEditor, gimp_symmetry_editor,
                            GIMP_TYPE_IMAGE_EDITOR)

#define parent_class gimp_symmetry_editor_parent_class


static void
gimp_symmetry_editor_class_init (GimpSymmetryEditorClass *klass)
{
  GimpImageEditorClass *image_editor_class = GIMP_IMAGE_EDITOR_CLASS (klass);

  image_editor_class->set_image = gimp_symmetry_editor_set_image;
}

static void
gimp_symmetry_editor_init (GimpSymmetryEditor *editor)
{
  GtkWidget *scrolled_window;
  GtkWidget *viewport;

  editor->p = gimp_symmetry_editor_get_instance_private (editor);

  gtk_widget_set_size_request (GTK_WIDGET (editor), -1, 200);

  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);
  gtk_box_pack_start (GTK_BOX (editor), scrolled_window, TRUE, TRUE, 0);
  gtk_widget_show (scrolled_window);

  viewport = gtk_viewport_new (NULL, NULL);
  gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
  gtk_widget_show (viewport);

  editor->p->options_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
  gtk_container_set_border_width (GTK_CONTAINER (editor->p->options_vbox), 2);
  gtk_container_add (GTK_CONTAINER (viewport), editor->p->options_vbox);
  gtk_widget_show (editor->p->options_vbox);

  gimp_symmetry_editor_set_image (GIMP_IMAGE_EDITOR (editor), NULL);
}

static void
gimp_symmetry_editor_set_image (GimpImageEditor *image_editor,
                                GimpImage       *image)
{
  GtkListStore *store;
  GtkTreeIter   iter;
  GList        *syms;
  GList        *sym_iter;
  GimpSymmetry *symmetry;

  GimpSymmetryEditor *editor = GIMP_SYMMETRY_EDITOR (image_editor);

  if (image_editor->image)
    {
      g_signal_handlers_disconnect_by_func (image_editor->image,
                                            G_CALLBACK (gimp_symmetry_editor_symmetry_notify),
                                            editor);
    }

  GIMP_IMAGE_EDITOR_CLASS (parent_class)->set_image (image_editor, image);

  /* Destroy the previous menu. */
  if (editor->p->menu)
    {
      gtk_widget_destroy (editor->p->menu);
      editor->p->menu = NULL;
    }

  store = gimp_int_store_new ();

  /* The menu of available symmetries. */
  syms = gimp_image_symmetry_list ();
  for (sym_iter = syms; sym_iter; sym_iter = g_list_next (sym_iter))
    {
      GimpSymmetryClass *klass;
      GType              type;

      type = (GType) sym_iter->data;
      klass = g_type_class_ref (type);

      gtk_list_store_prepend (store, &iter);
      gtk_list_store_set (store, &iter,
                          GIMP_INT_STORE_LABEL,
                          klass->label,
                          GIMP_INT_STORE_USER_DATA,
                          sym_iter->data,
                          -1);
      g_type_class_unref (klass);
    }
  g_list_free (syms);

  gtk_list_store_prepend (store, &iter);
  gtk_list_store_set (store, &iter,
                      GIMP_INT_STORE_LABEL, _("None"),
                      GIMP_INT_STORE_USER_DATA, GIMP_TYPE_SYMMETRY,
                      -1);
  if (image_editor->image)
    editor->p->menu =
      gimp_prop_pointer_combo_box_new (G_OBJECT (image_editor->image),
                                       "symmetry",
                                       GIMP_INT_STORE (store));
  else
    editor->p->menu =
      gimp_int_combo_box_new (_("None"), 0,
                              NULL);

  g_object_unref (store);

  gimp_int_combo_box_set_label (GIMP_INT_COMBO_BOX (editor->p->menu),
                                _("Symmetry"));
  g_object_set (editor->p->menu, "ellipsize", PANGO_ELLIPSIZE_END, NULL);

  gtk_box_pack_start (GTK_BOX (editor), editor->p->menu,
                      FALSE, FALSE, 0);
  gtk_box_reorder_child (GTK_BOX (editor), editor->p->menu, 0);

  if (image_editor->image)
    {
      /* Connect to symmetry change. */
      g_signal_connect (image_editor->image, "notify::symmetry",
                        G_CALLBACK (gimp_symmetry_editor_symmetry_notify),
                        editor);

      /* Update the symmetry options. */
      symmetry = gimp_image_get_active_symmetry (image_editor->image);
      gimp_symmetry_editor_set_options (editor, symmetry);
    }
  else
    {
      gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (editor->p->menu), 0);
      gtk_widget_set_sensitive (editor->p->menu, FALSE);
    }
  gtk_widget_show (editor->p->menu);
}

static void
gimp_symmetry_editor_symmetry_notify (GimpImage          *image,
                                      GParamSpec         *pspec,
                                      GimpSymmetryEditor *editor)
{
  GimpSymmetry *symmetry = NULL;

  if (image)
    {
      symmetry = gimp_image_get_active_symmetry (image);

      if (symmetry)
        g_signal_connect (symmetry, "gui-param-changed",
                          G_CALLBACK (gimp_symmetry_editor_symmetry_updated),
                          editor);
    }

  gimp_symmetry_editor_set_options (editor, symmetry);
}

static void
gimp_symmetry_editor_symmetry_updated (GimpSymmetry       *symmetry,
                                       GimpImage          *image,
                                       GimpSymmetryEditor *editor)
{
  if (image    != gimp_image_editor_get_image (GIMP_IMAGE_EDITOR (editor)) ||
      symmetry != gimp_image_get_active_symmetry (image))
    {
      g_signal_handlers_disconnect_by_func (symmetry,
                                            gimp_symmetry_editor_symmetry_updated,
                                            editor);
      return;
    }

  gimp_symmetry_editor_set_options (editor, symmetry);
}

static void
gimp_symmetry_editor_set_options (GimpSymmetryEditor *editor,
                                  GimpSymmetry       *symmetry)
{
  gtk_container_foreach (GTK_CONTAINER (editor->p->options_vbox),
                         (GtkCallback) gtk_widget_destroy, NULL);

  if (symmetry && G_TYPE_FROM_INSTANCE (symmetry) != GIMP_TYPE_SYMMETRY)
    {
      GimpImageEditor *image_editor = GIMP_IMAGE_EDITOR (editor);
      GimpImage       *image        = image_editor->image;
      GtkWidget       *gui;

      gui = gimp_prop_gui_new (G_OBJECT (symmetry),
                               GIMP_TYPE_SYMMETRY,
                               GIMP_SYMMETRY_PARAM_GUI,
                               GEGL_RECTANGLE (0, 0,
                                               gimp_image_get_width  (image),
                                               gimp_image_get_height (image)),
                               GIMP_IMAGE_EDITOR (editor)->context,
                               NULL, NULL, NULL);
      gtk_box_pack_start (GTK_BOX (editor->p->options_vbox), gui,
                          FALSE, FALSE, 0);
      gtk_widget_show (gui);
    }
}


/*  public functions  */

GtkWidget *
gimp_symmetry_editor_new (GimpMenuFactory *menu_factory)
{
  g_return_val_if_fail (GIMP_IS_MENU_FACTORY (menu_factory), NULL);

  return g_object_new (GIMP_TYPE_SYMMETRY_EDITOR,
                       "menu-factory",    menu_factory,
                       NULL);
}