1 /* utils.c - Personal utilities for Gtk+ development 2 * 3 * Copyright (C) 2000 Patrice St-Gelais 4 * patrstg@users.sourceforge.net 5 * www.oricom.ca/patrice.st-gelais 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 20 */ 21 22 #include <gtk/gtk.h> 23 #include <stdio.h> 24 #include <math.h> 25 #include <ctype.h> 26 #include <png.h> 27 #include <libintl.h> 28 29 #ifndef _UTILS 30 #define _UTILS 1 31 32 #define _(x) (gchar*)gettext(x) 33 #define gettext_noop(x) (x) 34 35 #ifndef MIN 36 #define MIN(a,b) (((a)<(b)) ? (a) : (b)) 37 #endif 38 #ifndef MAX 39 #define MAX(a,b) (((a)>(b)) ? (a) : (b)) 40 #endif 41 42 #ifndef FALSE 43 #define FALSE 0 44 #endif 45 46 #ifndef TRUE 47 #define TRUE 1 48 #endif 49 50 #ifndef CANCEL_YESNO 51 #define CANCEL_YESNO -1 52 #endif 53 54 #ifndef FILESEP 55 #define FILESEP '/' 56 #endif 57 58 #define ALIGN_BOTTOM 0.0 59 #define ALIGN_TOP 1.0 60 #define ALIGN_LEFT 0.0 61 #define ALIGN_RIGHT 1.0 62 #define ALIGN_CENTER 0.5 63 64 // Supposed to be defined in the calling application 65 // #ifndef DEF_PAD 66 // #define DEF_PAD 10 67 // #endif 68 69 extern gint DEF_PAD; 70 71 #define WARNING 1 72 #define FAIL 2 73 #define ABORT 3 74 #define SORRY 4 75 #define INFO 5 76 77 // Cosine interpolate pseudo-code from http://freespace.virgin.net/hugo.elias/models/m_perlin.htm 78 // Use with float / double values! 79 /* 80 function Cosine_Interpolate(a, b, x) 81 ft = x * 3.1415927 82 f = (1 - cos(ft)) * .5 83 84 return a*(1-f) + b*f 85 end of function 86 */ 87 #define COSINE_INTERPOLATE(a,b,x) ((a)+(0.5-0.5*cos((x)*PI))*((b)-(a))) 88 89 typedef struct { 90 91 /* void = omenu_struct_type, needed when connecting 92 menuitem signal (inheritance needed...) */ 93 94 void *parent_omenu; 95 int num; 96 GtkWidget *menuitem; 97 char *lbl; 98 void (*callback_fn) (gpointer user_data, GtkWidget *menu_item); 99 } omenu_list_type; 100 101 typedef struct { 102 char *current_lbl; 103 GtkWidget *omenu_ptr; 104 int max_index; 105 int def_index; // default 106 int current_index; // corresponds to current_lbl 107 omenu_list_type *list; 108 gpointer data; // any callback data for the whole menu 109 } omenu_struct_type; 110 111 typedef struct { 112 gint (*callb_fn) (GtkWidget *, gpointer); 113 gpointer callb_data; 114 } cb_args_struct; 115 116 char *default_dir; 117 118 /****************************************************************************/ 119 120 char * init_default_dir(); 121 122 int findnextrec(FILE *file, char *rec); 123 124 char * remove_spaces_both_ends(char *str); 125 126 void my_msg(gchar *message, gint flag_exit); 127 void my_msg_with_image(gchar *title, gchar *message, GtkWidget *imgwidget); 128 129 void modal_dialog(GtkWidget *wdg, gchar *title, 130 gint (*ok_callb) (GtkWidget*, gpointer), 131 gint (*cancel_callb) (GtkWidget *, gpointer), 132 gpointer, GtkWindowPosition, 133 gboolean if_ok_default) ; 134 135 void modal_dialog_with_titles (GtkWidget *wdg, gchar *title, 136 gint (*ok_callb) (GtkWidget*, gpointer), 137 gchar *ok_title, 138 gint (*cancel_callb) (GtkWidget *, gpointer), 139 gchar *cancel_title, 140 gpointer, GtkWindowPosition, 141 gboolean if_ok_default) ; 142 143 void modal_dialog_with_titles_window_provided (GtkWidget *window, 144 GtkWidget *wdg, gchar *title, 145 gint (*ok_callb) (GtkWidget*, gpointer), 146 gchar *ok_title, 147 gint (*cancel_callb) (GtkWidget *, gpointer), 148 gchar *cancel_title, 149 gpointer callb_data, 150 GtkWindowPosition pos, 151 gboolean if_ok_default); 152 153 void modal_dialog_showhide_with_titles(GtkWidget *window, 154 GtkWidget *wdg, 155 gchar *title, 156 gint (*ok_callb) (GtkWidget*, gpointer), 157 gchar *ok_title, 158 gint (*cancel_callb) (GtkWidget *, gpointer), 159 gchar *cancel_title, 160 gpointer, GtkWindowPosition, 161 gboolean if_ok_default) ; 162 163 void modal_dialog_showhide(GtkWidget *window, GtkWidget *wdg, gchar *title, 164 gint (*ok_callb) (GtkWidget*, gpointer), 165 gint (*cancel_callb) (GtkWidget *, gpointer), 166 gpointer, GtkWindowPosition, 167 gboolean if_ok_default) ; 168 169 GtkWidget *create_widget_from_xpm (GtkWidget *reference_window, gchar **data); 170 171 GtkWidget *define_label_in_table(char *txt,GtkWidget *table, gint colfrom, gint colto, 172 gint linefrom, gint lineto, gint pad); 173 174 GtkWidget *define_label_in_table_align(char *txt,GtkWidget *table, gint colfrom, gint colto, 175 gint linefrom, gint lineto, gint pad, gfloat horiz, gfloat vertic); 176 177 GtkWidget *define_label_in_box(char *txt, GtkWidget *box, gint expand, gint fill, gint pad); 178 179 GtkWidget *define_button_in_table (char *txt, GtkWidget *table, gint colfrom, 180 gint colto, gint lineform, gint lineto, gint pad); 181 182 GtkWidget *define_check_button_in_table (char *txt, GtkWidget *table, gint colfrom, 183 gint colto, gint lineform, gint lineto, gint pad); 184 185 GtkWidget * define_check_button_in_box (char *txt, GtkWidget *box, gint expand, gint fill, gint pad); 186 187 GtkWidget * define_scale_in_table(GtkObject *adj, GtkWidget *table, gint colfrom, gint colto, 188 gint linefrom, gint lineto, gint digits, gint pad); 189 190 GtkWidget * define_scale_in_box(GtkObject *adj, GtkWidget *box, gint digits, gint pad); 191 192 GtkWidget * define_entry_in_table(char *txt,GtkWidget *table, gint colfrom, gint colto, 193 gint linefrom, gint lineto, int length, int widget_len, gint pad) ; 194 195 GtkWidget * define_entry_in_table_align(char *txt,GtkWidget *table, gint colfrom, gint colto, 196 gint linefrom, gint lineto, gint length, gint widget_len, 197 gint pad, gfloat horiz, gfloat vertic) ; 198 199 GtkWidget * define_entry_in_box(char *txt, GtkWidget *box, gint expand, gint fill, 200 gint pad, int length, int widget_len); 201 202 GtkWidget * define_radio_button_in_box (GtkWidget *box, GSList **group, char *txt, 203 void (*callback_fn) (GtkWidget *, gpointer ), 204 int if_default ); 205 206 GtkWidget * define_radio_button_in_box_with_data (GtkWidget *box, GSList **group, char *txt, 207 void (*callback_fn) (GtkWidget *, gpointer ), 208 gpointer callb_data, int if_default ); 209 210 GtkWidget * define_radio_button_in_table_with_data (GtkWidget *box, GSList **group, char *txt, 211 void (*callback_fn) (GtkWidget *, gpointer ), 212 gpointer callb_data, int if_default, gint colfrom, gint colto, gint linefrom, gint lineto ); 213 214 int create_omenu (omenu_struct_type **new_omenu, 215 omenu_list_type *option_list, 216 int max_index, 217 int default_index); 218 int create_omenu_with_callb_data (omenu_struct_type **new_omenu, 219 omenu_list_type *option_list, 220 int max_index, 221 int default_index, 222 gpointer data); 223 int find_omenu_option (omenu_struct_type *omenu_str, char *label); 224 int find_omenu_option_num (omenu_struct_type *omenu_str, int value); 225 int find_omenu_num (omenu_list_type *option_list, int value, int max); 226 227 void set_option_menu_from_label(GtkWidget *omenu, gchar *label); 228 229 void set_sensitive_callb (GtkWidget *wdg, gpointer widget); 230 void unset_sensitive_callb (GtkWidget *wdg, gpointer widget); 231 void sensitive_if_true(GtkWidget *check_button, gpointer widget); 232 void unsensitive_if_true(GtkWidget *check_button, gpointer widget); 233 void show_if_true(GtkWidget *check_button, gpointer widget) ; 234 void hide_if_true(GtkWidget *check_button, gpointer widget) ; 235 GtkWidget *align_widget(GtkWidget *, gfloat horizontal, gfloat vertical); 236 void zero_callb (GtkWidget *wdg, gpointer adj) ; 237 238 char *strlow(char *); 239 char *strupp(char *); 240 gboolean is_integer(gchar *); 241 gboolean is_float(gchar *); 242 gdouble string_to_double (gchar *buf); 243 int filexists(char *); 244 int directory_exists(char *); 245 void split_dir_file(gchar *path_n_file, gchar **dir_name, gchar **file_name, gchar separator); 246 gchar *get_dir_name(gchar *path_n_file, gchar sep); 247 gchar *get_file_name(gchar *path_n_file, gchar sep) ; 248 gchar *add_filesep(gchar *dir_name); 249 gchar *concat_dname_fname (gchar *dname, gchar *fname); 250 int yes_no(gchar *, int); 251 int yes_no_cancel(gchar *, int); 252 253 int log2i(gint); 254 255 // Executes a command with execv 256 // "com" must contain the full path 257 // Returns the PID of the child 258 int execution(char *com, char **argum) ; 259 // Executes a command with execvp 260 // Path searched, but it seems it could allow Trojan horses under Linux 261 int executionp(char *com, char **argum) ; 262 263 // Emits a "dummy" expose event (with a 0x0 area) 264 void emit_expose_event (GtkWidget *window) ; 265 266 // Prints a GList (prec - data - next for each node) 267 void print_list (GList *list) ; 268 269 // Toolbar for hiding/showing a dialog (container widget) 270 GtkWidget *hideshow_dialog_new(GtkWidget *reference_window, 271 GtkOrientation orientation, 272 GtkWidget *wdg_to_show_hide, 273 gboolean *show_flag); 274 275 // Simple hide/show callbacks 276 void hide_callb(GtkWidget *wdg, gpointer data); 277 void show_callb(GtkWidget *wdg, gpointer data); 278 279 // File types handling 280 void write_png(char *file_name, int depth, unsigned char *image_buf, int width, int height); 281 void write_png_with_text(char *file_name, int depth, unsigned char *image_buf, 282 int width, int height, png_text* text, int nbkeys); 283 gboolean read_png_header(gchar *file_name, gint *depth, 284 gint *width, gint *height, gint *color_type, gint *interlace_type) ; 285 gboolean read_png(gchar *file_name, gint *depth, unsigned char **image_buf, 286 gint *width, gint *height, gint *color_type, gint *interlace_type); 287 gboolean read_png_with_text(gchar *file_name, gint *depth, unsigned char **image_buf, 288 gint *width, gint *height, gint *color_type, 289 gint *interlace_type, png_text **text, gint *nbkeys); 290 void free_png_text (png_text **txt, gint nbkeys); 291 292 // Only to economize some boring code 293 GtkWidget *frame_new(gchar *label, gint pad) ; 294 295 // "Universal" callbacks 296 gint gint_adj_callb (GtkWidget *wdg, gpointer data); 297 gint gfloat_adj_callb (GtkWidget *wdg, gpointer data); 298 gint gdouble_adj_callb (GtkWidget *wdg, gpointer data); 299 gint toggle_check_button_callb (GtkWidget *checkbutton, gpointer data); 300 gint gboolean_set_true (GtkWidget *wdg, gpointer data); 301 gint gboolean_set_false (GtkWidget *wdg, gpointer data); 302 gint gboolean_set_true_event (GtkWidget *wdg, GdkEventButton *, gpointer data); 303 gint gboolean_set_false_event (GtkWidget *wdg, GdkEventButton *, gpointer data); 304 305 // Obvious... 306 void swap_buffers (gpointer *a, gpointer *b); 307 void order_int (gint *i1, gint *i2); 308 309 void synchro_scr_win (GtkWidget *scr_win1, GtkWidget *scr_win2); 310 gint adjust_adj_callb (GtkAdjustment *adj_trigger, gpointer adj_toadjust); 311 312 GtkWidget *xpm_label_box(GtkWidget *window, gchar **xpm, gchar *label_text ); 313 314 #endif // _UTILS 315 316 317 318