diff options
author | 2011-12-27 22:33:50 +0000 | |
---|---|---|
committer | 2011-12-27 22:33:50 +0000 | |
commit | 6f6e8c39a7b07f8a916a60ad27f17bd13f3c89ad (patch) | |
tree | 1dd669330b69edd52e81fed70ab8dea76d5749a0 /samplecode/SampleApp.cpp | |
parent | 49eac192faa35159752525b23345563252721c64 (diff) |
RETURN key now records default slide for startup
git-svn-id: http://skia.googlecode.com/svn/trunk@2925 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'samplecode/SampleApp.cpp')
-rw-r--r-- | samplecode/SampleApp.cpp | 122 |
1 files changed, 90 insertions, 32 deletions
diff --git a/samplecode/SampleApp.cpp b/samplecode/SampleApp.cpp index 0a0aa2c263..746314b2b7 100644 --- a/samplecode/SampleApp.cpp +++ b/samplecode/SampleApp.cpp @@ -78,7 +78,56 @@ static void postEventToSink(SkEvent* evt, SkEventSink* sink) { 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 @@ static SkView* curr_view(SkWindow* wind) { 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 @@ SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* dev #endif fUseClip = false; fNClip = false; - fRepeatDrawing = false; fAnimating = false; fRotate = false; fPerspAnim = false; @@ -645,17 +705,19 @@ SampleWindow::SampleWindow(void* hwnd, int argc, char** argv, DeviceManager* dev } 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 @@ SampleWindow::~SampleWindow() { 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::showZoomer(SkCanvas* canvas) { } void SampleWindow::onDraw(SkCanvas* canvas) { - if (fRepeatDrawing) { - this->inval(NULL); - } } #include "SkColorPriv.h" @@ -1554,18 +1622,13 @@ bool SampleWindow::onHandleKey(SkKey key) { 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 @@ static const char* trystate_str(SkOSMenu::TriState state, } 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>"); } |