2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkOSMenu_DEFINED
|
|
|
|
#define SkOSMenu_DEFINED
|
|
|
|
|
2016-02-18 16:53:33 +00:00
|
|
|
#include "../private/SkTDArray.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkEvent.h"
|
|
|
|
|
|
|
|
class SkOSMenu {
|
|
|
|
public:
|
2011-08-01 17:27:33 +00:00
|
|
|
explicit SkOSMenu(const char title[] = "");
|
2008-12-17 15:59:43 +00:00
|
|
|
~SkOSMenu();
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-08-01 17:27:33 +00:00
|
|
|
/**
|
2012-08-23 18:09:54 +00:00
|
|
|
* Each of these (except action) has an associated value, which is stored in
|
2011-08-01 17:27:33 +00:00
|
|
|
* 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 Type {
|
|
|
|
kAction_Type,
|
|
|
|
kList_Type,
|
|
|
|
kSlider_Type,
|
|
|
|
kSwitch_Type,
|
|
|
|
kTriState_Type,
|
|
|
|
kTextField_Type,
|
|
|
|
kCustom_Type
|
|
|
|
};
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-08-05 22:11:41 +00:00
|
|
|
enum TriState {
|
|
|
|
kMixedState = -1,
|
|
|
|
kOffState = 0,
|
|
|
|
kOnState = 1
|
|
|
|
};
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-08-01 17:27:33 +00:00
|
|
|
class Item {
|
|
|
|
public:
|
2011-08-05 22:11:41 +00:00
|
|
|
/**
|
|
|
|
* Auto increments a global to generate an unique ID for each new item
|
|
|
|
* Note: Thread safe
|
|
|
|
*/
|
2012-08-23 18:09:54 +00:00
|
|
|
Item(const char label[], SkOSMenu::Type type, const char slotName[],
|
2011-08-05 22:11:41 +00:00
|
|
|
SkEvent* evt);
|
2011-08-01 17:27:33 +00:00
|
|
|
~Item() { delete fEvent; }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-08-05 22:11:41 +00:00
|
|
|
SkEvent* getEvent() const { return fEvent; }
|
|
|
|
int getID() const { return fID; }
|
2011-08-01 17:27:33 +00:00
|
|
|
const char* getLabel() const { return fLabel.c_str(); }
|
|
|
|
const char* getSlotName() const { return fSlotName.c_str(); }
|
2011-08-05 22:11:41 +00:00
|
|
|
Type getType() const { return fType; }
|
|
|
|
void setKeyEquivalent(SkUnichar key) { fKey = key; }
|
|
|
|
SkUnichar getKeyEquivalent() const { return fKey; }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-08-05 22:11:41 +00:00
|
|
|
/**
|
2011-08-12 14:27:47 +00:00
|
|
|
* Helper functions for predefined types
|
2011-08-05 22:11:41 +00:00
|
|
|
*/
|
2011-08-12 14:27:47 +00:00
|
|
|
void setBool(bool value) const; //For Switch
|
2012-08-23 18:09:54 +00:00
|
|
|
void setScalar(SkScalar value) const; //For Slider
|
2011-08-12 14:27:47 +00:00
|
|
|
void setInt(int value) const; //For List
|
|
|
|
void setTriState(TriState value) const; //For Tristate
|
|
|
|
void setString(const char value[]) const; //For TextField
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-08-05 22:11:41 +00:00
|
|
|
/**
|
2012-08-23 18:09:54 +00:00
|
|
|
* Post event associated with the menu item to target, any changes to
|
2011-08-12 14:27:47 +00:00
|
|
|
* the associated event must be made prior to calling this method
|
2011-08-05 22:11:41 +00:00
|
|
|
*/
|
2011-08-12 14:27:47 +00:00
|
|
|
void postEvent() const { (new SkEvent(*(fEvent)))->post(); }
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-08-01 17:27:33 +00:00
|
|
|
private:
|
|
|
|
int fID;
|
|
|
|
SkEvent* fEvent;
|
|
|
|
SkString fLabel;
|
|
|
|
SkString fSlotName;
|
|
|
|
Type fType;
|
2011-08-05 22:11:41 +00:00
|
|
|
SkUnichar fKey;
|
2011-08-01 17:27:33 +00:00
|
|
|
};
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-08-12 14:27:47 +00:00
|
|
|
void reset();
|
2011-08-05 22:11:41 +00:00
|
|
|
const char* getTitle() const { return fTitle.c_str(); }
|
|
|
|
void setTitle (const char title[]) { fTitle.set(title); }
|
2011-08-12 14:27:47 +00:00
|
|
|
int getCount() const { return fItems.count(); }
|
|
|
|
const Item* getItemByID(int itemID) const;
|
|
|
|
void getItems(const Item* items[]) const;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-08-05 22:11:41 +00:00
|
|
|
/**
|
2012-08-23 18:09:54 +00:00
|
|
|
* Assign key to the menu item with itemID, will do nothing if there's no
|
2011-08-05 22:11:41 +00:00
|
|
|
* item with the id given
|
|
|
|
*/
|
|
|
|
void assignKeyEquivalentToItem(int itemID, SkUnichar key);
|
|
|
|
/**
|
2012-08-23 18:09:54 +00:00
|
|
|
* 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
|
2011-08-05 22:11:41 +00:00
|
|
|
* defined for switches(toggling), tristates(cycle), and lists(cycle),
|
2012-08-23 18:09:54 +00:00
|
|
|
* for anything else, the event attached is posted without state changes)
|
2011-08-05 22:11:41 +00:00
|
|
|
* If no menu item can be matched with the key, false will be returned
|
|
|
|
*/
|
|
|
|
bool handleKeyEquivalent(SkUnichar key);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-08-05 22:11:41 +00:00
|
|
|
/**
|
2012-08-23 18:09:54 +00:00
|
|
|
* 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
|
2011-08-05 22:11:41 +00:00
|
|
|
* 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
|
|
|
|
*/
|
2012-08-23 18:09:54 +00:00
|
|
|
int appendItem(const char label[], Type type, const char slotName[],
|
|
|
|
SkEvent* evt);
|
|
|
|
|
2011-08-05 22:11:41 +00:00
|
|
|
/**
|
2012-08-23 18:09:54 +00:00
|
|
|
* Create predefined items with the given parameters. To be used with the
|
2011-08-05 22:11:41 +00:00
|
|
|
* other helper functions below to retrive/update state information.
|
2012-08-23 18:09:54 +00:00
|
|
|
* Note: the helper functions below assume that slotName is UNIQUE for all
|
2011-08-05 22:11:41 +00:00
|
|
|
* menu items of the same type since it's used to identify the event
|
|
|
|
*/
|
2011-08-01 17:27:33 +00:00
|
|
|
int appendAction(const char label[], SkEventSinkID target);
|
2012-08-23 18:09:54 +00:00
|
|
|
int appendList(const char label[], const char slotName[],
|
2014-09-18 18:00:40 +00:00
|
|
|
SkEventSinkID target, int defaultIndex, const char* ...);
|
2012-08-23 18:09:54 +00:00
|
|
|
int appendSlider(const char label[], const char slotName[],
|
|
|
|
SkEventSinkID target, SkScalar min, SkScalar max,
|
2011-08-01 17:27:33 +00:00
|
|
|
SkScalar defaultValue);
|
2012-08-23 18:09:54 +00:00
|
|
|
int appendSwitch(const char label[], const char slotName[],
|
2011-08-01 17:27:33 +00:00
|
|
|
SkEventSinkID target, bool defaultState = false);
|
|
|
|
int appendTriState(const char label[], const char slotName[],
|
2011-08-05 22:11:41 +00:00
|
|
|
SkEventSinkID target, TriState defaultState = kOffState);
|
2011-08-01 17:27:33 +00:00
|
|
|
int appendTextField(const char label[], const char slotName[],
|
|
|
|
SkEventSinkID target, const char placeholder[] = "");
|
2012-08-23 18:09:54 +00:00
|
|
|
|
|
|
|
|
2011-08-05 22:11:41 +00:00
|
|
|
/**
|
|
|
|
* Helper functions to retrieve information other than the stored value for
|
|
|
|
* some predefined types
|
|
|
|
*/
|
2011-08-12 14:27:47 +00:00
|
|
|
static bool FindListItemCount(const SkEvent& evt, int* count);
|
2011-08-05 22:11:41 +00:00
|
|
|
/**
|
2012-08-23 18:09:54 +00:00
|
|
|
* Ensure that the items array can store n SkStrings where n is the count
|
2011-08-05 22:11:41 +00:00
|
|
|
* extracted using FindListItemCount
|
|
|
|
*/
|
2011-08-12 14:27:47 +00:00
|
|
|
static bool FindListItems(const SkEvent& evt, SkString items[]);
|
|
|
|
static bool FindSliderMin(const SkEvent& evt, SkScalar* min);
|
|
|
|
static bool FindSliderMax(const SkEvent& evt, SkScalar* max);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-08-05 22:11:41 +00:00
|
|
|
/**
|
|
|
|
* Returns true if an action with the given label is found, false otherwise
|
|
|
|
*/
|
2011-08-12 14:27:47 +00:00
|
|
|
static bool FindAction(const SkEvent& evt, const char label[]);
|
2011-08-05 22:11:41 +00:00
|
|
|
/**
|
2012-08-23 18:09:54 +00:00
|
|
|
* 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
|
2011-08-05 22:11:41 +00:00
|
|
|
* mismatch or slotName is incorrect
|
|
|
|
*/
|
2011-08-12 14:27:47 +00:00
|
|
|
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* value);
|
|
|
|
static bool FindText(const SkEvent& evt, const char slotName[], SkString* value);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2011-08-01 17:27:33 +00:00
|
|
|
SkString fTitle;
|
|
|
|
SkTDArray<Item*> fItems;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// illegal
|
|
|
|
SkOSMenu(const SkOSMenu&);
|
|
|
|
SkOSMenu& operator=(const SkOSMenu&);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|