5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
12 /*#undef USE_FILESEL*/
21 # include "abook_curses.h"
31 #if defined(HAVE_LOCALE_H) || defined(HAVE_SETLOCALE)
35 * this is a quick and dirty hack and it is
36 * not likely to work well
40 # define iscntrl(X) (X < 32)
41 # define isprint(X) (!iscntrl(X))
45 * hmmm, these should be universal and nice :)
48 #define XCTRL(x) ((x) & 31)
50 #define ENTER_KEY KEY_ENTER
51 #define BACKSPACE_KEY KEY_BACKSPACE
52 #define CANCEL_KEY XCTRL('g')
57 # define MALLOC(X) abook_malloc(X)
58 # define REALLOC(X, XX) abook_realloc(X, XX)
60 # define MALLOC(X) malloc(X)
61 # define REALLOC(X, XX) realloc(X, XX)
64 /* introduce filesel */
69 #define INITIAL_BUFSIZE 100
72 wenter_string(WINDOW *win, const int maxlen, const int flags)
78 int size = maxlen > 0 ? maxlen + 1 : INITIAL_BUFSIZE;
83 for( ;; ) { /* main loop */
84 if(flags & ESTR_DONT_WRAP && x+i>COLS-2) {
85 mvwaddnstr(win, y, x, str+(1+x+i-COLS), COLS-x-1);
86 wmove(win, y, COLS-1);
93 switch( (ch = getch()) ) {
101 mvwdelch(win, y, x + --i);
108 if( ! (flags & ESTR_USE_FILESEL) )
116 if( !isprint(ch) || iscntrl(ch)
117 || (maxlen > 0 && i + 1 > maxlen) )
122 str = REALLOC( str, size *= 2 );
123 if( maxlen > 0 && i > maxlen)
127 if( i >= 0 && str != NULL )
138 * by JH <jheinonen@users.sourceforge.net>
140 * Copyright (C) Jaakko Heinonen
146 #include <sys/stat.h>
150 #define FILESEL_STATUSLINE 2
152 #define FILESEL_LIST_TOP 4
153 #define FILESEL_LIST_BOTTOM (LINES-2)
155 #define FILESEL_LIST_LINES (FILESEL_LIST_BOTTOM-FILESEL_LIST_TOP)
156 #define FILESEL_LIST_COLS COLS
158 #define FILESEL_LAST_LIST_ITEM ( filesel_first_list_item + FILESEL_LIST_LINES - 1 )
161 #define FLSL_TYPE_DIR 0
162 #define FLSL_TYPE_OTHER 1
164 struct filesel_list_item {
181 while( getcwd(dir, size) == NULL && errno == ERANGE )
182 dir = REALLOC(dir, size *= 2);
192 #define FILESEL_LAST_ITEM (filesel_items -1)
194 int filesel_curitem = -1;
195 int filesel_first_list_item = -1;
198 struct filesel_list_item *lst = NULL ;
200 int filesel_list_capacity = 0;
202 WINDOW *filesel_list;
210 filesel_first_list_item = filesel_curitem = -1;
211 filesel_list_capacity = 0;
217 filesel_list = newwin(FILESEL_LIST_LINES, FILESEL_LIST_COLS,
218 FILESEL_LIST_TOP, 0);
230 intrflush(stdscr, FALSE);
231 keypad(stdscr, TRUE);
239 filesel_close_list();
240 delwin(filesel_list);
249 if( filesel_curitem < 0 && filesel_first_list_item < 0 && filesel_items > 0 )
250 filesel_curitem = filesel_first_list_item = 0;
255 filesel_add_filesel_list_item(struct filesel_list_item item)
257 if( ++filesel_items > filesel_list_capacity) {
258 filesel_list_capacity =
259 filesel_list_capacity < 1 ? 30:filesel_list_capacity << 1;
260 lst = REALLOC(lst, filesel_list_capacity *
261 sizeof(struct filesel_list_item) );
264 lst[FILESEL_LAST_ITEM] = item;
266 filesel_check_list();
272 #define FILESEL_DIRS_FIRST
273 #define FILESEL_ALPHABETIC_ORDER
275 #ifdef FILESEL_ALPHABETIC_ORDER
276 # ifndef FILESEL_DIRS_FIRST
277 # define FILESEL_DIRS_FIRST
281 #ifdef FILESEL_ALPHABETIC_ORDER
283 filenamecmp(const void *a, const void *b)
285 return strcmp( ((struct filesel_list_item *)a) -> filename,
286 ((struct filesel_list_item *)b) -> filename );
293 #ifdef FILESEL_DIRS_FIRST
295 struct filesel_list_item tmp;
297 while(lst[fdp].type == FLSL_TYPE_DIR)
298 if( ++fdp >= FILESEL_LAST_ITEM )
301 for(i = fdp + 1; i < filesel_items; i++) {
302 if(lst[i].type == FLSL_TYPE_DIR ) {
305 for( j = i; j > fdp ; j--)
311 #ifdef FILESEL_ALPHABETIC_ORDER
313 if( options_get_int("filesel_sort") ) {
316 qsort((void *)lst, fdp, sizeof(struct filesel_list_item),
319 qsort((void *)(&lst[fdp]),
320 FILESEL_LAST_ITEM - fdp + 1,
321 sizeof(struct filesel_list_item),
331 filesel_refresh_statusline()
335 move(FILESEL_STATUSLINE,0);
338 mvaddnstr(FILESEL_STATUSLINE, 5,
339 ( p = my_getcwd() ), COLS-5 );
345 filesel_highlight_line(WINDOW *win, int line)
349 wattron(win, A_STANDOUT);
352 for(i = 0; i < FILESEL_LIST_COLS; i++)
357 filesel_print_list_line(int i, int line)
359 if( lst[i].type == FLSL_TYPE_DIR ) {
360 mvwaddch(filesel_list, line, 3, 'd');
362 wattron(filesel_list, A_BOLD);
366 mvwaddnstr(filesel_list, line, 5, lst[i].filename, COLS - 5);
370 filesel_refresh_list()
374 werase(filesel_list);
376 if( filesel_first_list_item < 0 || filesel_items < 1 ) {
378 wrefresh(filesel_list);
382 for( line = 0, i = filesel_first_list_item ;
383 i <= FILESEL_LAST_LIST_ITEM && i < filesel_items; line++, i++ ) {
385 if(i == filesel_curitem) {
386 filesel_highlight_line(filesel_list, line);
387 wattron(filesel_list, A_STANDOUT);
389 wattrset(filesel_list, 0);
391 filesel_print_list_line(i, line);
395 wrefresh(filesel_list);
401 if( filesel_curitem < 1 )
405 if( filesel_first_list_item > 0 && filesel_curitem < filesel_first_list_item )
406 filesel_first_list_item--;
408 filesel_refresh_list();
412 filesel_scroll_down()
414 if( filesel_curitem > filesel_items - 2 )
418 if( filesel_curitem > FILESEL_LAST_LIST_ITEM )
419 filesel_first_list_item++;
421 filesel_refresh_list();
428 if( filesel_curitem < 1 )
431 if( filesel_curitem == filesel_first_list_item ) {
432 filesel_curitem = (filesel_curitem -= FILESEL_LIST_LINES) < 0 ? 0 : filesel_curitem;
433 filesel_first_list_item = filesel_curitem;
435 filesel_curitem = filesel_first_list_item;
437 filesel_refresh_list();
443 if( filesel_curitem > filesel_items - 2 )
446 if( filesel_curitem == FILESEL_LAST_LIST_ITEM ) {
447 filesel_curitem = (filesel_curitem += FILESEL_LIST_LINES) > FILESEL_LAST_ITEM ?
448 FILESEL_LAST_ITEM : filesel_curitem;
449 filesel_first_list_item = filesel_curitem - FILESEL_LIST_LINES + 1;
450 if( filesel_first_list_item < 0 )
451 filesel_first_list_item = 0;
453 filesel_curitem = FILESEL_LAST_LIST_ITEM < FILESEL_LAST_ITEM ?
454 FILESEL_LAST_LIST_ITEM : FILESEL_LAST_ITEM;
456 filesel_refresh_list();
463 if(filesel_items > 0) {
464 filesel_first_list_item = 0;
467 filesel_refresh_list();
473 if(filesel_items > 0) {
474 filesel_curitem = FILESEL_LAST_ITEM;
475 filesel_first_list_item = filesel_curitem - FILESEL_LIST_LINES + 1;
476 if( filesel_first_list_item < 0 )
477 filesel_first_list_item = 0;
479 filesel_refresh_list();
487 struct dirent *entry;
489 struct filesel_list_item item;
493 entry = readdir(dir);
498 stat(entry->d_name, &st);
499 strcpy(item.filename, entry->d_name);
500 item.type = S_ISDIR(st.st_mode) ?
501 FLSL_TYPE_DIR : FLSL_TYPE_OTHER;
502 filesel_add_filesel_list_item(item);
507 filesel_check_list();
508 filesel_refresh_statusline();
509 filesel_refresh_list();
519 if(lst[filesel_curitem].type == FLSL_TYPE_DIR) {
521 newdir = MALLOC(strlen(dir)+strlen(lst[filesel_curitem].filename) +2 );
524 strcat(newdir,lst[filesel_curitem].filename);
526 if( (chdir(newdir)) != -1) {
527 filesel_close_list();
537 filesel_goto_item(int ch)
541 for(i=filesel_curitem+1; i<filesel_items; i++)
542 if( lst[i].filename[0] == ch ) {
547 if( filesel_curitem > FILESEL_LAST_LIST_ITEM)
548 filesel_first_list_item=filesel_curitem;
550 filesel_refresh_list();
560 switch( (ch = getch()) ) {
563 case 'R': filesel_close_list(); filesel_read_dir();
566 case KEY_UP: filesel_scroll_up(); break;
568 case KEY_DOWN: filesel_scroll_down(); break;
570 case KEY_PPAGE: filesel_page_up(); break;
572 case KEY_NPAGE: filesel_page_down(); break;
574 case KEY_HOME: filesel_goto_home(); break;
575 case KEY_END: filesel_goto_end(); break;
578 case '\r': if( filesel_enter() ) {
582 default: filesel_goto_item(ch);
590 char *dir, *tmp, *ptr = NULL;
595 if( !filesel_loop() ) {
597 tmp = MALLOC(strlen(dir) + strlen(lst[filesel_curitem].filename) + 2);
600 strcat(tmp, lst[filesel_curitem].filename);
611 #endif /* USE_FILESEL */