aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/e_widget_button.c
blob: 738050ba228ae71220a779d3a195808618e28fbc (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
#include "e.h"

typedef struct _E_Widget_Data E_Widget_Data;
struct _E_Widget_Data
{
   Evas_Object         *o_button;
   Evas_Object         *o_icon;
   void                 (*func)(void *data, void *data2);
   void                *data;
   void                *data2;
};

static void _e_wid_del_hook(Evas_Object *obj);
static void _e_wid_focus_hook(Evas_Object *obj);
static void _e_wid_activate_hook(Evas_Object *obj);
static void _e_wid_disable_hook(Evas_Object *obj);
static void _click(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED);
static void _e_wid_focus_steal(void *data, Evas *e, Evas_Object *obj, void *event_info);
/* local subsystem functions */

/* externally accessible functions */
E_API Evas_Object *
e_widget_button_add(Evas *evas, const char *label, const char *icon, void (*func)(void *data, void *data2), void *data, void *data2)
{
   Evas_Object *obj, *o;
   E_Widget_Data *wd;

   obj = e_widget_add(evas);

   e_widget_del_hook_set(obj, _e_wid_del_hook);
   e_widget_focus_hook_set(obj, _e_wid_focus_hook);
   e_widget_activate_hook_set(obj, _e_wid_activate_hook);
   e_widget_disable_hook_set(obj, _e_wid_disable_hook);
   wd = calloc(1, sizeof(E_Widget_Data));
   wd->func = func;
   wd->data = data;
   wd->data2 = data2;
   e_widget_data_set(obj, wd);

   o = elm_button_add(e_win_evas_win_get(evas));
   evas_object_smart_callback_add(o, "clicked", _click, obj);
   wd->o_button = o;
   if ((label) && (label[0] != 0))
     {
        elm_object_text_set(o, label);
     }

   e_widget_sub_object_add(obj, o);
   evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN,
                                  _e_wid_focus_steal, obj);
   e_widget_resize_object_set(obj, o);
   evas_object_show(o);

   if (icon)
     {
        o = e_icon_add(evas);
        wd->o_icon = o;
        e_util_icon_theme_set(o, icon);
        elm_object_content_set(wd->o_button, o);
        e_widget_sub_object_add(obj, o);
        evas_object_show(o);
     }

   return obj;
}

E_API void
e_widget_button_label_set(Evas_Object *obj, const char *label)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   elm_object_text_set(wd->o_button, label);
}

E_API void
e_widget_button_icon_set(Evas_Object *obj, Evas_Object *icon)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   if (wd->o_icon)
     {
        e_widget_sub_object_del(obj, wd->o_icon);
        evas_object_hide(wd->o_icon);
        elm_object_content_unset(wd->o_button);
        evas_object_del(wd->o_icon);
        wd->o_icon = NULL;
     }
   if (icon)
     {
        wd->o_icon = icon;
        elm_object_content_set(wd->o_button, icon);
        evas_object_show(icon);
        e_widget_sub_object_add(obj, icon);
     }
}

static void
_e_wid_del_hook(Evas_Object *obj)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   free(wd);
}

static void
_e_wid_focus_hook(Evas_Object *obj)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   elm_object_focus_set(wd->o_button, e_widget_focus_get(obj));
}

static void
_e_wid_activate_hook(Evas_Object *obj)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   if (wd->func) wd->func(wd->data, wd->data2);
}

static void
_e_wid_disable_hook(Evas_Object *obj)
{
   E_Widget_Data *wd;

   wd = e_widget_data_get(obj);
   elm_object_disabled_set(wd->o_button, e_widget_disabled_get(obj));
}

static void
_click(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Evas_Object *wid;

   wid = data;
   if ((!wid) || (e_widget_disabled_get(wid))) return;
   e_widget_focus_steal(wid);
   e_widget_change(wid);
   _e_wid_activate_hook(wid);
}

static void
_e_wid_focus_steal(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   e_widget_focus_steal(data);
}