Compare commits

..

7 Commits

Author SHA1 Message Date
Matthias Clasen
01bcd96d16 Reimplement gtk_choose_color without a dialog
This commit introduces a private GtkColorChooserWindow
which is a copy of GtkColorChooserDialog with the dialog
bits redone, and uses it for the async color choose API.

When GtkColorChooserDialog is dropped, the color chooser
window can be renamed (and made public, if desired).

We want to get rid of GtkDialog. This is a step in that direction.
2022-10-23 08:05:17 -04:00
Matthias Clasen
951773452a wip: Add async api to choose a file
This is an experiment to replace explicit use
of chooser dialogs with an async API.
2022-10-22 23:53:13 -04:00
Matthias Clasen
d0af6f812d wip: Add async api to choose a font
This is an experiment to replace explicit use
of chooser dialogs with an async API.
2022-10-22 23:53:13 -04:00
Matthias Clasen
b665a95558 wip: Add async api to choose a color
This is an experiment to replace explicit use
of chooser dialogs with an async API.
2022-10-22 23:53:13 -04:00
Matthias Clasen
799fb41937 Merge branch 'deprecation-cleanups' into 'main'
gtk-demo: Replace combo boxes in font features

See merge request GNOME/gtk!5164
2022-10-22 19:23:45 +00:00
Matthias Clasen
e6f4c596fa gtk-demo: Use global style in font_features 2022-10-22 11:38:46 -04:00
Matthias Clasen
3d7fb26588 gtk-demo: Replace combo boxes in font features
This is a slightly more complicated combo box,
so we need an auxiliary object.
2022-10-22 11:35:35 -04:00
15 changed files with 1061 additions and 211 deletions

View File

@@ -21,7 +21,75 @@
#include "script-names.h"
#include "language-names.h"
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
/* {{{ ScriptLang object */
G_DECLARE_FINAL_TYPE (ScriptLang, script_lang, SCRIPT, LANG, GObject)
struct _ScriptLang
{
GObject parent;
char *langname;
unsigned int script_index;
unsigned int lang_index;
hb_tag_t lang_tag;
};
struct _ScriptLangClass
{
GObjectClass parent_class;
};
G_DEFINE_TYPE (ScriptLang, script_lang, G_TYPE_OBJECT)
static void
script_lang_init (ScriptLang *self)
{
}
static void
script_lang_finalize (GObject *object)
{
ScriptLang *self = SCRIPT_LANG (object);
g_free (self->langname);
G_OBJECT_CLASS (script_lang_parent_class)->finalize (object);
}
static void
script_lang_class_init (ScriptLangClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->finalize = script_lang_finalize;
}
static ScriptLang *
script_lang_new (const char *langname,
unsigned int script_index,
unsigned int lang_index,
hb_tag_t lang_tag)
{
ScriptLang *self;
self = g_object_new (script_lang_get_type (), NULL);
self->langname = g_strdup (langname);
self->script_index = script_index;
self->lang_index = lang_index;
self->lang_tag = lang_tag;
return self;
}
static char *
script_lang_get_langname (ScriptLang *self)
{
return g_strdup (self->langname);
}
/* }}} */
#define MAKE_TAG(a,b,c,d) (unsigned int)(((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
@@ -95,6 +163,10 @@ demo_free (gpointer data)
g_clear_pointer (&demo->axes, g_hash_table_unref);
g_clear_pointer (&demo->text, g_free);
gtk_style_context_remove_provider_for_display (gdk_display_get_default (),
GTK_STYLE_PROVIDER (demo->provider));
g_object_unref (demo->provider);
g_free (demo);
}
@@ -472,8 +544,6 @@ update_display (void)
GString *s;
char *text;
gboolean has_feature;
GtkTreeIter iter;
GtkTreeModel *model;
PangoFontDescription *desc;
GList *l;
PangoAttrList *attrs;
@@ -576,14 +646,13 @@ update_display (void)
features = g_string_free (s, FALSE);
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (demo->script_lang), &iter))
if (gtk_drop_down_get_selected (GTK_DROP_DOWN (demo->script_lang)) != 0)
{
hb_tag_t lang_tag;
ScriptLang *selected;
model = gtk_combo_box_get_model (GTK_COMBO_BOX (demo->script_lang));
gtk_tree_model_get (model, &iter, 3, &lang_tag, -1);
selected = gtk_drop_down_get_selected_item (GTK_DROP_DOWN (demo->script_lang));
lang = pango_language_from_string (hb_language_to_string (hb_ot_tag_to_language (lang_tag)));
lang = pango_language_from_string (hb_language_to_string (hb_ot_tag_to_language (selected->lang_tag)));
}
else
lang = NULL;
@@ -740,40 +809,30 @@ tag_pair_equal (gconstpointer a, gconstpointer b)
return pair_a->script_tag == pair_b->script_tag && pair_a->lang_tag == pair_b->lang_tag;
}
static int
script_sort_func (GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer user_data)
static GtkOrdering
script_sort (const void *item1,
const void *item2,
void *data)
{
char *sa, *sb;
int ret;
ScriptLang *a = (ScriptLang *)item1;
ScriptLang *b = (ScriptLang *)item2;
gtk_tree_model_get (model, a, 0, &sa, -1);
gtk_tree_model_get (model, b, 0, &sb, -1);
ret = strcmp (sa, sb);
g_free (sa);
g_free (sb);
return ret;
return strcmp (a->langname, b->langname);
}
static void
update_script_combo (void)
{
GtkListStore *store;
GListStore *store;
GtkSortListModel *sortmodel;
hb_font_t *hb_font;
int i, j, k;
PangoFont *pango_font;
GHashTable *tags;
GHashTableIter iter;
TagPair *pair;
char *lang;
hb_tag_t active;
GtkTreeIter active_iter;
gboolean have_active = FALSE;
lang = gtk_font_chooser_get_language (GTK_FONT_CHOOSER (demo->font));
@@ -783,7 +842,7 @@ update_script_combo (void)
g_free (lang);
store = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT);
store = g_list_store_new (script_lang_get_type ());
pango_font = get_pango_font ();
hb_font = pango_font_get_hb_font (pango_font);
@@ -807,19 +866,19 @@ update_script_combo (void)
hb_face = hb_font_get_face (hb_font);
for (i= 0; i < 2; i++)
for (guint i = 0; i < 2; i++)
{
hb_tag_t scripts[80];
unsigned int script_count = G_N_ELEMENTS (scripts);
hb_ot_layout_table_get_script_tags (hb_face, tables[i], 0, &script_count, scripts);
for (j = 0; j < script_count; j++)
for (guint j = 0; j < script_count; j++)
{
hb_tag_t languages[80];
unsigned int language_count = G_N_ELEMENTS (languages);
hb_ot_layout_script_get_language_tags (hb_face, tables[i], j, 0, &language_count, languages);
for (k = 0; k < language_count; k++)
for (guint k = 0; k < language_count; k++)
{
pair = g_new (TagPair, 1);
pair->script_tag = scripts[j];
@@ -839,7 +898,6 @@ update_script_combo (void)
{
const char *langname;
char langbuf[5];
GtkTreeIter tree_iter;
if (pair->lang_tag == 0 && pair->script_tag == 0)
langname = NC_("Language", "None");
@@ -856,31 +914,31 @@ update_script_combo (void)
}
}
gtk_list_store_insert_with_values (store, &tree_iter, -1,
0, langname,
1, pair->script_index,
2, pair->lang_index,
3, pair->lang_tag,
-1);
if (pair->lang_tag == active)
{
have_active = TRUE;
active_iter = tree_iter;
}
g_list_store_append (store, script_lang_new (langname,
pair->script_index,
pair->lang_index,
pair->lang_tag));
}
g_hash_table_destroy (tags);
gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
script_sort_func, NULL, NULL);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
GTK_SORT_ASCENDING);
gtk_combo_box_set_model (GTK_COMBO_BOX (demo->script_lang), GTK_TREE_MODEL (store));
if (have_active)
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (demo->script_lang), &active_iter);
else
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (demo->script_lang), 0);
sortmodel = gtk_sort_list_model_new (G_LIST_MODEL (store),
GTK_SORTER (gtk_custom_sorter_new (script_sort, NULL, NULL)));
gtk_drop_down_set_model (GTK_DROP_DOWN (demo->script_lang), G_LIST_MODEL (sortmodel));
for (guint i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (sortmodel)); i++)
{
ScriptLang *item = g_list_model_get_item (G_LIST_MODEL (sortmodel), i);
g_object_unref (item);
if (item->lang_tag == active)
{
gtk_drop_down_set_selected (GTK_DROP_DOWN (demo->script_lang), i);
break;
}
}
g_object_unref (sortmodel);
}
static char *
@@ -905,27 +963,19 @@ static void
update_features (void)
{
int i, j;
GtkTreeModel *model;
GtkTreeIter iter;
guint script_index, lang_index;
hb_tag_t lang_tag;
PangoFont *pango_font;
hb_font_t *hb_font;
GList *l;
ScriptLang *selected;
/* set feature presence checks from the font features */
if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (demo->script_lang), &iter))
if (gtk_drop_down_get_selected (GTK_DROP_DOWN (demo->script_lang)) == 0)
return;
model = gtk_combo_box_get_model (GTK_COMBO_BOX (demo->script_lang));
gtk_tree_model_get (model, &iter,
1, &script_index,
2, &lang_index,
3, &lang_tag,
-1);
selected = gtk_drop_down_get_selected_item (GTK_DROP_DOWN (demo->script_lang));
if (lang_tag == 0) /* None is selected */
if (selected->lang_tag == 0) /* None is selected */
{
for (l = demo->feature_items; l; l = l->next)
{
@@ -966,8 +1016,8 @@ update_features (void)
hb_ot_layout_language_get_feature_tags (hb_face,
tables[i],
script_index,
lang_index,
selected->script_index,
selected->lang_index,
0,
&count,
features);
@@ -989,8 +1039,8 @@ update_features (void)
hb_ot_layout_language_find_feature (hb_face,
tables[i],
script_index,
lang_index,
selected->script_index,
selected->lang_index,
features[j],
&feature_index);
@@ -1322,10 +1372,9 @@ free_instance (gpointer data)
}
static void
add_instance (hb_face_t *face,
unsigned int index,
GtkWidget *combo,
int pos)
add_instance (hb_face_t *face,
unsigned int index,
GtkStringList *strings)
{
Instance *instance;
hb_ot_name_id_t name_id;
@@ -1341,20 +1390,20 @@ add_instance (hb_face_t *face,
instance->index = index;
g_hash_table_add (demo->instances, instance);
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), instance->name);
gtk_string_list_append (GTK_STRING_LIST (strings), instance->name);
}
static void
unset_instance (GtkAdjustment *adjustment)
{
if (demo->instance_combo)
gtk_combo_box_set_active (GTK_COMBO_BOX (demo->instance_combo), 0);
gtk_drop_down_set_selected (GTK_DROP_DOWN (demo->instance_combo), 0);
}
static void
instance_changed (GtkComboBox *combo)
instance_changed (GtkDropDown *combo)
{
char *text;
const char *text;
Instance *instance;
Instance ikey;
int i;
@@ -1366,11 +1415,12 @@ instance_changed (GtkComboBox *combo)
hb_font_t *hb_font;
hb_face_t *hb_face;
text = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (combo));
text = gtk_string_list_get_string (GTK_STRING_LIST (gtk_drop_down_get_model (combo)),
gtk_drop_down_get_selected (combo));
if (text[0] == '\0')
goto out;
ikey.name = text;
ikey.name = (char *) text;
instance = g_hash_table_lookup (demo->instances, &ikey);
if (!instance)
{
@@ -1411,7 +1461,6 @@ instance_changed (GtkComboBox *combo)
}
out:
g_free (text);
g_clear_object (&pango_font);
g_free (ai);
g_free (coords);
@@ -1521,6 +1570,7 @@ update_font_variations (void)
{
GtkWidget *label;
GtkWidget *combo;
GtkStringList *strings;
label = gtk_label_new ("Instance");
gtk_label_set_xalign (GTK_LABEL (label), 0);
@@ -1528,26 +1578,28 @@ update_font_variations (void)
gtk_widget_set_valign (label, GTK_ALIGN_BASELINE);
gtk_grid_attach (GTK_GRID (demo->variations_grid), label, 0, -1, 1, 1);
combo = gtk_combo_box_text_new ();
strings = gtk_string_list_new (NULL);
combo = gtk_drop_down_new (G_LIST_MODEL (strings), NULL);
gtk_widget_set_halign (combo, GTK_ALIGN_START);
gtk_widget_set_valign (combo, GTK_ALIGN_BASELINE);
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "");
gtk_string_list_append (strings, "");
for (i = 0; i < hb_ot_var_get_named_instance_count (hb_face); i++)
add_instance (hb_face, i, combo, i);
add_instance (hb_face, i, strings);
for (i = 0; i < hb_ot_var_get_named_instance_count (hb_face); i++)
{
if (matches_instance (hb_face, i, n_axes, design_coords))
{
gtk_combo_box_set_active (GTK_COMBO_BOX (combo), i + 1);
gtk_drop_down_set_selected (GTK_DROP_DOWN (combo), i + 1);
break;
}
}
gtk_grid_attach (GTK_GRID (demo->variations_grid), combo, 1, -1, 3, 1);
g_signal_connect (combo, "changed", G_CALLBACK (instance_changed), NULL);
g_signal_connect (combo, "notify::selecte", G_CALLBACK (instance_changed), NULL);
demo->instance_combo = combo;
}
@@ -1694,6 +1746,7 @@ do_font_features (GtkWidget *do_widget)
GtkBuilder *builder;
GtkBuilderScope *scope;
GtkEventController *controller;
GtkExpression *expression;
builder = gtk_builder_new ();
@@ -1727,6 +1780,9 @@ do_font_features (GtkWidget *do_widget)
demo->description = GTK_WIDGET (gtk_builder_get_object (builder, "description"));
demo->font = GTK_WIDGET (gtk_builder_get_object (builder, "font"));
demo->script_lang = GTK_WIDGET (gtk_builder_get_object (builder, "script_lang"));
expression = gtk_cclosure_expression_new (G_TYPE_STRING, NULL, 0, NULL, G_CALLBACK (script_lang_get_langname), NULL, NULL);
gtk_drop_down_set_expression (GTK_DROP_DOWN (demo->script_lang), expression);
gtk_expression_unref (expression);
demo->feature_list = GTK_WIDGET (gtk_builder_get_object (builder, "feature_list"));
demo->stack = GTK_WIDGET (gtk_builder_get_object (builder, "stack"));
demo->entry = GTK_WIDGET (gtk_builder_get_object (builder, "entry"));
@@ -1745,8 +1801,8 @@ do_font_features (GtkWidget *do_widget)
demo->swin = GTK_WIDGET (gtk_builder_get_object (builder, "swin"));
demo->provider = gtk_css_provider_new ();
gtk_style_context_add_provider (gtk_widget_get_style_context (demo->swin),
GTK_STYLE_PROVIDER (demo->provider), 800);
gtk_style_context_add_provider_for_display (gdk_display_get_default (),
GTK_STYLE_PROVIDER (demo->provider), 800);
basic_value_changed (demo->size_adjustment, demo->size_entry);
basic_value_changed (demo->letterspacing_adjustment, demo->letterspacing_entry);
@@ -1824,3 +1880,5 @@ do_font_features (GtkWidget *do_widget)
return window;
}
/* vim:set foldmethod=marker expandtab: */

View File

@@ -259,16 +259,10 @@
<object class="GtkBox" id="feature_list">
<property name="orientation">vertical</property>
<child>
<object class="GtkComboBox" id="script_lang">
<object class="GtkDropDown" id="script_lang">
<property name="tooltip-text" translatable="yes">Language System</property>
<property name="margin-top">10</property>
<signal name="changed" handler="font_features_script_changed" swapped="no"/>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
<signal name="notify::selected" handler="font_features_script_changed" swapped="no"/>
</object>
</child>
</object>

View File

@@ -360,7 +360,7 @@ combo_changed (GtkDropDown *combo,
char **accels;
char *str;
action = gtk_drop_down_get_selected_string (combo);
action = gtk_string_object_get_string (GTK_STRING_OBJECT (gtk_drop_down_get_selected_item (combo)));
if (!action)
return;
@@ -400,7 +400,7 @@ response (GtkDialog *dialog,
return;
}
action = gtk_drop_down_get_selected_string (combo);
action = gtk_string_object_get_string (GTK_STRING_OBJECT (gtk_drop_down_get_selected_item (combo)));
if (!action)
return;

View File

@@ -83,6 +83,32 @@ void gtk_color_chooser_add_palette (GtkColorChooser *chooser,
int n_colors,
GdkRGBA *colors);
typedef void (*GtkColorChooserPrepareCallback) (GtkColorChooser *chooser,
gpointer user_data);
GDK_AVAILABLE_IN_ALL
void gtk_choose_color (GtkWindow *parent,
const char *title,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
GDK_AVAILABLE_IN_ALL
void gtk_choose_color_full (GtkWindow *parent,
const char *title,
GtkColorChooserPrepareCallback prepare,
gpointer prepare_data,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
GDK_AVAILABLE_IN_ALL
gboolean gtk_choose_color_finish (GtkColorChooser *chooser,
GAsyncResult *result,
GdkRGBA *color,
GError **error);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkColorChooser, g_object_unref)
G_END_DECLS

439
gtk/gtkcolorchooserwindow.c Normal file
View File

@@ -0,0 +1,439 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2012 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtkwindow.h"
#include "gtkwindowprivate.h"
#include "gtkbutton.h"
#include "gtkbox.h"
#include "gtkprivate.h"
#include "gtksettings.h"
#include "gtkcolorchooserprivate.h"
#include "gtkcolorchooserwindowprivate.h"
#include "gtkcolorchooserwidget.h"
/*
* GtkColorChooserWindow:
*
* A window for choosing a color.
*
* ![An example GtkColorChooserWindow](colorchooser.png)
*
* `GtkColorChooserWindow` implements the [iface@Gtk.ColorChooser] interface
* and does not provide much API of its own.
*
* To create a `GtkColorChooserWindow`, use [ctor@Gtk.ColorChooserWindow.new].
*
* To change the initially selected color, use
* [method@Gtk.ColorChooser.set_rgba]. To get the selected color use
* [method@Gtk.ColorChooser.get_rgba].
*/
typedef struct _GtkColorChooserWindowClass GtkColorChooserWindowClass;
struct _GtkColorChooserWindow
{
GtkWindow parent_instance;
GtkWidget *chooser;
};
struct _GtkColorChooserWindowClass
{
GtkWindowClass parent_class;
};
enum
{
PROP_ZERO,
PROP_RGBA,
PROP_USE_ALPHA,
PROP_SHOW_EDITOR
};
static void gtk_color_chooser_window_iface_init (GtkColorChooserInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkColorChooserWindow, gtk_color_chooser_window, GTK_TYPE_WINDOW,
G_IMPLEMENT_INTERFACE (GTK_TYPE_COLOR_CHOOSER,
gtk_color_chooser_window_iface_init))
static void
propagate_notify (GObject *o,
GParamSpec *pspec,
GtkColorChooserWindow *cc)
{
g_object_notify (G_OBJECT (cc), pspec->name);
}
static void
save_color (GtkColorChooserWindow *window)
{
GdkRGBA color;
/* This causes the color chooser widget to save the
* selected and custom colors to GSettings.
*/
gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (window), &color);
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (window), &color);
}
enum
{
RESPONSE_OK,
RESPONSE_CANCEL
};
static void
response_cb (GtkWindow *window,
int response);
static void
color_activated_cb (GtkColorChooser *chooser,
GdkRGBA *color,
GtkWindow *window)
{
save_color (GTK_COLOR_CHOOSER_WINDOW (window));
response_cb (GTK_WINDOW (window), RESPONSE_OK);
}
static void
ok_button_cb (GtkButton *button,
GtkWindow *window)
{
response_cb (window, RESPONSE_OK);
}
static void
cancel_button_cb (GtkButton *button,
GtkWindow *window)
{
response_cb (window, RESPONSE_CANCEL);
}
static void
gtk_color_chooser_window_init (GtkColorChooserWindow *cc)
{
gtk_widget_init_template (GTK_WIDGET (cc));
}
static void
gtk_color_chooser_window_unmap (GtkWidget *widget)
{
GTK_WIDGET_CLASS (gtk_color_chooser_window_parent_class)->unmap (widget);
/* We never want the window to come up with the editor,
* even if it was showing the editor the last time it was used.
*/
g_object_set (widget, "show-editor", FALSE, NULL);
}
static void
gtk_color_chooser_window_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkColorChooserWindow *cc = GTK_COLOR_CHOOSER_WINDOW (object);
switch (prop_id)
{
case PROP_RGBA:
{
GdkRGBA color;
gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (cc), &color);
g_value_set_boxed (value, &color);
}
break;
case PROP_USE_ALPHA:
g_value_set_boolean (value, gtk_color_chooser_get_use_alpha (GTK_COLOR_CHOOSER (cc->chooser)));
break;
case PROP_SHOW_EDITOR:
{
gboolean show_editor;
g_object_get (cc->chooser, "show-editor", &show_editor, NULL);
g_value_set_boolean (value, show_editor);
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_color_chooser_window_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkColorChooserWindow *cc = GTK_COLOR_CHOOSER_WINDOW (object);
switch (prop_id)
{
case PROP_RGBA:
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (cc), g_value_get_boxed (value));
break;
case PROP_USE_ALPHA:
if (gtk_color_chooser_get_use_alpha (GTK_COLOR_CHOOSER (cc->chooser)) != g_value_get_boolean (value))
{
gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (cc->chooser), g_value_get_boolean (value));
g_object_notify_by_pspec (object, pspec);
}
break;
case PROP_SHOW_EDITOR:
g_object_set (cc->chooser,
"show-editor", g_value_get_boolean (value),
NULL);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_color_chooser_window_dispose (GObject *object)
{
GtkColorChooserWindow *cc = GTK_COLOR_CHOOSER_WINDOW (object);
g_clear_pointer (&cc->chooser, gtk_widget_unparent);
G_OBJECT_CLASS (gtk_color_chooser_window_parent_class)->dispose (object);
}
static void
gtk_color_chooser_window_class_init (GtkColorChooserWindowClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
object_class->dispose = gtk_color_chooser_window_dispose;
object_class->get_property = gtk_color_chooser_window_get_property;
object_class->set_property = gtk_color_chooser_window_set_property;
widget_class->unmap = gtk_color_chooser_window_unmap;
g_object_class_override_property (object_class, PROP_RGBA, "rgba");
g_object_class_override_property (object_class, PROP_USE_ALPHA, "use-alpha");
g_object_class_install_property (object_class, PROP_SHOW_EDITOR,
g_param_spec_boolean ("show-editor", NULL, NULL,
FALSE, GTK_PARAM_READWRITE));
/* Bind class to template
*/
gtk_widget_class_set_template_from_resource (widget_class,
"/org/gtk/libgtk/ui/gtkcolorchooserwindow.ui");
gtk_widget_class_bind_template_child (widget_class, GtkColorChooserWindow, chooser);
gtk_widget_class_bind_template_callback (widget_class, propagate_notify);
gtk_widget_class_bind_template_callback (widget_class, color_activated_cb);
gtk_widget_class_bind_template_callback (widget_class, ok_button_cb);
gtk_widget_class_bind_template_callback (widget_class, cancel_button_cb);
}
static void
gtk_color_chooser_window_get_rgba (GtkColorChooser *chooser,
GdkRGBA *color)
{
GtkColorChooserWindow *cc = GTK_COLOR_CHOOSER_WINDOW (chooser);
gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (cc->chooser), color);
}
static void
gtk_color_chooser_window_set_rgba (GtkColorChooser *chooser,
const GdkRGBA *color)
{
GtkColorChooserWindow *cc = GTK_COLOR_CHOOSER_WINDOW (chooser);
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (cc->chooser), color);
}
static void
gtk_color_chooser_window_add_palette (GtkColorChooser *chooser,
GtkOrientation orientation,
int colors_per_line,
int n_colors,
GdkRGBA *colors)
{
GtkColorChooserWindow *cc = GTK_COLOR_CHOOSER_WINDOW (chooser);
gtk_color_chooser_add_palette (GTK_COLOR_CHOOSER (cc->chooser),
orientation, colors_per_line, n_colors, colors);
}
static void
gtk_color_chooser_window_iface_init (GtkColorChooserInterface *iface)
{
iface->get_rgba = gtk_color_chooser_window_get_rgba;
iface->set_rgba = gtk_color_chooser_window_set_rgba;
iface->add_palette = gtk_color_chooser_window_add_palette;
}
/*
* gtk_color_chooser_window_new:
* @title: (nullable): Title of the window
* @parent: (nullable): Transient parent of the window
*
* Creates a new `GtkColorChooserWindow`.
*
* Returns: a new `GtkColorChooserWindow`
*/
GtkWidget *
gtk_color_chooser_window_new (const char *title,
GtkWindow *parent)
{
return g_object_new (GTK_TYPE_COLOR_CHOOSER_WINDOW,
"title", title,
"transient-for", parent,
NULL);
}
static void
cancelled_cb (GCancellable *cancellable,
GtkWindow *window)
{
response_cb (window, RESPONSE_CANCEL);
}
static void
response_cb (GtkWindow *window,
int response)
{
GTask *task = G_TASK (g_object_get_data (G_OBJECT (window), "task"));
GCancellable *cancellable = g_task_get_cancellable (task);
if (cancellable)
g_signal_handlers_disconnect_by_func (cancellable, cancelled_cb, window);
if (response == RESPONSE_OK)
{
save_color (GTK_COLOR_CHOOSER_WINDOW (window));
g_task_return_boolean (task, TRUE);
}
else
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled");
g_object_unref (task);
gtk_window_destroy (GTK_WINDOW (window));
}
/**
* gtk_choose_color:
* @parent: (nullable): parent window
* @title: title for the color chooser
* @cancellable: (nullable): a `GCancellable` to cancel the operation
* @callback: (scope async): callback to call when the action is complete
* @user_data: (closure callback): data to pass to @callback
*
* This function presents a color chooser to let the user
* pick a color.
*
* The @callback will be called when the window is closed.
* It should call [function@Gtk.choose_color_finish] to
* find out whether the operation was completed successfully,
* and to obtain the resulting color.
*/
void
gtk_choose_color (GtkWindow *parent,
const char *title,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
gtk_choose_color_full (parent, title, NULL, NULL, cancellable, callback, user_data);
}
/**
* gtk_choose_color_full:
* @parent: (nullable): parent window
* @title: title for the color chooser
* @prepare: (nullable) (scope call): callback to set up the color chooser
* @prepare_data: (closure prepare): data to pass to @prepare
* @cancellable: (nullable): a `GCancellable` to cancel the operation
* @callback: (scope async): callback to call when the action is complete
* @user_data: (closure callback): data to pass to @callback
*
* This function presents a color chooser to let the user
* pick a color.
*
* In addition to [function@Gtk.choose_color], this function takes
* a @prepare callback that lets you set up the color chooser according
* to your needs.
*
* The @callback will be called when the window is closed.
* It should call [function@Gtk.choose_color_finish] to
* find out whether the operation was completed successfully,
* and to obtain the resulting color.
*/
void
gtk_choose_color_full (GtkWindow *parent,
const char *title,
GtkColorChooserPrepareCallback prepare,
gpointer prepare_data,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GtkWidget *window;
GTask *task;
window = gtk_color_chooser_window_new (title, parent);
if (prepare)
prepare (GTK_COLOR_CHOOSER (window), prepare);
if (cancellable)
g_signal_connect (cancellable, "cancelled", G_CALLBACK (cancelled_cb), window);
task = g_task_new (window, cancellable, callback, user_data);
g_task_set_source_tag (task, gtk_choose_color_full);
g_object_set_data (G_OBJECT (window), "task", task);
gtk_window_present (GTK_WINDOW (window));
}
/**
* gtk_choose_color_finish:
* @chooser: the `GtkColorChooser`
* @result: `GAsyncResult` that was passed to @callback
* @color: return location for the color
* @error: return location for an error
*
* Finishes a gtk_choose_color() or gtk_choose_color_full() call
* and returns the results.
*
* If this function returns `TRUE`, @color contains
* the color that was chosen.
*
* Returns: `TRUE` if the operation was successful
*/
gboolean
gtk_choose_color_finish (GtkColorChooser *chooser,
GAsyncResult *result,
GdkRGBA *color,
GError **error)
{
if (!g_task_propagate_boolean (G_TASK (result), error))
return FALSE;
gtk_color_chooser_get_rgba (chooser, color);
return TRUE;
}

View File

@@ -0,0 +1,46 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2012 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GTK_COLOR_CHOOSER_WINDOW_PRIVATE_H___
#define __GTK_COLOR_CHOOSER_WINDOW_PRIVATE_H__
#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#include <gtk/gtkwindow.h>
G_BEGIN_DECLS
#define GTK_TYPE_COLOR_CHOOSER_WINDOW (gtk_color_chooser_window_get_type ())
#define GTK_COLOR_CHOOSER_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_COLOR_CHOOSER_WINDOW, GtkColorChooserWindow))
#define GTK_IS_COLOR_CHOOSER_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_COLOR_CHOOSER_WINDOW))
typedef struct _GtkColorChooserWindow GtkColorChooserWindow;
GDK_AVAILABLE_IN_ALL
GType gtk_color_chooser_window_get_type (void) G_GNUC_CONST;
GDK_AVAILABLE_IN_ALL
GtkWidget * gtk_color_chooser_window_new (const char *title,
GtkWindow *parent);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkColorChooserWindow, g_object_unref)
G_END_DECLS
#endif /* __GTK_COLOR_CHOOSER_WINDOW_PRIVATE_H__ */

View File

@@ -133,7 +133,6 @@ enum
PROP_MODEL,
PROP_SELECTED,
PROP_SELECTED_ITEM,
PROP_SELECTED_STRING,
PROP_ENABLE_SEARCH,
PROP_EXPRESSION,
PROP_SHOW_ARROW,
@@ -248,7 +247,6 @@ selection_item_changed (GtkSingleSelection *selection,
}
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED_ITEM]);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SELECTED_STRING]);
}
static void
@@ -360,10 +358,6 @@ gtk_drop_down_get_property (GObject *object,
g_value_set_object (value, gtk_drop_down_get_selected_item (self));
break;
case PROP_SELECTED_STRING:
g_value_set_string (value, gtk_drop_down_get_selected_string (self));
break;
case PROP_ENABLE_SEARCH:
g_value_set_boolean (value, self->enable_search);
break;
@@ -408,10 +402,6 @@ gtk_drop_down_set_property (GObject *object,
gtk_drop_down_set_selected (self, g_value_get_uint (value));
break;
case PROP_SELECTED_STRING:
gtk_drop_down_set_selected_string (self, g_value_get_string (value));
break;
case PROP_ENABLE_SEARCH:
gtk_drop_down_set_enable_search (self, g_value_get_boolean (value));
break;
@@ -555,22 +545,6 @@ gtk_drop_down_class_init (GtkDropDownClass *klass)
G_TYPE_OBJECT,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
/**
* GtkDropDown:selected-string: (attributes org.gtk.Property.get=gtk_drop_down_get_selected_string org.gtk.Property.set=gtk_drop_down_set_selected_string)
*
* The value of the string property of the selected item,
* if it is a [class@Gtk.StringObject].
*
* This is only useful for dropdowns with a [class@Gtk.StringList] as model,
* such as those created by [ctor@Gtk.DropDown.new_from_strings].
*
* Since: 4.10
*/
properties[PROP_SELECTED_STRING] =
g_param_spec_string ("selected-string", NULL, NULL,
NULL,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
/**
* GtkDropDown:enable-search: (attributes org.gtk.Property.get=gtk_drop_down_get_enable_search org.gtk.Property.set=gtk_drop_down_set_enable_search)
*
@@ -810,7 +784,8 @@ gtk_drop_down_new (GListModel *model,
* gtk_drop_down_new_from_strings:
* @strings: (array zero-terminated=1): The strings to put in the dropdown
*
* Creates a new `GtkDropDown` that is populated with the strings.
* Creates a new `GtkDropDown` that is populated with
* the strings.
*
* Returns: a new `GtkDropDown`
*/
@@ -1041,81 +1016,6 @@ gtk_drop_down_get_selected_item (GtkDropDown *self)
return gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (self->selection));
}
/**
* gtk_drop_down_get_selected_string: (attributes org.gtk.Method.get_property=selected-string)
* @self: a `GtkDropDown`
*
* Gets the string value for the selected [class@Gtk.StringObject].
*
* If no item is selected, or items are of another type, %NULL is returned.
*
* This function is meant for dropdowns with a [class@Gtk.StringList] as model,
* such as those created by [ctor@Gtk.DropDown.new_from_strings].
*
* Returns: (transfer none) (nullable): The string value for selected item
*
* Since: 4.10
*/
const char *
gtk_drop_down_get_selected_string (GtkDropDown *self)
{
gpointer item;
g_return_val_if_fail (GTK_IS_DROP_DOWN (self), NULL);
if (self->selection == NULL)
return NULL;
item = gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (self->selection));
if (GTK_IS_STRING_OBJECT (item))
return gtk_string_object_get_string (GTK_STRING_OBJECT (item));
return NULL;
}
/**
* gtk_drop_down_set_selected_string:
* @self: a `GtkDropDown`
* @string: the string to select
*
* Selects the first [class@Gtk.StringObject] whose string property
* matches @string.
*
* If items are not `GtkStringObjects`, the selection is not changed.
*
* This function is meant for dropdowns with a [class@Gtk.StringList] as model,
* such as those created by [ctor@Gtk.DropDown.new_from_strings].
*
* Since: 4.10
*/
void
gtk_drop_down_set_selected_string (GtkDropDown *self,
const char *string)
{
g_return_if_fail (GTK_IS_DROP_DOWN (self));
g_return_if_fail (string != NULL);
if (self->selection == NULL)
return;
for (guint i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (self->selection)); i++)
{
gpointer item = g_list_model_get_item (G_LIST_MODEL (self->selection), i);
g_object_unref (item);
if (!GTK_IS_STRING_OBJECT (item))
break;
if (g_str_equal (gtk_string_object_get_string (GTK_STRING_OBJECT (item)), string))
{
gtk_single_selection_set_selected (GTK_SINGLE_SELECTION (self->selection), i);
break;
}
}
}
/**
* gtk_drop_down_set_enable_search: (attributes org.gtk.Method.set_property=enable-search)
* @self: a `GtkDropDown`

View File

@@ -52,13 +52,6 @@ guint gtk_drop_down_get_selected (GtkDropDown
GDK_AVAILABLE_IN_ALL
gpointer gtk_drop_down_get_selected_item (GtkDropDown *self);
GDK_AVAILABLE_IN_4_10
const char * gtk_drop_down_get_selected_string (GtkDropDown *self);
GDK_AVAILABLE_IN_4_10
void gtk_drop_down_set_selected_string (GtkDropDown *self,
const char *string);
GDK_AVAILABLE_IN_ALL
void gtk_drop_down_set_factory (GtkDropDown *self,
GtkListItemFactory *factory);

View File

@@ -183,6 +183,34 @@ GDK_AVAILABLE_IN_ALL
const char * gtk_file_chooser_get_choice (GtkFileChooser *chooser,
const char *id);
typedef void (*GtkFileChooserPrepareCallback) (GtkFileChooser *chooser,
gpointer user_data);
GDK_AVAILABLE_IN_ALL
void gtk_choose_file (GtkWindow *parent,
const char *title,
GtkFileChooserAction action,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
GDK_AVAILABLE_IN_ALL
void gtk_choose_file_full (GtkWindow *parent,
const char *title,
GtkFileChooserAction action,
GtkFileChooserPrepareCallback prepare,
gpointer prepare_data,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
GDK_AVAILABLE_IN_ALL
gboolean gtk_choose_file_finish (GtkFileChooser *chooser,
GAsyncResult *result,
GError **error);
G_END_DECLS
#endif /* __GTK_FILE_CHOOSER_H__ */

View File

@@ -735,3 +735,140 @@ gtk_file_chooser_dialog_new (const char *title,
return result;
}
static void
cancelled_cb (GCancellable *cancellable,
GtkDialog *dialog)
{
gtk_dialog_response (dialog, GTK_RESPONSE_CANCEL);
}
static void
choose_response_cb (GtkDialog *dialog,
int response,
GTask *task)
{
GCancellable *cancellable = g_task_get_cancellable (task);
if (cancellable)
g_signal_handlers_disconnect_by_func (cancellable, cancelled_cb, dialog);
if (response == GTK_RESPONSE_OK)
g_task_return_boolean (task, TRUE);
else
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled");
g_object_unref (task);
gtk_window_destroy (GTK_WINDOW (dialog));
}
/**
* gtk_choose_file:
* @parent: (nullable): parent window
* @title: title for the font chooser
* @action: the action for the file chooser
* @cancellable: (nullable): a `GCancellable` to cancel the operation
* @callback: (scope async): callback to call when the action is complete
* @user_data: (closure callback): data to pass to @callback
*
* This function presents a file chooser to let the user
* pick a file.
*
* The @callback will be called when the dialog is closed.
* It should call [function@Gtk.choose_file_finish] to
* find out whether the operation was completed successfully,
* and use [class@Gtk.FileChooser] API to obtain the results.
*/
void
gtk_choose_file (GtkWindow *parent,
const char *title,
GtkFileChooserAction action,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
gtk_choose_file_full (parent, title, action, NULL, NULL, cancellable, callback, user_data);
}
/**
* gtk_choose_file_full:
* @parent: (nullable): parent window
* @title: title for the file chooser
* @action: the action for the file chooser
* @prepare: (nullable) (scope call): callback to set up the file chooser
* @prepare_data: (closure prepare): data to pass to @prepare
* @cancellable: (nullable): a `GCancellable` to cancel the operation
* @callback: (scope async): callback to call when the action is complete
* @user_data: (closure callback): data to pass to @callback
*
* This function presents a file chooser to let the user
* choose a file.
*
* In addition to [function@Gtk.choose_file], this function takes
* a @prepare callback that lets you set up the file chooser according
* to your needs.
*
* The @callback will be called when the dialog is closed.
* It should use [function@Gtk.choose_file_finish] to find
* out whether the operation was completed successfully,
* and use [class@Gtk.FileChooser] API to obtain the results.
*/
void
gtk_choose_file_full (GtkWindow *parent,
const char *title,
GtkFileChooserAction action,
GtkFileChooserPrepareCallback prepare,
gpointer prepare_data,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GtkWidget *dialog;
GTask *task;
const char *button[] = {
N_("_Open"),
N_("_Save"),
N_("_Select")
};
dialog = gtk_file_chooser_dialog_new (title, parent, action,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_(button[action]), GTK_RESPONSE_OK,
NULL);
if (prepare)
prepare (GTK_FILE_CHOOSER (dialog), prepare);
if (cancellable)
g_signal_connect (cancellable, "cancelled", G_CALLBACK (cancelled_cb), dialog);
task = g_task_new (dialog, cancellable, callback, user_data);
g_task_set_source_tag (task, gtk_choose_file_full);
g_signal_connect (dialog, "response", G_CALLBACK (choose_response_cb), task);
gtk_window_present (GTK_WINDOW (dialog));
}
/**
* gtk_choose_file_finish:
* @chooser: the `GtkFileChooser`
* @result: `GAsyncResult` that was passed to @callback
* @error: return location for an error
*
* Finishes a gtk_choose_file() or gtk_choose_file_full() call
* and returns whether the operation was successful.
*
* If this function returns `TRUE`, you can use
* [class@Gtk.FileChooser] API to get the results.
*
* Returns: `TRUE` if the operation was successful
*/
gboolean
gtk_choose_file_finish (GtkFileChooser *chooser,
GAsyncResult *result,
GError **error)
{
return g_task_propagate_boolean (G_TASK (result), error);
}

View File

@@ -7338,6 +7338,8 @@ gtk_file_chooser_widget_add_choice (GtkFileChooser *chooser,
gtk_box_append (GTK_BOX (box), gtk_label_new (label));
combo = gtk_drop_down_new_from_strings ((const char * const *)option_labels);
g_object_set_data_full (G_OBJECT (combo), "options",
g_strdupv ((char **)options), (GDestroyNotify)g_strfreev);
g_hash_table_insert (impl->choices, g_strdup (id), combo);
gtk_box_append (GTK_BOX (box), combo);
@@ -7394,8 +7396,21 @@ gtk_file_chooser_widget_set_choice (GtkFileChooser *chooser,
if (GTK_IS_BOX (widget))
{
GtkWidget *dropdown = gtk_widget_get_last_child (widget);
gtk_drop_down_set_selected_string (GTK_DROP_DOWN (dropdown), option);
guint i;
const char **options;
GtkWidget *dropdown;
dropdown = gtk_widget_get_last_child (widget);
options = (const char **) g_object_get_data (G_OBJECT (dropdown), "options");
for (i = 0; options[i]; i++)
{
if (strcmp (option, options[i]) == 0)
{
gtk_drop_down_set_selected (GTK_DROP_DOWN (dropdown), i);
break;
}
}
}
else if (GTK_IS_CHECK_BUTTON (widget))
gtk_check_button_set_active (GTK_CHECK_BUTTON (widget), g_str_equal (option, "true"));
@@ -7413,9 +7428,19 @@ gtk_file_chooser_widget_get_choice (GtkFileChooser *chooser,
widget = (GtkWidget *)g_hash_table_lookup (impl->choices, id);
if (GTK_IS_DROP_DOWN (widget))
return gtk_drop_down_get_selected_string (GTK_DROP_DOWN (widget));
{
const char **options;
guint selected;
options = (const char **) g_object_get_data (G_OBJECT (widget), "options");
selected = gtk_drop_down_get_selected (GTK_DROP_DOWN (widget));
return options[selected];
}
else if (GTK_IS_CHECK_BUTTON (widget))
return gtk_check_button_get_active (GTK_CHECK_BUTTON (widget)) ? "true" : "false";
{
return gtk_check_button_get_active (GTK_CHECK_BUTTON (widget)) ? "true" : "false";
}
return NULL;
}

View File

@@ -161,6 +161,31 @@ GDK_AVAILABLE_IN_ALL
void gtk_font_chooser_set_language (GtkFontChooser *fontchooser,
const char *language);
typedef void (*GtkFontChooserPrepareCallback) (GtkFontChooser *chooser,
gpointer user_data);
GDK_AVAILABLE_IN_ALL
void gtk_choose_font (GtkWindow *parent,
const char *title,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
GDK_AVAILABLE_IN_ALL
void gtk_choose_font_full (GtkWindow *parent,
const char *title,
GtkFontChooserPrepareCallback prepare,
gpointer prepare_data,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
GDK_AVAILABLE_IN_ALL
gboolean gtk_choose_font_finish (GtkFontChooser *chooser,
GAsyncResult *result,
GError **error);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkFontChooser, g_object_unref)
G_END_DECLS

View File

@@ -324,3 +324,127 @@ gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
return parent_buildable_iface->get_internal_child (buildable, builder, childname);
}
static void
cancelled_cb (GCancellable *cancellable,
GtkDialog *dialog)
{
gtk_dialog_response (dialog, GTK_RESPONSE_CANCEL);
}
static void
response_cb (GtkDialog *dialog,
int response,
GTask *task)
{
GCancellable *cancellable = g_task_get_cancellable (task);
if (cancellable)
g_signal_handlers_disconnect_by_func (cancellable, cancelled_cb, dialog);
if (response == GTK_RESPONSE_OK)
g_task_return_boolean (task, TRUE);
else
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled");
g_object_unref (task);
gtk_window_destroy (GTK_WINDOW (dialog));
}
/**
* gtk_choose_font:
* @parent: (nullable): parent window
* @title: title for the font chooser
* @cancellable: (nullable): a `GCancellable` to cancel the operation
* @callback: (scope async): callback to call when the action is complete
* @user_data: (closure callback): data to pass to @callback
*
* This function presents a font chooser to let the user
* pick a font.
*
* The @callback will be called when the dialog is closed.
* It should call [function@Gtk.choose_font_finish] to
* find out whether the operation was completed successfully,
* and use [class@Gtk.FontChooser] API to obtain the results.
*/
void
gtk_choose_font (GtkWindow *parent,
const char *title,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
gtk_choose_font_full (parent, title, NULL, NULL, cancellable, callback, user_data);
}
/**
* gtk_choose_font_full:
* @parent: (nullable): parent window
* @title: title for the font chooser
* @prepare: (nullable) (scope call): callback to set up the font chooser
* @prepare_data: (closure prepare): data to pass to @prepare
* @cancellable: (nullable): a `GCancellable` to cancel the operation
* @callback: (scope async): callback to call when the action is complete
* @user_data: (closure callback): data to pass to @callback
*
* This function presents a font chooser to let the user
* choose a font.
*
* In addition to [function@Gtk.choose_font], this function takes
* a @prepare callback that lets you set up the font chooser according
* to your needs.
*
* The @callback will be called when the dialog is closed.
* It should use [function@Gtk.choose_font_finish] to find
* out whether the operation was completed successfully,
* and use [class@Gtk.FontChooser] API to obtain the results.
*/
void
gtk_choose_font_full (GtkWindow *parent,
const char *title,
GtkFontChooserPrepareCallback prepare,
gpointer prepare_data,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GtkWidget *dialog;
GTask *task;
dialog = gtk_font_chooser_dialog_new (title, parent);
if (prepare)
prepare (GTK_FONT_CHOOSER (dialog), prepare);
if (cancellable)
g_signal_connect (cancellable, "cancelled", G_CALLBACK (cancelled_cb), dialog);
task = g_task_new (dialog, cancellable, callback, user_data);
g_task_set_source_tag (task, gtk_choose_font_full);
g_signal_connect (dialog, "response", G_CALLBACK (response_cb), task);
gtk_window_present (GTK_WINDOW (dialog));
}
/**
* gtk_choose_font_finish:
* @chooser: the `GtkFontChooser`
* @result: `GAsyncResult` that was passed to @callback
* @error: return location for an error
*
* Finishes a gtk_choose_font() or gtk_choose_font_full() call
* and returns whether the operation was successful.
*
* If this function returns `TRUE`, you can use
* [class@Gtk.FontChooser] API to get the results.
*
* Returns: `TRUE` if the operation was successful
*/
gboolean
gtk_choose_font_finish (GtkFontChooser *chooser,
GAsyncResult *result,
GError **error)
{
return g_task_propagate_boolean (G_TASK (result), error);
}

View File

@@ -186,6 +186,7 @@ gtk_public_sources = files([
'gtkcolorchooser.c',
'gtkcolorchooserdialog.c',
'gtkcolorchooserwidget.c',
'gtkcolorchooserwindow.c',
'gtkcolorutils.c',
'gtkcolumnview.c',
'gtkcolumnviewcolumn.c',

View File

@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface domain="gtk40">
<template class="GtkColorChooserWindow" parent="GtkWindow">
<property name="title" translatable="yes">Select a Color</property>
<property name="resizable">0</property>
<property name="default-widget">ok_button</property>
<child type="titlebar">
<object class="GtkHeaderBar">
<property name="show-title-buttons">0</property>
<child type="start">
<object class="GtkButton" id="cancel_button">
<property name="use-underline">1</property>
<property name="label" translatable="yes">_Cancel</property>
<signal name="clicked" handler="cancel_button_cb"/>
</object>
</child>
<child type="end">
<object class="GtkButton" id="ok_button">
<property name="label" translatable="yes">_Select</property>
<property name="use-underline">1</property>
<signal name="clicked" handler="ok_button_cb"/>
<style>
<class name="suggested-action"/>
</style>
</object>
</child>
</object>
</child>
<child>
<object class="GtkBox">
<property name="orientation">1</property>
<property name="spacing">2</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<child>
<object class="GtkColorChooserWidget" id="chooser">
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="rgba">rgb(255,255,255)</property>
<property name="hexpand">1</property>
<property name="vexpand">1</property>
<signal name="color-activated" handler="color_activated_cb" swapped="no"/>
<signal name="notify::rgba" handler="propagate_notify" swapped="no"/>
<signal name="notify::show-editor" handler="propagate_notify" swapped="no"/>
</object>
</child>
</object>
</child>
</template>
</interface>