summaryrefslogtreecommitdiff
path: root/src/gadgets.c
blob: fc19602582b3015176baf9ca6b73c1c92f3131d2 (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
#include <e.h>

#include "gadgets.h"
#include "utils.h"
#include "elfe_config.h"

typedef struct _Elfe_Gadget Elfe_Gadget;

struct _Elfe_Gadget
{
   int desktop;
   Evas_Object *layout;
   E_Gadcon_Location *location;
   E_Gadcon *gc;
   Eina_Bool in_use[4][4]; /* FIXME change gor a dynamic and use eina_array instead */
};

static Evas_Object *
_item_add(Elfe_Gadget *egad, Elfe_Desktop_Item_Type type, const char *name, Evas_Object *child, int col, int row, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
{
   Elfe_Desktop_Config *dc;
   Elfe_Desktop_Item_Config *dic;
   Evas_Object *ly;

   if (egad->in_use[col][row] == EINA_TRUE)
     {
	printf("ERROR : already in use\n");
	return NULL;
     }

   ly = edje_object_add(evas_object_evas_get(egad->layout));

   switch (type)
     {
      case ELFE_DESKTOP_ITEM_APP:
	 edje_object_file_set(ly, elfe_home_cfg->theme, "elfe/desktop/app/frame");
	 edje_object_part_text_set(ly, "elfe.text.label", name);
	 break;
      case ELFE_DESKTOP_ITEM_GADGET:
	 edje_object_file_set(ly, elfe_home_cfg->theme, "elfe/desktop/gadget/frame");
	 break;
      default:
	 break;
     }


   edje_object_part_swallow(ly, "elfe.swallow.content", child);
   evas_object_show(child);
   evas_object_show(ly);

   e_layout_pack(egad->layout, ly);
   //evas_object_move(ly, x,  y);
   //evas_object_resize(ly, w, h);

   e_layout_child_move(ly, x,  y);
   e_layout_child_resize(ly, w, h);

   egad->in_use[col][row] = EINA_TRUE;

   return ly;
}



static int
_gadget_client_add(void *data, const E_Gadcon_Client_Class *cc)
{

   Elfe_Gadget *egad = data;
   E_Gadcon_Client *gcc = NULL;
   Evas_Coord ox, oy, ow, oh;
   int i,j;
   Evas_Coord x, y, w, h;
   Evas_Object *ly;

   /* FIXME cols/row must be a config value */
   evas_object_geometry_get(egad->layout, &ox, &oy, &ow, &oh);

   w = ow / 4;
   h = oh / 4;

   for (i = 0; i < 4; i++)
     for (j = 0; j < 4; j++)
       {
	  if (!egad->in_use[i][j])
	    {
	       x = i*w;
	       y = j*h;
	       gcc = cc->func.init(egad->gc, cc->name, "test", cc->default_style);
               gcc->cf = NULL;
               gcc->client_class = cc;
	       ly = _item_add(egad,  ELFE_DESKTOP_ITEM_GADGET, cc->name, gcc->o_base, i, j, x, y, w, h);
	       if (ly)
		 {
		 elfe_home_config_desktop_item_add(egad->desktop, ELFE_DESKTOP_ITEM_GADGET, i, j, x, y, w, h, cc->name);
		 return 1;
		 }
	       else
		 return 0;
	    }
       }

   return 0;
}

static void
_gadget_client_remove(void *data, E_Gadcon_Client *gcc)
{
}

void
_gadget_populate_class(void *data, E_Gadcon *gc, const E_Gadcon_Client_Class *cc)
{

}

typedef struct _App_Item App_Item;

struct _App_Item
{
    Efreet_Desktop *desktop;
    Elfe_Gadget *gadget;
};

static void*
_app_exec_cb(void *data, Efreet_Desktop *desktop, char *command, int remaining)
{
    ecore_exe_run(command, NULL);
}


static void
_app_mouse_click_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
{
    App_Item *app_item = data;

    efreet_desktop_command_get(app_item->desktop, NULL,
                               _app_exec_cb, NULL);
}

void
elfe_gadgets_app_add(Evas_Object *obj, Efreet_Menu *app, Evas_Coord x, Evas_Coord y)
{
   Elfe_Gadget *egad =  evas_object_data_get(obj, "elfe_gadget");
   Evas_Object *ic;
   Evas_Object *ly;
   App_Item *app_item;
   Evas_Coord ox, oy, ow, oh;
   Evas_Coord pos_x, pos_y;
   int m,n;
   Evas_Coord w, h;

   evas_object_geometry_get(egad->layout, &ox, &oy, &ow, &oh);
   w = ow / 4;
   h = oh / 4;

   m = (x/w) % 4;
   n = (y/h) % 4;

   pos_x = m*w;
   pos_y = n*h;

   ic = elfe_utils_fdo_icon_add(obj, app->icon, MIN(w,h));
   evas_object_show(ic);
   printf("App name : %s\n", app->name);
   ly = _item_add(egad, ELFE_DESKTOP_ITEM_APP, app->icon, ic, m, n, pos_x, pos_y, w, h);
   if (ly)
     {
     elfe_home_config_desktop_item_add(egad->desktop, ELFE_DESKTOP_ITEM_APP, m, n, pos_x, pos_y, w, h, app->desktop->orig_path);
     printf("Desktop path : %s\n", app->desktop->orig_path);
     }
   else
     return;

   app_item = calloc(1, sizeof(App_Item));
   app_item->gadget = egad;
   app_item->desktop = app->desktop;

   edje_object_signal_callback_add(ly, "elfe,action,click", "", _app_mouse_click_cb, app_item);
}

void
elfe_gadgets_gadget_add(Evas_Object *obj, E_Gadcon_Client_Class *gcc)
{
   Elfe_Gadget *egad = evas_object_data_get(obj, "elfe_gadget");

   _gadget_client_add(egad, gcc);
}

static void
_object_resize_cb(void *data , Evas *e , Evas_Object *obj, void *event_info )
{
   Elfe_Gadget *egad = data;
   Evas_Coord x, y, w, h;


   evas_object_geometry_get(egad->layout, &x, &y, &w, &h);
   e_layout_virtual_size_set(egad->layout, w, h);
   printf("%d %d %d %d\n", x, y, w, h);



   evas_object_show(egad->layout);
}


Evas_Object *
elfe_gadgets_zone_add(Evas_Object *parent, E_Zone *zone, int desktop, const char *desktop_name)
{
   const char *location_name;
   Elfe_Gadget *egad;
   int i,j;
   Elfe_Desktop_Config *dc;
   Elfe_Desktop_Item_Config *dic;
   Eina_List *l;

   egad = calloc(1, sizeof(Elfe_Gadget));
   if (!egad)
     return NULL;

   egad->layout = e_layout_add(evas_object_evas_get(parent));
   evas_object_data_set(egad->layout, "elfe_gadget", egad);
   evas_object_event_callback_add(egad->layout, EVAS_CALLBACK_RESIZE,
				  _object_resize_cb, egad);

   //e_layout_virtual_size_set(egad->layout, 480, 480);

   egad->gc = E_OBJECT_ALLOC(E_Gadcon, E_GADCON_TYPE, NULL);
   if (!egad->gc)
     {
	evas_object_del(egad->layout);
	free(egad);
	return NULL;
     }

   /* FIXME already done by calloc ? */
   for (i = 0; i < 4; i++)
     for (j = 0; j < 4; j++)
       egad->in_use[i][j] = EINA_FALSE;


   location_name = eina_stringshare_printf("Elfe %s\n", desktop_name);
   egad->desktop = desktop;
   egad->location = e_gadcon_location_new (location_name, E_GADCON_SITE_DESKTOP,
					   _gadget_client_add, egad,
					   _gadget_client_remove, NULL);
   eina_stringshare_del(location_name);
   e_gadcon_location_set_icon_name(egad->location, "preferences-desktop");
   e_gadcon_location_register(egad->location);

   egad->gc->name = eina_stringshare_add(location_name);
   egad->gc->layout_policy = E_GADCON_LAYOUT_POLICY_PANEL;
   egad->gc->orient = E_GADCON_ORIENT_FLOAT;
   egad->gc->location = egad->location;
   egad->gc->evas = evas_object_evas_get(parent);
   e_gadcon_populate_callback_set(egad->gc, _gadget_populate_class, egad->gc);
   egad->gc->id = 0;
   egad->gc->edje.o_parent = NULL;
   egad->gc->edje.swallow_name = NULL;
   egad->gc->shelf = NULL;
   egad->gc->toolbar = NULL;
   egad->gc->editing = 0;
   egad->gc->o_container = NULL;
   egad->gc->frame_request.func = NULL;
   egad->gc->resize_request.func = NULL;
   egad->gc->min_size_request.func = NULL;
   e_gadcon_zone_set(egad->gc, zone);
   e_gadcon_custom_new(egad->gc);

   /* Fill zone with config items*/
  dc = eina_list_nth(elfe_home_cfg->desktops, egad->desktop);
   EINA_LIST_FOREACH(dc->items, l, dic)
     {
	Evas_Object *child = NULL;
	Efreet_Desktop *desktop;

	switch (dic->type)
	  {
	   case ELFE_DESKTOP_ITEM_APP:
	     {
		Evas_Object *ly;
		App_Item *app_item;

		desktop = efreet_desktop_get(dic->name);

		child = elfe_utils_fdo_icon_add(egad->layout, desktop->icon, MIN(dic->size_w,dic->size_h));
		evas_object_show(child);


		ly = _item_add(egad, dic->type, desktop->name,
			       child, dic->col, dic->row,
			       dic->pos_x, dic->pos_y,
			       dic->size_w, dic->size_h);

		app_item = calloc(1, sizeof(App_Item));
		app_item->gadget = egad;
		app_item->desktop = desktop;

		edje_object_signal_callback_add(ly, "elfe,action,click", "", _app_mouse_click_cb, app_item);

		break;
	     }
	   case ELFE_DESKTOP_ITEM_GADGET:
	      break;
	   default:
	      break;
	  }

	printf("%p %d %d %d %d %d %d\n", child, dic->col, dic->row,
		  dic->pos_x, dic->pos_y,
		  dic->size_w, dic->size_h);


     }


   return egad->layout;
}