180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/* 380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2006 The Android Open Source Project 480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * 580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be 680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file. 780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef SkOSMenu_DEFINED 1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkOSMenu_DEFINED 1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkEvent.h" 1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTDArray.h" 1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkOSMenu { 1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic: 1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru explicit SkOSMenu(const char title[] = ""); 1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru ~SkOSMenu(); 2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Each of these (except action) has an associated value, which is stored in 2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * the event payload for the item. 2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Each type has a specific type for its value... 2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Action : none 2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * List : int (selected index) 2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Segmented : int (selected index) 2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Slider : float 2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Switch : bool 3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * TextField : string 3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * TriState : TriState 3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Custom : custom object/value 3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru enum Type { 3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru kAction_Type, 3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru kList_Type, 3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru kSlider_Type, 3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru kSwitch_Type, 3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru kTriState_Type, 4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru kTextField_Type, 4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru kCustom_Type 4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru }; 4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru enum TriState { 4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru kMixedState = -1, 4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru kOffState = 0, 4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru kOnState = 1 4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru }; 4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru class Item { 5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru public: 5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Auto increments a global to generate an unique ID for each new item 5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Note: Thread safe 5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru Item(const char label[], SkOSMenu::Type type, const char slotName[], 5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkEvent* evt); 5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru ~Item() { delete fEvent; } 5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkEvent* getEvent() const { return fEvent; } 6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int getID() const { return fID; } 6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru const char* getLabel() const { return fLabel.c_str(); } 6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru const char* getSlotName() const { return fSlotName.c_str(); } 6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru Type getType() const { return fType; } 6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void setKeyEquivalent(SkUnichar key) { fKey = key; } 6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkUnichar getKeyEquivalent() const { return fKey; } 6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Helper functions for predefined types 7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void setBool(bool value) const; //For Switch 7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void setScalar(SkScalar value) const; //For Slider 7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void setInt(int value) const; //For List 7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void setTriState(TriState value) const; //For Tristate 7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void setString(const char value[]) const; //For TextField 7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Post event associated with the menu item to target, any changes to 7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * the associated event must be made prior to calling this method 8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void postEvent() const { (new SkEvent(*(fEvent)))->post(); } 8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru private: 8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int fID; 8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkEvent* fEvent; 8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkString fLabel; 8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkString fSlotName; 8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru Type fType; 8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkUnichar fKey; 9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru }; 9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void reset(); 9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru const char* getTitle() const { return fTitle.c_str(); } 9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void setTitle (const char title[]) { fTitle.set(title); } 9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int getCount() const { return fItems.count(); } 9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru const Item* getItemByID(int itemID) const; 9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void getItems(const Item* items[]) const; 9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Assign key to the menu item with itemID, will do nothing if there's no 10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * item with the id given 10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void assignKeyEquivalentToItem(int itemID, SkUnichar key); 10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Call this in a SkView's onHandleChar to trigger any menu items with the 10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * given key equivalent. If such an item is found, the method will return 10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * true and its corresponding event will be triggered (default behavior 10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * defined for switches(toggling), tristates(cycle), and lists(cycle), 10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * for anything else, the event attached is posted without state changes) 11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * If no menu item can be matched with the key, false will be returned 11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru bool handleKeyEquivalent(SkUnichar key); 11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * The following functions append new items to the menu and returns their 11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * associated unique id, which can be used to by the client to refer to 11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * the menu item created and change its state. slotName specifies the string 11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * identifier of any state/value to be returned in the item's SkEvent object 11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * NOTE: evt must be dynamically allocated 12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int appendItem(const char label[], Type type, const char slotName[], 12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkEvent* evt); 12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 12480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Create predefined items with the given parameters. To be used with the 12680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * other helper functions below to retrive/update state information. 12780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Note: the helper functions below assume that slotName is UNIQUE for all 12880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * menu items of the same type since it's used to identify the event 12980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 13080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int appendAction(const char label[], SkEventSinkID target); 13180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int appendList(const char label[], const char slotName[], 13280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkEventSinkID target, int defaultIndex, const char[] ...); 13380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int appendSlider(const char label[], const char slotName[], 13480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkEventSinkID target, SkScalar min, SkScalar max, 13580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkScalar defaultValue); 13680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int appendSwitch(const char label[], const char slotName[], 13780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkEventSinkID target, bool defaultState = false); 13880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int appendTriState(const char label[], const char slotName[], 13980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkEventSinkID target, TriState defaultState = kOffState); 14080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int appendTextField(const char label[], const char slotName[], 14180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkEventSinkID target, const char placeholder[] = ""); 14280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 14380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 14480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 14580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Helper functions to retrieve information other than the stored value for 14680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * some predefined types 14780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 14880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static bool FindListItemCount(const SkEvent& evt, int* count); 14980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 15080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Ensure that the items array can store n SkStrings where n is the count 15180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * extracted using FindListItemCount 15280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 15380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static bool FindListItems(const SkEvent& evt, SkString items[]); 15480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static bool FindSliderMin(const SkEvent& evt, SkScalar* min); 15580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static bool FindSliderMax(const SkEvent& evt, SkScalar* max); 15680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 15780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 15880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Returns true if an action with the given label is found, false otherwise 15980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 16080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static bool FindAction(const SkEvent& evt, const char label[]); 16180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 16280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * The following helper functions will return true if evt is generated from 16380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * a predefined item type and retrieve the corresponding state information. 16480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * They will return false and leave value unchanged if there's a type 16580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * mismatch or slotName is incorrect 16680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 16780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static bool FindListIndex(const SkEvent& evt, const char slotName[], int* value); 16880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static bool FindSliderValue(const SkEvent& evt, const char slotName[], SkScalar* value); 16980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static bool FindSwitchState(const SkEvent& evt, const char slotName[], bool* value); 17080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static bool FindTriState(const SkEvent& evt, const char slotName[], TriState* value); 17180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static bool FindText(const SkEvent& evt, const char slotName[], SkString* value); 17280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 17380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate: 17480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkString fTitle; 17580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkTDArray<Item*> fItems; 17680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 17780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru // illegal 17880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkOSMenu(const SkOSMenu&); 17980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkOSMenu& operator=(const SkOSMenu&); 18080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}; 18180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 18280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif 183