File: propdialogs.c

package info (click to toggle)
dia 0.97.3%2Bgit20160930-9
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 54,372 kB
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,833; sh: 540; perl: 137; sed: 19
file content (402 lines) | stat: -rw-r--r-- 12,593 bytes parent folder | download | duplicates (3)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* Dia -- a diagram creation/manipulation program -*- c -*-
 * Copyright (C) 1998 Alexander Larsson
 *
 * Property system for dia objects/shapes.
 * Copyright (C) 2000 James Henstridge
 * Copyright (C) 2001 Cyrille Chepelov
 * Major restructuration done in August 2001 by C. Chepelov
 *
 * Routines for the construction of a property dialog.
 *
 * 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.
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include <gtk/gtk.h>
#define WIDGET GtkWidget
#include "widgets.h"
#include "properties.h"
#include "propinternals.h"
#include "object.h"

const gchar *prop_dialogdata_key = "object-props:dialogdata";

static void prop_dialog_signal_destroy(GtkWidget *dialog_widget);
static void prop_dialog_fill(PropDialog *dialog, GList *objects, gboolean is_default);

PropDialog *
prop_dialog_new(GList *objects, gboolean is_default) 
{
  PropDialog *dialog = g_new0(PropDialog,1);
  dialog->props = NULL;
  dialog->widget = gtk_vbox_new(FALSE,1);
  dialog->prop_widgets = g_array_new(FALSE,TRUE,sizeof(PropWidgetAssoc));
  dialog->copies = NULL;
  dialog->curtable = NULL;
  dialog->containers = g_ptr_array_new();

  prop_dialog_container_push(dialog,dialog->widget);

  g_object_set_data (G_OBJECT (dialog->widget), prop_dialogdata_key, dialog);
  g_signal_connect (G_OBJECT (dialog->widget), "destroy",
                    G_CALLBACK (prop_dialog_signal_destroy), NULL);

  prop_dialog_fill(dialog,objects,is_default);

  return dialog;
}

void
prop_dialog_destroy(PropDialog *dialog)
{
  if (dialog->props) prop_list_free(dialog->props);
  g_array_free(dialog->prop_widgets,TRUE);
  g_ptr_array_free(dialog->containers,TRUE);
  if (dialog->copies) destroy_object_list(dialog->copies);
  g_free(dialog);
}

PropDialog *
prop_dialog_from_widget(GtkWidget *dialog_widget)
{
  return g_object_get_data (G_OBJECT (dialog_widget), prop_dialogdata_key);
}

static void 
prop_dialog_signal_destroy(GtkWidget *dialog_widget)
{
  prop_dialog_destroy(prop_dialog_from_widget(dialog_widget));
}

WIDGET *
prop_dialog_get_widget(const PropDialog *dialog)
{
  return dialog->widget;
}

void 
prop_dialog_add_raw(PropDialog *dialog, GtkWidget *widget)
{
  dialog->curtable = NULL;
  if (!widget) return;
  gtk_container_add(GTK_CONTAINER(dialog->lastcont),widget);
}

void 
prop_dialog_add_raw_with_flags(PropDialog *dialog, GtkWidget *widget,
			       gboolean expand, gboolean fill)
{
  g_return_if_fail(GTK_IS_BOX(dialog->lastcont));

  dialog->curtable = NULL;
  if (!widget) return;
  gtk_box_pack_start(GTK_BOX(dialog->lastcont),widget, expand, fill, 0);
}

static void
prop_dialog_make_curtable(PropDialog *dialog) 
{
  GtkWidget *table = gtk_table_new(1,2,FALSE);  
  gtk_table_set_row_spacings(GTK_TABLE(table), 2);
  gtk_table_set_col_spacings(GTK_TABLE(table), 5);
  gtk_widget_show(table);
  prop_dialog_add_raw(dialog,table);

  dialog->currow = 0;
  dialog->curtable = table;
}

static void
prop_dialog_add_widget(PropDialog *dialog, GtkWidget *label, GtkWidget *widget)
{
  gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);

  if (!dialog->curtable) prop_dialog_make_curtable(dialog);
  gtk_table_attach(GTK_TABLE(dialog->curtable),label, 
                   0,1,
                   dialog->currow, dialog->currow+1,
                   GTK_FILL, GTK_FILL|GTK_EXPAND,
                   0,0);
  gtk_table_attach(GTK_TABLE(dialog->curtable),widget, 
                   1,2,
                   dialog->currow, dialog->currow+1,
                   GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND,
                   0,0);
  gtk_widget_show(label);
  gtk_widget_show(widget);
  dialog->currow++;
}

/* Register a new container widget (which won't be automatically added) */
void 
prop_dialog_container_push(PropDialog *dialog, GtkWidget *container)
{
  g_ptr_array_add(dialog->containers,container);
  dialog->lastcont = container;
  dialog->curtable = NULL;
}

/* De-register the last container of the stack. */
GtkWidget *
prop_dialog_container_pop(PropDialog *dialog)
{
  GtkWidget *res = g_ptr_array_remove_index(dialog->containers,
                                            dialog->containers->len-1);
  dialog->lastcont = g_ptr_array_index(dialog->containers,
                                       dialog->containers->len-1);
  dialog->curtable = NULL;

  return res;
}

static void 
property_signal_handler(GObject *obj,
                        gpointer func_data)
{
  PropEventData *ped = (PropEventData *)func_data;
  if (ped) {
    PropDialog *dialog = ped->dialog;
    Property *prop = ped->self;
    GList *list, *tmp;
    guint j;

    list = dialog->copies;
    g_return_if_fail(list);

    prop->experience &= ~PXP_NOTSET;

    if (!prop->event_handler)
	return;

    /* g_message("Received a signal for object %s from property %s",
       obj->type->name,prop->descr->name); */

    /* obj is a scratch object ; we can do what we want with it. */
    prop_get_data_from_widgets(dialog);

    for (tmp = list; tmp != NULL; tmp = tmp->next) {
      DiaObject *obj = (DiaObject*)tmp->data;
      obj->ops->set_props(obj,dialog->props);
      prop->event_handler(obj,prop);
      obj->ops->get_props(obj,dialog->props);
    }

    for (j = 0; j < dialog->prop_widgets->len; j++) {
      PropWidgetAssoc *pwa = 
        &g_array_index(dialog->prop_widgets,PropWidgetAssoc,j);
      /* The event handler above might have changed every property 
       * so we would have to mark them all as set (aka. to be applied).
       * But doing so would not work well with multiple/grouped objects
       * with unchanged and unequal object properties. Idea: keep as
       * set, what was before calling reset (but it's too late after get_props).
       *
       * See also commonprop_reset_widget() for more information
       */
      gboolean was_set = ((pwa->prop->experience & PXP_NOTSET) == 0);
      pwa->prop->ops->reset_widget(pwa->prop,pwa->widget);
      if (was_set)
	pwa->prop->experience &= ~PXP_NOTSET;
    }
    /* once more at least for _this_ property otherwise a property with
     * signal handler attached would not be changed at all ...
     */
    prop->experience &= ~PXP_NOTSET;
  } else {
    g_assert_not_reached();
  }
}

void 
prophandler_connect(const Property *prop,
                    GObject *object,
                    const gchar *signal)
{
  if (0==strcmp(signal,"FIXME")) {
    g_warning("signal type unknown for this kind of property (name is %s), \n"
              "handler ignored.",prop->descr->name);
    return;
  } 

  g_signal_connect (G_OBJECT (object),
                    signal,
                    G_CALLBACK (property_signal_handler),
                    (gpointer)(&prop->self_event_data));
}

void 
prop_dialog_add_property(PropDialog *dialog, Property *prop)
{
  GtkWidget *widget = NULL;
  PropWidgetAssoc pwa;
  GtkWidget *label;

  prop->self_event_data.dialog = dialog;
  prop->self_event_data.self = prop;

  if (prop->ops->get_widget) 
    widget = prop->ops->get_widget(prop,dialog);  
  if (!widget) 
    return; /* either property has no widget or it's a container */

  prop->self_event_data.widget = widget;
  if (prop->ops->reset_widget) prop->ops->reset_widget(prop,widget);
  prop->experience |= PXP_NOTSET;
  
  pwa.prop = prop;
  pwa.widget = widget;  
  g_array_append_val(dialog->prop_widgets,pwa);  

  /* Don't let gettext translate the empty string (it would give po-info).
   * Admitted it is a hack the string is empty for meta info, but the
   * gettext behavior looks suspicious, too.
   */
  if (strlen (prop->descr->description) == 0)
    label = gtk_label_new("");
  else
    label = gtk_label_new(_(prop->descr->description));
  gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);

  prop_dialog_add_widget(dialog, label, widget);
}

static void 
prop_dialog_add_properties(PropDialog *dialog, GPtrArray *props)
{
  guint i;
  gboolean scrollable = (props->len > 16);

  if (scrollable) {
    GtkWidget *swin = gtk_scrolled_window_new (NULL, NULL); 
    GtkWidget *vbox = gtk_vbox_new(FALSE,2);
    gtk_box_pack_start(GTK_BOX (dialog->widget), swin, TRUE, TRUE, 0);
    gtk_widget_show (swin);
    gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (swin), vbox);
    gtk_viewport_set_shadow_type(GTK_VIEWPORT(gtk_bin_get_child(GTK_BIN(swin))), GTK_SHADOW_NONE);
    gtk_widget_show (vbox);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
    prop_dialog_container_push (dialog, swin);
    prop_dialog_container_push (dialog, vbox);
  }


  for (i = 0; i < props->len; i++) {
    Property *prop = (Property*)g_ptr_array_index(props,i);
    prop_dialog_add_property(dialog, prop);
  }

  if (scrollable) {
    GtkRequisition requisition;
    GtkWidget *vbox = prop_dialog_container_pop(dialog);
    GtkWidget *swin = prop_dialog_container_pop(dialog);
    GdkScreen *screen = gtk_widget_get_screen(swin);
    gint sheight = screen ? (2 * gdk_screen_get_height(screen)) / 3 : 400;

    gtk_widget_size_request (vbox, &requisition);
    /* I'd say default size calculation for scrollable is quite broken */
    gtk_widget_set_size_request (swin, -1, requisition.height > sheight ? sheight : requisition.height);    
  }
}

void 
prop_get_data_from_widgets(PropDialog *dialog)
{
  guint i;
  for (i = 0; i < dialog->prop_widgets->len; i++) {
    PropWidgetAssoc *pwa = &g_array_index(dialog->prop_widgets,
                                          PropWidgetAssoc,i);
    pwa->prop->ops->set_from_widget(pwa->prop,pwa->widget);
    
  }
}

static gboolean
pdtpp_is_visible_default (const PropDescription *pdesc)
{
  return pdtpp_defaults (pdesc) && pdtpp_is_visible_no_standard(pdesc);
}

static void
_prop_list_extend_for_meta (GPtrArray *props)
{
  static PropDescription extras[] = {
    PROP_STD_NOTEBOOK_BEGIN,
    PROP_NOTEBOOK_PAGE("general_page",PROP_FLAG_DONT_MERGE,N_("General")),
    PROP_NOTEBOOK_PAGE("meta_page",0,N_("Meta")),
    { "meta", PROP_TYPE_DICT, PROP_FLAG_VISIBLE|PROP_FLAG_SELF_ONLY, "", ""},
    PROP_STD_NOTEBOOK_END,
    {NULL}
  };

  /* Some objects have no properties in case of defaults */
  Property *p = props->len > 0 ? g_ptr_array_index(props,0) : NULL;
  GPtrArray *pex = prop_list_from_descs(extras,pdtpp_is_visible);

  if (!p || strcmp (p->descr->type, PROP_TYPE_NOTEBOOK_BEGIN) != 0) {
    int i, olen = props->len;
    /* wrap everything into a first notebook page */
    g_ptr_array_set_size (props, olen + 2);
    /* make room for 2 at the beginning */
    for (i = olen - 1; i >=  0; --i)
      g_ptr_array_index (props, i + 2) = g_ptr_array_index (props, i);
    g_ptr_array_index (props, 0) = g_ptr_array_index (pex, 0);
    g_ptr_array_index (props, 1) = g_ptr_array_index (pex, 1);
  } else {
    p = g_ptr_array_index (props, props->len - 1);
    g_assert (strcmp (p->descr->type, PROP_TYPE_NOTEBOOK_END) == 0);
    /* drop the end, we'll add it again below */
    g_ptr_array_set_size (props, props->len - 1);
  }
  g_ptr_array_add (props, g_ptr_array_index (pex, 2));
  g_ptr_array_add (props, g_ptr_array_index (pex, 3));
  g_ptr_array_add (props, g_ptr_array_index (pex, 4));
  /* free the array, but not the reused segments */
  g_ptr_array_free (pex, FALSE);
}

static void 
prop_dialog_fill(PropDialog *dialog, GList *objects, gboolean is_default)
{
  const PropDescription *pdesc;
  GPtrArray *props;

  g_return_if_fail(objects_comply_with_stdprop(objects));

  dialog->objects = g_list_copy(objects);
  dialog->copies = object_copy_list(objects);

  pdesc = object_list_get_prop_descriptions(objects, PROP_UNION);
  if (!pdesc) return;

  if (is_default)
    props = prop_list_from_descs(pdesc,pdtpp_is_visible_default);
  else
    props = prop_list_from_descs(pdesc,pdtpp_is_visible);

  if (!props) return;

  _prop_list_extend_for_meta (props);

  dialog->props = props;
  object_list_get_props(objects, props);

  prop_dialog_add_properties(dialog, props);
}