Merge branch 't/testsuite'
[paraslash.git] / gui_theme.c
index baa642472e2674cc4a35e427a0e4f5e076d39b43..ffa5d792ee199f16e5bb26c5e6557054520f8e5c 100644 (file)
@@ -1,22 +1,17 @@
 /*
- * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
+#include <stdbool.h>
 #include "para.h"
 #include "gui.h"
 #include <curses.h>
 
-#define NUM_THEMES 2
-
-
-static int current_theme_num;
-
 static void init_theme_simple(struct gui_theme *t)
 {
        struct stat_item_data *d = t->data;
-       t->name = "simple";
        t->author = "Andre Noll";
        t->lines_min = 5;
        t->top_lines_min = 2;
@@ -72,7 +67,6 @@ static void init_theme_simple(struct gui_theme *t)
 static void init_theme_colorful_blackness(struct gui_theme *t)
 {
        struct stat_item_data *d = t->data;
-       t->name = "colorful blackness";
        t->author = "Andre Noll";
        /* minimal number of lines that is needed to display all
         * information provided by this theme
@@ -363,24 +357,58 @@ static void init_theme_colorful_blackness(struct gui_theme *t)
        d[SI_DIRECTORY].len = 100;
 }
 
-void init_theme(int num, struct gui_theme *t)
+struct theme_description {
+       const char *name;
+       void (*init)(struct gui_theme *t);
+};
+
+struct theme_description themes[] = {
+       {
+               .name = "colorful blackness",
+               .init = init_theme_colorful_blackness,
+       },
+       {
+               .name = "simple",
+               .init = init_theme_simple,
+       },
+};
+
+#define NUM_THEMES (ARRAY_SIZE(themes))
+
+static int current_theme_num;
+
+static void set_theme(int num, struct gui_theme *t)
 {
        int i;
        FOR_EACH_STATUS_ITEM(i)
                t->data[i].len = 0;
+       num %= NUM_THEMES;
+       t->name = themes[num].name;
+       themes[num].init(t);
        current_theme_num = num;
+}
 
-       return (num % NUM_THEMES)?
-               init_theme_simple(t) : init_theme_colorful_blackness(t);
+void init_theme_or_die(const char *name, struct gui_theme *t)
+{
+       int i;
+
+       if (!name)
+               return set_theme(0, t);
+       for (i = 0; i < NUM_THEMES; i++)
+               if (strcmp(name, themes[i].name) == 0)
+                       return set_theme(i, t);
+       fprintf(stderr, "Available themes:\n");
+       for (i = 0; i < NUM_THEMES; i++)
+               fprintf(stderr, "\t%s\n", themes[i].name);
+       exit(EXIT_FAILURE);
 }
 
 void prev_theme(struct gui_theme *t)
 {
-       return init_theme(++current_theme_num, t);
+       return set_theme(++current_theme_num, t);
 }
 
 void next_theme(struct gui_theme *t)
 {
-       return init_theme(--current_theme_num, t);
+       return set_theme(--current_theme_num, t);
 }
-