diff options
author | yangsu@google.com <yangsu@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-08-05 22:11:41 +0000 |
---|---|---|
committer | yangsu@google.com <yangsu@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-08-05 22:11:41 +0000 |
commit | e55f533f4c80818d542c2beac14a37600c3718f8 (patch) | |
tree | 3156893714c27ad5ed2d042eb51a6da318009059 /include/views | |
parent | a767fa06ca28be9df1ff6e08a299e0bec839a2dc (diff) |
Updated SkOSMenu to use the updated SkEvents
http://codereview.appspot.com/4809075/
git-svn-id: http://skia.googlecode.com/svn/trunk@2055 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include/views')
-rw-r--r-- | include/views/SkOSMenu.h | 140 |
1 files changed, 89 insertions, 51 deletions
diff --git a/include/views/SkOSMenu.h b/include/views/SkOSMenu.h index 54c7dbe249..763499e073 100644 --- a/include/views/SkOSMenu.h +++ b/include/views/SkOSMenu.h @@ -18,7 +18,6 @@ public: 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. @@ -32,12 +31,6 @@ public: * TriState : TriState * Custom : custom object/value */ - enum TriState { - kMixedState = -1, - kOffState = 0, - kOnState = 1 - }; - enum Type { kAction_Type, kList_Type, @@ -48,59 +41,90 @@ public: kCustom_Type }; + enum TriState { + kMixedState = -1, + kOffState = 0, + kOnState = 1 + }; + class Item { public: - //Auto increments a global to generate an unique ID for each new item - //Thread safe + /** + * Auto increments a global to generate an unique ID for each new item + * Note: Thread safe + */ Item(const char label[], SkOSMenu::Type type, const char slotName[], - SkEvent* evt, SkEventSinkID target); + SkEvent* evt); ~Item() { delete fEvent; } - SkEvent* getEvent() const { return fEvent; } - int getID() { return fID; } + SkEvent* getEvent() const { return fEvent; } + int getID() const { return fID; } const char* getLabel() const { return fLabel.c_str(); } const char* getSlotName() const { return fSlotName.c_str(); } - Type getType() const { return fType; } + Type getType() const { return fType; } + void setKeyEquivalent(SkUnichar key) { fKey = key; } + SkUnichar getKeyEquivalent() const { return fKey; } - //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)))->setTargetID(fTarget)->post(); - } + /** + * 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(); } - //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 + /** + * 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 - private: int fID; SkEvent* fEvent; SkString fLabel; SkString fSlotName; - SkEventSinkID fTarget; Type fType; + SkUnichar fKey; }; - //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); + void reset(); + const char* getTitle() const { return fTitle.c_str(); } + void setTitle (const char title[]) { fTitle.set(title); } + int countItems() const { return fItems.count(); } + const Item* getItem(int index) const { return fItems[index]; } - //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; + /** + * Assign key to the menu item with itemID, will do nothing if there's no + * item with the id given + */ + void assignKeyEquivalentToItem(int itemID, SkUnichar key); + /** + * Call this in a SkView's onHandleChar to trigger any menu items with the + * given key equivalent. If such an item is found, the method will return + * true and its corresponding event will be triggered (default behavior + * defined for switches(toggling), tristates(cycle), and lists(cycle), + * for anything else, the event attached is posted without state changes) + * If no menu item can be matched with the key, false will be returned + */ + bool handleKeyEquivalent(SkUnichar key); + + /** + * 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); - //Create predefined items with the given parameters. To be used with the + /** + * Create predefined items with the given parameters. To be used with the + * other helper functions below to retrive/update state information. + * Note: the helper functions below assume that slotName is UNIQUE for all + * menu items of the same type since it's used to identify the event + */ int appendAction(const char label[], SkEventSinkID target); int appendList(const char label[], const char slotName[], SkEventSinkID target, int defaultIndex, const char[] ...); @@ -110,26 +134,40 @@ public: 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); + SkEventSinkID target, 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 + + /** + * Helper functions to retrieve information other than the stored value for + * some predefined types + */ + static bool FindListItemCount(const SkEvent* evt, int* count); + /** + * Ensure that the items array can store n SkStrings where n is the count + * extracted using FindListItemCount + */ + static bool FindListItems(const SkEvent* evt, SkString items[]); + static bool FindSliderMin(const SkEvent* evt, SkScalar* min); + static bool FindSliderMax(const SkEvent* evt, SkScalar* max); + + /** + * Returns true if an action with the given label is found, false otherwise + */ static bool FindAction(const SkEvent* evt, const char label[]); - static bool FindListIndex(const SkEvent* evt, const char slotName[], int* selected); + /** + * The following helper functions will return true if evt is generated from + * a predefined item type and retrieve the corresponding state information. + * They will return false and leave value unchanged if there's a type + * mismatch or slotName is incorrect + */ + static bool FindListIndex(const SkEvent* evt, const char slotName[], int* value); 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 FindTriState(const SkEvent* evt, const char slotName[], TriState* value); 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 Item* getItem(int index) const; - private: SkString fTitle; SkTDArray<Item*> fItems; |