[Frugalware-git] fwsetup-ng: add yesno ui dialog for newt

James Buren ryuo at frugalware.org
Fri Aug 31 05:35:52 CEST 2012


Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=fwsetup-ng.git;a=commitdiff;h=5957d510a0811ee7f0f421712a86d9d3825db4fe

commit 5957d510a0811ee7f0f421712a86d9d3825db4fe
Author: James Buren <ryuo at frugalware.org>
Date:   Thu Aug 30 22:35:34 2012 -0500

add yesno ui dialog for newt

diff --git a/src/local.h b/src/local.h
index 6d1693a..3b20356 100644
--- a/src/local.h
+++ b/src/local.h
@@ -32,5 +32,6 @@ extern bool get_text_screen_size(const char *text,int *width,int *height);
extern bool get_button_screen_size(const char *text,int *width,int *height);
extern int ui_main(int argc,char **argv);
extern void ui_dialog_text(const char *title,const char *text);
+extern bool ui_dialog_yesno(const char *title,const char *text,bool defaultno);
extern FILE *logfile;
extern int main(int argc,char **argv);
diff --git a/src/ui_newt.c b/src/ui_newt.c
index 2f2c995..7076892 100644
--- a/src/ui_newt.c
+++ b/src/ui_newt.c
@@ -82,3 +82,72 @@ extern void ui_dialog_text(const char *title,const char *text)

newtPopWindow();
}
+
+extern bool ui_dialog_yesno(const char *title,const char *text,bool defaultno)
+{
+  int textbox_width = 0;
+  int textbox_height = 0;
+  int yes_width = 0;
+  int yes_height = 0;
+  int no_width = 0;
+  int no_height = 0;
+  newtComponent textbox = 0;
+  newtComponent yes = 0;
+  newtComponent no = 0;
+  newtComponent form = 0;
+  struct newtExitStruct es = {0};
+  bool result = false;
+
+  if(title == 0 || text == 0)
+  {
+    errno = EINVAL;
+    fprintf(logfile,"%s: %s\n",__func__,strerror(errno));
+    return false;
+  }
+
+  if(!get_text_screen_size(text,&textbox_width,&textbox_height))
+    return false;
+
+  if(!get_button_screen_size(YES_BUTTON_TEXT,&yes_width,&yes_height))
+    return false;
+
+  if(!get_button_screen_size(NO_BUTTON_TEXT,&no_width,&no_height))
+    return false;
+
+  if(newtCenteredWindow(NEWT_WIDTH,NEWT_HEIGHT,title) != 0)
+  {
+    fprintf(logfile,_("Failed to open a NEWT window.\n"));
+    return false;
+  }
+
+  textbox = newtTextbox(0,0,textbox_width,textbox_height,0);
+
+  newtTextboxSetText(textbox,text);
+
+  yes = newtButton(NEWT_WIDTH-yes_width-no_width,NEWT_HEIGHT-yes_height,YES_BUTTON_TEXT);
+
+  no = newtButton(NEWT_WIDTH-no_width,NEWT_HEIGHT-no_height,NO_BUTTON_TEXT);
+
+  form = newtForm(0,0,NEWT_FLAG_NOF12);
+
+  newtFormAddComponents(form,textbox,yes,no,(void *) 0);
+
+  newtFormSetCurrent(form,(defaultno) ? no : yes);
+
+  while(true)
+  {
+    newtFormRun(form,&es);
+
+    if(es.reason == NEWT_EXIT_COMPONENT && (es.u.co == yes || es.u.co == no))
+    {
+      result = (es.u.co == yes);
+      break;
+    }
+  }
+
+  newtFormDestroy(form);
+
+  newtPopWindow();
+
+  return result;
+}


More information about the Frugalware-git mailing list