rofi  1.5.2
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 #define G_LOG_DOMAIN "Widgets.Icon"
30 
31 #include <config.h>
32 #include <stdio.h>
33 #include "widgets/widget.h"
35 #include "widgets/icon.h"
36 #include "theme.h"
37 
38 #include "rofi-icon-fetcher.h"
39 
40 struct _icon
41 {
43 
44  // Size of the icon.
45  int size;
46 
47  uint32_t icon_fetch_id;
48 
49  // Source surface.
50  cairo_surface_t *icon;
51 };
52 
54 {
55  icon *b = (icon *) widget;
56  int height = b->size;
58  return height;
59 }
61 {
62  icon *b = (icon *) widget;
63  int width = b->size;
65  return width;
66 }
67 
68 static void icon_draw ( widget *wid, cairo_t *draw )
69 {
70  icon *b = (icon *) wid;
71  // If no icon is loaded. quit.
72  if ( b->icon == NULL && b->icon_fetch_id > 0 ) {
74  if ( b->icon ) {
75  cairo_surface_reference ( b->icon );
76  }
77  }
78  if ( b->icon == NULL ) {
79  return;
80  }
81  int iconh = cairo_image_surface_get_height ( b->icon );
82  int iconw = cairo_image_surface_get_width ( b->icon );
83  int icons = MAX ( iconh, iconw );
84  double scale = (double) b->size / icons;
85 
86  cairo_save ( draw );
87 
88  cairo_translate ( draw, ( b->size - iconw * scale ) / 2.0, ( b->size - iconh * scale ) / 2.0 );
89  cairo_scale ( draw, scale, scale );
90  cairo_set_source_surface ( draw, b->icon, 0, 0 );
91  cairo_paint ( draw );
92  cairo_restore ( draw );
93 }
94 
95 static void icon_free ( widget *wid )
96 {
97  icon *b = (icon *) wid;
98 
99  if ( b->icon ) {
100  cairo_surface_destroy ( b->icon );
101  }
102 
103  g_free ( b );
104 }
105 
106 static void icon_resize ( widget *widget, short w, short h )
107 {
108  icon *b = (icon *) widget;
109  if ( b->widget.w != w || b->widget.h != h ) {
110  b->widget.w = w;
111  b->widget.h = h;
112  widget_update ( widget );
113  }
114 }
115 
116 void icon_set_surface ( icon *icon, cairo_surface_t *surf )
117 {
118  icon->icon_fetch_id = 0;
119  if ( icon->icon ) {
120  cairo_surface_destroy ( icon->icon );
121  icon->icon = NULL;
122  }
123  if ( surf ) {
124  cairo_surface_reference ( surf );
125  icon->icon = surf;
126  }
128 }
129 
130 icon * icon_create ( widget *parent, const char *name )
131 {
132  icon *b = g_malloc0 ( sizeof ( icon ) );
133 
134  b->size = 16;
135  // Initialize widget.
136  widget_init ( WIDGET ( b ), parent, WIDGET_TYPE_UNKNOWN, name );
137  b->widget.draw = icon_draw;
138  b->widget.free = icon_free;
142 
143  b->size = rofi_theme_get_integer ( WIDGET ( b ), "size", b->size );
144 
145  const char * filename = rofi_theme_get_string ( WIDGET ( b ), "filename", NULL );
146  if ( filename ) {
147  b->icon_fetch_id = rofi_icon_fetcher_query ( filename, b->size );
148  }
149 
150  return b;
151 }
static int icon_get_desired_width(widget *widget)
Definition: icon.c:60
void widget_update(widget *widget)
Definition: widget.c:422
int widget_padding_get_padding_width(const widget *wid)
Definition: widget.c:559
void(* resize)(struct _widget *, short, short)
const char * rofi_theme_get_string(const widget *widget, const char *property, const char *def)
Definition: theme.c:605
void icon_set_surface(icon *icon, cairo_surface_t *surf)
Definition: icon.c:116
void(* draw)(struct _widget *widget, cairo_t *draw)
int(* get_desired_width)(struct _widget *)
static void icon_free(widget *wid)
Definition: icon.c:95
cairo_surface_t * rofi_icon_fetcher_get(const uint32_t uid)
uint32_t icon_fetch_id
Definition: icon.c:47
widget widget
Definition: icon.c:42
Definition: icon.c:40
void widget_queue_redraw(widget *wid)
Definition: widget.c:432
cairo_surface_t * icon
Definition: icon.c:50
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
icon * icon_create(widget *parent, const char *name)
Definition: icon.c:130
int size
Definition: icon.c:45
void(* free)(struct _widget *widget)
#define WIDGET(a)
Definition: widget.h:115
void widget_init(widget *wid, widget *parent, WidgetType type, const char *name)
Definition: widget.c:37
static int icon_get_desired_height(widget *widget)
Definition: icon.c:53
static void icon_resize(widget *widget, short w, short h)
Definition: icon.c:106
int(* get_desired_height)(struct _widget *)
int widget_padding_get_padding_height(const widget *wid)
Definition: widget.c:552
int rofi_theme_get_integer(const widget *widget, const char *property, int def)
Definition: theme.c:534
static void icon_draw(widget *wid, cairo_t *draw)
Definition: icon.c:68