glade-named-icon-chooser-dialog.c 50.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * glade-named-icon-chooser-widget.c - Named icon chooser widget
 *
 * Copyright (C) 2007 Vincent Geddes
 *
 * Author:  Vincent Geddes <vgeddes@gnome.org>
 *
 * 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
 * MERCHANNAMED_ICON_CHOOSERILITY 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>

25
#include "glade-private.h"
26 27 28 29 30 31 32 33 34 35 36 37 38
#include "glade-named-icon-chooser-dialog.h"
#include "icon-naming-spec.c"

#include <gtk/gtk.h>
#include <glib/gi18n-lib.h>
#include <string.h>
#include <errno.h>


#define DEFAULT_SETTING_LIST_STANDARD_ONLY   TRUE

enum
{
39 40 41 42 43
  CONTEXTS_ID_COLUMN,
  CONTEXTS_NAME_COLUMN,
  CONTEXTS_TITLE_COLUMN,

  CONTEXTS_N_COLUMS
44 45 46 47
};

enum
{
48 49 50 51 52
  ICONS_CONTEXT_COLUMN,
  ICONS_STANDARD_COLUMN,
  ICONS_NAME_COLUMN,

  ICONS_N_COLUMNS
53 54 55 56
};

enum
{
57
  GLADE_NAMED_ICON
58 59
};

60 61 62 63 64 65
enum
{
  ICON_ACTIVATED,
  SELECTION_CHANGED,

  LAST_SIGNAL
66 67 68 69
};

struct _GladeNamedIconChooserDialogPrivate
{
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
  GtkWidget *icons_view;
  GtkTreeModel *filter_model;   /* filtering model  */
  GtkListStore *icons_store;    /* data store       */
  GtkTreeSelection *selection;

  GtkWidget *contexts_view;
  GtkListStore *contexts_store;

  GtkWidget *entry;
  GtkEntryCompletion *entry_completion;

  GtkWidget *button;            /* list-standard-only checkbutton */

  gint context_id;              /* current icon name context for icon filtering */

  gchar *pending_select_name;   /* an icon name for a pending treeview selection.
                                 * can only select name after model is loaded
                                 * and the widget is mapped */

  GtkIconTheme *icon_theme;     /* the current icon theme */
  guint load_id;                /* id of the idle function for loading data into model */

  gboolean settings_list_standard;      /* whether to list standard icon names only */

  GtkWidget *last_focus_widget;

  gboolean icons_loaded;        /* whether the icons have been loaded into the model */
97 98 99 100
};

static GHashTable *standard_icon_quarks = NULL;

101 102
static guint dialog_signals[LAST_SIGNAL] = { 0, };

103 104
gchar *
glade_named_icon_chooser_dialog_get_icon_name (GladeNamedIconChooserDialog *dialog);
105

106 107 108
void
glade_named_icon_chooser_dialog_set_icon_name (GladeNamedIconChooserDialog *dialog,
                                               const gchar                 *icon_name);
109

110
gboolean
111 112
glade_named_icon_chooser_dialog_set_context (GladeNamedIconChooserDialog *dialog,
                                             const gchar                 *context);
113

114 115
gchar *
glade_named_icon_chooser_dialog_get_context (GladeNamedIconChooserDialog *dialog);
116

117
static gboolean should_respond (GladeNamedIconChooserDialog *dialog);
118

119
static void filter_icons_model (GladeNamedIconChooserDialog *dialog);
120

121 122 123 124
static gboolean scan_for_name_func (GtkTreeModel *model,
                                    GtkTreePath  *path,
                                    GtkTreeIter  *iter,
                                    gpointer      data);
125

126 127 128 129
static gboolean scan_for_context_func (GtkTreeModel *model,
                                       GtkTreePath  *path,
                                       GtkTreeIter  *iter,
                                       gpointer      data);
130

131
static void settings_load (GladeNamedIconChooserDialog *dialog);
132

133
static void settings_save (GladeNamedIconChooserDialog *dialog);
134 135


136 137 138
G_DEFINE_TYPE_WITH_PRIVATE (GladeNamedIconChooserDialog,
                            glade_named_icon_chooser_dialog,
                            GTK_TYPE_DIALOG);
139 140 141


static void
142
entry_set_name (GladeNamedIconChooserDialog *dialog, const gchar *name)
143 144 145 146 147 148 149 150 151 152
{
  /* Must disable completion before setting text, in order to avoid
   * spurious warnings (possible GTK+ bug).
   */
  gtk_entry_set_completion (GTK_ENTRY (dialog->priv->entry), NULL);

  gtk_entry_set_text (GTK_ENTRY (dialog->priv->entry), name);

  gtk_entry_set_completion (GTK_ENTRY (dialog->priv->entry),
                            dialog->priv->entry_completion);
153 154 155
}

static GtkIconTheme *
156
get_icon_theme_for_widget (GtkWidget *widget)
157
{
158 159
  if (gtk_widget_has_screen (widget))
    return gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
160

161
  return gtk_icon_theme_get_default ();
162 163 164 165
}

/* validates name according to the icon naming spec (en_US.US_ASCII [a-z1-9_-.])  */
static gboolean
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
is_well_formed (const gchar * name)
{
  gchar *c = (gchar *) name;
  for (; *c; c++)
    {
      if (g_ascii_isalnum (*c))
        {
          if (g_ascii_isalpha (*c) && !g_ascii_islower (*c))
            return FALSE;
        }
      else if (*c != '_' && *c != '-' && *c != '.')
        {
          return FALSE;
        }
    }
  return TRUE;
182 183 184
}

static void
185 186 187 188
check_entry_text (GladeNamedIconChooserDialog *dialog,
                  gchar                      **name_ret,
                  gboolean                    *is_wellformed_ret,
                  gboolean                    *is_empty_ret)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
{
  if (strlen (gtk_entry_get_text (GTK_ENTRY (dialog->priv->entry))) == 0)
    {
      *name_ret = NULL;
      *is_wellformed_ret = TRUE;
      *is_empty_ret = TRUE;

      return;
    }

  *is_empty_ret = FALSE;

  *is_wellformed_ret =
      is_well_formed (gtk_entry_get_text (GTK_ENTRY (dialog->priv->entry)));

  if (*is_wellformed_ret)
    *name_ret = g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->priv->entry)));
  else
    *name_ret = NULL;
208 209 210
}

static void
211 212
changed_text_handler (GtkEditable                 *editable,
                      GladeNamedIconChooserDialog *dialog)
213
{
214 215
  g_signal_emit_by_name (dialog, "selection-changed", NULL);
}
216 217 218

/* ensure that only valid text can be inserted into entry */
static void
219 220 221 222 223
insert_text_handler (GtkEditable                 *editable,
                     const gchar                 *text,
                     gint                         length,
                     gint                        *position,
                     GladeNamedIconChooserDialog *dialog)
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
{
  if (is_well_formed (text))
    {

      g_signal_handlers_block_by_func (editable, (gpointer) insert_text_handler,
                                       dialog);

      gtk_editable_insert_text (editable, text, length, position);

      g_signal_handlers_unblock_by_func (editable,
                                         (gpointer) insert_text_handler,
                                         dialog);

    }
  else
    {
      gdk_display_beep (gtk_widget_get_display (GTK_WIDGET (dialog)));
    }

  g_signal_stop_emission_by_name (editable, "insert-text");
244 245 246 247
}

typedef struct
{
248
  gchar *name;                  /* the name of the icon or context */
249

250 251 252 253
  guint found:1;                /* whether an item matching `name' was found */
  guint do_select:1;            /* select the matched row */
  guint do_cursor:1;            /* put cursor at the matched row */
  guint do_activate:1;          /* activate the matched row */
254

255
  GladeNamedIconChooserDialog *dialog;
256 257 258
} ForEachFuncData;

void
259 260
glade_named_icon_chooser_dialog_set_icon_name (GladeNamedIconChooserDialog *dialog, 
                                               const gchar                 *name)
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
{
  ForEachFuncData *data;
  gboolean located_in_theme;

  g_return_if_fail (GLADE_IS_NAMED_ICON_CHOOSER_DIALOG (dialog));
  g_return_if_fail (gtk_widget_has_screen (GTK_WIDGET (dialog)));

  if (name == NULL)
    {
      gtk_tree_selection_unselect_all (dialog->priv->selection);
      entry_set_name (dialog, "");
      return;
    }

  located_in_theme =
      gtk_icon_theme_has_icon (get_icon_theme_for_widget (GTK_WIDGET (dialog)),
                               name);

  if (located_in_theme)
    {

      if (dialog->priv->icons_loaded && dialog->priv->filter_model)
        {

          data = g_slice_new0 (ForEachFuncData);
          data->name = g_strdup (name);
          data->found = FALSE;
          data->do_activate = FALSE;
          data->do_select = TRUE;
          data->do_cursor = TRUE;
          data->dialog = dialog;

          gtk_tree_model_foreach (dialog->priv->filter_model,
                                  scan_for_name_func, data);

          g_free (data->name);
          g_slice_free (ForEachFuncData, data);

        }
      else
        {
          dialog->priv->pending_select_name = g_strdup (name);
        }

      /* selecting a treeview row will set the entry text,
       * but we must have this here in case the row has been filtered out
       */
      entry_set_name (dialog, name);

    }
  else if (is_well_formed (name))
    {

      gtk_tree_selection_unselect_all (dialog->priv->selection);

      entry_set_name (dialog, name);
    }
  else
    {
      g_warning ("invalid icon name: '%s' is not well formed", name);
    }
322 323 324
}

gboolean
325 326
glade_named_icon_chooser_dialog_set_context (GladeNamedIconChooserDialog *dialog,
                                             const gchar                 *name)
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
{
  ForEachFuncData *data;

  g_return_val_if_fail (GLADE_IS_NAMED_ICON_CHOOSER_DIALOG (dialog), FALSE);

  data = g_slice_new0 (ForEachFuncData);

  if (name)
    data->name = g_strdup (name);
  else
    data->name = g_strdup ("All Contexts");

  data->found = FALSE;
  data->do_select = TRUE;
  data->do_activate = FALSE;
  data->do_cursor = FALSE;
  data->dialog = dialog;

  gtk_tree_model_foreach (GTK_TREE_MODEL (dialog->priv->contexts_store),
                          (GtkTreeModelForeachFunc) scan_for_context_func,
                          data);

  g_free (data->name);
  g_slice_free (ForEachFuncData, data);

  return TRUE;
353 354 355
}

gchar *
356
glade_named_icon_chooser_dialog_get_context (GladeNamedIconChooserDialog *dialog)
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
{
  GtkTreeSelection *sel;
  GtkTreeIter iter;
  gchar *context_name;

  g_return_val_if_fail (GLADE_IS_NAMED_ICON_CHOOSER_DIALOG (dialog), NULL);

  sel =
      gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->priv->contexts_view));

  if (gtk_tree_selection_get_selected (sel, NULL, &iter))
    {

      gtk_tree_model_get (GTK_TREE_MODEL (dialog->priv->contexts_store), &iter,
                          CONTEXTS_NAME_COLUMN, &context_name, -1);

      /* if context_name is NULL, then it is the 'all categories' special context */
      return context_name;

    }
  else
    {
      return NULL;
    }
381 382 383
}

static gchar *
384
get_icon_name_from_selection (GladeNamedIconChooserDialog *dialog)
385
{
386 387 388 389 390 391
  GtkTreeIter iter;
  GtkTreeModel *model;
  gchar *name;

  if (!gtk_tree_selection_get_selected (dialog->priv->selection, &model, &iter))
    return NULL;
392

393
  gtk_tree_model_get (model, &iter, ICONS_NAME_COLUMN, &name, -1);
394

395
  return name;
396 397
}

398
gchar *
399
glade_named_icon_chooser_dialog_get_icon_name (GladeNamedIconChooserDialog *dialog)
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
{
  GtkWidget *current_focus;
  gchar *name;

  g_return_val_if_fail (GLADE_IS_NAMED_ICON_CHOOSER_DIALOG (dialog), NULL);

  current_focus = gtk_window_get_focus (GTK_WINDOW (dialog));

  if (current_focus == dialog->priv->icons_view)
    {

    view:
      name = get_icon_name_from_selection (dialog);

      if (name == NULL)
        goto entry;

    }
  else if (current_focus == dialog->priv->entry)
    {
      gboolean is_wellformed, is_empty;
    entry:
      check_entry_text (dialog, &name, &is_wellformed, &is_empty);

      if (!is_wellformed || is_empty)
        return NULL;

    }
  else if (dialog->priv->last_focus_widget == dialog->priv->icons_view)
    {
      goto view;
    }
  else if (dialog->priv->last_focus_widget == dialog->priv->entry)
    {
      goto entry;
    }
  else
    {
      goto view;
    }

  return name;
442 443 444
}

static void
445
set_busy_cursor (GladeNamedIconChooserDialog *dialog, gboolean busy)
446
{
447 448
  GdkDisplay *display;
  GdkCursor *cursor;
449

450 451
  if (!gtk_widget_get_realized (GTK_WIDGET (dialog)))
    return;
452

453
  display = gtk_widget_get_display (GTK_WIDGET (dialog));
454

455 456 457 458
  if (busy)
    cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
  else
    cursor = NULL;
459

460 461
  gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (dialog)), cursor);
  gdk_display_flush (display);
462

463
  if (cursor)
464
    g_object_unref (cursor);
465 466
}

467
static GtkListStore *
468 469
populate_icon_contexts_model (void)
{
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
  GtkListStore *store;
  GtkTreeIter iter;
  guint i;

  store = gtk_list_store_new (CONTEXTS_N_COLUMS,
                              G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING);


  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter,
                      CONTEXTS_ID_COLUMN, -1,
                      CONTEXTS_NAME_COLUMN, "All Contexts",
                      CONTEXTS_TITLE_COLUMN, _("All Contexts"), -1);

  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter,
                      CONTEXTS_ID_COLUMN, -1,
                      CONTEXTS_NAME_COLUMN, NULL,
                      CONTEXTS_TITLE_COLUMN, NULL, -1);

  for (i = 0; i < G_N_ELEMENTS (standard_contexts); i++)
    {

      gtk_list_store_append (store, &iter);
      gtk_list_store_set (store, &iter,
                          CONTEXTS_ID_COLUMN, i,
                          CONTEXTS_NAME_COLUMN, standard_contexts[i].name,
                          CONTEXTS_TITLE_COLUMN, _(standard_contexts[i].title),
                          -1);
    }

  return store;
502 503 504
}

static void
505 506 507 508
icons_row_activated_cb (GtkTreeView                 *view,
                        GtkTreePath                 *path,
                        GtkTreeViewColumn           *column,
                        GladeNamedIconChooserDialog *dialog)
509
{
510
  g_signal_emit_by_name (dialog, "icon-activated", NULL);
511 512 513
}

static void
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
icons_selection_changed_cb (GtkTreeSelection * selection,
                            GladeNamedIconChooserDialog * dialog)
{
  GtkTreeModel *model;
  GtkTreeIter iter;
  gchar *name;

  if (gtk_tree_selection_get_selected (selection, &model, &iter))
    {
      gtk_tree_model_get (model, &iter, ICONS_NAME_COLUMN, &name, -1);
      if (name)
        entry_set_name (dialog, name);

      g_free (name);
    }
  else
    {
      /* entry_set_name (dialog, ""); */
    }

  /* we emit "selection-changed" for chooser in insert_text_handler()
   * to avoid emitting the signal twice */
536 537 538
}

static void
539 540 541 542
contexts_row_activated_cb (GtkTreeView                 *view,
                           GtkTreePath                 *cpath,
                           GtkTreeViewColumn           *column,
                           GladeNamedIconChooserDialog *dialog)
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
{
  GtkTreeIter iter;
  GtkTreePath *path;

  if (gtk_tree_model_get_iter_first (dialog->priv->filter_model, &iter))
    {

      gtk_tree_selection_select_iter (dialog->priv->selection, &iter);

      path = gtk_tree_model_get_path (dialog->priv->filter_model, &iter);

      gtk_tree_selection_select_path (dialog->priv->selection, path);

      gtk_tree_view_scroll_to_point (GTK_TREE_VIEW (dialog->priv->icons_view),
                                     -1, 0);

      gtk_tree_path_free (path);

    }
  gtk_widget_grab_focus (dialog->priv->icons_view);
563 564 565
}

static void
566 567
contexts_selection_changed_cb (GtkTreeSelection            *selection,
                               GladeNamedIconChooserDialog *dialog)
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
{
  GtkTreeIter iter;
  GtkTreeModel *model;
  gboolean retval;
  gint context_id;

  retval = gtk_tree_selection_get_selected (selection, &model, &iter);

  if (retval)
    {

      gtk_tree_model_get (model, &iter, CONTEXTS_ID_COLUMN, &context_id, -1);

      dialog->priv->context_id = context_id;

      if (!dialog->priv->filter_model)
        return;

      filter_icons_model (dialog);
    }

  entry_set_name (dialog, "");
590 591 592 593

}

static gboolean
594
row_separator_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer unused)
595
{
596 597 598 599 600 601
  gboolean retval;
  gchar *name, *title;

  gtk_tree_model_get (model, iter,
                      CONTEXTS_NAME_COLUMN, &name,
                      CONTEXTS_TITLE_COLUMN, &title, -1);
602

603
  retval = !name && !title;
604

605 606
  g_free (name);
  g_free (title);
607

608
  return retval;
609 610
}

611
static GtkWidget *
612
create_contexts_view (GladeNamedIconChooserDialog *dialog)
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
{
  GtkTreeView *view;
  GtkTreeViewColumn *column;
  GtkTreePath *path;

  dialog->priv->contexts_store = populate_icon_contexts_model ();

  view =
      GTK_TREE_VIEW (gtk_tree_view_new_with_model
                     (GTK_TREE_MODEL (dialog->priv->contexts_store)));

  column = gtk_tree_view_column_new_with_attributes (NULL,
                                                     gtk_cell_renderer_text_new
                                                     (), "text",
                                                     CONTEXTS_TITLE_COLUMN,
                                                     NULL);

  gtk_tree_view_append_column (view, column);
  gtk_tree_view_set_headers_visible (view, FALSE);

  gtk_tree_view_set_row_separator_func (view,
                                        (GtkTreeViewRowSeparatorFunc)
                                        row_separator_func, NULL, NULL);

  gtk_tree_selection_set_mode (gtk_tree_view_get_selection (view),
                               GTK_SELECTION_BROWSE);

  path = gtk_tree_path_new_from_indices (0, -1);
  gtk_tree_selection_select_path (gtk_tree_view_get_selection (view), path);
  gtk_tree_path_free (path);

  g_signal_connect (view, "row-activated",
                    G_CALLBACK (contexts_row_activated_cb), dialog);

  g_signal_connect (gtk_tree_view_get_selection (view), "changed",
                    G_CALLBACK (contexts_selection_changed_cb), dialog);

  gtk_widget_show (GTK_WIDGET (view));

  return GTK_WIDGET (view);
653 654 655 656
}

/* filters the icons model based on the current state */
static void
657
filter_icons_model (GladeNamedIconChooserDialog *dialog)
658 659
{

660 661 662 663 664 665 666 667
  set_busy_cursor (dialog, TRUE);

  g_object_ref (dialog->priv->filter_model);
  gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->priv->icons_view), NULL);
  gtk_entry_completion_set_model (dialog->priv->entry_completion, NULL);

  gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER
                                  (dialog->priv->filter_model));
668

669 670 671 672 673 674 675
  gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->priv->icons_view),
                           dialog->priv->filter_model);
  gtk_entry_completion_set_model (dialog->priv->entry_completion,
                                  GTK_TREE_MODEL (dialog->priv->icons_store));
  gtk_entry_completion_set_text_column (dialog->priv->entry_completion,
                                        ICONS_NAME_COLUMN);
  g_object_unref (dialog->priv->filter_model);
676

677
  set_busy_cursor (dialog, FALSE);
678 679 680
}

static gboolean
681 682 683
filter_visible_func (GtkTreeModel                *model,
                     GtkTreeIter                 *iter,
                     GladeNamedIconChooserDialog *dialog)
684
{
685 686
  gboolean standard;
  gint context_id;
687

688 689 690
  gtk_tree_model_get (model, iter,
                      ICONS_CONTEXT_COLUMN, &context_id,
                      ICONS_STANDARD_COLUMN, &standard, -1);
691

692 693
  if (dialog->priv->context_id == -1)
    return (dialog->priv->settings_list_standard) ? TRUE && standard : TRUE;
694

695 696 697 698
  if (context_id == dialog->priv->context_id)
    return (dialog->priv->settings_list_standard) ? TRUE && standard : TRUE;
  else
    return FALSE;
699 700 701 702
}


static gboolean
703 704 705 706 707
search_equal_func (GtkTreeModel                *model,
                   gint                         column,
                   const gchar                 *key,
                   GtkTreeIter                 *iter,
                   GladeNamedIconChooserDialog *dialog)
708
{
709 710
  gchar *name;
  gboolean retval;
711

712
  gtk_tree_model_get (model, iter, ICONS_NAME_COLUMN, &name, -1);
713

714 715 716 717 718
  retval = !g_str_has_prefix (name, key);

  g_free (name);

  return retval;
719 720 721 722

}

static gboolean
723 724 725 726
scan_for_context_func (GtkTreeModel *model,
                       GtkTreePath  *path,
                       GtkTreeIter  *iter,
                       gpointer      user_data)
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
{
  ForEachFuncData *data = (ForEachFuncData *) user_data;
  GtkTreeSelection *selection =
      gtk_tree_view_get_selection (GTK_TREE_VIEW
                                   (data->dialog->priv->contexts_view));
  gchar *name = NULL;

  gtk_tree_model_get (model, iter, CONTEXTS_NAME_COLUMN, &name, -1);
  if (!name)
    return FALSE;

  if (strcmp (name, data->name) == 0)
    {

      data->found = TRUE;

      if (data->do_activate)
        gtk_tree_view_row_activated (GTK_TREE_VIEW
                                     (data->dialog->priv->contexts_view), path,
                                     gtk_tree_view_get_column (GTK_TREE_VIEW
                                                               (data->dialog->
                                                                priv->
                                                                contexts_view),
                                                               0));

      if (data->do_select)
        gtk_tree_selection_select_path (selection, path);
      else
        gtk_tree_selection_unselect_path (selection, path);

      if (data->do_cursor)
        gtk_tree_view_set_cursor (GTK_TREE_VIEW
                                  (data->dialog->priv->contexts_view), path,
                                  NULL, FALSE);

      g_free (name);

      return TRUE;
    }

  g_free (name);

  return FALSE;
770 771 772
}

gboolean
773 774 775 776
scan_for_name_func (GtkTreeModel *model,
                    GtkTreePath  *path,
                    GtkTreeIter  *iter,
                    gpointer      user_data)
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
{
  ForEachFuncData *data = (ForEachFuncData *) user_data;
  gchar *name = NULL;

  gtk_tree_model_get (model, iter, ICONS_NAME_COLUMN, &name, -1);
  if (!name)
    return FALSE;

  if (strcmp (name, data->name) == 0)
    {

      data->found = TRUE;

      if (data->do_activate)
        gtk_tree_view_row_activated (GTK_TREE_VIEW
                                     (data->dialog->priv->icons_view), path,
                                     gtk_tree_view_get_column (GTK_TREE_VIEW
                                                               (data->dialog->
                                                                priv->
                                                                icons_view),
                                                               0));

      if (data->do_select)
        gtk_tree_selection_select_path (data->dialog->priv->selection, path);
      else
        gtk_tree_selection_unselect_path (data->dialog->priv->selection, path);

      if (data->do_cursor)
        gtk_tree_view_set_cursor (GTK_TREE_VIEW
                                  (data->dialog->priv->icons_view), path, NULL,
                                  FALSE);

      g_free (name);

      return TRUE;
    }

  g_free (name);

  return FALSE;
817 818 819
}

static void
820
centre_selected_row (GladeNamedIconChooserDialog *dialog)
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
{
  GList *l;

  g_assert (dialog->priv->icons_store != NULL);
  g_assert (dialog->priv->selection != NULL);

  l = gtk_tree_selection_get_selected_rows (dialog->priv->selection, NULL);

  if (l)
    {
      g_assert (gtk_widget_get_mapped (GTK_WIDGET (dialog)));
      g_assert (gtk_widget_get_visible (GTK_WIDGET (dialog)));

      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (dialog->priv->icons_view),
                                    (GtkTreePath *) l->data,
                                    NULL, TRUE, 0.5, 0.0);

838 839 840 841 842 843
/*		gtk_tree_view_set_cursor (GTK_TREE_VIEW (dialog->priv->icons_view),
					 (GtkTreePath *) l->data,
					 0,
					 FALSE);
					 
		gtk_widget_grab_focus (dialog->priv->icons_view);		
844 845 846 847
*/
      g_list_foreach (l, (GFunc) gtk_tree_path_free, NULL);
      g_list_free (l);
    }
848 849 850
}

static void
851
select_first_row (GladeNamedIconChooserDialog *dialog)
852
{
853
  GtkTreePath *path;
854

855 856
  if (!dialog->priv->filter_model)
    return;
857

858 859 860 861
  path = gtk_tree_path_new_from_indices (0, -1);
  gtk_tree_view_set_cursor (GTK_TREE_VIEW (dialog->priv->icons_view), path,
                            NULL, FALSE);
  gtk_tree_path_free (path);
862 863 864
}

static void
865
pending_select_name_process (GladeNamedIconChooserDialog *dialog)
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
{
  ForEachFuncData *data;

  g_assert (dialog->priv->icons_store != NULL);
  g_assert (dialog->priv->selection != NULL);

  if (dialog->priv->pending_select_name)
    {

      data = g_slice_new0 (ForEachFuncData);

      data->name = dialog->priv->pending_select_name;
      data->do_select = TRUE;
      data->do_activate = FALSE;
      data->dialog = dialog;

      gtk_tree_model_foreach (dialog->priv->filter_model,
                              scan_for_name_func, data);

      g_free (dialog->priv->pending_select_name);
      dialog->priv->pending_select_name = NULL;

      g_slice_free (ForEachFuncData, data);

    }
  else
    {
      if (strlen (gtk_entry_get_text (GTK_ENTRY (dialog->priv->entry))) == 0)
        {
          select_first_row (dialog);
        }
    }

  centre_selected_row (dialog);
900 901 902
}

static gboolean
903
is_standard_icon_name (const gchar *icon_name)
904
{
905 906 907
  GQuark quark;

  quark = g_quark_try_string (icon_name);
908

909 910 911 912 913
  if (quark == 0)
    return FALSE;

  return (g_hash_table_lookup (standard_icon_quarks, GUINT_TO_POINTER (quark))
          != NULL);
914 915 916 917

}

static void
918
cleanup_after_load (GladeNamedIconChooserDialog *dialog)
919
{
920
  dialog->priv->load_id = 0;
921

922
  pending_select_name_process (dialog);
923

924
  set_busy_cursor (dialog, FALSE);
925 926 927
}

static void
928
chooser_set_model (GladeNamedIconChooserDialog *dialog)
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
{

  /* filter model */
  dialog->priv->filter_model =
      gtk_tree_model_filter_new (GTK_TREE_MODEL (dialog->priv->icons_store),
                                 NULL);

  gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER
                                          (dialog->priv->filter_model),
                                          (GtkTreeModelFilterVisibleFunc)
                                          filter_visible_func, dialog, NULL);

  gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->priv->icons_view),
                           dialog->priv->filter_model);
  g_object_unref (dialog->priv->filter_model);

  gtk_entry_completion_set_model (dialog->priv->entry_completion,
                                  GTK_TREE_MODEL (dialog->priv->icons_store));
  gtk_entry_completion_set_text_column (dialog->priv->entry_completion,
                                        ICONS_NAME_COLUMN);

  gtk_tree_view_set_search_column (GTK_TREE_VIEW (dialog->priv->icons_view),
                                   ICONS_NAME_COLUMN);

  dialog->priv->icons_loaded = TRUE;
954 955 956 957
}

typedef struct
{
958 959
  gchar *name;
  gint context;
960 961 962
} IconData;

static gint
963
icon_data_compare (IconData *a, IconData *b)
964
{
965
  return g_ascii_strcasecmp (a->name, b->name);
966 967 968
}

static gboolean
969
reload_icons (GladeNamedIconChooserDialog *dialog)
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
{
  GtkListStore *store = dialog->priv->icons_store;
  GtkTreeIter iter;
  guint i;
  GList *l, *icons = NULL;

  /* retrieve icon names from each context */
  for (i = 0; i < G_N_ELEMENTS (standard_contexts); i++)
    {

      GList *icons_in_context =
          gtk_icon_theme_list_icons (dialog->priv->icon_theme,
                                     standard_contexts[i].name);

      for (l = icons_in_context; l; l = l->next)
        {

          IconData *data = g_slice_new (IconData);

          data->name = (gchar *) l->data;
          data->context = i;

          icons = g_list_prepend (icons, data);
        }

      g_list_free (icons_in_context);
    }

  /* sort icon names */
  icons = g_list_sort (icons, (GCompareFunc) icon_data_compare);

  /* put into to model */
  for (l = icons; l; l = l->next)
    {

      IconData *data = (IconData *) l->data;

      gtk_list_store_append (store, &iter);
      gtk_list_store_set (store, &iter,
                          ICONS_CONTEXT_COLUMN, data->context,
                          ICONS_STANDARD_COLUMN,
                          is_standard_icon_name (data->name), ICONS_NAME_COLUMN,
                          data->name, -1);

      g_free (data->name);
      g_slice_free (IconData, data);
    }

  g_list_free (icons);

  chooser_set_model (dialog);

  return FALSE;
1023 1024 1025
}

static void
1026
change_icon_theme (GladeNamedIconChooserDialog *dialog)
1027
{
1028 1029
  if (dialog->priv->icon_theme == NULL)
    dialog->priv->icon_theme = get_icon_theme_for_widget (GTK_WIDGET (dialog));
1030

1031 1032
  gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->priv->icons_view), NULL);
  gtk_list_store_clear (dialog->priv->icons_store);
1033

1034
  set_busy_cursor (dialog, TRUE);
1035

1036 1037 1038 1039
  dialog->priv->load_id = g_idle_add_full (G_PRIORITY_HIGH_IDLE + 300,
                                           (GSourceFunc) reload_icons,
                                           dialog,
                                           (GDestroyNotify) cleanup_after_load);
1040 1041 1042 1043

}

static void
1044 1045
glade_named_icon_chooser_dialog_screen_changed (GtkWidget *widget,
                                                GdkScreen *previous_screen)
1046
{
1047 1048 1049
  GladeNamedIconChooserDialog *dialog;

  dialog = GLADE_NAMED_ICON_CHOOSER_DIALOG (widget);
1050

1051 1052 1053 1054
  if (GTK_WIDGET_CLASS (glade_named_icon_chooser_dialog_parent_class)->
      screen_changed)
    GTK_WIDGET_CLASS (glade_named_icon_chooser_dialog_parent_class)->
        screen_changed (widget, previous_screen);
1055

1056 1057
  if (gtk_widget_get_mapped (widget))
    change_icon_theme (dialog);
1058 1059 1060

}

1061
static GtkWidget *
1062
create_icons_view (GladeNamedIconChooserDialog *dialog)
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
{
  GtkTreeView *view;
  GtkTreeViewColumn *column;
  GtkCellRenderer *pixbuf_renderer, *text_renderer;

  view = GTK_TREE_VIEW (gtk_tree_view_new ());

  column = gtk_tree_view_column_new ();
  gtk_tree_view_column_set_min_width (column, 56);
  gtk_tree_view_column_set_title (column, NULL);
  pixbuf_renderer = gtk_cell_renderer_pixbuf_new ();

  gtk_tree_view_column_pack_start (column, pixbuf_renderer, TRUE);

  gtk_tree_view_column_set_attributes (column,
                                       pixbuf_renderer,
                                       "icon-name", ICONS_NAME_COLUMN, NULL);

  gtk_tree_view_append_column (view, column);
  g_object_set (pixbuf_renderer,
                "xpad", 2,
                "xalign", 1.0, "stock-size", GTK_ICON_SIZE_MENU, NULL);

  column = gtk_tree_view_column_new ();
  gtk_tree_view_column_set_title (column, "Name");
  text_renderer = gtk_cell_renderer_text_new ();
  g_object_set (G_OBJECT (text_renderer),
                "ellipsize", PANGO_ELLIPSIZE_END, "yalign", 0.0, NULL);

  gtk_tree_view_column_pack_start (column, text_renderer, TRUE);

  gtk_tree_view_column_set_attributes (column,
                                       text_renderer,
                                       "text", ICONS_NAME_COLUMN, NULL);


  gtk_tree_view_append_column (view, column);
  gtk_tree_view_column_set_expand (column, TRUE);
  gtk_tree_view_column_set_resizable (column, FALSE);

  gtk_tree_view_set_headers_visible (view, FALSE);

  gtk_tree_view_set_enable_search (view, TRUE);
  gtk_tree_view_set_search_equal_func (view,
                                       (GtkTreeViewSearchEqualFunc)
                                       search_equal_func, dialog, NULL);

  g_signal_connect (view, "row-activated",
                    G_CALLBACK (icons_row_activated_cb), dialog);

  g_signal_connect (gtk_tree_view_get_selection (view), "changed",
                    G_CALLBACK (icons_selection_changed_cb), dialog);

  gtk_tree_selection_set_mode (gtk_tree_view_get_selection (view),
                               GTK_SELECTION_BROWSE);

  dialog->priv->selection = gtk_tree_view_get_selection (view);

  gtk_tree_view_set_rules_hint (view, TRUE);

  gtk_widget_show (GTK_WIDGET (view));

  return GTK_WIDGET (view);
1126 1127 1128 1129
}

/* sets the 'list-standard' state and refilters the icons model */
static void
1130
button_toggled (GtkToggleButton *button, GladeNamedIconChooserDialog *dialog)
1131
{
1132
  dialog->priv->settings_list_standard = gtk_toggle_button_get_active (button);
1133

1134 1135
  if (dialog->priv->filter_model != NULL)
    filter_icons_model (dialog);
1136 1137
}

1138
static GHashTable *
1139
create_standard_icon_quarks (void)
1140
{
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
  GHashTable *table;
  GQuark quark;
  guint i;

  table = g_hash_table_new (NULL, NULL);

  for (i = 0; i < G_N_ELEMENTS (standard_icon_names); i++)
    {

      quark = g_quark_from_static_string (standard_icon_names[i]);

      g_hash_table_insert (table,
                           GUINT_TO_POINTER (quark), GUINT_TO_POINTER (quark));
    }

  return table;
1157 1158 1159
}

static void
1160 1161
glade_named_icon_chooser_dialog_style_set (GtkWidget *widget,
                                           GtkStyle  *previous_style)
1162
{
1163 1164
  if (gtk_widget_has_screen (widget) && gtk_widget_get_mapped (widget))
    change_icon_theme (GLADE_NAMED_ICON_CHOOSER_DIALOG (widget));
1165 1166 1167 1168 1169 1170
}

/* override GtkWidget::show_all since we have internal widgets we wish to keep
 * hidden unless we decide otherwise, like the list-standard-icons-only checkbox.
 */
static void
1171
glade_named_icon_chooser_dialog_show_all (GtkWidget *widget)
1172
{
1173
  gtk_widget_show (widget);
1174 1175 1176 1177 1178 1179
}

/* Handler for GtkWindow::set-focus; this is where we save the last-focused
 * widget on our toplevel.  See glade_named_icon_chooser_dialog_hierarchy_changed()
 */
static void
1180
glade_named_icon_chooser_dialog_set_focus (GtkWindow *window, GtkWidget *focus)
1181 1182
{

1183 1184 1185 1186 1187
  GTK_WINDOW_CLASS (glade_named_icon_chooser_dialog_parent_class)->
      set_focus (window, focus);

  GLADE_NAMED_ICON_CHOOSER_DIALOG (window)->priv->last_focus_widget =
      gtk_window_get_focus (window);
1188 1189 1190
}

static void
1191
glade_named_icon_chooser_dialog_finalize (GObject *object)
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
{
  GladeNamedIconChooserDialog *dialog =
      GLADE_NAMED_ICON_CHOOSER_DIALOG (object);

  if (dialog->priv->pending_select_name)
    {
      g_free (dialog->priv->pending_select_name);
      dialog->priv->pending_select_name = NULL;
    }

  G_OBJECT_CLASS (glade_named_icon_chooser_dialog_parent_class)->
1203
    finalize (object);
1204 1205 1206
}

static void
1207
glade_named_icon_chooser_dialog_map (GtkWidget *widget)
1208
{
1209 1210 1211 1212 1213 1214
  GladeNamedIconChooserDialog *dialog =
      GLADE_NAMED_ICON_CHOOSER_DIALOG (widget);

  GTK_WIDGET_CLASS (glade_named_icon_chooser_dialog_parent_class)->map (widget);

  settings_load (dialog);
1215

1216 1217
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->button),
                                dialog->priv->settings_list_standard);
1218

1219
  gtk_widget_grab_focus (dialog->priv->icons_view);
1220 1221 1222
}

static void
1223
glade_named_icon_chooser_dialog_unmap (GtkWidget *widget)
1224
{
1225 1226
  GladeNamedIconChooserDialog *dialog =
      GLADE_NAMED_ICON_CHOOSER_DIALOG (widget);
1227

1228
  settings_save (dialog);
1229

1230 1231
  GTK_WIDGET_CLASS (glade_named_icon_chooser_dialog_parent_class)->
      unmap (widget);
1232 1233 1234 1235
}

/* we load the icons in expose() because we want the widget
 * to be fully painted before loading begins
1236
 */
1237
static gboolean
1238
glade_named_icon_chooser_dialog_draw (GtkWidget *widget, cairo_t *cr)
1239
{
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
  GladeNamedIconChooserDialog *dialog =
      GLADE_NAMED_ICON_CHOOSER_DIALOG (widget);
  gboolean retval;

  retval =
      GTK_WIDGET_CLASS (glade_named_icon_chooser_dialog_parent_class)->
      draw (widget, cr);
  if (!dialog->priv->icons_loaded)
    {
      change_icon_theme (GLADE_NAMED_ICON_CHOOSER_DIALOG (widget));
      dialog->priv->icons_loaded = TRUE;
    }

  return retval;
1254 1255 1256
}

static void
1257
response_cb (GtkDialog *dialog, gint response_id)
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
{
  /* Act only on response IDs we recognize */
  if (!(response_id == GTK_RESPONSE_ACCEPT
        || response_id == GTK_RESPONSE_OK
        || response_id == GTK_RESPONSE_YES
        || response_id == GTK_RESPONSE_APPLY))
    return;

  if (!should_respond (GLADE_NAMED_ICON_CHOOSER_DIALOG (dialog)))
    {
      g_signal_stop_emission_by_name (dialog, "response");
    }
1270 1271 1272 1273 1274 1275
}

/* we intercept the GladeNamedIconChooser::icon-activated signal and try to
 * make the dialog emit a valid response signal
 */
static void
1276
icon_activated_cb (GladeNamedIconChooserDialog *dialog)
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
{
  GList *children, *l;

  children =
      gtk_container_get_children (GTK_CONTAINER
                                  (gtk_dialog_get_action_area
                                   (GTK_DIALOG (dialog))));

  for (l = children; l; l = l->next)
    {
      GtkWidget *widget;
      gint response_id;

      widget = GTK_WIDGET (l->data);
      response_id =
          gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);

      if (response_id == GTK_RESPONSE_ACCEPT ||
          response_id == GTK_RESPONSE_OK ||
          response_id == GTK_RESPONSE_YES || response_id == GTK_RESPONSE_APPLY)
        {
          g_list_free (children);

          gtk_dialog_response (GTK_DIALOG (dialog), response_id);

          return;
        }
    }
  g_list_free (children);
1306 1307 1308 1309 1310 1311
}

/* we intercept the GladeNamedIconChooser::selection-changed signal and try to
 * make the affirmative response button insensitive when the selection is empty 
 */
static void
1312
selection_changed_cb (GladeNamedIconChooserDialog *dialog)
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
{
  GList *children, *l;
  gchar *icon_name;

  children =
      gtk_container_get_children (GTK_CONTAINER
                                  (gtk_dialog_get_action_area
                                   (GTK_DIALOG (dialog))));

  for (l = children; l; l = l->next)
    {
      GtkWidget *widget;
      gint response_id;

      widget = GTK_WIDGET (l->data);
      response_id =
          gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);

      if (response_id == GTK_RESPONSE_ACCEPT ||
          response_id == GTK_RESPONSE_OK ||
          response_id == GTK_RESPONSE_YES || response_id == GTK_RESPONSE_APPLY)
        {
          icon_name = glade_named_icon_chooser_dialog_get_icon_name (dialog);

          gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
                                             response_id, icon_name != NULL);
          g_free (icon_name);
          g_list_free (children);
          return;
        }
    }
  g_list_free (children);
1345 1346 1347
}

static void
1348
glade_named_icon_chooser_dialog_init (GladeNamedIconChooserDialog *dialog)
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
{
  GtkWidget *contents;
  GtkWidget *hbox;
  GtkWidget *vbox;
  GtkWidget *sw;
  GtkWidget *label;
  GtkWidget *hpaned;
  GtkWidget *content_area;
  GtkSizeGroup *group;

1359
  dialog->priv = glade_named_icon_chooser_dialog_get_instance_private (dialog);
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371

  dialog->priv->filter_model = NULL;
  dialog->priv->icons_store = NULL;
  dialog->priv->context_id = -1;
  dialog->priv->pending_select_name = NULL;
  dialog->priv->last_focus_widget = NULL;
  dialog->priv->icons_loaded = FALSE;


  gtk_window_set_title (GTK_WINDOW (dialog), _("Named Icon Chooser"));

  gtk_window_set_default_size (GTK_WINDOW (dialog), 610, 480);
1372

1373
  _glade_util_dialog_set_hig (GTK_DIALOG (dialog));
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
  content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));

  /* We do a signal connection here rather than overriding the method in
   * class_init because GtkDialog::response is a RUN_LAST signal.  We want *our*
   * handler to be run *first*, regardless of whether the user installs response
   * handlers of his own.
   */
  g_signal_connect (dialog, "response", G_CALLBACK (response_cb), NULL);

  g_signal_connect (dialog, "icon-activated",
                    G_CALLBACK (icon_activated_cb), NULL);

  g_signal_connect (dialog, "selection-changed",
                    G_CALLBACK (selection_changed_cb), NULL);


  if (standard_icon_quarks == NULL)
    standard_icon_quarks = create_standard_icon_quarks ();

1393
  contents = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
1394
  gtk_container_set_border_width (GTK_CONTAINER (contents), 5);
1395 1396 1397
  gtk_widget_show (contents);

  label = gtk_label_new_with_mnemonic (_("Icon _Name:"));
1398
  gtk_widget_set_halign (label, GTK_ALIGN_START);
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
  gtk_widget_show (label);

  dialog->priv->entry = gtk_entry_new ();
  gtk_entry_set_activates_default (GTK_ENTRY (dialog->priv->entry), TRUE);
  gtk_entry_set_width_chars (GTK_ENTRY (dialog->priv->entry), 40);
  g_object_set (G_OBJECT (dialog->priv->entry), "truncate-multiline", TRUE,
                NULL);
  g_signal_connect (G_OBJECT (dialog->priv->entry), "changed",
                    G_CALLBACK (changed_text_handler), dialog);
  g_signal_connect (G_OBJECT (dialog->priv->entry), "insert-text",
                    G_CALLBACK (insert_text_handler), dialog);
  gtk_widget_show (dialog->priv->entry);

  dialog->priv->entry_completion = gtk_entry_completion_new ();
  gtk_entry_set_completion (GTK_ENTRY (dialog->priv->entry),
                            dialog->priv->entry_completion);
  gtk_entry_completion_set_popup_completion (dialog->priv->entry_completion,
                                             FALSE);
  gtk_entry_completion_set_inline_completion (dialog->priv->entry_completion,
                                              TRUE);

  gtk_label_set_mnemonic_widget (GTK_LABEL (label), dialog->priv->entry);

1422
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
1423 1424 1425 1426 1427 1428
  gtk_widget_show (hbox);

  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (hbox), dialog->priv->entry, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (contents), hbox, FALSE, FALSE, 6);

1429
  hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
1430 1431 1432 1433 1434 1435
  gtk_paned_set_position (GTK_PANED (hpaned), 150);
  gtk_widget_show (hpaned);

  dialog->priv->contexts_view = create_contexts_view (dialog);
  dialog->priv->icons_view = create_icons_view (dialog);

1436
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
1437 1438 1439 1440 1441 1442 1443
  gtk_widget_show (vbox);

  group = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);

  label = gtk_label_new_with_mnemonic (_("C_ontexts:"));
  gtk_label_set_mnemonic_widget (GTK_LABEL (label),
                                 dialog->priv->contexts_view);
1444
  gtk_widget_set_halign (label, GTK_ALIGN_START);
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
  gtk_size_group_add_widget (group, label);
  gtk_widget_show (label);

  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), GTK_POLICY_NEVER,
                                  GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
  gtk_widget_show (sw);

  gtk_container_add (GTK_CONTAINER (sw), dialog->priv->contexts_view);
  gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0);
  gtk_paned_pack1 (GTK_PANED (hpaned), vbox, FALSE, FALSE);


1461
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
1462 1463 1464 1465
  gtk_widget_show (vbox);

  label = gtk_label_new_with_mnemonic (_("Icon Na_mes:"));
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), dialog->priv->icons_view);
1466
  gtk_widget_set_halign (label, GTK_ALIGN_START);
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
  gtk_size_group_add_widget (group, label);
  gtk_widget_show (label);

  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);

  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), GTK_POLICY_NEVER,
                                  GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
  gtk_widget_show (sw);

  gtk_container_add (GTK_CONTAINER (sw), dialog->priv->icons_view);
  gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0);
  gtk_paned_pack2 (GTK_PANED (hpaned), vbox, TRUE, FALSE);

  gtk_box_pack_start (GTK_BOX (contents), hpaned, TRUE, TRUE, 0);


  g_object_unref (G_OBJECT (group));

  dialog->priv->button =
      gtk_check_button_new_with_mnemonic (_("_List standard icons only"));
  gtk_widget_show (dialog->priv->button);

  g_signal_connect (dialog->priv->button, "toggled",
                    G_CALLBACK (button_toggled), dialog);

  gtk_box_pack_start (GTK_BOX (contents), dialog->priv->button, FALSE, FALSE,
                      0);
  gtk_box_pack_start (GTK_BOX (content_area), contents, TRUE, TRUE, 0);

  /* underlying model */
  dialog->priv->icons_store = gtk_list_store_new (ICONS_N_COLUMNS,
                                                  G_TYPE_UINT,
                                                  G_TYPE_BOOLEAN,
                                                  G_TYPE_STRING);
1503 1504 1505
}

static void
1506
glade_named_icon_chooser_dialog_class_init (GladeNamedIconChooserDialogClass *klass)
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
{
  GObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkWindowClass *window_class;

  object_class = G_OBJECT_CLASS (klass);
  widget_class = GTK_WIDGET_CLASS (klass);
  window_class = GTK_WINDOW_CLASS (klass);

  object_class->finalize = glade_named_icon_chooser_dialog_finalize;

  widget_class->map = glade_named_icon_chooser_dialog_map;
  widget_class->unmap = glade_named_icon_chooser_dialog_unmap;
  widget_class->draw = glade_named_icon_chooser_dialog_draw;
  widget_class->show_all = glade_named_icon_chooser_dialog_show_all;
  widget_class->style_set = glade_named_icon_chooser_dialog_style_set;
  widget_class->screen_changed = glade_named_icon_chooser_dialog_screen_changed;

  window_class->set_focus = glade_named_icon_chooser_dialog_set_focus;

        /**
1528 1529 1530 1531 1532 1533 1534 1535
	 * GladeNamedIconChooserDialog::icon-activated
	 * @chooser: the object which received the signal
	 *
	 * This signal is emitted when the user "activates" an icon
	 * in the named icon chooser.  This can happen by double-clicking on an item
	 * in the recently used resources list, or by pressing
	 * <keycap>Enter</keycap>.
	*/
1536 1537 1538 1539 1540 1541 1542 1543 1544
  dialog_signals[ICON_ACTIVATED] =
      g_signal_new ("icon-activated",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeNamedIconChooserDialogClass,
                                     icon_activated), NULL, NULL,
                    g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);

        /**
1545 1546 1547 1548 1549 1550 1551 1552
	 * GladeNamedIconChooserDialog::selection-changed
	 * @chooser: the object which received the signal
	 *
	 * This signal is emitted when there is a change in the set of
	 * selected icon names.  This can happen when a user
	 * modifies the selection with the mouse or the keyboard, or when
	 * explicitely calling functions to change the selection.
	*/
1553 1554 1555 1556 1557 1558 1559
  dialog_signals[SELECTION_CHANGED] =
      g_signal_new ("selection-changed",
                    G_TYPE_FROM_CLASS (object_class),
                    G_SIGNAL_RUN_LAST,
                    G_STRUCT_OFFSET (GladeNamedIconChooserDialogClass,
                                     selection_changed), NULL, NULL,
                    g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
1560 1561 1562
}

static gboolean
1563
should_respond (GladeNamedIconChooserDialog *dialog)
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
{
  gchar *icon_name;

  /* is there an icon selected? */
  icon_name = glade_named_icon_chooser_dialog_get_icon_name (dialog);
  if (!icon_name)
    return FALSE;

  g_free (icon_name);
  return TRUE;
1574 1575 1576 1577 1578 1579
}

/* get's the name of the configuration file */
static gchar *
get_config_filename (void)
{
1580
  return g_build_filename (g_get_user_config_dir (), "gladeui", "config", NULL);
1581 1582 1583 1584 1585 1586
}

/* get's the name of the directory that contains the config file */
static char *
get_config_dirname (void)
{
1587
  return g_build_filename (g_get_user_config_dir (), "gladeui", NULL);
1588 1589 1590 1591
}

/* loads the configuration settings */
static void
1592
settings_load (GladeNamedIconChooserDialog *dialog)
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
{
  GKeyFile *keyfile;
  gboolean success, boolean_value;
  gchar *filename;
  GError *error = NULL;

  keyfile = g_key_file_new ();

  filename = get_config_filename ();
  success = g_key_file_load_from_file (keyfile,
                                       filename, G_KEY_FILE_NONE, &error);
  g_free (filename);

  if (!success)
    {

      dialog->priv->settings_list_standard = DEFAULT_SETTING_LIST_STANDARD_ONLY;

      g_clear_error (&error);
      g_key_file_free (keyfile);
      return;
    }


  boolean_value = g_key_file_get_boolean (keyfile,
                                          "Named Icon Chooser",
                                          "ListStandardOnly", &error);
  if (error)
    {
      dialog->priv->settings_list_standard = DEFAULT_SETTING_LIST_STANDARD_ONLY;
      g_clear_error (&error);
    }
  else
    {
      dialog->priv->settings_list_standard = boolean_value;
    }

  g_key_file_free (keyfile);
1631 1632 1633 1634
}

/* creates a GKeyFile based on the current settings */
static GKeyFile *
1635
settings_to_keyfile (GladeNamedIconChooserDialog *dialog)
1636 1637 1638 1639 1640 1641 1642
{
  GKeyFile *keyfile;
  gchar *filename;

  keyfile = g_key_file_new ();

  filename = get_config_filename ();
1643 1644 1645
  g_key_file_load_from_file (keyfile,
                             get_config_filename (),
                             G_KEY_FILE_NONE, NULL);
1646 1647 1648 1649 1650 1651 1652 1653
  g_free (filename);

  g_key_file_set_boolean (keyfile,
                          "Named Icon Chooser",
                          "ListStandardOnly",
                          dialog->priv->settings_list_standard);

  return keyfile;
1654 1655 1656 1657
}

/* serializes the the current configuration to the config file */
static void
1658
settings_save (GladeNamedIconChooserDialog *dialog)
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
{
  GKeyFile *keyfile;
  gchar *contents;
  gsize contents_length;
  gchar *filename = NULL, *dirname = NULL;
  GError *error = NULL;

  keyfile = settings_to_keyfile (dialog);

  contents = g_key_file_to_data (keyfile, &contents_length, &error);

  if (error)
    goto out;

  filename = get_config_filename ();

  if (!g_file_set_contents (filename, contents, contents_length, NULL))
    {
      gchar *dirname;
      gint saved_errno;

      dirname = get_config_dirname ();
      if (g_mkdir_with_parents (dirname, 0700) != 0)    /* 0700 per the XDG basedir spec */
        {

          saved_errno = errno;
          g_set_error (&error,
                       G_FILE_ERROR,
                       g_file_error_from_errno (saved_errno),
                       _("Could not create directory: %s"), dirname);
          goto out;
        }

      if (!g_file_set_contents (filename, contents, contents_length, &error))
        {
          goto out;
        }
    }

out:

  g_free (contents);
  g_free (dirname);
  g_free (filename);
  g_clear_error (&error);
  g_key_file_free (keyfile);
1705 1706 1707
}

static GtkWidget *
1708 1709 1710 1711
glade_named_icon_chooser_dialog_new_valist (const gchar *title,
                                            GtkWindow   *parent,
                                            const gchar *first_button_text,
                                            va_list      varargs)
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
{
  GtkWidget *result;
  const char *button_text = first_button_text;
  gint response_id;

  result = g_object_new (GLADE_TYPE_NAMED_ICON_CHOOSER_DIALOG,
                         "title", title, "transient-for", parent, NULL);

  while (button_text)
    {
      response_id = va_arg (varargs, gint);
      gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
      button_text = va_arg (varargs, const gchar *);
    }

  return result;
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
}

/**
 * glade_named_icon_chooser_dialog_new:
 * @title: Title of the dialog, or %NULL
 * @parent: Transient parent of the dialog, or %NULL,
 * @first_button_text: stock ID or text to go in the first button, or %NULL
 * @Varargs: response ID for the first button, then additional (button, id)
 *   pairs, ending with %NULL
 *
 * Creates a new #GladeNamedIconChooserDialog.  This function is analogous to
 * gtk_dialog_new_with_buttons().
 *
 * Return value: a new #GladeNamedIconChooserDialog
 */
GtkWidget *
1744 1745 1746 1747
glade_named_icon_chooser_dialog_new (const gchar *title,
                                     GtkWindow   *parent,
                                     const gchar *first_button_text,
                                     ...)
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
{
  GtkWidget *result;
  va_list varargs;

  va_start (varargs, first_button_text);
  result = glade_named_icon_chooser_dialog_new_valist (title,
                                                       parent,
                                                       first_button_text,
                                                       varargs);
  va_end (varargs);

  return result;
1760
}