gtk2/demos/icon-browser/iconbrowserwin.c

416 lines
12 KiB
C
Raw Normal View History

#include <string.h>
#include "iconbrowserapp.h"
#include "iconbrowserwin.h"
2020-06-08 18:25:54 +00:00
#include "iconbrowsericon.h"
#include "iconbrowsercontext.h"
#include <gtk/gtk.h>
struct _IconBrowserWindow
{
GtkApplicationWindow parent;
2014-07-27 07:32:10 +00:00
GtkWidget *symbolic_radio;
GtkWidget *searchbar;
2020-06-08 18:25:54 +00:00
GListModel *icon_filter_model;
GListStore *icon_store;
GListStore *context_store;
GtkFilter *name_filter;
GtkWidget *details;
GtkWidget *image1;
GtkWidget *image2;
GtkWidget *image3;
GtkWidget *image4;
GtkWidget *image5;
GtkWidget *image6;
GtkWidget *image7;
GtkWidget *image8;
GtkWidget *label8;
GtkWidget *description;
};
struct _IconBrowserWindowClass
{
GtkApplicationWindowClass parent_class;
};
G_DEFINE_TYPE(IconBrowserWindow, icon_browser_window, GTK_TYPE_APPLICATION_WINDOW);
static GtkIconTheme *
icon_browser_window_get_icon_theme (IconBrowserWindow *win)
{
return gtk_icon_theme_get_for_display (gtk_widget_get_display (GTK_WIDGET (win)));
}
static void
add_icon (IconBrowserWindow *win,
2020-07-24 18:40:36 +00:00
const char *name,
const char *description,
const char *context)
{
GtkIconTheme *icon_theme = icon_browser_window_get_icon_theme (win);
2020-07-24 18:40:36 +00:00
char *regular_name;
char *symbolic_name;
2020-06-08 18:25:54 +00:00
IbIcon *icon;
2014-07-27 07:32:10 +00:00
regular_name = g_strdup (name);
if (!gtk_icon_theme_has_icon (icon_theme, regular_name))
2014-07-27 07:32:10 +00:00
{
g_free (regular_name);
regular_name = NULL;
}
symbolic_name = g_strconcat (name, "-symbolic", NULL);
if (!gtk_icon_theme_has_icon (icon_theme, symbolic_name))
2014-07-27 07:32:10 +00:00
{
g_free (symbolic_name);
symbolic_name = NULL;
}
2020-06-08 18:25:54 +00:00
icon = ib_icon_new (regular_name, symbolic_name, description, context);
g_object_bind_property (win->symbolic_radio, "active",
icon, "use-symbolic",
G_BINDING_DEFAULT);
g_list_store_append (win->icon_store, icon);
g_object_unref (icon);
}
static void
add_context (IconBrowserWindow *win,
2020-07-24 18:40:36 +00:00
const char *id,
const char *name,
const char *description)
{
2020-06-08 18:25:54 +00:00
IbContext *context;
2020-06-08 18:25:54 +00:00
context = ib_context_new (id, name, description);
g_list_store_append (win->context_store, context);
g_object_unref (context);
}
static void
populate (IconBrowserWindow *win)
{
GFile *file;
GKeyFile *kf;
char *data;
gsize length;
char **groups;
int i;
file = g_file_new_for_uri ("resource:/org/gtk/iconbrowser/gtk/icon.list");
g_file_load_contents (file, NULL, &data, &length, NULL, NULL);
kf = g_key_file_new ();
g_key_file_load_from_data (kf, data, length, G_KEY_FILE_NONE, NULL);
groups = g_key_file_get_groups (kf, &length);
for (i = 0; i < length; i++)
{
const char *context;
const char *name;
const char *description;
char **keys;
gsize len;
int j;
context = groups[i];
name = g_key_file_get_string (kf, context, "Name", NULL);
description = g_key_file_get_string (kf, context, "Description", NULL);
add_context (win, context, name, description);
keys = g_key_file_get_keys (kf, context, &len, NULL);
for (j = 0; j < len; j++)
{
const char *key = keys[j];
const char *value;
if (strcmp (key, "Name") == 0 || strcmp (key, "Description") == 0)
continue;
value = g_key_file_get_string (kf, context, key, NULL);
add_icon (win, key, value, context);
}
g_strfreev (keys);
}
g_strfreev (groups);
}
2020-06-08 18:25:54 +00:00
static gboolean
filter_by_icon_name (gpointer item,
gpointer data)
{
return ib_icon_get_name (IB_ICON (item)) != NULL;
}
static void
symbolic_toggled (IconBrowserWindow *win)
{
gtk_filter_changed (win->name_filter, GTK_FILTER_CHANGE_DIFFERENT);
}
static void
copy_to_clipboard (GtkButton *button,
IconBrowserWindow *win)
{
2017-12-02 14:41:22 +00:00
GdkClipboard *clipboard;
2017-12-02 14:41:22 +00:00
clipboard = gtk_widget_get_clipboard (GTK_WIDGET (win));
gdk_clipboard_set_text (clipboard, gtk_window_get_title (GTK_WINDOW (win->details)));
}
2020-06-08 18:25:54 +00:00
static void
2020-07-24 18:40:36 +00:00
set_image (GtkWidget *image, const char *name, int size)
2014-07-27 07:32:10 +00:00
{
2020-06-08 18:25:54 +00:00
gtk_image_set_from_icon_name (GTK_IMAGE (image), name);
gtk_image_set_pixel_size (GTK_IMAGE (image), size);
2014-07-27 07:32:10 +00:00
}
static void
2020-06-08 18:25:54 +00:00
item_activated (GtkGridView *view,
guint position,
IconBrowserWindow *win)
2014-07-27 07:32:10 +00:00
{
2020-06-08 18:25:54 +00:00
GListModel *model = gtk_grid_view_get_model (view);
IbIcon *icon = g_list_model_get_item (model, position);
const char *name;
const char *description;
gboolean symbolic;
2014-07-27 11:44:41 +00:00
2020-06-08 18:25:54 +00:00
name = ib_icon_get_name (icon);
description = ib_icon_get_description (icon);
symbolic = ib_icon_get_use_symbolic (icon);
2014-07-27 11:44:41 +00:00
2020-06-08 18:25:54 +00:00
gtk_window_set_title (GTK_WINDOW (win->details), name);
set_image (win->image1, name, 8);
set_image (win->image2, name, 16);
set_image (win->image3, name, 18);
set_image (win->image4, name, 24);
set_image (win->image5, name, 32);
set_image (win->image6, name, 48);
set_image (win->image7, name, 64);
if (symbolic)
{
gtk_widget_show (win->image8);
gtk_widget_show (win->label8);
set_image (win->image8, name, 64);
}
2014-07-27 11:44:41 +00:00
else
2020-06-08 18:25:54 +00:00
{
gtk_widget_hide (win->image8);
gtk_widget_hide (win->label8);
}
if (description && description[0])
{
gtk_label_set_text (GTK_LABEL (win->description), description);
gtk_widget_show (win->description);
}
else
{
gtk_widget_hide (win->description);
}
2020-06-08 18:25:54 +00:00
gtk_window_present (GTK_WINDOW (win->details));
2014-07-27 07:32:10 +00:00
2020-06-08 18:25:54 +00:00
g_object_unref (icon);
}
2020-01-01 02:06:43 +00:00
static GdkPaintable *
get_image_paintable (GtkImage *image)
{
2020-07-24 18:40:36 +00:00
const char *icon_name;
2020-01-01 02:06:43 +00:00
GtkIconTheme *icon_theme;
2020-02-04 16:19:22 +00:00
GtkIconPaintable *icon;
2020-01-01 02:06:43 +00:00
int size;
switch (gtk_image_get_storage_type (image))
{
case GTK_IMAGE_PAINTABLE:
return g_object_ref (gtk_image_get_paintable (image));
case GTK_IMAGE_ICON_NAME:
icon_name = gtk_image_get_icon_name (image);
size = gtk_image_get_pixel_size (image);
icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (GTK_WIDGET (image)));
icon = gtk_icon_theme_lookup_icon (icon_theme,
icon_name,
NULL,
size, 1,
gtk_widget_get_direction (GTK_WIDGET (image)),
0);
if (icon == NULL)
2020-06-08 18:25:54 +00:00
{
g_print ("no icon for %s\n", icon_name);
return NULL;
}
return GDK_PAINTABLE (icon);
case GTK_IMAGE_GICON:
case GTK_IMAGE_EMPTY:
2020-01-01 02:06:43 +00:00
default:
g_warning ("Image storage type %d not handled",
gtk_image_get_storage_type (image));
return NULL;
}
}
static void
2020-01-01 02:06:43 +00:00
drag_begin (GtkDragSource *source,
GdkDrag *drag,
2020-01-01 02:06:43 +00:00
GtkWidget *widget)
{
2020-01-01 02:06:43 +00:00
GdkPaintable *paintable;
2020-01-01 02:06:43 +00:00
paintable = get_image_paintable (GTK_IMAGE (widget));
if (paintable)
{
int w, h;
2020-01-01 02:06:43 +00:00
w = gdk_paintable_get_intrinsic_width (paintable);
h = gdk_paintable_get_intrinsic_height (paintable);
gtk_drag_source_set_icon (source, paintable, w, h);
g_object_unref (paintable);
}
}
static GdkContentProvider *
drag_prepare_texture (GtkDragSource *source,
double x,
double y,
GtkWidget *widget)
2020-01-01 02:06:43 +00:00
{
GdkPaintable *paintable = get_image_paintable (GTK_IMAGE (widget));
2020-06-08 18:25:54 +00:00
if (!GDK_IS_PAINTABLE (paintable))
return NULL;
2020-01-01 02:06:43 +00:00
2020-06-08 18:25:54 +00:00
return gdk_content_provider_new_typed (GDK_TYPE_PAINTABLE, paintable);
}
static GdkContentProvider *
drag_prepare_file (GtkDragSource *source,
double x,
double y,
GtkWidget *widget)
{
GdkContentProvider *content;
GtkIconTheme *icon_theme;
2020-01-01 02:06:43 +00:00
const char *name;
2020-02-04 16:19:22 +00:00
GtkIconPaintable *info;
name = gtk_image_get_icon_name (GTK_IMAGE (widget));
icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (widget));
info = gtk_icon_theme_lookup_icon (icon_theme,
name,
NULL,
32, 1,
gtk_widget_get_direction (widget),
0);
content = gdk_content_provider_new_typed (G_TYPE_FILE, gtk_icon_paintable_get_file (info));
2020-01-01 02:06:43 +00:00
g_object_unref (info);
return content;
}
static void
setup_image_dnd (GtkWidget *image)
{
2020-01-01 02:06:43 +00:00
GtkDragSource *source;
source = gtk_drag_source_new ();
g_signal_connect (source, "prepare", G_CALLBACK (drag_prepare_texture), image);
2020-01-01 02:06:43 +00:00
g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), image);
gtk_widget_add_controller (image, GTK_EVENT_CONTROLLER (source));
}
static void
setup_scalable_image_dnd (GtkWidget *image)
{
2020-01-01 02:06:43 +00:00
GtkDragSource *source;
source = gtk_drag_source_new ();
g_signal_connect (source, "prepare", G_CALLBACK (drag_prepare_file), image);
2020-01-01 02:06:43 +00:00
g_signal_connect (source, "drag-begin", G_CALLBACK (drag_begin), image);
gtk_widget_add_controller (image, GTK_EVENT_CONTROLLER (source));
}
static void
icon_browser_window_init (IconBrowserWindow *win)
{
2020-06-08 18:25:54 +00:00
GtkFilter *filter;
gtk_widget_init_template (GTK_WIDGET (win));
setup_image_dnd (win->image1);
setup_image_dnd (win->image2);
setup_image_dnd (win->image3);
setup_image_dnd (win->image4);
setup_image_dnd (win->image5);
setup_image_dnd (win->image6);
setup_image_dnd (win->image7);
setup_scalable_image_dnd (win->image8);
2014-07-27 07:32:10 +00:00
gtk_window_set_transient_for (GTK_WINDOW (win->details), GTK_WINDOW (win));
2020-06-08 18:25:54 +00:00
gtk_search_bar_set_key_capture_widget (GTK_SEARCH_BAR (win->searchbar), GTK_WIDGET (win));
2014-07-27 07:32:10 +00:00
2020-06-08 18:25:54 +00:00
populate (win);
2020-06-08 18:25:54 +00:00
filter = gtk_filter_list_model_get_filter (GTK_FILTER_LIST_MODEL (win->icon_filter_model));
2014-07-27 11:44:41 +00:00
2020-06-08 18:25:54 +00:00
win->name_filter = gtk_custom_filter_new (filter_by_icon_name, NULL, NULL);
gtk_multi_filter_append (GTK_MULTI_FILTER (filter), g_object_ref (win->name_filter));
}
static void
icon_browser_window_finalize (GObject *object)
{
IconBrowserWindow *win = ICON_BROWSER_WINDOW (object);
2020-06-08 18:25:54 +00:00
g_clear_object (&win->name_filter);
G_OBJECT_CLASS (icon_browser_window_parent_class)->finalize (object);
}
static void
icon_browser_window_class_init (IconBrowserWindowClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->finalize = icon_browser_window_finalize;
2020-06-08 18:25:54 +00:00
g_type_ensure (IB_TYPE_ICON);
g_type_ensure (IB_TYPE_CONTEXT);
gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
"/org/gtk/iconbrowser/gtk/window.ui");
2014-07-27 07:32:10 +00:00
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, symbolic_radio);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, searchbar);
2020-06-08 18:25:54 +00:00
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, icon_store);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, icon_filter_model);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, context_store);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, details);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, image1);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, image2);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, image3);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, image4);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, image5);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, image6);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, image7);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, image8);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, label8);
gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (class), IconBrowserWindow, description);
2014-07-27 07:32:10 +00:00
gtk_widget_class_bind_template_callback (GTK_WIDGET_CLASS (class), item_activated);
gtk_widget_class_bind_template_callback (GTK_WIDGET_CLASS (class), copy_to_clipboard);
2020-06-08 18:25:54 +00:00
gtk_widget_class_bind_template_callback (GTK_WIDGET_CLASS (class), symbolic_toggled);
}
IconBrowserWindow *
icon_browser_window_new (IconBrowserApp *app)
{
return g_object_new (ICON_BROWSER_WINDOW_TYPE, "application", app, NULL);
}