rofi  1.5.4
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  double yalign,xalign;
50 
51  // Source surface.
52  cairo_surface_t *icon;
53 };
54 
56 {
57  icon *b = (icon *) widget;
58  int height = b->size;
60  return height;
61 }
63 {
64  icon *b = (icon *) widget;
65  int width = b->size;
67  return width;
68 }
69 
70 static void icon_draw ( widget *wid, cairo_t *draw )
71 {
72  icon *b = (icon *) wid;
73  // If no icon is loaded. quit.
74  if ( b->icon == NULL && b->icon_fetch_id > 0 ) {
76  if ( b->icon ) {
77  cairo_surface_reference ( b->icon );
78  }
79  }
80  if ( b->icon == NULL ) {
81  return;
82  }
83  int iconh = cairo_image_surface_get_height ( b->icon );
84  int iconw = cairo_image_surface_get_width ( b->icon );
85  int icons = MAX ( iconh, iconw );
86  double scale = (double) b->size / icons;
87 
88  int lpad = widget_padding_get_left ( WIDGET ( b ) ) ;
89  int rpad = widget_padding_get_right ( WIDGET ( b ) ) ;
90  int tpad = widget_padding_get_top ( WIDGET ( b ) ) ;
91  int bpad = widget_padding_get_bottom ( WIDGET ( b ) ) ;
92 
93  cairo_save ( draw );
94 
95  cairo_translate ( draw,
96  lpad + ( b->widget.w - iconw * scale - lpad -rpad )*b->xalign,
97  tpad + ( b->widget.h- iconh * scale -tpad - bpad )*b->yalign );
98  cairo_scale ( draw, scale, scale );
99  cairo_set_source_surface ( draw, b->icon, 0, 0 );
100  cairo_paint ( draw );
101  cairo_restore ( draw );
102 }
103 
104 static void icon_free ( widget *wid )
105 {
106  icon *b = (icon *) wid;
107 
108  if ( b->icon ) {
109  cairo_surface_destroy ( b->icon );
110  }
111 
112  g_free ( b );
113 }
114 
115 static void icon_resize ( widget *widget, short w, short h )
116 {
117  icon *b = (icon *) widget;
118  if ( b->widget.w != w || b->widget.h != h ) {
119  b->widget.w = w;
120  b->widget.h = h;
121  widget_update ( widget );
122  }
123 }
124 
125 void icon_set_surface ( icon *icon, cairo_surface_t *surf )
126 {
127  icon->icon_fetch_id = 0;
128  if ( icon->icon ) {
129  cairo_surface_destroy ( icon->icon );
130  icon->icon = NULL;
131  }
132  if ( surf ) {
133  cairo_surface_reference ( surf );
134  icon->icon = surf;
135  }
137 }
138 
139 icon * icon_create ( widget *parent, const char *name )
140 {
141  icon *b = g_malloc0 ( sizeof ( icon ) );
142 
143  b->size = 16;
144  // Initialize widget.
145  widget_init ( WIDGET ( b ), parent, WIDGET_TYPE_UNKNOWN, name );
146  b->widget.draw = icon_draw;
147  b->widget.free = icon_free;
151 
152  b->size = rofi_theme_get_integer ( WIDGET ( b ), "size", b->size );
153 
154  const char * filename = rofi_theme_get_string ( WIDGET ( b ), "filename", NULL );
155  if ( filename ) {
156  b->icon_fetch_id = rofi_icon_fetcher_query ( filename, b->size );
157  }
158  b->yalign = rofi_theme_get_double ( WIDGET ( b ), "vertical-align", 0.5 );
159  b->yalign = MAX ( 0, MIN ( 1.0, b->yalign ) );
160  b->xalign = rofi_theme_get_double ( WIDGET ( b ), "horizontal-align", 0.5 );
161  b->xalign = MAX ( 0, MIN ( 1.0, b->xalign ) );
162 
163  return b;
164 }
WIDGET
#define WIDGET(a)
Definition: widget.h:115
_icon::icon
cairo_surface_t * icon
Definition: icon.c:52
WIDGET_TYPE_UNKNOWN
@ WIDGET_TYPE_UNKNOWN
Definition: widget.h:59
_icon::xalign
double xalign
Definition: icon.c:49
rofi_theme_get_double
double rofi_theme_get_double(const widget *widget, const char *property, double def)
Definition: theme.c:650
_widget::get_desired_width
int(* get_desired_width)(struct _widget *)
Definition: widget-internal.h:82
_widget::get_desired_height
int(* get_desired_height)(struct _widget *)
Definition: widget-internal.h:81
_icon
Definition: icon.c:41
widget_padding_get_bottom
int widget_padding_get_bottom(const widget *wid)
Definition: widget.c:527
rofi_theme_get_integer
int rofi_theme_get_integer(const widget *widget, const char *property, int def)
Definition: theme.c:563
widget_padding_get_right
int widget_padding_get_right(const widget *wid)
Definition: widget.c:507
widget_update
void widget_update(widget *widget)
Definition: widget.c:422
icon_free
static void icon_free(widget *wid)
Definition: icon.c:104
icon_resize
static void icon_resize(widget *widget, short w, short h)
Definition: icon.c:115
_widget::draw
void(* draw)(struct _widget *widget, cairo_t *draw)
Definition: widget-internal.h:72
icon_set_surface
void icon_set_surface(icon *icon, cairo_surface_t *surf)
Definition: icon.c:125
theme.h
_widget::free
void(* free)(struct _widget *widget)
Definition: widget-internal.h:92
_icon::yalign
double yalign
Definition: icon.c:49
widget_queue_redraw
void widget_queue_redraw(widget *wid)
Definition: widget.c:432
_icon::size
int size
Definition: icon.c:45
icon_create
icon * icon_create(widget *parent, const char *name)
Definition: icon.c:139
widget.h
rofi_icon_fetcher_get
cairo_surface_t * rofi_icon_fetcher_get(const uint32_t uid)
Definition: rofi-icon-fetcher.c:216
widget_padding_get_top
int widget_padding_get_top(const widget *wid)
Definition: widget.c:517
rofi_theme_get_string
const char * rofi_theme_get_string(const widget *widget, const char *property, const char *def)
Definition: theme.c:634
icon.h
_icon::icon_fetch_id
uint32_t icon_fetch_id
Definition: icon.c:47
_widget::h
short h
Definition: widget-internal.h:46
widget_init
void widget_init(widget *wid, widget *parent, WidgetType type, const char *name)
Definition: widget.c:37
_widget::w
short w
Definition: widget-internal.h:44
icon_get_desired_height
static int icon_get_desired_height(widget *widget)
Definition: icon.c:55
widget-internal.h
rofi_icon_fetcher_query
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
Definition: rofi-icon-fetcher.c:182
icon_get_desired_width
static int icon_get_desired_width(widget *widget)
Definition: icon.c:62
rofi-icon-fetcher.h
widget_padding_get_left
int widget_padding_get_left(const widget *wid)
Definition: widget.c:497
_icon::widget
widget widget
Definition: icon.c:42
_widget::resize
void(* resize)(struct _widget *, short, short)
Definition: widget-internal.h:74
icon_draw
static void icon_draw(widget *wid, cairo_t *draw)
Definition: icon.c:70
widget_padding_get_padding_width
int widget_padding_get_padding_width(const widget *wid)
Definition: widget.c:559
widget_padding_get_padding_height
int widget_padding_get_padding_height(const widget *wid)
Definition: widget.c:552
_widget
Definition: widget-internal.h:36