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

On 09/09/2007, Marko Lindqvist  wrote:
>
>  This patch makes linebreak behavior parameter of common
> get_unit_info_label_text2(), so also gui-sdl can use it. gui-sdl
> copies of these functions removed.

 - Updated comments

 This is for S2_2 and trunk only - don't want to modify common part in S2_1.

 Ready for commit.


 - ML

diff -Nurd -X.diff_ignore freeciv/client/gui-ftwl/gui_text.c freeciv/client/gui-ftwl/gui_text.c
--- freeciv/client/gui-ftwl/gui_text.c	2007-08-13 20:51:07.000000000 +0300
+++ freeciv/client/gui-ftwl/gui_text.c	2007-09-14 21:23:10.000000000 +0300
@@ -145,7 +145,7 @@
   add_line(_("Location: (%d, %d) [%d]"),
 	   ptile->x, ptile->y, ptile->continent);
 #endif
-  add_line("%s", tile_get_info_text(ptile));
+  add_line("%s", tile_get_info_text(ptile, 0));
   if (count > 0) {
     add_line("%s",
 	     get_infrastructure_text(infrastructure));
diff -Nurd -X.diff_ignore freeciv/client/gui-gtk-2.0/mapview.c freeciv/client/gui-gtk-2.0/mapview.c
--- freeciv/client/gui-gtk-2.0/mapview.c	2007-08-04 18:38:42.000000000 +0300
+++ freeciv/client/gui-gtk-2.0/mapview.c	2007-09-14 21:23:10.000000000 +0300
@@ -212,7 +212,7 @@
 		     get_unit_info_label_text1(punits));
 
   gtk_label_set_text(GTK_LABEL(unit_info_label),
-		     get_unit_info_label_text2(punits));
+		     get_unit_info_label_text2(punits, 0));
 
   update_unit_pix_label(punits);
 }
diff -Nurd -X.diff_ignore freeciv/client/gui-gtk-2.0/menu.c freeciv/client/gui-gtk-2.0/menu.c
--- freeciv/client/gui-gtk-2.0/menu.c	2007-08-30 21:50:13.000000000 +0300
+++ freeciv/client/gui-gtk-2.0/menu.c	2007-09-14 21:23:10.000000000 +0300
@@ -1242,7 +1242,7 @@
   struct tile newtile = *ptile;
 
   tile_apply_activity(&newtile, activity);
-  return tile_get_info_text(&newtile);
+  return tile_get_info_text(&newtile, 0);
 }
 
 /****************************************************************
diff -Nurd -X.diff_ignore freeciv/client/gui-sdl/mapview.c freeciv/client/gui-sdl/mapview.c
--- freeciv/client/gui-sdl/mapview.c	2007-08-25 15:28:38.000000000 +0300
+++ freeciv/client/gui-sdl/mapview.c	2007-09-14 21:23:10.000000000 +0300
@@ -473,201 +473,6 @@
   return -1;
 }
 
-/****************************************************************************
-  Return a (static) string with tile name describing terrain and specials.
-  Same as tile_get_info_text() from client/text.c, but with an additional
-  line break
-
-  Examples:
-    "Hills"
-    "Hills (Coals)"
-    "Hills (Coals)
-     [Pollution]"
-****************************************************************************/
-static const char *gui_sdl_tile_get_info_text(const struct tile *ptile)
-{
-  static char s[256];
-  bool first;
-
-  sz_strlcpy(s, terrain_name_translation(ptile->terrain));
-  if (tile_has_special(ptile, S_RIVER)) {
-    sz_strlcat(s, "/");
-    sz_strlcat(s, special_name_translation(S_RIVER));
-  }
-
-  if (ptile->resource) {
-    cat_snprintf(s, sizeof(s), " (%s)",
-		 resource_name_translation(ptile->resource));
-  }
-
-  first = TRUE;
-  if (tile_has_special(ptile, S_POLLUTION)) {
-    if (first) {
-      first = FALSE;
-      sz_strlcat(s, "\n[");
-    } else {
-      sz_strlcat(s, "/");
-    }
-    sz_strlcat(s, special_name_translation(S_POLLUTION));
-  }
-  if (tile_has_special(ptile, S_FALLOUT)) {
-    if (first) {
-      first = FALSE;
-      sz_strlcat(s, "\n[");
-    } else {
-      sz_strlcat(s, "/");
-    }
-    sz_strlcat(s, special_name_translation(S_FALLOUT));
-  }
-  if (!first) {
-    sz_strlcat(s, "]");
-  }
-
-  return s;
-}
-
-/****************************************************************************
-  Return the text body for the unit info shown in the info panel.
-  Same as get_unit_info_label_text2() from client/text.c, but calls
-  gui_sdl_tile_get_info_text() instead.
-
-  FIXME: this should be renamed.
-****************************************************************************/
-static const char *gui_sdl_get_unit_info_label_text2(struct unit_list *punits)
-{
-  static struct astring str = ASTRING_INIT;
-  int count;
-
-  astr_clear(&str);
-
-  if (!punits) {
-    return "";
-  }
-
-  count = unit_list_size(punits);
-
-  /* This text should always have the same number of lines.  Otherwise the
-   * GUI widgets may be confused and try to resize themselves. */
-
-  /* Line 1. Goto or activity text. */
-  if (count > 0 && unit_list_size(hover_units) > 0) {
-    int min, max;
-
-    if (!goto_get_turns(&min, &max)) {
-      astr_add_line(&str, _("Turns to target: %d"), min);
-    } else if (min == max) {
-      astr_add_line(&str, _("Turns to target: %d"), max);
-    } else {
-      astr_add_line(&str, _("Turns to target: %d to %d"), min, max);
-    }
-  } else if (count == 1) {
-    astr_add_line(&str, "%s",
-      unit_activity_text(unit_list_get(punits, 0)));
-  } else if (count > 1) {
-    astr_add_line(&str, PL_("%d unit selected",
-          "%d units selected",
-          count),
-      count);
-  } else {
-    astr_add_line(&str, _("No units selected."));
-  }
-
-  /* Lines 2, 3, and 4 vary. */
-  if (count == 1) {
-    struct unit *punit = unit_list_get(punits, 0);
-    struct city *pcity =
-  player_find_city_by_id(punit->owner, punit->homecity);
-    int infracount;
-    bv_special infrastructure =
-      get_tile_infrastructure_set(punit->tile, &infracount);
-
-    astr_add_line(&str, "%s", gui_sdl_tile_get_info_text(punit->tile));
-    if (infracount > 0) {
-      astr_add_line(&str, "%s", get_infrastructure_text(infrastructure));
-    } else {
-      astr_add_line(&str, " ");
-    }
-    if (pcity) {
-      astr_add_line(&str, "%s", pcity->name);
-    } else {
-      astr_add_line(&str, " ");
-    }
-  } else if (count > 1) {
-    int mil = 0, nonmil = 0;
-    int types_count[U_LAST], i;
-    struct unit_type *top[3];
-
-    memset(types_count, 0, sizeof(types_count));
-    unit_list_iterate(punits, punit) {
-      if (unit_has_type_flag(punit, F_CIVILIAN)) {
-  nonmil++;
-      } else {
-  mil++;
-      }
-      types_count[utype_index(unit_type(punit))]++;
-    } unit_list_iterate_end;
-
-    top[0] = top[1] = top[2] = NULL;
-    unit_type_iterate(utype) {
-      if (!top[2]
-    || types_count[utype_index(top[2])] < types_count[utype_index(utype)]) {
-  top[2] = utype;
-
-  if (!top[1]
-      || types_count[utype_index(top[1])] < types_count[utype_index(top[2])]) {
-    top[2] = top[1];
-    top[1] = utype;
-
-    if (!top[0]
-        || types_count[utype_index(top[0])] < types_count[utype_index(utype)]) {
-      top[1] = top[0];
-      top[0] = utype;
-    }
-  }
-      }
-    } unit_type_iterate_end;
-
-    for (i = 0; i < 3; i++) {
-      if (top[i] && types_count[utype_index(top[i])] > 0) {
-  if (utype_has_flag(top[i], F_CIVILIAN)) {
-    nonmil -= types_count[utype_index(top[i])];
-  } else {
-    mil -= types_count[utype_index(top[i])];
-  }
-  astr_add_line(&str, "%d: %s",
-          types_count[utype_index(top[i])], utype_name_translation(top[i]));
-      } else {
-  astr_add_line(&str, " ");
-      }
-    }
-
-    if (nonmil > 0 && mil > 0) {
-      astr_add_line(&str, _("Others: %d civil; %d military"), nonmil, mil);
-    } else if (nonmil > 0) {
-      astr_add_line(&str, _("Others: %d civilian"), nonmil);
-    } else if (mil > 0) {
-      astr_add_line(&str, _("Others: %d military"), mil);
-    } else {
-      astr_add_line(&str, " ");
-    }
-  } else {
-    astr_add_line(&str, " ");
-    astr_add_line(&str, " ");
-    astr_add_line(&str, " ");
-  }
-
-  /* Line 5. Debug text. */
-#ifdef DEBUG
-  if (count == 1) {
-    astr_add_line(&str, "(Unit ID %d)", unit_list_get(punits, 0)->id);
-  } else {
-    astr_add_line(&str, " ");
-  }
-#endif
-
-  return str.str;
-}
-
 /**************************************************************************
   Read Function Name :)
 **************************************************************************/
@@ -725,7 +530,9 @@
       change_ptsize16(pStr, adj_font(10));
       pStr->style |= SF_CENTER;
 
-      copy_chars_to_string16(pStr, gui_sdl_get_unit_info_label_text2(punitlist));
+      copy_chars_to_string16(pStr,
+                             get_unit_info_label_text2(punitlist,
+                                                       TILE_LB_RESOURCE_POLL));
       pInfo = create_text_surf_from_str16(pStr);
       
       if (pInfo_Window->size.h > 
diff -Nurd -X.diff_ignore freeciv/client/gui-win32/mapview.c freeciv/client/gui-win32/mapview.c
--- freeciv/client/gui-win32/mapview.c	2007-08-28 19:06:09.000000000 +0300
+++ freeciv/client/gui-win32/mapview.c	2007-09-14 21:23:10.000000000 +0300
@@ -199,7 +199,7 @@
 update_unit_info_label(struct unit_list *punitlist)
 {
   SetWindowText(unit_info_frame, get_unit_info_label_text1(punitlist));
-  SetWindowText(unit_info_label, get_unit_info_label_text2(punitlist));
+  SetWindowText(unit_info_label, get_unit_info_label_text2(punitlist, 0));
 
   /* Cursor handling has changed a lot. New form is not yet implemented
    * for gui-win32. Old code below is probably never needed again, but
diff -Nurd -X.diff_ignore freeciv/client/gui-win32/menu.c freeciv/client/gui-win32/menu.c
--- freeciv/client/gui-win32/menu.c	2007-08-28 19:06:09.000000000 +0300
+++ freeciv/client/gui-win32/menu.c	2007-09-14 21:23:10.000000000 +0300
@@ -995,7 +995,7 @@
   struct tile newtile = *ptile;
 
   tile_apply_activity(&newtile, activity);
-  return tile_get_info_text(ptile);
+  return tile_get_info_text(ptile, 0);
 }
 
 /**************************************************************************
diff -Nurd -X.diff_ignore freeciv/client/gui-xaw/mapview.c freeciv/client/gui-xaw/mapview.c
--- freeciv/client/gui-xaw/mapview.c	2007-08-08 22:14:10.000000000 +0300
+++ freeciv/client/gui-xaw/mapview.c	2007-09-14 21:23:10.000000000 +0300
@@ -207,8 +207,8 @@
   char buffer[512];
 
   my_snprintf(buffer, sizeof(buffer), "%s\n%s",
-		get_unit_info_label_text1(punitlist),
-		get_unit_info_label_text2(punitlist));
+              get_unit_info_label_text1(punitlist),
+              get_unit_info_label_text2(punitlist, 0));
   xaw_set_label(unit_info_label, buffer);
 
   if (unit_list_size(punitlist) > 0) {
diff -Nurd -X.diff_ignore freeciv/client/text.c freeciv/client/text.c
--- freeciv/client/text.c	2007-08-25 15:28:38.000000000 +0300
+++ freeciv/client/text.c	2007-09-14 21:25:36.000000000 +0300
@@ -98,7 +98,7 @@
   astr_add_line(&str, _("Location: (%d, %d) [%d]"), 
 		ptile->x, ptile->y, ptile->continent); 
 #endif /*DEBUG*/
-  astr_add_line(&str, _("Terrain: %s"),  tile_get_info_text(ptile));
+  astr_add_line(&str, _("Terrain: %s"),  tile_get_info_text(ptile, 0));
   astr_add_line(&str, _("Food/Prod/Trade: %s"),
 		get_tile_output_text(ptile));
   if (tile_has_special(ptile, S_HUT)) {
@@ -679,7 +679,7 @@
 
   FIXME: this should be renamed.
 ****************************************************************************/
-const char *get_unit_info_label_text2(struct unit_list *punits)
+const char *get_unit_info_label_text2(struct unit_list *punits, int linebreaks)
 {
   static struct astring str = ASTRING_INIT;
   int count;
@@ -692,8 +692,11 @@
 
   count = unit_list_size(punits);
 
-  /* This text should always have the same number of lines.  Otherwise the
-   * GUI widgets may be confused and try to resize themselves. */
+  /* This text should always have the same number of lines if
+   * 'linebreaks' has no flags at all. Otherwise the GUI widgets may be
+   * confused and try to resize themselves. If caller asks for
+   * conditional 'linebreaks', it should take care of these problems
+   * itself. */
 
   /* Line 1. Goto or activity text. */
   if (count > 0 && unit_list_size(hover_units) > 0) {
@@ -727,7 +730,7 @@
     bv_special infrastructure =
       get_tile_infrastructure_set(punit->tile, &infracount);
 
-    astr_add_line(&str, "%s", tile_get_info_text(punit->tile));
+    astr_add_line(&str, "%s", tile_get_info_text(punit->tile, linebreaks));
     if (infracount > 0) {
       astr_add_line(&str, "%s", get_infrastructure_text(infrastructure));
     } else {
diff -Nurd -X.diff_ignore freeciv/client/text.h freeciv/client/text.h
--- freeciv/client/text.h	2007-08-13 20:51:08.000000000 +0300
+++ freeciv/client/text.h	2007-09-14 21:23:10.000000000 +0300
@@ -37,7 +37,7 @@
 const char *get_nuclear_winter_tooltip(void);
 const char *get_government_tooltip(void);
 const char *get_unit_info_label_text1(struct unit_list *punits);
-const char *get_unit_info_label_text2(struct unit_list *punits);
+const char *get_unit_info_label_text2(struct unit_list *punits, int linebreaks);
 bool get_units_upgrade_info(char *buf, size_t bufsz,
 			    struct unit_list *punits);
 const char *get_spaceship_descr(struct player_spaceship *pship);
diff -Nurd -X.diff_ignore freeciv/common/tile.c freeciv/common/tile.c
--- freeciv/common/tile.c	2007-08-06 16:41:57.000000000 +0300
+++ freeciv/common/tile.c	2007-09-14 21:23:10.000000000 +0300
@@ -555,6 +555,34 @@
 }
 
 /****************************************************************************
+  Add one entry about pollution situation to buffer.
+  Return if there has been any pollution (even prior calling this)
+****************************************************************************/
+static bool tile_info_pollution(char *buf, int bufsz,
+                                const struct tile *ptile,
+                                enum tile_special_type special,
+                                bool prevp, bool linebreak)
+{
+  if (tile_has_special(ptile, special)) {
+    if (!prevp) {
+      if (linebreak) {
+        mystrlcat(buf, "\n[", bufsz);
+      } else {
+        mystrlcat(buf, " [", bufsz);
+      }
+    } else {
+      mystrlcat(buf, "/", bufsz);
+    }
+
+    mystrlcat(buf, special_name_translation(special), bufsz);
+
+    return TRUE;
+  }
+
+  return prevp;
+}
+
+/****************************************************************************
   Return a (static) string with tile name describing terrain and specials.
 
   Examples:
@@ -562,41 +590,53 @@
     "Hills (Coals)"
     "Hills (Coals) [Pollution]"
 ****************************************************************************/
-const char *tile_get_info_text(const struct tile *ptile)
+const char *tile_get_info_text(const struct tile *ptile, int linebreaks)
 {
   static char s[256];
-  bool first;
+  int bufsz;
+  bool pollution;
+  bool lb = FALSE;
 
-  sz_strlcpy(s, terrain_name_translation(ptile->terrain));
-  if (tile_has_special(ptile, S_RIVER)) {
-    sz_strlcat(s, "/");
-    sz_strlcat(s, special_name_translation(S_RIVER));
-  }
+  bufsz = sizeof(s);
 
-  if (ptile->resource) {
-    cat_snprintf(s, sizeof(s), " (%s)", resource_name_translation(ptile->resource));
+  sz_strlcpy(s, terrain_name_translation(ptile->terrain));
+  if (linebreaks & TILE_LB_TERRAIN_RIVER) {
+    /* Linebreak needed before next text */
+    lb = TRUE;
   }
 
-  first = TRUE;
-  if (tile_has_special(ptile, S_POLLUTION)) {
-    if (first) {
-      first = FALSE;
-      sz_strlcat(s, " [");
+  if (tile_has_special(ptile, S_RIVER)) {
+    if (lb) {
+      sz_strlcat(s, "\n");
+      lb = FALSE;
     } else {
       sz_strlcat(s, "/");
     }
-    sz_strlcat(s, special_name_translation(S_POLLUTION));
+    sz_strlcat(s, special_name_translation(S_RIVER));
   }
-  if (tile_has_special(ptile, S_FALLOUT)) {
-    if (first) {
-      first = FALSE;
-      sz_strlcat(s, " [");
+  if (linebreaks & TILE_LB_RIVER_RESOURCE) {
+    /* New linebreak requested */
+    lb = TRUE;
+  }
+
+  if (ptile->resource) {
+    if (lb) {
+      sz_strlcat(s, "\n");
+      lb = FALSE;
     } else {
-      sz_strlcat(s, "/");
+      sz_strlcat(s, " ");
     }
-    sz_strlcat(s, special_name_translation(S_FALLOUT));
+    cat_snprintf(s, sizeof(s), "(%s)", resource_name_translation(ptile->resource));
   }
-  if (!first) {
+  if (linebreaks & TILE_LB_RESOURCE_POLL) {
+    /* New linebreak requested */
+    lb = TRUE;
+  }
+
+  pollution = FALSE;
+  pollution = tile_info_pollution(s, bufsz, ptile, S_POLLUTION, pollution, lb);
+  pollution = tile_info_pollution(s, bufsz, ptile, S_FALLOUT, pollution, lb);
+  if (pollution) {
     sz_strlcat(s, "]");
   }
 
diff -Nurd -X.diff_ignore freeciv/common/tile.h freeciv/common/tile.h
--- freeciv/common/tile.h	2007-08-13 20:51:03.000000000 +0300
+++ freeciv/common/tile.h	2007-09-14 21:23:10.000000000 +0300
@@ -96,6 +96,9 @@
 void tile_remove_special(struct tile *ptile, enum tile_special_type special);
 bool tile_apply_activity(struct tile *ptile, Activity_type_id act);
 
-const char *tile_get_info_text(const struct tile *ptile);
+#define TILE_LB_TERRAIN_RIVER    (1 << 0)
+#define TILE_LB_RIVER_RESOURCE   (1 << 1)
+#define TILE_LB_RESOURCE_POLL    (1 << 2)
+const char *tile_get_info_text(const struct tile *ptile, int linebreaks);
 
 #endif /* FC__TILE_H */
diff -Nurd -X.diff_ignore freeciv/server/sanitycheck.c freeciv/server/sanitycheck.c
--- freeciv/server/sanitycheck.c	2007-08-13 20:51:02.000000000 +0300
+++ freeciv/server/sanitycheck.c	2007-09-14 21:23:10.000000000 +0300
@@ -404,7 +404,7 @@
 	freelog(LOG_ERROR, "%s at %d,%d (%s) has activity %s, "
 		"which it can't continue!",
 		unit_rule_name(punit),
-		TILE_XY(ptile), tile_get_info_text(ptile),
+		TILE_XY(ptile), tile_get_info_text(ptile, 0),
 		get_activity_text(punit->activity));
       }
 
diff -Nurd -X.diff_ignore freeciv/server/settlers.c freeciv/server/settlers.c
--- freeciv/server/settlers.c	2007-08-26 22:29:37.000000000 +0300
+++ freeciv/server/settlers.c	2007-09-14 21:23:10.000000000 +0300
@@ -994,7 +994,7 @@
       if (same_pos(punit->tile, ptile)) {
         if (!ai_do_build_city(pplayer, punit)) {
           UNIT_LOG(LOG_DEBUG, punit, "could not make city on %s",
-                   tile_get_info_text(punit->tile));
+                   tile_get_info_text(punit->tile, 0));
           ai_unit_new_role(punit, AIUNIT_NONE, NULL);
           /* Only known way to end in here is that hut turned in to a city
            * when settler entered tile. So this is not going to lead in any
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to