Added base64 image support
This commit is contained in:
parent
ad73f24c75
commit
da98a2d240
29
inc/utils_g.h
Normal file
29
inc/utils_g.h
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Scoopta
|
||||
* This file is part of Wofi
|
||||
* Wofi is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Wofi is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Wofi. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef UTILS_G_H
|
||||
#define UTILS_G_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <gdk-pixbuf/gdk-pixbuf.h>
|
||||
|
||||
GdkPixbuf* utils_g_resize_pixbuf(GdkPixbuf* pixbuf, uint64_t image_size, GdkInterpType interp);
|
||||
|
||||
GdkPixbuf* utils_g_pixbuf_from_base64(char* base64);
|
||||
|
||||
#endif
|
@ -24,6 +24,7 @@ sources = ['src/config.c',
|
||||
'src/main.c',
|
||||
'src/map.c',
|
||||
'src/property_box.c',
|
||||
'src/utils_g.c',
|
||||
'src/utils.c',
|
||||
'src/widget_builder.c',
|
||||
'src/wofi.c',
|
||||
@ -60,6 +61,7 @@ subdir = 'wofi-1'
|
||||
|
||||
install_headers('inc/config.h',
|
||||
'inc/map.h',
|
||||
'inc/utils_g.h',
|
||||
'inc/utils.h',
|
||||
'inc/widget_builder_api.h',
|
||||
'inc/wofi_api.h',
|
||||
|
17
modes/drun.c
17
modes/drun.c
@ -23,6 +23,7 @@
|
||||
#include <map.h>
|
||||
#include <utils.h>
|
||||
#include <config.h>
|
||||
#include <utils_g.h>
|
||||
#include <widget_builder_api.h>
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
@ -118,21 +119,7 @@ static bool populate_widget(char* file, char* action, struct widget_builder* bui
|
||||
pixbuf = gtk_icon_info_load_icon(info, NULL);
|
||||
}
|
||||
|
||||
int width = gdk_pixbuf_get_width(pixbuf);
|
||||
int height = gdk_pixbuf_get_height(pixbuf);
|
||||
uint64_t image_size = wofi_get_image_size();
|
||||
|
||||
if(height > width) {
|
||||
float percent = (float) image_size / height;
|
||||
GdkPixbuf* tmp = gdk_pixbuf_scale_simple(pixbuf, width * percent, image_size, GDK_INTERP_BILINEAR);
|
||||
g_object_unref(pixbuf);
|
||||
pixbuf = tmp;
|
||||
} else {
|
||||
float percent = (float) image_size / width;
|
||||
GdkPixbuf* tmp = gdk_pixbuf_scale_simple(pixbuf, image_size, height * percent, GDK_INTERP_BILINEAR);
|
||||
g_object_unref(pixbuf);
|
||||
pixbuf = tmp;
|
||||
}
|
||||
pixbuf = utils_g_resize_pixbuf(pixbuf, wofi_get_image_size(), GDK_INTERP_BILINEAR);
|
||||
|
||||
wofi_widget_builder_insert_image(builder, pixbuf, "icon");
|
||||
g_object_unref(pixbuf);
|
||||
|
76
src/utils_g.c
Normal file
76
src/utils_g.c
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Scoopta
|
||||
* This file is part of Wofi
|
||||
* Wofi is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Wofi is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Wofi. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <utils_g.h>
|
||||
|
||||
#include <wofi_api.h>
|
||||
|
||||
GdkPixbuf* utils_g_resize_pixbuf(GdkPixbuf* pixbuf, uint64_t image_size, GdkInterpType interp) {
|
||||
int width = gdk_pixbuf_get_width(pixbuf);
|
||||
int height = gdk_pixbuf_get_height(pixbuf);
|
||||
|
||||
if(height > width) {
|
||||
float percent = (float) image_size / height;
|
||||
GdkPixbuf* tmp = gdk_pixbuf_scale_simple(pixbuf, width * percent, image_size, interp);
|
||||
g_object_unref(pixbuf);
|
||||
return tmp;
|
||||
} else {
|
||||
float percent = (float) image_size / width;
|
||||
GdkPixbuf* tmp = gdk_pixbuf_scale_simple(pixbuf, image_size, height * percent, interp);
|
||||
g_object_unref(pixbuf);
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
|
||||
GdkPixbuf* utils_g_pixbuf_from_base64(char* base64) {
|
||||
char* str = strdup(base64);
|
||||
char* original_str = str;
|
||||
|
||||
GError* err = NULL;
|
||||
GdkPixbufLoader* loader;
|
||||
if(strncmp(str, "image/", sizeof("image/") - 1) == 0) {
|
||||
char* mime = strchr(str, ';');
|
||||
*mime = 0;
|
||||
loader = gdk_pixbuf_loader_new_with_mime_type(str, &err);
|
||||
if(err != NULL) {
|
||||
goto fail;
|
||||
}
|
||||
str = mime + 1;
|
||||
str = strchr(str, ',') + 1;
|
||||
} else {
|
||||
loader = gdk_pixbuf_loader_new();
|
||||
}
|
||||
|
||||
gsize data_l;
|
||||
guchar* data = g_base64_decode(str, &data_l);
|
||||
|
||||
gdk_pixbuf_loader_write(loader, data, data_l, &err);
|
||||
if(err != NULL) {
|
||||
g_free(data);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
g_free(data);
|
||||
|
||||
free(original_str);
|
||||
return gdk_pixbuf_loader_get_pixbuf(loader);
|
||||
|
||||
fail:
|
||||
free(str);
|
||||
fprintf(stderr, "Error loading base64 %s\n", err->message);
|
||||
return NULL;
|
||||
}
|
44
src/wofi.c
44
src/wofi.c
@ -29,6 +29,7 @@
|
||||
|
||||
#include <utils.h>
|
||||
#include <config.h>
|
||||
#include <utils_g.h>
|
||||
#include <property_box.h>
|
||||
#include <widget_builder.h>
|
||||
|
||||
@ -216,6 +217,8 @@ static char* parse_images(WofiPropertyBox* box, const char* text, bool create_wi
|
||||
struct map* mode_map = map_init();
|
||||
map_put(mode_map, "img", "true");
|
||||
map_put(mode_map, "img-noscale", "true");
|
||||
map_put(mode_map, "img-base64", "true");
|
||||
map_put(mode_map, "img-base64-noscale", "true");
|
||||
map_put(mode_map, "text", "true");
|
||||
|
||||
char* tmp = strdup(text);
|
||||
@ -286,19 +289,9 @@ static char* parse_images(WofiPropertyBox* box, const char* text, bool create_wi
|
||||
fprintf(stderr, "Image %s cannot be loaded\n", str);
|
||||
goto done;
|
||||
}
|
||||
int width = gdk_pixbuf_get_width(buf);
|
||||
int height = gdk_pixbuf_get_height(buf);
|
||||
if(height > width) {
|
||||
float percent = (float) image_size / height;
|
||||
GdkPixbuf* tmp = gdk_pixbuf_scale_simple(buf, width * percent, image_size, GDK_INTERP_BILINEAR);
|
||||
g_object_unref(buf);
|
||||
buf = tmp;
|
||||
} else {
|
||||
float percent = (float) image_size / width;
|
||||
GdkPixbuf* tmp = gdk_pixbuf_scale_simple(buf, image_size, height * percent, GDK_INTERP_BILINEAR);
|
||||
g_object_unref(buf);
|
||||
buf = tmp;
|
||||
}
|
||||
|
||||
buf = utils_g_resize_pixbuf(buf, image_size, GDK_INTERP_BILINEAR);
|
||||
|
||||
GtkWidget* img = gtk_image_new_from_pixbuf(buf);
|
||||
gtk_widget_set_name(img, "img");
|
||||
gtk_container_add(GTK_CONTAINER(box), img);
|
||||
@ -311,6 +304,27 @@ static char* parse_images(WofiPropertyBox* box, const char* text, bool create_wi
|
||||
GtkWidget* img = gtk_image_new_from_pixbuf(buf);
|
||||
gtk_widget_set_name(img, "img");
|
||||
gtk_container_add(GTK_CONTAINER(box), img);
|
||||
} else if(strcmp(mode, "img-base64") == 0 && create_widgets) {
|
||||
GdkPixbuf* buf = utils_g_pixbuf_from_base64(str);
|
||||
if(buf == NULL) {
|
||||
fprintf(stderr, "base64 image cannot be loaded\n");
|
||||
goto done;
|
||||
}
|
||||
|
||||
buf = utils_g_resize_pixbuf(buf, image_size, GDK_INTERP_BILINEAR);
|
||||
|
||||
GtkWidget* img = gtk_image_new_from_pixbuf(buf);
|
||||
gtk_widget_set_name(img, "img");
|
||||
gtk_container_add(GTK_CONTAINER(box), img);
|
||||
} else if(strcmp(mode, "img-base64-noscale") == 0 && create_widgets) {
|
||||
GdkPixbuf* buf = utils_g_pixbuf_from_base64(str);
|
||||
if(buf == NULL) {
|
||||
fprintf(stderr, "base64 image cannot be loaded\n");
|
||||
goto done;
|
||||
}
|
||||
GtkWidget* img = gtk_image_new_from_pixbuf(buf);
|
||||
gtk_widget_set_name(img, "img");
|
||||
gtk_container_add(GTK_CONTAINER(box), img);
|
||||
} else if(strcmp(mode, "text") == 0) {
|
||||
if(create_widgets) {
|
||||
GtkWidget* label = gtk_label_new(str);
|
||||
@ -855,8 +869,8 @@ void wofi_term_run(const char* cmd) {
|
||||
|
||||
static void flag_box(GtkBox* box, GtkStateFlags flags) {
|
||||
GList* selected_children = gtk_container_get_children(GTK_CONTAINER(box));
|
||||
GList* list = selected_children;
|
||||
for(GtkWidget* child = list->data; list != NULL; list = list->next) {
|
||||
for(GList* list = selected_children; list != NULL; list = list->next) {
|
||||
GtkWidget* child = list->data;
|
||||
gtk_widget_set_state_flags(child, flags, TRUE);
|
||||
}
|
||||
g_list_free(selected_children);
|
||||
|
Loading…
Reference in New Issue
Block a user