2010-03-11 20:12:44 -03:00
# include <newt.h>
2010-08-10 15:49:07 -03:00
# include <signal.h>
# include <stdio.h>
# include <stdbool.h>
# include <string.h>
2010-03-12 10:48:12 -03:00
# include <sys/ttydefaults.h>
2010-03-11 20:12:44 -03:00
2010-08-10 15:58:50 -03:00
# include "../cache.h"
# include "../debug.h"
# include "browser.h"
# include "helpline.h"
# include "util.h"
2010-03-11 20:12:44 -03:00
2010-03-12 10:48:12 -03:00
static void newt_form__set_exit_keys ( newtComponent self )
{
2010-05-16 20:29:38 -03:00
newtFormAddHotKey ( self , NEWT_KEY_LEFT ) ;
2010-03-12 10:48:12 -03:00
newtFormAddHotKey ( self , NEWT_KEY_ESCAPE ) ;
newtFormAddHotKey ( self , ' Q ' ) ;
newtFormAddHotKey ( self , ' q ' ) ;
newtFormAddHotKey ( self , CTRL ( ' c ' ) ) ;
}
2010-08-12 12:37:51 -03:00
static newtComponent newt_form__new ( void )
2010-03-12 10:48:12 -03:00
{
newtComponent self = newtForm ( NULL , NULL , 0 ) ;
if ( self )
newt_form__set_exit_keys ( self ) ;
return self ;
}
2010-08-10 15:58:50 -03:00
int ui__popup_menu ( int argc , char * const argv [ ] )
2010-03-24 16:40:14 -03:00
{
struct newtExitStruct es ;
int i , rc = - 1 , max_len = 5 ;
newtComponent listbox , form = newt_form__new ( ) ;
if ( form = = NULL )
return - 1 ;
listbox = newtListbox ( 0 , 0 , argc , NEWT_FLAG_RETURNEXIT ) ;
if ( listbox = = NULL )
goto out_destroy_form ;
2010-05-10 10:51:25 -03:00
newtFormAddComponent ( form , listbox ) ;
2010-03-24 16:40:14 -03:00
for ( i = 0 ; i < argc ; + + i ) {
int len = strlen ( argv [ i ] ) ;
if ( len > max_len )
max_len = len ;
if ( newtListboxAddEntry ( listbox , argv [ i ] , ( void * ) ( long ) i ) )
goto out_destroy_form ;
}
newtCenteredWindow ( max_len , argc , NULL ) ;
newtFormRun ( form , & es ) ;
rc = newtListboxGetCurrent ( listbox ) - NULL ;
if ( es . reason = = NEWT_EXIT_HOTKEY )
rc = - 1 ;
newtPopWindow ( ) ;
out_destroy_form :
newtFormDestroy ( form ) ;
return rc ;
}
2010-08-10 15:49:07 -03:00
int ui__help_window ( const char * text )
2010-05-16 21:04:27 -03:00
{
struct newtExitStruct es ;
newtComponent tb , form = newt_form__new ( ) ;
int rc = - 1 ;
int max_len = 0 , nr_lines = 0 ;
const char * t ;
if ( form = = NULL )
return - 1 ;
t = text ;
while ( 1 ) {
const char * sep = strchr ( t , ' \n ' ) ;
int len ;
if ( sep = = NULL )
sep = strchr ( t , ' \0 ' ) ;
len = sep - t ;
if ( max_len < len )
max_len = len ;
+ + nr_lines ;
if ( * sep = = ' \0 ' )
break ;
t = sep + 1 ;
}
tb = newtTextbox ( 0 , 0 , max_len , nr_lines , 0 ) ;
if ( tb = = NULL )
goto out_destroy_form ;
newtTextboxSetText ( tb , text ) ;
newtFormAddComponent ( form , tb ) ;
newtCenteredWindow ( max_len , nr_lines , NULL ) ;
newtFormRun ( form , & es ) ;
newtPopWindow ( ) ;
rc = 0 ;
out_destroy_form :
newtFormDestroy ( form ) ;
return rc ;
}
2010-11-27 02:41:01 -02:00
static const char yes [ ] = " Yes " , no [ ] = " No " ,
warning_str [ ] = " Warning! " , ok [ ] = " Ok " ;
2010-11-06 11:47:24 +03:00
2010-08-10 15:58:50 -03:00
bool ui__dialog_yesno ( const char * msg )
2010-03-24 16:40:14 -03:00
{
/* newtWinChoice should really be accepting const char pointers... */
2010-11-06 11:47:24 +03:00
return newtWinChoice ( NULL , ( char * ) yes , ( char * ) no , ( char * ) msg ) = = 1 ;
2010-03-24 16:40:14 -03:00
}
2010-11-27 02:41:01 -02:00
void ui__warning ( const char * format , . . . )
{
va_list args ;
va_start ( args , format ) ;
if ( use_browser > 0 )
newtWinMessagev ( ( char * ) warning_str , ( char * ) ok ,
( char * ) format , args ) ;
else
vfprintf ( stderr , format , args ) ;
va_end ( args ) ;
}