ecere/samples/gui: Added 'TextScroller', showing how to display and scroll more text...
authorJerome St-Louis <jerome@ecere.com>
Sat, 4 Feb 2012 17:36:06 +0000 (00:36 +0700)
committerJerome St-Louis <jerome@ecere.com>
Sat, 4 Feb 2012 17:36:06 +0000 (00:36 +0700)
samples/guiAndGfx/textScroller/Scroll.epj [new file with mode: 0644]
samples/guiAndGfx/textScroller/textScroller.ec [new file with mode: 0644]

diff --git a/samples/guiAndGfx/textScroller/Scroll.epj b/samples/guiAndGfx/textScroller/Scroll.epj
new file mode 100644 (file)
index 0000000..a6e9814
--- /dev/null
@@ -0,0 +1,38 @@
+{
+   "Version" : 0.2,
+   "ModuleName" : "Scroll",
+   "Options" : {
+      "Warnings" : "All",
+      "TargetType" : "Executable",
+      "TargetFileName" : "Scroll",
+      "Libraries" : [
+         "ecere"
+      ]
+   },
+   "Configurations" : [
+      {
+         "Name" : "Debug",
+         "Options" : {
+            "Debug" : true,
+            "Optimization" : "None",
+            "PreprocessorDefinitions" : [
+               "_DEBUG"
+            ]
+         }
+      },
+      {
+         "Name" : "Release",
+         "Options" : {
+            "Debug" : false,
+            "Optimization" : "Speed"
+         }
+      }
+   ],
+   "Files" : [
+      "textScroller.ec"
+   ],
+   "ResourcesPath" : "",
+   "Resources" : [
+
+   ]
+}
\ No newline at end of file
diff --git a/samples/guiAndGfx/textScroller/textScroller.ec b/samples/guiAndGfx/textScroller/textScroller.ec
new file mode 100644 (file)
index 0000000..099c19c
--- /dev/null
@@ -0,0 +1,114 @@
+public import "ecere"
+
+TextScrollerDemo demo {};
+
+class TextScrollerDemo : Window
+{
+   text = "Text Scroller Demo";
+   background = activeBorder;
+   borderStyle = sizable;
+   hasMaximize = true;
+   hasMinimize = true;
+   hasClose = true;
+   size = { 576, 432 };
+   TextScroller scroller
+   {
+      this, anchor = { 10, 10, 10, 10 };
+      contents = "Could not load ./textScroller.ec";
+   };
+
+   TextScrollerDemo()
+   {
+      File f = FileOpen("textScroller.ec", read);
+      if(f)
+      {
+         uint size = f.GetSize();
+         String s = new char[size+1];
+         f.Read(s, 1, size);
+         scroller.contents = s;
+         delete f;
+      }
+   }
+}
+
+public class TextScroller : Window
+{
+   borderStyle = deepContour;
+   font = { "Consolas", 12 };
+   background = black;
+   foreground = lime;
+
+   hasHorzScroll = true;
+   hasVertScroll = true;
+
+   int maxW, lh;
+   int linesCount;
+   String contents;
+   String linesText;
+   char * lines[1024];
+
+   ~TextScroller()
+   {
+      delete linesText;
+      delete contents;
+   }
+   
+   property String contents
+   {
+      set
+      {
+         delete contents;
+         contents = CopyString(value);
+         delete linesText;
+         linesText = CopyString(contents);
+         // Lazy lines splitting: it will unfortunately ignore empty lines...
+         linesCount = TokenizeWith(linesText, sizeof(lines), lines, "\n", false);
+         OnLoadGraphics();
+      }
+      get { return contents; }
+   };
+   
+   bool OnLoadGraphics()
+   {
+      int c;
+      maxW = 0;
+      display.FontExtent(fontObject, " ", 1, null, &lh);
+      for(c = 0; c < linesCount; c++)
+      {
+         int w;
+         display.FontExtent(fontObject, lines[c], strlen(lines[c]), &w, null);
+         if(w > maxW) maxW = w;
+      }
+      snapVertScroll = lh; // Snap to line height vertically
+      snapHorzScroll = 1;
+      scrollArea = { maxW, lh * (linesCount-1) };
+      return true;
+   }
+
+   void OnRedraw(Surface surface)
+   {
+      int c;
+      int x = -scroll.x, y = -scroll.y;
+      int ch = clientSize.h;
+      for(c = 0; c < linesCount; c++, y += lh)
+      {
+         int w;
+         if(y > -lh && y < ch)
+            surface.WriteText(x, y, lines[c], strlen(lines[c]));
+      }
+   }
+
+   // *** Brute force implementation ***
+   // Could be optimized by using display.Scroll(), and only updating
+   // the new portion of the screen
+   // (See sdk/ecere/src/gui/controls/EditBox.ec for example)
+   void OnVScroll(ScrollBarAction action, int position, Key key)
+   {
+      Update(null);
+   }
+
+   void OnHScroll(ScrollBarAction action, int position, Key key)
+   {
+      Update(null);
+   }
+}