<URL: http://bugs.freeciv.org/Ticket/Display.html?id=40624 >

> [cazf...@gmail.com - Sun Jan 04 21:38:32 2009]:
> 
>  Maybe it should be impossible to select military base tool at all
> when ruleset has no base types at all. Currently it ends printing
> error message "...because 0 is not a valid base type id" (this is not
> too bad, at least it doesn't crash)

Attached patch makes the editor gui popup an error message if
the ruleset does not define any values for the tool the user
tries to select.


-----------------------------------------------------------------------
エラー。定義されていない現実。
 client/editor.c              |   30 ++++++++++++++++++++++++++++++
 client/editor.h              |    1 +
 client/gui-gtk-2.0/editgui.c |   30 ++++++++++++++++++++++++++++--
 3 files changed, 59 insertions(+), 2 deletions(-)

diff --git a/client/editor.c b/client/editor.c
index d5b89f8..a09be2c 100644
--- a/client/editor.c
+++ b/client/editor.c
@@ -270,6 +270,36 @@ bool editor_is_active(void)
 }
 
 /****************************************************************************
+  Returns TRUE if the given tool should be made availble to the user via
+  the editor GUI. For example, this will return FALSE for ETT_MILITARY_BASE
+  if there are no bases defined in the ruleset.
+
+  NB: This depends on the ruleset information received from the server, so
+  it will return FALSE if the client does not have it yet.
+****************************************************************************/
+bool editor_tool_is_usable(enum editor_tool_type ett)
+{
+  if (!editor || !(0 <= ett && ett < NUM_EDITOR_TOOL_TYPES)) {
+    return FALSE;
+  }
+
+  switch (ett) {
+  case ETT_MILITARY_BASE:
+    return base_count() > 0;
+    break;
+  case ETT_TERRAIN_RESOURCE:
+    return resource_count() > 0;
+    break;
+  case ETT_UNIT:
+    return utype_count() > 0;
+    break;
+  default:
+    break;
+  }
+  return TRUE;
+}
+
+/****************************************************************************
   Returns TRUE if the given tool type has sub-values (e.g. the terrain
   tool has values corresponding to the terrain types).
 ****************************************************************************/
diff --git a/client/editor.h b/client/editor.h
index 093c509..2388720 100644
--- a/client/editor.h
+++ b/client/editor.h
@@ -82,6 +82,7 @@ int editor_tool_get_applied_player(enum editor_tool_type ett);
 void editor_tool_set_applied_player(enum editor_tool_type,
                                     int player_no);
 
+bool editor_tool_is_usable(enum editor_tool_type ett);
 bool editor_tool_has_value(enum editor_tool_type ett);
 bool editor_tool_has_value_erase(enum editor_tool_type ett);
 int editor_tool_get_value(enum editor_tool_type ett);
diff --git a/client/gui-gtk-2.0/editgui.c b/client/gui-gtk-2.0/editgui.c
index 2168cc2..4bb25ed 100644
--- a/client/gui-gtk-2.0/editgui.c
+++ b/client/gui-gtk-2.0/editgui.c
@@ -97,6 +97,7 @@ static void editbar_mode_button_toggled(GtkToggleButton *tb,
                                         gpointer userdata);
 static void editbar_tool_button_toggled(GtkToggleButton *tb,
                                         gpointer userdata);
+static void try_to_set_editor_tool(enum editor_tool_type ett);
 
 static struct editbar *editor_toolbar;
 static struct editinfobox *editor_infobox;
@@ -162,6 +163,31 @@ static void editbar_mode_button_toggled(GtkToggleButton *tb,
 }
 
 /****************************************************************************
+  Try to set the given tool as the current editor tool. If the tool is
+  unavailable (editor_tool_is_usable) an error popup is displayed.
+****************************************************************************/
+static void try_to_set_editor_tool(enum editor_tool_type ett)
+{
+  if (!(0 <= ett && ett < NUM_EDITOR_TOOL_TYPES)) {
+    return;
+  }
+
+  if (!editor_tool_is_usable(ett)) {
+    GtkWidget *dialog;
+    dialog = gtk_message_dialog_new(GTK_WINDOW(toplevel),
+        GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+        GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s",
+        _("The current ruleset does not define any "
+          "objects corresponding to this editor tool."));
+    gtk_window_set_title(GTK_WINDOW(dialog), editor_tool_get_name(ett));
+    gtk_dialog_run(GTK_DIALOG(dialog));
+    gtk_widget_destroy(dialog);
+  } else {
+    editor_set_tool(ett);
+  }
+}
+
+/****************************************************************************
   Callback to handle toggling of any of the tool buttons.
 ****************************************************************************/
 static void editbar_tool_button_toggled(GtkToggleButton *tb,
@@ -174,7 +200,7 @@ static void editbar_tool_button_toggled(GtkToggleButton *tb,
   ett = GPOINTER_TO_INT(userdata);
 
   if (active) {
-    editor_set_tool(ett);
+    try_to_set_editor_tool(ett);
     editgui_refresh();
   }
 }
@@ -1712,7 +1738,7 @@ gboolean handle_edit_key_press(GdkEventKey *ev)
   }
 
   if (new_ett != NUM_EDITOR_TOOL_TYPES) {
-    editor_set_tool(new_ett);
+    try_to_set_editor_tool(new_ett);
   }
 
   editgui_refresh();
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to