5 * by JH <jheinonen@bigfoot.com>
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@bigfoot.com>
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 * sizeof(struct filesel_list_item),
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 wattron(filesel_list, A_BOLD);
362 mvwaddnstr(filesel_list, line, 5, lst[i].filename, COLS - 5);
366 filesel_refresh_list()
370 werase(filesel_list);
372 if( filesel_first_list_item < 0 || filesel_items < 1 ) {
374 wrefresh(filesel_list);
378 for( line = 0, i = filesel_first_list_item ;
379 i <= FILESEL_LAST_LIST_ITEM && i < filesel_items; line++, i++ ) {
381 if(i == filesel_curitem) {
382 filesel_highlight_line(filesel_list, line);
383 wattron(filesel_list, A_STANDOUT);
385 wattrset(filesel_list, 0);
387 filesel_print_list_line(i, line);
391 wrefresh(filesel_list);
397 if( filesel_curitem < 1 )
401 if( filesel_first_list_item > 0 && filesel_curitem < filesel_first_list_item )
402 filesel_first_list_item--;
404 filesel_refresh_list();
408 filesel_scroll_down()
410 if( filesel_curitem > filesel_items - 2 )
414 if( filesel_curitem > FILESEL_LAST_LIST_ITEM )
415 filesel_first_list_item++;
417 filesel_refresh_list();
424 if( filesel_curitem < 1 )
427 if( filesel_curitem == filesel_first_list_item ) {
428 filesel_curitem = (filesel_curitem -= FILESEL_LIST_LINES) < 0 ? 0 : filesel_curitem;
429 filesel_first_list_item = filesel_curitem;
431 filesel_curitem = filesel_first_list_item;
433 filesel_refresh_list();
439 if( filesel_curitem > filesel_items - 2 )
442 if( filesel_curitem == FILESEL_LAST_LIST_ITEM ) {
443 filesel_curitem = (filesel_curitem += FILESEL_LIST_LINES) > FILESEL_LAST_ITEM ?
444 FILESEL_LAST_ITEM : filesel_curitem;
445 filesel_first_list_item = filesel_curitem - FILESEL_LIST_LINES + 1;
446 if( filesel_first_list_item < 0 )
447 filesel_first_list_item = 0;
449 filesel_curitem = FILESEL_LAST_LIST_ITEM < FILESEL_LAST_ITEM ?
450 FILESEL_LAST_LIST_ITEM : FILESEL_LAST_ITEM;
452 filesel_refresh_list();
459 if(filesel_items > 0) {
460 filesel_first_list_item = 0;
463 filesel_refresh_list();
469 if(filesel_items > 0) {
470 filesel_curitem = FILESEL_LAST_ITEM;
471 filesel_first_list_item = filesel_curitem - FILESEL_LIST_LINES + 1;
472 if( filesel_first_list_item < 0 )
473 filesel_first_list_item = 0;
475 filesel_refresh_list();
483 struct dirent *entry;
485 struct filesel_list_item item;
489 entry = readdir(dir);
494 stat(entry->d_name, &st);
495 strcpy(item.filename, entry->d_name);
496 item.type = S_ISDIR(st.st_mode) ?
497 FLSL_TYPE_DIR : FLSL_TYPE_OTHER;
498 filesel_add_filesel_list_item(item);
503 filesel_check_list();
504 filesel_refresh_statusline();
505 filesel_refresh_list();
515 if(lst[filesel_curitem].type == FLSL_TYPE_DIR) {
517 newdir = MALLOC(strlen(dir)+strlen(lst[filesel_curitem].filename) +2 );
520 strcat(newdir,lst[filesel_curitem].filename);
522 if( (chdir(newdir)) != -1) {
523 filesel_close_list();
533 filesel_goto_item(int ch)
537 for(i=filesel_curitem+1; i<filesel_items; i++)
538 if( lst[i].filename[0] == ch ) {
543 if( filesel_curitem > FILESEL_LAST_LIST_ITEM)
544 filesel_first_list_item=filesel_curitem;
546 filesel_refresh_list();
556 switch( (ch = getch()) ) {
559 case 'R': filesel_close_list(); filesel_read_dir();
562 case KEY_UP: filesel_scroll_up(); break;
564 case KEY_DOWN: filesel_scroll_down(); break;
566 case KEY_PPAGE: filesel_page_up(); break;
568 case KEY_NPAGE: filesel_page_down(); break;
570 case KEY_HOME: filesel_goto_home(); break;
571 case KEY_END: filesel_goto_end(); break;
574 case '\r': if( filesel_enter() ) {
578 default: filesel_goto_item(ch);
586 char *dir, *tmp, *ptr = NULL;
591 if( !filesel_loop() ) {
593 tmp = MALLOC(strlen(dir) + strlen(lst[filesel_curitem].filename) + 2);
596 strcat(tmp, lst[filesel_curitem].filename);
607 #endif /* USE_FILESEL */