1 /*
2  * Copyright (C) 2002-2012 Edscott Wilson Garcia
3  * EMail: edscott@users.sf.net
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program;
18  */
19 
20 #define RFM_PRIMARY_COMPAT_C
21 
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25 
26 #include "rfm.h"
27 #include "rfm_modules.h"
28 
29 ////////////////////////////////////////////////////////////////////////////
30 /// glib 2.32 compatibility
31 ////////////////////////////////////////////////////////////////////////////
32 //
rfm_replace_menu_label(GtkWidget * parent,const gchar * text)33 void rfm_replace_menu_label(GtkWidget *parent, const gchar *text){
34      GtkWidget *label = g_object_get_data(G_OBJECT(parent), "label");
35      gtk_label_set_markup ((GtkLabel *) label, text);
36 }
rfm_replace_menu_image(GtkWidget * parent,const gchar * icon_id)37 void rfm_replace_menu_image(GtkWidget *parent, const gchar *icon_id){
38       GdkPixbuf *p = rfm_get_pixbuf (icon_id, SIZE_BUTTON);
39       if(p==NULL) {
40           DBG("unable to get pixbuf for menu item \"%s\"\n",icon_id);
41           return;
42       }
43 
44     GtkWidget *label = g_object_get_data(G_OBJECT(parent), "label");
45     GtkWidget *image;
46     GtkWidget *replacement;
47     if (!label) {
48         replacement = rfm_hbox_new(FALSE,0);
49         label = gtk_bin_get_child(GTK_BIN(parent));
50 	if (label && GTK_IS_WIDGET(label)) {
51             g_object_ref(label);
52             gtk_container_remove(GTK_CONTAINER(parent), label);
53         }
54         gtk_container_add(GTK_CONTAINER(parent), replacement);
55     } else {
56         image = g_object_get_data(G_OBJECT(parent), "image");
57         replacement = gtk_bin_get_child(GTK_BIN(parent));
58 	if (image && GTK_IS_WIDGET(image)) {
59             gtk_container_remove(GTK_CONTAINER(replacement), image);
60         }
61 	if (label && GTK_IS_WIDGET(label)) {
62             g_object_ref(label);
63             gtk_container_remove(GTK_CONTAINER(replacement), label);
64         }
65     }
66     image = gtk_image_new_from_pixbuf (p);
67 
68     gtk_box_pack_start(GTK_BOX(replacement), image, FALSE,FALSE,0);
69     if (label && GTK_IS_WIDGET(label)) {
70         gtk_box_pack_start(GTK_BOX(replacement), label, FALSE,FALSE,0);
71         g_object_unref(label);
72         gtk_widget_show(label);
73     }
74     gtk_widget_show(image);
75     gtk_widget_show(replacement);
76     g_object_set_data(G_OBJECT(parent), "image", image);
77     g_object_set_data(G_OBJECT(parent), "label", label);
78 }
79 // GtkAccelLabel
rfm_set_menu_image(GtkWidget * parent,GtkWidget * image)80 void rfm_set_menu_image(GtkWidget *parent, GtkWidget *image){
81     if (!image || !GTK_IS_WIDGET(image)) return;
82     GtkWidget *label = gtk_bin_get_child(GTK_BIN(parent));
83     if (label && GTK_IS_WIDGET(label)) {
84         g_object_ref(label);
85         gtk_container_remove(GTK_CONTAINER(parent), label);
86     }
87     GtkWidget *replacement = rfm_hbox_new(FALSE,0);
88     gtk_container_add(GTK_CONTAINER(parent), replacement);
89     gtk_box_pack_start(GTK_BOX(replacement), image, FALSE,FALSE,0);
90     if (label && GTK_IS_WIDGET(label)) {
91         gtk_box_pack_start(GTK_BOX(replacement), label, FALSE,FALSE,0);
92         g_object_unref(label);
93         gtk_widget_show(label);
94     }
95     gtk_widget_show(image);
96     gtk_widget_show(replacement);
97     g_object_set_data(G_OBJECT(parent), "image", image);
98     g_object_set_data(G_OBJECT(parent), "label", label);
99 
100 }
101 
rfm_menu_item_new(const gchar * icon_id,const gchar * text)102 GtkWidget * rfm_menu_item_new(const gchar *icon_id, const gchar *text)
103 {
104     GdkPixbuf *pb = (icon_id)? rfm_get_pixbuf (icon_id, SIZE_BUTTON): NULL;
105                 // no replacement for gtk_image_menu_item
106     GtkWidget *w = gtk_menu_item_new_with_label ("");
107     GtkWidget *replacement = rfm_hbox_new(FALSE,0);
108     GtkWidget *label = gtk_bin_get_child(GTK_BIN(w));
109     if (label && GTK_IS_WIDGET(label)) {
110         g_object_ref(label);
111         gtk_container_remove(GTK_CONTAINER(w), label);
112     }
113 
114     if (pb){
115         GtkWidget *image = gtk_image_new_from_pixbuf (pb);
116         gtk_widget_show (image);
117         gtk_box_pack_start(GTK_BOX(replacement), image, FALSE,FALSE,0);
118         g_object_set_data(G_OBJECT(w), "image", image);
119 	g_object_unref(pb);
120     }
121     if (label && GTK_IS_WIDGET(label)) {
122         gtk_label_set_markup(GTK_LABEL(label), text);
123         gtk_box_pack_start(GTK_BOX(replacement), label, FALSE,FALSE,3);
124         g_object_set_data(G_OBJECT(w), "label", label);
125         gtk_widget_show(label);
126         g_object_unref(label);
127     }
128     gtk_widget_show(replacement);
129     gtk_container_add(GTK_CONTAINER(w), replacement);
130     return w;
131 }
132 
133 gint
rfm_gtk_version(void)134 rfm_gtk_version(void){
135 #if GTK_MAJOR_VERSION==2
136     return 2;
137 #else
138     return 3;
139 #endif
140 }
141 
rfm_thread_create(const gchar * name,GThreadFunc func,gpointer data,gboolean joinable)142 GThread * rfm_thread_create(const gchar *name, GThreadFunc func, gpointer data, gboolean joinable){
143     NOOP(stderr, "rfm_thread_create: %s\n", name);
144     GThread *thread;
145     GError *error = NULL;
146 #if GLIB_MAJOR_VERSION==2 && GLIB_MINOR_VERSION<32
147     thread = g_thread_create(func, data, joinable, &error);
148 #else
149     thread = g_thread_try_new(name, func, data, &error);
150     if (!error && !joinable) g_thread_unref(thread);
151 #endif
152     if (error){
153 	DBG("rfm_thread_create(): %s\n", error->message);
154 	g_error_free(error);
155         thread = NULL;
156     }
157     return thread;
158 }
159 
rfm_cond_timed_wait(GCond * signal,GMutex * mutex,gint seconds)160 gboolean rfm_cond_timed_wait(GCond *signal, GMutex *mutex, gint seconds){
161     gboolean retval;
162 #if GLIB_MAJOR_VERSION==2 && GLIB_MINOR_VERSION<32
163     GTimeVal tv;
164     g_get_current_time (&tv);
165     tv.tv_sec += seconds;
166     retval = g_cond_timed_wait(signal, mutex, &tv);
167 #else
168     gint64 end_time;
169     end_time = g_get_monotonic_time () + seconds * G_TIME_SPAN_SECOND;
170     retval = g_cond_wait_until (signal, mutex, end_time);
171 #endif
172     return retval;
173 }
174 
rfm_rw_lock_init(RfmRWLock * rw_lock)175 void rfm_rw_lock_init(RfmRWLock *rw_lock){
176 #if GLIB_MAJOR_VERSION==2 && GLIB_MINOR_VERSION>=32
177     g_rw_lock_init(rw_lock);
178 #else
179     g_static_rw_lock_init(rw_lock);
180 #endif
181 }
182 
rfm_rw_lock_clear(RfmRWLock * rw_lock)183 void rfm_rw_lock_clear(RfmRWLock *rw_lock){
184 #if GLIB_MAJOR_VERSION==2 && GLIB_MINOR_VERSION>=32
185     g_rw_lock_clear (rw_lock);
186 #else
187     g_static_rw_lock_free(rw_lock);
188 #endif
189 }
190 
191 
rfm_rw_lock_writer_lock(RfmRWLock * rw_lock)192 void rfm_rw_lock_writer_lock (RfmRWLock *rw_lock){
193 #if GLIB_MAJOR_VERSION==2 && GLIB_MINOR_VERSION>=32
194     g_rw_lock_writer_lock(rw_lock);
195 #else
196     g_static_rw_lock_writer_lock (rw_lock);
197 #endif
198 }
199 
rfm_rw_lock_writer_trylock(RfmRWLock * rw_lock)200 gboolean rfm_rw_lock_writer_trylock (RfmRWLock *rw_lock){
201 #if GLIB_MAJOR_VERSION==2 && GLIB_MINOR_VERSION>=32
202     return g_rw_lock_writer_trylock(rw_lock);
203 #else
204     return g_static_rw_lock_writer_trylock (rw_lock);
205 #endif
206 }
207 
rfm_rw_lock_writer_unlock(RfmRWLock * rw_lock)208 void rfm_rw_lock_writer_unlock (RfmRWLock *rw_lock){
209 #if GLIB_MAJOR_VERSION==2 && GLIB_MINOR_VERSION>=32
210     g_rw_lock_writer_unlock(rw_lock);
211 #else
212     g_static_rw_lock_writer_unlock (rw_lock);
213 #endif
214 }
215 
rfm_rw_lock_reader_lock(RfmRWLock * rw_lock)216 void rfm_rw_lock_reader_lock (RfmRWLock *rw_lock){
217 #if GLIB_MAJOR_VERSION==2 && GLIB_MINOR_VERSION>=32
218     g_rw_lock_reader_lock(rw_lock);
219 #else
220     g_static_rw_lock_reader_lock (rw_lock);
221 #endif
222 }
223 
rfm_rw_lock_reader_unlock(RfmRWLock * rw_lock)224 void rfm_rw_lock_reader_unlock (RfmRWLock *rw_lock){
225 #if GLIB_MAJOR_VERSION==2 && GLIB_MINOR_VERSION>=32
226     g_rw_lock_reader_unlock(rw_lock);
227 #else
228     g_static_rw_lock_reader_unlock (rw_lock);
229 #endif
230 }
231 
rfm_rw_lock_reader_trylock(RfmRWLock * rw_lock)232 gboolean rfm_rw_lock_reader_trylock (RfmRWLock *rw_lock){
233 #if GLIB_MAJOR_VERSION==2 && GLIB_MINOR_VERSION>=32
234     return g_rw_lock_reader_trylock(rw_lock);
235 #else
236     return g_static_rw_lock_reader_trylock (rw_lock);
237 #endif
238 }
239 
240 
241 ////////////////////////////////////////////////////////////////////////////
242 /// gtk+ compatibility
243 ////////////////////////////////////////////////////////////////////////////
244 
245 
rfm_text_view_set_wrap_mode(GtkTextView * text_view,gint mode)246 void rfm_text_view_set_wrap_mode(GtkTextView *text_view, gint mode){
247 #if GTK_MAJOR_VERSION==3
248     gtk_text_view_set_wrap_mode (text_view, mode);
249 #endif
250     return;
251 }
252 
253 
rfm_combo_box_new_with_entry(void)254 GtkWidget *rfm_combo_box_new_with_entry (void){
255 #if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
256     // this is deprecated...
257     return gtk_combo_box_entry_new ();
258 #else
259     return gtk_combo_box_new_with_entry ();
260 #endif
261 
262 }
263 
rfm_combo_box_text_remove_all(GtkWidget * comboentry)264 void rfm_combo_box_text_remove_all(GtkWidget *comboentry){
265 #if GTK_MAJOR_VERSION==2
266     // XXX: This is a known bug due to gtk2 limitations.
267     // This will just remove first item...
268     // (in Rodent only a problem if more than one bluetooth is present)
269 # if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
270     gtk_combo_box_remove_text (GTK_COMBO_BOX(comboentry), 0);
271 # else
272     gtk_combo_box_text_remove  (GTK_COMBO_BOX_TEXT(comboentry), 0);
273 # endif
274 
275 #else
276     // This is gtk3, and no bug is present.
277     gtk_combo_box_text_remove_all  (GTK_COMBO_BOX_TEXT(comboentry));
278 #endif
279 
280 }
281 
rfm_combo_box_text_prepend(GtkWidget * comboentry,const gchar * text)282 void rfm_combo_box_text_prepend(GtkWidget *comboentry, const gchar *text){
283 #if GTK_MAJOR_VERSION==2 && GTK_MINOR_VERSION<24
284     gtk_combo_box_prepend_text(GTK_COMBO_BOX(comboentry),text);
285 #else
286     gtk_combo_box_text_prepend_text(GTK_COMBO_BOX_TEXT(comboentry),text);
287 #endif
288 }
289 
290 #if GTK_MAJOR_VERSION==3 && GTK_MINOR_VERSION>=2
291 GtkWidget * // XXX deprecated
rfm_hscale_new_with_range(gdouble min,gdouble max,gdouble step)292 rfm_hscale_new_with_range(gdouble min, gdouble max, gdouble step){
293     return gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, min, max, step);
294 }
295 
296 GtkWidget *
rfm_hbox_new(gboolean homogeneous,gint spacing)297 rfm_hbox_new(gboolean homogeneous, gint spacing){
298     GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, spacing);
299     gtk_box_set_homogeneous (GTK_BOX(box),homogeneous);
300     return box;
301 }
302 
303 GtkWidget *
rfm_vbox_new(gboolean homogeneous,gint spacing)304 rfm_vbox_new(gboolean homogeneous, gint spacing){
305     GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, spacing);
306     gtk_box_set_homogeneous (GTK_BOX(box),homogeneous);
307     return box;
308 }
309 GtkWidget *
rfm_vpaned_new(void)310 rfm_vpaned_new(void){
311     return gtk_paned_new(GTK_ORIENTATION_VERTICAL);
312 }
313 GtkWidget *
rfm_hpaned_new(void)314 rfm_hpaned_new(void){
315     return gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
316 }
317 GtkWidget *
rfm_vbutton_box_new(void)318 rfm_vbutton_box_new(void){
319     return gtk_button_box_new(GTK_ORIENTATION_VERTICAL);
320 }
321 GtkWidget *
rfm_hbutton_box_new(void)322 rfm_hbutton_box_new(void){
323     return gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
324 }
325 #else
326 GtkWidget * // XXX deprecated
rfm_hscale_new_with_range(gdouble min,gdouble max,gdouble step)327 rfm_hscale_new_with_range(gdouble min, gdouble max, gdouble step){
328     return gtk_hscale_new_with_range(min, max, step);
329 }
330 
331 GtkWidget *
rfm_hbox_new(gboolean homogeneous,gint spacing)332 rfm_hbox_new(gboolean homogeneous, gint spacing){
333     return gtk_hbox_new(homogeneous, spacing);
334 }
335 
336 GtkWidget *
rfm_vbox_new(gboolean homogeneous,gint spacing)337 rfm_vbox_new(gboolean homogeneous, gint spacing){
338     return gtk_vbox_new(homogeneous, spacing);
339 }
340 GtkWidget *
rfm_vpaned_new(void)341 rfm_vpaned_new(void){
342     return gtk_vpaned_new();
343 }
344 GtkWidget *
rfm_hpaned_new(void)345 rfm_hpaned_new(void){
346     return gtk_hpaned_new();
347 }
348 GtkWidget *
rfm_vbutton_box_new(void)349 rfm_vbutton_box_new(void){
350     return gtk_vbutton_box_new();
351 }
352 GtkWidget *
rfm_hbutton_box_new(void)353 rfm_hbutton_box_new(void){
354     return gtk_hbutton_box_new();
355 }
356 #endif
357 
358 
359