1 /**********************
3 **********************/
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() */
14 /*** Random numbers ***/
16 gint32
g_random_int_range(gint32 begin
, gint32 end
)
18 static gboolean initialized
= FALSE
;
20 if (initialized
== FALSE
) {
22 srandom(getpid() + time(NULL
));
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
)
36 str
= g_strndup(val
, len
);
37 string
= g_string_append(string
, str
);
43 static gchar
*glib2_string_free(GString
* string
, gboolean free_segment
)
47 res
= free_segment
? NULL
: string
->str
;
49 g_string_free(string
, free_segment
);
54 static gchar
*g_build_pathv(const gchar
* separator
,
55 const gchar
* first_element
, va_list args
)
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
;
75 element
= next_element
;
76 next_element
= va_arg(args
, gchar
*);
80 /* Ignore empty elements */
87 while (start
&& strncmp(start
, separator
, separator_len
) == 0)
88 start
+= separator_len
;
91 end
= start
+ strlen(start
);
94 while (end
>= start
+ separator_len
&&
95 strncmp(end
- separator_len
, separator
, separator_len
) == 0)
99 while (last_trailing
>= element
+ separator_len
&&
100 strncmp(last_trailing
- separator_len
, separator
,
102 last_trailing
-= separator_len
;
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
);
114 single_element
= NULL
;
121 g_string_append(result
, separator
);
123 g_string_append_len(result
, start
, end
- start
);
127 if (single_element
) {
128 glib2_string_free(result
, TRUE
);
129 return g_strdup(single_element
);
132 g_string_append(result
, last_trailing
);
134 return glib2_string_free(result
, FALSE
);
138 gchar
*glib2_build_filename(const gchar
* first_element
, ...)
143 va_start(args
, first_element
);
144 str
= g_build_pathv("/", first_element
, args
);
150 /*** g_list_delete_link() ***/
152 GList
*g_list_delete_link(GList
* list
, GList
* link
)
154 list
= g_list_remove_link(list
, link
);
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
);
172 void gtk_widget_modify_font(GtkWidget
* widget
, PangoFontDescription
* unused
)
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
);
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
,
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
,
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
)
231 gchar
**array
, **ptr
;
232 gchar
*dirname
, *buf
;
235 node
= GTK_CLIST(filesel
->file_list
)->selection
;
236 while (node
!= NULL
) {
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
);
263 /*** GtkAdjustment ***/
265 gdouble
gtk_adjustment_get_value(GtkAdjustment
* adjustment
)
267 return adjustment
->value
;
272 gdouble
gtk_range_get_value(GtkRange
* range
)
274 return gtk_adjustment_get_value(gtk_range_get_adjustment(range
));