Previous 15994 Revisions Next

r2971 Sunday 20th July, 2008 at 14:15:24 UTC by Nathan Woods
Cleanups on the File Manager create image menu, still does not work yet
[src/mess]filemngr.c

src/mess/filemngr.c
r2970r2971
1818#include "ui.h"
1919#include "uimenu.h"
2020#include "zippath.h"
21#include "unicode.h"
2122
2223
2324
r2970r2971
2526    CONSTANTS
2627***************************************************************************/
2728
28#define ITEMREF_NEW_IMAGE_NAME   ((void *) 0x0001)
29#define ITEMREF_CREATE   ((void *) 0x0001)
2930
3031
3132
r2970r2971
9596***************************************************************************/
9697
9798/*-------------------------------------------------
98    code_to_ascii - converts an input_code to its
99   ASCII equivalent
99    input_character - inputs a typed character
100   into a buffer
100101-------------------------------------------------*/
101102
102static char code_to_ascii(input_code code)
103static void input_character(char *buffer, size_t buffer_length, unicode_char unichar, int (*filter)(unicode_char))
103104{
104   /* code, lower case (w/o shift), upper case (with shift), control */
105   static const struct
106   {
107      input_code code;
108      char ch;
109      char shift_ch;
110      char ctrl_ch;
111   } code_to_char_table[] =
112   {
113      { KEYCODE_0, '0', ')', 0},
114      { KEYCODE_1, '1', '!', 0},
115      { KEYCODE_2, '2', '"', 0},
116      { KEYCODE_3, '3', '#', 0},
117      { KEYCODE_4, '4', '$', 0},
118      { KEYCODE_5, '5', '%', 0},
119      { KEYCODE_6, '6', '^', 0},
120      { KEYCODE_7, '7', '&', 0},
121      { KEYCODE_8, '8', '*', 0},
122      { KEYCODE_9, '9', '(', 0},
123      { KEYCODE_A, 'a', 'A', 1},
124      { KEYCODE_B, 'b', 'B', 2},
125      { KEYCODE_C, 'c', 'C', 3},
126      { KEYCODE_D, 'd', 'D', 4},
127      { KEYCODE_E, 'e', 'E', 5},
128      { KEYCODE_F, 'f', 'F', 6},
129      { KEYCODE_G, 'g', 'G', 7},
130      { KEYCODE_H, 'h', 'H', 8},
131      { KEYCODE_I, 'i', 'I', 9},
132      { KEYCODE_J, 'j', 'J', 10},
133      { KEYCODE_K, 'k', 'K', 11},
134      { KEYCODE_L, 'l', 'L', 12},
135      { KEYCODE_M, 'm', 'M', 13},
136      { KEYCODE_N, 'n', 'N', 14},
137      { KEYCODE_O, 'o', 'O', 15},
138      { KEYCODE_P, 'p', 'P', 16},
139      { KEYCODE_Q, 'q', 'Q', 17},
140      { KEYCODE_R, 'r', 'R', 18},
141      { KEYCODE_S, 's', 'S', 19},
142      { KEYCODE_T, 't', 'T', 20},
143      { KEYCODE_U, 'u', 'U', 21},
144      { KEYCODE_V, 'v', 'V', 22},
145      { KEYCODE_W, 'w', 'W', 23},
146      { KEYCODE_X, 'x', 'X', 24},
147      { KEYCODE_Y, 'y', 'Y', 25},
148      { KEYCODE_Z, 'z', 'Z', 26},
149      { KEYCODE_OPENBRACE, '[', '{', 27},
150      { KEYCODE_BACKSLASH, '\\', '|', 28},
151      { KEYCODE_CLOSEBRACE, ']', '}', 29},
152      { KEYCODE_TILDE, '^', '~', 30},
153      { KEYCODE_BACKSPACE, 127, 127, 31},
154      { KEYCODE_COLON, ':', ';', 0},
155      { KEYCODE_EQUALS, '=', '+', 0},
156      { KEYCODE_MINUS, '-', '_', 0},
157      { KEYCODE_STOP, '.', '<', 0},
158      { KEYCODE_COMMA, ',', '>', 0},
159      { KEYCODE_SLASH, '/', '?', 0},
160      { KEYCODE_ENTER, 13, 13, 13},
161      { KEYCODE_ESC, 27, 27, 27 }
162   };
105   size_t buflen = strlen(buffer);
163106
164   int i;
165   char result = 0;
166
167   for (i = 0; i < ARRAY_LENGTH(code_to_char_table); i++)
107   if ((unichar == 8) && (buflen > 0))
168108   {
169      if (code_to_char_table[i].code == code)
170      {
171         if (input_code_pressed(KEYCODE_LCONTROL) || input_code_pressed(KEYCODE_RCONTROL))
172            result = code_to_char_table[i].ctrl_ch;
173         else if (input_code_pressed(KEYCODE_LSHIFT) || input_code_pressed(KEYCODE_RSHIFT))
174            result = code_to_char_table[i].shift_ch;
175         else
176            result = code_to_char_table[i].ch;
177         break;
178      }
109      *(char *)utf8_previous_char(&buffer[buflen]) = 0;
179110   }
180   return result;
181}
182
183
184
185/*-------------------------------------------------
186    poll_keyboard - polls the keyboard and appends
187   the result to the specified buffer
188-------------------------------------------------*/
189
190static void poll_keyboard(char *buffer, size_t buffer_length, int (*filter)(unicode_char))
191{
192   input_code code;
193   char ascii_char;
194   int length;
195
196   /* poll keyboard */
197   code = input_code_poll_switches(FALSE);
198   if (code != INPUT_CODE_INVALID)
111   else if ((unichar > ' ') && ((filter == NULL) || (*filter)(unichar)))
199112   {
200      ascii_char = code_to_ascii(code);
201
202      switch (ascii_char)
203      {
204         case 0:      /* NUL */
205         case 13:   /* return */
206         case 27:   /* escape */
207            break;
208
209         case 25:   /* Ctrl-Y (clear line) */
210            buffer[0] = '\0';
211            break;
212
213         case 127:   /* delete */
214            length = strlen(buffer);
215            if (length > 0)
216               buffer[length - 1] = '\0';
217            break;
218
219         default:
220            if ((filter == NULL) || (*filter)(ascii_char))
221            {
222               /* got a char - add to string */
223               snprintf(buffer + strlen(buffer), buffer_length - strlen(buffer), "%c", ascii_char);
224            }
225            break;
226      }
113      buflen += utf8_from_uchar(&buffer[buflen], buffer_length - buflen, unichar);
114      buffer[buflen] = 0;
227115   }
228116}
229117
r2970r2971
303191   character is valid in a filename
304192-------------------------------------------------*/
305193
306static int is_valid_filename_char(unicode_char ch)
194static int is_valid_filename_char(unicode_char unichar)
307195{
308196   /* this should really be in the OSD layer */
309197   static const char valid_filename_char[] =
r2970r2971
317205      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    /* `abcdefghijklmno */
318206      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0,    /* pqrstuvwxyz{|}~   */
319207   };
320   return (ch < ARRAY_LENGTH(valid_filename_char)) && valid_filename_char[ch];
208   return (unichar < ARRAY_LENGTH(valid_filename_char)) && valid_filename_char[unichar];
321209}
322210
323211
324212
325213/*-------------------------------------------------
326    file_creator_render_extra - perform our
214    file_create_render_extra - perform our
327215    special rendering
328216-------------------------------------------------*/
329217
330static void file_creator_render_extra(running_machine *machine, ui_menu *menu, void *state, void *selectedref, float top, float bottom, float origx1, float origy1, float origx2, float origy2)
218static void file_create_render_extra(running_machine *machine, ui_menu *menu, void *state, void *selectedref, float top, float bottom, float origx1, float origy1, float origx2, float origy2)
331219{
220   astring *buffer;
332221   file_create_menu_state *menustate = (file_create_menu_state *) state;
333222
334   extra_text_render(machine, menu, state, selectedref, top, bottom, origx1, origy1, origx2, origy2,
223   buffer = astring_assemble_4(astring_alloc(),
335224      astring_c(menustate->manager_menustate->current_directory),
225      "\nNew Image Name: ",
226      menustate->filename_buffer,
227      "_");
228
229   extra_text_render(machine, menu, state, selectedref, top, bottom, origx1, origy1, origx2, origy2,
230      astring_c(buffer),
336231      NULL);
232
233   astring_free(buffer);
337234}
338235
339236
340237
341238/*-------------------------------------------------
239    menu_file_create_populate - populates the file
240   creator menu
241-------------------------------------------------*/
242
243static void menu_file_create_populate(running_machine *machine, ui_menu *menu)
244{
245   /* append menu items */
246   ui_menu_item_append(menu, "Create", NULL, 0, ITEMREF_CREATE);
247
248   /* set up custom render proc */
249   ui_menu_set_custom_render(menu, file_create_render_extra, (ui_get_line_height() * 2) + 3.0f * UI_BOX_TB_BORDER, 0);
250}
251
252
253
254/*-------------------------------------------------
342255    menu_file_create - file creator menu
343256-------------------------------------------------*/
344257
r2970r2971
346259{
347260   astring *new_path;
348261   const ui_menu_event *event;
349   int underscore_pos = -1;
350262   file_create_menu_state *menustate = (file_create_menu_state *) state;
351263
352   /* clear the menu */
353   ui_menu_reset(menu, 0);
354   
355   /* add the "[empty slot]" entry */
356   ui_menu_item_append(menu, "New Image Name:", menustate->filename_buffer, 0, ITEMREF_NEW_IMAGE_NAME);
264   /* if the menu isn't built, populate now */
265   if (!ui_menu_populated(menu))
266      menu_file_create_populate(machine, menu);
357267
358   /* set up custom render proc */
359   ui_menu_set_custom_render(menu, file_creator_render_extra, ui_get_line_height() + 3.0f * UI_BOX_TB_BORDER, 0);
360
361   /* features that are only enabled when selecting the file name */
362   if (ui_menu_get_selection(menu) == 0)
363   {
364      /* poll the keyboard */
365      poll_keyboard(menustate->filename_buffer, ARRAY_LENGTH(menustate->filename_buffer), is_valid_filename_char);
366
367      /* put the underscore in the menu */
368      underscore_pos = strlen(menustate->filename_buffer);
369      snprintf(menustate->filename_buffer + underscore_pos, ARRAY_LENGTH(menustate->filename_buffer) - underscore_pos, "_");
370   }
371
372268   /* process the menu */
373269   event = ui_menu_process(menu, 0);
374270
375   /* remove the underscore, if present */
376   if (underscore_pos >= 0)
377      menustate->filename_buffer[underscore_pos] = '\0';
378
379271   /* process the event */
380   if (event != NULL && event->itemref != NULL)
272   if (event != NULL)
381273   {
382274      /* handle selections */
383      if (event->iptkey == IPT_UI_SELECT)
275      switch(event->iptkey)
384276      {
385         if (event->itemref == ITEMREF_NEW_IMAGE_NAME)
386         {
387            /* create the image */
388            new_path = zippath_combine(
389               astring_alloc(),
390               astring_c(menustate->manager_menustate->current_directory),
391               menustate->filename_buffer);
392            image_create(
393               menustate->manager_menustate->selected_device,
394               astring_c(new_path),
395               0,
396               NULL);
397            astring_free(new_path);
398         }
277         case IPT_UI_SELECT:
278            if (event->itemref == ITEMREF_CREATE)
279            {
280               /* create the image */
281               new_path = zippath_combine(
282                  astring_alloc(),
283                  astring_c(menustate->manager_menustate->current_directory),
284                  menustate->filename_buffer);
285               image_create(
286                  menustate->manager_menustate->selected_device,
287                  astring_c(new_path),
288                  0,
289                  NULL);
290               astring_free(new_path);
291            }
292            break;
293
294         case IPT_SPECIAL:
295            input_character(
296               menustate->filename_buffer,
297               ARRAY_LENGTH(menustate->filename_buffer),
298               event->unichar,
299               is_valid_filename_char);
300            break;
399301      }
400302   }
401303}

Previous 15994 Revisions Next


© 1998-2018 The MESS Team