Set up global options for SampleApp and now allows SampleApp to request menu items from the slides
http://codereview.appspot.com/4807056/


git-svn-id: http://skia.googlecode.com/svn/trunk@2024 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/samplecode/SampleApp.cpp b/samplecode/SampleApp.cpp
index f548b60..0d70f66 100644
--- a/samplecode/SampleApp.cpp
+++ b/samplecode/SampleApp.cpp
@@ -431,13 +431,32 @@
     fFatBitsScale = 8;
     fTypeface = SkTypeface::CreateFromTypeface(NULL, SkTypeface::kBold);
     fShowZoomer = false;
-
+    
     fZoomLevel = 0;
     fZoomScale = SK_Scalar1;
 
     fSaveToPdf = false;
     fPdfCanvas = NULL;
 
+    int sinkID = this->getSinkID();
+    fAppMenu.setTitle("Global Settings");
+    fAppMenu.appendList("Device Type", "Device Type", sinkID, 0, "Raster", "Picture", "OpenGL", NULL);
+    fAppMenu.appendTriState("AA", "AA", sinkID, SkOSMenu::kMixedState);
+    fAppMenu.appendTriState("LCD", "LCD", sinkID, SkOSMenu::kMixedState);
+    fAppMenu.appendTriState("Filter", "Filter", sinkID, SkOSMenu::kMixedState);
+    fAppMenu.appendTriState("Hinting", "Hinting", sinkID, SkOSMenu::kMixedState);
+    fAppMenu.appendSwitch("Pipe", "Pipe" , sinkID, fUsePipe);    
+    fAppMenu.appendSwitch("Slide Show", "Slide Show" , sinkID, false);    
+    fAppMenu.appendSwitch("Clip", "Clip" , sinkID, fUseClip);    
+    fAppMenu.appendSwitch("Measure FPS", "Measure FPS" , sinkID, fMeasureFPS);    
+    fAppMenu.appendSwitch("Flip X", "Flip X" , sinkID, false);    
+    fAppMenu.appendSwitch("Flip Y", "Flip Y" , sinkID, false);
+    fAppMenu.appendSwitch("Zoomer", "Zoomer" , sinkID, fShowZoomer);
+    fAppMenu.appendAction("Save to PDF", sinkID);
+    
+    this->addMenu(&fAppMenu);
+    this->addMenu(&fSlideMenu);
+    
 //    this->setConfig(SkBitmap::kRGB_565_Config);
     this->setConfig(SkBitmap::kARGB_8888_Config);
     this->setVisibleP(true);
@@ -578,7 +597,7 @@
         m.preTranslate(-cx, -cy);
         canvas->concat(m);
     }
-
+    
     // Apply any gesture matrix
     if (true) {
         const SkMatrix& localM = fGesture.localM();
@@ -592,7 +611,7 @@
             this->inval(NULL);
         }
     }
-
+    
     if (fNClip) {
         this->INHERITED::draw(canvas);
         SkBitmap orig = capture_bitmap(canvas);
@@ -629,7 +648,7 @@
     if (fShowZoomer && !fSaveToPdf) {
         showZoomer(canvas);
     }
-
+    
     // do this last
     fDevManager->publishCanvas(fDeviceType, canvas, this);
 }
@@ -1005,6 +1024,70 @@
         this->inval(NULL);
         return true;
     }
+    int selected = -1;
+    if (SkOSMenu::FindListIndex(&evt, "Device Type", &selected)) {
+        this->setDeviceType((DeviceType)selected);
+        return true; 
+    }
+    SkOSMenu::TriState state;
+    if (SkOSMenu::FindTriState(&evt, "AA", &state)) {
+        fAAState = (SkTriState)state;
+        this->updateTitle();
+        this->inval(NULL);
+    }
+    if (SkOSMenu::FindTriState(&evt, "LCD", &state)) {
+        fLCDState = (SkTriState)state;
+        this->updateTitle();
+        this->inval(NULL);
+    }
+    if (SkOSMenu::FindTriState(&evt, "Filter", &state)) {
+        fFilterState = (SkTriState)state;
+        this->updateTitle();
+        this->inval(NULL);
+    }
+    if (SkOSMenu::FindTriState(&evt, "Hinting", &state)) {
+        fHintingState = (SkTriState)state;
+        this->updateTitle();
+        this->inval(NULL);
+    }
+    if (SkOSMenu::FindSwitchState(&evt, "Pipe", NULL)) {
+        this->togglePipe();
+        return true;
+    }
+    if (SkOSMenu::FindSwitchState(&evt, "Slide Show", NULL)) {
+        this->toggleSlideshow();
+        return true;
+    }
+    if (SkOSMenu::FindSwitchState(&evt, "Clip", NULL)) {
+        fUseClip = !fUseClip;
+        this->inval(NULL);
+        this->updateTitle();
+        return true;
+    }
+    if (SkOSMenu::FindSwitchState(&evt, "Measure FPS", NULL)) {
+        this->toggleFPS();
+        return true;
+    }
+    if (SkOSMenu::FindSwitchState(&evt, "Flip X", NULL)) {
+        fFlipAxis ^= kFlipAxis_X;
+        this->updateTitle();
+        this->inval(NULL);
+        return true;
+    }
+    if (SkOSMenu::FindSwitchState(&evt, "Flip Y", NULL)) {
+        fFlipAxis ^= kFlipAxis_Y;
+        this->updateTitle();
+        this->inval(NULL);
+        return true;
+    }
+    if (SkOSMenu::FindSwitchState(&evt, "Zoomer", NULL)) {
+        this->toggleZoomer();
+        return true;
+    }
+    if (SkOSMenu::FindAction(&evt,"Save to PDF")) {
+        this->saveToPdf();
+        return true;
+    }    
     return this->INHERITED::onEvent(evt);
 }
 
@@ -1169,6 +1252,13 @@
     return this->INHERITED::onHandleChar(uni);
 }
 
+void SampleWindow::setDeviceType(DeviceType type) {
+    if (type != fDeviceType && fDevManager->supportsDeviceType(fDeviceType))
+        fDeviceType = type;
+    this->updateTitle();
+    this->inval(NULL);
+}
+
 void SampleWindow::toggleFPS() {
     fMeasureFPS = !fMeasureFPS;
     this->inval(NULL);
@@ -1191,6 +1281,11 @@
     this->inval(NULL);
 }
 
+void SampleWindow::togglePipe() {
+    fUsePipe = !fUsePipe;
+    this->updateTitle();
+}
+
 #include "SkDumpCanvas.h"
 
 bool SampleWindow::onHandleKey(SkKey key) {
@@ -1326,6 +1421,12 @@
     this->attachChildToFront(view)->unref();
     view->setSize(this->width(), this->height());
 
+    //repopulate the slide menu when a view is loaded
+    fSlideMenu.reset();
+    if (SampleView::IsSampleView(view))
+        ((SampleView*)view)->requestMenus(&fSlideMenu);
+    this->onUpdateMenu(&fSlideMenu);
+    
     this->updateTitle();
 }
 
diff --git a/samplecode/SampleApp.h b/samplecode/SampleApp.h
index 43b9799..b3f3c93 100644
--- a/samplecode/SampleApp.h
+++ b/samplecode/SampleApp.h
@@ -18,6 +18,7 @@
 #include "SkTDArray.h"
 #include "SkTouchGesture.h"
 #include "SkWindow.h"
+#include "SkOSMenu.h"
 
 class GrContext;
 class GrRenderTarget;
@@ -29,9 +30,9 @@
 class SkData;
 
 enum SkTriState {
-    kFalse_SkTriState,
-    kTrue_SkTriState,
-    kUnknown_SkTriState,
+    kFalse_SkTriState = SkOSMenu::kOffState,
+    kTrue_SkTriState = SkOSMenu::kOnState,
+    kUnknown_SkTriState = SkOSMenu::kMixedState,
 };
 
 class SampleWindow : public SkOSWindow {
@@ -83,9 +84,11 @@
 
     virtual void draw(SkCanvas* canvas);
 
+    void setDeviceType(DeviceType type);
     void toggleRendering();
     void toggleSlideshow();
     void toggleFPS();
+    void togglePipe();
 
     GrContext* getGrContext() const { return fDevManager->getGrContext(); }
 
@@ -167,6 +170,11 @@
     int fScrollTestX, fScrollTestY;
     SkScalar fZoomCenterX, fZoomCenterY;
 
+    //Stores global settings
+    SkOSMenu fAppMenu;
+    //Stores slide specific settings
+    SkOSMenu fSlideMenu;
+    
     void loadView(SkView*);
     void updateTitle();
 
diff --git a/samplecode/SampleCode.h b/samplecode/SampleCode.h
index 7bc1c51..c22533a 100644
--- a/samplecode/SampleCode.h
+++ b/samplecode/SampleCode.h
@@ -12,7 +12,7 @@
 #include "SkEvent.h"
 #include "SkKey.h"
 #include "SkView.h"
-
+class SkOSMenu;
 class GrContext;
 
 class SampleCode {
@@ -69,11 +69,17 @@
     static bool IsSampleView(SkView*);
     static bool SetRepeatDraw(SkView*, int count);
     static bool SetUsePipe(SkView*, bool);
+    
+    //call this to request menu items from a SampleView. A SampleView can 
+    //overwrite this method to add new items of various types to the menu and 
+    //change its title. The events attached to any new menu items must be 
+    //handled in the onEvent method. See SkOSMenu.h for helper functions.
+    virtual void requestMenus(SkOSMenu* menu) {}
 
 protected:
     virtual void onDrawBackground(SkCanvas*);
     virtual void onDrawContent(SkCanvas*) = 0;
-
+    
     // overrides
     virtual bool onEvent(const SkEvent& evt);
     virtual bool onQuery(SkEvent* evt);