summaryrefslogtreecommitdiff
path: root/src/bin/e_config_dialog.c
blob: b75f139436bb4a4ab48311af64be262b8ae680f1 (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
#include "e.h"

/* local subsystem functions */
static void      _e_config_dialog_free(E_Config_Dialog *cfd);
static void      _e_config_dialog_go(E_Config_Dialog *cfd, E_Config_Dialog_CFData_Type type);
static Eina_Bool _e_config_dialog_cb_auto_apply_timer(void *data);
static void      _e_config_dialog_cb_dialog_del(void *obj);
static void      _e_config_dialog_cb_ok(void *data, E_Dialog *dia);
static void      _e_config_dialog_cb_apply(void *data, E_Dialog *dia);
static void      _e_config_dialog_cb_advanced(void *data, void *data2);
static void      _e_config_dialog_cb_basic(void *data, void *data2);
static int       _e_config_dialog_check_changed(E_Config_Dialog *cfd, unsigned char def);
static void      _e_config_dialog_cb_changed(void *data, Evas_Object *obj);
static void      _e_config_dialog_cb_close(void *data, E_Dialog *dia);

/* local subsystem globals */
static Eina_List *_e_config_dialog_list = NULL;

/* externally accessible functions */

/**
 * Creates a new dialog
 *
 * @param parent the parent dialog
 * @param title to display for the dialog
 * @param name the name used to register the window in e
 * @param class the call used to register the window in e
 * @param icon the path to the icon file
 * @param icon_size is of the width and height of the icon
 * @param view the callbacks used to create the dialog and save the settings
 * @param data additional data to attach to the dialog, will be passed to the callbacks
 * @return returns the created dialog. Null on failure
 */
EAPI E_Config_Dialog *
e_config_dialog_new(Evas_Object *parent, const char *title, const char *name, const char *class, const char *icon, int icon_size, E_Config_Dialog_View *view, void *data)
{
   E_Config_Dialog *cfd;

   cfd = E_OBJECT_ALLOC(E_Config_Dialog, E_CONFIG_DIALOG_TYPE,
                        _e_config_dialog_free);
   cfd->view = view;
   cfd->parent = parent;
   cfd->title = eina_stringshare_add(title);
   cfd->name = eina_stringshare_add(name);
   cfd->class = eina_stringshare_add(class);
   if (icon)
     {
        cfd->icon = eina_stringshare_add(icon);
        cfd->icon_size = icon_size;
     }
   cfd->data = data;
   cfd->hide_buttons = 1;
   cfd->cfg_changed = 0;
   cfd->cfg_changed_auto = 1;

   if (cfd->view->override_auto_apply)
     {
        /* Dialog Requested To Not Auto-Apply */
        if ((cfd->view->basic.apply_cfdata) ||
            (cfd->view->advanced.apply_cfdata))
          cfd->hide_buttons = 0;
     }
   else
     {
        /* Ok To Override, Or Not Specified. Use Config Value */
        if (e_config->cfgdlg_auto_apply)
          cfd->hide_buttons = 1;
        else
          {
             if ((cfd->view->basic.apply_cfdata) ||
                 (cfd->view->advanced.apply_cfdata))
               cfd->hide_buttons = 0;
          }
     }

   switch (e_config->cfgdlg_default_mode)
     {
      case E_CONFIG_DIALOG_CFDATA_TYPE_BASIC:
        if (cfd->view->basic.create_widgets)
          _e_config_dialog_go(cfd, E_CONFIG_DIALOG_CFDATA_TYPE_BASIC);
        break;

      case E_CONFIG_DIALOG_CFDATA_TYPE_ADVANCED:
        if (cfd->view->advanced.create_widgets)
          _e_config_dialog_go(cfd, E_CONFIG_DIALOG_CFDATA_TYPE_ADVANCED);
        else if (cfd->view->basic.create_widgets)
          _e_config_dialog_go(cfd, E_CONFIG_DIALOG_CFDATA_TYPE_BASIC);
        break;
     }
   _e_config_dialog_list = eina_list_append(_e_config_dialog_list, cfd);

   return cfd;
}

EAPI int
e_config_dialog_find(const char *name, const char *class)
{
   Eina_List *l;
   E_Config_Dialog *cfd;

   EINA_LIST_FOREACH(_e_config_dialog_list, l, cfd)
     {
        if ((!e_util_strcmp(name, cfd->name)) &&
            (!e_util_strcmp(class, cfd->class)))
          {
             E_Zone *z;
             E_Client *ec;

             z = e_zone_current_get();
             ec = e_win_client_get(cfd->dia->win);
             e_client_uniconify(ec);
             elm_win_raise(cfd->dia->win);
             e_client_desk_set(ec, e_desk_current_get(z));
             if (ec->shaded || ec->shading)
               e_client_unshade(ec, ec->shade_dir);
             if ((e_config->focus_setting == E_FOCUS_NEW_DIALOG) ||
                 (e_config->focus_setting == E_FOCUS_NEW_WINDOW))
               evas_object_focus_set(ec->frame, 1);
             return 1;
          }
     }
   return 0;
}

EAPI E_Config_Dialog *
e_config_dialog_get(const char *name, const char *class)
{
   Eina_List *l;
   E_Config_Dialog *cfd;

   EINA_LIST_FOREACH(_e_config_dialog_list, l, cfd)
     {
        if (!cfd) continue;
        if ((!e_util_strcmp(name, cfd->name)) &&
            (!e_util_strcmp(class, cfd->class)))
          {
             return cfd;
          }
     }
   return NULL;
}

/* local subsystem functions */
static void
_e_config_dialog_free(E_Config_Dialog *cfd)
{
   _e_config_dialog_list = eina_list_remove(_e_config_dialog_list, cfd);
   if (cfd->auto_apply_timer) _e_config_dialog_cb_auto_apply_timer(cfd);
   if (cfd->title) eina_stringshare_del(cfd->title);
   if (cfd->name) eina_stringshare_del(cfd->name);
   if (cfd->class) eina_stringshare_del(cfd->class);
   if (cfd->icon) eina_stringshare_del(cfd->icon);
   if (cfd->view->free_cfdata)
     {
        cfd->view->free_cfdata(cfd, cfd->cfdata);
        cfd->cfdata = NULL;
     }
   if (cfd->dia)
     {
        e_object_del_attach_func_set(E_OBJECT(cfd->dia), NULL);
        e_object_del(E_OBJECT(cfd->dia));
        cfd->dia = NULL;
     }
   E_FREE(cfd->view);
   E_FREE(cfd);
}

static void
_e_config_dialog_go(E_Config_Dialog *cfd, E_Config_Dialog_CFData_Type type)
{
   Evas *evas;
   E_Dialog *pdia;
   Evas_Object *o, *ob, *sf;
   char buf[256];
   int mw = -1, mh = -1;
   void *cfdata;

   pdia = cfd->dia;
   /* FIXME: get name/class form new call and use here */
   /* if (type == E_CONFIG_DIALOG_CFDATA_TYPE_BASIC)
    *   snprintf(buf, sizeof(buf), "%s...%s", cfd->class, "BASIC");
    * else
    *   snprintf(buf, sizeof(buf), "%s...%s", cfd->class, "ADVANCED"); */
   snprintf(buf, sizeof(buf), "_config::%s", cfd->class);

   if (!pdia)  /* creating window for the first time */
     {
        if ((cfd->view->normal_win) || (e_config->cfgdlg_normal_wins))
          cfd->dia = e_dialog_normal_win_new(cfd->parent, cfd->name, buf);
        else
          cfd->dia = e_dialog_new(cfd->parent, cfd->name, buf);
        e_object_del_attach_func_set(E_OBJECT(cfd->dia),
                                     _e_config_dialog_cb_dialog_del);
     } /* window was created before - deleting content only */
   else if (cfd->dia->content_object)
     evas_object_del(cfd->dia->content_object);

   cfd->view_type = type;
   cfd->dia->data = cfd;
   e_dialog_title_set(cfd->dia, cfd->title);
   cfdata = cfd->cfdata;
   if (cfd->view->create_cfdata && (!cfd->cfdata)) 
     cfd->cfdata = cfd->view->create_cfdata(cfd);

   evas = evas_object_evas_get(cfd->dia->win);
   if (type == E_CONFIG_DIALOG_CFDATA_TYPE_BASIC)
     {
        if (cfd->view->advanced.create_widgets)
          {
             o = e_widget_list_add(evas, 0, 0);
             ob = cfd->view->basic.create_widgets(cfd, evas, cfd->cfdata);
             if (cfd->view->scroll)
               {
                  e_widget_size_min_resize(ob);
                  sf = e_widget_scrollframe_simple_add(evas, ob);
                  e_widget_list_object_append(o, sf, 1, 1, 0.0);
               }
             else
               e_widget_list_object_append(o, ob, 1, 1, 0.0);
             ob = e_widget_button_add(evas,
                                      _("Advanced"), "go-next",
                                      _e_config_dialog_cb_advanced, cfd, NULL);
             e_widget_list_object_append(o, ob, 0, 0, 1.0);
          }
        else
          {
             o = cfd->view->basic.create_widgets(cfd, evas, cfd->cfdata);
             if (cfd->view->scroll)
               {
                  e_widget_size_min_resize(o);
                  o = e_widget_scrollframe_simple_add(evas, o);
               }
          }
     }
   else
     {
        if (cfd->view->basic.create_widgets)
          {
             o = e_widget_list_add(evas, 0, 0);
             ob = cfd->view->advanced.create_widgets(cfd, evas, cfd->cfdata);
             if (cfd->view->scroll)
               {
                  e_widget_size_min_resize(ob);
                  sf = e_widget_scrollframe_simple_add(evas, ob);
                  e_widget_list_object_append(o, sf, 1, 1, 0.0);
               }
             else
               e_widget_list_object_append(o, ob, 1, 1, 0.0);
             ob = e_widget_button_add(evas,
                                      _("Basic"), "go-next",
                                      _e_config_dialog_cb_basic, cfd, NULL);
             e_widget_list_object_append(o, ob, 0, 0, 1.0);
          }
        else
          {
             o = cfd->view->advanced.create_widgets(cfd, evas, cfd->cfdata);
             if (cfd->view->scroll)
               {
                  e_widget_size_min_resize(o);
                  o = e_widget_scrollframe_simple_add(evas, o);
               }
          }
     }

   e_widget_on_change_hook_set(o, _e_config_dialog_cb_changed, cfd);
   e_widget_size_min_get(o, &mw, &mh);
   e_dialog_content_set(cfd->dia, o, mw, mh);

   if (!pdia) /* dialog window was created in this function call - need to create buttons once */
     {
        if (!cfd->hide_buttons)
          {
             e_dialog_button_add(cfd->dia, _("OK"), NULL,
                                 _e_config_dialog_cb_ok, cfd);
             e_dialog_button_add(cfd->dia, _("Apply"), NULL,
                                 _e_config_dialog_cb_apply, cfd);
             if (!cfd->cfg_changed)
               {
                  e_dialog_button_disable_num_set(cfd->dia, 0, 1);
                  e_dialog_button_disable_num_set(cfd->dia, 1, 1);
               }
          }
        e_dialog_button_add(cfd->dia, _("Close"), NULL,
                            _e_config_dialog_cb_close, cfd);
     }

   if (cfdata && cfd->cfg_changed_auto)
     {
        int changed;

        changed = _e_config_dialog_check_changed(cfd, 0);
        e_config_dialog_changed_set(cfd, changed);
     }

   e_dialog_show(cfd->dia);
   if (cfd->icon) e_dialog_border_icon_set(cfd->dia, cfd->icon);
}

static Eina_Bool
_e_config_dialog_cb_auto_apply_timer(void *data)
{
   E_Config_Dialog *cfd;

   cfd = data;

   if (cfd->auto_apply_timer) ecore_timer_del(cfd->auto_apply_timer);
   cfd->auto_apply_timer = NULL;

   if (cfd->view_type == E_CONFIG_DIALOG_CFDATA_TYPE_BASIC)
     {
        if (cfd->view->basic.apply_cfdata)
          cfd->view->basic.apply_cfdata(cfd, cfd->cfdata);
     }
   else
     {
        if (cfd->view->advanced.apply_cfdata)
          cfd->view->advanced.apply_cfdata(cfd, cfd->cfdata);
     }
   return ECORE_CALLBACK_CANCEL;
}

static void
_e_config_dialog_cb_dialog_del(void *obj)
{
   E_Dialog *dia;
   E_Config_Dialog *cfd;

   dia = obj;
   cfd = dia->data;
   if (cfd->auto_apply_timer) _e_config_dialog_cb_auto_apply_timer(cfd);
   cfd->dia = NULL;
   e_object_del(E_OBJECT(cfd));
}

static void
_e_config_dialog_cb_ok(void *data __UNUSED__, E_Dialog *dia)
{
   E_Config_Dialog *cfd;
   int ok = 0;

   cfd = dia->data;
   if (cfd->view_type == E_CONFIG_DIALOG_CFDATA_TYPE_BASIC)
     {
        if (cfd->view->basic.apply_cfdata)
          ok = cfd->view->basic.apply_cfdata(cfd, cfd->cfdata);
     }
   else
     {
        if (cfd->view->advanced.apply_cfdata)
          ok = cfd->view->advanced.apply_cfdata(cfd, cfd->cfdata);
     }
   if (ok)
     e_util_defer_object_del(E_OBJECT(cfd));
}

static void
_e_config_dialog_cb_apply(void *data __UNUSED__, E_Dialog *dia)
{
   E_Config_Dialog *cfd;
   int ok = 0;

   cfd = dia->data;
   if (cfd->view_type == E_CONFIG_DIALOG_CFDATA_TYPE_BASIC)
     {
        if (cfd->view->basic.apply_cfdata)
          ok = cfd->view->basic.apply_cfdata(cfd, cfd->cfdata);
     }
   else
     {
        if (cfd->view->advanced.apply_cfdata)
          ok = cfd->view->advanced.apply_cfdata(cfd, cfd->cfdata);
     }
   if ((ok) && (!cfd->hide_buttons))
     {
        cfd->cfg_changed = 0;
        e_dialog_button_disable_num_set(cfd->dia, 0, 1);
        e_dialog_button_disable_num_set(cfd->dia, 1, 1);
     }
}

static void
_e_config_dialog_cb_advanced(void *data, void *data2 __UNUSED__)
{
   E_Config_Dialog *cfd;

   cfd = data;
   if (cfd->auto_apply_timer) _e_config_dialog_cb_auto_apply_timer(cfd);
   _e_config_dialog_go(cfd, E_CONFIG_DIALOG_CFDATA_TYPE_ADVANCED);
}

static void
_e_config_dialog_cb_basic(void *data, void *data2 __UNUSED__)
{
   E_Config_Dialog *cfd;

   cfd = data;
   if (cfd->auto_apply_timer) _e_config_dialog_cb_auto_apply_timer(cfd);
   _e_config_dialog_go(cfd, E_CONFIG_DIALOG_CFDATA_TYPE_BASIC);
}

static void
_e_config_dialog_changed(E_Config_Dialog *cfd)
{
   if (!cfd->hide_buttons)
     {
        cfd->cfg_changed = 1;
        e_dialog_button_disable_num_set(cfd->dia, 0, 0);
        e_dialog_button_disable_num_set(cfd->dia, 1, 0);
     }
   else
     {
        if (cfd->auto_apply_timer) ecore_timer_del(cfd->auto_apply_timer);
        cfd->auto_apply_timer = NULL;
        cfd->auto_apply_timer =
          ecore_timer_add(0.5, _e_config_dialog_cb_auto_apply_timer, cfd);
     }
}

static void
_e_config_dialog_unchanged(E_Config_Dialog *cfd)
{
   if (!cfd->hide_buttons)
     {
        e_dialog_button_disable_num_set(cfd->dia, 0, 1);
        e_dialog_button_disable_num_set(cfd->dia, 1, 1);
     }
   else
     {
        if (cfd->auto_apply_timer)
          {
             ecore_timer_del(cfd->auto_apply_timer);
             cfd->auto_apply_timer = NULL;
          }
     }
}

static int
_e_config_dialog_check_changed(E_Config_Dialog *cfd, unsigned char def)
{
   int changed = 0;

   if (cfd->view_type == E_CONFIG_DIALOG_CFDATA_TYPE_BASIC)
     {
        if (cfd->view->basic.check_changed)
          changed = cfd->view->basic.check_changed(cfd, cfd->cfdata);
        else
          changed = def;
     }
   else if (cfd->view_type == E_CONFIG_DIALOG_CFDATA_TYPE_ADVANCED)
     {
        if (cfd->view->advanced.check_changed)
          changed = cfd->view->advanced.check_changed(cfd, cfd->cfdata);
        else
          changed = def;
     }

   return changed;
}

static void
_e_config_dialog_cb_changed(void *data, Evas_Object *obj __UNUSED__)
{
   E_Config_Dialog *cfd;
   int changed;

   cfd = data;
   if (!cfd->cfg_changed_auto) return;
   changed = _e_config_dialog_check_changed(cfd, 1);

   e_config_dialog_changed_set(cfd, changed);
}

static void
_e_config_dialog_cb_close(void *data __UNUSED__, E_Dialog *dia)
{
   E_Config_Dialog *cfd;
   int ok = 1;

   cfd = dia->data;
   if (cfd->auto_apply_timer) _e_config_dialog_cb_auto_apply_timer(cfd);
   if (cfd->view->close_cfdata)
     ok = cfd->view->close_cfdata(cfd, cfd->cfdata);
   if (ok) e_util_defer_object_del(E_OBJECT(cfd));
}

EAPI void
e_config_dialog_changed_auto_set(E_Config_Dialog *cfd, unsigned char value)
{
   if (!cfd) return;
   cfd->cfg_changed_auto = !!value;
}

EAPI void
e_config_dialog_changed_set(E_Config_Dialog *cfd, unsigned char value)
{
   if (!cfd) return;
   cfd->cfg_changed = !!value;
   if (cfd->cfg_changed)
     _e_config_dialog_changed(cfd);
   else
     _e_config_dialog_unchanged(cfd);
}