/**************************************************************************** Returns TRUE if the nation is valid, else, print an error message and returns FALSE. ****************************************************************************/ static inline bool nation_check(const struct nation_type *pnation, bool do_output, const char *file, const char *function, int line) { if (0 == nation_count()) { if (do_output) { do_log(file, function, line, TRUE, LOG_ERROR, "Function called before nations setup."); } return FALSE; } if (NULL == pnation) { if (do_output) { do_log(file, function, line, TRUE, LOG_ERROR, "This function has NULL nation argument."); } return FALSE; } if (pnation->item_number < 0 || pnation->item_number >= nation_count() || &nations[nation_index(pnation)] != pnation) { if (do_output) { do_log(file, function, line, TRUE, LOG_ERROR, "This function has bad nation number %d (count %d).", pnation->item_number, nation_count()); } return FALSE; } return TRUE; }
/************************************************************************** ... **************************************************************************/ void races_toggles_callback(Widget w, XtPointer client_data, XtPointer call_data) { int index = XTPOINTER_TO_INT(client_data); struct nation_type *race = races_toggles_to_nations[index]; int j; Widget entry; if(races_leader_pick_popupmenu) XtDestroyWidget(races_leader_pick_popupmenu); races_leader_pick_popupmenu = XtVaCreatePopupShell("menu", simpleMenuWidgetClass, races_leader_pick_menubutton, NULL); local_nation_count = nation_count(); j = 0; nation_leader_list_iterate(nation_leaders(race), pleader) { entry = XtVaCreateManagedWidget(nation_leader_name(pleader), smeBSBObjectClass, races_leader_pick_popupmenu, NULL); XtAddCallback(entry, XtNcallback, races_leader_pick_callback, INT_TO_XTPOINTER(local_nation_count * j + nation_index(race))); j++; } nation_leader_list_iterate_end;
/**************************************************************** ... *****************************************************************/ static void select_random_race(void) { /* try to find a free nation */ /* FIXME: this code should be done another way. -ev */ while (1) { unsigned int race_toggle_index = fc_rand(nation_count()); if (!is_nation_playable(nation_by_number(race_toggle_index)) || !nation_by_number(race_toggle_index)->is_available || nation_by_number(race_toggle_index)->player) { continue; } if (XtIsSensitive(races_toggles[race_toggle_index])) { x_simulate_button_click(races_toggles[race_toggle_index]); break; } } }
/************************************************************************** ... **************************************************************************/ void races_toggles_set_sensitive(void) { int i; if (!races_dialog_shell) { return; } for (i = 0; i < nation_count(); i++) { if (nation_idx_to_race_toggle[i] > -1) { XtSetSensitive(races_toggles[nation_idx_to_race_toggle[i]], TRUE); } } nations_iterate(nation) { int selected_nation = -1; int this_index = nation_index(nation); if (!is_nation_playable(nation)) { continue; } if (nation->is_available && !nation->player) { continue; } if (races_buttons_get_current() != -1) { selected_nation = nation_index(races_toggles_to_nations[races_buttons_get_current()]); } log_debug(" [%d]: %d = %s", selected_nation, nation_number(nation), nation_rule_name(nation)); if (this_index == selected_nation) { XawToggleUnsetCurrent(races_toggles[0]); XtSetSensitive(races_toggles[nation_idx_to_race_toggle[this_index]], FALSE); select_random_race(); } else { XtSetSensitive(races_toggles[nation_idx_to_race_toggle[this_index]], FALSE); } } nations_iterate_end; }
/**************************************************************** ... *****************************************************************/ void create_races_dialog(struct player *pplayer) { int per_row = 5; int i, j, len, maxracelen, index, nat_count; char maxracename[MAX_LEN_NAME]; char namebuf[64]; int space; XtWidgetGeometry geom; races_player = pplayer; maxracelen = 0; nations_iterate(pnation) { if (is_nation_playable(pnation)) { len = strlen(nation_adjective_translation(pnation)); maxracelen = MAX(maxracelen, len); } } nations_iterate_end; maxracelen = MIN(maxracelen, MAX_LEN_NAME-1); fc_snprintf(maxracename, sizeof(maxracename), "%*s", maxracelen+2, "W"); races_dialog_shell = I_T(XtCreatePopupShell("racespopup", transientShellWidgetClass, toplevel, NULL, 0)); races_form = XtVaCreateManagedWidget("racesform", formWidgetClass, races_dialog_shell, NULL); races_label = I_L(XtVaCreateManagedWidget("raceslabel", labelWidgetClass, races_form, NULL)); races_toggles_viewport = XtVaCreateManagedWidget("racestogglesviewport", viewportWidgetClass, races_form, XtNfromVert, races_label, NULL); races_toggles_form = XtVaCreateManagedWidget("racestogglesform", formWidgetClass, races_toggles_viewport, NULL); free(races_toggles); races_toggles = fc_calloc(nation_count(), sizeof(Widget)); free(races_toggles_to_nations); races_toggles_to_nations = fc_calloc(nation_count(), sizeof(struct nation_type *)); i = 0; j = 0; index = 0; nations_iterate(pnation) { if (!is_nation_playable(pnation)) { continue; } if (j == 0) { index = i * per_row; fc_snprintf(namebuf, sizeof(namebuf), "racestoggle%d", index); if (i == 0) { races_toggles[index] = XtVaCreateManagedWidget(namebuf, toggleWidgetClass, races_toggles_form, XtNlabel, maxracename, NULL); } else { races_toggles[index] = XtVaCreateManagedWidget(namebuf, toggleWidgetClass, races_toggles_form, XtNradioGroup, races_toggles[index - 1], XtNfromVert, races_toggles[index - per_row], XtNlabel, maxracename, NULL); } } else { index = i * per_row + j; fc_snprintf(namebuf, sizeof(namebuf), "racestoggle%d", index); if (i == 0) { races_toggles[index] = XtVaCreateManagedWidget(namebuf, toggleWidgetClass, races_toggles_form, XtNradioGroup, races_toggles[index - 1], XtNfromHoriz, races_toggles[index - 1], XtNlabel, maxracename, NULL); } else { races_toggles[index] = XtVaCreateManagedWidget(namebuf, toggleWidgetClass, races_toggles_form, XtNradioGroup, races_toggles[index - 1], XtNfromVert, races_toggles[index - per_row], XtNfromHoriz, races_toggles[index - 1], XtNlabel, maxracename, NULL); } } races_toggles_to_nations[index] = pnation; j++; if (j >= per_row) { j = 0; i++; } } nations_iterate_end; nat_count = index + 1; races_leader_form = XtVaCreateManagedWidget("racesleaderform", formWidgetClass, races_form, XtNfromVert, races_toggles_viewport, /* XtNfromHoriz, races_toggles_viewport,*/ NULL); XtVaGetValues(races_leader_form, XtNdefaultDistance, &space, NULL); XtQueryGeometry(races_toggles[0], NULL, &geom); races_leader = XtVaCreateManagedWidget("racesleader", asciiTextWidgetClass, races_leader_form, XtNeditType, XawtextEdit, XtNwidth, space + 2*(geom.width + geom.border_width), XtNstring, "", NULL); races_leader_pick_popupmenu = 0; races_leader_pick_menubutton = I_L(XtVaCreateManagedWidget("racesleaderpickmenubutton", menuButtonWidgetClass, races_leader_form, /* XtNfromVert, races_leader,*/ XtNfromHoriz, races_leader, NULL)); races_sex_label = I_L(XtVaCreateManagedWidget("racessexlabel", labelWidgetClass, races_form, XtNfromVert, races_leader_form, NULL)); races_sex_form = XtVaCreateManagedWidget("racessexform", formWidgetClass, races_form, XtNfromVert, races_sex_label, NULL); races_sex_toggles[0] = I_L(XtVaCreateManagedWidget("racessextoggle0", toggleWidgetClass, races_sex_form, NULL)); races_sex_toggles[1] = I_L(XtVaCreateManagedWidget("racessextoggle1", toggleWidgetClass, races_sex_form, XtNfromHoriz, (XtArgVal)races_sex_toggles[0], XtNradioGroup, races_sex_toggles[0], NULL)); /* find out styles that can be used at the game beginning */ /* Limit of 64 city_styles should be deleted. -ev */ for (i = 0, b_s_num = 0; i < game.control.styles_count && i < 64; i++) { if (!city_style_has_requirements(&city_styles[i])) { city_style_idx[b_s_num] = i; city_style_ridx[i] = b_s_num; b_s_num++; } } races_style_label = I_L(XtVaCreateManagedWidget("racesstylelabel", labelWidgetClass, races_form, XtNfromVert, races_sex_form, /* XtNfromHoriz, races_toggles_viewport,*/ NULL)); races_style_form = XtVaCreateManagedWidget("racesstyleform", formWidgetClass, races_form, XtNfromVert, races_style_label, /* XtNfromHoriz, races_toggles_viewport,*/ NULL); free(races_style_toggles); races_style_toggles = fc_calloc(b_s_num,sizeof(Widget)); for( i = 0; i < ((b_s_num-1)/per_row)+1; i++) { index = i * per_row; fc_snprintf(namebuf, sizeof(namebuf), "racesstyle%d", index); if( i == 0 ) { races_style_toggles[index] = XtVaCreateManagedWidget(namebuf, toggleWidgetClass, races_style_form, XtNlabel, maxracename, NULL); } else { races_style_toggles[index] = XtVaCreateManagedWidget(namebuf, toggleWidgetClass, races_style_form, XtNradioGroup, races_style_toggles[index-1], XtNfromVert, races_style_toggles[index-per_row], XtNlabel, maxracename, NULL); } for( j = 1; j < per_row; j++) { index = i * per_row + j; if( index >= b_s_num ) break; fc_snprintf(namebuf, sizeof(namebuf), "racesstyle%d", index); if( i == 0 ) { races_style_toggles[index] = XtVaCreateManagedWidget(namebuf, toggleWidgetClass, races_style_form, XtNradioGroup, races_style_toggles[index-1], XtNfromHoriz, races_style_toggles[index-1], XtNlabel, maxracename, NULL); } else { races_style_toggles[index] = XtVaCreateManagedWidget(namebuf, toggleWidgetClass, races_style_form, XtNradioGroup, races_style_toggles[index-1], XtNfromVert, races_style_toggles[index-per_row], XtNfromHoriz, races_style_toggles[index-1], XtNlabel, maxracename, NULL); } } } races_action_form = XtVaCreateManagedWidget("racesactionform", formWidgetClass, races_form, XtNfromVert, races_style_form, NULL); races_ok_command = I_L(XtVaCreateManagedWidget("racesokcommand", commandWidgetClass, races_action_form, NULL)); races_random_command = I_L(XtVaCreateManagedWidget("racesdisconnectcommand", commandWidgetClass, races_action_form, XtNfromHoriz, races_ok_command, NULL)); races_quit_command = I_L(XtVaCreateManagedWidget("racesquitcommand", commandWidgetClass, races_action_form, XtNfromHoriz, races_random_command, NULL)); XtAddCallback(races_random_command, XtNcallback, races_random_command_callback, NULL); XtAddCallback(races_quit_command, XtNcallback, races_quit_command_callback, NULL); for (i = 0; i < nation_count(); i++) { if (races_toggles[i]) { XtAddCallback(races_toggles[i], XtNcallback, races_toggles_callback, INT_TO_XTPOINTER(i)); } } XtAddCallback(races_ok_command, XtNcallback, races_ok_command_callback, NULL); XtSetKeyboardFocus(races_form, races_leader); XtRealizeWidget(races_dialog_shell); /* for(i=0; i<game.control.playable_nation_count; i++) { races_toggles_to_nations[i] = i; } */ qsort(races_toggles_to_nations, nat_count, sizeof(struct nation_type *), races_indirect_compare); /* Build nation_to_race_toggle */ free(nation_idx_to_race_toggle); nation_idx_to_race_toggle = fc_calloc(nation_count(), sizeof(int)); for (i = 0; i < nation_count(); i++) { nation_idx_to_race_toggle[i] = -1; } for (i = 0; i < nation_count(); i++) { if (races_toggles_to_nations[i]) { nation_idx_to_race_toggle[nation_index(races_toggles_to_nations[i])] = i; } } for (i = 0; i < nation_count(); i++) { if (races_toggles[i]) { XtVaSetValues(races_toggles[i], XtNlabel, (XtArgVal)nation_adjective_translation(races_toggles_to_nations[i]), NULL); } } for(i=0; i<b_s_num; i++) { XtVaSetValues(races_style_toggles[i], XtNlabel, (XtArgVal)city_style_name_translation(city_style_idx[i]), NULL); } select_random_race(); }