atusb: Delete temporary file
[linux-libre-firmware.git] / as31 / as31 / as31_gtk.c
1 /* ----------------------------------------------------------------------
2  * FILE: main.c
3  * PACKAGE: as31 - 8031/8051 Assembler.
4  *
5  * DESCRIPTION:
6  *      The file contains main(). It handles the arguments and makes
7  *      sure that pass 1 is done before pass 2 etc...
8  *
9  * REVISION HISTORY:
10  *      Jan. 19, 1990 - Created. (Ken Stauffer)
11  *
12  * AUTHOR:
13  *      All code in this file written by Ken Stauffer (University of Calgary).
14  *      January, 1990. "Written by: Ken Stauffer"
15  *
16  *      April, 2000, Paul Stoffregen: see Makefile for details
17  */
18
19 #include <stdio.h>
20 #include <setjmp.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <gtk/gtk.h>
25
26 #include "as31.h"
27
28
29 void do_assemble(GtkWidget *widget, gpointer *data);
30 gint delete_handler(GtkWidget *widget, gpointer *data);
31
32
33 /* make all the GTK widgets global, instead of passing lots */
34 /* of pointers into the callbacks, through the parser, etc */
35
36 GtkWidget *file_label, *file_entry, *otype_combo;
37 GtkWidget *list_check, *go_button;
38 GtkWidget *otype_label, *output_text;
39 GtkWidget *main_window, *main_vbox;
40 GtkWidget *top_hbox, *middle_hbox, *bottom_hbox;
41
42
43 int main(int argc, char **argv)
44 {
45         GList *otypelist;
46         const char *desc;
47         int n;
48
49         gtk_init(&argc, &argv);
50
51         /* create all the gtk widgets... it's a pain to type in all */
52         /* this stuff, maybe someday I'll have to learn about glade */
53
54         file_label = gtk_label_new("ASM File: ");
55         gtk_label_set_justify(GTK_LABEL(file_label), GTK_JUSTIFY_RIGHT);
56         gtk_widget_show(file_label);
57
58         otype_label = gtk_label_new("Output Format");
59         gtk_label_set_justify(GTK_LABEL(otype_label), GTK_JUSTIFY_RIGHT);
60         gtk_widget_show(otype_label);
61
62         file_entry = gtk_entry_new();
63         /* fill in a default filename, from a previous session ??? */
64         gtk_widget_show(file_entry);
65
66         list_check = gtk_check_button_new_with_label("Create List File");
67         /* check or unchecked based on previous usage */
68         gtk_widget_show(list_check);
69
70         otype_combo = gtk_combo_new();
71         otypelist = NULL;
72         n = 0;
73         while ((desc = emit_desc_lookup(n++)) != NULL) {
74                 otypelist = g_list_append(otypelist, strdup(desc));
75         }
76         gtk_combo_set_popdown_strings(GTK_COMBO(otype_combo), otypelist);
77         gtk_entry_set_editable(GTK_ENTRY(GTK_COMBO(otype_combo)->entry),
78                 FALSE);
79         /* set to same output format as a previous run ?? */
80         gtk_widget_show(otype_combo);
81
82         go_button = gtk_button_new_with_label("Assemble");
83         gtk_widget_show(go_button);
84
85         output_text = gtk_text_new(NULL, NULL);
86         gtk_text_set_editable(GTK_TEXT(output_text), FALSE);
87         gtk_widget_show(output_text);
88
89         top_hbox = gtk_hbox_new(FALSE, 2);
90         gtk_box_pack_start(GTK_BOX(top_hbox), file_label, FALSE, FALSE, 2);
91         gtk_box_pack_start(GTK_BOX(top_hbox), file_entry, TRUE, TRUE, 2);
92         gtk_widget_show(top_hbox);
93
94         middle_hbox = gtk_hbox_new(FALSE, 2);
95         gtk_box_pack_start(GTK_BOX(middle_hbox), otype_label, FALSE, FALSE, 2);
96         gtk_box_pack_start(GTK_BOX(middle_hbox), otype_combo, FALSE, FALSE, 2);
97         gtk_widget_show(middle_hbox);
98
99         bottom_hbox = gtk_hbox_new(FALSE, 2);
100         gtk_box_pack_start(GTK_BOX(bottom_hbox), go_button, FALSE, FALSE, 2);
101         gtk_box_pack_start(GTK_BOX(bottom_hbox), list_check, FALSE, FALSE, 2);
102         gtk_widget_show(bottom_hbox);
103
104         main_vbox = gtk_vbox_new(FALSE, 2);
105         gtk_box_pack_start(GTK_BOX(main_vbox), top_hbox, FALSE, FALSE, 2);
106         gtk_box_pack_start(GTK_BOX(main_vbox), middle_hbox, FALSE, FALSE, 2);
107         gtk_box_pack_start(GTK_BOX(main_vbox), bottom_hbox, FALSE, FALSE, 2);
108         gtk_box_pack_start(GTK_BOX(main_vbox), output_text, TRUE, TRUE, 2);
109         gtk_widget_show(main_vbox);
110
111         main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
112         gtk_container_add(GTK_CONTAINER(main_window), main_vbox);
113         gtk_widget_show(main_window);
114
115         /* attach signal handlers so we can actually do something */
116
117         gtk_signal_connect(GTK_OBJECT(main_window), "delete_event",
118                 GTK_SIGNAL_FUNC(delete_handler), NULL);
119
120         gtk_signal_connect(GTK_OBJECT(go_button), "pressed",
121                 GTK_SIGNAL_FUNC(do_assemble), NULL);
122
123         /* that's it, everything past here is a callback from GTK */
124
125         mesg("WARNING: this is an alpha-quality release.\n");
126         mesg("It WILL crash the second time you assemble !!\n");
127         mesg("Please report any problems that occur on the\n");
128         mesg("first assembly to: paul@pjrc.com\n");
129
130         gtk_main();
131         return 0;
132 }
133
134
135 /* actually do the assembly.  The entire assembly is done from */
136 /* start to finish in this callback attached to the "assemble now" */
137 /* button.  It's not so nice to spend a lot of CPU time in a */
138 /* callback, because all the gtk widgets stop responding until */
139 /* we're done, but it keeps things simple, and at least the */
140 /* window manager takes care of raise/lower and reposition, */
141 /* ...unlike ms windoze where those are handled by the */
142 /* application's event loop.  It would be "nice" to launch a */
143 /* thread that would do the assembly, but for a simple free */
144 /* software project (where the assembly completes rapidly on */
145 /* any modern cpu), I don't want to bother with all the unpleasant */
146 /* threading issues */
147
148 void
149 do_assemble(GtkWidget *widget, gpointer *data)
150 {
151         const char *fmt=NULL, *filename;
152         int r, do_lst;
153
154
155         /* collect info from the GTK widgets */
156
157         do_lst = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(list_check));
158
159         filename = gtk_entry_get_text(GTK_ENTRY(file_entry));
160         if (filename == NULL || *filename == '\0') {
161                 mesg("You need to specify the ASM file\n");
162                 return;
163         }
164
165         fmt = emit_desc_to_name_lookup(gtk_entry_get_text(
166                 GTK_ENTRY(GTK_COMBO(otype_combo)->entry)));
167         if (fmt == NULL) {
168                 mesg("Please select a valid output type\n");
169                 return;
170         }
171
172         /* now we can begin working like normal */
173
174         r = run_as31(filename, do_lst, 0, fmt, NULL, NULL);
175
176         if (r == 0) {
177                 mesg("Assembly complete, no errors :)\n");
178         }
179 }
180
181
182 /* quit the program if they close it */
183
184 gint
185 delete_handler(GtkWidget *widget, gpointer *data)
186 {
187         gtk_main_quit();
188         return FALSE;
189 }
190
191
192 /* the assembler calls here to display any messages */
193
194 void mesg(const char *str)
195 {
196         if (str == NULL) str = "(null)";
197         /* printf("%s", str); */
198         gtk_text_insert(GTK_TEXT(output_text), NULL, NULL,
199                 NULL, str, strlen(str));
200 }
201