glade-utils.c 56.7 KB
Newer Older
Chema Celorio's avatar
Chema Celorio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (C) 2001 Ximian, Inc.
 *
 * 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
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chema Celorio's avatar
Chema Celorio committed
17 18 19 20
 *
 * Authors:
 *   Chema Celorio <chema@celorio.com>
 */
21

22
#include <config.h>
23

24 25 26 27 28 29 30 31
/**
 * SECTION:glade-utils
 * @Title: Glade Utils
 * @Short_Description: Welcome to the zoo.
 *
 * This is where all of that really usefull miscalanious stuff lands up.
 */

32
#include "glade.h"
Joaquín Cuenca Abela's avatar
Joaquín Cuenca Abela committed
33 34 35
#include "glade-project.h"
#include "glade-command.h"
#include "glade-debug.h"
36
#include "glade-placeholder.h"
37
#include "glade-widget.h"
38
#include "glade-widget-adaptor.h"
39 40
#include "glade-property.h"
#include "glade-property-class.h"
41
#include "glade-clipboard.h"
42
#include "glade-private.h"
Chema Celorio's avatar
Chema Celorio committed
43

44 45 46 47 48 49 50
#include <string.h>
#include <gdk/gdkkeysyms.h>
#include <gmodule.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
#include <errno.h>

Tristan Van Berkom's avatar
Tristan Van Berkom committed
51
#ifdef G_OS_WIN32
52 53 54
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
Tristan Van Berkom's avatar
Tristan Van Berkom committed
55 56
#endif

57
#define GLADE_UTIL_COPY_BUFFSIZE       1024
Chema Celorio's avatar
Chema Celorio committed
58

Tristan Van Berkom's avatar
Tristan Van Berkom committed
59

60 61 62 63 64 65 66 67
/**
 * glade_util_compose_get_type_func:
 * @name:
 *
 * TODO: write me
 *
 * Returns:
 */
68
static gchar *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
69
glade_util_compose_get_type_func (const gchar *name)
70
{
71 72 73
  gchar *retval;
  GString *tmp;
  gint i = 1, j;
74

75
  tmp = g_string_new (name);
76

77 78 79 80 81
  while (tmp->str[i])
    {
      if (g_ascii_isupper (tmp->str[i]))
        {
          tmp = g_string_insert_c (tmp, i++, '_');
82

83 84 85
          j = 0;
          while (g_ascii_isupper (tmp->str[i++]))
            j++;
86

87 88
          if (j > 2)
            g_string_insert_c (tmp, i - 2, '_');
89

90 91 92 93
          continue;
        }
      i++;
    }
94

95 96 97
  tmp = g_string_append (tmp, "_get_type");
  retval = g_ascii_strdown (tmp->str, tmp->len);
  g_string_free (tmp, TRUE);
98

99
  return retval;
100 101
}

102 103
/**
 * glade_util_get_type_from_name:
104 105
 * @name: the name of the #GType - like 'GtkWidget' or a "get-type" function.
 * @have_func: function-name flag -- true if the name is a "get-type" function.
106
 *
107 108 109
 * Returns the type using the "get type" function name based on @name.  
 * If the @have_func flag is true,@name is used directly, otherwise the get-type 
 * function is contrived from @name then used.
110
 *
111
 * Returns: the new #GType
112
 */
113
GType
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
114
glade_util_get_type_from_name (const gchar *name, gboolean have_func)
115 116
{
  static GModule *allsymbols = NULL;
117
  GType (*get_type) (void);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  GType type = 0;
  gchar *func_name = (gchar *) name;

  if ((type = g_type_from_name (name)) == 0 &&
      (have_func ||
       (func_name = glade_util_compose_get_type_func (name)) != NULL))
    {

      if (!allsymbols)
        allsymbols = g_module_open (NULL, 0);

      if (g_module_symbol (allsymbols, func_name, (gpointer) & get_type))
        {
          g_assert (get_type);
          type = get_type ();
        }
      else
        {
          g_warning (_("We could not find the symbol \"%s\""), func_name);
        }
138 139 140

      if (!have_func)
	g_free (func_name);
141 142 143 144 145 146
    }

  if (type == 0)
    g_warning (_("Could not get the type from \"%s\""), name);

  return type;
147 148 149 150
}

/**
 * glade_utils_get_pspec_from_funcname:
151
 * @funcname: the symbol name of a function to generate a #GParamSpec
152
 *
153 154
 * Returns: A #GParamSpec created by the delagate function
 *          specified by @funcname
155 156
 */
GParamSpec *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
157
glade_utils_get_pspec_from_funcname (const gchar *funcname)
158
{
159 160
  static GModule *allsymbols = NULL;
  GParamSpec *pspec = NULL;
161
  GParamSpec *(*get_pspec) (void) = NULL;
162 163 164

  if (!allsymbols)
    allsymbols = g_module_open (NULL, 0);
165

166 167 168 169 170
  if (!g_module_symbol (allsymbols, funcname, (gpointer) & get_pspec))
    {
      g_warning (_("We could not find the symbol \"%s\""), funcname);
      return NULL;
    }
171

172 173
  g_assert (get_pspec);
  pspec = get_pspec ();
174

175
  return pspec;
176 177
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
void
_glade_util_dialog_set_hig (GtkDialog *dialog)
{
  GtkWidget *vbox, *action_area;

  /* HIG spacings */
  vbox = gtk_dialog_get_content_area (dialog);
  gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
  gtk_box_set_spacing (GTK_BOX (vbox), 2); /* 2 * 5 + 2 = 12 */

  action_area = gtk_dialog_get_action_area (dialog);
  gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
  gtk_box_set_spacing (GTK_BOX (action_area), 6);
}

193
/**
194
 * glade_util_ui_message:
195
 * @parent: a #GtkWindow cast as a #GtkWidget
196
 * @type:   a #GladeUIMessageType
197 198
 * @widget: a #GtkWidget to append to the dialog vbox
 * @format: a printf style format string
199
 * @...:    args for the format.
200 201
 *
 * Creates a new warning dialog window as a child of @parent containing
202 203 204 205 206 207
 * the text of @format, runs it, then destroys it on close. Depending
 * on @type, a cancel button may apear or the icon may change.
 *
 * Returns: True if the @type was GLADE_UI_ARE_YOU_SURE and the user
 *          selected "OK", True if the @type was GLADE_UI_YES_OR_NO and
 *          the user selected "YES"; False otherwise.
208
 */
209
gint
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
210
glade_util_ui_message (GtkWidget *parent,
211
                       GladeUIMessageType type,
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
212 213 214
                       GtkWidget *widget,
                       const gchar *format,
                       ...)
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
{
  GtkWidget *dialog;
  GtkMessageType message_type = GTK_MESSAGE_INFO;
  GtkButtonsType buttons_type = GTK_BUTTONS_OK;
  va_list args;
  gchar *string;
  gint response;

  va_start (args, format);
  string = g_strdup_vprintf (format, args);
  va_end (args);

  /* Get message_type */
  switch (type)
    {
      case GLADE_UI_INFO:
        message_type = GTK_MESSAGE_INFO;
        break;
      case GLADE_UI_WARN:
      case GLADE_UI_ARE_YOU_SURE:
        message_type = GTK_MESSAGE_WARNING;
        break;
      case GLADE_UI_ERROR:
        message_type = GTK_MESSAGE_ERROR;
        break;
      case GLADE_UI_YES_OR_NO:
        message_type = GTK_MESSAGE_QUESTION;
        break;
        break;
      default:
        g_critical ("Bad arg for glade_util_ui_message");
        break;
    }


  /* Get buttons_type */
  switch (type)
    {
      case GLADE_UI_INFO:
      case GLADE_UI_WARN:
      case GLADE_UI_ERROR:
        buttons_type = GTK_BUTTONS_OK;
        break;
      case GLADE_UI_ARE_YOU_SURE:
        buttons_type = GTK_BUTTONS_OK_CANCEL;
        break;
      case GLADE_UI_YES_OR_NO:
        buttons_type = GTK_BUTTONS_YES_NO;
        break;
        break;
      default:
        g_critical ("Bad arg for glade_util_ui_message");
        break;
    }

  dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
                                   GTK_DIALOG_DESTROY_WITH_PARENT,
272 273 274
                                   message_type, buttons_type, NULL);
  
  gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog), string);
275 276

  if (widget)
277 278 279 280 281
    {
      gtk_box_pack_end (GTK_BOX
                        (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
                        widget, TRUE, TRUE, 2);
      gtk_widget_show (widget);
282 283 284

      /* If theres additional content, make it resizable */
      gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
285 286
    }
  
287 288 289 290 291 292
  response = gtk_dialog_run (GTK_DIALOG (dialog));

  gtk_widget_destroy (dialog);
  g_free (string);

  return (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_YES);
293
}
294

295

296
gboolean
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
297 298 299
glade_util_check_and_warn_scrollable (GladeWidget *parent,
                                      GladeWidgetAdaptor *child_adaptor,
                                      GtkWidget *parent_widget)
300
{
301
  if (GTK_IS_SCROLLED_WINDOW (glade_widget_get_object (parent)) &&
302 303 304 305
      GWA_SCROLLABLE_WIDGET (child_adaptor) == FALSE)
    {
      GladeWidgetAdaptor *vadaptor =
          glade_widget_adaptor_get_by_type (GTK_TYPE_VIEWPORT);
306
      GladeWidgetAdaptor *parent_adaptor = glade_widget_get_adaptor (parent);
307 308 309

      glade_util_ui_message (parent_widget,
                             GLADE_UI_INFO, NULL,
310
                             _("Cannot add non scrollable %s widget to a %s directly.\n"
311 312 313 314
			       "Add a %s first."), 
			     glade_widget_adaptor_get_title (child_adaptor),
                             glade_widget_adaptor_get_title (parent_adaptor), 
			     glade_widget_adaptor_get_title (vadaptor));
315 316 317 318 319 320 321 322 323 324
      return TRUE;
    }
  return FALSE;
}

typedef struct
{
  GtkStatusbar *statusbar;
  guint context_id;
  guint message_id;
325 326
} FlashInfo;

327
static const guint flash_length = 3;
328 329

static gboolean
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
330
remove_message_timeout (FlashInfo *fi)
331
{
332
  gtk_statusbar_remove (fi->statusbar, fi->context_id, fi->message_id);
333
  g_slice_free (FlashInfo, fi);
334

335 336
  /* remove the timeout */
  return FALSE;
337 338 339 340
}

/**
 * glade_utils_flash_message:
341
 * @statusbar: The statusbar
342 343 344 345
 * @context_id: The message context_id
 * @format: The message to flash on the statusbar
 *
 * Flash a temporary message on the statusbar.
346
 */
347
void
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
348 349 350 351
glade_util_flash_message (GtkWidget *statusbar,
                          guint context_id,
                          gchar *format,
                          ...)
352
{
353 354 355
  va_list args;
  FlashInfo *fi;
  gchar *message;
356

357 358
  g_return_if_fail (GTK_IS_STATUSBAR (statusbar));
  g_return_if_fail (format != NULL);
359

360 361 362
  va_start (args, format);
  message = g_strdup_vprintf (format, args);
  va_end (args);
363

364
  fi = g_slice_new0 (FlashInfo);
365 366 367
  fi->statusbar = GTK_STATUSBAR (statusbar);
  fi->context_id = context_id;
  fi->message_id = gtk_statusbar_push (fi->statusbar, fi->context_id, message);
368

369 370
  g_timeout_add_seconds (flash_length, (GSourceFunc) remove_message_timeout,
                         fi);
371

372
  g_free (message);
373 374
}

375
static gint
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
376
glade_util_compare_uline_labels (const gchar *labela, const gchar *labelb)
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 403 404 405 406 407 408
  for (;;)
    {
      gunichar c1, c2;

      if (*labela == '\0')
        return (*labelb == '\0') ? 0 : -1;
      if (*labelb == '\0')
        return 1;

      c1 = g_utf8_get_char (labela);
      if (c1 == '_')
        {
          labela = g_utf8_next_char (labela);
          c1 = g_utf8_get_char (labela);
        }

      c2 = g_utf8_get_char (labelb);
      if (c2 == '_')
        {
          labelb = g_utf8_next_char (labelb);
          c2 = g_utf8_get_char (labelb);
        }

      if (c1 < c2)
        return -1;
      if (c1 > c2)
        return 1;

      labela = g_utf8_next_char (labela);
      labelb = g_utf8_next_char (labelb);
    }
409

410 411
  /* Shouldn't be reached. */
  return 0;
412 413
}

414 415 416 417 418 419 420 421 422 423 424
/**
 * glade_util_compare_stock_labels:
 * @a: a #gconstpointer to a #GtkStockItem
 * @b: a #gconstpointer to a #GtkStockItem
 *
 * This is a #GCompareFunc that compares the labels of two stock items, 
 * ignoring any '_' characters. It isn't particularly efficient.
 *
 * Returns: negative value if @a < @b; zero if @a = @b; 
 *          positive value if @a > @b
 */
425 426 427
gint
glade_util_compare_stock_labels (gconstpointer a, gconstpointer b)
{
428 429 430 431 432
  const gchar *stock_ida = a, *stock_idb = b;
  GtkStockItem itema, itemb;
  gboolean founda, foundb;
  gint retval;

433
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
434 435
  founda = gtk_stock_lookup (stock_ida, &itema);
  foundb = gtk_stock_lookup (stock_idb, &itemb);
436
G_GNUC_END_IGNORE_DEPRECATIONS
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

  if (founda)
    {
      if (!foundb)
        retval = -1;
      else
        /* FIXME: Not ideal for UTF-8. */
        retval = glade_util_compare_uline_labels (itema.label, itemb.label);
    }
  else
    {
      if (!foundb)
        retval = 0;
      else
        retval = 1;
    }

  return retval;
455 456
}

457
/**
458
 * glade_util_file_dialog_new:
459
 * @title: dialog title
460 461
 * @project: a #GladeProject used when saving
 * @parent: a parent #GtkWindow for the dialog
462
 * @action: a #GladeUtilFileDialogType to say if the dialog will open or save
463
 *
464
 * Returns: a "glade file" file chooser dialog. The caller is responsible 
465
 *          for showing the dialog
466
 */
467
GtkWidget *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
468 469 470 471
glade_util_file_dialog_new (const gchar *title,
                            GladeProject *project,
                            GtkWindow *parent,
                            GladeUtilFileDialogType action)
472 473 474 475 476 477 478 479 480 481 482
{
  GtkWidget *file_dialog;
  GtkFileFilter *file_filter;

  g_return_val_if_fail ((action == GLADE_FILE_DIALOG_ACTION_OPEN ||
                         action == GLADE_FILE_DIALOG_ACTION_SAVE), NULL);

  g_return_val_if_fail ((action != GLADE_FILE_DIALOG_ACTION_SAVE ||
                         GLADE_IS_PROJECT (project)), NULL);

  file_dialog = gtk_file_chooser_dialog_new (title, parent, action,
483
                                             _("_Cancel"), GTK_RESPONSE_CANCEL,
484 485
                                             action ==
                                             GLADE_FILE_DIALOG_ACTION_OPEN ?
486
                                             _("_Open") : _("_Save"),
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
                                             GTK_RESPONSE_OK, NULL);

  file_filter = gtk_file_filter_new ();
  gtk_file_filter_add_pattern (file_filter, "*");
  gtk_file_filter_set_name (file_filter, _("All Files"));
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (file_dialog), file_filter);

  file_filter = gtk_file_filter_new ();
  gtk_file_filter_add_pattern (file_filter, "*.glade");
  gtk_file_filter_set_name (file_filter, _("Libglade Files"));
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (file_dialog), file_filter);

  file_filter = gtk_file_filter_new ();
  gtk_file_filter_add_pattern (file_filter, "*.ui");
  gtk_file_filter_set_name (file_filter, _("GtkBuilder Files"));
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (file_dialog), file_filter);

  file_filter = gtk_file_filter_new ();
  gtk_file_filter_add_pattern (file_filter, "*.ui");
  gtk_file_filter_add_pattern (file_filter, "*.glade");
  gtk_file_filter_set_name (file_filter, _("All Glade Files"));
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (file_dialog), file_filter);

  gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (file_dialog), file_filter);

  gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER
                                                  (file_dialog), TRUE);
  gtk_dialog_set_default_response (GTK_DIALOG (file_dialog), GTK_RESPONSE_OK);

  return file_dialog;
517
}
518

519
/**
520 521
 * glade_util_replace:
 * @str: a string
522 523
 * @a: a #gchar
 * @b: a #gchar
524 525
 *
 * Replaces each occurance of the character @a in @str to @b.
526 527
 */
void
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
528
glade_util_replace (gchar *str, gchar a, gchar b)
529
{
530
  g_return_if_fail (str != NULL);
531

532 533 534 535
  while (*str != 0)
    {
      if (*str == a)
        *str = b;
536

537 538
      str = g_utf8_next_char (str);
    }
539
}
540

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
/**
 * _glade_util_strreplace:
 * @str: a string
 * @free_str: wheter to free str or not
 * @key: the key string to search for
 * @replacement: string to replace key
 *
 * Replaces each occurance of the string @key in @str to @replacement.
 */
gchar *
_glade_util_strreplace (gchar *str,
                        gboolean free_str,
                        const gchar *key,
                        const gchar *replacement)
{
  gchar *retval, **array;

  if ((array = g_strsplit (str, key, -1)) && array[0])
    retval = g_strjoinv (replacement, array);
  else
    retval = g_strdup (str);

  g_strfreev (array);

  if (free_str)
    g_free (str);
	
  return retval;
}

Tristan Van Berkom's avatar
Tristan Van Berkom committed
571 572 573 574 575 576 577 578
/**
 * glade_util_read_prop_name:
 * @str: a string
 *
 * Return a usable version of a property identifier as found
 * in a freshly parserd #GladeInterface
 */
gchar *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
579
glade_util_read_prop_name (const gchar *str)
Tristan Van Berkom's avatar
Tristan Van Berkom committed
580
{
581
  gchar *id;
582

583
  g_return_val_if_fail (str != NULL, NULL);
584

585
  id = g_strdup (str);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
586

587
  glade_util_replace (id, '_', '-');
Tristan Van Berkom's avatar
Tristan Van Berkom committed
588

589
  return id;
Tristan Van Berkom's avatar
Tristan Van Berkom committed
590 591 592
}


593
/**
594 595 596 597 598 599 600
 * glade_util_duplicate_underscores:
 * @name: a string
 *
 * Duplicates @name, but the copy has two underscores in place of any single
 * underscore in the original.
 *
 * Returns: a newly allocated string
601
 */
Tristan Van Berkom's avatar
Tristan Van Berkom committed
602
gchar *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
603
glade_util_duplicate_underscores (const gchar *name)
604
{
605 606 607 608
  const gchar *tmp;
  const gchar *last_tmp = name;
  gchar *underscored_name = g_malloc (strlen (name) * 2 + 1);
  gchar *tmp_underscored = underscored_name;
609

610 611 612 613 614 615 616 617 618 619
  for (tmp = last_tmp; *tmp; tmp = g_utf8_next_char (tmp))
    {
      if (*tmp == '_')
        {
          memcpy (tmp_underscored, last_tmp, tmp - last_tmp + 1);
          tmp_underscored += tmp - last_tmp + 1;
          last_tmp = tmp + 1;
          *tmp_underscored++ = '_';
        }
    }
620

621
  memcpy (tmp_underscored, last_tmp, tmp - last_tmp + 1);
622

623
  return underscored_name;
624
}
Joaquín Cuenca Abela's avatar
Joaquín Cuenca Abela committed
625

626 627 628 629 630
/*
 * taken from gtk... maybe someday we can convince them to
 * expose gtk_container_get_all_children
 */
static void
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
631
gtk_container_children_callback (GtkWidget *widget, gpointer client_data)
632
{
633
  GList **children;
634

635
  children = (GList **) client_data;
636 637
  if (!g_list_find (*children, widget))
    *children = g_list_prepend (*children, widget);
638 639
}

640 641 642 643
/**
 * glade_util_container_get_all_children:
 * @container: a #GtkContainer
 *
644 645 646 647 648 649 650
 * Use this to itterate over all children in a GtkContainer,
 * as it used _forall() instead of _foreach() (and the GTK+ version
 * of this function is simply not exposed).
 *
 * Note that glade_widget_class_get_children() is the high-level
 * abstraction and will usually end up calling this function.
 *
651 652
 * Returns: a #GList giving the contents of @container
 */
653
GList *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
654
glade_util_container_get_all_children (GtkContainer *container)
655
{
656
  GList *children = NULL;
657

658
  g_return_val_if_fail (GTK_IS_CONTAINER (container), NULL);
659

660
  gtk_container_forall (container, gtk_container_children_callback, &children);
661
  gtk_container_foreach (container, gtk_container_children_callback, &children);
662

663 664
  /* Preserve the natural order by reversing the list */
  return g_list_reverse (children);
665
}
666

667
/**
668
 * glade_util_count_placeholders:
669 670
 * @parent: a #GladeWidget
 *
671
 * Returns: the amount of #GladePlaceholders parented by @parent
672 673
 */
gint
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
674
glade_util_count_placeholders (GladeWidget *parent)
675
{
676 677
  gint placeholders = 0;
  GList *list, *children;
678

679
  /* count placeholders */
680 681 682
  if ((children = 
       glade_widget_adaptor_get_children (glade_widget_get_adaptor (parent), 
					  glade_widget_get_object (parent))) != NULL)
683 684 685 686 687 688 689 690
    {
      for (list = children; list && list->data; list = list->next)
        {
          if (GLADE_IS_PLACEHOLDER (list->data))
            placeholders++;
        }
      g_list_free (children);
    }
691

692
  return placeholders;
693
}
694 695

static GtkTreeIter *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
696 697 698 699
glade_util_find_iter (GtkTreeModel *model,
                      GtkTreeIter *iter,
                      GladeWidget *findme,
                      gint column)
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
{
  GtkTreeIter *retval = NULL;
  GObject *object = NULL;
  GtkTreeIter *next;

  g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
  g_return_val_if_fail (iter != NULL, NULL);

  next = gtk_tree_iter_copy (iter);
  g_return_val_if_fail (next != NULL, NULL);

  while (retval == NULL)
    {
      GladeWidget *widget;

      gtk_tree_model_get (model, next, column, &object, -1);
      if (object &&
          gtk_tree_model_get_column_type (model, column) == G_TYPE_OBJECT)
        g_object_unref (object);

      widget = glade_widget_get_from_gobject (object);

      if (widget == findme)
        {
          retval = gtk_tree_iter_copy (next);
          break;
        }
      else if (glade_widget_is_ancestor (findme, widget))
        {
          if (gtk_tree_model_iter_has_child (model, next))
            {
              GtkTreeIter child;
              gtk_tree_model_iter_children (model, &child, next);
              if ((retval = glade_util_find_iter
                   (model, &child, findme, column)) != NULL)
                break;
            }

          /* Only search the branches where the searched widget
           * is actually a child of the this row, optimize the
           * searching this way
           */
          break;
        }

      if (!gtk_tree_model_iter_next (model, next))
        break;
    }
  gtk_tree_iter_free (next);

  return retval;
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
}

/**
 * glade_util_find_iter_by_widget:
 * @model: a #GtkTreeModel
 * @findme: a #GladeWidget
 * @column: a #gint
 *
 * Looks through @model for the #GtkTreeIter corresponding to 
 * @findme under @column.
 *
 * Returns: a newly allocated #GtkTreeIter from @model corresponding
 * to @findme which should be freed with gtk_tree_iter_free()
 * 
 */
GtkTreeIter *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
767 768 769
glade_util_find_iter_by_widget (GtkTreeModel *model,
                                GladeWidget *findme,
                                gint column)
770
{
771 772 773 774 775 776
  GtkTreeIter iter;
  if (gtk_tree_model_get_iter_first (model, &iter))
    {
      return glade_util_find_iter (model, &iter, findme, column);
    }
  return NULL;
777
}
778

779 780 781 782
/**
 * glade_util_purify_list:
 * @list: A #GList
 *
Tristan Van Berkom's avatar
Tristan Van Berkom committed
783 784
 * Returns: A newly allocated version of @list with no 
 *          duplicate data entries
785 786
 */
GList *
787
glade_util_purify_list (GList * list)
788
{
789
  GList *l, *newlist = NULL;
790

791 792 793
  for (l = list; l; l = l->next)
    if (!g_list_find (newlist, l->data))
      newlist = g_list_prepend (newlist, l->data);
794

795
  g_list_free (list);
796

797
  return g_list_reverse (newlist);
798
}
799

Tristan Van Berkom's avatar
Tristan Van Berkom committed
800 801
/**
 * glade_util_added_in_list:
802 803
 * @old_list: the old #GList
 * @new_list: the new #GList
Tristan Van Berkom's avatar
Tristan Van Berkom committed
804 805 806 807 808 809
 *
 * Returns: A newly allocated #GList of elements that
 *          are in @new but not in @old
 *
 */
GList *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
810
glade_util_added_in_list (GList *old_list, GList *new_list)
Tristan Van Berkom's avatar
Tristan Van Berkom committed
811
{
812
  GList *added = NULL, *list;
Tristan Van Berkom's avatar
Tristan Van Berkom committed
813

814 815 816 817 818
  for (list = new_list; list; list = list->next)
    {
      if (!g_list_find (old_list, list->data))
        added = g_list_prepend (added, list->data);
    }
Tristan Van Berkom's avatar
Tristan Van Berkom committed
819

820
  return g_list_reverse (added);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
821 822 823 824
}

/**
 * glade_util_removed_from_list:
825 826
 * @old_list: the old #GList
 * @new_list: the new #GList
Tristan Van Berkom's avatar
Tristan Van Berkom committed
827 828 829 830 831 832
 *
 * Returns: A newly allocated #GList of elements that
 *          are in @old no longer in @new
 *
 */
GList *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
833
glade_util_removed_from_list (GList *old_list, GList *new_list)
Tristan Van Berkom's avatar
Tristan Van Berkom committed
834
{
835
  GList *added = NULL, *list;
Tristan Van Berkom's avatar
Tristan Van Berkom committed
836

837 838 839 840 841
  for (list = old_list; list; list = list->next)
    {
      if (!g_list_find (new_list, list->data))
        added = g_list_prepend (added, list->data);
    }
Tristan Van Berkom's avatar
Tristan Van Berkom committed
842

843
  return g_list_reverse (added);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
844 845 846
}


847 848 849 850 851 852 853 854 855 856 857 858 859 860
/**
 * glade_util_canonical_path:
 * @path: any path that may contain ".." or "." components
 *
 * Returns: an absolute path to the specified file or directory
 *          that contains no ".." or "." components (this does
 *          not call readlink like realpath() does).
 *
 * Note: on some systems; I think its possible that we dont have
 *       permission to execute in the directory in which the glade
 *       file resides; I decided finally to do it this way anyway
 *       since libc's realpath() does exactly the same.
 */
gchar *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
861
glade_util_canonical_path (const gchar *path)
862
{
863
  gchar *orig_dir, *dirname, *basename, *direct_dir, *direct_name = NULL;
864

865
  g_return_val_if_fail (path != NULL, NULL);
866

867
  basename = g_path_get_basename (path);
868

869 870 871 872 873 874 875
  if ((orig_dir = g_get_current_dir ()) != NULL)
    {
      if ((dirname = g_path_get_dirname (path)) != NULL)
        {
          if (g_chdir (dirname) == 0)
            {
              if ((direct_dir = g_get_current_dir ()) != NULL)
876 877 878 879
		{
		  direct_name = g_build_filename (direct_dir, basename, NULL);
		  g_free (direct_dir);
		}
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
              else
                g_warning ("g_path");

              if (g_chdir (orig_dir) != 0)
                g_warning ("Unable to chdir back to %s directory (%s)",
                           orig_dir, g_strerror (errno));

            }
          else
            g_warning ("Unable to chdir to %s directory (%s)",
                       dirname, g_strerror (errno));

          g_free (dirname);
        }
      else
        g_warning ("Unable to get directory component of %s\n", path);
      g_free (orig_dir);
    }
898

899 900
  if (basename)
    g_free (basename);
901

902
  return direct_name;
903
}
904

905
static GModule *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
906
try_load_library (const gchar *library_path, const gchar *library_name)
907
{
908
  gchar *path = g_module_build_path (library_path, library_name);
909
  GModule *module = NULL;
910

911
  if (!library_path || g_file_test (path, G_FILE_TEST_EXISTS))
912 913 914 915
    {
      if (!(module = g_module_open (path, G_MODULE_BIND_LAZY)))
        g_warning ("Failed to load %s: %s", path, g_module_error ());
    }
916

917
  g_free (path);
918

919
  return module;
920 921
}

922 923 924 925
/**
 * glade_util_load_library:
 * @library_name: name of the library
 *
926 927
 * Loads the named library from the Glade modules and lib directory or failing that
 * from the standard platform specific directories. (Including /usr/local/lib for unices)
928 929 930 931 932 933 934
 *
 * The @library_name should not include any platform specifix prefix or suffix,
 * those are automatically added, if needed, by g_module_build_path()
 *
 * Returns: a #GModule on success, or %NULL on failure.
 */
GModule *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
935
glade_util_load_library (const gchar *library_name)
936
{
937 938 939
  GModule *module = NULL;
  const gchar *search_path;
  gint i;
940

941 942
  if ((search_path = g_getenv (GLADE_ENV_MODULE_PATH)) != NULL)
    {
943 944
      gchar **split;

945 946 947 948 949 950 951 952 953
      if ((split = g_strsplit (search_path, ":", 0)) != NULL)
        {
          for (i = 0; split[i] != NULL; i++)
            if ((module = try_load_library (split[i], library_name)) != NULL)
              break;

          g_strfreev (split);
        }
    }
954

955
  if (g_getenv (GLADE_ENV_TESTING) == NULL && !module)
956
    {
957 958 959 960 961 962 963 964
      const gchar *paths[] = { glade_app_get_modules_dir (),
                               glade_app_get_lib_dir (),
#ifndef G_OS_WIN32 /* Try local lib dir on Unices */
                               "/usr/local/lib",
#endif
                               NULL}; /* Use default system paths */

      
965 966
      for (i = 0; i < G_N_ELEMENTS (paths); i++)
        if ((module = try_load_library (paths[i], library_name)) != NULL)
967 968
          break;
    }
969

970
  return module;
971
}
972 973 974 975 976 977 978 979 980 981

/**
 * glade_util_file_is_writeable:
 * @path:  the path to the file
 *
 * Checks whether the file at @path is writeable
 *
 * Returns: TRUE if file is writeable
 */
gboolean
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
982
glade_util_file_is_writeable (const gchar *path)
983
{
984 985
  GIOChannel *channel;
  g_return_val_if_fail (path != NULL, FALSE);
986

987 988 989 990 991 992 993
  /* The only way to really know if the file is writable */
  if ((channel = g_io_channel_new_file (path, "a+", NULL)) != NULL)
    {
      g_io_channel_unref (channel);
      return TRUE;
    }
  return FALSE;
994
}
995 996

/**
997
 * glade_util_have_devhelp:
998
 *
999
 * Returns: whether the devhelp module is loaded
1000
 */
1001 1002
gboolean
glade_util_have_devhelp (void)
1003
{
1004 1005 1006 1007
  static gint have_devhelp = -1;
  gchar *ptr;
  gint cnt, ret, major, minor;
  GError *error = NULL;
1008

1009 1010 1011 1012 1013 1014 1015
#define DEVHELP_OLD_MESSAGE  \
    "The DevHelp installed on your system is too old, " \
    "devhelp feature will be disabled."

#define DEVHELP_MISSING_MESSAGE  \
    "No DevHelp installed on your system, " \
    "devhelp feature will be disabled."
1016

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
  if (have_devhelp >= 0)
    return have_devhelp;

  have_devhelp = 0;

  if ((ptr = g_find_program_in_path ("devhelp")) != NULL)
    {
      g_free (ptr);

      if (g_spawn_command_line_sync ("devhelp --version",
                                     &ptr, NULL, &ret, &error))
        {
          /* If we have a successfull return code.. parse the output.
           */
          if (ret == 0)
            {
              gchar name[16];
              if ((cnt = sscanf (ptr, "%15s %d.%d\n",
                                 name, &major, &minor)) == 3)
                {
                  /* Devhelp 0.12 required.
                   */
1039
                  if (major >= 2 || (major >= 0 && minor >= 12))
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
                    have_devhelp = 1;
                  else
                    g_message (DEVHELP_OLD_MESSAGE);
                }
              else

                {
                  if (ptr != NULL || strlen (ptr) > 0)
                    g_warning ("devhelp had unparsable output: "
                               "'%s' (parsed %d elements)", ptr, cnt);
                  else
                    g_message (DEVHELP_OLD_MESSAGE);
                }
            }
          else
            g_warning ("devhelp had bad return code: '%d'", ret);
        }
      else
        {
          g_warning ("Error trying to launch devhelp: %s", error->message);
          g_error_free (error);
        }
    }
  else
    g_message (DEVHELP_MISSING_MESSAGE);

  return have_devhelp;
1067 1068
}

Tristan Van Berkom's avatar
Tristan Van Berkom committed
1069 1070 1071 1072 1073 1074 1075 1076
/**
 * glade_util_get_devhelp_icon:
 * @size: the preferred icon size
 *
 * Creates an image displaying the devhelp icon.
 *
 * Returns: a #GtkImage
 */
1077
GtkWidget *
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1078 1079
glade_util_get_devhelp_icon (GtkIconSize size)
{
1080 1081 1082 1083
  GtkIconTheme *icon_theme;
  GdkScreen *screen;
  GtkWidget *image;
  gchar *path;
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1084

1085 1086 1087
  image = gtk_image_new ();
  screen = gtk_widget_get_screen (GTK_WIDGET (image));
  icon_theme = gtk_icon_theme_get_for_screen (screen);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1088

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
  if (gtk_icon_theme_has_icon (icon_theme, GLADE_DEVHELP_ICON_NAME))
    {
      gtk_image_set_from_icon_name (GTK_IMAGE (image), GLADE_DEVHELP_ICON_NAME,
                                    size);
    }
  else
    {
      path =
          g_build_filename (glade_app_get_pixmaps_dir (),
                            GLADE_DEVHELP_FALLBACK_ICON_FILE, NULL);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1099

1100
      gtk_image_set_from_file (GTK_IMAGE (image), path);
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1101

1102 1103
      g_free (path);
    }
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1104

1105
  return image;
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1106 1107
}

1108 1109
/**
 * glade_util_search_devhep:
1110
 * @devhelp: the devhelp widget created by the devhelp module.
1111 1112 1113 1114
 * @book: the devhelp book (or %NULL)
 * @page: the page in the book (or %NULL)
 * @search: the search string (or %NULL)
 *
1115
 * Envokes devhelp with the appropriate search string
1116 1117 1118
 *
 */
void
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
1119 1120 1121
glade_util_search_devhelp (const gchar *book,
                           const gchar *page,
                           const gchar *search)
1122
{
1123 1124 1125
  GError *error = NULL;
  gchar *book_comm = NULL, *page_comm = NULL, *search_comm = NULL;
  gchar *string;
1126

1127
  g_return_if_fail (glade_util_have_devhelp ());
1128

1129 1130 1131 1132 1133 1134
  if (book)
    book_comm = g_strdup_printf ("book:%s", book);
  if (page)
    page_comm = g_strdup_printf (" page:%s", page);
  if (search)
    search_comm = g_strdup_printf (" %s", search);
1135

1136 1137 1138 1139
  string = g_strdup_printf ("devhelp -s \"%s%s%s\"",
                            book_comm ? book_comm : "",
                            page_comm ? page_comm : "",
                            search_comm ? search_comm : "");
1140

1141 1142 1143 1144 1145
  if (g_spawn_command_line_async (string, &error) == FALSE)
    {
      g_warning ("Error envoking devhelp: %s", error->message);
      g_error_free (error);
    }
1146

1147 1148 1149 1150 1151 1152 1153
  g_free (string);
  if (book_comm)
    g_free (book_comm);
  if (page_comm)
    g_free (page_comm);
  if (search_comm)
    g_free (search_comm);
1154
}
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1155

1156
GtkWidget *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
1157
glade_util_get_placeholder_from_pointer (GtkContainer *container)
1158
{
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
  GdkDeviceManager *manager;
  GdkDisplay *display;
  GdkDevice *device;
  GdkWindow *window;

  if (((display = gtk_widget_get_display (GTK_WIDGET (container))) || 
       (display = gdk_display_get_default ())) &&
      (manager = gdk_display_get_device_manager (display)) &&
      (device = gdk_device_manager_get_client_pointer (manager)) &&
      (window = gdk_device_get_window_at_position (device, NULL, NULL)))
1169
    {
1170 1171
      gpointer widget;
      gdk_window_get_user_data (window, &widget);
1172

1173
      return GLADE_IS_PLACEHOLDER (widget) ? GTK_WIDGET (widget) : NULL;
1174 1175
    }

1176
  return NULL;
1177
}
1178 1179 1180 1181 1182 1183 1184 1185 1186

/**
 * glade_util_object_is_loading:
 * @object: A #GObject
 *
 * Returns: Whether the object's project is being loaded or not.
 *       
 */
gboolean
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
1187
glade_util_object_is_loading (GObject *object)
1188
{
1189 1190 1191 1192 1193 1194 1195
  GladeProject *project;
  GladeWidget *widget;

  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);

  widget = glade_widget_get_from_gobject (object);
  g_return_val_if_fail (GLADE_IS_WIDGET (widget), FALSE);
1196

1197 1198
  project = glade_widget_get_project (widget);

1199
  return project && glade_project_is_loading (project);
1200
}
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1201 1202 1203 1204 1205 1206 1207 1208

/**
 * glade_util_url_show:
 * @url: An URL to display
 *
 * Portable function for showing an URL @url in a web browser.
 *
 * Returns: TRUE if a web browser was successfully launched, or FALSE
1209
 *
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1210 1211
 */
gboolean
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
1212
glade_util_url_show (const gchar *url)
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1213
{
1214 1215 1216
  GtkWidget *widget;
  GError *error = NULL;
  gboolean ret;
1217

1218
  g_return_val_if_fail (url != NULL, FALSE);
1219

1220
  widget = glade_app_get_window ();
1221

1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
  ret = gtk_show_uri (gtk_widget_get_screen (widget),
                      url, gtk_get_current_event_time (), &error);
  if (error != NULL)
    {
      GtkWidget *dialog_widget;

      dialog_widget = gtk_message_dialog_new (GTK_WINDOW (widget),
                                              GTK_DIALOG_DESTROY_WITH_PARENT,
                                              GTK_MESSAGE_ERROR,
                                              GTK_BUTTONS_CLOSE,
                                              "%s", _("Could not show link:"));
      gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG
                                                (dialog_widget), "%s",
                                                error->message);
      g_error_free (error);

      g_signal_connect (dialog_widget, "response",
                        G_CALLBACK (gtk_widget_destroy), NULL);

      gtk_window_present (GTK_WINDOW (dialog_widget));
    }
1243

1244
  return ret;
Tristan Van Berkom's avatar
Tristan Van Berkom committed
1245
}
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

/**
 * glade_util_get_file_mtime:
 * @filename: A filename
 * @error: return location for errors
 *
 * Gets the UTC modification time of file @filename.
 *
 * Returns: The mtime of the file, or %0 if the file attributes
 *          could not be read.
 */
time_t
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
1258
glade_util_get_file_mtime (const gchar *filename, GError **error)
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
{
  struct stat info;
  gint retval;

  retval = g_stat (filename, &info);

  if (retval != 0)
    {
      g_set_error (error,
                   G_FILE_ERROR,
                   g_file_error_from_errno (errno),
                   "could not stat file '%s': %s", filename,
                   g_strerror (errno));
      return (time_t) 0;
    }
  else
    {
      return info.st_mtime;
    }
1278
}
1279

1280
gchar *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
1281
glade_util_filename_to_icon_name (const gchar *value)
1282
{
1283 1284 1285 1286
  gchar *icon_name, *p;
  g_return_val_if_fail (value && value[0], NULL);

  icon_name = g_strdup_printf ("glade-generated-%s", value);
1287

1288 1289
  if ((p = strrchr (icon_name, '.')) != NULL)
    *p = '-';
1290

1291
  return icon_name;
1292 1293 1294
}

gchar *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
1295
glade_util_icon_name_to_filename (const gchar *value)
1296
{
1297 1298 1299
  /* sscanf makes us allocate a buffer */
  gchar filename[FILENAME_MAX], *p;
  g_return_val_if_fail (value && value[0], NULL);
1300

1301
  sscanf (value, "glade-generated-%s", filename);
1302

1303 1304 1305 1306 1307 1308 1309
  /* XXX: Filenames without an extention will evidently
   * break here
   */
  if ((p = strrchr (filename, '-')) != NULL)
    *p = '.';

  return g_strdup (filename);
1310
}
1311 1312

gint
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
1313
glade_utils_enum_value_from_string (GType enum_type, const gchar *strval)
1314
{
1315 1316 1317
  gint value = 0;
  const gchar *displayable;
  GValue *gvalue;
1318

1319
  g_return_val_if_fail (strval && strval[0], 0);
1320

1321 1322 1323
  if (((displayable =
        glade_get_value_from_displayable (enum_type, strval)) != NULL &&
       (gvalue =
1324
        glade_utils_value_from_string (enum_type, displayable, NULL)) != NULL) ||
1325
      (gvalue =
1326
       glade_utils_value_from_string (enum_type, strval, NULL)) != NULL)
1327 1328 1329 1330 1331 1332
    {
      value = g_value_get_enum (gvalue);
      g_value_unset (gvalue);
      g_free (gvalue);
    }
  return value;
1333 1334
}

1335
static gchar *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
1336 1337
glade_utils_enum_string_from_value_real (GType enum_type,
                                         gint value,
1338
                                         gboolean displayable)
1339
{
1340 1341
  GValue gvalue = { 0, };
  gchar *string;
1342

1343 1344
  g_value_init (&gvalue, enum_type);
  g_value_set_enum (&gvalue, value);
1345

1346 1347
  string = glade_utils_string_from_value (&gvalue);
  g_value_unset (&gvalue);
1348

1349 1350 1351 1352 1353 1354 1355 1356 1357
  if (displayable && string)
    {
      const gchar *dstring = glade_get_displayable_value (enum_type, string);
      if (dstring)
        {
          g_free (string);
          return g_strdup (dstring);
        }
    }
1358

1359
  return string;
1360 1361
}

1362 1363 1364
gchar *
glade_utils_enum_string_from_value (GType enum_type, gint value)
{
1365
  return glade_utils_enum_string_from_value_real (enum_type, value, FALSE);
1366 1367 1368 1369 1370
}

gchar *
glade_utils_enum_string_from_value_displayable (GType enum_type, gint value)
{
1371
  return glade_utils_enum_string_from_value_real (enum_type, value, TRUE);
1372 1373 1374
}


1375
gint
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
1376
glade_utils_flags_value_from_string (GType flags_type, const gchar *strval)
1377
{
1378 1379 1380
  gint value = 0;
  const gchar *displayable;
  GValue *gvalue;
1381

1382
  g_return_val_if_fail (strval && strval[0], 0);
1383

1384 1385 1386
  if (((displayable =
        glade_get_value_from_displayable (flags_type, strval)) != NULL &&
       (gvalue =
1387
        glade_utils_value_from_string (flags_type, displayable, NULL)) != NULL) ||
1388
      (gvalue =
1389
       glade_utils_value_from_string (flags_type, strval, NULL)) != NULL)
1390 1391 1392 1393 1394 1395
    {
      value = g_value_get_flags (gvalue);
      g_value_unset (gvalue);
      g_free (gvalue);
    }
  return value;
1396 1397
}

1398
static gchar *
Juan Pablo Ugarte's avatar
Juan Pablo Ugarte committed
1399 1400
glade_utils_flags_string_from_value_real (GType flags_type,
                                          gint value,
1401
                                          gboolean displayable)
1402
{
1403 1404
  GValue gvalue = { 0, };
  gchar *string;
1405

1406 1407
  g_value_init (&gvalue, flags_type);
  g_value_set_flags (&gvalue, value);
1408

1409 1410
  string = glade_utils_string_from_value (&gvalue);
  g_value_unset (&gvalue);