Compare commits

..

3 Commits

Author SHA1 Message Date
Matthias Clasen
9dfa663990 filechooser: Use dropdown convenience api
No need to do things the hard way.
2022-10-22 08:43:05 -04:00
Matthias Clasen
831b1e1cde examples: Use the new convenience API 2022-10-22 08:43:05 -04:00
Matthias Clasen
801a261250 dropdown: Add some convenience api
Add a selected-string property to GtkDropDown, which
holds the value of the string property of the selected
item, if it is a GtkStringObject.

This brings gtk_drop_down_new_from_strings()
on par with GtkComboBoxText, in terms of convenience.
2022-10-22 08:43:05 -04:00
15 changed files with 211 additions and 1268 deletions

View File

@@ -21,75 +21,7 @@
#include "script-names.h"
#include "language-names.h"
/* {{{ 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);
}
/* }}} */
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
#define MAKE_TAG(a,b,c,d) (unsigned int)(((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
@@ -163,10 +95,6 @@ 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);
}
@@ -544,6 +472,8 @@ update_display (void)
GString *s;
char *text;
gboolean has_feature;
GtkTreeIter iter;
GtkTreeModel *model;
PangoFontDescription *desc;
GList *l;
PangoAttrList *attrs;
@@ -646,13 +576,14 @@ update_display (void)
features = g_string_free (s, FALSE);
if (gtk_drop_down_get_selected (GTK_DROP_DOWN (demo->script_lang)) != 0)
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (demo->script_lang), &iter))
{
ScriptLang *selected;
hb_tag_t lang_tag;
selected = gtk_drop_down_get_selected_item (GTK_DROP_DOWN (demo->script_lang));
model = gtk_combo_box_get_model (GTK_COMBO_BOX (demo->script_lang));
gtk_tree_model_get (model, &iter, 3, &lang_tag, -1);
lang = pango_language_from_string (hb_language_to_string (hb_ot_tag_to_language (selected->lang_tag)));
lang = pango_language_from_string (hb_language_to_string (hb_ot_tag_to_language (lang_tag)));
}
else
lang = NULL;
@@ -809,30 +740,40 @@ 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 GtkOrdering
script_sort (const void *item1,
const void *item2,
void *data)
static int
script_sort_func (GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer user_data)
{
ScriptLang *a = (ScriptLang *)item1;
ScriptLang *b = (ScriptLang *)item2;
char *sa, *sb;
int ret;
return strcmp (a->langname, b->langname);
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;
}
static void
update_script_combo (void)
{
GListStore *store;
GtkSortListModel *sortmodel;
GtkListStore *store;
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));
@@ -842,7 +783,7 @@ update_script_combo (void)
g_free (lang);
store = g_list_store_new (script_lang_get_type ());
store = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT);
pango_font = get_pango_font ();
hb_font = pango_font_get_hb_font (pango_font);
@@ -866,19 +807,19 @@ update_script_combo (void)
hb_face = hb_font_get_face (hb_font);
for (guint i = 0; i < 2; i++)
for (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 (guint j = 0; j < script_count; j++)
for (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 (guint k = 0; k < language_count; k++)
for (k = 0; k < language_count; k++)
{
pair = g_new (TagPair, 1);
pair->script_tag = scripts[j];
@@ -898,6 +839,7 @@ 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");
@@ -914,31 +856,31 @@ update_script_combo (void)
}
}
g_list_store_append (store, script_lang_new (langname,
pair->script_index,
pair->lang_index,
pair->lang_tag));
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_hash_table_destroy (tags);
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);
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);
}
static char *
@@ -963,19 +905,27 @@ 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_drop_down_get_selected (GTK_DROP_DOWN (demo->script_lang)) == 0)
if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (demo->script_lang), &iter))
return;
selected = gtk_drop_down_get_selected_item (GTK_DROP_DOWN (demo->script_lang));
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);
if (selected->lang_tag == 0) /* None is selected */
if (lang_tag == 0) /* None is selected */
{
for (l = demo->feature_items; l; l = l->next)
{
@@ -1016,8 +966,8 @@ update_features (void)
hb_ot_layout_language_get_feature_tags (hb_face,
tables[i],
selected->script_index,
selected->lang_index,
script_index,
lang_index,
0,
&count,
features);
@@ -1039,8 +989,8 @@ update_features (void)
hb_ot_layout_language_find_feature (hb_face,
tables[i],
selected->script_index,
selected->lang_index,
script_index,
lang_index,
features[j],
&feature_index);
@@ -1372,9 +1322,10 @@ free_instance (gpointer data)
}
static void
add_instance (hb_face_t *face,
unsigned int index,
GtkStringList *strings)
add_instance (hb_face_t *face,
unsigned int index,
GtkWidget *combo,
int pos)
{
Instance *instance;
hb_ot_name_id_t name_id;
@@ -1390,20 +1341,20 @@ add_instance (hb_face_t *face,
instance->index = index;
g_hash_table_add (demo->instances, instance);
gtk_string_list_append (GTK_STRING_LIST (strings), instance->name);
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), instance->name);
}
static void
unset_instance (GtkAdjustment *adjustment)
{
if (demo->instance_combo)
gtk_drop_down_set_selected (GTK_DROP_DOWN (demo->instance_combo), 0);
gtk_combo_box_set_active (GTK_COMBO_BOX (demo->instance_combo), 0);
}
static void
instance_changed (GtkDropDown *combo)
instance_changed (GtkComboBox *combo)
{
const char *text;
char *text;
Instance *instance;
Instance ikey;
int i;
@@ -1415,12 +1366,11 @@ instance_changed (GtkDropDown *combo)
hb_font_t *hb_font;
hb_face_t *hb_face;
text = gtk_string_list_get_string (GTK_STRING_LIST (gtk_drop_down_get_model (combo)),
gtk_drop_down_get_selected (combo));
text = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (combo));
if (text[0] == '\0')
goto out;
ikey.name = (char *) text;
ikey.name = text;
instance = g_hash_table_lookup (demo->instances, &ikey);
if (!instance)
{
@@ -1461,6 +1411,7 @@ instance_changed (GtkDropDown *combo)
}
out:
g_free (text);
g_clear_object (&pango_font);
g_free (ai);
g_free (coords);
@@ -1570,7 +1521,6 @@ update_font_variations (void)
{
GtkWidget *label;
GtkWidget *combo;
GtkStringList *strings;
label = gtk_label_new ("Instance");
gtk_label_set_xalign (GTK_LABEL (label), 0);
@@ -1578,28 +1528,26 @@ 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);
strings = gtk_string_list_new (NULL);
combo = gtk_drop_down_new (G_LIST_MODEL (strings), NULL);
combo = gtk_combo_box_text_new ();
gtk_widget_set_halign (combo, GTK_ALIGN_START);
gtk_widget_set_valign (combo, GTK_ALIGN_BASELINE);
gtk_string_list_append (strings, "");
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "");
for (i = 0; i < hb_ot_var_get_named_instance_count (hb_face); i++)
add_instance (hb_face, i, strings);
add_instance (hb_face, i, combo, i);
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_drop_down_set_selected (GTK_DROP_DOWN (combo), i + 1);
gtk_combo_box_set_active (GTK_COMBO_BOX (combo), i + 1);
break;
}
}
gtk_grid_attach (GTK_GRID (demo->variations_grid), combo, 1, -1, 3, 1);
g_signal_connect (combo, "notify::selecte", G_CALLBACK (instance_changed), NULL);
g_signal_connect (combo, "changed", G_CALLBACK (instance_changed), NULL);
demo->instance_combo = combo;
}
@@ -1746,7 +1694,6 @@ do_font_features (GtkWidget *do_widget)
GtkBuilder *builder;
GtkBuilderScope *scope;
GtkEventController *controller;
GtkExpression *expression;
builder = gtk_builder_new ();
@@ -1780,9 +1727,6 @@ 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"));
@@ -1801,8 +1745,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_for_display (gdk_display_get_default (),
GTK_STYLE_PROVIDER (demo->provider), 800);
gtk_style_context_add_provider (gtk_widget_get_style_context (demo->swin),
GTK_STYLE_PROVIDER (demo->provider), 800);
basic_value_changed (demo->size_adjustment, demo->size_entry);
basic_value_changed (demo->letterspacing_adjustment, demo->letterspacing_entry);
@@ -1880,5 +1824,3 @@ do_font_features (GtkWidget *do_widget)
return window;
}
/* vim:set foldmethod=marker expandtab: */

View File

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

View File

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

View File

@@ -79,8 +79,6 @@
#include <gtk/gtkcenterlayout.h>
#include <gtk/gtkcheckbutton.h>
#include <gtk/gtkcolorbutton.h>
#include <gtk/gtkcolorchoice.h>
#include <gtk/gtkcolorchoicebutton.h>
#include <gtk/gtkcolorchooser.h>
#include <gtk/gtkcolorchooserdialog.h>
#include <gtk/gtkcolorchooserwidget.h>

View File

@@ -1,195 +0,0 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 2022 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtkcolorchoice.h"
#include "gtkcolorchooserwindowprivate.h"
#include "gtkcolorchooser.h"
#include "gtkbutton.h"
/* {{{ GObject implementation */
struct _GtkColorChoice
{
GObject parent_instance;
GtkWindow *parent;
char *title;
gboolean use_alpha;
GTask *task;
GtkColorChooserWindow *window;
};
G_DEFINE_TYPE (GtkColorChoice, gtk_color_choice, G_TYPE_OBJECT)
static void
gtk_color_choice_init (GtkColorChoice *self)
{
}
static void
gtk_color_choice_finalize (GObject *object)
{
GtkColorChoice *self = GTK_COLOR_CHOICE (object);
g_assert (self->task == NULL);
g_assert (self->window == NULL);
g_free (self->title);
G_OBJECT_CLASS (gtk_color_choice_parent_class)->finalize (object);
}
static void
gtk_color_choice_class_init (GtkColorChoiceClass *class)
{
G_OBJECT_CLASS (class)->finalize = gtk_color_choice_finalize;
}
/* }}} */
/* {{{ Public API */
/* {{{ Constructor */
GtkColorChoice *
gtk_color_choice_new (GtkWindow *parent,
const char *title,
gboolean use_alpha)
{
GtkColorChoice *self;
self = g_object_new (GTK_TYPE_COLOR_CHOICE, NULL);
self->parent = parent;
self->title = g_strdup (title);
self->use_alpha = use_alpha;
return self;
}
/* }}} */
/* {{{ Async API */
enum
{
RESPONSE_OK,
RESPONSE_CANCEL
};
static void response_cb (GtkColorChoice *self,
int response);
static void
cancelled_cb (GCancellable *cancellable,
GtkColorChoice *self)
{
response_cb (self, RESPONSE_CANCEL);
}
static void
response_cb (GtkColorChoice *self,
int response)
{
GCancellable *cancellable;
g_assert (self->window != NULL);
g_assert (self->task != NULL);
cancellable = g_task_get_cancellable (self->task);
if (cancellable)
g_signal_handlers_disconnect_by_func (cancellable, cancelled_cb, self);
if (response == RESPONSE_OK)
{
GdkRGBA color;
gtk_color_chooser_window_save_color (GTK_COLOR_CHOOSER_WINDOW (self->window));
gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (self->window), &color);
g_task_return_pointer (self->task, gdk_rgba_copy (&color), (GDestroyNotify) gdk_rgba_free);
}
else
g_task_return_new_error (self->task, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled");
g_clear_pointer ((GtkWindow **)&self->window, gtk_window_destroy);
g_clear_object (&self->task);
}
static void
ok_button_clicked (GtkButton *button,
GtkColorChoice *self)
{
response_cb (self, RESPONSE_OK);
}
static void
cancel_button_clicked (GtkButton *button,
GtkColorChoice *self)
{
response_cb (self, RESPONSE_CANCEL);
}
void
gtk_color_choice_choose (GtkColorChoice *self,
const GdkRGBA *initial_color,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GtkColorChooserWindow *window;
GTask *task;
g_return_if_fail (GTK_IS_COLOR_CHOICE (self));
window = GTK_COLOR_CHOOSER_WINDOW (gtk_color_chooser_window_new (self->title, self->parent));
gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (window), self->use_alpha);
if (initial_color)
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (window), initial_color);
if (cancellable)
g_signal_connect (cancellable, "cancelled", G_CALLBACK (cancelled_cb), self);
g_signal_connect (gtk_color_chooser_window_get_ok_button (window), "clicked",
G_CALLBACK (ok_button_clicked), self);
g_signal_connect (gtk_color_chooser_window_get_cancel_button (window), "clicked",
G_CALLBACK (cancel_button_clicked), self);
task = g_task_new (self, cancellable, callback, user_data);
g_task_set_source_tag (task, gtk_color_choice_choose);
self->window = window;
self->task = task;
gtk_window_present (GTK_WINDOW (window));
}
GdkRGBA *
gtk_color_choice_choose_finish (GtkColorChoice *self,
GAsyncResult *result,
GError **error)
{
return g_task_propagate_pointer (G_TASK (result), error);
}
/* }}} */
/* }}} */
/* vim:set foldmethod=marker expandtab: */

View File

@@ -1,52 +0,0 @@
/* GTK - The GIMP Toolkit
*
* Copyright (C) 2022 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/>.
*/
#pragma once
#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#include <gdk/gdk.h>
#include <gtk/gtkwindow.h>
G_BEGIN_DECLS
#define GTK_TYPE_COLOR_CHOICE (gtk_color_choice_get_type ())
GDK_AVAILABLE_IN_4_10
G_DECLARE_FINAL_TYPE (GtkColorChoice, gtk_color_choice, GTK, COLOR_CHOICE, GObject)
GDK_AVAILABLE_IN_4_10
GtkColorChoice * gtk_color_choice_new (GtkWindow *parent,
const char *title,
gboolean use_alpha);
GDK_AVAILABLE_IN_4_10
void gtk_color_choice_choose (GtkColorChoice *choice,
const GdkRGBA *initial_color,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
GDK_AVAILABLE_IN_4_10
GdkRGBA * gtk_color_choice_choose_finish (GtkColorChoice *self,
GAsyncResult *result,
GError **error);
G_END_DECLS

View File

@@ -1,409 +0,0 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 2022 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtkcolorchoicebutton.h"
#include "gtkcolorchoice.h"
#include "gtkbinlayout.h"
#include "gtkbutton.h"
#include "gtkcolorswatchprivate.h"
#include "gtkdragsource.h"
#include "gtkdroptarget.h"
#include <glib/gi18n-lib.h>
//#include "gtkmain.h"
#include "gtkprivate.h"
//#include "gtksnapshot.h"
#include "gtkwidgetprivate.h"
static gboolean drop (GtkDropTarget *dest,
const GValue *value,
double x,
double y,
GtkColorChoiceButton *self);
static GdkContentProvider *
drag_prepare (GtkDragSource *source,
double x,
double y,
GtkColorChoiceButton *self);
static void button_clicked (GtkColorChoiceButton *self);
/* {{{ GObject implementation */
struct _GtkColorChoiceButton
{
GtkWidget parent_instance;
GdkRGBA color;
gboolean use_alpha;
char *title;
GtkWidget *button;
GtkWidget *swatch;
GtkColorChoice *choice;
};
/* Properties */
enum
{
PROP_COLOR = 1,
PROP_USE_ALPHA,
PROP_TITLE,
NUM_PROPERTIES
};
static GParamSpec *properties[NUM_PROPERTIES];
G_DEFINE_TYPE (GtkColorChoiceButton, gtk_color_choice_button, GTK_TYPE_WIDGET)
static void
gtk_color_choice_button_init (GtkColorChoiceButton *self)
{
PangoLayout *layout;
PangoRectangle rect;
GtkDragSource *source;
GtkDropTarget *dest;
self->use_alpha = TRUE;
self->title = g_strdup ("");
self->button = gtk_button_new ();
g_signal_connect_swapped (self->button, "clicked", G_CALLBACK (button_clicked), self);
gtk_widget_set_parent (self->button, GTK_WIDGET (self));
self->swatch = g_object_new (GTK_TYPE_COLOR_SWATCH,
"accessible-role", GTK_ACCESSIBLE_ROLE_IMG,
"selectable", FALSE,
"has-menu", FALSE,
"can-drag", FALSE,
NULL);
gtk_widget_set_can_focus (self->swatch, FALSE);
gtk_widget_remove_css_class (self->swatch, "activatable");
layout = gtk_widget_create_pango_layout (GTK_WIDGET (self), "Black");
pango_layout_get_pixel_extents (layout, NULL, &rect);
g_object_unref (layout);
gtk_widget_set_size_request (self->swatch, rect.width, rect.height);
gtk_button_set_child (GTK_BUTTON (self->button), self->swatch);
dest = gtk_drop_target_new (GDK_TYPE_RGBA, GDK_ACTION_COPY);
g_signal_connect (dest, "drop", G_CALLBACK (drop), self);
gtk_widget_add_controller (GTK_WIDGET (self->button), GTK_EVENT_CONTROLLER (dest));
source = gtk_drag_source_new ();
g_signal_connect (source, "prepare", G_CALLBACK (drag_prepare), self);
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (source),
GTK_PHASE_CAPTURE);
gtk_widget_add_controller (self->button, GTK_EVENT_CONTROLLER (source));
gtk_widget_add_css_class (self->button, "color");
}
static void
gtk_color_choice_button_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
{
GtkColorChoiceButton *self = GTK_COLOR_CHOICE_BUTTON (object);
switch (param_id)
{
case PROP_COLOR:
gtk_color_choice_button_set_color (self, g_value_get_boxed (value));
break;
case PROP_USE_ALPHA:
gtk_color_choice_button_set_use_alpha (self, g_value_get_boolean (value));
break;
case PROP_TITLE:
gtk_color_choice_button_set_title (self, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
static void
gtk_color_choice_button_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
{
GtkColorChoiceButton *self = GTK_COLOR_CHOICE_BUTTON (object);
switch (param_id)
{
case PROP_COLOR:
g_value_set_boxed (value, &self->color);
break;
case PROP_USE_ALPHA:
g_value_set_boolean (value, self->use_alpha);
break;
case PROP_TITLE:
g_value_set_string (value, self->title);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
static void
gtk_color_choice_button_dispose (GObject *object)
{
GtkColorChoiceButton *self = GTK_COLOR_CHOICE_BUTTON (object);
g_clear_pointer (&self->button, gtk_widget_unparent);
G_OBJECT_CLASS (gtk_color_choice_button_parent_class)->dispose (object);
}
static void
gtk_color_choice_button_finalize (GObject *object)
{
GtkColorChoiceButton *self = GTK_COLOR_CHOICE_BUTTON (object);
g_free (self->title);
G_OBJECT_CLASS (gtk_color_choice_button_parent_class)->finalize (object);
}
static void
gtk_color_choice_button_class_init (GtkColorChoiceButtonClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
object_class->get_property = gtk_color_choice_button_get_property;
object_class->set_property = gtk_color_choice_button_set_property;
object_class->dispose = gtk_color_choice_button_dispose;
object_class->finalize = gtk_color_choice_button_finalize;
widget_class->grab_focus = gtk_widget_grab_focus_child;
widget_class->focus = gtk_widget_focus_child;
properties[PROP_COLOR] =
g_param_spec_boxed ("color", NULL, NULL,
GDK_TYPE_RGBA,
G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
properties[PROP_USE_ALPHA] =
g_param_spec_boolean ("use-alpha", NULL, NULL,
TRUE,
G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
properties[PROP_TITLE] =
g_param_spec_string ("title", NULL, NULL,
"",
G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
gtk_widget_class_set_css_name (widget_class, "colorbutton");
}
/* }}} */
/* {{{ Private API, callbacks */
static guint
scale_round (double value,
double scale)
{
value = floor (value * scale + 0.5);
value = CLAMP (value, 0, scale);
return (guint)value;
}
static char *
accessible_color_name (const GdkRGBA *color)
{
if (color->alpha < 1.0)
return g_strdup_printf (_("Red %d%%, Green %d%%, Blue %d%%, Alpha %d%%"),
scale_round (color->red, 100),
scale_round (color->green, 100),
scale_round (color->blue, 100),
scale_round (color->alpha, 100));
else
return g_strdup_printf (_("Red %d%%, Green %d%%, Blue %d%%"),
scale_round (color->red, 100),
scale_round (color->green, 100),
scale_round (color->blue, 100));
}
static gboolean
drop (GtkDropTarget *dest,
const GValue *value,
double x,
double y,
GtkColorChoiceButton *self)
{
GdkRGBA *color = g_value_get_boxed (value);
gtk_color_choice_button_set_color (self, color);
return TRUE;
}
static GdkContentProvider *
drag_prepare (GtkDragSource *source,
double x,
double y,
GtkColorChoiceButton *self)
{
return gdk_content_provider_new_typed (GDK_TYPE_RGBA, &self->color);
}
static void
color_chosen (GObject *source,
GAsyncResult *result,
gpointer data)
{
GtkColorChoice *choice = GTK_COLOR_CHOICE (source);
GtkColorChoiceButton *self = data;
GdkRGBA *color;
GError *error = NULL;
color = gtk_color_choice_choose_finish (choice, result, &error);
if (color)
{
gtk_color_choice_button_set_color (self, color);
gdk_rgba_free (color);
}
else
{
g_print ("%s\n", error->message);
g_error_free (error);
}
}
static void
button_clicked (GtkColorChoiceButton *self)
{
GtkRoot *root;
GtkWindow *parent = NULL;
GtkColorChoice *choice;
root = gtk_widget_get_root (GTK_WIDGET (self));
if (GTK_IS_WINDOW (root))
parent = GTK_WINDOW (root);
choice = gtk_color_choice_new (parent, self->title, self->use_alpha);
gtk_color_choice_choose (choice, &self->color, NULL, color_chosen, self);
g_object_unref (choice);
}
/* }}} */
/* {{{ Public API */
/* {{{ Constructor */
GtkColorChoiceButton *
gtk_color_choice_button_new (void)
{
return g_object_new (GTK_TYPE_COLOR_CHOICE_BUTTON, NULL);
}
/* }}} */
/* {{{ Setters and Getters */
void
gtk_color_choice_button_set_color (GtkColorChoiceButton *self,
const GdkRGBA *color)
{
char *text;
g_return_if_fail (GTK_IS_COLOR_CHOICE_BUTTON (self));
g_return_if_fail (color != NULL);
if (gdk_rgba_equal (&self->color, color))
return;
self->color = *color;
gtk_color_swatch_set_rgba (GTK_COLOR_SWATCH (self->swatch), color);
text = accessible_color_name (color);
gtk_accessible_update_property (GTK_ACCESSIBLE (self->swatch),
GTK_ACCESSIBLE_PROPERTY_LABEL, text,
-1);
g_free (text);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COLOR]);
}
const GdkRGBA *
gtk_color_choice_button_get_color (GtkColorChoiceButton *self)
{
g_return_val_if_fail (GTK_IS_COLOR_CHOICE_BUTTON (self), NULL);
return &self->color;
}
void
gtk_color_choice_button_set_use_alpha (GtkColorChoiceButton *self,
gboolean use_alpha)
{
g_return_if_fail (GTK_IS_COLOR_CHOICE_BUTTON (self));
if (self->use_alpha == use_alpha)
return;
self->use_alpha = use_alpha;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_USE_ALPHA]);
}
gboolean
gtk_color_choice_button_get_use_alpha (GtkColorChoiceButton *self)
{
g_return_val_if_fail (GTK_IS_COLOR_CHOICE_BUTTON (self), TRUE);
return self->use_alpha;
}
void
gtk_color_choice_button_set_title (GtkColorChoiceButton *self,
const char *title)
{
char *new_title;
g_return_if_fail (GTK_IS_COLOR_CHOICE_BUTTON (self));
g_return_if_fail (title != NULL);
if (g_str_equal (self->title, title))
return;
new_title = g_strdup (title);
g_free (self->title);
self->title = new_title;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TITLE]);
}
/* }}} */
/* }}} */
/* vim:set foldmethod=marker expandtab: */

View File

@@ -1,60 +0,0 @@
/*
* GTK - The GIMP Toolkit
* Copyright (C) 2022 Red Hat, Inc.
* All rights reserved.
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#include <gtk/gtkwidget.h>
G_BEGIN_DECLS
#define GTK_TYPE_COLOR_CHOICE_BUTTON (gtk_color_choice_button_get_type ())
GDK_AVAILABLE_IN_4_10
G_DECLARE_FINAL_TYPE (GtkColorChoiceButton, gtk_color_choice_button, GTK, COLOR_CHOICE_BUTTON, GtkWidget)
GDK_AVAILABLE_IN_4_10
GtkColorChoiceButton *
gtk_color_choice_button_new (void);
GDK_AVAILABLE_IN_4_10
void gtk_color_choice_button_set_color (GtkColorChoiceButton *self,
const GdkRGBA *color);
GDK_AVAILABLE_IN_4_10
const GdkRGBA * gtk_color_choice_button_get_color (GtkColorChoiceButton *self);
GDK_AVAILABLE_IN_4_10
void gtk_color_choice_button_set_use_alpha (GtkColorChoiceButton *self,
gboolean use_alpha);
GDK_AVAILABLE_IN_4_10
gboolean gtk_color_choice_button_get_use_alpha (GtkColorChoiceButton *self);
GDK_AVAILABLE_IN_4_10
void gtk_color_choice_button_set_title (GtkColorChoiceButton *self,
const char *title);
GDK_AVAILABLE_IN_4_10
const char * gtk_color_choice_button_get_title (GtkColorChoiceButton *self);
G_END_DECLS

View File

@@ -1,278 +0,0 @@
/* 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].
*/
struct _GtkColorChooserWindow
{
GtkWindow parent_instance;
GtkWidget *chooser;
GtkWidget *ok_button;
GtkWidget *cancel_button;
};
enum
{
PROP_RGBA = 1,
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 *self)
{
g_object_notify (G_OBJECT (self), pspec->name);
}
static void
color_activated_cb (GtkColorChooser *chooser,
GdkRGBA *color,
GtkColorChooserWindow *window)
{
g_signal_emit_by_name (window->ok_button, "clicked");
}
static void
gtk_color_chooser_window_init (GtkColorChooserWindow *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
}
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 *self = GTK_COLOR_CHOOSER_WINDOW (object);
switch (prop_id)
{
case PROP_RGBA:
{
GdkRGBA color;
gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (self), &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 (self->chooser)));
break;
case PROP_SHOW_EDITOR:
{
gboolean show_editor;
g_object_get (self->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 *self = GTK_COLOR_CHOOSER_WINDOW (object);
switch (prop_id)
{
case PROP_RGBA:
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (self), g_value_get_boxed (value));
break;
case PROP_USE_ALPHA:
if (gtk_color_chooser_get_use_alpha (GTK_COLOR_CHOOSER (self->chooser)) != g_value_get_boolean (value))
{
gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (self->chooser), g_value_get_boolean (value));
g_object_notify_by_pspec (object, pspec);
}
break;
case PROP_SHOW_EDITOR:
g_object_set (self->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 *self = GTK_COLOR_CHOOSER_WINDOW (object);
g_clear_pointer (&self->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));
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_child (widget_class, GtkColorChooserWindow, ok_button);
gtk_widget_class_bind_template_child (widget_class, GtkColorChooserWindow, cancel_button);
gtk_widget_class_bind_template_callback (widget_class, propagate_notify);
gtk_widget_class_bind_template_callback (widget_class, color_activated_cb);
}
static void
gtk_color_chooser_window_get_rgba (GtkColorChooser *chooser,
GdkRGBA *color)
{
GtkColorChooserWindow *self = GTK_COLOR_CHOOSER_WINDOW (chooser);
gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (self->chooser), color);
}
static void
gtk_color_chooser_window_set_rgba (GtkColorChooser *chooser,
const GdkRGBA *color)
{
GtkColorChooserWindow *self = GTK_COLOR_CHOOSER_WINDOW (chooser);
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (self->chooser), color);
}
static void
gtk_color_chooser_window_add_palette (GtkColorChooser *chooser,
GtkOrientation orientation,
int colors_per_line,
int n_colors,
GdkRGBA *colors)
{
GtkColorChooserWindow *self = GTK_COLOR_CHOOSER_WINDOW (chooser);
gtk_color_chooser_add_palette (GTK_COLOR_CHOOSER (self->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;
}
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,
"modal", TRUE,
NULL);
}
void
gtk_color_chooser_window_save_color (GtkColorChooserWindow *self)
{
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 (self), &color);
gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (self), &color);
}
GtkWidget *
gtk_color_chooser_window_get_ok_button (GtkColorChooserWindow *self)
{
return self->ok_button;
}
GtkWidget *
gtk_color_chooser_window_get_cancel_button (GtkColorChooserWindow *self)
{
return self->cancel_button;
}

View File

@@ -1,34 +0,0 @@
/* 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/>.
*/
#pragma once
#include <gtk/gtkwindow.h>
G_BEGIN_DECLS
#define GTK_TYPE_COLOR_CHOOSER_WINDOW (gtk_color_chooser_window_get_type ())
G_DECLARE_FINAL_TYPE (GtkColorChooserWindow, gtk_color_chooser_window, GTK, COLOR_CHOOSER_WINDOW, GtkWindow)
GtkWidget * gtk_color_chooser_window_new (const char *title,
GtkWindow *parent);
void gtk_color_chooser_window_save_color (GtkColorChooserWindow *self);
GtkWidget *gtk_color_chooser_window_get_ok_button (GtkColorChooserWindow *self);
GtkWidget *gtk_color_chooser_window_get_cancel_button (GtkColorChooserWindow *self);
G_END_DECLS

View File

@@ -133,6 +133,7 @@ enum
PROP_MODEL,
PROP_SELECTED,
PROP_SELECTED_ITEM,
PROP_SELECTED_STRING,
PROP_ENABLE_SEARCH,
PROP_EXPRESSION,
PROP_SHOW_ARROW,
@@ -247,6 +248,7 @@ 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
@@ -358,6 +360,10 @@ 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;
@@ -402,6 +408,10 @@ 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;
@@ -545,6 +555,22 @@ 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)
*
@@ -784,8 +810,7 @@ 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`
*/
@@ -1016,6 +1041,81 @@ 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,6 +52,13 @@ 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

@@ -7338,8 +7338,6 @@ 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);
@@ -7396,21 +7394,8 @@ gtk_file_chooser_widget_set_choice (GtkFileChooser *chooser,
if (GTK_IS_BOX (widget))
{
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;
}
}
GtkWidget *dropdown = gtk_widget_get_last_child (widget);
gtk_drop_down_set_selected_string (GTK_DROP_DOWN (dropdown), option);
}
else if (GTK_IS_CHECK_BUTTON (widget))
gtk_check_button_set_active (GTK_CHECK_BUTTON (widget), g_str_equal (option, "true"));
@@ -7428,19 +7413,9 @@ gtk_file_chooser_widget_get_choice (GtkFileChooser *chooser,
widget = (GtkWidget *)g_hash_table_lookup (impl->choices, id);
if (GTK_IS_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];
}
return gtk_drop_down_get_selected_string (GTK_DROP_DOWN (widget));
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

@@ -183,12 +183,9 @@ gtk_public_sources = files([
'gtkcenterlayout.c',
'gtkcheckbutton.c',
'gtkcolorbutton.c',
'gtkcolorchoice.c',
'gtkcolorchoicebutton.c',
'gtkcolorchooser.c',
'gtkcolorchooserdialog.c',
'gtkcolorchooserwidget.c',
'gtkcolorchooserwindow.c',
'gtkcolorutils.c',
'gtkcolumnview.c',
'gtkcolumnviewcolumn.c',
@@ -442,8 +439,6 @@ gtk_public_headers = files([
'gtkcenterlayout.h',
'gtkcheckbutton.h',
'gtkcolorbutton.h',
'gtkcolorchoice.h',
'gtkcolorchoicebutton.h',
'gtkcolorchooser.h',
'gtkcolorchooserdialog.h',
'gtkcolorchooserwidget.h',

View File

@@ -1,52 +0,0 @@
<?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>
</object>
</child>
<child type="end">
<object class="GtkButton" id="ok_button">
<property name="label" translatable="yes">_Select</property>
<property name="use-underline">1</property>
<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>