gimplanguagecombobox.c 3.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimplanguagecombobox.c
 * Copyright (C) 2009  Sven Neumann <sven@gimp.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 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 */

/* GimpLanguageComboBox is a combo-box widget to select the user
 * interface language.
 */

#include "config.h"

#include <string.h>

#include <gtk/gtk.h>

#include "widgets-types.h"

#include "gimplanguagecombobox.h"
34
#include "gimptranslationstore.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62


struct _GimpLanguageComboBox
{
  GtkComboBox parent_instance;
};


G_DEFINE_TYPE (GimpLanguageComboBox,
               gimp_language_combo_box, GTK_TYPE_COMBO_BOX)

#define parent_class gimp_language_combo_box_parent_class


static void
gimp_language_combo_box_class_init (GimpLanguageComboBoxClass *klass)
{
}

static void
gimp_language_combo_box_init (GimpLanguageComboBox *combo)
{
  GtkCellRenderer *renderer;

  renderer = gtk_cell_renderer_text_new ();

  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
63
                                  "text",  GIMP_LANGUAGE_STORE_LABEL,
64
                                  NULL);
65 66 67

  /* See issues #2828 and #2642. */
  gtk_combo_box_set_wrap_width (GTK_COMBO_BOX (combo), 1);
68 69
}

70 71 72 73 74 75 76 77 78 79 80 81
/**
 * gimp_language_combo_box_new:
 * @manual_l18n: get only the sublist of manual languages.
 * @empty_label: the label for empty language code.
 *
 * Returns a combo box containing all GUI localization languages if
 * @manual_l18n is #FALSE, or all manual localization languages
 * otherwise. If @empty_label is not #NULL, an entry with this label
 * will be created for the language code "", otherwise if @empty_label
 * is #NULL and @manual_l18n is #FALSE, the entry will be "System
 * Language" localized in itself (not in the GUI language).
 */
82
GtkWidget *
83 84
gimp_language_combo_box_new (gboolean     manual_l18n,
                             const gchar *empty_label)
85 86 87 88
{
  GtkWidget    *combo;
  GtkListStore *store;

89
  store = gimp_translation_store_new (manual_l18n, empty_label);
90 91 92 93 94 95 96 97 98 99
  combo = g_object_new (GIMP_TYPE_LANGUAGE_COMBO_BOX,
                        "model", store,
                        NULL);

  g_object_unref (store);

  return combo;
}

gchar *
100
gimp_language_combo_box_get_code (GimpLanguageComboBox *combo)
101 102 103 104 105 106 107 108 109 110
{
  GtkTreeIter  iter;
  gchar       *code;

  g_return_val_if_fail (GIMP_IS_LANGUAGE_COMBO_BOX (combo), NULL);

  if (! gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter))
    return NULL;

  gtk_tree_model_get (gtk_combo_box_get_model (GTK_COMBO_BOX (combo)), &iter,
111
                      GIMP_LANGUAGE_STORE_CODE, &code,
112 113 114 115 116 117
                      -1);

  return code;
}

gboolean
118 119
gimp_language_combo_box_set_code (GimpLanguageComboBox *combo,
                                  const gchar          *code)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
{
  GtkTreeModel *model;
  GtkTreeIter   iter;

  g_return_val_if_fail (GIMP_IS_LANGUAGE_COMBO_BOX (combo), FALSE);

  if (! code || ! strlen (code))
    {
      gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
      return TRUE;
    }

  model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));

  if (gimp_language_store_lookup (GIMP_LANGUAGE_STORE (model), code, &iter))
    {
      gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo), &iter);
      return TRUE;
    }

  return FALSE;
}