6 * by JH <jheinonen@bigfoot.com>
8 * Copyright (C) Jaakko Heinonen
14 /*#undef USE_FILESEL*/
23 # include "abook_curses.h"
33 #if defined(HAVE_LOCALE_H) || defined(HAVE_SETLOCALE)
37 * this is a quick and dirty hack and it is
38 * not likely to work well
42 # define iscntrl(X) (X < 32)
43 # define isprint(X) (!iscntrl(X))
47 * hmmm, these should be universal and nice :)
50 #define XCTRL(x) ((x) & 31)
52 #define ENTER_KEY KEY_ENTER
53 #define BACKSPACE_KEY KEY_BACKSPACE
54 #define CANCEL_KEY XCTRL('g')
59 # define MALLOC(X) abook_malloc(X)
60 # define REALLOC(X, XX) abook_realloc(X, XX)
62 # define MALLOC(X) malloc(X)
63 # define REALLOC(X, XX) realloc(X, XX)
66 /* introduce filesel */
71 #define INITIAL_BUFSIZE 100
74 wenter_string(WINDOW *win, const int maxlen, const int flags)
80 int size = maxlen > 0 ? maxlen + 1 : INITIAL_BUFSIZE;
85 for( ;; ) { /* main loop */
86 if(flags & ESTR_DONT_WRAP && x+i>COLS-2) {
87 mvwaddnstr(win, y, x, str+(1+x+i-COLS), COLS-x-1);
88 wmove(win, y, COLS-1);
95 switch( (ch = getch()) ) {
103 mvwdelch(win, y, x + --i);
110 if( ! (flags & ESTR_USE_FILESEL) )
118 if( !isprint(ch) || iscntrl(ch)
119 || (maxlen > 0 && i + 1 > maxlen) )
124 str = REALLOC( str, size *= 2 );
125 if( maxlen > 0 && i > maxlen)
129 if( i >= 0 && str != NULL )
140 * by JH <jheinonen@bigfoot.com>
142 * Copyright (C) Jaakko Heinonen
148 #include <sys/stat.h>
152 #define FILESEL_STATUSLINE 2
154 #define FILESEL_LIST_TOP 4
155 #define FILESEL_LIST_BOTTOM (LINES-2)
157 #define FILESEL_LIST_LINES (FILESEL_LIST_BOTTOM-FILESEL_LIST_TOP)
158 #define FILESEL_LIST_COLS COLS
160 #define FILESEL_LAST_LIST_ITEM ( filesel_first_list_item + FILESEL_LIST_LINES - 1 )
163 #define FLSL_TYPE_DIR 0
164 #define FLSL_TYPE_OTHER 1
166 struct filesel_list_item {
183 while( getcwd(dir, size) == NULL && errno == ERANGE )
184 dir = REALLOC(dir, size *= 2);
194 #define FILESEL_LAST_ITEM (filesel_items -1)
196 int filesel_curitem = -1;
197 int filesel_first_list_item = -1;
200 struct filesel_list_item *lst = NULL ;
202 int filesel_list_capacity = 0;
204 WINDOW *filesel_list;
212 filesel_first_list_item = filesel_curitem = -1;
213 filesel_list_capacity = 0;
219 filesel_list = newwin(FILESEL_LIST_LINES, FILESEL_LIST_COLS,
220 FILESEL_LIST_TOP, 0);
232 intrflush(stdscr, FALSE);
233 keypad(stdscr, TRUE);
241 filesel_close_list();
242 delwin(filesel_list);
251 if( filesel_curitem < 0 && filesel_first_list_item < 0 && filesel_items > 0 )
252 filesel_curitem = filesel_first_list_item = 0;
257 filesel_add_filesel_list_item(struct filesel_list_item item)
259 if( ++filesel_items > filesel_list_capacity) {
260 filesel_list_capacity =
261 filesel_list_capacity < 1 ? 30:filesel_list_capacity << 1;
262 lst = REALLOC(lst, filesel_list_capacity *
263 sizeof(struct filesel_list_item) );
266 lst[FILESEL_LAST_ITEM] = item;
268 filesel_check_list();
274 #define FILESEL_DIRS_FIRST
275 #define FILESEL_ALPHABETIC_ORDER
277 #ifdef FILESEL_ALPHABETIC_ORDER
278 # ifndef FILESEL_DIRS_FIRST
279 # define FILESEL_DIRS_FIRST
283 #ifdef FILESEL_ALPHABETIC_ORDER
285 filenamecmp(const void *a, const void *b)
287 return strcmp( ((struct filesel_list_item *)a) -> filename,
288 ((struct filesel_list_item *)b) -> filename );
295 #ifdef FILESEL_DIRS_FIRST
297 struct filesel_list_item tmp;
299 while(lst[fdp].type == FLSL_TYPE_DIR)
300 if( ++fdp >= FILESEL_LAST_ITEM )
303 for(i = fdp + 1; i < filesel_items; i++) {
304 if(lst[i].type == FLSL_TYPE_DIR ) {
307 for( j = i; j > fdp ; j--)
313 #ifdef FILESEL_ALPHABETIC_ORDER
315 if( options_get_int("filesel_sort") ) {
318 qsort((void *)lst, fdp, sizeof(struct filesel_list_item),
321 qsort((void *)lst + fdp * sizeof(struct filesel_list_item),
322 FILESEL_LAST_ITEM - fdp + 1,
323 sizeof(struct filesel_list_item),
333 filesel_refresh_statusline()
337 move(FILESEL_STATUSLINE,0);
340 mvaddnstr(FILESEL_STATUSLINE, 5,
341 ( p = my_getcwd() ), COLS-5 );
347 filesel_highlight_line(WINDOW *win, int line)
351 wattron(win, A_STANDOUT);
354 for(i = 0; i < FILESEL_LIST_COLS; i++)
359 filesel_print_list_line(int i, int line)
361 if( lst[i].type == FLSL_TYPE_DIR )
362 wattron(filesel_list, A_BOLD);
364 mvwaddnstr(filesel_list, line, 5, lst[i].filename, COLS - 5);
368 filesel_refresh_list()
372 werase(filesel_list);
374 if( filesel_first_list_item < 0 || filesel_items < 1 ) {
376 wrefresh(filesel_list);
380 for( line = 0, i = filesel_first_list_item ;
381 i <= FILESEL_LAST_LIST_ITEM && i < filesel_items; line++, i++ ) {
383 if(i == filesel_curitem) {
384 filesel_highlight_line(filesel_list, line);
385 wattron(filesel_list, A_STANDOUT);
387 wattrset(filesel_list, 0);
389 filesel_print_list_line(i, line);
393 wrefresh(filesel_list);
399 if( filesel_curitem < 1 )
403 if( filesel_first_list_item > 0 && filesel_curitem < filesel_first_list_item )
404 filesel_first_list_item--;
406 filesel_refresh_list();
410 filesel_scroll_down()
412 if( filesel_curitem > filesel_items - 2 )
416 if( filesel_curitem > FILESEL_LAST_LIST_ITEM )
417 filesel_first_list_item++;
419 filesel_refresh_list();
426 if( filesel_curitem < 1 )
429 if( filesel_curitem == filesel_first_list_item ) {
430 filesel_curitem = (filesel_curitem -= FILESEL_LIST_LINES) < 0 ? 0 : filesel_curitem;
431 filesel_first_list_item = filesel_curitem;
433 filesel_curitem = filesel_first_list_item;
435 filesel_refresh_list();
441 if( filesel_curitem > filesel_items - 2 )
444 if( filesel_curitem == FILESEL_LAST_LIST_ITEM ) {
445 filesel_curitem = (filesel_curitem += FILESEL_LIST_LINES) > FILESEL_LAST_ITEM ?
446 FILESEL_LAST_ITEM : filesel_curitem;
447 filesel_first_list_item = filesel_curitem - FILESEL_LIST_LINES + 1;
448 if( filesel_first_list_item < 0 )
449 filesel_first_list_item = 0;
451 filesel_curitem = FILESEL_LAST_LIST_ITEM < FILESEL_LAST_ITEM ?
452 FILESEL_LAST_LIST_ITEM : FILESEL_LAST_ITEM;
454 filesel_refresh_list();
461 if(filesel_items > 0) {
462 filesel_first_list_item = 0;
465 filesel_refresh_list();
471 if(filesel_items > 0) {
472 filesel_curitem = FILESEL_LAST_ITEM;
473 filesel_first_list_item = filesel_curitem - FILESEL_LIST_LINES + 1;
474 if( filesel_first_list_item < 0 )
475 filesel_first_list_item = 0;
477 filesel_refresh_list();
485 struct dirent *entry;
487 struct filesel_list_item item;
491 entry = readdir(dir);
496 stat(entry->d_name, &st);
497 strcpy(item.filename, entry->d_name);
498 item.type = S_ISDIR(st.st_mode) ?
499 FLSL_TYPE_DIR : FLSL_TYPE_OTHER;
500 filesel_add_filesel_list_item(item);
505 filesel_check_list();
506 filesel_refresh_statusline();
507 filesel_refresh_list();
517 if(lst[filesel_curitem].type == FLSL_TYPE_DIR) {
519 newdir = MALLOC(strlen(dir)+strlen(lst[filesel_curitem].filename) +2 );
522 strcat(newdir,lst[filesel_curitem].filename);
524 if( (chdir(newdir)) != -1) {
525 filesel_close_list();
535 filesel_goto_item(int ch)
539 for(i=filesel_curitem+1; i<filesel_items; i++)
540 if( lst[i].filename[0] == ch ) {
545 if( filesel_curitem > FILESEL_LAST_LIST_ITEM)
546 filesel_first_list_item=filesel_curitem;
548 filesel_refresh_list();
558 switch( (ch = getch()) ) {
561 case 'R': filesel_close_list(); filesel_read_dir();
564 case KEY_UP: filesel_scroll_up(); break;
566 case KEY_DOWN: filesel_scroll_down(); break;
568 case KEY_PPAGE: filesel_page_up(); break;
570 case KEY_NPAGE: filesel_page_down(); break;
572 case KEY_HOME: filesel_goto_home(); break;
573 case KEY_END: filesel_goto_end(); break;
576 case '\r': if( filesel_enter() ) {
580 default: filesel_goto_item(ch);
588 char *dir, *tmp, *ptr = NULL;
593 if( !filesel_loop() ) {
595 tmp = MALLOC(strlen(dir) + strlen(lst[filesel_curitem].filename) + 2);
598 strcat(tmp, lst[filesel_curitem].filename);
609 #endif /* USE_FILESEL */