2013-01-27 23:18:24 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Red Hat, 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
|
2014-01-23 12:57:27 +01:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2013-01-27 23:18:24 -05:00
|
|
|
*
|
|
|
|
* Written by:
|
|
|
|
* Matthias Clasen
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <config.h>
|
|
|
|
#include "cc-format-chooser.h"
|
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
#include <langinfo.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
|
2018-04-06 13:38:40 +02:00
|
|
|
#include "list-box-helper.h"
|
2013-01-27 23:18:24 -05:00
|
|
|
#include "cc-common-language.h"
|
2013-02-05 18:08:44 +01:00
|
|
|
#include "cc-util.h"
|
2013-01-27 23:18:24 -05:00
|
|
|
|
|
|
|
#define GNOME_DESKTOP_USE_UNSTABLE_API
|
|
|
|
#include <libgnome-desktop/gnome-languages.h>
|
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
struct _CcFormatChooser {
|
|
|
|
GtkDialog parent_instance;
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2015-07-07 17:20:36 +02:00
|
|
|
GtkWidget *done_button;
|
2013-01-27 23:18:24 -05:00
|
|
|
GtkWidget *no_results;
|
2013-06-13 12:21:06 +02:00
|
|
|
GtkListBoxRow *more_item;
|
2018-06-29 12:36:21 +12:00
|
|
|
GtkWidget *region_filter_entry;
|
|
|
|
GtkWidget *region_listbox;
|
|
|
|
GtkWidget *date_format_label;
|
|
|
|
GtkWidget *time_format_label;
|
|
|
|
GtkWidget *date_time_format_label;
|
|
|
|
GtkWidget *number_format_label;
|
|
|
|
GtkWidget *measurement_format_label;
|
|
|
|
GtkWidget *paper_format_label;
|
2013-01-27 23:18:24 -05:00
|
|
|
gboolean adding;
|
|
|
|
gboolean showing_extra;
|
|
|
|
gchar *region;
|
2013-02-05 18:20:05 +01:00
|
|
|
gchar **filter_words;
|
2018-06-29 12:36:21 +12:00
|
|
|
};
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
G_DEFINE_TYPE (CcFormatChooser, cc_format_chooser, GTK_TYPE_DIALOG)
|
2013-01-27 23:18:24 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
display_date (GtkWidget *label, GDateTime *dt, const gchar *format)
|
|
|
|
{
|
2018-01-26 16:18:15 +13:00
|
|
|
g_autofree gchar *s = g_date_time_format (dt, format);
|
|
|
|
gtk_label_set_text (GTK_LABEL (label), g_strstrip (s));
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-06-29 12:36:21 +12:00
|
|
|
update_format_examples (CcFormatChooser *chooser)
|
2013-01-27 23:18:24 -05:00
|
|
|
{
|
2018-01-26 16:18:15 +13:00
|
|
|
g_autofree gchar *time_locale = NULL;
|
|
|
|
g_autofree gchar *numeric_locale = NULL;
|
|
|
|
g_autofree gchar *monetary_locale = NULL;
|
|
|
|
g_autofree gchar *measurement_locale = NULL;
|
|
|
|
g_autofree gchar *paper_locale = NULL;
|
|
|
|
g_autoptr(GDateTime) dt = NULL;
|
|
|
|
g_autofree gchar *s = NULL;
|
2013-01-27 23:18:24 -05:00
|
|
|
const gchar *fmt;
|
2018-01-26 16:18:15 +13:00
|
|
|
g_autoptr(GtkPaperSize) paper = NULL;
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-01-26 16:18:15 +13:00
|
|
|
time_locale = g_strdup (setlocale (LC_TIME, NULL));
|
2018-06-29 12:36:21 +12:00
|
|
|
setlocale (LC_TIME, chooser->region);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
|
|
|
dt = g_date_time_new_now_local ();
|
2018-06-29 12:36:21 +12:00
|
|
|
display_date (chooser->date_format_label, dt, "%x");
|
|
|
|
display_date (chooser->time_format_label, dt, "%X");
|
|
|
|
display_date (chooser->date_time_format_label, dt, "%c");
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-01-26 16:18:15 +13:00
|
|
|
setlocale (LC_TIME, time_locale);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-01-26 16:18:15 +13:00
|
|
|
numeric_locale = g_strdup (setlocale (LC_NUMERIC, NULL));
|
2018-06-29 12:36:21 +12:00
|
|
|
setlocale (LC_NUMERIC, chooser->region);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
|
|
|
s = g_strdup_printf ("%'.2f", 123456789.00);
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_label_set_text (GTK_LABEL (chooser->number_format_label), s);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-01-26 16:18:15 +13:00
|
|
|
setlocale (LC_NUMERIC, numeric_locale);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
|
|
|
#if 0
|
2018-01-26 16:18:15 +13:00
|
|
|
monetary_locale = g_strdup (setlocale (LC_MONETARY, NULL));
|
2018-06-29 12:36:21 +12:00
|
|
|
setlocale (LC_MONETARY, chooser->region);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
|
|
|
num_info = localeconv ();
|
2013-02-17 00:41:08 +01:00
|
|
|
if (num_info != NULL)
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_label_set_text (GTK_LABEL (chooser->currency_format_label), num_info->currency_symbol);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-01-26 16:18:15 +13:00
|
|
|
setlocale (LC_MONETARY, monetary_locale);
|
2013-01-27 23:18:24 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LC_MEASUREMENT
|
2018-01-26 16:18:15 +13:00
|
|
|
measurement_locale = g_strdup (setlocale (LC_MEASUREMENT, NULL));
|
2018-06-29 12:36:21 +12:00
|
|
|
setlocale (LC_MEASUREMENT, chooser->region);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
|
|
|
fmt = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
|
|
|
|
if (fmt && *fmt == 2)
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_label_set_text (GTK_LABEL (chooser->measurement_format_label), C_("measurement format", "Imperial"));
|
2013-01-27 23:18:24 -05:00
|
|
|
else
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_label_set_text (GTK_LABEL (chooser->measurement_format_label), C_("measurement format", "Metric"));
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-01-26 16:18:15 +13:00
|
|
|
setlocale (LC_MEASUREMENT, measurement_locale);
|
2013-01-27 23:18:24 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LC_PAPER
|
2018-01-26 16:18:15 +13:00
|
|
|
paper_locale = g_strdup (setlocale (LC_PAPER, NULL));
|
2018-06-29 12:36:21 +12:00
|
|
|
setlocale (LC_PAPER, chooser->region);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
|
|
|
paper = gtk_paper_size_new (gtk_paper_size_get_default ());
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_label_set_text (GTK_LABEL (chooser->paper_format_label), gtk_paper_size_get_display_name (paper));
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-01-26 16:18:15 +13:00
|
|
|
setlocale (LC_PAPER, paper_locale);
|
2013-01-27 23:18:24 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-06-29 12:36:21 +12:00
|
|
|
set_locale_id (CcFormatChooser *chooser,
|
|
|
|
const gchar *locale_id)
|
2013-01-27 23:18:24 -05:00
|
|
|
{
|
2018-01-26 16:18:15 +13:00
|
|
|
g_autoptr(GList) children = NULL;
|
|
|
|
GList *l;
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
children = gtk_container_get_children (GTK_CONTAINER (chooser->region_listbox));
|
2013-01-27 23:18:24 -05:00
|
|
|
for (l = children; l; l = l->next) {
|
|
|
|
GtkWidget *row = l->data;
|
|
|
|
GtkWidget *check = g_object_get_data (G_OBJECT (row), "check");
|
|
|
|
const gchar *region = g_object_get_data (G_OBJECT (row), "locale-id");
|
|
|
|
if (check == NULL || region == NULL)
|
|
|
|
continue;
|
|
|
|
|
2013-02-17 00:42:18 +01:00
|
|
|
if (g_strcmp0 (locale_id, region) == 0) {
|
2013-01-27 23:18:24 -05:00
|
|
|
gboolean is_extra;
|
|
|
|
|
|
|
|
/* mark as selected */
|
2013-03-15 18:37:33 +01:00
|
|
|
gtk_widget_set_opacity (check, 1.0);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
|
|
|
/* make sure this row is shown */
|
|
|
|
is_extra = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (row), "is-extra"));
|
2018-06-29 12:36:21 +12:00
|
|
|
if (!chooser->showing_extra && is_extra) {
|
2013-01-27 23:18:24 -05:00
|
|
|
g_object_set_data (G_OBJECT (row), "is-extra", GINT_TO_POINTER (FALSE));
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_list_box_invalidate_filter (GTK_LIST_BOX (chooser->region_listbox));
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* mark as unselected */
|
2013-03-15 18:37:33 +01:00
|
|
|
gtk_widget_set_opacity (check, 0.0);
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
g_free (chooser->region);
|
|
|
|
chooser->region = g_strdup (locale_id);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
|
|
|
update_format_examples (chooser);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
sort_regions (gconstpointer a,
|
|
|
|
gconstpointer b,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
const gchar *la;
|
|
|
|
const gchar *lb;
|
|
|
|
|
2013-02-17 00:41:08 +01:00
|
|
|
if (g_object_get_data (G_OBJECT (a), "locale-id") == NULL)
|
2013-01-27 23:18:24 -05:00
|
|
|
return 1;
|
2013-02-17 00:41:08 +01:00
|
|
|
if (g_object_get_data (G_OBJECT (b), "locale-id") == NULL)
|
2013-01-27 23:18:24 -05:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
la = g_object_get_data (G_OBJECT (a), "locale-name");
|
|
|
|
lb = g_object_get_data (G_OBJECT (b), "locale-name");
|
|
|
|
|
2013-02-05 17:53:09 +01:00
|
|
|
return g_strcmp0 (la, lb);
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
padded_label_new (char *text, gboolean narrow)
|
|
|
|
{
|
2018-10-11 15:47:14 +13:00
|
|
|
GtkWidget *widget, *label;
|
2013-01-27 23:18:24 -05:00
|
|
|
|
|
|
|
widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
|
|
|
|
gtk_widget_set_halign (widget, GTK_ALIGN_CENTER);
|
|
|
|
gtk_widget_set_margin_top (widget, 10);
|
|
|
|
gtk_widget_set_margin_bottom (widget, 10);
|
2013-11-19 22:29:36 +02:00
|
|
|
gtk_widget_set_margin_start (widget, narrow ? 10 : 80);
|
|
|
|
gtk_widget_set_margin_end (widget, narrow ? 10 : 80);
|
2018-10-11 15:47:14 +13:00
|
|
|
label = gtk_label_new (text);
|
|
|
|
gtk_widget_show (label);
|
|
|
|
gtk_container_add (GTK_CONTAINER (widget), label);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
region_widget_new (const gchar *locale_id,
|
|
|
|
gboolean is_extra)
|
|
|
|
{
|
|
|
|
gchar *locale_name;
|
2013-02-05 18:08:44 +01:00
|
|
|
gchar *locale_current_name;
|
|
|
|
gchar *locale_untranslated_name;
|
2013-06-13 12:21:06 +02:00
|
|
|
GtkWidget *row, *box;
|
2013-01-27 23:18:24 -05:00
|
|
|
GtkWidget *check;
|
|
|
|
|
2013-02-04 13:47:59 +01:00
|
|
|
locale_name = gnome_get_country_from_locale (locale_id, locale_id);
|
2015-01-09 18:47:46 +08:00
|
|
|
if (!locale_name)
|
|
|
|
return NULL;
|
|
|
|
|
2013-02-05 18:08:44 +01:00
|
|
|
locale_current_name = gnome_get_country_from_locale (locale_id, NULL);
|
|
|
|
locale_untranslated_name = gnome_get_country_from_locale (locale_id, "C");
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2013-06-13 12:21:06 +02:00
|
|
|
row = gtk_list_box_row_new ();
|
2018-10-11 15:47:14 +13:00
|
|
|
gtk_widget_show (row);
|
2013-06-13 12:21:06 +02:00
|
|
|
box = padded_label_new (locale_name, is_extra);
|
2018-10-11 15:47:14 +13:00
|
|
|
gtk_widget_show (box);
|
2013-06-13 12:21:06 +02:00
|
|
|
gtk_container_add (GTK_CONTAINER (row), box);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2013-03-15 18:37:33 +01:00
|
|
|
/* We add a check on each side of the label to keep it centered. */
|
2013-01-27 23:18:24 -05:00
|
|
|
check = gtk_image_new ();
|
2018-10-11 15:47:14 +13:00
|
|
|
gtk_widget_show (check);
|
2013-02-20 19:05:26 -05:00
|
|
|
gtk_image_set_from_icon_name (GTK_IMAGE (check), "object-select-symbolic", GTK_ICON_SIZE_MENU);
|
2013-03-15 18:37:33 +01:00
|
|
|
gtk_widget_set_opacity (check, 0.0);
|
|
|
|
g_object_set (check, "icon-size", GTK_ICON_SIZE_MENU, NULL);
|
2018-09-11 10:39:09 +12:00
|
|
|
gtk_container_add (GTK_CONTAINER (box), check);
|
2013-06-13 12:21:06 +02:00
|
|
|
gtk_box_reorder_child (GTK_BOX (box), check, 0);
|
2013-03-15 18:37:33 +01:00
|
|
|
|
|
|
|
check = gtk_image_new ();
|
2018-10-11 15:47:14 +13:00
|
|
|
gtk_widget_show (check);
|
2013-03-15 18:37:33 +01:00
|
|
|
gtk_image_set_from_icon_name (GTK_IMAGE (check), "object-select-symbolic", GTK_ICON_SIZE_MENU);
|
|
|
|
gtk_widget_set_opacity (check, 0.0);
|
2013-01-27 23:18:24 -05:00
|
|
|
g_object_set (check, "icon-size", GTK_ICON_SIZE_MENU, NULL);
|
2018-09-11 10:39:09 +12:00
|
|
|
gtk_container_add (GTK_CONTAINER (box), check);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2013-06-13 12:21:06 +02:00
|
|
|
g_object_set_data (G_OBJECT (row), "check", check);
|
|
|
|
g_object_set_data_full (G_OBJECT (row), "locale-id", g_strdup (locale_id), g_free);
|
|
|
|
g_object_set_data_full (G_OBJECT (row), "locale-name", locale_name, g_free);
|
|
|
|
g_object_set_data_full (G_OBJECT (row), "locale-current-name", locale_current_name, g_free);
|
|
|
|
g_object_set_data_full (G_OBJECT (row), "locale-untranslated-name", locale_untranslated_name, g_free);
|
|
|
|
g_object_set_data (G_OBJECT (row), "is-extra", GUINT_TO_POINTER (is_extra));
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2013-06-13 12:21:06 +02:00
|
|
|
return row;
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
|
2013-06-13 12:21:06 +02:00
|
|
|
static GtkListBoxRow *
|
2013-01-27 23:18:24 -05:00
|
|
|
more_widget_new (void)
|
|
|
|
{
|
2013-06-13 12:21:06 +02:00
|
|
|
GtkWidget *box, *row;
|
2013-03-08 22:32:55 +01:00
|
|
|
GtkWidget *arrow;
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2013-06-13 12:21:06 +02:00
|
|
|
row = gtk_list_box_row_new ();
|
|
|
|
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
|
2018-10-11 15:47:14 +13:00
|
|
|
gtk_widget_show (box);
|
2013-06-13 12:21:06 +02:00
|
|
|
gtk_container_add (GTK_CONTAINER (row), box);
|
|
|
|
gtk_widget_set_tooltip_text (box, _("More…"));
|
2013-03-08 22:32:55 +01:00
|
|
|
|
|
|
|
arrow = gtk_image_new_from_icon_name ("view-more-symbolic", GTK_ICON_SIZE_MENU);
|
2018-10-11 15:47:14 +13:00
|
|
|
gtk_widget_show (arrow);
|
2013-03-08 22:32:55 +01:00
|
|
|
gtk_style_context_add_class (gtk_widget_get_style_context (arrow), "dim-label");
|
2018-09-11 10:39:09 +12:00
|
|
|
gtk_widget_set_hexpand (arrow, TRUE);
|
2013-06-13 12:21:06 +02:00
|
|
|
gtk_widget_set_margin_top (box, 10);
|
|
|
|
gtk_widget_set_margin_bottom (box, 10);
|
2018-09-11 10:39:09 +12:00
|
|
|
gtk_container_add (GTK_CONTAINER (box), arrow);
|
2013-03-08 22:32:55 +01:00
|
|
|
|
2013-06-13 12:21:06 +02:00
|
|
|
return GTK_LIST_BOX_ROW (row);
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
no_results_widget_new (void)
|
|
|
|
{
|
|
|
|
GtkWidget *widget;
|
|
|
|
|
|
|
|
widget = padded_label_new (_("No regions found"), TRUE);
|
|
|
|
gtk_widget_set_sensitive (widget, FALSE);
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-06-29 12:36:21 +12:00
|
|
|
add_regions (CcFormatChooser *chooser,
|
|
|
|
gchar **locale_ids,
|
|
|
|
GHashTable *initial)
|
2013-01-27 23:18:24 -05:00
|
|
|
{
|
2018-06-29 12:36:21 +12:00
|
|
|
chooser->adding = TRUE;
|
2013-01-27 23:18:24 -05:00
|
|
|
|
|
|
|
while (*locale_ids) {
|
|
|
|
gchar *locale_id;
|
|
|
|
gboolean is_initial;
|
|
|
|
GtkWidget *widget;
|
|
|
|
|
|
|
|
locale_id = *locale_ids;
|
|
|
|
locale_ids ++;
|
|
|
|
|
|
|
|
if (!cc_common_language_has_font (locale_id))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
is_initial = (g_hash_table_lookup (initial, locale_id) != NULL);
|
|
|
|
widget = region_widget_new (locale_id, !is_initial);
|
2015-01-09 18:47:46 +08:00
|
|
|
if (!widget)
|
|
|
|
continue;
|
|
|
|
|
2018-10-11 15:47:14 +13:00
|
|
|
gtk_widget_show (widget);
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_container_add (GTK_CONTAINER (chooser->region_listbox), widget);
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_container_add (GTK_CONTAINER (chooser->region_listbox), GTK_WIDGET (chooser->more_item));
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
chooser->adding = FALSE;
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-06-29 12:36:21 +12:00
|
|
|
add_all_regions (CcFormatChooser *chooser)
|
2013-01-27 23:18:24 -05:00
|
|
|
{
|
2018-01-26 16:18:15 +13:00
|
|
|
g_auto(GStrv) locale_ids = NULL;
|
|
|
|
g_autoptr(GHashTable) initial = NULL;
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2013-02-04 13:47:59 +01:00
|
|
|
locale_ids = gnome_get_all_locales ();
|
2013-01-27 23:18:24 -05:00
|
|
|
initial = cc_common_language_get_initial_languages ();
|
|
|
|
add_regions (chooser, locale_ids, initial);
|
|
|
|
}
|
|
|
|
|
2013-02-05 18:20:05 +01:00
|
|
|
static gboolean
|
|
|
|
match_all (gchar **words,
|
|
|
|
const gchar *str)
|
|
|
|
{
|
|
|
|
gchar **w;
|
|
|
|
|
|
|
|
for (w = words; *w; ++w)
|
|
|
|
if (!strstr (str, *w))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-01-27 23:18:24 -05:00
|
|
|
static gboolean
|
2013-06-13 12:21:06 +02:00
|
|
|
region_visible (GtkListBoxRow *row,
|
2013-01-27 23:18:24 -05:00
|
|
|
gpointer user_data)
|
|
|
|
{
|
2018-06-29 12:36:21 +12:00
|
|
|
CcFormatChooser *chooser = user_data;
|
2018-01-26 16:18:15 +13:00
|
|
|
g_autofree gchar *locale_name = NULL;
|
|
|
|
g_autofree gchar *locale_current_name = NULL;
|
|
|
|
g_autofree gchar *locale_untranslated_name = NULL;
|
2013-01-27 23:18:24 -05:00
|
|
|
gboolean is_extra;
|
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
if (row == chooser->more_item)
|
|
|
|
return !chooser->showing_extra;
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2013-06-13 12:21:06 +02:00
|
|
|
is_extra = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (row), "is-extra"));
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
if (!chooser->showing_extra && is_extra)
|
2013-01-27 23:18:24 -05:00
|
|
|
return FALSE;
|
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
if (!chooser->filter_words)
|
2013-02-05 18:08:44 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
locale_name =
|
2013-06-13 12:21:06 +02:00
|
|
|
cc_util_normalize_casefold_and_unaccent (g_object_get_data (G_OBJECT (row), "locale-name"));
|
2018-06-29 12:36:21 +12:00
|
|
|
if (match_all (chooser->filter_words, locale_name))
|
2018-01-26 16:18:15 +13:00
|
|
|
return TRUE;
|
2013-02-05 18:08:44 +01:00
|
|
|
|
|
|
|
locale_current_name =
|
2013-06-13 12:21:06 +02:00
|
|
|
cc_util_normalize_casefold_and_unaccent (g_object_get_data (G_OBJECT (row), "locale-current-name"));
|
2018-06-29 12:36:21 +12:00
|
|
|
if (match_all (chooser->filter_words, locale_current_name))
|
2018-01-26 16:18:15 +13:00
|
|
|
return TRUE;
|
2013-02-05 18:08:44 +01:00
|
|
|
|
|
|
|
locale_untranslated_name =
|
2013-06-13 12:21:06 +02:00
|
|
|
cc_util_normalize_casefold_and_unaccent (g_object_get_data (G_OBJECT (row), "locale-untranslated-name"));
|
2018-06-29 12:36:21 +12:00
|
|
|
return match_all (chooser->filter_words, locale_untranslated_name);
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
|
2013-02-05 18:20:05 +01:00
|
|
|
static void
|
2018-06-29 12:36:21 +12:00
|
|
|
filter_changed (CcFormatChooser *chooser)
|
2013-02-05 18:20:05 +01:00
|
|
|
{
|
2018-01-26 16:18:15 +13:00
|
|
|
g_autofree gchar *filter_contents = NULL;
|
2013-02-05 18:20:05 +01:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
g_clear_pointer (&chooser->filter_words, g_strfreev);
|
2013-02-05 18:20:05 +01:00
|
|
|
|
|
|
|
filter_contents =
|
2018-06-29 12:36:21 +12:00
|
|
|
cc_util_normalize_casefold_and_unaccent (gtk_entry_get_text (GTK_ENTRY (chooser->region_filter_entry)));
|
2013-02-05 18:20:05 +01:00
|
|
|
if (!filter_contents) {
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_list_box_invalidate_filter (GTK_LIST_BOX (chooser->region_listbox));
|
|
|
|
gtk_list_box_set_placeholder (GTK_LIST_BOX (chooser->region_listbox), NULL);
|
2013-02-05 18:20:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-06-29 12:36:21 +12:00
|
|
|
chooser->filter_words = g_strsplit_set (g_strstrip (filter_contents), " ", 0);
|
|
|
|
gtk_list_box_set_placeholder (GTK_LIST_BOX (chooser->region_listbox), GTK_WIDGET (chooser->no_results));
|
|
|
|
gtk_list_box_invalidate_filter (GTK_LIST_BOX (chooser->region_listbox));
|
2013-02-05 18:20:05 +01:00
|
|
|
}
|
|
|
|
|
2013-01-27 23:18:24 -05:00
|
|
|
static void
|
2018-06-29 12:36:21 +12:00
|
|
|
show_more (CcFormatChooser *chooser)
|
2013-01-27 23:18:24 -05:00
|
|
|
{
|
|
|
|
gint width, height;
|
|
|
|
|
|
|
|
gtk_window_get_size (GTK_WINDOW (chooser), &width, &height);
|
|
|
|
gtk_widget_set_size_request (GTK_WIDGET (chooser), width, height);
|
|
|
|
gtk_window_set_resizable (GTK_WINDOW (chooser), TRUE);
|
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_widget_show (chooser->region_filter_entry);
|
|
|
|
gtk_widget_grab_focus (chooser->region_filter_entry);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
chooser->showing_extra = TRUE;
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_list_box_invalidate_filter (GTK_LIST_BOX (chooser->region_listbox));
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-10-09 09:40:50 +13:00
|
|
|
row_activated (CcFormatChooser *chooser,
|
|
|
|
GtkListBoxRow *row)
|
2013-01-27 23:18:24 -05:00
|
|
|
{
|
2018-01-26 16:18:15 +13:00
|
|
|
const gchar *new_locale_id;
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
if (chooser->adding)
|
2013-01-27 23:18:24 -05:00
|
|
|
return;
|
|
|
|
|
2013-06-13 12:21:06 +02:00
|
|
|
if (row == NULL)
|
2013-01-27 23:18:24 -05:00
|
|
|
return;
|
2013-02-17 00:41:08 +01:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
if (row == chooser->more_item) {
|
2013-01-27 23:18:24 -05:00
|
|
|
show_more (chooser);
|
2013-02-17 00:41:08 +01:00
|
|
|
return;
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
2013-06-13 12:21:06 +02:00
|
|
|
new_locale_id = g_object_get_data (G_OBJECT (row), "locale-id");
|
2018-06-29 12:36:21 +12:00
|
|
|
if (g_strcmp0 (new_locale_id, chooser->region) == 0) {
|
2015-07-07 17:20:36 +02:00
|
|
|
gtk_dialog_response (GTK_DIALOG (chooser),
|
|
|
|
gtk_dialog_get_response_for_widget (GTK_DIALOG (chooser),
|
2018-06-29 12:36:21 +12:00
|
|
|
chooser->done_button));
|
2015-07-07 17:20:36 +02:00
|
|
|
} else {
|
|
|
|
set_locale_id (chooser, new_locale_id);
|
|
|
|
}
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
|
2015-07-07 17:58:47 +02:00
|
|
|
static void
|
2018-10-09 09:40:50 +13:00
|
|
|
activate_default (CcFormatChooser *chooser)
|
2015-07-07 17:58:47 +02:00
|
|
|
{
|
|
|
|
GtkWidget *focus;
|
2018-01-26 16:18:15 +13:00
|
|
|
const gchar *locale_id;
|
2015-07-07 17:58:47 +02:00
|
|
|
|
2018-10-09 09:40:50 +13:00
|
|
|
focus = gtk_window_get_focus (GTK_WINDOW (chooser));
|
2015-07-07 17:58:47 +02:00
|
|
|
if (!focus)
|
|
|
|
return;
|
|
|
|
|
|
|
|
locale_id = g_object_get_data (G_OBJECT (focus), "locale-id");
|
2018-06-29 12:36:21 +12:00
|
|
|
if (g_strcmp0 (locale_id, chooser->region) == 0)
|
2015-07-07 17:58:47 +02:00
|
|
|
return;
|
|
|
|
|
2018-10-09 09:40:50 +13:00
|
|
|
g_signal_stop_emission_by_name (chooser, "activate-default");
|
2015-07-07 17:58:47 +02:00
|
|
|
gtk_widget_activate (focus);
|
|
|
|
}
|
|
|
|
|
2013-01-27 23:18:24 -05:00
|
|
|
static void
|
2018-06-29 12:36:21 +12:00
|
|
|
cc_format_chooser_dispose (GObject *object)
|
2013-01-27 23:18:24 -05:00
|
|
|
{
|
2018-06-29 12:36:21 +12:00
|
|
|
CcFormatChooser *chooser = CC_FORMAT_CHOOSER (object);
|
2013-02-05 17:56:34 +01:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
g_clear_object (&chooser->no_results);
|
|
|
|
g_clear_pointer (&chooser->filter_words, g_strfreev);
|
|
|
|
g_clear_pointer (&chooser->region, g_free);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
G_OBJECT_CLASS (cc_format_chooser_parent_class)->dispose (object);
|
|
|
|
}
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
void
|
|
|
|
cc_format_chooser_class_init (CcFormatChooserClass *klass)
|
2013-01-27 23:18:24 -05:00
|
|
|
{
|
2018-06-29 12:36:21 +12:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->dispose = cc_format_chooser_dispose;
|
|
|
|
|
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/region/cc-format-chooser.ui");
|
|
|
|
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcFormatChooser, done_button);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcFormatChooser, region_filter_entry);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcFormatChooser, region_listbox);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcFormatChooser, date_format_label);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcFormatChooser, time_format_label);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcFormatChooser, date_time_format_label);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcFormatChooser, number_format_label);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcFormatChooser, measurement_format_label);
|
|
|
|
gtk_widget_class_bind_template_child (widget_class, CcFormatChooser, paper_format_label);
|
|
|
|
}
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
void
|
|
|
|
cc_format_chooser_init (CcFormatChooser *chooser)
|
|
|
|
{
|
|
|
|
gtk_widget_init_template (GTK_WIDGET (chooser));
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
chooser->more_item = more_widget_new ();
|
2018-10-11 15:47:14 +13:00
|
|
|
gtk_widget_show (GTK_WIDGET (chooser->more_item));
|
2013-06-13 12:21:06 +02:00
|
|
|
/* We ref-sink here so we can reuse this widget multiple times */
|
2018-06-29 12:36:21 +12:00
|
|
|
chooser->no_results = g_object_ref_sink (no_results_widget_new ());
|
2018-10-11 15:47:14 +13:00
|
|
|
gtk_widget_show (chooser->no_results);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_list_box_set_sort_func (GTK_LIST_BOX (chooser->region_listbox),
|
2013-06-13 12:21:06 +02:00
|
|
|
(GtkListBoxSortFunc)sort_regions, chooser, NULL);
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_list_box_set_filter_func (GTK_LIST_BOX (chooser->region_listbox),
|
2013-01-27 23:18:24 -05:00
|
|
|
region_visible, chooser, NULL);
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_list_box_set_selection_mode (GTK_LIST_BOX (chooser->region_listbox),
|
2013-01-27 23:18:24 -05:00
|
|
|
GTK_SELECTION_NONE);
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_list_box_set_header_func (GTK_LIST_BOX (chooser->region_listbox),
|
2014-06-23 15:37:34 +02:00
|
|
|
cc_list_box_update_header_func, NULL, NULL);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
add_all_regions (chooser);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-10-09 09:40:50 +13:00
|
|
|
g_signal_connect_object (chooser->region_filter_entry, "search-changed",
|
|
|
|
G_CALLBACK (filter_changed), chooser, G_CONNECT_SWAPPED);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-10-09 09:40:50 +13:00
|
|
|
g_signal_connect_object (chooser->region_listbox, "row-activated",
|
|
|
|
G_CALLBACK (row_activated), chooser, G_CONNECT_SWAPPED);
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
gtk_list_box_invalidate_filter (GTK_LIST_BOX (chooser->region_listbox));
|
2013-01-27 23:18:24 -05:00
|
|
|
|
2018-10-09 09:40:50 +13:00
|
|
|
g_signal_connect_object (chooser, "activate-default",
|
|
|
|
G_CALLBACK (activate_default), chooser, G_CONNECT_SWAPPED);
|
2018-06-29 12:36:21 +12:00
|
|
|
}
|
2015-07-07 17:58:47 +02:00
|
|
|
|
2018-06-29 12:36:21 +12:00
|
|
|
CcFormatChooser *
|
|
|
|
cc_format_chooser_new (void)
|
|
|
|
{
|
|
|
|
return CC_FORMAT_CHOOSER (g_object_new (CC_TYPE_FORMAT_CHOOSER,
|
|
|
|
"use-header-bar", 1,
|
|
|
|
NULL));
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-06-29 12:36:21 +12:00
|
|
|
cc_format_chooser_clear_filter (CcFormatChooser *chooser)
|
2013-01-27 23:18:24 -05:00
|
|
|
{
|
2018-06-29 12:36:21 +12:00
|
|
|
g_return_if_fail (CC_IS_FORMAT_CHOOSER (chooser));
|
|
|
|
gtk_entry_set_text (GTK_ENTRY (chooser->region_filter_entry), "");
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const gchar *
|
2018-06-29 12:36:21 +12:00
|
|
|
cc_format_chooser_get_region (CcFormatChooser *chooser)
|
2013-01-27 23:18:24 -05:00
|
|
|
{
|
2018-06-29 12:36:21 +12:00
|
|
|
g_return_val_if_fail (CC_IS_FORMAT_CHOOSER (chooser), NULL);
|
|
|
|
return chooser->region;
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-06-29 12:36:21 +12:00
|
|
|
cc_format_chooser_set_region (CcFormatChooser *chooser,
|
|
|
|
const gchar *region)
|
2013-01-27 23:18:24 -05:00
|
|
|
{
|
2018-06-29 12:36:21 +12:00
|
|
|
g_return_if_fail (CC_IS_FORMAT_CHOOSER (chooser));
|
|
|
|
set_locale_id (chooser, region);
|
2013-01-27 23:18:24 -05:00
|
|
|
}
|