/* To Build
cc ent.c -o ent `pkg-config --cflags --libs elementary`
 */
#include <Elementary.h>

static void
_fill_expand(Evas_Object *o)
{
  // we want this object to allow its space to expand AND to then fll that
  evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  evas_object_size_hint_align_set(o, EVAS_HINT_FILL, EVAS_HINT_FILL);
}

static void
_table_sizing_add(Evas_Object *tb, int x, int y, int w, int h,
                  int minw, int minh)
{
  // sizing obj to force a table cell to be a minimum size - standard elm
  // trick to use a "sizing object" as a restriction then se table cell logic
  // which will limit the sizing of the cell to the largest of all the content
  // in the cell - be it this sizing we never show or the entry
  Evas_Object *o = evas_object_rectangle_add(evas_object_evas_get(tb));
  _fill_expand(o);
  // min size and make it scalable...
  evas_object_size_hint_min_set(o, ELM_SCALE_SIZE(minw), ELM_SCALE_SIZE(minh));
  elm_table_pack(tb, o, x, y, w, h); // yes pack into same table cell as others
}

static void
_entry_bg_color_rect_add(Evas_Object *en, int r, int g, int b, int a)
{
  // yup - you can put ANY object as a background to a scroller and
  // entires if scrollable have a scroller! could be an image - a video
  // from emotion... a whole edje object or even widgets! (might not be useful
  // to interact with... but a table with more content for example...)
  Evas_Object *o = evas_object_rectangle_add(evas_object_evas_get(en));
  _fill_expand(o);
  elm_object_part_content_set(en, "elm.swallow.background", o);
  evas_object_color_set(o, r, g, b, a);
  evas_object_show(o);
}

static Evas_Object *
_entry_add(Evas_Object *tb, int x, int y, int w, int h, const char *str)
{
  // make an entry, make it scrollable, have some text and put it int he table
  Evas_Object *o = elm_entry_add(tb);
  _fill_expand(o);
  elm_entry_scrollable_set(o, EINA_TRUE);
  elm_object_text_set(o, str);
  elm_table_pack(tb, o, x, y, w, h);
  evas_object_show(o);
  return o;
}

// app main set up after ELM_MAIN() does some setup work for us
EAPI_MAIN int
elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED) // we dont use args
{
  Evas_Object *o, *win, *tb, *en; // some vars we need

  // this is not default as it's unexpected behavior - but let elm handle
  // exiting the main loop that runs once the last window is closed
  elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);

  // standard boring window - close button auto-deletes this object and that
  // triggers the above p[olicy to exit
  win = o = elm_win_util_standard_add("Main", "2 Entries");
  elm_win_autodel_set(o, EINA_TRUE);

  // tables are amazingly versatile. let's use that
  tb = o = elm_table_add(win);
  _fill_expand(o);
  // windows can have a stack of N objects as resize objects that resize along
  // with the window (and affect the window min and max sizes etc.)
  elm_win_resize_object_add(win, o);
  evas_object_show(o);

  // our first entry with a pinkish background tint - let's tint using alpha
  // and not a raw color so this works better with different themes...
  en = _entry_add(tb, 0, 0, 1, 1,
                  "This is the first entry.<br>"
                  "Also a second line.");
  _entry_bg_color_rect_add(en, 20, 10, 10, 20); // pink tint: semi-trans
  _table_sizing_add(tb,   0, 0, 1, 1,   160, 120);

  // second entry - a big bigger and less tall with a green tint
  en = _entry_add(tb, 0, 1, 1, 1,
                  "This is some other text.<br>"
                  "Also a second line<br>"
                  "And a 3rd line now with <b>BOLD</> and <em>italic</> text");
  _entry_bg_color_rect_add(en, 0, 20, 0, 20); // green tint: semi-trans
  _table_sizing_add(tb,   0, 1, 1, 1,   240, 120);

  // and at the very end show window and launch our main loop
  evas_object_show(win);
  elm_run();
  // process exit code... :)
  return 0;
}
ELM_MAIN()
