File: data_entry_numeric.c

package info (click to toggle)
mlterm 3.9.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 25,340 kB
  • sloc: ansic: 154,713; sh: 5,302; cpp: 2,953; objc: 2,776; java: 2,472; makefile: 2,445; perl: 1,674; xml: 44
file content (104 lines) | stat: -rw-r--r-- 2,945 bytes parent folder | download
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
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */

#include <string.h> /* strdup/strchr/strrchr/memmove */
#include <stdlib.h> /* free/malloc */
#include "comm.h"
#include "data.h"
#include "data_entry_numeric.h"

void entry_numeric_reset(entry_t *entry) {
  /*
   * XXX
   * Initial value of "alpha" might be 0 which is the default value of
   * ui_picture_modifier_t::alpha but mlterm_set_value("alpha", "0")
   * unexpectedly changes alpha of true transparency on x composite
   * extension or win32.
   */
  if (strcmp(entry->key, "alpha") != 0) {
    entry_numeric_t *data = entry->data;
    mlterm_set_value(entry->key, data->initial);
  }
}

void entry_numeric_apply(entry_t *entry) {
  entry_numeric_t *data = entry->data;
  mlterm_set_value(entry->key, data->current);
}

entry_numeric_t *entry_numeric_new(const char *key, int max, int min, const char *unit) {
  entry_numeric_t *entry;
  char *tmp;

  tmp = mlterm_get_param(key);
  entry = malloc(sizeof(entry_numeric_t));
  if (tmp)
    entry->initial = atoi(tmp);
  else
    entry->initial = 0;
  entry->current = entry->initial;
  entry->unit = unit;
  entry->max = max;
  entry->min = min;
  return entry;
}

int entry_numeric_add(section_t *section, const char *name, const char *key, int min, int max,
                      const char *unit) {
  if (section->maxwidth < strlen(name)) section->maxwidth = strlen(name);
  section->entry[section->size].name = name;
  section->entry[section->size].key = key;
  section->entry[section->size].type = ENT_NUMERIC;
  section->entry[section->size].data = entry_numeric_new(key, max, min, unit);
  section->size++;
  return 0;
}

void entry_numeric_free_data(entry_t *entry) {
  free(entry->data);
  return;
}

void entry_numeric_display(window_t *window, entry_t *entry, int x, int y, int state) {
  entry_numeric_t *data;

  data = entry->data;
  display_numeric(window, x, y, data->current, data->unit, DC_NORMAL);
}

int entry_numeric_edit(window_t *window, entry_t *entry, int x, int y) {
  entry_numeric_t *data;
  int buffer;

  data = entry->data;
  buffer = read_one();
  switch (buffer) {
    case KEY_ESC:
    case 10:
      return -1;
    case KEY_UP:
      data->current *= 2;
      if ((data->max != -1) && (data->current > data->max)) data->current = data->max;
      entry->modified = 1;
      return 1; /* redraw */
    case KEY_DOWN:
      data->current /= 2;
      if ((data->min != -1) && (data->current < data->min)) data->current = data->min;
      entry->modified = 1;
      return 1; /* redraw */
    case KEY_RIGHT:
      if ((data->max == -1) || (data->current < data->max)) {
        data->current++;
        entry->modified = 1;
      }
      return 1; /* redraw */
    case KEY_LEFT:
      if ((data->min == -1) || (data->current > data->min)) {
        entry->modified = 1;
        data->current--;
      }
      return 1; /* redraw */
    default:
      /* ignore */
      return 0;
  }
}