1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/views/SkOSMenu.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,182 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2006 The Android Open Source Project 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#ifndef SkOSMenu_DEFINED 1.14 +#define SkOSMenu_DEFINED 1.15 + 1.16 +#include "SkEvent.h" 1.17 +#include "SkTDArray.h" 1.18 + 1.19 +class SkOSMenu { 1.20 +public: 1.21 + explicit SkOSMenu(const char title[] = ""); 1.22 + ~SkOSMenu(); 1.23 + 1.24 + /** 1.25 + * Each of these (except action) has an associated value, which is stored in 1.26 + * the event payload for the item. 1.27 + * Each type has a specific type for its value... 1.28 + * Action : none 1.29 + * List : int (selected index) 1.30 + * Segmented : int (selected index) 1.31 + * Slider : float 1.32 + * Switch : bool 1.33 + * TextField : string 1.34 + * TriState : TriState 1.35 + * Custom : custom object/value 1.36 + */ 1.37 + enum Type { 1.38 + kAction_Type, 1.39 + kList_Type, 1.40 + kSlider_Type, 1.41 + kSwitch_Type, 1.42 + kTriState_Type, 1.43 + kTextField_Type, 1.44 + kCustom_Type 1.45 + }; 1.46 + 1.47 + enum TriState { 1.48 + kMixedState = -1, 1.49 + kOffState = 0, 1.50 + kOnState = 1 1.51 + }; 1.52 + 1.53 + class Item { 1.54 + public: 1.55 + /** 1.56 + * Auto increments a global to generate an unique ID for each new item 1.57 + * Note: Thread safe 1.58 + */ 1.59 + Item(const char label[], SkOSMenu::Type type, const char slotName[], 1.60 + SkEvent* evt); 1.61 + ~Item() { delete fEvent; } 1.62 + 1.63 + SkEvent* getEvent() const { return fEvent; } 1.64 + int getID() const { return fID; } 1.65 + const char* getLabel() const { return fLabel.c_str(); } 1.66 + const char* getSlotName() const { return fSlotName.c_str(); } 1.67 + Type getType() const { return fType; } 1.68 + void setKeyEquivalent(SkUnichar key) { fKey = key; } 1.69 + SkUnichar getKeyEquivalent() const { return fKey; } 1.70 + 1.71 + /** 1.72 + * Helper functions for predefined types 1.73 + */ 1.74 + void setBool(bool value) const; //For Switch 1.75 + void setScalar(SkScalar value) const; //For Slider 1.76 + void setInt(int value) const; //For List 1.77 + void setTriState(TriState value) const; //For Tristate 1.78 + void setString(const char value[]) const; //For TextField 1.79 + 1.80 + /** 1.81 + * Post event associated with the menu item to target, any changes to 1.82 + * the associated event must be made prior to calling this method 1.83 + */ 1.84 + void postEvent() const { (new SkEvent(*(fEvent)))->post(); } 1.85 + 1.86 + private: 1.87 + int fID; 1.88 + SkEvent* fEvent; 1.89 + SkString fLabel; 1.90 + SkString fSlotName; 1.91 + Type fType; 1.92 + SkUnichar fKey; 1.93 + }; 1.94 + 1.95 + void reset(); 1.96 + const char* getTitle() const { return fTitle.c_str(); } 1.97 + void setTitle (const char title[]) { fTitle.set(title); } 1.98 + int getCount() const { return fItems.count(); } 1.99 + const Item* getItemByID(int itemID) const; 1.100 + void getItems(const Item* items[]) const; 1.101 + 1.102 + /** 1.103 + * Assign key to the menu item with itemID, will do nothing if there's no 1.104 + * item with the id given 1.105 + */ 1.106 + void assignKeyEquivalentToItem(int itemID, SkUnichar key); 1.107 + /** 1.108 + * Call this in a SkView's onHandleChar to trigger any menu items with the 1.109 + * given key equivalent. If such an item is found, the method will return 1.110 + * true and its corresponding event will be triggered (default behavior 1.111 + * defined for switches(toggling), tristates(cycle), and lists(cycle), 1.112 + * for anything else, the event attached is posted without state changes) 1.113 + * If no menu item can be matched with the key, false will be returned 1.114 + */ 1.115 + bool handleKeyEquivalent(SkUnichar key); 1.116 + 1.117 + /** 1.118 + * The following functions append new items to the menu and returns their 1.119 + * associated unique id, which can be used to by the client to refer to 1.120 + * the menu item created and change its state. slotName specifies the string 1.121 + * identifier of any state/value to be returned in the item's SkEvent object 1.122 + * NOTE: evt must be dynamically allocated 1.123 + */ 1.124 + int appendItem(const char label[], Type type, const char slotName[], 1.125 + SkEvent* evt); 1.126 + 1.127 + /** 1.128 + * Create predefined items with the given parameters. To be used with the 1.129 + * other helper functions below to retrive/update state information. 1.130 + * Note: the helper functions below assume that slotName is UNIQUE for all 1.131 + * menu items of the same type since it's used to identify the event 1.132 + */ 1.133 + int appendAction(const char label[], SkEventSinkID target); 1.134 + int appendList(const char label[], const char slotName[], 1.135 + SkEventSinkID target, int defaultIndex, const char[] ...); 1.136 + int appendSlider(const char label[], const char slotName[], 1.137 + SkEventSinkID target, SkScalar min, SkScalar max, 1.138 + SkScalar defaultValue); 1.139 + int appendSwitch(const char label[], const char slotName[], 1.140 + SkEventSinkID target, bool defaultState = false); 1.141 + int appendTriState(const char label[], const char slotName[], 1.142 + SkEventSinkID target, TriState defaultState = kOffState); 1.143 + int appendTextField(const char label[], const char slotName[], 1.144 + SkEventSinkID target, const char placeholder[] = ""); 1.145 + 1.146 + 1.147 + /** 1.148 + * Helper functions to retrieve information other than the stored value for 1.149 + * some predefined types 1.150 + */ 1.151 + static bool FindListItemCount(const SkEvent& evt, int* count); 1.152 + /** 1.153 + * Ensure that the items array can store n SkStrings where n is the count 1.154 + * extracted using FindListItemCount 1.155 + */ 1.156 + static bool FindListItems(const SkEvent& evt, SkString items[]); 1.157 + static bool FindSliderMin(const SkEvent& evt, SkScalar* min); 1.158 + static bool FindSliderMax(const SkEvent& evt, SkScalar* max); 1.159 + 1.160 + /** 1.161 + * Returns true if an action with the given label is found, false otherwise 1.162 + */ 1.163 + static bool FindAction(const SkEvent& evt, const char label[]); 1.164 + /** 1.165 + * The following helper functions will return true if evt is generated from 1.166 + * a predefined item type and retrieve the corresponding state information. 1.167 + * They will return false and leave value unchanged if there's a type 1.168 + * mismatch or slotName is incorrect 1.169 + */ 1.170 + static bool FindListIndex(const SkEvent& evt, const char slotName[], int* value); 1.171 + static bool FindSliderValue(const SkEvent& evt, const char slotName[], SkScalar* value); 1.172 + static bool FindSwitchState(const SkEvent& evt, const char slotName[], bool* value); 1.173 + static bool FindTriState(const SkEvent& evt, const char slotName[], TriState* value); 1.174 + static bool FindText(const SkEvent& evt, const char slotName[], SkString* value); 1.175 + 1.176 +private: 1.177 + SkString fTitle; 1.178 + SkTDArray<Item*> fItems; 1.179 + 1.180 + // illegal 1.181 + SkOSMenu(const SkOSMenu&); 1.182 + SkOSMenu& operator=(const SkOSMenu&); 1.183 +}; 1.184 + 1.185 +#endif