summaryrefslogtreecommitdiff
path: root/src/mode_default.c
blob: 8324c95b0c530041b60128fb4b7dd6ae27e64f63 (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
/*
 *  LiberTI - TI-like calculator designed for LibreCalc
 *  Copyright (C) 2016 Delwink, LLC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, version 3 only.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>

#include "button.h"
#include "colors.h"
#include "font.h"
#include "keys.h"
#include "log.h"
#include "mode_default.h"
#include "tibchar.h"
#include "tibeval.h"
#include "util.h"

static SDL_Surface *
render_line (const struct tib_expr *line, unsigned int *width)
{
  int rc;
  char *s = get_expr_display_str (line);
  if (!s)
    {
      error ("Error converting expression to string");
      return NULL;
    }

  unsigned int len = strlen (s);
  SDL_Surface *parts[len + 1];
  parts[len] = get_font_char (' ');

  SDL_Rect pos;
  pos.x = 0;
  pos.y = 8;

  for (unsigned int i = 0; i < len; ++i)
    {
      SDL_Surface *part = get_font_char ((unsigned char) s[i]);

      pos.x += 6;
      if (pos.x >= 96)
        {
          pos.y += 8;
          pos.x = 6;
        }

      parts[i] = part;
    }

  SDL_Surface *final = SDL_CreateRGBSurface (0, 96, pos.y, 32, 0, 0, 0, 0);
  if (!final)
    {
      error ("Failed to initialize expression render surface: %s",
             SDL_GetError ());
      goto end;
    }

  SDL_LockSurface (final);

  rc = SDL_FillRect (final, NULL, SDL_MapRGB (final->format, 255, 255, 255));
  if (rc < 0)
    error ("Failed to set background of expression render surface: %s",
           SDL_GetError ());

  SDL_UnlockSurface (final);

  pos.x = 0;
  pos.y = 0;
  for (unsigned int i = 0; i <= len; ++i)
    {
      if (pos.x + 6 > 96)
        {
          pos.x = 0;
          pos.y += 8;
        }

      rc = SDL_BlitSurface (parts[i], NULL, final, &pos);
      if (rc < 0)
        {
          error ("Failed to blit expression portion: %s", SDL_GetError ());
          continue;
        }

      pos.x += 6;
    }

  *width = min (16, len) * 6;

 end:
  free (s);
  return final;
}

static void
draw_line (const struct tib_expr *line, SDL_Surface *final,
           unsigned int *height, bool right_align)
{
  unsigned int width;

  SDL_Surface *line_render = render_line (line, &width);
  if (line_render)
    {
      SDL_Rect pos;
      if (right_align)
        pos.x = 96 - width;
      else
        pos.x = 0;

      *height += line_render->h;
      pos.y = 64 - *height;

      int rc = SDL_BlitSurface (line_render, NULL, final, &pos);
      if (rc < 0)
        error ("Failed to draw line on screen frame: %s", SDL_GetError ());

      SDL_FreeSurface (line_render);
    }
}

static void
draw_cursor (const struct state *state, SDL_Surface *frame)
{
  if (!state->blink_state)
    return;

  SDL_Keymod mod = SDL_GetModState ();
  int c = 1;

  if (state->insert_mode)
    c += 4;

  if (STATE_2ND == state->action_state || mod & KMOD_CTRL)
    ++c;
  else if (STATE_ALPHA == state->action_state || mod & KMOD_SHIFT)
    c += 2;

  int x = 0;
  for (int i = 0; i < state->entry_cursor; ++i)
    {
      const char *special = display_special_char (state->entry.data[i]);
      if (special)
        x += strlen (special);
      else
        ++x;

      if (x >= 16)
        x %= 16;
    }

  SDL_Rect pos = { .x = (6 * x), .y = (64 - 8) };
  SDL_Surface *tile = get_font_char (c);

  int rc = SDL_BlitSurface (tile, NULL, frame, &pos);
  if (rc < 0)
    error ("Failed to draw cursor on screen frame: %s", SDL_GetError ());
}

SDL_Surface *
default_draw (const struct screen *screen)
{
  int rc;
  SDL_Surface *final;

  final = SDL_CreateRGBSurface (0, 96, 64, 32, 0, 0, 0, 0);
  if (!final)
    {
      error ("Failed to initialize screen frame: %s", SDL_GetError ());
      return NULL;
    }

  rc = SDL_FillRect (final, NULL, SDL_MapRGB (final->format, 255, 255, 255));
  if (rc < 0)
    error ("Failed to fill screen frame with white background: %s",
           SDL_GetError ());

  struct state *state = screen->state;
  unsigned int height = 0;
  draw_line (&state->entry, final, &height, false);
  draw_cursor (state, final);

  for (int i = state->history_len - 1; i >= 0 && height < 64; --i)
    {
      draw_line (&state->history[i].answer_string, final, &height, true);

      if (height < 64)
        draw_line (&state->history[i].entry, final, &height, false);
    }

  return final;
}

int
default_input (struct screen *screen, SDL_KeyboardEvent *key)
{
  struct state *state = screen->state;
  SDL_Keycode code = key->keysym.sym;
  SDL_Keymod mod = key->keysym.mod;

  switch (state->action_state)
    {
    case STATE_2ND:
      mod |= KMOD_LCTRL;
      break;

    case STATE_ALPHA:
      mod |= KMOD_LSHIFT;
      break;

    default:
      break;
    }

  switch (code)
    {
    case SDLK_BACKSPACE:
      if (!state->entry_cursor)
        return 0;

      --state->entry_cursor;
      // SPILLS OVER!
    case SDLK_DELETE:
      if (state->entry_cursor < state->entry.len)
        tib_expr_delete (&state->entry, state->entry_cursor);
      return 0;

    case SDLK_END:
      state->entry_cursor = state->entry.len;
      return 0;

    case SDLK_HOME:
      state->entry_cursor = 0;
      return 0;

    case SDLK_INSERT:
      state->insert_mode = !state->insert_mode;
      return 0;

    case SDLK_LEFT:
      entry_move_cursor (state, LEFT);
      return 0;

    case SDLK_RETURN:
    case SDLK_KP_ENTER:
      if (mod & KMOD_CTRL)
        {
          return entry_recall (state);
        }
      else
        {
          if (0 == state->entry.len)
            {
              int rc = entry_write (state, TIB_CHAR_ANS);
              if (rc)
                return rc;
            }

          return state_calc_entry (state);
        }

    case SDLK_RIGHT:
      entry_move_cursor (state, RIGHT);
      return 0;
    }

  int normal = normalize_keycode (code, mod);
  if (normal)
    {
      if (is_math_operator (normal) && 0 == state->entry.len)
        {
          int rc = entry_write (state, TIB_CHAR_ANS);
          if (rc)
            return rc;
        }

      return entry_write (state, normal);
    }

  return 0;
}