RETURN key now records default slide for startup



git-svn-id: http://skia.googlecode.com/svn/trunk@2925 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/samplecode/SampleApp.cpp b/samplecode/SampleApp.cpp
index 0a0aa2c..746314b 100644
--- a/samplecode/SampleApp.cpp
+++ b/samplecode/SampleApp.cpp
@@ -78,7 +78,56 @@
     evt->setTargetID(sink->getSinkID())->post();
 }
 
-///////////////
+///////////////////////////////////////////////////////////////////////////////
+
+static const char* skip_until(const char* str, const char* skip) {
+    if (!str) {
+        return NULL;
+    }
+    return strstr(str, skip);
+}
+
+static const char* skip_past(const char* str, const char* skip) {
+    const char* found = skip_until(str, skip);
+    if (!found) {
+        return NULL;
+    }
+    return found + strlen(skip);
+}
+
+static const char* gPrefFileName = "sampleapp_prefs.txt";
+
+static bool readTiTleFromPrefs(SkString* title) {
+    SkFILEStream stream(gPrefFileName);
+    if (!stream.isValid()) {
+        return false;
+    }
+
+    int len = stream.getLength();
+    SkString data(len);
+    stream.read(data.writable_str(), len);
+    const char* s = data.c_str();
+
+    s = skip_past(s, "curr-slide-title");
+    s = skip_past(s, "=");
+    s = skip_past(s, "\"");
+    const char* stop = skip_until(s, "\"");
+    if (stop > s) {
+        title->set(s, stop - s);
+        return true;
+    }
+    return false;
+}
+
+static bool writeTitleToPrefs(const char* title) {
+    SkFILEWStream stream(gPrefFileName);
+    SkString data;
+    data.printf("curr-slide-title = \"%s\"\n", title);
+    stream.write(data.c_str(), data.size());
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
 class SampleWindow::DefaultDeviceManager : public SampleWindow::DeviceManager {
 public:
 
@@ -488,6 +537,18 @@
     return iter.next();
 }
 
+static bool curr_title(SkWindow* wind, SkString* title) {
+    SkView* view = curr_view(wind);
+    if (view) {
+        SkEvent evt(gTitleEvtName);
+        if (view->doQuery(&evt)) {
+            title->set(evt.findString(gTitleEvtName));
+            return true;
+        }
+    }
+    return false;
+}
+
 void SampleWindow::setZoomCenter(float x, float y)
 {
     fZoomCenterX = SkFloatToScalar(x);
@@ -548,7 +609,6 @@
 #endif
     fUseClip = false;
     fNClip = false;
-    fRepeatDrawing = false;
     fAnimating = false;
     fRotate = false;
     fPerspAnim = false;
@@ -645,17 +705,19 @@
     }
     fCurrIndex = 0;
     if (argc > 1) {
-        int i, count = fSamples.count();
-        for (i = 0; i < count; i++) {
-            SkString title = getSampleTitle(i);
-            if (title.equals(argv[1])) {
-                fCurrIndex = i;
-                break;
-            }
-        }
-        if (i == count) {
+        fCurrIndex = findByTitle(argv[1]);
+        if (fCurrIndex < 0) {
             fprintf(stderr, "Unknown sample \"%s\"\n", argv[1]);
         }
+    } else {
+        SkString title;
+        if (readTiTleFromPrefs(&title)) {
+            fCurrIndex = findByTitle(title.c_str());
+        }
+    }
+
+    if (fCurrIndex < 0) {
+        fCurrIndex = 0;
     }
     this->loadView((*fSamples[fCurrIndex])());
     
@@ -690,6 +752,15 @@
     SkSafeUnref(fDevManager);
 }
 
+int SampleWindow::findByTitle(const char title[]) {
+    int i, count = fSamples.count();
+    for (i = 0; i < count; i++) {
+        if (getSampleTitle(i).equals(title)) {
+            return i;
+        }
+    }
+}
+
 static SkBitmap capture_bitmap(SkCanvas* canvas) {
     SkBitmap bm;
     const SkBitmap& src = canvas->getDevice()->accessBitmap(false);
@@ -927,9 +998,6 @@
 }
 
 void SampleWindow::onDraw(SkCanvas* canvas) {
-    if (fRepeatDrawing) {
-        this->inval(NULL);
-    }
 }
 
 #include "SkColorPriv.h"
@@ -1554,18 +1622,13 @@
                 this->updateTitle();
             }
             return true;
-        case kOK_SkKey:
-            if (false) {
-                SkDebugfDumper dumper;
-                SkDumpCanvas dc(&dumper);
-                this->draw(&dc);
-            } else {
-                fRepeatDrawing = !fRepeatDrawing;
-                if (fRepeatDrawing) {
-                    this->inval(NULL);
-                }
+        case kOK_SkKey: {
+            SkString title;
+            if (curr_title(this, &title)) {
+                writeTitleToPrefs(title.c_str());
             }
             return true;
+        }
         case kBack_SkKey:
             this->showOverview();
             return true;
@@ -1700,15 +1763,10 @@
 }
 
 void SampleWindow::updateTitle() {
-    SkString title;
+    SkView* view = curr_view(this);
 
-    SkView::F2BIter iter(this);
-    SkView* view = iter.next();
-    SkEvent evt(gTitleEvtName);
-    if (view->doQuery(&evt)) {
-        title.set(evt.findString(gTitleEvtName));
-    }
-    if (title.size() == 0) {
+    SkString title;
+    if (!curr_title(this, &title)) {
         title.set("<unknown>");
     }