blob: 54c7dbe249679cdb119738987d5038c333759eb3 [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.
reed@google.com87fac4a2011-08-04 13:50:17 +000067 void postEvent() const {
68 (new SkEvent(*(fEvent)))->setTargetID(fTarget)->post();
69 }
yangsu@google.com654d72f2011-08-01 17:27:33 +000070
71 //Helper functions for predefined types
72 void postEventWithBool(bool value) const; //For Switch
73 void postEventWithScalar(SkScalar value) const; //For Slider
74 void postEventWithInt(int value) const; //For List, TriState
75 void postEventWithString(const char value[]) const; //For TextField
reed@android.com8a1c16f2008-12-17 15:59:43 +000076
yangsu@google.com654d72f2011-08-01 17:27:33 +000077
78 private:
79 int fID;
80 SkEvent* fEvent;
81 SkString fLabel;
82 SkString fSlotName;
83 SkEventSinkID fTarget;
84 Type fType;
85 };
86
87 //The following functions append new items to the menu and returns their
88 //associated unique id, which can be used to by the client to refer to
89 //the menu item created and change its state. slotName specifies the string
90 //identifier of any state/value to be returned in the item's SkEvent object
91 //NOTE: evt must be dynamically allocated
92 int appendItem(const char label[], Type type, const char slotName[],
93 SkEvent* evt, SkEventSinkID target);
94
95 //Predefined items and helper functions:
96 //Identifiers
97 static const char* EventType;
98 static const char* Delimiter;
99 static const char* List_Items_Str;
100 static const char* Slider_Min_Scalar;
101 static const char* Slider_Max_Scalar;
102
103 //Create predefined items with the given parameters. To be used with the
104 int appendAction(const char label[], SkEventSinkID target);
105 int appendList(const char label[], const char slotName[],
106 SkEventSinkID target, int defaultIndex, const char[] ...);
107 int appendSlider(const char label[], const char slotName[],
108 SkEventSinkID target, SkScalar min, SkScalar max,
109 SkScalar defaultValue);
110 int appendSwitch(const char label[], const char slotName[],
111 SkEventSinkID target, bool defaultState = false);
112 int appendTriState(const char label[], const char slotName[],
113 SkEventSinkID target, SkOSMenu::TriState defaultState = kOffState);
114 int appendTextField(const char label[], const char slotName[],
115 SkEventSinkID target, const char placeholder[] = "");
116
117 //Returns true if the event is of type SkOSMenu::EventType and retrieves
118 //value stored in the evt that corresponds to the slotName. Otherwise,
119 //returns false and leaves value unchanged
120 static bool FindAction(const SkEvent* evt, const char label[]);
121 static bool FindListIndex(const SkEvent* evt, const char slotName[], int* selected);
122 static bool FindSliderValue(const SkEvent* evt, const char slotName[], SkScalar* value);
123 static bool FindSwitchState(const SkEvent* evt, const char slotName[], bool* value);
124 static bool FindTriState(const SkEvent* evt, const char slotName[], TriState* state);
125 static bool FindText(const SkEvent* evt, const char slotName[], SkString* value);
126
127 const char* getTitle() const { return fTitle.c_str(); }
128 void setTitle (const char title[]) { fTitle.set(title); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 // called by SkOSWindow when it receives an OS menu event
130 int countItems() const;
yangsu@google.com654d72f2011-08-01 17:27:33 +0000131 const Item* getItem(int index) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132
133private:
yangsu@google.com654d72f2011-08-01 17:27:33 +0000134 SkString fTitle;
135 SkTDArray<Item*> fItems;
136
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 // illegal
138 SkOSMenu(const SkOSMenu&);
139 SkOSMenu& operator=(const SkOSMenu&);
140};
141
142#endif
143