5 * by JH <jheinonen@bigfoot.com>
7 * Copyright (C) Jaakko Heinonen
13 /*#undef USE_FILESEL*/
22 # include "abook_curses.h"
32 #if defined(HAVE_LOCALE_H) || defined(HAVE_SETLOCALE)
36 * this is a quick and dirty hack and it is
37 * not likely to work well
41 # define iscntrl(X) (X < 32)
42 # define isprint(X) (!iscntrl(X))
46 * hmmm, these should be universal and nice :)
49 #define XCTRL(x) ((x) & 31)
51 #define ENTER_KEY KEY_ENTER
52 #define BACKSPACE_KEY KEY_BACKSPACE
53 #define CANCEL_KEY XCTRL('g')
58 # define MALLOC(X) abook_malloc(X)
59 # define REALLOC(X, XX) abook_realloc(X, XX)
61 # define MALLOC(X) malloc(X)
62 # define REALLOC(X, XX) realloc(X, XX)
65 /* introduce filesel */
70 #define INITIAL_BUFSIZE 100
73 wenter_string(WINDOW *win, const int maxlen, const int flags)
79 int size = maxlen > 0 ? maxlen + 1 : INITIAL_BUFSIZE;
84 for( ;; ) { /* main loop */
85 if(flags & ESTR_DONT_WRAP && x+i>COLS-2) {
86 mvwaddnstr(win, y, x, str+(1+x+i-COLS), COLS-x-1);
87 wmove(win, y, COLS-1);
94 switch( (ch = getch()) ) {
102 mvwdelch(win, y, x + --i);
109 if( ! (flags & ESTR_USE_FILESEL) )
117 if( !isprint(ch) || iscntrl(ch)
118 || (maxlen > 0 && i + 1 > maxlen) )
123 str = REALLOC( str, size *= 2 );
124 if( maxlen > 0 && i > maxlen)
128 if( i >= 0 && str != NULL )
139 * by JH <jheinonen@bigfoot.com>
141 * Copyright (C) Jaakko Heinonen
147 #include <sys/stat.h>
151 #define FILESEL_STATUSLINE 2
153 #define FILESEL_LIST_TOP 4
154 #define FILESEL_LIST_BOTTOM (LINES-2)
156 #define FILESEL_LIST_LINES (FILESEL_LIST_BOTTOM-FILESEL_LIST_TOP)
157 #define FILESEL_LIST_COLS COLS
159 #define FILESEL_LAST_LIST_ITEM ( filesel_first_list_item + FILESEL_LIST_LINES - 1 )
162 #define FLSL_TYPE_DIR 0
163 #define FLSL_TYPE_OTHER 1
165 struct filesel_list_item {
182 while( getcwd(dir, size) == NULL && errno == ERANGE )
183 dir = REALLOC(dir, size *= 2);
193 #define FILESEL_LAST_ITEM (filesel_items -1)
195 int filesel_curitem = -1;
196 int filesel_first_list_item = -1;
199 struct filesel_list_item *lst = NULL ;
201 int filesel_list_capacity = 0;
203 WINDOW *filesel_list;
211 filesel_first_list_item = filesel_curitem = -1;
212 filesel_list_capacity = 0;
218 filesel_list = newwin(FILESEL_LIST_LINES, FILESEL_LIST_COLS,
219 FILESEL_LIST_TOP, 0);
231 intrflush(stdscr, FALSE);
232 keypad(stdscr, TRUE);
240 filesel_close_list();
241 delwin(filesel_list);
250 if( filesel_curitem < 0 && filesel_first_list_item < 0 && filesel_items > 0 )
251 filesel_curitem = filesel_first_list_item = 0;
256 filesel_add_filesel_list_item(struct filesel_list_item item)
258 if( ++filesel_items > filesel_list_capacity) {
259 filesel_list_capacity =
260 filesel_list_capacity < 1 ? 30:filesel_list_capacity << 1;
261 lst = REALLOC(lst, filesel_list_capacity *
262 sizeof(struct filesel_list_item) );
265 lst[FILESEL_LAST_ITEM] = item;
267 filesel_check_list();
273 #define FILESEL_DIRS_FIRST
274 #define FILESEL_ALPHABETIC_ORDER
276 #ifdef FILESEL_ALPHABETIC_ORDER
277 # ifndef FILESEL_DIRS_FIRST
278 # define FILESEL_DIRS_FIRST
282 #ifdef FILESEL_ALPHABETIC_ORDER
284 filenamecmp(const void *a, const void *b)
286 return strcmp( ((struct filesel_list_item *)a) -> filename,
287 ((struct filesel_list_item *)b) -> filename );
294 #ifdef FILESEL_DIRS_FIRST
296 struct filesel_list_item tmp;
298 while(lst[fdp].type == FLSL_TYPE_DIR)
299 if( ++fdp >= FILESEL_LAST_ITEM )
302 for(i = fdp + 1; i < filesel_items; i++) {
303 if(lst[i].type == FLSL_TYPE_DIR ) {
306 for( j = i; j > fdp ; j--)
312 #ifdef FILESEL_ALPHABETIC_ORDER
314 if( options_get_int("filesel_sort") ) {
317 qsort((void *)lst, fdp, sizeof(struct filesel_list_item),
320 qsort((void *)lst + fdp * sizeof(struct filesel_list_item),
321 FILESEL_LAST_ITEM - fdp + 1,
322 sizeof(struct filesel_list_item),
332 filesel_refresh_statusline()
336 move(FILESEL_STATUSLINE,0);
339 mvaddnstr(FILESEL_STATUSLINE, 5,
340 ( p = my_getcwd() ), COLS-5 );
346 filesel_highlight_line(WINDOW *win, int line)
350 wattron(win, A_STANDOUT);
353 for(i = 0; i < FILESEL_LIST_COLS; i++)
358 filesel_print_list_line(int i, int line)
360 if( lst[i].type == FLSL_TYPE_DIR )
361 wattron(filesel_list, A_BOLD);
363 mvwaddnstr(filesel_list, line, 5, lst[i].filename, COLS - 5);
367 filesel_refresh_list()
371 werase(filesel_list);
373 if( filesel_first_list_item < 0 || filesel_items < 1 ) {
375 wrefresh(filesel_list);
379 for( line = 0, i = filesel_first_list_item ;
380 i <= FILESEL_LAST_LIST_ITEM && i < filesel_items; line++, i++ ) {
382 if(i == filesel_curitem) {
383 filesel_highlight_line(filesel_list, line);
384 wattron(filesel_list, A_STANDOUT);
386 wattrset(filesel_list, 0);
388 filesel_print_list_line(i, line);
392 wrefresh(filesel_list);
398 if( filesel_curitem < 1 )
402 if( filesel_first_list_item > 0 && filesel_curitem < filesel_first_list_item )
403 filesel_first_list_item--;
405 filesel_refresh_list();
409 filesel_scroll_down()
411 if( filesel_curitem > filesel_items - 2 )
415 if( filesel_curitem > FILESEL_LAST_LIST_ITEM )
416 filesel_first_list_item++;
418 filesel_refresh_list();
425 if( filesel_curitem < 1 )
428 if( filesel_curitem == filesel_first_list_item ) {
429 filesel_curitem = (filesel_curitem -= FILESEL_LIST_LINES) < 0 ? 0 : filesel_curitem;
430 filesel_first_list_item = filesel_curitem;
432 filesel_curitem = filesel_first_list_item;
434 filesel_refresh_list();
440 if( filesel_curitem > filesel_items - 2 )
443 if( filesel_curitem == FILESEL_LAST_LIST_ITEM ) {
444 filesel_curitem = (filesel_curitem += FILESEL_LIST_LINES) > FILESEL_LAST_ITEM ?
445 FILESEL_LAST_ITEM : filesel_curitem;
446 filesel_first_list_item = filesel_curitem - FILESEL_LIST_LINES + 1;
447 if( filesel_first_list_item < 0 )
448 filesel_first_list_item = 0;
450 filesel_curitem = FILESEL_LAST_LIST_ITEM < FILESEL_LAST_ITEM ?
451 FILESEL_LAST_LIST_ITEM : FILESEL_LAST_ITEM;
453 filesel_refresh_list();
460 if(filesel_items > 0) {
461 filesel_first_list_item = 0;
464 filesel_refresh_list();
470 if(filesel_items > 0) {
471 filesel_curitem = FILESEL_LAST_ITEM;
472 filesel_first_list_item = filesel_curitem - FILESEL_LIST_LINES + 1;
473 if( filesel_first_list_item < 0 )
474 filesel_first_list_item = 0;
476 filesel_refresh_list();
484 struct dirent *entry;
486 struct filesel_list_item item;
490 entry = readdir(dir);
495 stat(entry->d_name, &st);
496 strcpy(item.filename, entry->d_name);
497 item.type = S_ISDIR(st.st_mode) ?
498 FLSL_TYPE_DIR : FLSL_TYPE_OTHER;
499 filesel_add_filesel_list_item(item);
504 filesel_check_list();
505 filesel_refresh_statusline();
506 filesel_refresh_list();
516 if(lst[filesel_curitem].type == FLSL_TYPE_DIR) {
518 newdir = MALLOC(strlen(dir)+strlen(lst[filesel_curitem].filename) +2 );
521 strcat(newdir,lst[filesel_curitem].filename);
523 if( (chdir(newdir)) != -1) {
524 filesel_close_list();
534 filesel_goto_item(int ch)
538 for(i=filesel_curitem+1; i<filesel_items; i++)
539 if( lst[i].filename[0] == ch ) {
544 if( filesel_curitem > FILESEL_LAST_LIST_ITEM)
545 filesel_first_list_item=filesel_curitem;
547 filesel_refresh_list();
557 switch( (ch = getch()) ) {
560 case 'R': filesel_close_list(); filesel_read_dir();
563 case KEY_UP: filesel_scroll_up(); break;
565 case KEY_DOWN: filesel_scroll_down(); break;
567 case KEY_PPAGE: filesel_page_up(); break;
569 case KEY_NPAGE: filesel_page_down(); break;
571 case KEY_HOME: filesel_goto_home(); break;
572 case KEY_END: filesel_goto_end(); break;
575 case '\r': if( filesel_enter() ) {
579 default: filesel_goto_item(ch);
587 char *dir, *tmp, *ptr = NULL;
592 if( !filesel_loop() ) {
594 tmp = MALLOC(strlen(dir) + strlen(lst[filesel_curitem].filename) + 2);
597 strcat(tmp, lst[filesel_curitem].filename);
608 #endif /* USE_FILESEL */