1 #include <forms.h>
2 #include "mainwindowdesign.h"
3 #include "configure.h"
4 #include "apptogui.h"
5 #include "guiglobal.h"
6
7 void apptogui_initialize_main_window(int *argc,
8 char *argv[],
9 const char *name);
10 void apptogui_show_main_window(int total, int left, int eliminated);
11 void apptogui_run_main_window(void);
12 void apptogui_show_correct_solution(char *);
13 void apptogui_add_choices(FD_mainwindow *mainwindow);
14 void apptogui_hide_shortcut_buttons(FD_mainwindow *mainwindow);
15 void apptogui_current_state(int, int, int);
16 void apptogui_get_entered_try(long, char *);
17 void apptogui_flat_try_button(long);
18 void apptogui_show_answer(char *, long);
19 void apptogui_reset_all(int total, int left, int eliminated);
20 void apptogui_change_choice_item(int position, char ch);
21
22 void
apptogui_initialize_main_window(int * argc,char * argv[],const char * name)23 apptogui_initialize_main_window(int *argc,
24 char *argv[],
25 const char *name)
26 {
27 fl_initialize(argc, argv, name, 0, 0);
28 }
29
30 void
apptogui_show_main_window(int total,int left,int eliminated)31 apptogui_show_main_window(int total, int left, int eliminated)
32 {
33 int i;
34
35 mainwindow = create_form_mainwindow();
36
37 apptogui_add_choices(mainwindow);
38 apptogui_hide_shortcut_buttons(mainwindow);
39 apptogui_current_state(total, left, eliminated);
40
41 for (i = 0; i < HEIGHT; i++)
42 {
43 fl_deactivate_object(mainwindow->try_button[i]);
44 fl_set_object_boxtype(mainwindow->try_button[i], FL_UP_BOX);
45 fl_redraw_object(mainwindow->try_button[i]);
46 }
47 fl_activate_object(mainwindow->try_button[0]);
48
49 fl_show_form(mainwindow->mainwindow,
50 FL_PLACE_MOUSE,
51 FL_TRANSIENT,
52 "Mastermind");
53 }
54
55 void
apptogui_run_main_window()56 apptogui_run_main_window()
57 {
58 fl_do_forms();
59 }
60
apptogui_show_correct_solution(char * solution)61 void apptogui_show_correct_solution(char *solution)
62 {
63 int i;
64 char label[2];
65
66 label[1] = 0;
67 for (i = 0; i < WIDTH; i++)
68 {
69 label[0] = solution[i];
70 fl_set_object_label(mainwindow->correct_solution[i], label);
71 fl_redraw_object(mainwindow->correct_solution[i]);
72 }
73 fl_deactivate_object(mainwindow->get_solution_button);
74 for (i = 0; i < HEIGHT; i++)
75 apptogui_flat_try_button(i);
76 }
77
78 void
apptogui_add_choices(FD_mainwindow * mainwindow)79 apptogui_add_choices(FD_mainwindow *mainwindow)
80 {
81 int i;
82 int j;
83 char text[2];
84
85 text[1] = 0;
86 for (i = 0 ; i < WIDTH * HEIGHT ; i++)
87 {
88
89 fl_set_choice_fontsize(mainwindow->game_area_choice[i], FL_HUGE_SIZE);
90 fl_set_choice_fontstyle(mainwindow->game_area_choice[i], FL_BOLD_STYLE);
91 for (j = 0 ; j < COLORS ; j++)
92 {
93 text[0] = (char) ('A' + j);
94 fl_addto_choice(mainwindow->game_area_choice[i], text);
95 }
96 }
97 }
98
99 void
apptogui_hide_shortcut_buttons(FD_mainwindow * mainwindow)100 apptogui_hide_shortcut_buttons(FD_mainwindow *mainwindow)
101 {
102 int i;
103
104 for (i = 0; i < 26; i++)
105 {
106 mainwindow->shortcut[i]->type = FL_HIDDEN_BUTTON;
107 }
108 }
109
apptogui_current_state(int total,int left,int eliminated)110 void apptogui_current_state(int total, int left, int eliminated)
111 {
112 char total_str[100];
113 char left_str[100];
114 char eliminated_str[100];
115
116 sprintf(total_str, "%u", total);
117 fl_set_object_label(mainwindow->total_combinations, total_str);
118 fl_redraw_object(mainwindow->total_combinations);
119
120 sprintf(left_str,
121 "%u (ca. %u%%)",
122 left,
123 left * 100 / total);
124 fl_set_object_label(mainwindow->left_combinations, left_str);
125 fl_redraw_object(mainwindow->left_combinations);
126
127 sprintf(eliminated_str,
128 "%u (ca. %u%%)",
129 eliminated,
130 100 - (left * 100 / total));
131 fl_set_object_label(mainwindow->eliminated_combinations, eliminated_str);
132 fl_redraw_object(mainwindow->eliminated_combinations);
133 }
134
apptogui_get_entered_try(long number,char * entered)135 void apptogui_get_entered_try(long number, char *entered)
136 {
137 char choice[10];
138 int i;
139
140 /* Get the contents from the choices of this try */
141 for (i = 0; i < WIDTH; i++)
142 {
143 strncpy(choice, fl_get_choice_text(mainwindow->game_area_choice[number
144 * WIDTH
145 + i]),2);
146 if (NULL == choice)
147 {
148 printf("In a choice nothing was selected.\n");
149 }
150 entered[i] = choice[0];
151 fl_deactivate_object(mainwindow->game_area_choice[number
152 *WIDTH
153 + i]);
154 }
155 }
156
apptogui_flat_try_button(long number)157 void apptogui_flat_try_button(long number)
158 {
159 fl_set_object_boxtype(mainwindow->try_button[number], FL_FRAME_BOX);
160 fl_deactivate_object(mainwindow->try_button[number]);
161 if (HEIGHT > (number + 1))
162 fl_activate_object(mainwindow->try_button[number + 1]);
163 fl_redraw_object(mainwindow->try_button[number]);
164 }
165
apptogui_show_answer(char * to_show,long line)166 void apptogui_show_answer(char *to_show, long line)
167 {
168 int i;
169
170 for (i = 0; i < WIDTH; i++)
171 {
172 switch (to_show[i])
173 {
174 case '*':
175 fl_set_object_color(mainwindow->answer[line * WIDTH + i],
176 FL_BLACK, FL_BLACK);
177 fl_redraw_object(mainwindow->answer[line * WIDTH + i]);
178 break;
179 case 'O':
180 fl_set_object_color(mainwindow->answer[line * WIDTH + i],
181 FL_WHITE, FL_WHITE);
182 fl_redraw_object(mainwindow->answer[line * WIDTH + i]);
183 break;
184 default:
185 fl_set_object_boxtype(mainwindow->answer[line * WIDTH + i],
186 FL_FLAT_BOX);
187 fl_set_object_color(mainwindow->answer[line * WIDTH + i],
188 FL_COL1, FL_COL1);
189 fl_redraw_object(mainwindow->answer[line * WIDTH + i]);
190 }
191 }
192 }
193
194 void
apptogui_reset_all(int total,int left,int eliminated)195 apptogui_reset_all(int total, int left, int eliminated)
196 {
197 int i;
198 char choice[2];
199 char label[2];
200
201 for (i = 0; i < WIDTH * HEIGHT; i++)
202 {
203 fl_activate_object(mainwindow->game_area_choice[i]);
204 strncpy(choice, fl_get_choice_text(mainwindow->game_area_choice[i]), 2);
205 if ('A' != choice[0])
206 {
207 fl_set_choice_text(mainwindow->game_area_choice[i], "A");
208 fl_redraw_object(mainwindow->game_area_choice[i]);
209 }
210 fl_set_object_boxtype(mainwindow->answer[i], FL_BORDER_BOX);
211 fl_set_object_color(mainwindow->answer[i], FL_COL1, FL_COL1);
212 fl_redraw_object(mainwindow->answer[i]);
213 }
214
215 label[1] = 0;
216 label[0] = '?';
217
218 for (i = 0; i < HEIGHT; i++)
219 {
220 fl_deactivate_object(mainwindow->try_button[i]);
221 fl_set_object_boxtype(mainwindow->try_button[i], FL_UP_BOX);
222 fl_redraw_object(mainwindow->try_button[i]);
223 }
224 fl_activate_object(mainwindow->try_button[0]);
225
226 for (i = 0; i < WIDTH; i++)
227 {
228 fl_set_object_label(mainwindow->correct_solution[i], label);
229 }
230 fl_activate_object(mainwindow->get_solution_button);
231 apptogui_current_state(total, left, eliminated);
232 }
233
234 void
apptogui_change_choice_item(int position,char ch)235 apptogui_change_choice_item(int position, char ch)
236 {
237 fl_set_choice(mainwindow->game_area_choice[position], ch+1);
238 }
239
240
241