2 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
4 #include <Elementary.h>
8 * @addtogroup Toolbar Toolbar
10 * @addtogroup Toolbar Toolbar
11 * For listing a selection of items in a list within a "bar", each item having an icon and label.
12 * This is more or less intended for use when selecting different modes - much like a tab widget,
13 * but this is just the bar piece.
16 typedef struct _Widget_Data Widget_Data;
20 Evas_Object *scr, *bx;
23 Eina_Bool scrollable : 1;
24 Eina_Bool homogeneous : 1;
28 struct _Elm_Toolbar2_Item
33 void (*func) (void *data, Evas_Object *obj, void *event_info);
34 void (*del_cb) (void *data, Evas_Object *obj, void *event_info);
38 static void _item_show(Elm_Toolbar2_Item *it);
39 static void _del_pre_hook(Evas_Object *obj);
40 static void _del_hook(Evas_Object *obj);
41 static void _theme_hook(Evas_Object *obj);
42 static void _sizing_eval(Evas_Object *obj);
43 static void _resize(void *data, Evas *e, Evas_Object *obj, void *event_info);
44 static void press_down_cb(void *data, Evas *evas, Evas_Object *obj, void *event_info);
45 static void press_up_cb(void *data, Evas *evas, Evas_Object *obj, void *event_info);
47 static void _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data);
49 static void _item_show(Elm_Toolbar2_Item *it)
51 Widget_Data *wd = elm_widget_data_get(it->obj);
52 Evas_Coord x, y, w, h, bx, by, bw, bh;
55 evas_object_geometry_get(wd->bx, &bx, &by, &bw, &bh);
56 evas_object_geometry_get(it->base, &x, &y, &w, &h);
57 elm_smart_scroller_child_region_show(wd->scr, x - bx, y - by, w, h);
62 static void _del_pre_hook(Evas_Object *obj)
64 Widget_Data *wd = elm_widget_data_get(obj);
65 Elm_Toolbar2_Item *it;
68 EINA_LIST_FREE(wd->items, it)
70 if (it->del_cb) it->del_cb((void *)it->data, it->obj, it);
71 if (it->icon) evas_object_del(it->icon);
72 evas_object_del(it->base);
77 static void _del_hook(Evas_Object *obj)
79 Widget_Data *wd = elm_widget_data_get(obj);
85 static void _theme_hook(Evas_Object *obj)
87 Widget_Data *wd = elm_widget_data_get(obj);
89 Elm_Toolbar2_Item *it;
90 const char *style = elm_widget_style_get(obj);
95 scale = (elm_widget_scale_get(obj) * _elm_config->scale);
96 edje_object_scale_set(wd->scr, scale);
97 EINA_LIST_FOREACH(wd->items, l, it)
100 Evas_Coord ic_w, ic_h;
102 edje_object_scale_set(it->base, scale);
104 _elm_theme_set(it->base, "toolbar2", "item", style);
108 evas_object_size_hint_min_get(it->icon, &ic_w, &ic_h);
110 ms = ((double)wd->icon_size * _elm_config->scale);
111 if (ic_w > 0 && ic_h > 0)
113 if (ic_h > wd->icon_size) ic_h = wd->icon_size;
114 evas_object_size_hint_min_set(it->icon, ic_w, ic_h);
115 evas_object_size_hint_max_set(it->icon, ic_w, ic_h);
118 evas_object_size_hint_min_set(it->icon, ms, ms);
119 evas_object_size_hint_max_set(it->icon, ms, ms);
122 evas_object_size_hint_align_set(it->icon, 0.5, 0.5);
123 edje_object_part_swallow(it->base, "elm.swallow.icon", it->icon);
124 evas_object_show(it->icon);
125 elm_widget_sub_object_add(obj, it->icon);
129 elm_coords_finger_size_adjust(1, &mw, 1, &mh);
130 edje_object_size_min_restricted_calc(it->base, &mw, &mh, mw, mh);
131 elm_coords_finger_size_adjust(1, &mw, 1, &mh);
133 evas_object_size_hint_weight_set(it->base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
134 evas_object_size_hint_align_set(it->base, 0.5, 0.5);
136 if (ic_w > 0 && ic_h > 0) {
137 evas_object_size_hint_min_set(it->base, ic_w, ic_h);
140 // evas_object_size_hint_min_set(it->base, mw, mh);
141 ms = ((double)wd->icon_size * _elm_config->scale);
143 evas_object_size_hint_min_set(it->base, ms, ms);
149 static void _sizing_eval(Evas_Object *obj)
151 Widget_Data *wd = elm_widget_data_get(obj);
152 Evas_Coord minw = -1, minh = -1;
153 Evas_Coord vw = 0, vh = 0;
158 evas_object_smart_calculate(wd->bx);
159 edje_object_size_min_calc(elm_smart_scroller_edje_object_get(wd->scr), &minw, &minh);
160 evas_object_geometry_get(obj, NULL, NULL, &w, &h);
162 if (w < minw) w = minw;
163 if (h < minh) h = minh;
165 evas_object_resize(wd->scr, w, h);
167 evas_object_size_hint_min_get(wd->bx, &minw, &minh);
168 if (w > minw) minw = w;
170 evas_object_resize(wd->bx, minw, 66);
171 if (w > minw) minw = w;
172 elm_smart_scroller_child_viewport_size_get(wd->scr, &vw, &vh);
177 minw = minw + (w - vw);
179 minh = minh + (h - vh);
181 evas_object_size_hint_min_set(obj, vw, vh);
182 evas_object_size_hint_max_set(obj, -1, -1);
185 static void _resize(void *data, Evas *e, Evas_Object *obj, void *event_info)
187 Widget_Data *wd = elm_widget_data_get(data);
188 Evas_Coord mw, mh, vw, vh, x, y, w, h;
190 Elm_Toolbar2_Item *it;
194 elm_smart_scroller_child_viewport_size_get(wd->scr, &vw, &vh);
195 evas_object_size_hint_min_get(wd->bx, &mw, &mh);
196 evas_object_geometry_get(wd->bx, NULL, NULL, &w, &h);
200 if (w != vw) evas_object_resize(wd->bx, vw, h);
204 static void press_down_cb(void *data, Evas *evas, Evas_Object *obj, void *event_info)
206 Evas_Event_Mouse_Down *ev = event_info;
207 // printf("\nDown %d\n", ev->flags);
208 //Evas_Event_Mouse_Down *ev = event_info;
209 Elm_Toolbar2_Item *it = (Elm_Toolbar2_Item *)data;
210 edje_object_signal_emit(it->base, "elm,state,selected", "elm");
214 static void press_up_cb(void *data, Evas *evas, Evas_Object *obj, void *event_info)
217 Elm_Toolbar2_Item *it = (Elm_Toolbar2_Item *)data;
219 edje_object_signal_emit(it->base, "elm,state,unselected", "elm");
221 Evas_Event_Mouse_Up *ev = event_info;
222 // printf("\nUp %d\n", ev->flags);
223 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) {
224 // printf("\nReturn\n");
228 // printf("\nSend\n");
229 if (it->func) it->func((void *)(it->data), it->obj, it);
230 evas_object_smart_callback_call(it->obj, "clicked", it);
235 static void _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data)
237 Widget_Data *wd = data;
239 _els_box_layout(o, priv, 1, wd->homogeneous);
243 * Add a new Toolbar object
245 * @param parent The parent object
246 * @return The new object or NULL if it cannot be created
250 EAPI Evas_Object *elm_toolbar2_add(Evas_Object *parent)
256 wd = ELM_NEW(Widget_Data);
257 e = evas_object_evas_get(parent);
258 obj = elm_widget_add(e);
259 elm_widget_type_set(obj, "toolbar");
260 elm_widget_sub_object_add(parent, obj);
261 elm_widget_data_set(obj, wd);
262 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
263 elm_widget_del_hook_set(obj, _del_hook);
264 elm_widget_theme_hook_set(obj, _theme_hook);
265 elm_widget_can_focus_set(obj, 0);
267 wd->scr = elm_smart_scroller_add(e);
268 elm_smart_scroller_bounce_allow_set(wd->scr, 0, 0);
269 elm_smart_scroller_theme_set(wd->scr, "toolbar2", "base", "default");
270 elm_widget_resize_object_set(obj, wd->scr);
271 elm_smart_scroller_policy_set(wd->scr,
272 ELM_SMART_SCROLLER_POLICY_AUTO,
273 ELM_SMART_SCROLLER_POLICY_OFF);
276 wd->scrollable = EINA_TRUE;
277 wd->homogeneous = EINA_TRUE;
280 wd->bx = evas_object_box_add(e);
281 evas_object_size_hint_align_set(wd->bx, wd->align, 0.5);
282 evas_object_box_layout_set(wd->bx, _layout, wd, NULL);
283 elm_widget_sub_object_add(obj, wd->bx);
284 elm_smart_scroller_child_set(wd->scr, wd->bx);
285 evas_object_show(wd->bx);
287 evas_object_event_callback_add(wd->scr, EVAS_CALLBACK_RESIZE, _resize, obj);
294 * Set the size of icon of Toolbar object
296 * @param obj The Toolbar object
297 * @param icon_size The size of icon to be set
301 EAPI void elm_toolbar2_icon_size_set(Evas_Object *obj, int icon_size)
303 Widget_Data *wd = elm_widget_data_get(obj);
306 if (icon_size > 50) return;
307 if (wd->icon_size == icon_size) return;
308 wd->icon_size = icon_size;
313 * Get the size of icon of Toolbar object
315 * @param obj The Toolbar object
316 * @return The size of icon to be set
320 EAPI int elm_toolbar2_icon_size_get(Evas_Object *obj)
322 Widget_Data *wd = elm_widget_data_get(obj);
325 return wd->icon_size;
329 * Add items to Toolbar object
331 * @param obj The Toolbar object
332 * @param icon The icon
333 * @param label The Label
334 * @param func The function
335 * @param data The data
336 * @return TheToolbar item
340 EAPI Elm_Toolbar2_Item *elm_toolbar2_item_add(Evas_Object *obj, Evas_Object *icon, const char *label, void (*func) (void *data, Evas_Object *obj, void *event_info), const void *data)
342 Widget_Data *wd = elm_widget_data_get(obj);
345 Evas_Coord ic_w, ic_h;
346 Elm_Toolbar2_Item *it;
348 if (!wd) return NULL;
349 it = ELM_NEW(Elm_Toolbar2_Item);
350 if (!it) return NULL;
351 wd->items = eina_list_append(wd->items, it);
357 it->base = edje_object_add(evas_object_evas_get(obj));
359 _elm_theme_set(it->base, "toolbar2", "item", elm_widget_style_get(obj));
361 evas_object_event_callback_add(it->icon, EVAS_CALLBACK_MOUSE_DOWN, press_down_cb, it);
362 evas_object_event_callback_add(it->icon, EVAS_CALLBACK_MOUSE_UP, press_up_cb, it);
364 elm_widget_sub_object_add(obj, it->base);
371 evas_object_size_hint_min_get(it->icon, &ic_w, &ic_h);
373 if (ic_w > 0 && ic_h > 0)
375 if (ic_h > wd->icon_size) ic_h = wd->icon_size;
376 evas_object_size_hint_min_set(it->icon, ic_w, ic_h);
377 evas_object_size_hint_max_set(it->icon, ic_w, ic_h);
380 ms = ((double)wd->icon_size * _elm_config->scale);
382 evas_object_size_hint_min_set(it->icon, ms, ms);
383 evas_object_size_hint_max_set(it->icon, ms, ms);
386 // evas_object_size_hint_weight_set(it->icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
387 // evas_object_size_hint_align_set(it->icon, 0.5, 0.5);
389 edje_object_part_swallow(it->base, "elm.swallow.icon", it->icon);
390 evas_object_show(it->icon);
391 elm_widget_sub_object_add(obj, it->icon);
396 elm_coords_finger_size_adjust(1, &mw, 1, &mh);
397 edje_object_size_min_restricted_calc(it->base, &mw, &mh, mw, mh);
398 elm_coords_finger_size_adjust(1, &mw, 1, &mh);
400 evas_object_size_hint_weight_set(it->base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
401 evas_object_size_hint_align_set(it->base, 0.5, 0.5);
403 if (ic_w > 0 && ic_h > 0) {
404 evas_object_size_hint_min_set(it->base, ic_w, ic_h);
407 // evas_object_size_hint_min_set(it->base, mw, mh);
408 ms = ((double)wd->icon_size * _elm_config->scale);
410 evas_object_size_hint_min_set(it->base, ms, ms);
412 evas_object_box_append(wd->bx, it->base);
413 evas_object_show(it->base);
419 * Get icon of item of Toolbar object
421 * @param item TheToolbar item
422 * @return The icon object
426 EAPI Evas_Object *elm_toolbar2_item_icon_get(Elm_Toolbar2_Item *item)
428 if (!item) return NULL;
434 * Delete the Toolbar item
436 * @param it TheToolbar item
440 EAPI void elm_toolbar2_item_del(Elm_Toolbar2_Item *it)
442 Widget_Data *wd = elm_widget_data_get(it->obj);
443 Evas_Object *obj2 = it->obj;
445 if ((!wd) || (!it)) return;
446 if (it->del_cb) it->del_cb((void *)it->data, it->obj, it);
447 wd->items = eina_list_remove(wd->items, it);
448 if (it->icon) evas_object_del(it->icon);
449 evas_object_del(it->base);
455 * Set the function called when a toolbar item is freed.
457 * @param it The item to set the callback on
458 * @param func The function called
462 EAPI void elm_toolbar2_item_del_cb_set(Elm_Toolbar2_Item *it, void (*func)(void *data, Evas_Object *obj, void *event_info))
470 * Whether toolbar is scrollable
472 * @param obj The Toolbar
473 * @param scrollable The scrollable flag (1 if toolbar is scrollable, 0 otherwise)
477 EAPI void elm_toolbar2_scrollable_set(Evas_Object *obj, Eina_Bool scrollable)
479 Widget_Data *wd = elm_widget_data_get(obj);
482 wd->scrollable = scrollable;
488 * Whether toolbar is homogenous
490 * @param obj The Toolbar
491 * @param homogenous The homogenous flag (1 if toolbar is homogenous, 0 otherwise)
497 elm_toolbar2_homogenous_set(Evas_Object *obj, Eina_Bool homogenous)
499 Widget_Data *wd = elm_widget_data_get(obj);
502 wd->homogeneous = !!homogenous;
503 evas_object_smart_calculate(wd->bx);
508 * Set if the alignment of the items.
510 * @param obj The toolbar object
511 * @param align The new alignment. (left) 0.0 ... 1.0 (right)
515 EAPI void elm_toolbar2_align_set(Evas_Object *obj, double align)
518 Elm_Toolbar2_Item *it;
519 Widget_Data *wd = elm_widget_data_get(obj);
522 if(wd->align != align)
523 evas_object_size_hint_align_set(wd->bx, align, 0.5);