blob: 2dbc1a5a363b0d0b3df519760285e1f488a8e6a0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkOSMenu_DEFINED
11#define SkOSMenu_DEFINED
12
13#include "SkEvent.h"
14#include "SkTDArray.h"
15
16class SkOSMenu {
17public:
yangsu@google.com654d72f2011-08-01 17:27:33 +000018 explicit SkOSMenu(const char title[] = "");
reed@android.com8a1c16f2008-12-17 15:59:43 +000019 ~SkOSMenu();
yangsu@google.com654d72f2011-08-01 17:27:33 +000020
21 void reset();
22 /**
23 * Each of these (except action) has an associated value, which is stored in
24 * the event payload for the item.
25 * Each type has a specific type for its value...
26 * Action : none
27 * List : int (selected index)
28 * Segmented : int (selected index)
29 * Slider : float
30 * Switch : bool
31 * TextField : string
32 * TriState : TriState
33 * Custom : custom object/value
34 */
35 enum TriState {
36 kMixedState = -1,
37 kOffState = 0,
38 kOnState = 1
39 };
40
41 enum Type {
42 kAction_Type,
43 kList_Type,
44 kSlider_Type,
45 kSwitch_Type,
46 kTriState_Type,
47 kTextField_Type,
48 kCustom_Type
49 };
50
51 class Item {
52 public:
53 //Auto increments a global to generate an unique ID for each new item
54 //Thread safe
55 Item(const char label[], SkOSMenu::Type type, const char slotName[],
56 SkEvent* evt, SkEventSinkID target);
57 ~Item() { delete fEvent; }
58
59 SkEvent* getEvent() const { return fEvent; }
60 int getID() { return fID; }
61 const char* getLabel() const { return fLabel.c_str(); }
62 const char* getSlotName() const { return fSlotName.c_str(); }
63 Type getType() const { return fType; }
64
65 //Post event associated with the menu item to target, any changes to the
66 //associated event must be made prior to calling this method.
67 void postEvent() const { (new SkEvent(*(fEvent)))->post(fTarget); }
68
69 //Helper functions for predefined types
70 void postEventWithBool(bool value) const; //For Switch
71 void postEventWithScalar(SkScalar value) const; //For Slider
72 void postEventWithInt(int value) const; //For List, TriState
73 void postEventWithString(const char value[]) const; //For TextField
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
yangsu@google.com654d72f2011-08-01 17:27:33 +000075
76 private:
77 int fID;
78 SkEvent* fEvent;
79 SkString fLabel;
80 SkString fSlotName;
81 SkEventSinkID fTarget;
82 Type fType;
83 };
84
85 //The following functions append new items to the menu and returns their
86 //associated unique id, which can be used to by the client to refer to
87 //the menu item created and change its state. slotName specifies the string
88 //identifier of any state/value to be returned in the item's SkEvent object
89 //NOTE: evt must be dynamically allocated
90 int appendItem(const char label[], Type type, const char slotName[],
91 SkEvent* evt, SkEventSinkID target);
92
93 //Predefined items and helper functions:
94 //Identifiers
95 static const char* EventType;
96 static const char* Delimiter;
97 static const char* List_Items_Str;
98 static const char* Slider_Min_Scalar;
99 static const char* Slider_Max_Scalar;
100
101 //Create predefined items with the given parameters. To be used with the
102 int appendAction(const char label[], SkEventSinkID target);
103 int appendList(const char label[], const char slotName[],
104 SkEventSinkID target, int defaultIndex, const char[] ...);
105 int appendSlider(const char label[], const char slotName[],
106 SkEventSinkID target, SkScalar min, SkScalar max,
107 SkScalar defaultValue);
108 int appendSwitch(const char label[], const char slotName[],
109 SkEventSinkID target, bool defaultState = false);
110 int appendTriState(const char label[], const char slotName[],
111 SkEventSinkID target, SkOSMenu::TriState defaultState = kOffState);
112 int appendTextField(const char label[], const char slotName[],
113 SkEventSinkID target, const char placeholder[] = "");
114
115 //Returns true if the event is of type SkOSMenu::EventType and retrieves
116 //value stored in the evt that corresponds to the slotName. Otherwise,
117 //returns false and leaves value unchanged
118 static bool FindAction(const SkEvent* evt, const char label[]);
119 static bool FindListIndex(const SkEvent* evt, const char slotName[], int* selected);
120 static bool FindSliderValue(const SkEvent* evt, const char slotName[], SkScalar* value);
121 static bool FindSwitchState(const SkEvent* evt, const char slotName[], bool* value);
122 static bool FindTriState(const SkEvent* evt, const char slotName[], TriState* state);
123 static bool FindText(const SkEvent* evt, const char slotName[], SkString* value);
124
125 const char* getTitle() const { return fTitle.c_str(); }
126 void setTitle (const char title[]) { fTitle.set(title); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 // called by SkOSWindow when it receives an OS menu event
128 int countItems() const;
yangsu@google.com654d72f2011-08-01 17:27:33 +0000129 const Item* getItem(int index) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130
131private:
yangsu@google.com654d72f2011-08-01 17:27:33 +0000132 SkString fTitle;
133 SkTDArray<Item*> fItems;
134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 // illegal
136 SkOSMenu(const SkOSMenu&);
137 SkOSMenu& operator=(const SkOSMenu&);
138};
139
140#endif
141