gliv-1.6
[gliv.git] / src / gtk2_compat.c
blob94243551bd4ba9f2730390027da3325a5ddd4488
1 /**********************
2 * Born to disappear. *
3 **********************/
5 #include "gliv.h"
7 #include <unistd.h> /* access() */
8 #include <stdlib.h> /* srandom(), random() */
9 #include <string.h> /* strncmp() */
10 #include <time.h> /* time() */
11 #include <unistd.h> /* getpid() */
12 #include <stdarg.h>
14 /*** Random numbers ***/
16 gint32 g_random_int_range(gint32 begin, gint32 end)
18 static gboolean initialized = FALSE;
20 if (initialized == FALSE) {
21 /* First time. */
22 srandom(getpid() + time(NULL));
23 initialized = TRUE;
26 return begin + random() % (end - begin);
29 /*** g_build_filename() from glib-2.0 ***/
31 static GString *g_string_append_len(GString * string,
32 const gchar * val, gssize len)
34 gchar *str;
36 str = g_strndup(val, len);
37 string = g_string_append(string, str);
38 g_free(str);
40 return string;
43 static gchar *glib2_string_free(GString * string, gboolean free_segment)
45 gchar *res;
47 res = free_segment ? NULL : string->str;
49 g_string_free(string, free_segment);
50 return res;
53 /* From glib-2.0.6 */
54 static gchar *g_build_pathv(const gchar * separator,
55 const gchar * first_element, va_list args)
57 GString *result;
58 gint separator_len = strlen(separator);
59 gboolean is_first = TRUE;
60 gboolean have_leading = FALSE;
61 const gchar *single_element = NULL;
62 const gchar *next_element;
63 const gchar *last_trailing = NULL;
65 result = g_string_new(NULL);
67 next_element = first_element;
69 while (TRUE) {
70 const gchar *element;
71 const gchar *start;
72 const gchar *end;
74 if (next_element) {
75 element = next_element;
76 next_element = va_arg(args, gchar *);
77 } else
78 break;
80 /* Ignore empty elements */
81 if (!*element)
82 continue;
84 start = element;
86 if (separator_len) {
87 while (start && strncmp(start, separator, separator_len) == 0)
88 start += separator_len;
91 end = start + strlen(start);
93 if (separator_len) {
94 while (end >= start + separator_len &&
95 strncmp(end - separator_len, separator, separator_len) == 0)
96 end -= separator_len;
98 last_trailing = end;
99 while (last_trailing >= element + separator_len &&
100 strncmp(last_trailing - separator_len, separator,
101 separator_len) == 0)
102 last_trailing -= separator_len;
104 if (!have_leading) {
105 /* If the leading and trailing separator strings are in the
106 * same element and overlap, the result is exactly that element
108 if (last_trailing <= start)
109 single_element = element;
111 g_string_append_len(result, element, start - element);
112 have_leading = TRUE;
113 } else
114 single_element = NULL;
117 if (end == start)
118 continue;
120 if (!is_first)
121 g_string_append(result, separator);
123 g_string_append_len(result, start, end - start);
124 is_first = FALSE;
127 if (single_element) {
128 glib2_string_free(result, TRUE);
129 return g_strdup(single_element);
130 } else {
131 if (last_trailing)
132 g_string_append(result, last_trailing);
134 return glib2_string_free(result, FALSE);
138 gchar *glib2_build_filename(const gchar * first_element, ...)
140 gchar *str;
141 va_list args;
143 va_start(args, first_element);
144 str = g_build_pathv("/", first_element, args);
145 va_end(args);
147 return str;
150 /*** g_list_delete_link() ***/
152 GList *g_list_delete_link(GList * list, GList * link)
154 list = g_list_remove_link(list, link);
155 g_list_free_1(link);
157 return list;
161 /*** g_file_test() ***/
163 gboolean g_file_test(const gchar * filename, gint test)
165 /* gtk2_compat.h:#define G_FILE_TEST_EXISTS R_OK */
167 return !access(filename, test);
170 /*** Pango ***/
172 void gtk_widget_modify_font(GtkWidget * widget, PangoFontDescription * unused)
174 GtkStyle *style;
176 style = gtk_widget_get_style(widget);
177 style = gtk_style_copy(style);
178 style->font = gdk_font_load(FONT);
179 gtk_widget_set_style(widget, style);
182 /*** GtkWindow ***/
184 void gtk_window_resize(GtkWindow * window, gint width, gint height)
186 gtk_window_set_default_size(window, width, height);
187 gdk_window_resize(GTK_WIDGET(window)->window, width, height);
190 /*** GtkColorSelection ***/
192 void gtk_color_selection_get_current_color(GtkColorSelection * colorsel,
193 GdkColor * color)
195 gdouble col[4];
197 gtk_color_selection_get_color(colorsel, col);
199 color->red = (gushort) (col[0] * 65535.0);
200 color->green = (gushort) (col[1] * 65535.0);
201 color->blue = (gushort) (col[2] * 65535.0);
204 void gtk_color_selection_set_current_color(GtkColorSelection * colorsel,
205 GdkColor * color)
207 gdouble col[4];
209 col[0] = color->red / 65535.0;
210 col[1] = color->green / 65535.0;
211 col[2] = color->blue / 65535.0;
213 gtk_color_selection_set_color(colorsel, col);
216 /*** GtkFileSelection ***/
218 void gtk_file_selection_set_select_multiple(GtkFileSelection * filesel,
219 gboolean select_multiple)
221 GtkSelectionMode mode;
223 mode = (select_multiple) ? GTK_SELECTION_EXTENDED : GTK_SELECTION_SINGLE;
224 gtk_clist_set_selection_mode(GTK_CLIST(filesel->file_list), mode);
227 gchar **gtk_file_selection_get_selections(GtkFileSelection * filesel)
229 gint count = 0;
230 GList *node;
231 gchar **array, **ptr;
232 gchar *dirname, *buf;
234 /* Count them. */
235 node = GTK_CLIST(filesel->file_list)->selection;
236 while (node != NULL) {
237 count++;
238 node = node->next;
241 /* They are all in the same directory. */
242 dirname = g_path_get_dirname(gtk_file_selection_get_filename(filesel));
244 /* Put them in an array. */
245 array = ptr = g_new(gchar *, count + 1);
246 node = GTK_CLIST(filesel->file_list)->selection;
248 while (node != NULL) {
249 gtk_clist_get_text(GTK_CLIST(filesel->file_list),
250 GPOINTER_TO_INT(node->data), 0, &buf);
252 *ptr = fast_build_filename(dirname, buf);
253 ptr++;
254 node = node->next;
257 g_free(dirname);
259 array[count] = NULL;
260 return array;
263 /*** GtkAdjustment ***/
265 gdouble gtk_adjustment_get_value(GtkAdjustment * adjustment)
267 return adjustment->value;
270 /*** GtkRange ***/
272 gdouble gtk_range_get_value(GtkRange * range)
274 return gtk_adjustment_get_value(gtk_range_get_adjustment(range));