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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
|
/* filesel.c: File selection dialog box
Copyright (c) 2001-2005 Matan Ziv-Av, Philip Kendall, Russell Marks,
Marek Januszewski
$Id: filesel.c 4246 2011-01-09 20:47:53Z pak21 $
This program 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.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include <config.h>
#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#ifdef WIN32
#include <windows.h>
#include <direct.h>
#include <ctype.h>
#endif /* #ifdef WIN32 */
#include "fuse.h"
#include "ui/ui.h"
#include "utils.h"
#include "widget_internals.h"
#if defined AMIGA || defined __MORPHOS__
#include <proto/asl.h>
#include <proto/dos.h>
#include <proto/exec.h>
struct Library *AslBase;
#ifndef __MORPHOS__
struct AslIFace *IAsl;
struct Library *DOSBase;
#endif /* #ifndef __MORPHOS__ */
#ifndef __MORPHOS__
struct DOSIFace *IDOS;
struct Library *ExecBase;
#endif /* #ifndef __MORPHOS__ */
int err = 0;
char *amiga_asl( char *title, BOOL is_saving );
#endif /* ifdef AMIGA */
struct widget_dirent **widget_filenames; /* Filenames in the current
directory */
size_t widget_numfiles; /* The number of files in the current
directory */
static const char *title;
static int is_saving;
#ifdef WIN32
static int is_drivesel = 0;
static int is_rootdir;
#endif /* #ifdef WIN32 */
#define PAGESIZE (is_saving ? 32 : 36)
/* The number of the filename in the top-left corner of the current
display, that of the filename which the `cursor' is on, and that
which it will be on after this keypress */
static size_t top_left_file, current_file, new_current_file;
static char *widget_get_filename( const char *title, int saving );
static int widget_add_filename( int *allocated, int *number,
struct widget_dirent ***namelist, char *name );
static void widget_scan( char *dir );
static int widget_select_file( const char *name );
static int widget_scan_compare( const widget_dirent **a,
const widget_dirent **b );
#if !defined AMIGA && !defined __MORPHOS__
static char* widget_getcwd( void );
#endif /* ifndef AMIGA */
static int widget_print_all_filenames( struct widget_dirent **filenames, int n,
int top_left, int current,
const char *dir );
static int widget_print_filename( struct widget_dirent *filename, int position,
int inverted );
#ifdef WIN32
static void widget_filesel_chdrv( void );
static void widget_filesel_drvlist( void );
#endif /* #ifdef WIN32 */
static int widget_filesel_chdir( void );
/* The filename to return */
char* widget_filesel_name;
/* Should we exit all widgets when we're done with this selector? */
static int exit_all_widgets;
static char *
widget_get_filename( const char *title, int saving )
{
char *filename = NULL;
widget_type wtype;
widget_filesel_data data;
data.exit_all_widgets = 1;
data.title = title;
if( saving ) {
wtype = WIDGET_TYPE_FILESELECTOR_SAVE;
} else {
wtype = WIDGET_TYPE_FILESELECTOR;
}
widget_do( wtype, &data );
if( widget_filesel_name ) {
filename = strdup( widget_filesel_name );
if( !filename )
ui_error( UI_ERROR_ERROR, "Out of memory at %s:%d", __FILE__, __LINE__ );
}
return filename;
}
char *
ui_get_open_filename( const char *title )
{
#if !defined AMIGA && !defined __MORPHOS__
return widget_get_filename( title, 0 );
#else
return amiga_asl( title, FALSE );
#endif
}
char *
ui_get_save_filename( const char *title )
{
#if !defined AMIGA && !defined __MORPHOS__
return widget_get_filename( title, 1 );
#else
return amiga_asl( title, TRUE );
#endif
}
static int widget_add_filename( int *allocated, int *number,
struct widget_dirent ***namelist,
char *name ) {
int i; size_t length;
if( ++*number > *allocated ) {
struct widget_dirent **oldptr = *namelist;
*namelist = realloc( (*namelist), 2 * *allocated * sizeof(**namelist) );
if( *namelist == NULL ) {
for( i=0; i<*number-1; i++ ) {
free( oldptr[i]->name );
free( oldptr[i] );
}
free( oldptr );
return -1;
}
*allocated *= 2;
}
(*namelist)[*number-1] = malloc( sizeof(***namelist) );
if( !(*namelist)[*number-1] ) {
for( i=0; i<*number-1; i++ ) {
free( (*namelist)[i]->name );
free( (*namelist)[i] );
}
free( *namelist );
*namelist = NULL;
return -1;
}
length = strlen( name ) + 1;
if( length < 16 ) length = 16;
(*namelist)[*number-1]->name = malloc( length );
if( !(*namelist)[*number-1]->name ) {
free( (*namelist)[*number-1] );
for( i=0; i<*number-1; i++ ) {
free( (*namelist)[i]->name );
free( (*namelist)[i] );
}
free( *namelist );
*namelist = NULL;
return -1;
}
strncpy( (*namelist)[*number-1]->name, name, length );
(*namelist)[*number-1]->name[ length - 1 ] = 0;
return 0;
}
#if defined AMIGA || defined __MORPHOS__
char *
amiga_asl( char *title, BOOL is_saving ) {
char *filename;
struct FileRequester *filereq;
#ifndef __MORPHOS__
if( AslBase = IExec->OpenLibrary( "asl.library", 52 ) ) {
if( IAsl = ( struct AslIFace * ) IExec->GetInterface( AslBase,"main",1,NULL ) ) {
filereq = IAsl->AllocAslRequestTags( ASL_FileRequest,
ASLFR_RejectIcons,TRUE,
ASLFR_TitleText,title,
ASLFR_DoSaveMode,is_saving,
ASLFR_InitialPattern,"#?.(sna|z80|szx|sp|snp|zxs|tap|tzx|csw|rzx|dsk|trd|scl|mdr|dck|hdf|rom|psg|scr|png|gz|bz2)",
ASLFR_DoPatterns,TRUE,
TAG_DONE );
if( err = IAsl->AslRequest( filereq, NULL ) ) {
filename = ( STRPTR ) IExec->AllocVec( 1024, MEMF_CLEAR );
#else /* #ifndef __MORPHOS__ */
if( AslBase = OpenLibrary( "asl.library", 0 ) ) {
filereq = AllocAslRequestTags( ASL_FileRequest,
ASLFR_RejectIcons,TRUE,
ASLFR_TitleText,title,
ASLFR_DoSaveMode,is_saving,
ASLFR_InitialPattern,"#?.(sna|z80|szx|sp|snp|zxs|tap|tzx|csw|rzx|dsk|trd|scl|mdr|dck|hdf|rom|psg|scr|png|gz|bz2)",
ASLFR_DoPatterns,TRUE,
TAG_DONE );
if( err = AslRequest( filereq, NULL ) ) {
filename = ( STRPTR ) AllocVec( 1024, MEMF_CLEAR );
#endif /* #ifndef __MORPHOS__ */
strcpy( filename,filereq->fr_Drawer );
#ifndef __MORPHOS__
IDOS->AddPart( filename, filereq->fr_File, 1024 );
#else /* #ifndef __MORPHOS__ */
AddPart( filename, filereq->fr_File, 1024 );
#endif /* #ifndef __MORPHOS__ */
widget_filesel_name = strdup( filename );
#ifndef __MORPHOS__
IExec->FreeVec( filename );
#else /* #ifndef __MORPHOS__ */
FreeVec( filename );
#endif /* #ifndef __MORPHOS__ */
err = WIDGET_FINISHED_OK;
} else {
err = WIDGET_FINISHED_CANCEL;
}
#ifndef __MORPHOS__
IExec->DropInterface( ( struct Interface * )IAsl );
}
IExec->CloseLibrary( AslBase );
#else /* #ifndef __MORPHOS__ */
CloseLibrary( AslBase );
#endif /* #ifndef __MORPHOS__ */
}
return widget_filesel_name;
}
#else /* ifdef AMIGA */
static int widget_scandir( const char *dir, struct widget_dirent ***namelist,
int (*select_fn)(const char*) )
{
compat_dir directory;
int allocated, number;
int i;
int done = 0;
*namelist = malloc( 32 * sizeof(**namelist) );
if( !*namelist ) return -1;
allocated = 32; number = 0;
directory = compat_opendir( dir );
if( !directory ) {
free( *namelist );
*namelist = NULL;
return -1;
}
#ifdef WIN32
/* Assume this is the root directory, unless we find an entry named ".." */
is_rootdir = 1;
#endif /* #ifdef WIN32 */
while( !done ) {
#ifndef WIN32
char name[ NAME_MAX + 1 ];
#else /* #ifndef WIN32 */
/* mingw's dirent implementation doesn't have NAME_MAX */
char name[ FILENAME_MAX ];
#endif /* #ifndef WIN32 */
compat_dir_result_t result = compat_readdir( directory, name, sizeof( name ) );
switch( result )
{
case COMPAT_DIR_RESULT_OK:
if( select_fn( name ) ) {
#ifdef WIN32
if( is_rootdir && !strcmp( name, ".." ) ) {
is_rootdir = 0;
}
#endif /* #ifdef WIN32 */
if( widget_add_filename( &allocated, &number, namelist, name ) ) {
compat_closedir( directory );
return -1;
}
}
break;
case COMPAT_DIR_RESULT_END:
done = 1;
break;
case COMPAT_DIR_RESULT_ERROR:
for( i=0; i<number; i++ ) {
free( (*namelist)[i]->name );
free( (*namelist)[i] );
}
free( *namelist );
*namelist = NULL;
compat_closedir( directory );
return -1;
}
}
if( compat_closedir( directory ) ) {
for( i=0; i<number; i++ ) {
free( (*namelist)[i]->name );
free( (*namelist)[i] );
}
free( *namelist );
*namelist = NULL;
return -1;
}
#ifdef WIN32
if( is_rootdir ) {
/* Add a fake ".." entry for drive selection */
if( widget_add_filename( &allocated, &number, namelist, ".." ) ) {
return -1;
}
}
#endif /* #ifdef WIN32 */
return number;
}
#ifdef WIN32
static int widget_scandrives( struct widget_dirent ***namelist )
{
int allocated, number;
unsigned long drivemask;
int i;
char drive[3];
char *driveletters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
drive[1] = ':';
drive[2] = '\0';
*namelist = malloc( 32 * sizeof(**namelist) );
if( !*namelist ) return -1;
allocated = 32; number = 0;
drivemask = _getdrives();
if( !drivemask ) {
free( *namelist );
*namelist = NULL;
return -1;
}
for( i = 0; i < 26; i++ ) {
if( drivemask & 1) {
drive[0] = driveletters[i];
if( widget_add_filename( &allocated, &number, namelist, drive ) ) {
return -1;
}
}
drivemask >>= 1;
}
return number;
}
#endif
static void widget_scan( char *dir )
{
struct stat file_info;
size_t i; int error;
/* Free the memory belonging to the files in the previous directory */
for( i=0; i<widget_numfiles; i++ ) {
free( widget_filenames[i]->name );
free( widget_filenames[i] );
}
#ifdef WIN32
if( dir ) {
widget_numfiles = widget_scandir( dir, &widget_filenames,
widget_select_file );
} else {
widget_numfiles = widget_scandrives( &widget_filenames );
}
#else /* #ifdef WIN32 */
widget_numfiles = widget_scandir( dir, &widget_filenames,
widget_select_file );
#endif /* #ifdef WIN32 */
if( widget_numfiles == (size_t)-1 ) return;
for( i=0; i<widget_numfiles; i++ ) {
error = stat( widget_filenames[i]->name, &file_info );
widget_filenames[i]->mode = error ? 0 : file_info.st_mode;
}
qsort( widget_filenames, widget_numfiles, sizeof(struct widget_dirent*),
(int(*)(const void*,const void*))widget_scan_compare );
}
static int
widget_select_file( const char *name )
{
return( name && strcmp( name, "." ) );
}
static int widget_scan_compare( const struct widget_dirent **a,
const struct widget_dirent **b )
{
int isdir1 = S_ISDIR( (*a)->mode ),
isdir2 = S_ISDIR( (*b)->mode );
if( isdir1 && !isdir2 ) {
return -1;
} else if( isdir2 && !isdir1 ) {
return 1;
} else {
return strcmp( (*a)->name, (*b)->name );
}
}
#endif /* ifdef AMIGA */
/* File selection widget */
static int
widget_filesel_draw( void *data )
{
widget_filesel_data *filesel_data = data;
char *directory;
int error;
exit_all_widgets = filesel_data->exit_all_widgets;
title = filesel_data->title;
#if !defined AMIGA && !defined __MORPHOS__
#ifdef WIN32
if( !is_drivesel ) {
directory = widget_getcwd();
if( directory == NULL ) return 1;
} else {
directory = NULL;
}
#else /* #ifdef WIN32 */
directory = widget_getcwd();
if( directory == NULL ) return 1;
#endif /* #ifdef WIN32 */
widget_scan( directory );
new_current_file = current_file = 0;
top_left_file = 0;
/* Create the dialog box */
error = widget_dialog_with_border( 1, 2, 30, 22 );
if( error ) {
if( directory ) free( directory );
return error;
}
#ifdef WIN32
if( directory == NULL ) {
directory = "Drive selection";
}
#endif /* #ifdef WIN32 */
/* Show all the filenames */
widget_print_all_filenames( widget_filenames, widget_numfiles,
top_left_file, current_file, directory );
#endif /* ifndef AMIGA */
return 0;
}
int widget_filesel_finish( widget_finish_state finished ) {
/* Return with null if we didn't finish cleanly */
if( finished != WIDGET_FINISHED_OK ) {
if( widget_filesel_name ) free( widget_filesel_name );
widget_filesel_name = NULL;
}
return 0;
}
int
widget_filesel_load_draw( void *data )
{
is_saving = 0;
return widget_filesel_draw( data );
}
int
widget_filesel_save_draw( void *data )
{
is_saving = 1;
return widget_filesel_draw( data );
}
#if !defined AMIGA && !defined __MORPHOS__
static char* widget_getcwd( void )
{
char *directory; size_t directory_length;
char *ptr;
directory_length = 64;
directory = malloc( directory_length * sizeof( char ) );
if( directory == NULL ) {
return NULL;
}
do {
ptr = getcwd( directory, directory_length );
if( ptr ) break;
if( errno == ERANGE ) {
ptr = directory;
directory_length *= 2;
directory =
(char*)realloc( directory, directory_length * sizeof( char ) );
if( directory == NULL ) {
free( ptr );
return NULL;
}
} else {
free( directory );
return NULL;
}
} while(1);
#ifdef WIN32
if( directory[0] && directory[1] == ':' ) {
directory[0] = toupper( directory[0] );
}
#endif
return directory;
}
static int widget_print_all_filenames( struct widget_dirent **filenames, int n,
int top_left, int current,
const char *dir )
{
int i;
int error;
/* Give us a clean box to start with */
error = widget_dialog_with_border( 1, 2, 30, 22 );
if( error ) return error;
widget_printstring( 10, 16, WIDGET_COLOUR_TITLE, title );
if( widget_stringwidth( dir ) > 223 ) {
char buffer[128];
int prefix = widget_stringwidth( "..." ) + 1;
while( widget_stringwidth( dir ) > 223 - prefix ) dir++;
snprintf( buffer, sizeof( buffer ), "...%s", dir );
widget_print_title( 24, WIDGET_COLOUR_FOREGROUND, buffer );
} else {
widget_print_title( 24, WIDGET_COLOUR_FOREGROUND, dir );
}
if( top_left ) widget_up_arrow( 1, 5, WIDGET_COLOUR_FOREGROUND );
/* Print the filenames, mostly normally, but with the currently
selected file inverted */
for( i = top_left; i < n && i < top_left + PAGESIZE; i++ ) {
if( i == current ) {
widget_print_filename( filenames[i], i-top_left, 1 );
} else {
widget_print_filename( filenames[i], i-top_left, 0 );
}
}
if( is_saving )
{
widget_printstring( 12, 22 * 8, WIDGET_COLOUR_FOREGROUND,
"\012RETURN\001 = select" );
widget_printstring_right( 244, 22 * 8, WIDGET_COLOUR_FOREGROUND,
"\012TAB\001 = enter name" );
}
if( i < n )
widget_down_arrow( 1, is_saving ? 20 : 22, WIDGET_COLOUR_FOREGROUND );
/* Display that lot */
widget_display_lines( 2, 22 );
return 0;
}
/* Print a filename onto the dialog box */
static int widget_print_filename( struct widget_dirent *filename, int position,
int inverted )
{
char buffer[64], suffix[64], *dot = 0;
int width, suffix_width = 0;
int dir = S_ISDIR( filename->mode );
int truncated = 0, suffix_truncated = 0;
#define FILENAME_WIDTH 112
#define MAX_SUFFIX_WIDTH 56
int x = (position & 1) ? 132 : 16,
y = 40 + (position >> 1) * 8;
int foreground = WIDGET_COLOUR_FOREGROUND,
background = inverted ? WIDGET_COLOUR_HIGHLIGHT
: WIDGET_COLOUR_BACKGROUND;
widget_rectangle( x, y, FILENAME_WIDTH, 8, background );
strncpy( buffer, filename->name, sizeof( buffer ) - dir - 1);
buffer[sizeof( buffer ) - dir - 1] = '\0';
if (dir)
dir = widget_charwidth( FUSE_DIR_SEP_CHR );
else {
/* get filename extension */
dot = strrchr( filename->name, '.' );
/* if .gz or .bz2, we want the previous component too */
if( dot &&( !strcasecmp( dot, ".gz" ) || !strcasecmp( dot, ".bz2" ) ) ) {
char *olddot = dot;
*olddot = '\0';
dot = strrchr( filename->name, '.' );
*olddot = '.';
if (!dot)
dot = olddot;
}
/* if the dot is at the start of the name, ignore it */
if( dot == filename->name )
dot = 0;
}
if( dot ) {
/* split filename at extension separator */
if( dot - filename->name < sizeof( buffer ) )
buffer[dot - filename->name] = '\0';
/* get extension width (for display purposes) */
snprintf( suffix, sizeof( suffix ), "%s", dot );
while( ( suffix_width = ( dot && !dir )
? widget_stringwidth( suffix ) : 0 ) > 110 ) {
suffix_truncated = 1;
suffix[strlen( suffix ) - 1] = '\0';
}
}
while( ( width = widget_stringwidth( buffer ) ) >=
FILENAME_WIDTH - dir - ( dot ? truncated + suffix_width : 0 ) ) {
truncated = 2;
if( suffix_width >= MAX_SUFFIX_WIDTH ) {
suffix_truncated = 2;
suffix[strlen (suffix) - 1] = '\0';
suffix_width = widget_stringwidth (suffix);
}
else
buffer[strlen (buffer) - 1] = '\0';
}
if( dir )
strcat (buffer, FUSE_DIR_SEP_STR );
widget_printstring( x + 1, y, foreground, buffer );
if( truncated )
widget_rectangle( x + width + 2, y, 1, 8, 4 );
if( dot )
widget_printstring( x + width + 2 + truncated, y,
foreground ^ 2, suffix );
if( suffix_truncated )
widget_rectangle( x + FILENAME_WIDTH, y, 1, 8, 4 );
return 0;
}
#endif /* ifndef AMIGA */
#ifdef WIN32
static void
widget_filesel_chdrv( void )
{
char *fn;
if( chdir( widget_filenames[ current_file ]->name ) ) {
ui_error( UI_ERROR_ERROR, "Could not change directory" );
return;
}
is_drivesel = 0;
fn = widget_getcwd();
widget_scan( fn ); free( fn );
new_current_file = 0;
/* Force a redisplay of all filenames */
current_file = 1; top_left_file = 1;
}
static void
widget_filesel_drvlist( void )
{
is_drivesel = 1;
widget_scan( NULL );
new_current_file = 0;
/* Force a redisplay of all filenames */
current_file = 1; top_left_file = 1;
}
#endif /* #ifdef WIN32 */
static int
widget_filesel_chdir( void )
{
char *fn, *ptr;
/* Get the new directory name */
fn = widget_getcwd();
if( fn == NULL ) {
widget_end_widget( WIDGET_FINISHED_CANCEL );
return 1;
}
ptr = fn;
fn = realloc( fn,
( strlen( fn ) + 1 + strlen( widget_filenames[ current_file ]->name ) +
1 ) * sizeof(char)
);
if( fn == NULL ) {
free( ptr );
widget_end_widget( WIDGET_FINISHED_CANCEL );
return 1;
}
#ifndef GEKKO
/* Wii getcwd() already has the slash on the end */
strcat( fn, FUSE_DIR_SEP_STR );
#endif /* #ifndef GEKKO */
strcat( fn, widget_filenames[ current_file ]->name );
/*
in Win32 errno resulting from chdir on file is EINVAL which may mean many things
this will not be fixed in mingw - must use native function instead
http://thread.gmane.org/gmane.comp.gnu.mingw.user/9197
*/
if( chdir( fn ) == -1 ) {
#ifndef WIN32
if( errno == ENOTDIR ) {
#else /* #ifndef WIN32 */
if( GetFileAttributes( fn ) != FILE_ATTRIBUTE_DIRECTORY ) {
#endif /* #ifndef WIN32 */
widget_filesel_name = fn;
if( exit_all_widgets ) {
widget_end_all( WIDGET_FINISHED_OK );
} else {
widget_end_widget( WIDGET_FINISHED_OK );
}
}
} else {
widget_scan( fn ); free( fn );
new_current_file = 0;
/* Force a redisplay of all filenames */
current_file = 1; top_left_file = 1;
}
return 0;
}
void
widget_filesel_keyhandler( input_key key )
{
/* If there are no files (possible on the Wii), can't really do anything */
if( widget_numfiles == 0 ) {
if( key == INPUT_KEY_Escape ) widget_end_widget( WIDGET_FINISHED_CANCEL );
return;
}
#if defined AMIGA || defined __MORPHOS__
if( exit_all_widgets ) {
widget_end_all( err );
} else {
widget_end_widget( err );
}
#else /* ifndef AMIGA */
char *fn;
char *dirtitle;
new_current_file = current_file;
switch(key) {
#if 0
case INPUT_KEY_Resize: /* Fake keypress used on window resize */
widget_dialog_with_border( 1, 2, 30, 20 );
widget_print_all_filenames( widget_filenames, widget_numfiles,
top_left_file, current_file );
break;
#endif
case INPUT_KEY_Escape:
case INPUT_JOYSTICK_FIRE_2:
widget_end_widget( WIDGET_FINISHED_CANCEL );
break;
case INPUT_KEY_Left:
case INPUT_KEY_5:
case INPUT_KEY_h:
case INPUT_JOYSTICK_LEFT:
if( current_file > 0 ) new_current_file--;
break;
case INPUT_KEY_Down:
case INPUT_KEY_6:
case INPUT_KEY_j:
case INPUT_JOYSTICK_DOWN:
if( current_file+2 < widget_numfiles ) new_current_file += 2;
break;
case INPUT_KEY_Up:
case INPUT_KEY_7: /* Up */
case INPUT_KEY_k:
case INPUT_JOYSTICK_UP:
if( current_file > 1 ) new_current_file -= 2;
break;
case INPUT_KEY_Right:
case INPUT_KEY_8:
case INPUT_KEY_l:
case INPUT_JOYSTICK_RIGHT:
if( current_file < widget_numfiles-1 ) new_current_file++;
break;
case INPUT_KEY_Page_Up:
new_current_file = ( current_file > PAGESIZE ) ?
current_file - PAGESIZE :
0;
break;
case INPUT_KEY_Page_Down:
new_current_file = current_file + PAGESIZE;
if( new_current_file >= widget_numfiles )
new_current_file = widget_numfiles - 1;
break;
case INPUT_KEY_Home:
new_current_file = 0;
break;
case INPUT_KEY_End:
new_current_file = widget_numfiles - 1;
break;
case INPUT_KEY_Tab:
if( is_saving ) {
widget_text_t text_data;
text_data.title = title;
text_data.allow = WIDGET_INPUT_ASCII;
text_data.text[0] = 0;
if( widget_do( WIDGET_TYPE_TEXT, &text_data ) ||
!widget_text_text || !*widget_text_text )
break;
if( !compat_is_absolute_path( widget_text_text ) ) {
/* relative name */
/* Get current dir name and allocate space for the leafname */
fn = widget_getcwd();
if( fn )
fn = realloc( fn, strlen( fn ) + strlen( widget_text_text ) + 2 );
if( !fn ) {
widget_end_widget( WIDGET_FINISHED_CANCEL );
return;
}
/* Append the leafname and return it */
strcat( fn, FUSE_DIR_SEP_STR );
strcat( fn, widget_text_text );
} else { /* absolute name */
fn = strdup( widget_text_text );
if( !fn ) {
widget_end_widget( WIDGET_FINISHED_CANCEL );
return;
}
}
widget_filesel_name = fn;
if( exit_all_widgets ) {
widget_end_all( WIDGET_FINISHED_OK );
} else {
widget_end_widget( WIDGET_FINISHED_OK );
}
}
break;
case INPUT_KEY_Return:
case INPUT_KEY_KP_Enter:
case INPUT_JOYSTICK_FIRE_1:
#ifdef WIN32
if( is_drivesel ) {
widget_filesel_chdrv();
} else if( is_rootdir &&
!strcmp( widget_filenames[ current_file ]->name, ".." ) ) {
widget_filesel_drvlist();
} else {
#endif /* #ifdef WIN32 */
if( widget_filesel_chdir() ) return;
#ifdef WIN32
}
#endif /* #ifdef WIN32 */
break;
default: /* Keep gcc happy */
break;
}
#ifdef WIN32
if( is_drivesel ) {
dirtitle = "Drive selection";
} else {
#endif /* #ifdef WIN32 */
dirtitle = widget_getcwd();
#ifdef WIN32
}
#endif /* #ifdef WIN32 */
/* If we moved the cursor */
if( new_current_file != current_file ) {
/* If we've got off the top or bottom of the currently displayed
file list, then reset the top-left corner and display the whole
thing */
if( new_current_file < top_left_file ) {
top_left_file = new_current_file & ~1;
widget_print_all_filenames( widget_filenames, widget_numfiles,
top_left_file, new_current_file, dirtitle );
} else if( new_current_file >= top_left_file+PAGESIZE ) {
top_left_file = new_current_file & ~1; top_left_file -= PAGESIZE - 2;
widget_print_all_filenames( widget_filenames, widget_numfiles,
top_left_file, new_current_file, dirtitle );
} else {
/* Otherwise, print the current file uninverted and the
new current file inverted */
widget_print_filename( widget_filenames[ current_file ],
current_file - top_left_file, 0 );
widget_print_filename( widget_filenames[ new_current_file ],
new_current_file - top_left_file, 1 );
widget_display_lines( 2, 21 );
}
/* Reset the current file marker */
current_file = new_current_file;
}
#endif /* ifdef AMIGA */
}
|