+
+
+/* Create Winconf, initialize ->view/height_type/width_type to 0, ->id to "id"
+ * and ->draw to "f".
+ */
+static void create_winconf(char id, struct WinConf * wcp,
+ void (* f) (struct Win *));
+
+/* Initialize Winconf of "id" from appropriate config file.*/
+static void init_winconf_from_file(struct World * world, char id);
+
+/* Wrapper around init_win() called with values from Winconf of "id". */
+static void init_win_from_winconf(struct World * world, char id);
+
+/* Save title, size of window identified by "id" to its configuration file. */
+static void save_win_config(struct World * world, char id);
+
+
+
+/* Write size of a window to its WinConf, as positive or negative values
+ * (dependent on state ofWinConf->height_type / WinConf->width_type).
+ */
+static void set_winconf(struct World * world, char id);
+
+
+
+/* Get WinConf by "id"; get id of WinConf mothering "win". */
+static struct WinConf * get_winconf_by_id(struct World * world, char id);
+
+
+
+static char * string_prefixed_id(struct World * world, char * prefix, char id)
+{
+ uint8_t size = strlen(prefix) + 2;
+ char * path = try_malloc(size, world, "string_prefixed_id()");
+ sprintf(path, "%s_", prefix);
+ path[size - 2] = id;
+ return path;
+}
+
+
+
+static void create_winconf(char id, struct WinConf * wcp,
+ void (* f) (struct Win *))
+{
+ wcp->id = id;
+ wcp->draw = f;
+ wcp->view = 0;
+ wcp->height_type = 0;
+ wcp->width_type = 0;
+}
+
+
+
+static void init_winconf_from_file(struct World * world, char id)
+{
+ char * f_name = "init_winconf_from_file()";
+
+ char * path = string_prefixed_id(world, "config/windows/Win_", id);
+ FILE * file = try_fopen(path, "r", world, f_name);
+ free(path);
+ uint16_t linemax = get_linemax(file, world, f_name);
+ char line[linemax + 1];
+
+ struct WinConf * winconf = get_winconf_by_id(world, id);
+ try_fgets(line, linemax + 1, file, world, f_name);
+ winconf->title = try_malloc(strlen(line), world, f_name);
+ memcpy(winconf->title, line, strlen(line) - 1); /* Eliminate newline char */
+ winconf->title[strlen(line) - 1] = '\0'; /* char at end of string. */
+ try_fgets(line, linemax + 1, file, world, f_name);
+ winconf->height = atoi(line);
+ if (0 >= winconf->height)
+ {
+ winconf->height_type = 1;
+ }
+ try_fgets(line, linemax + 1, file, world, f_name);
+ winconf->width = atoi(line);
+ if (0 >= winconf->width)
+ {
+ winconf->width_type = 1;
+ }
+
+ try_fclose(file, world, f_name);
+}
+
+
+
+static void init_win_from_winconf(struct World * world, char id)
+{
+ char * err = "Trouble in init_win_from_file() with init_win().";
+ struct WinConf * winconf = get_winconf_by_id(world, id);
+ exit_err(init_win(world->wmeta, &winconf->win, winconf->title,
+ winconf->height, winconf->width, world, winconf->draw),
+ world, err);
+}
+
+
+
+extern void save_win_config(struct World * world, char id)
+{
+ char * f_name = "save_win_config()";
+
+ char * path_tmp = string_prefixed_id(world, "config/windows/Win_tmp_", id);
+ FILE * file = try_fopen(path_tmp, "w", world, f_name);
+
+ struct WinConf * wc = get_winconf_by_id(world, id);
+ uint8_t size = strlen(wc->title) + 2;
+ if (size < 7)
+ {
+ size = 7;
+ }
+ char line[size];
+ sprintf(line, "%s\n", wc->title);
+ fwrite(line, sizeof(char), strlen(line), file);
+ sprintf(line, "%d\n", wc->height);
+ fwrite(line, sizeof(char), strlen(line), file);
+ sprintf(line, "%d\n", wc->width);
+ fwrite(line, sizeof(char), strlen(line), file);
+
+ char * path = string_prefixed_id(world, "config/windows/Win_", id);
+ try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
+ free(path);
+ free(path_tmp);
+}
+
+
+
+static void set_winconf(struct World * world, char id)
+{
+ struct WinConf * wcp = get_winconf_by_id(world, id);
+ if (0 == wcp->height_type)
+ {
+ wcp->height = wcp->win->frame.size.y;
+ }
+ else if (1 == wcp->height_type)
+ {
+ wcp->height = wcp->win->frame.size.y - world->wmeta->padframe.size.y
+ + 1;
+ }
+ if (0 == wcp->width_type)
+ {
+ wcp->width = wcp->win->frame.size.x;
+ }
+ else if (1 == wcp->width_type)
+ {
+ wcp->width = wcp->win->frame.size.x - world->wmeta->padframe.size.x;
+ }
+}
+
+
+
+static struct WinConf * get_winconf_by_id(struct World * world, char id)