aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/libraries/winFang.c
blob: dc5188a53581eee7d8e5176f0b750ea23b819e69 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include "winFang.h"


void HamrTime(void *elm_main, char *domain)
{
  char *env, cwd[PATH_MAX], temp[PATH_MAX * 2];

  elm_app_compile_bin_dir_set(PACKAGE_BIN_DIR);
  elm_app_compile_data_dir_set(PACKAGE_DATA_DIR);
  elm_app_compile_lib_dir_set(PACKAGE_LIB_DIR);
  elm_app_compile_locale_set(PACKAGE_LOCALE_DIR);
  // Do this after the above calls, but before changing the working directory, or screwing with argv[0].
  // It tries to set up the package paths depending on where the executable is, so things are relocatable.
  // First argument is the elm_main() function that Elementary starts us from.
  // Second argument should be a lower case string used as the "domain", which is different from the log domain.
  //   It's used lower case as part of the data directory path.
  //     So, if prefix is /usr/local, then the system data dir is /usr/local/share,
  //     and this apps data dir is /usr/local/share/"domain".
  //   It's used upper case as part of environment variables to override directory paths at run time.
  //     So "DOMAIN"_PREFIX, "DOMAIN"_BIN_DIR, "DOMAIN"_LIB_DIR, "DOMAIN"_DATA_DIR, and "DOMAIN"_LOCALE_DIR
  // Third argument is the name of a file it can check for to make sure it found the correct path.
  //  This file is looked for in the data dir.
  elm_app_info_set(elm_main, domain, "checkme.txt");
  // Once this is all setup, the code can do -
  // elm_app_prefix_dir_get();  // or bin, lib, data, locale.

  getcwd(cwd, PATH_MAX);
  env = getenv("LUA_CPATH");
  if (!env)  env = "";
  sprintf(temp, "%s;%s/lib?.so;%s/?.so;%s/?.so", env, elm_app_lib_dir_get(), elm_app_lib_dir_get(), cwd);
  setenv("LUA_CPATH", temp, 1);

  env = getenv("LUA_PATH");
  if (!env)  env = "";
  sprintf(temp, "%s;%s/?.lua;%s/?.lua", env, elm_app_lib_dir_get(), cwd);
  setenv("LUA_PATH", temp, 1);
}

static void _checkWindowBounds(winFang *win, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
{
  Evas_Object *test;
  Eina_List *objs, *this;
  Evas_Coord mw, mh;
  int padding = 1, i = 0, overs[4][2];

  // Sanity check.
  if ((win->mw > w) || (win->mh > h))
    return;

  overs[0][0] = x - padding;		overs[0][1] = y - padding;
  overs[1][0] = x + padding + w;	overs[1][1] = y - padding;
  overs[2][0] = x + padding + w;	overs[2][1] = y + padding + h;
  overs[3][0] = x - padding;		overs[3][1] = y + padding + h;

  // If we are over other windows, stop.
  objs = evas_objects_in_rectangle_get(win->e, overs[0][0], overs[0][1], w + (padding * 2), h + (padding * 2), EINA_TRUE, EINA_FALSE);
  EINA_LIST_FOREACH(objs, this, test)
  {
    const char * name = evas_object_name_get(test);
    if (name && (strcmp(WF_LAYOUT, name) == 0))
      i++;
  }
  if (2 <= i)
  {
    return;
  }

  // Check if we are outside the parent window.
  evas_object_geometry_get(win->parent->win, NULL, NULL, &mw, &mh);
  if ((overs[0][0] < 0) || (overs[0][1] < 0))
  {
    return;
  }
  if ((overs[2][0] > mw) || (overs[2][1] > mh))
  {
    return;
  }

  // All good, do it.
  win->x = x;  win->y = y;
  win->w = w;  win->h = h;
  evas_object_geometry_set(win->layout, x, y, w, h);
  evas_object_resize(win->title, w, 15);
  elm_layout_sizing_eval(win->layout);
  for (i = 0; i < 4; i++)
  {
    int cx = win->x, cy = win->y;

         if (i == 1)   cx += win->w;
    else if (i == 2)  {cx += win->w;  cy += win->h;}
    else if (i == 3)   cy += win->h;
    evas_object_move(win->hand[i], cx - 15, cy - 15);
  }
}

static void _onHandleMove(void *data, Evas *evas, Evas_Object *obj, void *event_info)
{
  Evas_Event_Mouse_Move *ev = event_info;
  winFang *win = data;
  Evas_Coord x, y, w, h, dx, dy;
  int i;

  if (!ev->buttons) return;

  dx = ev->cur.canvas.x - ev->prev.output.x;
  dy = ev->cur.canvas.y - ev->prev.output.y;
  evas_object_geometry_get(win->layout, &x, &y, &w, &h);
  for (i = 0; i < 4; i++)
  {
    if (obj == win->hand[i])
    {
      switch (i)
      {
        case 0 :
        {
          w -= dx;  h -= dy;  x += dx;  y += dy;
          break;
        }
        case 1 :
        {
          w += dx;  h -= dy;  y += dy;
          break;
        }
        case 2 :
        {
          w += dx;  h += dy;
          break;
        }
        case 3 :
        {
          w -= dx;  h += dy;  x += dx;
          break;
        }
      }
      _checkWindowBounds(win, x, y, w, h);
      return;
    }
  }
}

static void _onBgMove(void *data, Evas *evas, Evas_Object *obj, void *event_info)
{
  Evas_Event_Mouse_Move *ev = event_info;
  winFang *win = data;
  Evas_Coord x, y, w, h;

  if (1 != ev->buttons)  return;

  // Looks like ePhysics wont cooperate about coords and other things, so plan B.

  evas_object_geometry_get(win->layout, &x, &y, &w, &h);
  _checkWindowBounds(win, x + ev->cur.canvas.x - ev->prev.output.x, y + ev->cur.canvas.y - ev->prev.output.y, w, h);
}

static void _onBgUnclick(void *data, Evas *evas, Evas_Object *obj, void *event_info)
{
  Evas_Event_Mouse_Down *ev = event_info;

  if (1 == ev->button)
  {
    // Stop moving the window.
    evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_UP,   _onBgUnclick);
    evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_MOVE, _onBgMove);
  }
}

static void _onBgClick(void *data, Evas *evas, Evas_Object *obj, void *event_info)
{
  Evas_Event_Mouse_Down *ev = event_info;

  if (1 == ev->button)
  {
    // Start moving the window.
    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,   _onBgUnclick, data);
    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_MOVE, _onBgMove, data);
  }
}

static void _on_done(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
  elm_exit();
}

void winFangHide(winFang *win)
{
  int i;

  evas_object_hide(win->win);
  for (i = 0; i < 4; i++)
    evas_object_hide(win->hand[i]);
}

void winFangShow(winFang *win)
{
  int i;

  evas_object_show(win->win);
  for (i = 0; i < 4; i++)
    evas_object_show(win->hand[i]);
}

winFang *winFangAdd(winFang *parent, int x, int y, int w, int h, char *title, char *name, EPhysics_World *world)
{
  winFang *result;
  Evas_Object *obj;
  char buf[PATH_MAX];
  int i;

  result = calloc(1, sizeof(winFang));
  eina_clist_init(&result->widgets);
  eina_clist_init(&result->winFangs);

  result->parent = parent;
  result->x = x;
  result->y = y;
  result->w = w;
  result->h = h;

  if (result->parent)
  {
    eina_clist_add_head(&parent->winFangs, &result->node);
    obj = parent->win;
    h += 15;
    result->h = h;
  }
  else
  {
    result->win = elm_win_add(NULL, name, ELM_WIN_BASIC);
    evas_object_move(result->win, result->x, result->y);
    evas_object_smart_callback_add(result->win, "delete,request", _on_done, NULL);
    elm_win_title_set(result->win, title);

    obj = result->win;
    x = 0;  y = 0;
  }

  snprintf(buf, sizeof(buf), "%s/winFang.edj", elm_app_data_dir_get());
  result->layout = eo_add(ELM_OBJ_LAYOUT_CLASS, obj,
    evas_obj_size_set(w, h),
    evas_obj_position_set(x, y),
    evas_obj_name_set(WF_LAYOUT),
    elm_obj_layout_file_set(buf, WF_LAYOUT),
    evas_obj_visibility_set(EINA_TRUE)
  );
  result->e = evas_object_evas_get(result->layout);

//  result->bg = edje_object_part_object_get(elm_layout_edje_get(result->layout), WF_BACKGROUND);

  if (result->parent)
  {
    result->win = result->layout;
    elm_layout_signal_emit(result->layout, "isInternal", "isInternal");

    // Something to catch clicks on the background, for moving the window.
    // Coz Elm is uncooperative with this sort of thing, so we need to stick in a rectangle.
    obj = eo_add(EVAS_OBJ_RECTANGLE_CLASS, result->layout,
	evas_obj_size_hint_align_set(EVAS_HINT_FILL, EVAS_HINT_FILL),
	evas_obj_size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND),
	evas_obj_name_set(WF_UNDERLAY),
        evas_obj_color_set(0, 0, 0, 0),
	evas_obj_visibility_set(EINA_TRUE)
      );
    elm_object_part_content_set(result->layout, WF_UNDERLAY, obj);
    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_DOWN, _onBgClick, result);
    eo_unref(obj);

    // Create corner handles.
    snprintf(buf, sizeof(buf), "%s/pt.png", elm_app_data_dir_get());
    for (i = 0; i < 4; i++)
    {
      int cx = result->x, cy = result->y;

           if (i == 1)   cx += result->w;
      else if (i == 2)  {cx += result->w;  cy += result->h;}
      else if (i == 3)   cy += result->h;
      result->hand[i] = eo_add(EVAS_OBJ_IMAGE_CLASS, result->e,
	evas_obj_image_filled_set(EINA_TRUE),
	evas_obj_image_file_set(buf, NULL),
	evas_obj_size_set(31, 31),
	evas_obj_position_set(cx - 15, cy - 15),
	evas_obj_visibility_set(EINA_TRUE)
      );
      evas_object_event_callback_add(result->hand[i], EVAS_CALLBACK_MOUSE_MOVE, _onHandleMove, result);
      eo_unref(result->hand[i]);
    }

    result->title = eo_add(ELM_OBJ_LABEL_CLASS, result->layout,
	evas_obj_size_hint_align_set(EVAS_HINT_FILL, EVAS_HINT_FILL),
	evas_obj_size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND),
	evas_obj_visibility_set(EINA_TRUE)
      );
    elm_object_style_set(result->title, "marker");
    elm_object_text_set(result->title, title);
    elm_object_part_content_set(result->layout, WF_TITLE, result->title);
    eo_unref(result->title);
  }

  result->grid = eo_add(ELM_OBJ_GRID_CLASS, result->layout,
    evas_obj_size_hint_align_set(EVAS_HINT_FILL, EVAS_HINT_FILL),
    evas_obj_size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND),
    evas_obj_name_set(WF_SWALLOW),
    // TODO - Actually, this should be minus the size of the title stuff.
    elm_obj_grid_size_set(result->w, result->h),
    evas_obj_visibility_set(EINA_TRUE)
  );
  elm_object_part_content_set(result->layout, WF_SWALLOW, result->grid);

  elm_layout_sizing_eval(result->layout);

  if (result->parent)
  {
#if 0
    // EPysics enable the window.
    if (world)
    {
      result->body = ephysics_body_box_add(world);
      ephysics_body_evas_object_set(result->body, obj, EINA_TRUE);
      ephysics_body_mass_set(result->body, 0);
//      ephysics_body_restitution_set(result->body, 0.7);
//      ephysics_body_friction_set(result->body, 0);
//      ephysics_body_linear_velocity_set(result->body, 0, 0, 0);
//      ephysics_body_angular_velocity_set(result->body, 0, 0, 0);
      ephysics_body_sleeping_threshold_set(result->body, 0.1, 0.1);
    }
#endif

  }

  evas_object_resize(result->win, result->w, result->h);
  evas_object_show(result->win);
  winFangCalcMinSize(result);

  return result;
}

void winFangCalcMinSize(winFang *win)
{
  Evas_Object *edje = elm_layout_edje_get(win->layout);
  int w, h;

  // Would be nice if everything properly set their minimums, but they don't.
  //   Actually it looks like BOX gets it's minimum width set to the width of the window.
  win->mw = 0;  win->mh = 0;
  evas_object_size_hint_min_get(edje_object_part_object_get(edje, WF_BOX), &w, &h);
  if (w > win->mw)  win->mw = w;
  if (h > win->mh)  win->mh = h;
  // SWALLOW just returns 0.
  evas_object_size_hint_min_get(edje_object_part_object_get(edje, WF_SWALLOW), &w, &h);
  if (w > win->mw)  win->mw = w;
  if (h > win->mh)  win->mh = h;
  if (win->title)
  {
    // This at least returns proper values.
    evas_object_size_hint_min_get(win->title, &w, &h);
    if (w > win->mw)  win->mw = w;
    win->mh += h;
  }
}

void winFangDel(winFang *win)
{
  winFang *wf;
  Widget  *wid;

  if (!win)  return;

  if (win->bg)      eo_unref(win->bg);
  if (win->grid)    eo_unref(win->grid);
  if (win->layout)  eo_unref(win->layout);
  EINA_CLIST_FOR_EACH_ENTRY(wf, &win->winFangs, winFang, node)
  {
    winFangDel(wf);
  }

  // Elm will delete our widgets, but if we are using eo, we need to unref them.
  EINA_CLIST_FOR_EACH_ENTRY(wid, &win->widgets, Widget, node)
  {
    if (wid->on_del)  wid->on_del(wid, wid->obj, NULL);
    widgetDel(wid);
    eo_unref(wid->obj);
  }
  if (win->on_del)  win->on_del(win, win->win, NULL);
  evas_object_del(win->win);
}


static widgetSpec widgetClasses[15];

Widget *widgetAdd(winFang *win, char *type , char *title, int x, int y, int w, int h)
{
  Widget *result;
  const Eo_Class *klass = NULL;
  int i;

  // Poor mans introspection.
  if (NULL == widgetClasses[0].name)
  {
    i = 0;
    widgetClasses[i].name = WT_CHECK;	widgetClasses[i++].klass = 	ELM_OBJ_CHECK_CLASS;
    widgetClasses[i].name = WT_BOX;	widgetClasses[i++].klass = 	ELM_OBJ_BOX_CLASS;
    widgetClasses[i].name = WT_BUTTON;	widgetClasses[i++].klass = 	ELM_OBJ_BUTTON_CLASS;
    widgetClasses[i].name = WT_ENTRY;	widgetClasses[i++].klass = 	ELM_OBJ_ENTRY_CLASS;
    widgetClasses[i].name = WT_FILES;	widgetClasses[i++].klass = 	ELM_OBJ_FILESELECTOR_CLASS;
    widgetClasses[i].name = WT_GRID;	widgetClasses[i++].klass = 	ELM_OBJ_GRID_CLASS;
    widgetClasses[i].name = WT_HOVER;	widgetClasses[i++].klass = 	ELM_OBJ_HOVERSEL_CLASS;
    widgetClasses[i].name = WT_IMAGE;	widgetClasses[i++].klass = 	ELM_OBJ_IMAGE_CLASS;
    widgetClasses[i].name = WT_LABEL;	widgetClasses[i++].klass = 	ELM_OBJ_LABEL_CLASS;
    widgetClasses[i].name = WT_LAYOUT;	widgetClasses[i++].klass = 	ELM_OBJ_LAYOUT_CLASS;
    widgetClasses[i].name = WT_RADIO;	widgetClasses[i++].klass = 	ELM_OBJ_RADIO_CLASS;
    widgetClasses[i].name = WT_RECT;	widgetClasses[i++].klass = 	EVAS_OBJ_RECTANGLE_CLASS;
    widgetClasses[i].name = WT_TEXT;	widgetClasses[i++].klass = 	EVAS_OBJ_TEXT_CLASS;
    widgetClasses[i].name = WT_TEXTBOX; widgetClasses[i++].klass = 	ELM_OBJ_ENTRY_CLASS;
    widgetClasses[i].name = WT_TOOLBAR;	widgetClasses[i++].klass = 	ELM_OBJ_TOOLBAR_CLASS;
  }

  for (i = 0; i < ARRAY_LENGTH(widgetClasses); i++)
  {
    if (strcmp(type, widgetClasses[i].name) == 0)
    {
      klass = widgetClasses[i].klass;
      break;
    }
  }

  if (klass)
  {
    result = calloc(1, sizeof(Widget));
    strcpy(result->magic, "Widget");
    strcpy(result->type, type);
    eina_clist_add_head(&win->widgets, &result->node);
    result->win = win;

    result->obj = eo_add(klass, win->win,
      evas_obj_size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND),
      evas_obj_size_hint_align_set(EVAS_HINT_FILL, EVAS_HINT_FILL),
      evas_obj_visibility_set(EINA_TRUE),
      eo_key_data_set("Widget", result, NULL)
    );

    if (x < 0)
      elm_layout_box_append(win->win, WF_BOX, result->obj);
    else
      elm_grid_pack(win->grid, result->obj, x, y, w, h);
    winFangCalcMinSize(win);

    if (title)
    {
      result->label = strdup(title);
      elm_object_text_set(result->obj, result->label);
      evas_object_name_set(result->obj, title);
    }
  }

  return result;
}

void widgetDel(Widget *wid)
{
  if (wid)
  {
    // TODO - This is to work around a bug in Elm entry, remove it when the bug is fixed.
    //   The bug is that editable entry widgets cause the app to hang on exit.
    if (strcmp(WT_ENTRY, wid->type) == 0)
      elm_entry_editable_set(wid->obj, EINA_FALSE);
  }
}

/*  CALLBACK types

void edje_object_signal_callback_add(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func, void *data)
    typedef void(* Edje_Signal_Cb)(void *data, Evas_Object *obj, const char *emission, const char *source)
    called for signals sent from edje that match emission and source

void evas_object_event_callback_add(Evas_Object *obj, Evas_Callback_Type type, Evas_Object_Event_Cb func, const void *data)
    typedef void(* 	Evas_Object_Event_Cb )(void *data, Evas *e, Evas_Object *obj, void *event_info)
    no propogation

void evas_object_smart_callback_add(Evas_Object *obj, const char *event, Evas_Smart_Cb func, const void *data)
    typedef void(* 	Evas_Smart_Cb )(void *data, Evas_Object *obj, void *event_info)
    smart events on smart objects

void elm_object_signal_callback_add(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func, void *data)
    typedef void(* Edje_Signal_Cb)(void *data, Evas_Object *obj, const char *emission, const char *source)
    called for signals sent from obj that match emission and source

void elm_object_event_callback_add(Evas_Object *, Elm_Event_Cb, void *)
  typedef Eina_Bool(* Elm_Event_Cb)(void *data, Evas_Object *obj, Evas_Object *src, Evas_Callback_Type type, void *event_info)
  called for all input events
  Key up / down / mouse wheel events on a Elm widget
  propogate up through parents
    any focusable widget with this callback can deal with it
    and tell Elm to stop propagating the event.
  Seems to almost NEVER actually be called.  Pfft.

BUUUTT....

There's also specific callbacks that don't follow the above.

static void _resize_gl(Evas_Object *obj)
static void _draw_gl(Evas_Object *obj)

    elm_glview_resize_func_set(gld->elmGl, _resize_gl);
    elm_glview_render_func_set(gld->elmGl, (Elm_GLView_Func_Cb) _draw_gl);

And others no doubt.

ALSOOOO....

Ecore events.  lol


BTW.....

Elm has C&P / DND, but it's very limited.  So far only does Unix X text (plain, markup, html), images, and vcards.
Might as well imlpement it myself.
On the other hand, image is all I really need to fake it.
elm_cnp.h seems to be the only docs, not actually linked to the rest of Elm docs.

*/