rofi 2.1.0
icon.c
Go to the documentation of this file.
1/*
2 * rofi
3 *
4 * MIT/X11 License
5 * Copyright © 2013-2018 Qball Cow <qball@gmpclient.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
29#include "cairo.h"
30#define G_LOG_DOMAIN "Widgets.Icon"
31#include "config.h"
32
33#include "theme.h"
34#include "widgets/icon.h"
36#include "widgets/widget.h"
37#include <stdio.h>
38
39#include "rofi-icon-fetcher.h"
40
41struct _icon {
43
44 // Size of the icon.
45 int size;
46
48
49 uint32_t icon_fetch_id;
50
51 double yalign, xalign;
52
53 // Source surface.
54 cairo_surface_t *icon;
55};
56
57static int icon_get_desired_height(widget *wid, G_GNUC_UNUSED const int width) {
58 icon *b = (icon *)wid;
59 int height = b->size;
60 if (b->squared == FALSE) {
61 if (b->icon) {
62 int iconh = cairo_image_surface_get_height(b->icon);
63 int iconw = cairo_image_surface_get_width(b->icon);
64 int icons = MAX(iconh, iconw);
65 double scale = (double)b->size / icons;
66 height = iconh * scale;
67 }
68 }
70 return height;
71}
72static int icon_get_desired_width(widget *wid, G_GNUC_UNUSED const int height) {
73 icon *b = (icon *)wid;
74 int width = b->size;
75 if (b->squared == FALSE) {
76 if (b->icon) {
77 int iconh = cairo_image_surface_get_height(b->icon);
78 int iconw = cairo_image_surface_get_width(b->icon);
79 int icons = MAX(iconh, iconw);
80 double scale = (double)b->size / icons;
81 width = iconw * scale;
82 }
83 }
85 return width;
86}
87
88static void icon_draw(widget *wid, cairo_t *draw) {
89 icon *b = (icon *)wid;
90 // If no icon is loaded. quit.
91 if (b->icon == NULL && b->icon_fetch_id > 0) {
93 if (b->icon) {
94 cairo_surface_reference(b->icon);
95 }
96 }
97 if (b->icon == NULL) {
98 return;
99 }
100 int iconh = cairo_image_surface_get_height(b->icon);
101 int iconw = cairo_image_surface_get_width(b->icon);
102 int lpad = widget_padding_get_left(WIDGET(b));
103 int rpad = widget_padding_get_right(WIDGET(b));
104 int tpad = widget_padding_get_top(WIDGET(b));
105 int bpad = widget_padding_get_bottom(WIDGET(b));
106 double scalex = (double)(b->widget.w - lpad - rpad) / iconw;
107 double scaley = (double)(b->widget.h - tpad - bpad) / iconh;
108 double scale = MIN(scalex, scaley);
109
110 cairo_save(draw);
111
112 cairo_translate(
113 draw, lpad + (b->widget.w - iconw * scale - lpad - rpad) * b->xalign,
114 tpad + (b->widget.h - iconh * scale - tpad - bpad) * b->yalign);
115 cairo_scale(draw, scale, scale);
116 if (rofi_theme_has_property(WIDGET(wid), P_COLOR, "tint")) {
117 cairo_pattern_t *pat = cairo_pattern_create_for_surface(b->icon);
118 cairo_set_source_rgb(draw, 0, 0, 0);
119 rofi_theme_get_color(WIDGET(wid), "tint", draw);
120 cairo_mask(draw, pat);
121 cairo_set_operator(draw, CAIRO_OPERATOR_HSL_LUMINOSITY);
122 cairo_pattern_destroy(pat);
123 }
124 cairo_set_source_surface(draw, b->icon, 0, 0);
125 cairo_paint(draw);
126 cairo_restore(draw);
127}
128
129static void icon_free(widget *wid) {
130 icon *b = (icon *)wid;
131
132 if (b->icon) {
133 cairo_surface_destroy(b->icon);
134 }
135
136 g_free(b);
137}
138
139static void icon_resize(widget *wid, short w, short h) {
140 icon *b = (icon *)wid;
141 if (b->widget.w != w || b->widget.h != h) {
142 b->widget.w = w;
143 b->widget.h = h;
144 widget_update(wid);
145 }
146}
147
148void icon_set_surface(icon *icon_widget, cairo_surface_t *surf) {
149 icon_widget->icon_fetch_id = 0;
150 if (icon_widget->icon) {
151 cairo_surface_destroy(icon_widget->icon);
152 icon_widget->icon = NULL;
153 }
154 if (surf) {
155 cairo_surface_reference(surf);
156 icon_widget->icon = surf;
157 }
158 widget_queue_redraw(WIDGET(icon_widget));
159}
160
161icon *icon_create(widget *parent, const char *name) {
162 icon *b = g_malloc0(sizeof(icon));
163
164 b->size = 16;
165 // Initialize widget.
166 widget_init(WIDGET(b), parent, WIDGET_TYPE_UNKNOWN, name);
167 b->widget.draw = icon_draw;
168 b->widget.free = icon_free;
172
175
176 b->squared = rofi_theme_get_boolean(WIDGET(b), "squared", TRUE);
177
178 const char *filename = rofi_theme_get_string(WIDGET(b), "filename", NULL);
179 if (filename) {
180 b->icon_fetch_id = rofi_icon_fetcher_query(filename, b->size);
181 }
182 b->yalign = rofi_theme_get_double(WIDGET(b), "vertical-align", 0.5);
183 b->yalign = MAX(0, MIN(1.0, b->yalign));
184 b->xalign = rofi_theme_get_double(WIDGET(b), "horizontal-align", 0.5);
185 b->xalign = MAX(0, MIN(1.0, b->xalign));
186
187 return b;
188}
cairo_surface_t * rofi_icon_fetcher_get(const uint32_t uid)
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
void icon_set_surface(icon *icon_widget, cairo_surface_t *surf)
Definition icon.c:148
icon * icon_create(widget *parent, const char *name)
Definition icon.c:161
struct _icon icon
Definition icon.h:44
void widget_queue_redraw(widget *wid)
Definition widget.c:487
struct _widget widget
Definition widget.h:49
void widget_update(widget *wid)
Definition widget.c:477
#define WIDGET(a)
Definition widget.h:117
@ WIDGET_TYPE_UNKNOWN
Definition widget.h:56
static int icon_get_desired_width(widget *wid, G_GNUC_UNUSED const int height)
Definition icon.c:72
static void icon_free(widget *wid)
Definition icon.c:129
static void icon_resize(widget *wid, short w, short h)
Definition icon.c:139
static int icon_get_desired_height(widget *wid, G_GNUC_UNUSED const int width)
Definition icon.c:57
static void icon_draw(widget *wid, cairo_t *draw)
Definition icon.c:88
@ P_COLOR
Definition rofi-types.h:20
@ ROFI_ORIENTATION_VERTICAL
Definition rofi-types.h:140
Definition icon.c:41
double xalign
Definition icon.c:51
int squared
Definition icon.c:47
widget widget
Definition icon.c:42
int size
Definition icon.c:45
cairo_surface_t * icon
Definition icon.c:54
uint32_t icon_fetch_id
Definition icon.c:49
double yalign
Definition icon.c:51
int(*) get_desired_height(struct _widget *, const int width)
void(*) draw(struct _widget *widget, cairo_t *draw)
void(*) free(struct _widget *widget)
int(*) get_desired_width(struct _widget *, const int height)
void(*) resize(struct _widget *, short, short)
int distance_get_pixel(RofiDistance d, RofiOrientation ori)
Definition theme.c:1404
double rofi_theme_get_double(const widget *wid, const char *property, double def)
Definition theme.c:1040
int rofi_theme_get_boolean(const widget *wid, const char *property, int def)
Definition theme.c:903
gboolean rofi_theme_has_property(const widget *wid_in, const PropertyType type, const char *property)
Definition theme.c:1653
RofiDistance rofi_theme_get_distance(const widget *wid, const char *property, int def)
Definition theme.c:877
void rofi_theme_get_color(const widget *wid, const char *property, cairo_t *d)
Definition theme.c:1067
const char * rofi_theme_get_string(const widget *wid, const char *property, const char *def)
Definition theme.c:989
void widget_init(widget *wid, widget *parent, WidgetType type, const char *name)
Definition widget.c:36
int widget_padding_get_padding_width(const widget *wid)
Definition widget.c:637
int widget_padding_get_left(const widget *wid)
Definition widget.c:576
int widget_padding_get_right(const widget *wid)
Definition widget.c:586
int widget_padding_get_padding_height(const widget *wid)
Definition widget.c:631
int widget_padding_get_top(const widget *wid)
Definition widget.c:598
int widget_padding_get_bottom(const widget *wid)
Definition widget.c:608