Esempio n. 1
0
File: file.c Progetto: btb/d2x
int ui_get_filename( char * filename, char * Filespec, char * message  )
{
	char		ViewDir[PATH_MAX];
	char		InputText[PATH_MAX];
	char		*p;
	PHYSFS_file	*TempFile;
	int			NumFiles, NumDirs,i;
	char		**filename_list;
	char		**directory_list;
	char		Spaces[35];
	UI_WINDOW			*wnd;
	UI_GADGET_BUTTON	*Button1, *Button2, *HelpButton;
	UI_GADGET_LISTBOX	*ListBox1;
	UI_GADGET_LISTBOX	*ListBox2;
	UI_GADGET_INPUTBOX	*UserFile;
	int 				new_listboxes;

	if ((p = strrchr(filename, '/')))
	{
		*p++ = 0;
		strcpy(ViewDir, filename);
		strcpy(InputText, p);
	}
	else
	{
		strcpy(ViewDir, "");
		strcpy(InputText, filename);
	}

	filename_list = file_getfilelist(&NumFiles, Filespec, ViewDir);
	directory_list = file_getdirlist(&NumDirs, ViewDir);

	// Running out of memory may become likely in the future
	if (!filename_list && !directory_list)
		return 0;

	if (!filename_list)
	{
		PHYSFS_freeList(directory_list);
		return 0;
	}

	if (!directory_list)
	{
		PHYSFS_freeList(filename_list);
		return 0;
	}

	//ui_messagebox( -2,-2, 1,"DEBUG:0", "Ok" );
	for (i=0; i<35; i++)
		Spaces[i] = ' ';
	Spaces[34] = 0;

	wnd = ui_open_window( 200, 100, 400, 370, WIN_DIALOG );

	ui_wprintf_at( wnd, 10, 5, message );

	ui_wprintf_at( wnd, 20, 32,"N&ame" );
	UserFile  = ui_add_gadget_inputbox( wnd, 60, 30, PATH_MAX, 40, InputText );

	ui_wprintf_at( wnd, 20, 86,"&Files" );
	ui_wprintf_at( wnd, 210, 86,"&Dirs" );

	ListBox1 = ui_add_gadget_listbox(wnd,  20, 110, 125, 200, NumFiles, filename_list);
	ListBox2 = ui_add_gadget_listbox(wnd, 210, 110, 100, 200, NumDirs, directory_list);

	Button1 = ui_add_gadget_button( wnd,     20, 330, 60, 25, "Ok", NULL );
	Button2 = ui_add_gadget_button( wnd,    100, 330, 60, 25, "Cancel", NULL );
	HelpButton = ui_add_gadget_button( wnd, 180, 330, 60, 25, "Help", NULL );

	wnd->keyboard_focus_gadget = (UI_GADGET *)UserFile;

	Button1->hotkey = KEY_CTRLED + KEY_ENTER;
	Button2->hotkey = KEY_ESC;
	HelpButton->hotkey = KEY_F1;
	ListBox1->hotkey = KEY_ALTED + KEY_F;
	ListBox2->hotkey = KEY_ALTED + KEY_D;
	UserFile->hotkey = KEY_ALTED + KEY_A;

	ui_gadget_calc_keys(wnd);

	ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
	ui_wprintf_at( wnd, 20, 60, "%s", ViewDir );

	new_listboxes = 0;

	while( 1 )
	{
		ui_mega_process();
		ui_window_do_gadgets(wnd);

		if ( Button2->pressed )
		{
			PHYSFS_freeList(filename_list);
			PHYSFS_freeList(directory_list);
			ui_close_window(wnd);
			return 0;
		}

		if ( HelpButton->pressed )
			ui_messagebox( -1, -1, 1, "Sorry, no help is available!", "Ok" );

		if (ListBox1->moved || new_listboxes)
		{
			if (ListBox1->current_item >= 0 )
				ui_inputbox_set_text(UserFile, filename_list[ListBox1->current_item]);
		}

		if (ListBox2->moved || new_listboxes)
		{
			if (ListBox2->current_item >= 0 )
				ui_inputbox_set_text(UserFile, directory_list[ListBox2->current_item]);
		}
		new_listboxes = 0;

		if (Button1->pressed || UserFile->pressed || (ListBox1->selected_item > -1 ) || (ListBox2->selected_item > -1 ))
		{
			ui_mouse_hide();

			if (ListBox2->selected_item > -1 )
				strcpy(UserFile->text, directory_list[ListBox2->selected_item]);

			strncpy(filename, ViewDir, PATH_MAX);

			p = UserFile->text;
			while (!strncmp(p, "..", 2))	// shorten the path manually
			{
				char *sep = strrchr(filename, '/');
				if (sep)
					*sep = 0;
				else
					*filename = 0;	// look directly in search paths

				p += 2;
				if (*p == '/')
					p++;
			}

			if (*filename && *p)
				strncat(filename, "/", PATH_MAX - strlen(filename));
			strncat(filename, p, PATH_MAX - strlen(filename));

			if (!PHYSFS_isDirectory(filename))
			{
				TempFile = PHYSFS_openRead(filename);
				if (TempFile)
				{
					// Looks like a valid filename that already exists!
					PHYSFS_close(TempFile);
					break;
				}
	
				// File doesn't exist, but can we create it?
				TempFile = PHYSFS_openWrite(filename);
				if (TempFile)
				{
					// Looks like a valid filename!
					PHYSFS_close(TempFile);
					PHYSFS_delete(filename);
					break;
				}
			}
			else
			{
				if (filename[strlen(filename) - 1] == '/')	// user typed a separator on the end
					filename[strlen(filename) - 1] = 0;
	
				strcpy(ViewDir, filename);
	
				//mprintf( 0, "----------------------------\n" );
				//mprintf( 0, "Full dir: '%s'\n", ViewDir );
	
				PHYSFS_freeList(filename_list);
				filename_list = file_getfilelist(&NumFiles, Filespec, ViewDir);
				if (!filename_list)
				{
					PHYSFS_freeList(directory_list);
					return 0;
				}

				ui_inputbox_set_text(UserFile, Filespec);

				PHYSFS_freeList(directory_list);
				directory_list = file_getdirlist(&NumDirs, ViewDir);
				if (!directory_list)
				{
					PHYSFS_freeList(filename_list);
					return 0;
				}

				ui_listbox_change(wnd, ListBox1, NumFiles, filename_list);
				ui_listbox_change(wnd, ListBox2, NumDirs, directory_list);
				new_listboxes = 0;

				ui_wprintf_at( wnd, 20, 60, "%s", Spaces );
				ui_wprintf_at( wnd, 20, 60, "%s", ViewDir );

				//i = TICKER;
				//while ( TICKER < i+2 );

			}

			ui_mouse_show();

		}

		vid_update();
	}

	//key_flush();

	ui_close_window(wnd);
	if (filename_list)
		PHYSFS_freeList(filename_list);
	if (directory_list)
		PHYSFS_freeList(directory_list);

	return 1;
}
Esempio n. 2
0
int ui_get_filename( char * filename, const char * filespec, const char * message  )
{
    char			InputText[PATH_MAX];
    char			*p;
    int				i;
    file_browser	*b;
    UI_DIALOG		*dlg;
    window			*wind;
    int				rval = 0;

    MALLOC(b, file_browser, 1);
    if (!b)
        return 0;

    if ((p = strrchr(filename, '/')))
    {
        *p++ = 0;
        strcpy(b->view_dir, filename);
        strcpy(InputText, p);
    }
    else
    {
        strcpy(b->view_dir, "");
        strcpy(InputText, filename);
    }

    b->filename_list = file_getfilelist(&b->num_files, filespec, b->view_dir);
    if (!b->filename_list)
    {
        d_free(b);
        return 0;
    }

    b->directory_list = file_getdirlist(&b->num_dirs, b->view_dir);
    if (!b->directory_list)
    {
        PHYSFS_freeList(b->filename_list);
        d_free(b);
        return 0;
    }

    //ui_messagebox( -2,-2, 1,"DEBUG:0", "Ok" );
    for (i=0; i<35; i++)
        b->spaces[i] = ' ';
    b->spaces[34] = 0;

    dlg = ui_create_dialog( 200, 100, 400, 370, static_cast<dialog_flags>(DF_DIALOG | DF_MODAL), (int (*)(UI_DIALOG *, d_event *, void *))browser_handler, b );

    b->user_file  = ui_add_gadget_inputbox( dlg, 60, 30, PATH_MAX, 40, InputText );

    b->listbox1 = ui_add_gadget_listbox(dlg,  20, 110, 125, 200, b->num_files, b->filename_list);
    b->listbox2 = ui_add_gadget_listbox(dlg, 210, 110, 100, 200, b->num_dirs, b->directory_list);

    b->button1 = ui_add_gadget_button( dlg,     20, 330, 60, 25, "Ok", NULL );
    b->button2 = ui_add_gadget_button( dlg,    100, 330, 60, 25, "Cancel", NULL );
    b->help_button = ui_add_gadget_button( dlg, 180, 330, 60, 25, "Help", NULL );

    dlg->keyboard_focus_gadget = (UI_GADGET *)b->user_file;

    b->button1->hotkey = KEY_CTRLED + KEY_ENTER;
    b->button2->hotkey = KEY_ESC;
    b->help_button->hotkey = KEY_F1;
    b->listbox1->hotkey = KEY_ALTED + KEY_F;
    b->listbox2->hotkey = KEY_ALTED + KEY_D;
    b->user_file->hotkey = KEY_ALTED + KEY_A;

    ui_gadget_calc_keys(dlg);

    b->filename = filename;
    b->filespec = filespec;
    b->message = message;

    wind = ui_dialog_get_window(dlg);

    while (window_exists(wind))
        event_process();

    //key_flush();

    if (b->filename_list)
        PHYSFS_freeList(b->filename_list);
    if (b->directory_list)
        PHYSFS_freeList(b->directory_list);

    rval = b->filename_list != NULL;
    d_free(b);

    return rval;
}
Esempio n. 3
0
int ui_get_filename(char (&filename)[PATH_MAX], const char *const filespec, const char *const message)
{
	char		InputText[PATH_MAX];
	char		*p;
	UI_DIALOG	*dlg;
	int			rval = 0;
	auto b = make_unique<ui_file_browser>();
	if ((p = strrchr(filename, '/')))
	{
		*p++ = 0;
		strcpy(b->view_dir, filename);
		strcpy(InputText, p);
	}
	else
	{
		strcpy(b->view_dir, "");
		strcpy(InputText, filename);
	}

	b->filename_list = file_getfilelist(filespec, b->view_dir);
	if (!b->filename_list)
	{
		return 0;
	}
	
	b->directory_list = file_getdirlist(b->view_dir);
	if (!b->directory_list)
	{
		b->filename_list.reset();
		return 0;
	}

	//ui_messagebox( -2,-2, 1,"DEBUG:0", "Ok" );
	for (int i=0; i<35; i++)
		b->spaces[i] = ' ';
	b->spaces[34] = 0;

	dlg = ui_create_dialog( 200, 100, 400, 370, static_cast<dialog_flags>(DF_DIALOG | DF_MODAL), browser_handler, b.get());

	b->user_file  = ui_add_gadget_inputbox<40>(dlg, 60, 30, InputText);

	b->listbox1 = ui_add_gadget_listbox(dlg,  20, 110, 125, 200, b->filename_list.get_count(), b->filename_list.get());
	b->listbox2 = ui_add_gadget_listbox(dlg, 210, 110, 100, 200, b->directory_list.get_count(), b->directory_list.get());

	b->button1 = ui_add_gadget_button( dlg,     20, 330, 60, 25, "Ok", NULL );
	b->button2 = ui_add_gadget_button( dlg,    100, 330, 60, 25, "Cancel", NULL );
	b->help_button = ui_add_gadget_button( dlg, 180, 330, 60, 25, "Help", NULL );

	dlg->keyboard_focus_gadget = b->user_file.get();

	b->button1->hotkey = KEY_CTRLED + KEY_ENTER;
	b->button2->hotkey = KEY_ESC;
	b->help_button->hotkey = KEY_F1;
	b->listbox1->hotkey = KEY_ALTED + KEY_F;
	b->listbox2->hotkey = KEY_ALTED + KEY_D;
	b->user_file->hotkey = KEY_ALTED + KEY_A;

	ui_gadget_calc_keys(dlg);

	b->filename = filename;
	b->filespec = filespec;
	b->message = message;
	
	event_process_all();

	//key_flush();

	rval = static_cast<bool>(b->filename_list);
	b->filename_list.reset();
	b->directory_list.reset();
	return rval;
}