diff mde/content/Items.d @ 128:41582439a42b

Added support for dynamic EnumContent loading and saving, with translation loading. WMScreen.init removed; code moved to this() since class is now created by main() instead of a static this(). Fix for SwitchWidget not passing events. Still some resizing bugs evident in SwitchWidget :-(
author Diggory Hardy <diggory.hardy@gmail.com>
date Wed, 14 Jan 2009 20:24:14 +0000
parents c9843fbaac88
children 264028f4115a
line wrap: on
line diff
--- a/mde/content/Items.d	Thu Jan 08 20:09:46 2009 +0000
+++ b/mde/content/Items.d	Wed Jan 14 20:24:14 2009 +0000
@@ -41,7 +41,8 @@
      * Items.get ("Options.MiscOptions") returns a ContentList of all misc options. */
     Content get (char[] item) {
 	assert (currentL10n is miscOpts.L10n(), "must call loadTranslation (code error)");
-	
+	char[] orig = item;	// item is modified by head()
+        
 	char[] h = head (item);
 	if (h == "Options") {
 	    if (item is null)
@@ -63,11 +64,12 @@
 		return imde.menu;
 	    else if (h == "quit" && item is null)
 		return imde.quit;
-            else if (h == "sw" && item is null)
-                return imde.sw;
-	}
-        logger.warn ("Bad content specifier: {}",h);
-	return new ErrorContent ("Error: bad content specifier",h);
+	} else if (h == "dynamic") {
+            auto i = head (item) in items;
+            if (i) return *i;
+        }
+        
+	return new ErrorContent ("Error: bad content specifier", orig);
     }
     
     /** Creates some content on first run (required by get()).
@@ -88,14 +90,15 @@
 	    Options.allContentList = new ContentList ("Options", list);
 	}
 	
+        Translation trl;
+        Translation.Entry trle;
+        
 	// Translate Options:
-	Translation.Entry trle;
 	with (Options.allContentList) {
 	    trle = Translation.get (symbol).getStruct (symbol);
 	    name (trle.name, trle.desc);
 	}
 	foreach (n,opts; Options.optionsClasses) {
-	    Translation trl;
 	    trl = Translation.get (n);
 	    trle = trl.getStruct (n);
 	    opts.contentList.name (trle.name, trle.desc);
@@ -113,15 +116,42 @@
 	}
 	
 	// Translate imde:
-	trle = Translation.get ("imde").getStruct ("menu");
+        trl = Translation.get ("imde");
+	trle = trl.getStruct ("menu");
 	imde.menu.name (trle.name, trle.desc);
-	trle = Translation.get ("imde").getStruct ("quit");
+	trle = trl.getStruct ("quit");
 	imde.quit.name (trle.name, trle.desc);
+        
+        // Translate dynamic content:
+        if (items.length) {
+            trl = Translation.get ("dynamic");
+            foreach (n,item; items) {
+                trle = trl.getStruct (n);
+                item.name (trle.name, trle.desc);
+                IContentList cl = cast(IContentList) item;
+                if (cl) {
+                    foreach (i,c; cl.list) {
+                        trle = trl.getStruct (c.symbol);
+                        c.name (trle.name, trle.desc);
+                    }
+                }
+            }
+        }
 	
 	currentL10n = miscOpts.L10n();
     }
     
+    /** Add content c with name c.symbol, so that translations are loaded for it and it can be
+     * returned by get ("dynamic."~c.symbol). */
+    Content addContent (Content c) {
+        items[c.symbol] = c;
+        return c;
+    }
+    
 private:
+    // NOTE: possibly add all content to this list. Lookups would be faster.
+    Content[char[]] items;	// dynamically added content
+    
     /** Takes the string "head.tail" where tail may contain '.' but head does not, returns "head",
      * with str set to "tail". */
     char[] head (ref char[] str) {