summaryrefslogtreecommitdiff
path: root/main.c
blob: c2fb744ca89af8437b6e51c961f957cb756e4b16 (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
#include <eynote.h>

typedef struct {
   Elm_Win *win;
   Elm_Bg *bg;
   Eynote *note;
   Elm_Popup *popup;
} App_Private_Data;

static Eina_Bool
_saved_cb(void *data, Eo *obj EINA_UNUSED, const Eo_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
{
   App_Private_Data *pd = data;

   eo_del(pd->win);
   return true;
}

static void
_popup_save_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   App_Private_Data *pd = data;
   const char *file = NULL, *key;

   eo_del(pd->popup);
   if (!pd->note)
     eo_del(pd->win);

   eo_do(pd->note, efl_file_get(&file, &key));

   eo_do(pd->note, eo_event_callback_add(EYNOTE_EVENT_SAVED, _saved_cb, pd));
   if (file)
     eo_do(pd->note, efl_file_save(file,  key, NULL));
   else
     eo_do(pd->note, eynote_save_request());
}

static void
_popup_discard_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   App_Private_Data *pd = data;

   eo_del(pd->popup);
   eo_del(pd->win);
}

static void
_popup_cancel_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   App_Private_Data *pd = data;

   eo_del(pd->popup);
}

static void
_ask_save_popup(App_Private_Data *pd)
{
   Elm_Popup *popup;
   Evas_Object *btn;

   popup = elm_popup_add(pd->win);
   pd->popup = popup;

   elm_object_part_text_set(popup, "title,text", _("Au revoir!"));
   elm_object_text_set(popup, _("Do you want to save before leaving?"));

   btn = elm_button_add(popup);
   elm_object_text_set(btn, _("Save & Exit"));
   elm_object_part_content_set(popup, "button1", btn);
   evas_object_smart_callback_add(btn, "clicked", _popup_save_cb, pd);

   btn = elm_button_add(popup);
   elm_object_text_set(btn, _("Discard & Exit"));
   elm_object_part_content_set(popup, "button2", btn);
   evas_object_smart_callback_add(btn, "clicked", _popup_discard_cb, pd);

   btn = elm_button_add(popup);
   elm_object_text_set(btn, _("Cancel"));
   elm_object_part_content_set(popup, "button3", btn);
   evas_object_smart_callback_add(btn, "clicked", _popup_cancel_cb, pd);

   SX(btn);
   SX(popup);
}

static void
_key_up(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   Evas_Event_Key_Up *ev = event_info;
   App_Private_Data *pd = data;

   if (!strcmp(ev->keyname, KEY_F11))
     {
        elm_win_fullscreen_set(pd->win, !elm_win_fullscreen_get(pd->win));
        return;
     }
   else if (!strcmp(ev->key, KEY_ESC))
     {
        _ask_save_popup(pd);
     }
}

static Eina_Bool
_request_close(void *data, Eo *obj EINA_UNUSED, const Eo_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
{
   App_Private_Data *pd = data;

   _ask_save_popup(pd);

   return true;
}

int
elm_main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
{
   App_Private_Data pd;
   bool ok;

   elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);

   pd.win = elm_win_add(NULL, "eynote", ELM_WIN_BASIC);
   elm_win_title_set(pd.win, _("Eynote writer"));

   pd.bg = elm_bg_add(pd.win);
   elm_win_resize_object_add(pd.win, pd.bg);

   pd.note = eo_add(eynote_class_get(), pd.win);
   if (argc > 1)
     eo_do(pd.note, efl_file_set(argv[1], NULL));

   evas_object_resize(pd.win, 800, 600);
   elm_win_resize_object_add(pd.win, pd.note);
   elm_bg_file_set(pd.bg, eynote_theme(pd.note), EYNOTE_BG_LAYOUT);

   ok = true;
   ok &= evas_object_key_grab(pd.bg, KEY_F11, 0, ~0, true);
   //ok &= evas_object_key_grab(pd.bg, KEY_ESC, 0, ~0, false);
   if (!ok) ERR("Failed to grab some keys. No big deal.");
   evas_object_event_callback_add(pd.bg, EVAS_CALLBACK_KEY_UP, _key_up, &pd);

   eo_do(pd.win, eo_event_callback_add(ELM_WIN_EVENT_DELETE_REQUEST, _request_close, &pd));
   eo_do(pd.note, eo_event_callback_add(EYNOTE_EVENT_REQUEST_CLOSE, _request_close, &pd));

   SX(pd.note);
   SX(pd.bg);
   S(pd.win);
   elm_run();
   return 0;
}
ELM_MAIN()