static void add_button_clicked(GtkButton *button, XmiMsimGuiLayerDialog *dialog) { //make entries empty and disable OK button GtkWidget *compound_dialog = xmi_msim_gui_compound_dialog_new(GTK_WINDOW(dialog), XMI_MSIM_GUI_COMPOUND_DIALOG_TYPE_ADD); int rv = gtk_dialog_run(GTK_DIALOG(compound_dialog)); if (rv == GTK_RESPONSE_ACCEPT) { //something was changed gchar *compound = xmi_msim_gui_compound_dialog_get_compound(XMI_MSIM_GUI_COMPOUND_DIALOG(compound_dialog)); gdouble compound_weight = xmi_msim_gui_compound_dialog_get_weight(XMI_MSIM_GUI_COMPOUND_DIALOG(compound_dialog)); struct compoundData *cd, *cd2, *cdsum; cd2 = CompoundParser(compound); //get current composition int n_elements, *Z; double *weight; xmi_msim_gui_layer_dialog_get_composition(dialog, &n_elements, &Z, &weight); if (n_elements > 0) { xmi_layer layer = {n_elements, Z, weight, 0.0, 0.0}; //copy xmi_layer to compoundData and add current contents cd = xmi_layer2compoundData(&layer); //calculate sum cdsum = add_compound_data(*cd, 1.0, *cd2, compound_weight/100.0); xmi_msim_gui_layer_dialog_set_composition(dialog, cdsum->nElements, cdsum->Elements, cdsum->massFractions); g_free(Z); g_free(weight); FreeCompoundData(cdsum); FreeCompoundData(cd); } else { //list is empty! xmi_scale_double(cd2->massFractions, cd2->nElements, compound_weight/100.0); if (cd2->nElements == 1) { // if compound double density = ElementDensity(cd2->Elements[0]); gchar *buffer = g_strdup_printf("%f", density); g_signal_handler_block(G_OBJECT(dialog->densityEntry), dialog->density_changed); gtk_entry_set_text(GTK_ENTRY(dialog->densityEntry), buffer); g_signal_handler_unblock(G_OBJECT(dialog->densityEntry), dialog->density_changed); g_free(buffer); } xmi_msim_gui_layer_dialog_set_composition(dialog, cd2->nElements, cd2->Elements, cd2->massFractions); } FreeCompoundData(cd2); } gtk_widget_destroy(compound_dialog); return; }
static void normalize_button_clicked(GtkButton *button, XmiMsimGuiLayerDialog *dialog) { double sum; int n_elements; int *Z; double *weight; xmi_msim_gui_layer_dialog_get_composition(dialog, &n_elements, &Z, &weight); if (n_elements == 0) return; sum = xmi_sum_double(weight, n_elements); xmi_scale_double(weight, n_elements, 1.0/sum); xmi_msim_gui_layer_dialog_set_composition(dialog, n_elements, Z, weight); g_free(Z); g_free(weight); }
void normalize_button_clicked_cb(GtkWidget *widget, gpointer data) { struct add_data *ad = (struct add_data *) data; double sum; int i; GtkTreeIter iter; if ((*(ad->layer))->n_elements > 0) { sum = xmi_sum_double((*(ad->layer))->weight,(*(ad->layer))->n_elements ); gtk_entry_set_text(GTK_ENTRY(ad->sumEntry),"100"); xmi_scale_double((*(ad->layer))->weight,(*(ad->layer))->n_elements, 1.0/sum); gtk_list_store_clear(ad->store); for (i = 0 ; i < (*(ad->layer))->n_elements ; i++) { gtk_list_store_append(ad->store, &iter); gtk_list_store_set(ad->store, &iter, SYMBOL_COLUMN, AtomicNumberToSymbol((*(ad->layer))->Z[i]), WEIGHT_COLUMN, (*(ad->layer))->weight[i]*100.0, -1 ); } } }
void dialog_buttons_clicked_cb (GtkDialog *dialog, gint response_id, gpointer data) { struct compoundWidget *cw = (struct compoundWidget *) data; struct compoundData *cd, *cd2, *cdsum; char *textPtr; double weight; double density, thickness; int i; GtkTreeIter iter; char buffer[512]; #if DEBUG == 1 fprintf(stdout,"Entering dialog_buttons_clicked_cb\n"); #endif if (response_id == GTK_RESPONSE_REJECT) { gtk_widget_hide_all(GTK_WIDGET(dialog)); } else if (response_id == GTK_RESPONSE_ACCEPT) { //update if (cw->kind == CW_ADD) { #if DEBUG == 1 fprintf(stdout,"Before mallocing cd2\n"); #endif //adding something cd2 = (struct compoundData *) malloc(sizeof(struct compoundData)); if (CompoundParser(gtk_entry_get_text(GTK_ENTRY(cw->compoundEntry)),cd2 ) != 1) { fprintf(stdout,"compoundParser error in dialog_buttons_clicked_cb\n"); exit(1); } #if DEBUG == 1 fprintf(stdout,"After calling CompoundParser: compound is %s\n",gtk_entry_get_text(GTK_ENTRY(cw->compoundEntry))); #endif weight = strtod(gtk_entry_get_text(GTK_ENTRY(cw->weightEntry)),NULL); #if DEBUG == 1 fprintf(stdout,"After calling CompoundParser: weight is %lf\n",weight); #endif if (*(cw->lw->my_layer) != NULL && (*(cw->lw->my_layer))->n_elements > 0) { //copy xmi_layer to compoundData and add current contents cd = xmi_layer2compoundData(*(cw->lw->my_layer) ); //calculate sum cdsum = add_compound_data(*cd, 1.0, *cd2, weight/100.0); density =(*(cw->lw->my_layer))->density; thickness=(*(cw->lw->my_layer))->thickness; xmi_free_layer(*(cw->lw->my_layer)); free( *(cw->lw->my_layer)); *(cw->lw->my_layer) = compoundData2xmi_layer (cdsum); (*(cw->lw->my_layer))->thickness = thickness; (*(cw->lw->my_layer))->density = density; } else if (*(cw->lw->my_layer) == NULL) { *(cw->lw->my_layer) = compoundData2xmi_layer (cd2); (*(cw->lw->my_layer))->thickness = 0.0; (*(cw->lw->my_layer))->density = 0.0; xmi_scale_double((*(cw->lw->my_layer))->weight,(*(cw->lw->my_layer))->n_elements, weight/100.0); } else if ((*(cw->lw->my_layer))->n_elements == 0) { free( *(cw->lw->my_layer)); *(cw->lw->my_layer) = compoundData2xmi_layer (cd2); xmi_scale_double((*(cw->lw->my_layer))->weight,(*(cw->lw->my_layer))->n_elements, weight/100.0); } else { fprintf(stdout,"error in dialog_buttons_clicked_cb\n"); exit(1); } } else if (cw->kind == CW_EDIT) { weight = strtod(gtk_entry_get_text(GTK_ENTRY(cw->weightEntry)),NULL); (*(cw->lw->my_layer))->weight[cw->index] = weight/100.0; } //update store gtk_list_store_clear(cw->lw->store); for (i = 0 ; i < (*(cw->lw->my_layer))->n_elements ; i++) { gtk_list_store_append(cw->lw->store, &iter); gtk_list_store_set(cw->lw->store, &iter, SYMBOL_COLUMN, AtomicNumberToSymbol((*(cw->lw->my_layer))->Z[i]), WEIGHT_COLUMN, (*(cw->lw->my_layer))->weight[i]*100.0, -1 ); } sprintf(buffer,"%g", xmi_sum_double((*(cw->lw->my_layer))->weight,(*(cw->lw->my_layer))->n_elements )*100.0); gtk_entry_set_text(GTK_ENTRY(cw->lw->sumEntry), buffer); gtk_widget_hide_all(GTK_WIDGET(dialog)); } }