Jump to content

GTK question


Recommended Posts

Here's a question for all you GTK gurus out there ...

 

 

I have a program where I want to take the number displayed on a label (text), convert it to an integer, manipulate it (subtract the integer taken from a spin button), convert the result back to text and then write that text back to the label (thus displaying the resultant value).

 

As I am incredibly new to this I am unsure as to what all is involved. What issues might arise? I will post the snippet of code that I have written below:

 

void
on_ResearchDiceSpinButton_changed      (GtkEditable     *editable,
                                       gpointer         user_data)
{
gdouble dice;
gdouble IPCs, IPCs_new;
G_CONST_RETURN gchar *IPCstr;
gchar *IPCstr_old;
gchar *IPCstr_new;
gchar *IPCstr_buffer;
gchar *IPCcost;
GtkWidget *ResearchDiceSpinButton = lookup_widget( GTK_WIDGET(editable),
               "ResearchDiceSpinButton");
GtkWidget *ResearchDiceTotalIPCCostLabel = lookup_widget( GTK_WIDGET(editable),
                "ResearchDiceTotalIPCCostLabel");
GtkWidget *DevelopWeaponsIPCsRemainingLabel = lookup_widget( GTK_WIDGET(editable),
                "DevelopWeaponsIPCsRemainingLabel");

// Get the number of dice from the spin button
dice = (gdouble) gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON(ResearchDiceSpinButton) );

// Set the IPC cost Label text from the number of dice (*5)
if (dice == 0)
{
 gtk_label_set_label( GTK_LABEL(ResearchDiceTotalIPCCostLabel), g_ascii_dtostr( IPCcost, 3, (dice*5.0)));
}
else
{
 gtk_label_set_label( GTK_LABEL(ResearchDiceTotalIPCCostLabel), "0");
}

// Get the remaining IPCs from the label 
IPCstr_old = gtk_label_get_text(GTK_LABEL(DevelopWeaponsIPCsRemainingLabel));
IPCs = g_strtod( IPCstr_old, NULL ); //convert to a double
IPCs_new = (IPCs - dice*5.0);  //calculate how many are left
IPCstr_new = g_strconcat( "<b>", g_ascii_dtostr( IPCstr_buffer, 3, IPCs_new), "</b>", NULL); // concat the new label string to include Pango markup
// set the new IPCs Remaining label text
gtk_label_set_label( GTK_LABEL(DevelopWeaponsIPCsRemainingLabel), IPCstr_new );
}

 

Maybe I should say that I am using Anjuta/Glade to design the project.

 

When I build the project, all goes well. On execution, however, the program seg faults when the spin button up arrow is clicked or the value inside is deleted.

 

What am I doing wrong?

 

Nate

Link to comment
Share on other sites

Heh, I solved it. Turned out that the function g_strdup_printf() worked instead. I found it in on e of the example codes.

 

Anyway, what do you guys think about the g_random_int_range() function? Part of the code that I am writing needs to generate ALOT of 6-sided die rolls at a certain point. Is this function going to be too slow, or is there a better one?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...