Mercurial > audlegacy
annotate libaudacious/titlestring.c @ 1963:8cca76c8f5c3 trunk
[svn] - show_newui_window() implementation
author | nenolod |
---|---|
date | Sun, 12 Nov 2006 02:37:32 -0800 |
parents | fda280358660 |
children | 90cc014f8fdc |
rev | line source |
---|---|
0 | 1 /* |
2 * Copyright (C) 2001, Espen Skoglund <esk@ira.uka.de> | |
3 * Copyright (C) 2001, Haavard Kvaalen <havardk@xmms.org> | |
4 * | |
5 * This program is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU General Public License | |
7 * as published by the Free Software Foundation; either version 2 | |
8 * of the License, or (at your option) any later version. | |
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 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
1458
f12d7e208b43
[svn] Update FSF address in copyright notices. Update autotools templates.
chainsaw
parents:
1350
diff
changeset
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
1459 | 18 * 02110-1301, USA. |
0 | 19 * |
20 */ | |
21 | |
22 #ifdef HAVE_CONFIG_H | |
23 # include "config.h" | |
24 #endif | |
25 | |
751 | 26 #define GETTEXT_PACKAGE PACKAGE_NAME |
0 | 27 |
28 #include <glib.h> | |
29 #include <glib/gi18n-lib.h> | |
30 #include <gtk/gtk.h> | |
31 #include <stdio.h> | |
32 #include <string.h> | |
33 | |
34 #include "titlestring.h" | |
35 | |
36 #define CHECK(input, field) \ | |
37 (((gchar *) &input->field - (gchar *) input) < input->__size) | |
38 | |
39 #define VS(input, field) (CHECK(input, field) ? input->field : NULL) | |
40 #define VI(input, field) (CHECK(input, field) ? input->field : 0) | |
41 | |
42 | |
43 BmpTitleInput * | |
44 bmp_title_input_new() | |
45 { | |
46 BmpTitleInput *input; | |
47 input = g_new0(BmpTitleInput, 1); | |
48 input->__size = XMMS_TITLEINPUT_SIZE; | |
49 input->__version = XMMS_TITLEINPUT_VERSION; | |
50 return input; | |
51 } | |
52 | |
53 void | |
54 bmp_title_input_free(BmpTitleInput * input) | |
55 { | |
1350
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
56 if (input == NULL) |
0 | 57 return; |
58 | |
1350
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
59 if (input->performer != NULL) |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
60 g_free(input->performer); |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
61 |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
62 if (input->album_name != NULL) |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
63 g_free(input->album_name); |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
64 |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
65 if (input->track_name != NULL) |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
66 g_free(input->track_name); |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
67 |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
68 if (input->date != NULL) |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
69 g_free(input->date); |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
70 |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
71 if (input->genre != NULL) |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
72 g_free(input->genre); |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
73 |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
74 if (input->comment != NULL) |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
75 g_free(input->comment); |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
76 |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
77 if (input->file_name != NULL) |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
78 g_free(input->file_name); |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
79 |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
80 if (input->file_path != NULL) |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
81 g_free(input->file_path); |
ca5d03c4b3f1
[svn] - extra sanity checking keeps the double-free away
nenolod
parents:
751
diff
changeset
|
82 |
0 | 83 g_free(input); |
84 } | |
85 | |
86 gchar * | |
87 xmms_get_titlestring(const gchar * fmt, TitleInput * input) | |
88 { | |
89 GString *outstr; | |
90 const gchar *string; | |
91 gchar c, convert[16]; | |
92 gint numdigits, numpr, val, i; | |
93 gint f_left, f_space, f_zero, someflag, width, precision; | |
94 gboolean did_output = FALSE; | |
95 gchar digits[] = "0123456789"; | |
96 | |
97 #define PUTCH(ch) g_string_append_c(outstr, ch) | |
98 | |
99 #define LEFTPAD(num) \ | |
100 G_STMT_START { \ | |
101 gint cnt = (num); \ | |
102 if ( ! f_left && cnt > 0 ) \ | |
103 while ( cnt-- > 0 ) \ | |
104 PUTCH(f_zero ? '0' : ' '); \ | |
105 } G_STMT_END; | |
106 | |
107 #define RIGHTPAD(num) \ | |
108 G_STMT_START { \ | |
109 gint cnt = (num); \ | |
110 if ( f_left && cnt > 0 ) \ | |
111 while ( cnt-- > 0 ) \ | |
112 PUTCH( ' ' ); \ | |
113 } G_STMT_END; | |
114 | |
1658 | 115 if (fmt == NULL || input == NULL) |
0 | 116 return NULL; |
117 outstr = g_string_new(""); | |
118 | |
119 for (;;) { | |
120 /* Copy characters until we encounter '%'. */ | |
121 while ((c = *fmt++) != '%') { | |
122 if (c == '\0') | |
123 goto Done; | |
124 g_string_append_c(outstr, c); | |
125 } | |
126 | |
127 f_left = f_space = f_zero = 0; | |
128 someflag = 1; | |
129 | |
130 | |
131 /* Parse flags. */ | |
132 while (someflag) { | |
133 switch (*fmt) { | |
134 case '-': | |
135 f_left = 1; | |
136 fmt++; | |
137 break; | |
138 case ' ': | |
139 f_space = 1; | |
140 fmt++; | |
141 break; | |
142 case '0': | |
143 f_zero = 1; | |
144 fmt++; | |
145 break; | |
146 default: | |
147 someflag = 0; | |
148 break; | |
149 } | |
150 } | |
151 | |
152 | |
153 /* Parse field width. */ | |
154 if ((c = *fmt) >= '0' && c <= '9') { | |
155 width = 0; | |
156 while ((c = *fmt++) >= '0' && c <= '9') { | |
157 width *= 10; | |
158 width += c - '0'; | |
159 } | |
160 fmt--; | |
161 } | |
162 else | |
163 width = -1; | |
164 | |
165 | |
166 /* Parse precision. */ | |
167 if (*fmt == '.') { | |
168 if ((c = *++fmt) >= '0' && c <= '9') { | |
169 precision = 0; | |
170 while ((c = *fmt++) >= '0' && c <= '9') { | |
171 precision *= 10; | |
172 precision += c - '0'; | |
173 } | |
174 fmt--; | |
175 } | |
176 else | |
177 precision = -1; | |
178 } | |
179 else | |
180 precision = -1; | |
181 | |
182 | |
183 /* Parse format conversion. */ | |
184 switch (c = *fmt++) { | |
185 case '}': /* close optional, just ignore */ | |
186 continue; | |
187 | |
188 case '{':{ /* optional entry: %{n:...%} */ | |
189 char n = *fmt++; | |
190 if (!((n == 'a' && VS(input, album_name)) || | |
191 (n == 'c' && VS(input, comment)) || | |
192 (n == 'd' && VS(input, date)) || | |
193 (n == 'e' && VS(input, file_ext)) || | |
194 (n == 'f' && VS(input, file_name)) || | |
195 (n == 'F' && VS(input, file_path)) || | |
196 (n == 'g' && VS(input, genre)) || | |
197 (n == 'n' && VI(input, track_number)) || | |
198 (n == 'p' && VS(input, performer)) || | |
199 (n == 't' && VS(input, track_name)))) { | |
200 int nl = 0; | |
201 char c; | |
202 while ((c = *fmt++)) /* until end of string */ | |
203 if (c == '}') /* if end of opt */ | |
204 if (!nl) | |
205 break; /* if outmost indent level */ | |
206 else | |
207 --nl; /* else reduce indent */ | |
208 else if (c == '{') | |
209 ++nl; /* increase indent */ | |
210 } | |
211 else | |
212 ++fmt; | |
213 break; | |
214 } | |
215 | |
216 case 'a': | |
217 string = VS(input, album_name); | |
218 goto Print_string; | |
219 case 'c': | |
220 string = VS(input, comment); | |
221 goto Print_string; | |
222 case 'd': | |
223 string = VS(input, date); | |
224 goto Print_string; | |
225 case 'e': | |
226 string = VS(input, file_ext); | |
227 goto Print_string; | |
228 case 'f': | |
229 string = VS(input, file_name); | |
230 goto Print_string; | |
231 case 'F': | |
232 string = VS(input, file_path); | |
233 goto Print_string; | |
234 case 'g': | |
235 string = VS(input, genre); | |
236 goto Print_string; | |
237 case 'n': | |
238 val = VI(input, track_number); | |
239 goto Print_number; | |
240 case 'p': | |
241 string = VS(input, performer); | |
242 goto Print_string; | |
243 case 't': | |
244 string = VS(input, track_name); | |
245 | |
246 Print_string: | |
247 if (string == NULL) | |
248 break; | |
249 did_output = TRUE; | |
250 | |
251 numpr = 0; | |
252 if (width > 0) { | |
253 /* Calculate printed size. */ | |
254 numpr = strlen(string); | |
255 if (precision >= 0 && precision < numpr) | |
256 numpr = precision; | |
257 | |
258 LEFTPAD(width - numpr); | |
259 } | |
260 | |
261 /* Insert string. */ | |
262 if (precision >= 0) { | |
1594
44f67f556b60
[svn] - precision in title format is regarded as character count, not byte count.
yaz
parents:
1459
diff
changeset
|
263 glong offset_max = precision, offset; |
44f67f556b60
[svn] - precision in title format is regarded as character count, not byte count.
yaz
parents:
1459
diff
changeset
|
264 gchar *uptr = NULL; |
44f67f556b60
[svn] - precision in title format is regarded as character count, not byte count.
yaz
parents:
1459
diff
changeset
|
265 const gchar *tmpstring = string; |
44f67f556b60
[svn] - precision in title format is regarded as character count, not byte count.
yaz
parents:
1459
diff
changeset
|
266 while (precision > 0) { |
44f67f556b60
[svn] - precision in title format is regarded as character count, not byte count.
yaz
parents:
1459
diff
changeset
|
267 offset = offset_max - precision; |
44f67f556b60
[svn] - precision in title format is regarded as character count, not byte count.
yaz
parents:
1459
diff
changeset
|
268 uptr = g_utf8_offset_to_pointer(tmpstring, offset); |
44f67f556b60
[svn] - precision in title format is regarded as character count, not byte count.
yaz
parents:
1459
diff
changeset
|
269 if (*uptr == '\0') |
44f67f556b60
[svn] - precision in title format is regarded as character count, not byte count.
yaz
parents:
1459
diff
changeset
|
270 break; |
44f67f556b60
[svn] - precision in title format is regarded as character count, not byte count.
yaz
parents:
1459
diff
changeset
|
271 g_string_append_unichar(outstr, g_utf8_get_char(uptr)); |
44f67f556b60
[svn] - precision in title format is regarded as character count, not byte count.
yaz
parents:
1459
diff
changeset
|
272 precision--; |
44f67f556b60
[svn] - precision in title format is regarded as character count, not byte count.
yaz
parents:
1459
diff
changeset
|
273 } |
0 | 274 } |
275 else { | |
276 while ((c = *string++) != '\0') | |
277 PUTCH(c); | |
278 } | |
279 | |
280 RIGHTPAD(width - numpr); | |
281 break; | |
282 | |
283 case 'y': | |
284 val = VI(input, year); | |
285 | |
286 Print_number: | |
287 if (val == 0) | |
288 break; | |
289 if (c != 'N') | |
290 did_output = TRUE; | |
291 | |
292 /* Create reversed number string. */ | |
293 numdigits = 0; | |
294 do { | |
295 convert[numdigits++] = digits[val % 10]; | |
296 val /= 10; | |
297 } | |
298 while (val > 0); | |
299 | |
300 numpr = numdigits > precision ? numdigits : precision; | |
301 | |
302 /* Insert left padding. */ | |
303 if (!f_left && width > numpr) { | |
304 if (f_zero) | |
305 numpr = width; | |
306 else | |
307 for (i = width - numpr; i-- > 0;) | |
308 PUTCH(' '); | |
309 } | |
310 | |
311 /* Insert zero padding. */ | |
312 for (i = numpr - numdigits; i-- > 0;) | |
313 PUTCH('0'); | |
314 | |
315 /* Insert number. */ | |
316 while (numdigits > 0) | |
317 PUTCH(convert[--numdigits]); | |
318 | |
319 RIGHTPAD(width - numpr); | |
320 break; | |
321 | |
322 case '%': | |
323 PUTCH('%'); | |
324 break; | |
325 | |
326 default: | |
327 PUTCH('%'); | |
328 PUTCH(c); | |
329 break; | |
330 } | |
331 } | |
332 | |
333 Done: | |
334 if (did_output) | |
335 return g_string_free(outstr, FALSE); | |
336 else | |
337 return NULL; | |
338 } | |
339 | |
340 struct _TagDescription { | |
341 gchar tag; | |
342 gchar *description; | |
343 }; | |
344 | |
345 typedef struct _TagDescription TagDescription; | |
346 | |
347 static TagDescription tag_descriptions[] = { | |
348 {'p', N_("Performer/Artist")}, | |
349 {'a', N_("Album")}, | |
350 {'g', N_("Genre")}, | |
351 {'f', N_("File name")}, | |
352 {'F', N_("File path")}, | |
353 {'e', N_("File extension")}, | |
354 {'t', N_("Track name")}, | |
355 {'n', N_("Track number")}, | |
356 {'d', N_("Date")}, | |
357 {'y', N_("Year")}, | |
358 {'c', N_("Comment")} | |
359 }; | |
360 | |
361 gint tag_descriptions_length = | |
362 sizeof(tag_descriptions) / sizeof(TagDescription); | |
363 | |
364 GtkWidget * | |
365 xmms_titlestring_descriptions(gchar * tags, gint columns) | |
366 { | |
367 GtkWidget *table, *label; | |
368 gchar tag_str[5]; | |
369 gint num = strlen(tags); | |
370 gint r = 0, c, i; | |
371 | |
372 g_return_val_if_fail(tags != NULL, NULL); | |
373 g_return_val_if_fail(columns <= num, NULL); | |
374 | |
375 table = gtk_table_new((num + columns - 1) / columns, columns * 2, FALSE); | |
376 gtk_table_set_row_spacings(GTK_TABLE(table), 2); | |
377 gtk_table_set_col_spacings(GTK_TABLE(table), 5); | |
378 | |
379 for (c = 0; c < columns; c++) { | |
380 for (r = 0; r < (num + columns - 1 - c) / columns; r++) { | |
381 g_snprintf(tag_str, sizeof(tag_str), "%%%c:", *tags); | |
382 label = gtk_label_new(tag_str); | |
383 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); | |
384 gtk_table_attach(GTK_TABLE(table), label, 2 * c, 2 * c + 1, r, | |
385 r + 1, GTK_FILL, GTK_FILL, 0, 0); | |
386 gtk_widget_show(label); | |
387 | |
388 for (i = 0; i < tag_descriptions_length; i++) { | |
389 if (*tags == tag_descriptions[i].tag) { | |
390 label = gtk_label_new(_(tag_descriptions[i].description)); | |
391 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); | |
392 gtk_table_attach(GTK_TABLE(table), label, 2 * c + 1, | |
393 2 * c + 2, r, r + 1, | |
394 GTK_EXPAND | GTK_FILL, | |
395 GTK_EXPAND | GTK_FILL, 0, 0); | |
396 gtk_widget_show(label); | |
397 break; | |
398 } | |
399 } | |
400 | |
401 if (i == tag_descriptions_length) | |
402 g_warning("Invalid tag: %c", *tags); | |
403 | |
404 tags++; | |
405 } | |
406 | |
407 } | |
408 | |
409 label = gtk_label_new(_("%{n:...%}: Display \"...\" only if element " | |
410 "%n is present")); | |
411 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); | |
412 gtk_table_attach(GTK_TABLE(table), label, 0, r + 1, | |
413 r + 1, r + 2, GTK_FILL, GTK_FILL, 0, 0); | |
414 gtk_widget_show(label); | |
415 | |
416 return table; | |
417 } |