[efl-upgrade]
[framework/uifw/elementary.git] / src / lib / elm_button.c
index f7f134b..2d0ce3f 100644 (file)
@@ -3,6 +3,7 @@
 
 /**
  * @defgroup Button Button
+ * @ingroup Elementary
  *
  * This is a push-button. Press it and run some function. It can contain
  * a simple label and icon object.
 
 typedef struct _Widget_Data Widget_Data;
 
+enum
+{
+   DEFAULT = 0,
+   HIGHLIGHTED,
+   FOCUSED,
+   DISABLED,
+};
+
 struct _Widget_Data
 {
    Evas_Object *btn, *icon;
@@ -19,6 +28,8 @@ struct _Widget_Data
    double ar_threshold;
    double ar_interval;
    Ecore_Timer *timer;
+   const char *statelabel[4];
+   int statetype[4];
 };
 
 static const char *widtype = NULL;
@@ -32,6 +43,8 @@ static void _signal_clicked(void *data, Evas_Object *obj, const char *emission,
 static void _signal_pressed(void *data, Evas_Object *obj, const char *emission, const char *source);
 static void _signal_unpressed(void *data, Evas_Object *obj, const char *emission, const char *source);
 static void _on_focus_hook(void *data, Evas_Object *obj);
+static void _set_label(Evas_Object *obj, const char *label);
+static void _signal_default_text_set(void *data, Evas_Object *obj, const char *emission, const char *source);
 
 static const char SIG_CLICKED[] = "clicked";
 static const char SIG_REPEATED[] = "repeated";
@@ -49,6 +62,10 @@ _del_hook(Evas_Object *obj)
    Widget_Data *wd = elm_widget_data_get(obj);
    if (!wd) return;
    if (wd->label) eina_stringshare_del(wd->label);
+   if (wd->statelabel[DEFAULT]) eina_stringshare_del(wd->statelabel[DEFAULT]);
+   if (wd->statelabel[HIGHLIGHTED]) eina_stringshare_del(wd->statelabel[HIGHLIGHTED]);
+   if (wd->statelabel[FOCUSED]) eina_stringshare_del(wd->statelabel[FOCUSED]);
+   if (wd->statelabel[DISABLED]) eina_stringshare_del(wd->statelabel[DISABLED]);
    free(wd);
 }
 
@@ -59,11 +76,21 @@ _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
    if (!wd) return;
    if (elm_widget_focus_get(obj))
      {
+       if(wd->statelabel[FOCUSED])
+         {
+            _set_label(obj, wd->statelabel[FOCUSED]);
+         }
        edje_object_signal_emit(wd->btn, "elm,action,focus", "elm");
        evas_object_focus_set(wd->btn, 1);
      }
    else
      {
+       if(wd->statelabel[DEFAULT])
+         _set_label(obj, wd->statelabel[DEFAULT]);
+#if 0
+       else
+         _set_label(obj, wd->label);
+#endif
        edje_object_signal_emit(wd->btn, "elm,action,unfocus", "elm");
        evas_object_focus_set(wd->btn, 0);
      }
@@ -75,16 +102,20 @@ _theme_hook(Evas_Object *obj)
    Widget_Data *wd = elm_widget_data_get(obj);
    if (!wd) return;
    _elm_theme_object_set(obj, wd->btn, "button", "base", elm_widget_style_get(obj));
+
    if (wd->icon)
      edje_object_part_swallow(wd->btn, "elm.swallow.content", wd->icon);
+   
    if (wd->label)
      edje_object_signal_emit(wd->btn, "elm,state,text,visible", "elm");
    else
      edje_object_signal_emit(wd->btn, "elm,state,text,hidden", "elm");
+   
    if (wd->icon)
      edje_object_signal_emit(wd->btn, "elm,state,icon,visible", "elm");
    else
      edje_object_signal_emit(wd->btn, "elm,state,icon,hidden", "elm");
+
    edje_object_part_text_set(wd->btn, "elm.text", wd->label);
    edje_object_message_signal_process(wd->btn);
    edje_object_scale_set(wd->btn, elm_widget_scale_get(obj) * _elm_config->scale);
@@ -97,9 +128,23 @@ _disable_hook(Evas_Object *obj)
    Widget_Data *wd = elm_widget_data_get(obj);
    if (!wd) return;
    if (elm_widget_disabled_get(obj))
-     edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
+     {
+       if(wd->statelabel[DISABLED] )
+         {
+            _set_label(obj, wd->statelabel[DISABLED]);
+         }
+       edje_object_signal_emit(wd->btn, "elm,state,disabled", "elm");
+     }
    else
-     edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
+     {
+       if(wd->statelabel[DEFAULT])
+         _set_label(obj, wd->statelabel[DEFAULT]);
+#if 0
+       else
+         _set_label(obj, wd->label);
+#endif
+       edje_object_signal_emit(wd->btn, "elm,state,enabled", "elm");
+     }
 }
 
 static void
@@ -107,11 +152,17 @@ _sizing_eval(Evas_Object *obj)
 {
    Widget_Data *wd = elm_widget_data_get(obj);
    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
+   Evas_Coord w, h;
 
    if (!wd) return;
    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
    edje_object_size_min_restricted_calc(wd->btn, &minw, &minh, minw, minh);
    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
+
+   evas_object_size_hint_min_get(obj, &w, &h);
+   if (w > minw) minw = w;
+   if (h > minh) minh = h;
+
    evas_object_size_hint_min_set(obj, minw, minh);
    evas_object_size_hint_max_set(obj, maxw, maxh);
 }
@@ -154,7 +205,9 @@ _signal_clicked(void *data, Evas_Object *obj, const char *emission, const char *
      }
    wd->repeating = EINA_FALSE;
    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
+#if 0
    _signal_unpressed(data, obj, emission, source); /* safe guard when the theme does not emit the 'unpress' signal */
+#endif
 }
 
 static Eina_Bool
@@ -193,6 +246,10 @@ _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __
    Widget_Data *wd = elm_widget_data_get(data);
    if (!wd) return;
 
+   if(wd->statelabel[HIGHLIGHTED])
+     {
+       _set_label(data, wd->statelabel[HIGHLIGHTED]);
+     }
    if (wd->autorepeat && !wd->repeating)
      {
        if (wd->ar_threshold <= 0.0)
@@ -203,10 +260,30 @@ _signal_pressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __
 }
 
 static void
+_signal_default_text_set(void *data, Evas_Object *obj, const char *emission, const char *source)
+{
+   Widget_Data *wd = elm_widget_data_get(data);
+   if (!wd) return;
+   if(wd->statelabel[DEFAULT])
+     _set_label(data, wd->statelabel[DEFAULT]);
+#if 0
+   else
+     _set_label(data, wd->label);
+#endif
+   return;
+}
+
+static void
 _signal_unpressed(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
 {
    Widget_Data *wd = elm_widget_data_get(data);
    if (!wd) return;
+   if(wd->statelabel[DEFAULT])
+     _set_label(data, wd->statelabel[DEFAULT]);
+#if 0
+   else
+     _set_label(data, wd->label);
+#endif
 
    if (wd->timer)
      {
@@ -242,16 +319,26 @@ elm_button_add(Evas_Object *parent)
    elm_widget_del_hook_set(obj, _del_hook);
    elm_widget_theme_hook_set(obj, _theme_hook);
    elm_widget_disable_hook_set(obj, _disable_hook);
-   elm_widget_can_focus_set(obj, 1 );                 
+   elm_widget_can_focus_set(obj, 1 );
 
    wd->btn = edje_object_add(e);
    _elm_theme_object_set(obj, wd->btn, "button", "base", "default");
+   wd->statetype[DEFAULT] = 0;
+   wd->statetype[HIGHLIGHTED] = 0;
+   wd->statetype[FOCUSED] = 0;
+   wd->statetype[DISABLED] = 0;
+   wd->statelabel[DEFAULT] = 0;
+   wd->statelabel[HIGHLIGHTED] = 0;
+   wd->statelabel[FOCUSED] = 0;
+   wd->statelabel[DISABLED] = 0;
    edje_object_signal_callback_add(wd->btn, "elm,action,click", "",
                                    _signal_clicked, obj);
    edje_object_signal_callback_add(wd->btn, "elm,action,press", "",
                                    _signal_pressed, obj);
    edje_object_signal_callback_add(wd->btn, "elm,action,unpress", "",
                                    _signal_unpressed, obj);
+   edje_object_signal_callback_add(wd->btn, "elm,action,default,text,set", "",
+               _signal_default_text_set, obj);
    elm_widget_resize_object_set(obj, wd->btn);
 
    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
@@ -288,6 +375,66 @@ elm_button_label_set(Evas_Object *obj, const char *label)
    _sizing_eval(obj);
 }
 
+static void
+_set_label(Evas_Object *obj, const char *label)
+{
+   Widget_Data *wd = elm_widget_data_get(obj);
+
+   edje_object_message_signal_process(wd->btn);
+   edje_object_part_text_set(wd->btn, "elm.text", label);
+   _sizing_eval(obj);
+}
+/**
+ * Set the label for each state of button
+ *
+ * @param obj The button object
+ * @param label The text will be written on the button
+ * @param state The state of button
+ *
+ * @ingroup Button
+ */
+EAPI void
+elm_button_label_set_for_state(Evas_Object *obj, const char *label, UIControlState state)
+{
+   Widget_Data *wd = elm_widget_data_get(obj);
+
+   if (!wd) return;
+   if(label == NULL) return;
+
+   if(state == UIControlStateDefault)
+     {
+       wd->statetype[DEFAULT] = UIControlStateDefault;
+       eina_stringshare_replace(&wd->statelabel[DEFAULT], label);
+       return;
+     }
+   if(state == UIControlStateHighlighted)
+     {
+       wd->statetype[HIGHLIGHTED] = UIControlStateHighlighted;
+       eina_stringshare_replace(&wd->statelabel[HIGHLIGHTED], label);
+       return;
+     }
+   if(state == UIControlStateFocused)
+     {
+       wd->statetype[FOCUSED] = UIControlStateFocused;
+       eina_stringshare_replace(&wd->statelabel[FOCUSED], label);
+       return;
+     }
+   if(state == UIControlStateDisabled)
+     {
+       wd->statetype[DISABLED] = UIControlStateDisabled;
+       eina_stringshare_replace(&wd->statelabel[DISABLED], label);
+       return;
+     }
+}
+
+/**
+ * Get the label of button
+ *
+ * @param obj The button object
+ * @return The title of button
+ *
+ * @ingroup Button
+ */
 EAPI const char *
 elm_button_label_get(const Evas_Object *obj)
 {
@@ -296,6 +443,32 @@ elm_button_label_get(const Evas_Object *obj)
    if (!wd) return NULL;
    return wd->label;
 }
+/**
+ * Get the label of button for each state
+ *
+ * @param obj The button object
+ * @param state The state of button
+ * @return The title of button for state
+ *
+ * @ingroup Button
+ */
+EAPI const char*
+elm_button_label_get_for_state(const Evas_Object *obj, UIControlState state)
+{
+   Widget_Data *wd = elm_widget_data_get(obj);
+   if (!wd) return NULL;
+
+   if(state == UIControlStateDefault)
+     return wd->statelabel[DEFAULT];
+   else if(state == UIControlStateHighlighted)
+     return wd->statelabel[HIGHLIGHTED];
+   else if(state == UIControlStateFocused)
+     return wd->statelabel[FOCUSED];
+   else if(state == UIControlStateDisabled)
+     return wd->statelabel[DISABLED];
+   else
+     return NULL;
+}
 
 /**
  * Set the icon used for the button
@@ -399,7 +572,7 @@ elm_button_autorepeat_initial_timeout_set(Evas_Object *obj, double t)
  *
  * @ingroup Button
  */
-EAPI void         
+EAPI void
 elm_button_autorepeat_gap_timeout_set(Evas_Object *obj, double t)
 {
    ELM_CHECK_WIDTYPE(obj, widtype);