aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/elementary/efl_ui_nstate.c
blob: 4f5f89c693d72c0c7f48a63c336f863b93767004 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#define EFL_UI_NSTATE_PROTECTED
#include "Elementary.h"
#include "elm_priv.h"
#include "efl_ui_nstate.eo.h"
#include "elm_widget_button.h"

#define MY_CLASS EFL_UI_NSTATE_CLASS
#define MY_CLASS_NAME "Efl.Ui.Nstate"
#define MY_CLASS_NAME_LEGACY "elm_check"


typedef struct
{
   int nstate;
   int state;
} Efl_Ui_Nstate_Data;

static const Evas_Smart_Cb_Description _smart_callbacks[] = {
   {NULL, NULL}
};

static Eina_Bool _key_action_activate(Evas_Object *obj, const char *params);
static void _state_active(Evas_Object *obj, Efl_Ui_Nstate_Data *sd);

static const Elm_Action key_actions[] = {
   {"activate", _key_action_activate},
   {NULL, NULL}
};

EOLIAN static Efl_Object *
_efl_ui_nstate_efl_object_constructor(Eo *obj, Efl_Ui_Nstate_Data *pd EINA_UNUSED)
{
   obj = efl_constructor(efl_super(obj, MY_CLASS));
   efl_canvas_object_type_set(obj, MY_CLASS_NAME);
   evas_object_smart_callbacks_descriptions_set(obj, _smart_callbacks);
   //TODO: Add ATSPI call here

   return obj;
}

static void
_next_state_set(Efl_Ui_Nstate_Data *sd)
{
   ++sd->state;
   if (sd->state == sd->nstate) sd->state = 0;
}

static void
_state_active(Evas_Object *obj, Efl_Ui_Nstate_Data *sd)
{
   char buf[64];

   sprintf(buf, "elm,state,changed,%d", sd->state);
   elm_layout_signal_emit(obj, buf, "elm");
   edje_object_message_signal_process(elm_layout_edje_get(obj));
   elm_obj_layout_sizing_eval(obj);
   efl_event_callback_legacy_call(obj, EFL_UI_NSTATE_EVENT_STATE_CHANGED, NULL);
}

static void
_on_state_changed(void *data,
                  Evas_Object *o EINA_UNUSED,
                  const char *emission EINA_UNUSED,
                  const char *source EINA_UNUSED)
{
   efl_ui_nstate_activate(data);
}

EOLIAN static void
_efl_ui_nstate_efl_canvas_group_group_add(Eo *obj, Efl_Ui_Nstate_Data *pd)
{
   ELM_WIDGET_DATA_GET_OR_RETURN(obj, wd);

   efl_canvas_group_add(efl_super(obj, MY_CLASS));
   elm_widget_sub_object_parent_add(obj);

   pd->state = 0;
   // Default: 2 states
   pd->nstate = 2;

   if (!elm_layout_theme_set(obj, "nstate", "base", elm_widget_style_get(obj)))
     CRI("Failed to set layout!");

   edje_object_signal_callback_add(wd->resize_obj, "elm,action,state,changed",
                                   "*", _on_state_changed, obj);
}

EOLIAN static void
_efl_ui_nstate_efl_canvas_group_group_del(Eo *obj, Efl_Ui_Nstate_Data *pd EINA_UNUSED)
{
   efl_canvas_group_del(efl_super(obj, MY_CLASS));
}

EOLIAN static int
_efl_ui_nstate_count_get(Eo *obj EINA_UNUSED, Efl_Ui_Nstate_Data *pd)
{
   return pd->nstate;
}

EOLIAN static void
_efl_ui_nstate_count_set(Eo *obj EINA_UNUSED, Efl_Ui_Nstate_Data *pd, int nstate)
{
   if (pd->nstate == nstate) return;

   pd->nstate = nstate;
   pd->state = 0;
}

EOLIAN static int
_efl_ui_nstate_value_get(Eo *obj EINA_UNUSED, Efl_Ui_Nstate_Data *pd)
{
   return pd->state;
}

static Eina_Bool
_is_valid_state(Efl_Ui_Nstate_Data *sd, int state)
{
   if (sd->state == state || (state < 0 || state >= sd->nstate))
     return EINA_FALSE;

   return EINA_TRUE;
}

EOLIAN static void
_efl_ui_nstate_value_set(Eo *obj, Efl_Ui_Nstate_Data *pd, int state)
{
   if (!_is_valid_state(pd, state)) return;

   pd->state = state;
   _state_active(obj, pd);
}

EOLIAN static Elm_Theme_Apply
_efl_ui_nstate_elm_widget_theme_apply(Eo *obj, Efl_Ui_Nstate_Data *pd)
{
   Elm_Theme_Apply int_ret = ELM_THEME_APPLY_FAILED;

   int_ret = elm_obj_widget_theme_apply(efl_super(obj, MY_CLASS));
   if (!int_ret) return ELM_THEME_APPLY_FAILED;

   _state_active(obj, pd);

   return int_ret;
}

static Eina_Bool
_key_action_activate(Evas_Object *obj, const char *params EINA_UNUSED)
{
   efl_ui_nstate_activate(obj);
   return EINA_TRUE;
}

EOLIAN static Eina_Bool
_efl_ui_nstate_elm_widget_event(Eo *obj, Efl_Ui_Nstate_Data *_pd EINA_UNUSED, Evas_Object *src EINA_UNUSED, Evas_Callback_Type type, void *event_info)
{
   Evas_Event_Key_Down *ev = event_info;

   if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;

   if (!_elm_config_key_binding_call(obj, MY_CLASS_NAME_LEGACY, ev, key_actions))
     return EINA_FALSE;

   ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
   return EINA_TRUE;
}

EOLIAN static void
_efl_ui_nstate_activate(Eo *obj, Efl_Ui_Nstate_Data *_pd)
{
   _next_state_set(_pd);
   _state_active(obj, _pd);
}

EOLIAN static void
_efl_ui_nstate_class_constructor(Efl_Class *klass)
{
   evas_smart_legacy_type_register(MY_CLASS_NAME, klass);
}

#include "efl_ui_nstate.eo.c"