Languages

Menu
Sites
Language
How to center a box?

I am ashamed that I need to ask this question, but my searching has found nothing and the documentation is unhelpful and borderline unusable.

I have (based on the native watchface template):

  1. Window
  2. Conformant
  3. Box
  4. Three labels

Code:

static void
create_base_gui(appdata_s *ad, int width, int height)
{
    int ret;
	watch_time_h watch_time = NULL;

	/* Window */
	ret = watch_app_get_elm_win(&ad->win);
	if (ret != APP_ERROR_NONE) {
		dlog_print(DLOG_ERROR, LOG_TAG, "failed to get window. err = %d", ret);
		return;
	}

	evas_object_resize(ad->win, width, height);

	/* Conformant */
	ad->conform = elm_conformant_add(ad->win);
	evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	elm_win_resize_object_add(ad->win, ad->conform);
	evas_object_show(ad->conform);

	/* Box */
	ad->box = elm_box_add(ad->win);
	evas_object_size_hint_weight_set(ad->box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_size_hint_align_set(ad->box, EVAS_HINT_FILL, EVAS_HINT_FILL);
	elm_win_resize_object_add(ad->conform, ad->box); // what does this do? do I need it?
	evas_object_show(ad->box);

	/* Time label*/
	ad->time_label = elm_label_add(ad->conform);
	evas_object_resize(ad->time_label, width, height / 3);
	//evas_object_move(ad->time_label, 0, height / 3);

	evas_object_size_hint_weight_set(ad->time_label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	//evas_object_size_hint_align_set(ad->time_label, 0.5, 0.5);

	evas_object_show(ad->time_label);
	elm_box_pack_end(ad->box, ad->time_label);


	/* Date label*/
	ad->date_label = elm_label_add(ad->box);
	evas_object_resize(ad->date_label, width, height / 3);
	//evas_object_move(ad->time_label, 0, height / 3);

	evas_object_size_hint_weight_set(ad->date_label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	//evas_object_size_hint_align_set(ad->date_label, 0.5, 0.5);

	evas_object_show(ad->date_label);
	elm_box_pack_start(ad->box, ad->date_label);

	/* Battery label */
	ad->battery_label = elm_label_add(ad->box);
	evas_object_resize(ad->battery_label, width, height / 3);
	//evas_object_move(ad->time_label, 0, height / 3);

	evas_object_size_hint_weight_set(ad->battery_label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	//evas_object_size_hint_align_set(ad->battery_label, 0.5, 0.5);

	evas_object_show(ad->battery_label);
	elm_box_pack_end(ad->box, ad->battery_label);

	//elm_box_align_set(ad->box, 0.5, 0.5);



	ret = watch_time_get_current_time(&watch_time);
	if (ret != APP_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "failed to get current time. err = %d", ret);

	update_watch(ad, watch_time, 0);
	watch_time_delete(watch_time);

	/* Show window after base gui is set up */
	evas_object_show(ad->win);
}

I've tried everything I can think of, but the box is never centered within the screen. It's always stuck to the top-left corner, but all the labels are attached within the box. Screenshot attached:

What is the code to center the box on the screen?

Edited by: Nitai S on 31 Jul, 2019

Responses

1 Replies
Nitai S

Obviously, after struggling with this nonsense for two days, I find the answer immediately after posting on the forum.

The magic line that I was missing is;

elm_object_content_set(ad->conform, ad->box);

It wasn't in the original template, presumably because they only put a single label inside the conformant and wanted to position it manually.

So, I guess elm_object_content_set(conform, obj) is the conformant version of elm_win_resize_object_add.

This platform is going to take some getting used to...