]> Dogcows Code - chaz/tint2/blob - src/launcher/launcher.c
fixed memleak
[chaz/tint2] / src / launcher / launcher.c
1 /**************************************************************************
2 * Tint2 : launcher
3 *
4 * Copyright (C) 2010 (mrovi@interfete-web-club.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 **************************************************************************/
18
19 #include <string.h>
20 #include <stdio.h>
21 #include <cairo.h>
22 #include <cairo-xlib.h>
23 #include <pango/pangocairo.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <stdlib.h>
27
28 #include "window.h"
29 #include "server.h"
30 #include "area.h"
31 #include "panel.h"
32 #include "taskbar.h"
33 #include "launcher.h"
34
35 int launcher_enabled;
36 int launcher_max_icon_size;
37
38
39 void default_launcher()
40 {
41 launcher_enabled = 0;
42 launcher_max_icon_size = 0;
43 }
44
45
46 void init_launcher()
47 {
48 }
49
50
51 void init_launcher_panel(void *p)
52 {
53 Panel *panel =(Panel*)p;
54 Launcher *launcher = &panel->launcher;
55
56 launcher->area.parent = p;
57 launcher->area.panel = p;
58 launcher->area._draw_foreground = draw_launcher;
59 launcher->area._resize = resize_launcher;
60 launcher->area.resize = 1;
61 launcher->area.redraw = 1;
62 launcher->area.on_screen = 1;
63
64 if (panel_horizontal) {
65 // panel horizonal => fixed height and posy
66 launcher->area.posy = panel->area.bg->border.width + panel->area.paddingy;
67 launcher->area.height = panel->area.height - (2 * launcher->area.posy);
68 }
69 else {
70 // panel vertical => fixed width, height, posy and posx
71 launcher->area.posx = panel->area.bg->border.width + panel->area.paddingxlr;
72 launcher->area.width = panel->area.width - (2 * panel->area.bg->border.width) - (2 * panel->area.paddingy);
73 }
74
75 // Load icons
76 {
77 GSList* path = launcher->list_icon_paths;
78 GSList* cmd = launcher->list_cmds;
79 while (path != NULL && cmd != NULL)
80 {
81 LauncherIcon *launcherIcon = malloc(sizeof(LauncherIcon));
82 launcherIcon->icon = imlib_load_image(path->data);
83 launcherIcon->cmd = strdup(cmd->data);
84 launcher->list_icons = g_slist_append(launcher->list_icons, launcherIcon);
85 path = g_slist_next(path);
86 cmd = g_slist_next(cmd);
87 }
88 }
89 }
90
91
92 void cleanup_launcher()
93 {
94 int i;
95
96 for (i = 0 ; i < nb_panel ; i++) {
97 Panel *panel = &panel1[i];
98 Launcher *launcher = &panel->launcher;
99 free_area(&launcher->area);
100 GSList *l;
101 for (l = launcher->list_icons; l ; l = l->next) {
102 LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
103 if (launcherIcon && launcherIcon->icon) {
104 imlib_context_set_image (launcherIcon->icon);
105 imlib_free_image();
106 }
107 free(launcherIcon->cmd);
108 free(launcherIcon);
109 }
110 g_slist_free(launcher->list_icons);
111 for (l = launcher->list_icon_paths; l ; l = l->next) {
112 free(l->data);
113 }
114 g_slist_free(launcher->list_icon_paths);
115 for (l = launcher->list_cmds; l ; l = l->next) {
116 free(l->data);
117 }
118 g_slist_free(launcher->list_cmds);
119 }
120 launcher_enabled = 0;
121 }
122
123 void resize_launcher(void *obj)
124 {
125 Launcher *launcher = obj;
126 Panel *panel = launcher->area.panel;
127 GSList *l;
128 int count, icon_size;
129 int icons_per_column=1, icons_per_row=1, marging=0;
130
131 if (panel_horizontal)
132 icon_size = launcher->area.height;
133 else
134 icon_size = launcher->area.width;
135 icon_size = icon_size - (2 * launcher->area.bg->border.width) - (2 * launcher->area.paddingy);
136 if (launcher_max_icon_size > 0 && icon_size > launcher_max_icon_size)
137 icon_size = launcher_max_icon_size;
138
139 count = 0;
140 for (l = launcher->list_icons; l ; l = l->next) {
141 count++;
142 }
143
144 if (panel_horizontal) {
145 if (!count) launcher->area.width = 0;
146 else {
147 int height = launcher->area.height - 2*launcher->area.bg->border.width - 2*launcher->area.paddingy;
148 // here icons_per_column always higher than 0
149 icons_per_column = (height+launcher->area.paddingx) / (icon_size+launcher->area.paddingx);
150 marging = height - (icons_per_column-1)*(icon_size+launcher->area.paddingx) - icon_size;
151 icons_per_row = count / icons_per_column + (count%icons_per_column != 0);
152 launcher->area.width = (2 * launcher->area.bg->border.width) + (2 * launcher->area.paddingxlr) + (icon_size * icons_per_row) + ((icons_per_row-1) * launcher->area.paddingx);
153 }
154
155 launcher->area.posx = panel->area.bg->border.width + panel->area.paddingxlr;
156 launcher->area.posy = panel->area.bg->border.width;
157 }
158 else {
159 if (!count) launcher->area.height = 0;
160 else {
161 int width = launcher->area.width - 2*launcher->area.bg->border.width - 2*launcher->area.paddingy;
162 // here icons_per_row always higher than 0
163 icons_per_row = (width+launcher->area.paddingx) / (icon_size+launcher->area.paddingx);
164 marging = width - (icons_per_row-1)*(icon_size+launcher->area.paddingx) - icon_size;
165 icons_per_column = count / icons_per_row+ (count%icons_per_row != 0);
166 launcher->area.height = (2 * launcher->area.bg->border.width) + (2 * launcher->area.paddingxlr) + (icon_size * icons_per_column) + ((icons_per_column-1) * launcher->area.paddingx);
167 }
168
169 launcher->area.posx = panel->area.bg->border.width;
170 launcher->area.posy = panel->area.height - panel->area.bg->border.width - panel->area.paddingxlr - launcher->area.height;
171 }
172
173 int i, posx, posy;
174 int start = launcher->area.bg->border.width + launcher->area.paddingy;// +marging/2;
175 if (panel_horizontal) {
176 posy = start;
177 posx = launcher->area.bg->border.width + launcher->area.paddingxlr;
178 }
179 else {
180 posx = start;
181 posy = launcher->area.bg->border.width + launcher->area.paddingxlr;
182 }
183
184 for (i=1, l = launcher->list_icons; l ; i++, l = l->next) {
185 LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
186
187 launcherIcon->y = posy;
188 launcherIcon->x = posx;
189 launcherIcon->width = icon_size;
190 launcherIcon->height = icon_size;
191 if (panel_horizontal) {
192 if (i % icons_per_column)
193 posy += icon_size + launcher->area.paddingx;
194 else {
195 posy = start;
196 posx += (icon_size + launcher->area.paddingx);
197 }
198 }
199 else {
200 if (i % icons_per_row)
201 posx += icon_size + launcher->area.paddingx;
202 else {
203 posx = start;
204 posy += (icon_size + launcher->area.paddingx);
205 }
206 }
207 }
208
209 // resize force the redraw
210 launcher->area.redraw = 1;
211 }
212
213
214 void draw_launcher(void *obj, cairo_t *c)
215 {
216 Launcher *launcher = obj;
217 GSList *l;
218 if (launcher->list_icons == 0) return;
219
220 for (l = launcher->list_icons; l ; l = l->next) {
221 LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
222 int pos_x = launcherIcon->x;
223 int pos_y = launcherIcon->y;
224 int width = launcherIcon->width;
225 int height = launcherIcon->height;
226 Imlib_Image icon_scaled;
227 if (launcherIcon->icon) {
228 imlib_context_set_image (launcherIcon->icon);
229 icon_scaled = imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), width, height);
230 } else {
231 icon_scaled = imlib_create_image(width, height);
232 imlib_context_set_image (icon_scaled);
233 imlib_context_set_color(255, 255, 255, 255);
234 imlib_image_fill_rectangle(0, 0, width, height);
235 }
236
237 // Render
238 imlib_context_set_image (icon_scaled);
239 if (server.real_transparency) {
240 render_image(launcher->area.pix, pos_x, pos_y, imlib_image_get_width(), imlib_image_get_height() );
241 }
242 else {
243 imlib_context_set_drawable(launcher->area.pix);
244 imlib_render_image_on_drawable (pos_x, pos_y);
245 }
246 imlib_free_image();
247 }
248 }
249
250 void launcher_action(LauncherIcon *icon)
251 {
252 char *cmd = malloc(strlen(icon->cmd) + 10);
253 sprintf(cmd, "(%s&)", icon->cmd);
254 tint_exec(cmd);
255 free(cmd);
256 }
257
This page took 0.046683 seconds and 5 git commands to generate.