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);
111 if( ! (flags & ESTR_USE_FILESEL) )
119 if( !isprint(ch) || iscntrl(ch)
120 || (maxlen > 0 && i + 1 > maxlen) )
125 str = REALLOC( str, size *= 2 );
128 if( i >= 0 && str != NULL )
139 * by JH <jheinonen@users.sourceforge.net>
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]),
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 mvwaddch(filesel_list, line, 3, 'd');
363 wattron(filesel_list, A_BOLD);
367 mvwaddnstr(filesel_list, line, 5, lst[i].filename, COLS - 5);
371 filesel_refresh_list()
375 werase(filesel_list);
377 if( filesel_first_list_item < 0 || filesel_items < 1 ) {
379 wrefresh(filesel_list);
383 for( line = 0, i = filesel_first_list_item ;
384 i <= FILESEL_LAST_LIST_ITEM && i < filesel_items; line++, i++ ) {
386 if(i == filesel_curitem) {
387 filesel_highlight_line(filesel_list, line);
388 wattron(filesel_list, A_STANDOUT);
390 wattrset(filesel_list, 0);
392 filesel_print_list_line(i, line);
396 wrefresh(filesel_list);
402 if( filesel_curitem < 1 )
406 if( filesel_first_list_item > 0 && filesel_curitem < filesel_first_list_item )
407 filesel_first_list_item--;
409 filesel_refresh_list();
413 filesel_scroll_down()
415 if( filesel_curitem > filesel_items - 2 )
419 if( filesel_curitem > FILESEL_LAST_LIST_ITEM )
420 filesel_first_list_item++;
422 filesel_refresh_list();
429 if( filesel_curitem < 1 )
432 if( filesel_curitem == filesel_first_list_item ) {
433 filesel_curitem = (filesel_curitem -= FILESEL_LIST_LINES) < 0 ? 0 : filesel_curitem;
434 filesel_first_list_item = filesel_curitem;
436 filesel_curitem = filesel_first_list_item;
438 filesel_refresh_list();
444 if( filesel_curitem > filesel_items - 2 )
447 if( filesel_curitem == FILESEL_LAST_LIST_ITEM ) {
448 filesel_curitem = (filesel_curitem += FILESEL_LIST_LINES) > FILESEL_LAST_ITEM ?
449 FILESEL_LAST_ITEM : filesel_curitem;
450 filesel_first_list_item = filesel_curitem - FILESEL_LIST_LINES + 1;
451 if( filesel_first_list_item < 0 )
452 filesel_first_list_item = 0;
454 filesel_curitem = FILESEL_LAST_LIST_ITEM < FILESEL_LAST_ITEM ?
455 FILESEL_LAST_LIST_ITEM : FILESEL_LAST_ITEM;
457 filesel_refresh_list();
464 if(filesel_items > 0) {
465 filesel_first_list_item = 0;
468 filesel_refresh_list();
474 if(filesel_items > 0) {
475 filesel_curitem = FILESEL_LAST_ITEM;
476 filesel_first_list_item = filesel_curitem - FILESEL_LIST_LINES + 1;
477 if( filesel_first_list_item < 0 )
478 filesel_first_list_item = 0;
480 filesel_refresh_list();
488 struct dirent *entry;
490 struct filesel_list_item item;
494 entry = readdir(dir);
499 stat(entry->d_name, &st);
500 strcpy(item.filename, entry->d_name);
501 item.type = S_ISDIR(st.st_mode) ?
502 FLSL_TYPE_DIR : FLSL_TYPE_OTHER;
503 filesel_add_filesel_list_item(item);
508 filesel_check_list();
509 filesel_refresh_statusline();
510 filesel_refresh_list();
520 if(lst[filesel_curitem].type == FLSL_TYPE_DIR) {
522 newdir = MALLOC(strlen(dir)+strlen(lst[filesel_curitem].filename) +2 );
525 strcat(newdir,lst[filesel_curitem].filename);
527 if( (chdir(newdir)) != -1) {
528 filesel_close_list();
538 filesel_goto_item(int ch)
542 for(i=filesel_curitem+1; i<filesel_items; i++)
543 if( lst[i].filename[0] == ch ) {
548 if( filesel_curitem > FILESEL_LAST_LIST_ITEM)
549 filesel_first_list_item=filesel_curitem;
551 filesel_refresh_list();
561 switch( (ch = getch()) ) {
564 case 'R': filesel_close_list(); filesel_read_dir();
567 case KEY_UP: filesel_scroll_up(); break;
569 case KEY_DOWN: filesel_scroll_down(); break;
571 case KEY_PPAGE: filesel_page_up(); break;
573 case KEY_NPAGE: filesel_page_down(); break;
575 case KEY_HOME: filesel_goto_home(); break;
576 case KEY_END: filesel_goto_end(); break;
579 case '\r': if( filesel_enter() ) {
583 default: filesel_goto_item(ch);
591 char *dir, *tmp, *ptr = NULL;
596 if( !filesel_loop() ) {
598 tmp = MALLOC(strlen(dir) + strlen(lst[filesel_curitem].filename) + 2);
601 strcat(tmp, lst[filesel_curitem].filename);
612 #endif /* USE_FILESEL */