/
gui-keypad.c
237 lines (198 loc) · 4.98 KB
/
gui-keypad.c
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
/*
Copyright (C) 2003 Sridhar Ratnakumar <srid@nearfar.org>
This file is part of GNUSim8085.
GNUSim8085 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GNUSim8085 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUSim8085; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "gui-keypad.h"
#include "asm-id.h"
#include "gui-app.h"
#include "gui-input-symbol.h"
static GtkWidget *table;
#define TABLE_ROWS 21
#define TABLE_COLS 4
IdOpcode *p_opcode;
IdPseudo *p_pseudo;
/* Due to the overall unknown bug, I need this
Call this of you want to use the globals */
static void
validate_globals()
{
p_opcode = asm_id_get_opcode (); /* make sure */
p_pseudo = asm_id_get_pseudo ();
}
/* helper: return string containing registers that is used by opcode */
gchar *
get_register (IdOpcode * opcode)
{
gchar *ret;
gint tot;
gint i = 0;
gint index;
g_assert(opcode);
validate_globals();
if (!g_ascii_strcasecmp ("MOV", opcode->op_str))
{
ret = g_strdup ("ABCDEHLM");
return ret;
}
ret = g_malloc (15);
for (i = 0; i < 15; i++)
ret[i] = 0;
tot = asm_id_total_opcodes ();
index = -1;
for (i = 0; i < tot; i++)
{
if (opcode == &p_opcode[i])
{
index = i;
break;
}
}
g_assert (index != -1);
i = 0;
while (!g_ascii_strcasecmp (opcode->op_str, p_opcode[index].op_str))
{
ret[i++] = p_opcode[index].arg1[0];
index--;
}
ret[i] = '\0';
return ret;
}
/* callback for Opcode Button click */
static void
cb_clicked (GtkButton * button, gpointer user_data)
{
IdOpcode *opdata = (IdOpcode *) user_data;
GString *line_buf;
gchar *re, *re2;
gchar *newop;
g_assert (opdata);
line_buf = g_string_new ("");
newop = g_ascii_strdown (opdata->op_str, -1);
g_string_append_printf (line_buf, "%s ", newop);
g_free (newop);
if (opdata->num_args > 0)
{
re = get_register (opdata);
re2 = gui_input_reg (re);
if ( !re2 ) goto done;
newop = g_ascii_strdown (re2, -1);
g_string_append_printf (line_buf, " %s", newop);
g_free (newop);
g_free (re2);
if (opdata->num_args > 1)
{
re2 = gui_input_reg (re);
if ( !re2 ) goto done;
newop = g_ascii_strdown (re2, -1);
g_string_append_printf (line_buf, ", %s", newop);
g_free (newop);
g_free (re2);
}
g_free (re);
}
if (opdata->user_args)
{
re = gui_input_symbol ();
if ( !re ) goto done;
if (opdata->num_args > 0)
g_string_append_printf (line_buf, ", %s", re);
else
g_string_append_printf (line_buf, " %s", re);
g_free (re);
}
g_string_append (line_buf, "\n");
/* Insert into editor */
{
gint old_no = gui_editor_get_line (app->editor);
gui_editor_insert (app->editor, line_buf->str);
gui_editor_goto_line (app->editor, old_no + 1);
}
done:
/* set focus */
gui_editor_grab_focus (app->editor);
}
/* a generator for distinct opcodes, die after end */
IdOpcode *
get_next_opcode (void)
{
static int valid = 1;
static int next = 0;
gchar save[5];
IdOpcode *opcode, *prev = NULL;
g_assert (valid);
if (next == asm_id_total_opcodes ())
{
valid = 0;
return NULL;
}
opcode = &p_opcode[next++];
g_stpcpy (save, opcode->op_str);
while (next < asm_id_total_opcodes () + 1
&& !g_ascii_strcasecmp (save, opcode->op_str))
{
prev = opcode;
opcode = &p_opcode[next++];
}
next--;
return prev;
}
static void
cb_focus ()
{
/* set focus */
gui_editor_grab_focus (app->editor);
}
static void
create_me (void)
{
int i, j;
IdOpcode *opcode;
opcode = asm_id_get_opcode ();
/* Construct a table of cells */
table = gtk_table_new (TABLE_ROWS, TABLE_COLS, TRUE);
g_signal_connect (table, "focus", (GCallback) cb_focus, NULL);
p_opcode = asm_id_get_opcode ();
p_pseudo = asm_id_get_pseudo ();
for (i = 0; i < TABLE_ROWS; i++)
{
for (j = 0; j < TABLE_COLS; j++)
{
GtkWidget *button;
IdOpcode *op;
/* get opcode name */
op = get_next_opcode ();
if (!op)
goto breakout;
button = gtk_button_new_with_label (op->op_str);
gtk_widget_set_tooltip_text (button, op->op_desc);
g_signal_connect (button, "clicked",
(GCallback) cb_clicked, op);
gtk_table_attach_defaults (GTK_TABLE (table), button,
j, j + 1, i, i + 1);
}
}
breakout:
return;
}
void
gui_keypad_attach_me (void)
{
GtkWidget *cont;
cont = lookup_widget (app->window_main, "main_keypad_scroll");
g_assert (cont);
create_me ();
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (cont),
GTK_WIDGET (table));
gtk_widget_show_all (GTK_WIDGET (cont));
}