Function Pointers and Arguments
I'm using Gtk+ and when setting functions that listen for events a function pointer, and seperate set of data is passed to it. The function itself then as generic parameters. IE: GtkWidget *widget and gpointer data
. A gpointer is basically the same as a void pointer and I guess the widget is the object the event was triggered on. I have functions working where I pass in one argument (an array of structs), but I want to pass in more (a string too). Heres my function. You can ignore most of the code, at the top I want to pack fname in with the data. The only way I can think of getting this to work would be to get the string length, string, and array of structs. malloc size to hold it all, copy the data to the allocated space, ten have to dissasemble the mess again inside the function. Is there an easier way to do this?
Code:
static void write_config(GtkWidget *widget, gpointer data)
{
syntax *type = (syntax*) data;
const char* fname = "output.xml";
FILE *fp = fopen(fname, "w");
int i;
if(! fp)
{
printf("Error writing to file.\n");
return;
}
fprintf(fp, "<style-scheme id=\"output\" name=\"output\" version=\"1.0\">\n");
for(i=0; i<30; i++, type++)
{
// Get Foreground and background colors
GdkColor foreground, background;
GtkColorButton *fore = GTK_COLOR_BUTTON(type->fore);
GtkColorButton *back = GTK_COLOR_BUTTON(type->back);
gtk_color_button_get_color(fore, &foreground);
gtk_color_button_get_color(back, &background);
// Get Text Type States
GtkToggleButton *bold_button = GTK_TOGGLE_BUTTON(type->bold);
GtkToggleButton *italic_button = GTK_TOGGLE_BUTTON(type->italic);
GtkToggleButton *ulined_button = GTK_TOGGLE_BUTTON(type->ulined);
gboolean is_bold = gtk_toggle_button_get_active(bold_button);
gboolean is_italic = gtk_toggle_button_get_active(italic_button);
gboolean is_ulined = gtk_toggle_button_get_active(ulined_button);
// Print Line To File
fprintf(fp, "\t<style name=\"%s\"\t\t", SYNTAX_OUTPUT[i]);
fprintf(fp, "foreground=\"#%s\" ", gdk_to_hex(foreground));
fprintf(fp, "background=\"#%s\" ", gdk_to_hex(background));
if(is_bold) fprintf(fp, "bold=\"true\" "); // <- Must test this works
if(is_italic) fprintf(fp, "italic=\"true\" ");
if(is_ulined) fprintf(fp, "underline=\"true\" ");
fprintf(fp, "/>\n");
}
fprintf(fp, "</style-scheme>");
popup(NULL, "File Saved as output.xml");
}