diff options
author | yangsu@google.com <yangsu@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-08-01 17:27:33 +0000 |
---|---|---|
committer | yangsu@google.com <yangsu@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-08-01 17:27:33 +0000 |
commit | 654d72f94c855ceae34db0ee82d92844835c0d29 (patch) | |
tree | 084b76dc0f862b5a398882da44234d9f938801c0 /include/views | |
parent | 12d177d1f3b61851c782930d5bd7cce80525deb3 (diff) |
Revised SkOSMenu
http://codereview.appspot.com/4827042/
git-svn-id: http://skia.googlecode.com/svn/trunk@2013 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include/views')
-rw-r--r-- | include/views/SkOSMenu.h | 129 | ||||
-rw-r--r-- | include/views/SkWindow.h | 5 |
2 files changed, 114 insertions, 20 deletions
diff --git a/include/views/SkOSMenu.h b/include/views/SkOSMenu.h index c599bd8561..2dbc1a5a36 100644 --- a/include/views/SkOSMenu.h +++ b/include/views/SkOSMenu.h @@ -15,30 +15,123 @@ class SkOSMenu { public: - explicit SkOSMenu(const char title[]); + explicit SkOSMenu(const char title[] = ""); ~SkOSMenu(); + + void reset(); + /** + * Each of these (except action) has an associated value, which is stored in + * the event payload for the item. + * Each type has a specific type for its value... + * Action : none + * List : int (selected index) + * Segmented : int (selected index) + * Slider : float + * Switch : bool + * TextField : string + * TriState : TriState + * Custom : custom object/value + */ + enum TriState { + kMixedState = -1, + kOffState = 0, + kOnState = 1 + }; + + enum Type { + kAction_Type, + kList_Type, + kSlider_Type, + kSwitch_Type, + kTriState_Type, + kTextField_Type, + kCustom_Type + }; + + class Item { + public: + //Auto increments a global to generate an unique ID for each new item + //Thread safe + Item(const char label[], SkOSMenu::Type type, const char slotName[], + SkEvent* evt, SkEventSinkID target); + ~Item() { delete fEvent; } + + SkEvent* getEvent() const { return fEvent; } + int getID() { return fID; } + const char* getLabel() const { return fLabel.c_str(); } + const char* getSlotName() const { return fSlotName.c_str(); } + Type getType() const { return fType; } + + //Post event associated with the menu item to target, any changes to the + //associated event must be made prior to calling this method. + void postEvent() const { (new SkEvent(*(fEvent)))->post(fTarget); } + + //Helper functions for predefined types + void postEventWithBool(bool value) const; //For Switch + void postEventWithScalar(SkScalar value) const; //For Slider + void postEventWithInt(int value) const; //For List, TriState + void postEventWithString(const char value[]) const; //For TextField - const char* getTitle() const { return fTitle; } - - void appendItem(const char title[], const char eventType[], int32_t eventData); - + + private: + int fID; + SkEvent* fEvent; + SkString fLabel; + SkString fSlotName; + SkEventSinkID fTarget; + Type fType; + }; + + //The following functions append new items to the menu and returns their + //associated unique id, which can be used to by the client to refer to + //the menu item created and change its state. slotName specifies the string + //identifier of any state/value to be returned in the item's SkEvent object + //NOTE: evt must be dynamically allocated + int appendItem(const char label[], Type type, const char slotName[], + SkEvent* evt, SkEventSinkID target); + + //Predefined items and helper functions: + //Identifiers + static const char* EventType; + static const char* Delimiter; + static const char* List_Items_Str; + static const char* Slider_Min_Scalar; + static const char* Slider_Max_Scalar; + + //Create predefined items with the given parameters. To be used with the + int appendAction(const char label[], SkEventSinkID target); + int appendList(const char label[], const char slotName[], + SkEventSinkID target, int defaultIndex, const char[] ...); + int appendSlider(const char label[], const char slotName[], + SkEventSinkID target, SkScalar min, SkScalar max, + SkScalar defaultValue); + int appendSwitch(const char label[], const char slotName[], + SkEventSinkID target, bool defaultState = false); + int appendTriState(const char label[], const char slotName[], + SkEventSinkID target, SkOSMenu::TriState defaultState = kOffState); + int appendTextField(const char label[], const char slotName[], + SkEventSinkID target, const char placeholder[] = ""); + + //Returns true if the event is of type SkOSMenu::EventType and retrieves + //value stored in the evt that corresponds to the slotName. Otherwise, + //returns false and leaves value unchanged + static bool FindAction(const SkEvent* evt, const char label[]); + static bool FindListIndex(const SkEvent* evt, const char slotName[], int* selected); + static bool FindSliderValue(const SkEvent* evt, const char slotName[], SkScalar* value); + static bool FindSwitchState(const SkEvent* evt, const char slotName[], bool* value); + static bool FindTriState(const SkEvent* evt, const char slotName[], TriState* state); + static bool FindText(const SkEvent* evt, const char slotName[], SkString* value); + + const char* getTitle() const { return fTitle.c_str(); } + void setTitle (const char title[]) { fTitle.set(title); } // called by SkOSWindow when it receives an OS menu event int countItems() const; - const char* getItem(int index, uint32_t* cmdID) const; - - SkEvent* createEvent(uint32_t os_cmd); + const Item* getItem(int index) const; private: - const char* fTitle; - - struct Item { - const char* fTitle; - const char* fEventType; - uint32_t fEventData; - uint32_t fOSCmd; // internal - }; - SkTDArray<Item> fItems; - + SkString fTitle; + SkTDArray<Item*> fItems; + // illegal SkOSMenu(const SkOSMenu&); SkOSMenu& operator=(const SkOSMenu&); diff --git a/include/views/SkWindow.h b/include/views/SkWindow.h index 64c5beab61..eda928c948 100644 --- a/include/views/SkWindow.h +++ b/include/views/SkWindow.h @@ -51,9 +51,9 @@ public: bool handleChar(SkUnichar); bool handleKey(SkKey); bool handleKeyUp(SkKey); - bool handleMenu(uint32_t os_cmd); void addMenu(SkOSMenu*); + const SkTDArray<SkOSMenu*>* getMenus() { return &fMenus; } const char* getTitle() const { return fTitle.c_str(); } void setTitle(const char title[]); @@ -73,7 +73,8 @@ protected: virtual bool onHandleChar(SkUnichar); virtual bool onHandleKey(SkKey); virtual bool onHandleKeyUp(SkKey); - virtual void onAddMenu(const SkOSMenu*) {} + virtual void onAddMenu(const SkOSMenu*) {}; + virtual void onUpdateMenu(const SkOSMenu*) {}; virtual void onSetTitle(const char title[]) {} // overrides from SkView |