aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/elementary/efl_ui_alert_popup.c
blob: 1fe6f55a34fa8af5e994e6a546a203e187fd69eb (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#define EFL_PART_PROTECTED

#include <Elementary.h>

#include "elm_priv.h"
#include "efl_ui_alert_popup_private.h"
#include "efl_ui_alert_popup_part.eo.h"
#include "elm_part_helper.h"

#define MY_CLASS EFL_UI_ALERT_POPUP_CLASS
#define MY_CLASS_NAME "Efl.Ui.Alert_Popup"

static const char PART_NAME_BUTTON[] = "button";
static const char PART_NAME_BUTTON_LAYOUT[EFL_UI_ALERT_POPUP_BUTTON_COUNT][15] =
                                                {"button_layout1",
                                                 "button_layout2",
                                                 "button_layout3"};

static const char BUTTON_SWALLOW_NAME[EFL_UI_ALERT_POPUP_BUTTON_COUNT][20] =
                                                {"efl.button1",
                                                 "efl.button2",
                                                 "efl.button3"};

static Eina_Bool
_efl_ui_alert_popup_text_set(Eo *obj, Efl_Ui_Alert_Popup_Data *pd, const char *part, const char *label)
{
   if (eina_streq(part, "title") || eina_streq(part, "efl.text.title"))
     {
        Eina_Bool changed = eina_stringshare_replace(&pd->title_text, label);
        if (changed)
          {
             efl_text_set(efl_part(efl_super(obj, MY_CLASS), "title"), label);
             if (label)
               elm_layout_signal_emit(obj, "efl,title,show", "efl");
             else
               elm_layout_signal_emit(obj, "efl,title,hide", "efl");

             ELM_WIDGET_DATA_GET_OR_RETURN(obj, wd, EINA_FALSE);
             edje_object_message_signal_process(wd->resize_obj);
             elm_layout_sizing_eval(obj);
          }
     }
   else
     efl_text_set(efl_part(efl_super(obj, MY_CLASS), part), label);

   return EINA_TRUE;
}

const char *
_efl_ui_alert_popup_text_get(Eo *obj EINA_UNUSED, Efl_Ui_Alert_Popup_Data *pd, const char *part)
{
   if (eina_streq(part, "title") || eina_streq(part, "efl.text.title"))
     {
        if (pd->title_text)
          return pd->title_text;

        return NULL;
     }

   return efl_text_get(efl_part(efl_super(obj, MY_CLASS), part));
}

static void
_positive_button_clicked_cb(void *data, const Efl_Event *ev EINA_UNUSED)
{
   Eo *popup_obj = data;

   Efl_Ui_Alert_Popup_Button_Clicked_Event event;
   event.button_type = EFL_UI_ALERT_POPUP_BUTTON_POSITIVE;

   efl_event_callback_call(popup_obj, EFL_UI_ALERT_POPUP_EVENT_BUTTON_CLICKED, &event);
}

static void
_negative_button_clicked_cb(void *data, const Efl_Event *ev EINA_UNUSED)
{
   Eo *popup_obj = data;

   Efl_Ui_Alert_Popup_Button_Clicked_Event event;
   event.button_type = EFL_UI_ALERT_POPUP_BUTTON_NEGATIVE;

   efl_event_callback_call(popup_obj, EFL_UI_ALERT_POPUP_EVENT_BUTTON_CLICKED, &event);
}

static void
_user_button_clicked_cb(void *data, const Efl_Event *ev EINA_UNUSED)
{
   Eo *popup_obj = data;

   Efl_Ui_Alert_Popup_Button_Clicked_Event event;
   event.button_type = EFL_UI_ALERT_POPUP_BUTTON_USER;

   efl_event_callback_call(popup_obj, EFL_UI_ALERT_POPUP_EVENT_BUTTON_CLICKED, &event);
}

static void
_apply_button_style(Eo *obj, Efl_Ui_Alert_Popup_Data *pd, int button_cnt)
{
   if (pd->button[EFL_UI_ALERT_POPUP_BUTTON_USER])
     {
        if (button_cnt > 1)
          elm_widget_element_update(obj,
                                    pd->button[EFL_UI_ALERT_POPUP_BUTTON_USER],
                                    "left_button");
     }

   if (pd->button[EFL_UI_ALERT_POPUP_BUTTON_POSITIVE])
     {
        if (button_cnt == 2)
          {
             if (pd->button[EFL_UI_ALERT_POPUP_BUTTON_USER])
               elm_widget_element_update(obj,
                                         pd->button[EFL_UI_ALERT_POPUP_BUTTON_POSITIVE],
                                         "right_button");
             else
               elm_widget_element_update(obj,
                                         pd->button[EFL_UI_ALERT_POPUP_BUTTON_POSITIVE],
                                         "left_button");
          }
     }

   if (pd->button[EFL_UI_ALERT_POPUP_BUTTON_NEGATIVE])
     {
        if (button_cnt > 1)
          elm_widget_element_update(obj,
                                    pd->button[EFL_UI_ALERT_POPUP_BUTTON_NEGATIVE],
                                    "right_button");
     }
}

EOLIAN static void
_efl_ui_alert_popup_button_set(Eo *obj, Efl_Ui_Alert_Popup_Data *pd, Efl_Ui_Alert_Popup_Button type, const char *text, Eo *icon)
{
   int i;
   Eina_Bool is_btn_created = EINA_FALSE;
   Eo *cur_content;
   ELM_WIDGET_DATA_GET_OR_RETURN(obj, wd);

   if ((type < EFL_UI_ALERT_POPUP_BUTTON_POSITIVE) || (type > EFL_UI_ALERT_POPUP_BUTTON_USER))
     {
        ERR("Wrong type (%d) is passed!", type);
        return;
     }
   if (!pd->button[type])
     {
        is_btn_created = EINA_TRUE;
        pd->button[type] = efl_add(EFL_UI_BUTTON_CLASS, obj,
                                   elm_widget_element_update(obj, efl_added,
                                                             PART_NAME_BUTTON));
        switch (type)
          {
            case EFL_UI_ALERT_POPUP_BUTTON_POSITIVE:
              efl_event_callback_add(pd->button[type], EFL_UI_EVENT_CLICKED,
                                     _positive_button_clicked_cb, obj);
              break;
            case EFL_UI_ALERT_POPUP_BUTTON_NEGATIVE:
              efl_event_callback_add(pd->button[type], EFL_UI_EVENT_CLICKED,
                                     _negative_button_clicked_cb, obj);
              break;
            case EFL_UI_ALERT_POPUP_BUTTON_USER:
              efl_event_callback_add(pd->button[type], EFL_UI_EVENT_CLICKED,
                                     _user_button_clicked_cb, obj);
              break;
            default:
              break;
          }
     }
   else
     {
        const char *pre_text = efl_text_get(pd->button[type]);
        if ((pre_text != NULL) && (text != NULL) &&
            (!strcmp(pre_text, text)) &&
            (efl_content_get(pd->button[type]) == icon))
          return;
     }

   efl_text_set(pd->button[type], text);
   efl_content_set(pd->button[type], icon);

   if (is_btn_created)
     {
        int btn_count = !!pd->button[EFL_UI_ALERT_POPUP_BUTTON_POSITIVE] +
                        !!pd->button[EFL_UI_ALERT_POPUP_BUTTON_NEGATIVE] +
                        !!pd->button[EFL_UI_ALERT_POPUP_BUTTON_USER];

        cur_content = efl_content_get(efl_part(obj, "efl.buttons"));
        if (cur_content)
          {
             for (i = 0; i < EFL_UI_ALERT_POPUP_BUTTON_COUNT; i++)
              efl_content_unset(efl_part(cur_content, BUTTON_SWALLOW_NAME[i]));
          }
        else
          {
             cur_content = efl_add(EFL_UI_LAYOUT_CLASS, obj,
                           efl_content_set(efl_part(obj, "efl.buttons"), efl_added));
          }

        elm_widget_element_update(obj, cur_content, PART_NAME_BUTTON_LAYOUT[btn_count - 1]);

        _apply_button_style(obj, pd, btn_count);

        i = 0;
        if (pd->button[EFL_UI_ALERT_POPUP_BUTTON_USER])
          {
             efl_content_set(efl_part(cur_content, BUTTON_SWALLOW_NAME[i]),
                             pd->button[EFL_UI_ALERT_POPUP_BUTTON_USER]);
             i++;
          }

        if (pd->button[EFL_UI_ALERT_POPUP_BUTTON_POSITIVE])
          {
             efl_content_set(efl_part(cur_content, BUTTON_SWALLOW_NAME[i]),
                             pd->button[EFL_UI_ALERT_POPUP_BUTTON_POSITIVE]);
             i++;
          }

        if (pd->button[EFL_UI_ALERT_POPUP_BUTTON_NEGATIVE])
          {
             efl_content_set(efl_part(cur_content, BUTTON_SWALLOW_NAME[i]),
                             pd->button[EFL_UI_ALERT_POPUP_BUTTON_NEGATIVE]);
          }

        elm_layout_signal_emit(obj, "efl,buttons,show", "efl");
        edje_object_message_signal_process(wd->resize_obj);
     }

   elm_layout_sizing_eval(obj);
}

EOLIAN static Eo *
_efl_ui_alert_popup_efl_object_constructor(Eo *obj,
                                           Efl_Ui_Alert_Popup_Data *pd EINA_UNUSED)
{
   ELM_WIDGET_DATA_GET_OR_RETURN(obj, wd, NULL);

   if (!elm_widget_theme_klass_get(obj))
     elm_widget_theme_klass_set(obj, "alert_popup");
   obj = efl_constructor(efl_super(obj, MY_CLASS));
   efl_canvas_object_type_set(obj, MY_CLASS_NAME);

   elm_widget_sub_object_parent_add(obj);

   return obj;
}

EOLIAN static void
_efl_ui_alert_popup_efl_object_destructor(Eo *obj, Efl_Ui_Alert_Popup_Data *pd)
{
   ELM_SAFE_FREE(pd->title_text, eina_stringshare_del);
   efl_destructor(efl_super(obj, MY_CLASS));
}

static Eina_Bool
_part_is_efl_ui_alert_popup_part(const Eo *obj EINA_UNUSED, const char *part)
{
   return (eina_streq(part, "title") || eina_streq(part, "efl.text.title"));
}

/* Efl.Part begin */
ELM_PART_OVERRIDE_PARTIAL(efl_ui_alert_popup, EFL_UI_ALERT_POPUP,
                          Efl_Ui_Alert_Popup_Data, _part_is_efl_ui_alert_popup_part)
ELM_PART_OVERRIDE_TEXT_SET(efl_ui_alert_popup, EFL_UI_ALERT_POPUP, Efl_Ui_Alert_Popup_Data)
ELM_PART_OVERRIDE_TEXT_GET(efl_ui_alert_popup, EFL_UI_ALERT_POPUP, Efl_Ui_Alert_Popup_Data)
#include "efl_ui_alert_popup_part.eo.c"

/* Efl.Part end */

#include "efl_ui_alert_popup.eo.c"