summaryrefslogtreecommitdiff
path: root/src/main.c
blob: b1d5d817f5e3db321ec0c283ab00aa50c22aa05a (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
#include <Elementary.h>
#ifndef ELM_LIB_QUICKLAUNCH

#include "Equate.h"
#include "calc.h"

static Evas_Object *win;
static Evas_Object *entry;

static const struct
{
   const char *keyname;
   int op;
} _keymap[] = {
  {"KP_0",        NUM_0},
  {"0",           NUM_0},
  {"KP_1",        NUM_1},
  {"1",           NUM_1},
  {"KP_2",        NUM_2},
  {"2",           NUM_2},
  {"KP_3",        NUM_3},
  {"3",           NUM_3},
  {"KP_4",        NUM_4},
  {"4",           NUM_4},
  {"KP_5",        NUM_5},
  {"5",           NUM_5},
  {"KP_6",        NUM_6},
  {"6",           NUM_6},
  {"KP_7",        NUM_7},
  {"7",           NUM_7},
  {"KP_8",        NUM_8},
  {"8",           NUM_8},
  {"KP_9",        NUM_9},
  {"9",           NUM_9},
  {"KP_Divide",   OP_DIV},
  {"KP_Multiply", OP_MUT},
  {"KP_Subtract", OP_SUB},
  {"KP_Add",      OP_ADD},
  {"plus",        OP_ADD},
  {"minus",       OP_SUB},
  {"asterisk",    OP_MUT},
  {"slash",       OP_DIV},
  {"KP_Enter",    OP_EQU},
  {"Return",      OP_EQU},
  {"equal",       OP_EQU},
  {"BackSpace",   OP_CLR},
  {"Escape",      OP_CLR},
  {"period",      OP_DEC},
  {"KP_Decimal",  OP_DEC},
  {"comma",       OP_DEC},
  {"parenleft",   OP_OBRAK},
  {"parenright",  OP_CBRAK},
  NULL
};


static void
_calc_do(int op)
{
  double result;
  char   buf[BUFLEN];

  switch (op)
    {
    case EQ_EXIT:
      elm_exit();
      break;
    case OP_CLR:
      equate_clear();
      break;
    case OP_DIV:
      equate_append("/");
      break;
    case OP_MUT:
      equate_append("*");
      break;
    case OP_ADD:
      equate_append("+");
      break;
    case OP_SUB:
      equate_append("-");
      break;
    case OP_EQU:
      snprintf(buf, BUFLEN, "<title><align=right>%.10g</align></title>", equate_eval());
      elm_object_text_set(entry, buf);
      return;
      break;
    case OP_DEC:
      equate_append(".");
      break;
    case OP_OBRAK:
      equate_append("(");
      break;
    case OP_CBRAK:
      equate_append(")");
      break;
    case OP_SIN:
      equate_append("sin");
      break;
    case OP_COS:
      equate_append("cos");
      break;
    case OP_TAN:
      equate_append("tan");
      break;
    case OP_ROOT:
      equate_append("sqrt");
      break;
    case OP_POW:
      equate_append("^");
      break;
    case OP_LOG:
      equate_append("log");
      break;
    case OP_LN:
      equate_append("ln");
      break;
    case OP_EXP:
      equate_append("exp");
      break;
    case NUM_0:
      equate_append("0");
      break;
    case NUM_1:
      equate_append("1");
      break;
    case NUM_2:
      equate_append("2");
      break;
    case NUM_3:
      equate_append("3");
      break;
    case NUM_4:
      equate_append("4");
      break;
    case NUM_5:
      equate_append("5");
      break;
    case NUM_6:
      equate_append("6");
      break;
    case NUM_7:
      equate_append("7");
      break;
    case NUM_8:
      equate_append("8");
      break;
    case NUM_9:
      equate_append("9");
      break;
    default:
      E(1, "Unknown operator %d", op);
      break;
      /* etc */
    }
  snprintf(buf, BUFLEN, "<title><align=right>%s</align></title>", equate_string_get());
  elm_object_text_set(entry, buf);
}

static Eina_Bool
_key_down_cb(void *data, int type, void *event)
{
   Ecore_Event_Key *ev = event;
   int i;

   for (i = 0; _keymap[i].keyname; i++)
     {
        if (!strcmp(_keymap[i].keyname, ev->key))
          {
            _calc_do(_keymap[i].op);
          }
     }
   return EINA_TRUE;

}

static void
_bt_clicked(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
  int val = (int) data;
  _calc_do(val);
}

#define BUTTON_ADD(label, x, y, w, h, op)                               \
  bt = elm_button_add(tb);                                              \
  elm_object_text_set(bt, label);                                       \
  evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); \
  evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);  \
  evas_object_smart_callback_add(bt, "clicked", _bt_clicked, (void *)op); \
  elm_table_pack(tb, bt, x, y, w, h);                                   \
  evas_object_show(bt);                                                 \


static Evas_Object *
_gui_standard(Evas_Object *parent)
{
  Evas_Object *tb, *bt;
  tb = elm_table_add(win);
  elm_table_homogeneous_set(tb, EINA_TRUE);
  evas_object_size_hint_weight_set(tb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  evas_object_show(tb);

  BUTTON_ADD("C", 0, 0, 1, 1, OP_CLR);
  BUTTON_ADD("(", 1, 0, 1, 1, OP_CBRAK);
  BUTTON_ADD(")", 2, 0, 1, 1, OP_OBRAK);
  BUTTON_ADD("/", 3, 0, 1, 1, OP_DIV);

  BUTTON_ADD("7", 0, 1, 1, 1, NUM_7);
  BUTTON_ADD("8", 1, 1, 1, 1, NUM_8);
  BUTTON_ADD("9", 2, 1, 1, 1, NUM_9);
  BUTTON_ADD("x", 3, 1, 1, 1, OP_MUT);

  BUTTON_ADD("4", 0, 2, 1, 1, NUM_4);
  BUTTON_ADD("5", 1, 2, 1, 1, NUM_5);
  BUTTON_ADD("6", 2, 2, 1, 1, NUM_6);
  BUTTON_ADD("-", 3, 2, 1, 1, OP_SUB);

  BUTTON_ADD("1", 0, 3, 1, 1, NUM_1);
  BUTTON_ADD("2", 1, 3, 1, 1, NUM_2);
  BUTTON_ADD("3", 2, 3, 1, 1, NUM_3);
  BUTTON_ADD("+", 3, 3, 1, 1, OP_ADD);

  BUTTON_ADD("0", 0, 4, 2, 1, NUM_0);
  BUTTON_ADD(".", 2, 4, 1, 1, OP_DEC);
  BUTTON_ADD("=", 3, 4, 1, 1, OP_EQU);

  return tb;

}

static void
_create_gui(void)
{
   char buf[1024];
   Evas_Object *tb, *bx;

   win = elm_win_util_standard_add("equate", "Equate");
   elm_win_autodel_set(win, EINA_TRUE);

   bx = elm_box_add(win);
   evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   elm_win_resize_object_add(win, bx);
   evas_object_show(bx);

   entry = elm_entry_add(win);
   elm_entry_scrollable_set(entry, EINA_TRUE);
   evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, 0.0);
   evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, 0.5);
   elm_scroller_policy_set(entry, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_OFF);
   elm_object_text_set(entry, "<align=right><title>0</title></align>");
   elm_entry_single_line_set(entry, EINA_TRUE);
   elm_box_pack_end(bx, entry);
   evas_object_show(entry);

   tb = _gui_standard(win);
   evas_object_size_hint_weight_set(tb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   evas_object_size_hint_align_set(tb, EVAS_HINT_FILL, EVAS_HINT_FILL);
   elm_box_pack_end(bx, tb);

   ecore_event_handler_add (ECORE_EVENT_KEY_DOWN, _key_down_cb, NULL);

   evas_object_show(win);
   evas_object_resize(win, 240, 320);
}

EAPI_MAIN int
elm_main(int argc, char **argv)
{

   elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
   elm_app_compile_bin_dir_set(PACKAGE_BIN_DIR);
   elm_app_compile_data_dir_set(PACKAGE_DATA_DIR);
   elm_app_info_set(elm_main, "equate", "images/equate.png");

   math_init();
   _create_gui();
   elm_run();
   elm_shutdown();

   return 0;
}
#endif
ELM_MAIN()