Exemple #1
0
void test_left_trim(void) {
	astr *asleading;
	astr *astrailing;
	astr *asleading_trailing;
	astr *asupper;

	asleading = astr_create(leading);
	aut_assert("1 test creation", strcmp(asleading->string, leading) == 0);

	astrailing = astr_create(trailing);
	aut_assert("2 test creation", strcmp(astrailing->string, trailing) == 0);

	asleading_trailing = astr_create(leading_trailing);
	aut_assert("3 test creation", strcmp(asleading_trailing->string, leading_trailing) == 0);

	asupper = astr_create(upper);
	
	asleading = astr_left_trim(asleading);
	aut_assert("4 test left_trim", astr_equals(asleading, asupper) == 1);

	astr_free(asleading);
	astr_free(astrailing);
	astr_free(asleading_trailing);
	astr_free(asupper);
}
/********************************************************************** 
  Close the file and free associated memory, but don't recurse
  included_from files, and don't free the actual memory where
  the inf record is stored (ie, the memory where the users pointer
  points to).  This is used when closing an included file.
***********************************************************************/
static void inf_close_partial(struct inputfile *inf)
{
  assert_sanity(inf);

  freelog(LOG_DEBUG, "inputfile: sub-closing \"%s\"", inf_filename(inf));

  if (fz_ferror(inf->fp) != 0) {
    freelog(LOG_ERROR, "Error before closing %s: %s", inf_filename(inf),
	    fz_strerror(inf->fp));
    fz_fclose(inf->fp);
    inf->fp = NULL;
  }
  else if (fz_fclose(inf->fp) != 0) {
    freelog(LOG_ERROR, "Error closing %s", inf_filename(inf));
  }
  if (inf->filename) {
    free(inf->filename);
  }
  inf->filename = NULL;
  astr_free(&inf->cur_line);
  astr_free(&inf->copy_line);
  astr_free(&inf->token);
  astr_free(&inf->partial);

  /* assign zeros for safety if accidently re-use etc: */
  init_zeros(inf);
  inf->magic = ~INF_MAGIC;

  freelog(LOG_DEBUG, "inputfile: sub-closed ok");
}
Exemple #3
0
/*
 * afile_free
 *
 * Free the memory allocated for the file.
 *
 * Parameter: The afile instance
 */
afile *afile_free(afile *af) {
	if (af != NULL) {
		if (af->file != NULL) {
			afile_close(af);
		}

		if (af->buffer != NULL) {
			free(af->buffer);
			af->buffer = NULL;
		}

		if (af->open_modes != NULL) {
			astr_free(af->open_modes);
			af->open_modes = NULL;
		}

		if (af->filespec != NULL) {
			astr_free(af->filespec);
			af->filespec = NULL;
		}

		free(af);
	}

	return NULL;
}
Exemple #4
0
void test_creation(void) {
	char *str = "ABC";
	char *buf = "DEF   ";
	astr *as_str;
	astr *as_buf;

	as_str = astr_create(str);
	aut_assert("1 test_creation", strcmp(as_str->string, str) == 0);

	as_buf = astr_create_from_buffer(buf, strlen(buf));
	aut_assert("2 test_creation", strcmp(as_buf->string, buf) == 0);

	astr_free(as_str);
	astr_free(as_buf);
}
Exemple #5
0
void test_to_lower_case(void) {
	astr *aslower;
	astr *asupper;

	aslower = astr_create(lower);
	aut_assert("1 test creation", strcmp(aslower->string, lower) == 0);

	asupper = astr_create(upper);
	aut_assert("2 test creation", strcmp(asupper->string, upper) == 0);

	asupper = astr_to_lower_case(asupper);
	aut_assert("3 test to_lower", astr_equals(aslower, asupper) == 1);

	astr_free(aslower);
	astr_free(asupper);
}
Exemple #6
0
void spend_time() {
	char *str = "STR";
	astr *as;
	int i;

	as = astr_create(str);
	for(i = 0; i < 10000; i++) {
		astr_append(as, str);
	}
	astr_free(as);
}
Exemple #7
0
void test_to_mixed_case(void) {
	astr *aslower;
	astr *asupper;
	astr *asmixed;

	aslower = astr_create(lower);
	aut_assert("1 test creation", strcmp(aslower->string, lower) == 0);

	asupper = astr_create(upper);
	aut_assert("2 test creation", strcmp(asupper->string, upper) == 0);

	asmixed = astr_create(mixed);
	aut_assert("3 test creation", strcmp(asmixed->string, mixed) == 0);

	asupper = astr_to_mixed_case(asupper);
	aut_assert("3 test to_mixed", astr_equals(asmixed, asupper) == 1);
	
	aslower = astr_to_mixed_case(aslower);
	aut_assert("4 test to_mixed", astr_equals(asmixed, aslower) == 1);

	astr_free(aslower);
	astr_free(asupper);
	astr_free(asmixed);
}
void test_astr_print(void) {
	astr *as_str;
	char *print;

	as_str = astr_create(str);
	aut_assert("1 print", strcmp(as_str->string, str) == 0);

	print = astr_print(as_str);
	aut_assert("2 print", print != NULL);
	aut_assert("3 print", strlen(print) > 0);
/*
	printf("%s", print);
*/

	free(print);
	astr_free(as_str);
}
void test_astr_hexdump(void) {
	astr *as_str;
	char *dump;

	as_str = astr_create(str);
	aut_assert("1 hexdump", strcmp(as_str->string, str) == 0);

	dump = astr_hexdump(as_str);
	aut_assert("2 hexdump", dump != NULL);
	aut_assert("3 hexdump", strlen(dump) > 0);
/*
	printf("%s", dump);
*/

	free(dump);
	astr_free(as_str);
}
Exemple #10
0
/****************************************************************
  Popups the diplomat dialog
*****************************************************************/
void popup_diplomat_dialog(struct unit *punit, struct tile *dest_tile)
{
  struct city *pcity;
  struct unit *ptunit;
  struct astring text = ASTRING_INIT;

  diplomat_id=punit->id;

  if((pcity=tile_city(dest_tile))){
    /* Spy/Diplomat acting against a city */

    diplomat_target_id=pcity->id;
    astr_set(&text,
             _("Your %s has arrived at %s.\nWhat is your command?"),
             unit_name_translation(punit),
             city_name(pcity));

    if (!unit_has_type_flag(punit, UTYF_SPY)) {
      diplomat_dialog =
        popup_message_dialog(toplevel, "diplomatdialog", astr_str(&text),
			       diplomat_embassy_callback, 0, 1,
			       diplomat_investigate_callback, 0, 1,
			       spy_poison_callback, 0, 1,
			       diplomat_sabotage_callback, 0, 1,
			       diplomat_steal_callback, 0, 1,
			       diplomat_incite_callback, 0, 1,
			       diplomat_keep_moving_callback_city, 0, 1,
			       diplomat_cancel_callback, 0, 0,
			       NULL);
    } else {
      diplomat_dialog =
        popup_message_dialog(toplevel, "spydialog", astr_str(&text),
			       diplomat_embassy_callback, 0,  1,
			       diplomat_investigate_callback, 0, 1,
			       spy_poison_callback, 0, 1,
			       spy_request_sabotage_list, 0, 1,
			       spy_steal_popup, 0, 1,
			       diplomat_incite_callback, 0, 1,
			       diplomat_keep_moving_callback_city, 0, 1,
			       diplomat_cancel_callback, 0, 0,
			       NULL);
    }
      
    if(!diplomat_can_do_action(punit, DIPLOMAT_EMBASSY, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button0"), FALSE);
    }
    if(!diplomat_can_do_action(punit, DIPLOMAT_INVESTIGATE, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button1"), FALSE);
    }
    if(!diplomat_can_do_action(punit, SPY_POISON, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button2"), FALSE);
    }
    if(!diplomat_can_do_action(punit, DIPLOMAT_SABOTAGE, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button3"), FALSE);
    }
    if(!diplomat_can_do_action(punit, DIPLOMAT_STEAL, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button4"), FALSE);
    }
    if(!diplomat_can_do_action(punit, DIPLOMAT_INCITE, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button5"), FALSE);
    }
    if(!diplomat_can_do_action(punit, DIPLOMAT_MOVE, dest_tile)) {
      XtSetSensitive(XtNameToWidget(diplomat_dialog, "*button6"), FALSE);
    }
  } else { 
    if ((ptunit = unit_list_get(dest_tile->units, 0))) {
      /* Spy/Diplomat acting against a unit */
      
      Widget shl;

      astr_set(&text,
               _("Your %s is waiting for your command."),
               unit_name_translation(punit));
      
      diplomat_target_id=ptunit->id;

      shl=popup_message_dialog(toplevel, "spybribedialog",
			       astr_str(&text),
			       diplomat_bribe_callback, 0, 0,
			       spy_sabotage_unit_callback, 0, 0,
			       diplomat_keep_moving_callback_unit, 0, 1,
			       diplomat_cancel_callback, 0, 0,
			       NULL);
	
      if(!diplomat_can_do_action(punit, DIPLOMAT_BRIBE, dest_tile))
	XtSetSensitive(XtNameToWidget(shl, "*button0"), FALSE);
      if(!diplomat_can_do_action(punit, SPY_SABOTAGE_UNIT, dest_tile))
	XtSetSensitive(XtNameToWidget(shl, "*button1"), FALSE);
      if(!diplomat_can_do_action(punit, DIPLOMAT_MOVE, dest_tile))
        XtSetSensitive(XtNameToWidget(shl, "*button2"), FALSE);
    }
  }
  astr_free(&text);
}
Exemple #11
0
/*****************************************************************************
  Report a lua error.
*****************************************************************************/
static int luascript_report(struct fc_lua *fcl, int status, const char *code)
{
  fc_assert_ret_val(fcl, -1);
  fc_assert_ret_val(fcl->state, -1);

  if (status) {
    struct astring str = ASTRING_INIT;
    const char *msg;
    int lineno;

    if (!(msg = lua_tostring(fcl->state, -1))) {
      msg = "(error with no message)";
    }

    /* Add error message. */
    astr_add_line(&str, "lua error:");
    astr_add_line(&str, "\t%s", msg);

    if (code) {
      /* Add lines around the place the parse error is. */
      if (sscanf(msg, "%*[^:]:%d:", &lineno) == 1) {
        const char *begin, *end;
        int i;

        astr_add(&str, "\n");

        i = 1;
        for (begin = code; *begin != '\0';) {
          int len;

          end = strchr(begin, '\n');
          if (end) {
            len = end - begin;
          } else {
            len = strlen(begin);
          }

          if (abs(lineno - i) <= 3) {
            const char *indicator;

            indicator = (lineno == i) ? "-->" : "   ";

            astr_add_line(&str, "\t%s%3d:\t%*.*s",
                indicator, i, len, len, begin);
          }

          i++;

          if (end) {
            begin = end + 1;
          } else {
            break;
          }
        }

        astr_add(&str, "\n");
      }
    }

    luascript_log(fcl, LOG_ERROR, "%s", astr_str(&str));

    astr_free(&str);

    lua_pop(fcl->state, 1);
  }

  return status;
}
Exemple #12
0
/**************************************************************************
  Update all information in the player list dialog.
**************************************************************************/
void real_players_dialog_update(void)
{
  if(pPlayers_Dlg) {
    struct widget *pPlayer0, *pPlayer1;
    struct player *pPlayer;
    SDL_Rect dst0, dst1;
    int i;
    struct astring astr = ASTRING_INIT;
          
    /* redraw window */
    widget_redraw(pPlayers_Dlg->pEndWidgetList);
    
    /* exit button -> neutral -> war -> casefire -> peace -> alliance */
    pPlayer0 = pPlayers_Dlg->pEndWidgetList->prev->prev->prev->prev->prev->prev;
    do{
      pPlayer0 = pPlayer0->prev;
      pPlayer1 = pPlayer0;
      pPlayer = pPlayer0->data.player;
      
      for (i = 0; i < num_player_dlg_columns; i++) {
        if (player_dlg_columns[i].show) {
          switch (player_dlg_columns[i].type) {
            case COL_TEXT:
            case COL_RIGHT_TEXT:
              astr_add_line(&astr, "%s: %s", player_dlg_columns[i].title,
                                             player_dlg_columns[i].func(pPlayer));
              break;
            case COL_BOOLEAN:
              astr_add_line(&astr, "%s: %s", player_dlg_columns[i].title,
                            player_dlg_columns[i].bool_func(pPlayer) ? 
                              _("Yes") : _("No"));
              break;
            default:
              break;
          }
        }
      }

      copy_chars_to_string16(pPlayer0->info_label, astr_str(&astr));

      astr_free(&astr);
          
      /* now add some eye candy ... */
      if(pPlayer1 != pPlayers_Dlg->pBeginWidgetList) {
        dst0.x = pPlayer0->size.x + pPlayer0->size.w / 2;
        dst0.y = pPlayer0->size.y + pPlayer0->size.h / 2;

        do{
          pPlayer1 = pPlayer1->prev;
	  if (have_diplomat_info_about(pPlayer) ||
	     have_diplomat_info_about(pPlayer1->data.player)) {
            dst1.x = pPlayer1->size.x + pPlayer1->size.w / 2;
            dst1.y = pPlayer1->size.y + pPlayer1->size.h / 2;

            switch (player_diplstate_get(pPlayer,
                                         pPlayer1->data.player)->type) {
	      case DS_ARMISTICE:
	        if(SDL_Client_Flags & CF_DRAW_PLAYERS_NEUTRAL_STATUS) {
	          putline(pPlayer1->dst->surface,
	                  dst0.x, dst0.y, dst1.x, dst1.y,
	                  get_theme_color(COLOR_THEME_PLRDLG_ARMISTICE));
	        }
	      break;
              case DS_WAR:
	        if(SDL_Client_Flags & CF_DRAW_PLAYERS_WAR_STATUS) {
	          putline(pPlayer1->dst->surface,
	                  dst0.x, dst0.y, dst1.x, dst1.y,
	                  get_theme_color(COLOR_THEME_PLRDLG_WAR));
	        }
              break;
	      case DS_CEASEFIRE:
	        if (SDL_Client_Flags & CF_DRAW_PLAYERS_CEASEFIRE_STATUS) {
	          putline(pPlayer1->dst->surface,
	                  dst0.x, dst0.y, dst1.x, dst1.y,
	                  get_theme_color(COLOR_THEME_PLRDLG_CEASEFIRE));
	        }
              break;
              case DS_PEACE:
	        if (SDL_Client_Flags & CF_DRAW_PLAYERS_PEACE_STATUS) {
	          putline(pPlayer1->dst->surface,
	                  dst0.x, dst0.y, dst1.x, dst1.y,
	                  get_theme_color(COLOR_THEME_PLRDLG_PEACE));
	        }
              break;
	      case DS_ALLIANCE:
	        if (SDL_Client_Flags & CF_DRAW_PLAYERS_ALLIANCE_STATUS) {
	          putline(pPlayer1->dst->surface,
	                  dst0.x, dst0.y, dst1.x, dst1.y,
	                  get_theme_color(COLOR_THEME_PLRDLG_ALLIANCE));
	        }
              break;
              default:
	        /* no contact */
              break;
	    }  
	  }
	  
        } while(pPlayer1 != pPlayers_Dlg->pBeginWidgetList);
      }
      
    } while(pPlayer0 != pPlayers_Dlg->pBeginWidgetList);
    
    /* -------------------- */
    /* redraw */
    redraw_group(pPlayers_Dlg->pBeginWidgetList,
    			pPlayers_Dlg->pEndWidgetList->prev, 0);
    widget_mark_dirty(pPlayers_Dlg->pEndWidgetList);
  
    flush_dirty();
  }
}
/****************************************************************
 Popup new diplomat dialog.
*****************************************************************/
void popup_diplomat_dialog(struct unit *punit, struct tile *dest_tile)
{
  struct city *pcity;
  struct unit *ptunit;
  GtkWidget *shl;
  struct astring title = ASTRING_INIT, text = ASTRING_INIT;

  diplomat_id = punit->id;

  if ((pcity = tile_city(dest_tile))) {
    /* Spy/Diplomat acting against a city */

    diplomat_target_id = pcity->id;
    astr_set(&title,
             /* TRANS: %s is a unit name, e.g., Spy */
             _("Choose Your %s's Strategy"), unit_name_translation(punit));
    astr_set(&text, 
             _("Your %s has arrived at %s.\nWhat is your command?"),
             unit_name_translation(punit),
             city_name(pcity));

    if (!unit_has_type_flag(punit, UTYF_SPY)) {
      shl = popup_choice_dialog(GTK_WINDOW(toplevel),
        astr_str(&title), astr_str(&text),
	_("Establish _Embassy"), diplomat_embassy_callback, NULL,
	_("_Investigate City"), diplomat_investigate_callback, NULL,
	_("_Sabotage City"), diplomat_sabotage_callback, NULL,
	_("Steal _Technology"), diplomat_steal_callback, NULL,
	_("Incite a _Revolt"), diplomat_incite_callback, NULL,
	_("_Keep moving"), diplomat_keep_moving_callback, NULL,
	GTK_STOCK_CANCEL, diplomat_cancel_callback, NULL,
	NULL);

      if (!diplomat_can_do_action(punit, DIPLOMAT_EMBASSY, dest_tile))
	choice_dialog_button_set_sensitive(shl, 0, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_INVESTIGATE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 1, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_SABOTAGE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 2, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_STEAL, dest_tile))
	choice_dialog_button_set_sensitive(shl, 3, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_INCITE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 4, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_MOVE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 5, FALSE);
    } else {
       shl = popup_choice_dialog(GTK_WINDOW(toplevel),
        astr_str(&title), astr_str(&text),
	_("Establish _Embassy"), diplomat_embassy_callback, NULL,
	_("_Investigate City"), diplomat_investigate_callback, NULL,
	_("_Poison City"), spy_poison_callback, NULL,
	_("Industrial _Sabotage"), spy_request_sabotage_list, NULL,
	_("Steal _Technology"), spy_steal_popup, NULL,
	_("Incite a _Revolt"), diplomat_incite_callback, NULL,
	_("_Keep moving"), diplomat_keep_moving_callback, NULL,
	GTK_STOCK_CANCEL, diplomat_cancel_callback, NULL,
	NULL);

      if (!diplomat_can_do_action(punit, DIPLOMAT_EMBASSY, dest_tile))
	choice_dialog_button_set_sensitive(shl, 0, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_INVESTIGATE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 1, FALSE);
      if (!diplomat_can_do_action(punit, SPY_POISON, dest_tile))
	choice_dialog_button_set_sensitive(shl, 2, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_SABOTAGE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 3, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_STEAL, dest_tile))
	choice_dialog_button_set_sensitive(shl, 4, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_INCITE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 5, FALSE);
      if (!diplomat_can_do_action(punit, DIPLOMAT_MOVE, dest_tile))
	choice_dialog_button_set_sensitive(shl, 6, FALSE);
     }

    diplomat_dialog = shl;

    choice_dialog_set_hide(shl, TRUE);
    g_signal_connect(shl, "destroy",
		     G_CALLBACK(diplomat_destroy_callback), NULL);
    g_signal_connect(shl, "delete_event",
		     G_CALLBACK(diplomat_cancel_callback), NULL);
  } else { 
    if ((ptunit = unit_list_get(dest_tile->units, 0))){
      /* Spy/Diplomat acting against a unit */ 
       
      diplomat_target_id = ptunit->id;
      astr_set(&text,
               /* TRANS: %s is a unit name, e.g., Diplomat, Spy */
               _("Your %s is waiting for your command."),
               unit_name_translation(punit));
 
      shl = popup_choice_dialog(GTK_WINDOW(toplevel),
	_("Subvert Enemy Unit"), astr_str(&text),
	_("_Bribe Enemy Unit"), diplomat_bribe_callback, NULL,
	_("_Sabotage Enemy Unit"), spy_sabotage_unit_callback, NULL,
	GTK_STOCK_CANCEL, diplomat_cancel_callback, NULL,
	NULL);

      if (!diplomat_can_do_action(punit, DIPLOMAT_BRIBE, dest_tile)) {
	choice_dialog_button_set_sensitive(shl, 0, FALSE);
      }
      if (!diplomat_can_do_action(punit, SPY_SABOTAGE_UNIT, dest_tile)) {
	choice_dialog_button_set_sensitive(shl, 1, FALSE);
      }

      diplomat_dialog = shl;

      choice_dialog_set_hide(shl, TRUE);
      g_signal_connect(shl, "destroy",
		       G_CALLBACK(diplomat_destroy_callback), NULL);
      g_signal_connect(shl, "delete_event",
		       G_CALLBACK(diplomat_cancel_callback), NULL);
    }
  }
  astr_free(&title);
  astr_free(&text);
}
/****************************************************************
  Creates spy's building sabotaging dialog
*****************************************************************/
static void create_improvements_list(struct player *pplayer,
				     struct city *pcity)
{  
  GtkWidget *sw, *label, *vbox, *view;
  GtkListStore *store;
  GtkCellRenderer *rend;
  GtkTreeViewColumn *col;
  GtkTreeIter it;
  
  spy_sabotage_shell = gtk_dialog_new_with_buttons(_("Sabotage Improvements"),
    NULL,
    0,
    GTK_STOCK_CANCEL,
    GTK_RESPONSE_CANCEL,
    _("_Sabotage"), 
    GTK_RESPONSE_ACCEPT,
    NULL);
  setup_dialog(spy_sabotage_shell, toplevel);
  gtk_window_set_position(GTK_WINDOW(spy_sabotage_shell), GTK_WIN_POS_MOUSE);

  gtk_dialog_set_default_response(GTK_DIALOG(spy_sabotage_shell),
				  GTK_RESPONSE_ACCEPT);

  label = gtk_frame_new(_("Select Improvement to Sabotage"));
  gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(spy_sabotage_shell))), label);

  vbox = gtk_vbox_new(FALSE, 6);
  gtk_container_add(GTK_CONTAINER(label), vbox);
      
  store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);

  view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
  g_object_unref(store);
  gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);

  rend = gtk_cell_renderer_text_new();
  col = gtk_tree_view_column_new_with_attributes(NULL, rend,
						 "text", 0, NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);

  label = g_object_new(GTK_TYPE_LABEL,
    "use-underline", TRUE,
    "mnemonic-widget", view,
    "label", _("_Improvements:"),
    "xalign", 0.0,
    "yalign", 0.5,
    NULL);
  gtk_container_add(GTK_CONTAINER(vbox), label);
  
  sw = gtk_scrolled_window_new(NULL, NULL);
  gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
				      GTK_SHADOW_ETCHED_IN);
  gtk_container_add(GTK_CONTAINER(sw), view);

  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
    GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
  gtk_widget_set_size_request(sw, -1, 200);
  
  gtk_container_add(GTK_CONTAINER(vbox), sw);

  /* Now populate the list */
  gtk_list_store_append(store, &it);
  gtk_list_store_set(store, &it, 0, _("City Production"), 1, -1, -1);

  city_built_iterate(pcity, pimprove) {
    if (pimprove->sabotage > 0) {
      gtk_list_store_append(store, &it);
      gtk_list_store_set(store, &it,
                         0, city_improvement_name_translation(pcity, pimprove),
                         1, improvement_number(pimprove),
                         -1);
    }  
  } city_built_iterate_end;

  gtk_list_store_append(store, &it);
  {
    struct astring str = ASTRING_INIT;
    /* TRANS: %s is a unit name, e.g., Spy */
    astr_set(&str, _("At %s's Discretion"),
             unit_name_translation(game_unit_by_number(diplomat_id)));
    gtk_list_store_set(store, &it, 0, astr_str(&str), 1, B_LAST, -1);
    astr_free(&str);
  }

  gtk_dialog_set_response_sensitive(GTK_DIALOG(spy_sabotage_shell),
    GTK_RESPONSE_ACCEPT, FALSE);
  
  gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(spy_sabotage_shell)));

  g_signal_connect(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), "changed",
		   G_CALLBACK(spy_improvements_callback), NULL);
  g_signal_connect(spy_sabotage_shell, "response",
		   G_CALLBACK(spy_improvements_response), NULL);

  sabotage_improvement = -2;
	  
  gtk_tree_view_focus(GTK_TREE_VIEW(view));
}
/****************************************************************
  Create spy's tech stealing dialog
*****************************************************************/
static void create_advances_list(struct player *pplayer,
				 struct player *pvictim)
{  
  GtkWidget *sw, *label, *vbox, *view;
  GtkListStore *store;
  GtkCellRenderer *rend;
  GtkTreeViewColumn *col;

  spy_tech_shell = gtk_dialog_new_with_buttons(_("Steal Technology"),
    NULL,
    0,
    GTK_STOCK_CANCEL,
    GTK_RESPONSE_CANCEL,
    _("_Steal"),
    GTK_RESPONSE_ACCEPT,
    NULL);
  setup_dialog(spy_tech_shell, toplevel);
  gtk_window_set_position(GTK_WINDOW(spy_tech_shell), GTK_WIN_POS_MOUSE);

  gtk_dialog_set_default_response(GTK_DIALOG(spy_tech_shell),
				  GTK_RESPONSE_ACCEPT);

  label = gtk_frame_new(_("Select Advance to Steal"));
  gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(spy_tech_shell))), label);

  vbox = gtk_vbox_new(FALSE, 6);
  gtk_container_add(GTK_CONTAINER(label), vbox);
      
  store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);

  view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
  g_object_unref(store);
  gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);

  rend = gtk_cell_renderer_text_new();
  col = gtk_tree_view_column_new_with_attributes(NULL, rend,
						 "text", 0, NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);

  label = g_object_new(GTK_TYPE_LABEL,
    "use-underline", TRUE,
    "mnemonic-widget", view,
    "label", _("_Advances:"),
    "xalign", 0.0,
    "yalign", 0.5,
    NULL);
  gtk_container_add(GTK_CONTAINER(vbox), label);
  
  sw = gtk_scrolled_window_new(NULL, NULL);
  gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
				      GTK_SHADOW_ETCHED_IN);
  gtk_container_add(GTK_CONTAINER(sw), view);

  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
    GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
  gtk_widget_set_size_request(sw, -1, 200);
  
  gtk_container_add(GTK_CONTAINER(vbox), sw);

  /* Now populate the list */
  if (pvictim) { /* you don't want to know what lag can do -- Syela */
    GtkTreeIter it;
    GValue value = { 0, };

    advance_index_iterate(A_FIRST, i) {
      if(player_invention_state(pvictim, i)==TECH_KNOWN && 
	 (player_invention_state(pplayer, i)==TECH_UNKNOWN || 
	  player_invention_state(pplayer, i)==TECH_PREREQS_KNOWN)) {
	gtk_list_store_append(store, &it);

	g_value_init(&value, G_TYPE_STRING);
	g_value_set_static_string(&value,
				  advance_name_for_player(client.conn.playing, i));
	gtk_list_store_set_value(store, &it, 0, &value);
	g_value_unset(&value);
	gtk_list_store_set(store, &it, 1, i, -1);
      }
    } advance_index_iterate_end;

    gtk_list_store_append(store, &it);

    g_value_init(&value, G_TYPE_STRING);
    {
      struct astring str = ASTRING_INIT;
      /* TRANS: %s is a unit name, e.g., Spy */
      astr_set(&str, _("At %s's Discretion"),
               unit_name_translation(game_unit_by_number(diplomat_id)));
      g_value_set_string(&value, astr_str(&str));
      astr_free(&str);
    }
    gtk_list_store_set_value(store, &it, 0, &value);
    g_value_unset(&value);
    gtk_list_store_set(store, &it, 1, A_UNSET, -1);
  }

  gtk_dialog_set_response_sensitive(GTK_DIALOG(spy_tech_shell),
    GTK_RESPONSE_ACCEPT, FALSE);
  
  gtk_widget_show_all(gtk_dialog_get_content_area(GTK_DIALOG(spy_tech_shell)));

  g_signal_connect(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), "changed",
		   G_CALLBACK(spy_advances_callback), NULL);
  g_signal_connect(spy_tech_shell, "response",
		   G_CALLBACK(spy_advances_response), NULL);
  
  steal_advance = 0;

  gtk_tree_view_focus(GTK_TREE_VIEW(view));
}
Exemple #16
0
/****************************************************************
 Popup new diplomat dialog.
*****************************************************************/
void popup_diplomat_dialog(struct unit *punit, struct tile *dest_tile)
{
    struct city *pcity;
    struct unit *ptunit;
    GtkWidget *shl;
    struct astring title = ASTRING_INIT, text = ASTRING_INIT;

    diplomat_target_id[ATK_CITY] = -1;
    diplomat_target_id[ATK_UNIT] = -1;

    diplomat_id = punit->id;

    astr_set(&title,
             /* TRANS: %s is a unit name, e.g., Spy */
             _("Choose Your %s's Strategy"), unit_name_translation(punit));
    astr_set(&text,
             /* TRANS: %s is a unit name, e.g., Diplomat, Spy */
             _("Your %s is waiting for your command."),
             unit_name_translation(punit));

    shl = choice_dialog_start(GTK_WINDOW(toplevel), astr_str(&title),
                              astr_str(&text));

    if ((pcity = tile_city(dest_tile))) {
        /* Spy/Diplomat acting against a city */

        diplomat_target_id[ATK_CITY] = pcity->id;

        action_entry(shl,
                     action_enabled_unit_on_city_local(
                         ACTION_ESTABLISH_EMBASSY, punit, pcity),
                     _("Establish _Embassy"),
                     (GCallback)diplomat_embassy_callback);

        action_entry(shl,
                     action_enabled_unit_on_city_local(
                         ACTION_SPY_INVESTIGATE_CITY, punit, pcity),
                     _("_Investigate City"),
                     (GCallback)diplomat_investigate_callback);

        action_entry(shl,
                     action_enabled_unit_on_city_local(ACTION_SPY_POISON,
                             punit, pcity),
                     _("_Poison City"),
                     (GCallback)spy_poison_callback);

        action_entry(shl,
                     action_enabled_unit_on_city_local(
                         ACTION_SPY_SABOTAGE_CITY, punit, pcity),
                     _("_Sabotage City"),
                     (GCallback)diplomat_sabotage_callback);

        action_entry(shl,
                     action_enabled_unit_on_city_local(
                         ACTION_SPY_TARGETED_SABOTAGE_CITY, punit, pcity),
                     _("Industrial _Sabotage"),
                     (GCallback)spy_request_sabotage_list);

        action_entry(shl,
                     action_enabled_unit_on_city_local(
                         ACTION_SPY_STEAL_TECH, punit, pcity),
                     _("Steal _Technology"),
                     (GCallback)diplomat_steal_callback);

        action_entry(shl,
                     action_enabled_unit_on_city_local(
                         ACTION_SPY_TARGETED_STEAL_TECH, punit, pcity),
                     _("Industrial espionage"),
                     (GCallback)spy_steal_popup);

        action_entry(shl,
                     action_enabled_unit_on_city_local(ACTION_SPY_INCITE_CITY,
                             punit, pcity),
                     _("Incite a _Revolt"),
                     (GCallback)diplomat_incite_callback);
    }

    if ((ptunit = unit_list_get(dest_tile->units, 0))) {
        /* Spy/Diplomat acting against a unit */

        diplomat_target_id[ATK_UNIT] = ptunit->id;

        action_entry(shl,
                     action_enabled_unit_on_unit_local(
                         ACTION_SPY_BRIBE_UNIT, punit, ptunit),
                     _("_Bribe Enemy Unit"),
                     (GCallback)diplomat_bribe_callback);

        action_entry(shl,
                     action_enabled_unit_on_unit_local(
                         ACTION_SPY_SABOTAGE_UNIT, punit, ptunit),
                     _("_Sabotage Enemy Unit"),
                     (GCallback)spy_sabotage_unit_callback);
    }

    if (diplomat_can_do_action(punit, DIPLOMAT_MOVE, dest_tile)) {
        if (pcity) {
            choice_dialog_add(shl, _("_Keep moving"),
                              (GCallback)diplomat_keep_moving_city_callback,
                              NULL, FALSE);
        } else {
            choice_dialog_add(shl, _("_Keep moving"),
                              (GCallback)diplomat_keep_moving_unit_callback,
                              NULL, FALSE);
        }
    }

    choice_dialog_add(shl, GTK_STOCK_CANCEL,
                      (GCallback)diplomat_cancel_callback, NULL, FALSE);

    choice_dialog_end(shl);

    diplomat_dialog = shl;

    choice_dialog_set_hide(shl, TRUE);
    g_signal_connect(shl, "destroy",
                     G_CALLBACK(diplomat_destroy_callback), NULL);
    g_signal_connect(shl, "delete_event",
                     G_CALLBACK(diplomat_cancel_callback), NULL);
    astr_free(&title);
    astr_free(&text);
}