]> Dogcows Code - chaz/tint2/blob - src/launcher/launcher.c
fixed Issue 282
[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.size_mode = SIZE_BY_CONTENT;
60 launcher->area._resize = resize_launcher;
61 launcher->area.resize = 1;
62 launcher->area.redraw = 1;
63 launcher->area.on_screen = 1;
64
65 if (panel_horizontal) {
66 // panel horizonal => fixed height and posy
67 launcher->area.posy = panel->area.bg->border.width + panel->area.paddingy;
68 launcher->area.height = panel->area.height - (2 * launcher->area.posy);
69 }
70 else {
71 // panel vertical => fixed width, height, posy and posx
72 launcher->area.posx = panel->area.bg->border.width + panel->area.paddingxlr;
73 launcher->area.width = panel->area.width - (2 * panel->area.bg->border.width) - (2 * panel->area.paddingy);
74 }
75
76 // Load icons
77 {
78 GSList* path = launcher->list_icon_paths;
79 GSList* cmd = launcher->list_cmds;
80 while (path != NULL && cmd != NULL)
81 {
82 LauncherIcon *launcherIcon = malloc(sizeof(LauncherIcon));
83 launcherIcon->icon = imlib_load_image(path->data);
84 launcherIcon->cmd = strdup(cmd->data);
85 launcher->list_icons = g_slist_append(launcher->list_icons, launcherIcon);
86 path = g_slist_next(path);
87 cmd = g_slist_next(cmd);
88 }
89 }
90 }
91
92
93 void cleanup_launcher()
94 {
95 int i;
96
97 for (i = 0 ; i < nb_panel ; i++) {
98 Panel *panel = &panel1[i];
99 Launcher *launcher = &panel->launcher;
100 free_area(&launcher->area);
101 GSList *l;
102 for (l = launcher->list_icons; l ; l = l->next) {
103 LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
104 if (launcherIcon && launcherIcon->icon) {
105 imlib_context_set_image (launcherIcon->icon);
106 imlib_free_image();
107 }
108 free(launcherIcon->cmd);
109 free(launcherIcon);
110 }
111 g_slist_free(launcher->list_icons);
112 for (l = launcher->list_icon_paths; l ; l = l->next) {
113 free(l->data);
114 }
115 g_slist_free(launcher->list_icon_paths);
116 for (l = launcher->list_cmds; l ; l = l->next) {
117 free(l->data);
118 }
119 g_slist_free(launcher->list_cmds);
120 }
121 launcher_enabled = 0;
122 }
123
124 void resize_launcher(void *obj)
125 {
126 Launcher *launcher = obj;
127 Panel *panel = launcher->area.panel;
128 GSList *l;
129 int count, icon_size;
130 int icons_per_column=1, icons_per_row=1, marging=0;
131
132 if (panel_horizontal)
133 icon_size = launcher->area.height;
134 else
135 icon_size = launcher->area.width;
136 icon_size = icon_size - (2 * launcher->area.bg->border.width) - (2 * launcher->area.paddingy);
137 if (launcher_max_icon_size > 0 && icon_size > launcher_max_icon_size)
138 icon_size = launcher_max_icon_size;
139
140 count = 0;
141 for (l = launcher->list_icons; l ; l = l->next) {
142 count++;
143 }
144
145 if (panel_horizontal) {
146 if (!count) launcher->area.width = 0;
147 else {
148 int height = launcher->area.height - 2*launcher->area.bg->border.width - 2*launcher->area.paddingy;
149 // here icons_per_column always higher than 0
150 icons_per_column = (height+launcher->area.paddingx) / (icon_size+launcher->area.paddingx);
151 marging = height - (icons_per_column-1)*(icon_size+launcher->area.paddingx) - icon_size;
152 icons_per_row = count / icons_per_column + (count%icons_per_column != 0);
153 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);
154 }
155
156 launcher->area.posx = panel->area.bg->border.width + panel->area.paddingxlr;
157 launcher->area.posy = panel->area.bg->border.width;
158 }
159 else {
160 if (!count) launcher->area.height = 0;
161 else {
162 int width = launcher->area.width - 2*launcher->area.bg->border.width - 2*launcher->area.paddingy;
163 // here icons_per_row always higher than 0
164 icons_per_row = (width+launcher->area.paddingx) / (icon_size+launcher->area.paddingx);
165 marging = width - (icons_per_row-1)*(icon_size+launcher->area.paddingx) - icon_size;
166 icons_per_column = count / icons_per_row+ (count%icons_per_row != 0);
167 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);
168 }
169
170 launcher->area.posx = panel->area.bg->border.width;
171 launcher->area.posy = panel->area.height - panel->area.bg->border.width - panel->area.paddingxlr - launcher->area.height;
172 }
173
174 int i, posx, posy;
175 int start = launcher->area.bg->border.width + launcher->area.paddingy;// +marging/2;
176 if (panel_horizontal) {
177 posy = start;
178 posx = launcher->area.bg->border.width + launcher->area.paddingxlr;
179 }
180 else {
181 posx = start;
182 posy = launcher->area.bg->border.width + launcher->area.paddingxlr;
183 }
184
185 for (i=1, l = launcher->list_icons; l ; i++, l = l->next) {
186 LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
187
188 launcherIcon->y = posy;
189 launcherIcon->x = posx;
190 launcherIcon->width = icon_size;
191 launcherIcon->height = icon_size;
192 if (panel_horizontal) {
193 if (i % icons_per_column)
194 posy += icon_size + launcher->area.paddingx;
195 else {
196 posy = start;
197 posx += (icon_size + launcher->area.paddingx);
198 }
199 }
200 else {
201 if (i % icons_per_row)
202 posx += icon_size + launcher->area.paddingx;
203 else {
204 posx = start;
205 posy += (icon_size + launcher->area.paddingx);
206 }
207 }
208 }
209
210 // resize force the redraw
211 launcher->area.redraw = 1;
212 }
213
214
215 void draw_launcher(void *obj, cairo_t *c)
216 {
217 Launcher *launcher = obj;
218 GSList *l;
219 if (launcher->list_icons == 0) return;
220
221 for (l = launcher->list_icons; l ; l = l->next) {
222 LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
223 int pos_x = launcherIcon->x;
224 int pos_y = launcherIcon->y;
225 int width = launcherIcon->width;
226 int height = launcherIcon->height;
227 Imlib_Image icon_scaled;
228 if (launcherIcon->icon) {
229 imlib_context_set_image (launcherIcon->icon);
230 icon_scaled = imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), width, height);
231 } else {
232 icon_scaled = imlib_create_image(width, height);
233 imlib_context_set_image (icon_scaled);
234 imlib_context_set_color(255, 255, 255, 255);
235 imlib_image_fill_rectangle(0, 0, width, height);
236 }
237
238 // Render
239 imlib_context_set_image (icon_scaled);
240 if (server.real_transparency) {
241 render_image(launcher->area.pix, pos_x, pos_y, imlib_image_get_width(), imlib_image_get_height() );
242 }
243 else {
244 imlib_context_set_drawable(launcher->area.pix);
245 imlib_render_image_on_drawable (pos_x, pos_y);
246 }
247 imlib_free_image();
248 }
249 }
250
251 void launcher_action(LauncherIcon *icon)
252 {
253 char *cmd = malloc(strlen(icon->cmd) + 10);
254 sprintf(cmd, "(%s&)", icon->cmd);
255 tint_exec(cmd);
256 free(cmd);
257 }
258
This page took 0.047466 seconds and 5 git commands to generate.