/* * This program demonstrates the file selector and the viewer widget. */ int main (int argc, char **argv) { /* *INDENT-EQLS* */ CDKSCREEN *cdkscreen = 0; CDKVIEWER *example = 0; CDKFSELECT *fSelect = 0; char **info = 0; const char *button[5]; char vTitle[256]; const char *mesg[4]; char temp[256]; int selected, lines; CDK_PARAMS params; char *filename; /* specify filename, bypassing fselect */ char *directory; /* specify starting directory for fselect */ int interp_it; /* interpret embedded markup */ int link_it; /* load file via embedded link */ CDKparseParams (argc, argv, ¶ms, "f:d:il" CDK_CLI_PARAMS); /* *INDENT-EQLS* */ filename = CDKparamString (¶ms, 'f'); directory = CDKparamString2 (¶ms, 'd', "."); interp_it = CDKparamNumber2 (¶ms, 'i', FALSE); link_it = CDKparamNumber2 (¶ms, 'l', FALSE); /* Create the viewer buttons. */ button[0] = "</5><OK><!5>"; button[1] = "</5><Cancel><!5>"; cdkscreen = initCDKScreen (NULL); /* Start color. */ initCDKColor (); /* Get the filename. */ if (filename == 0) { const char *title = "<C>Pick\n<C>A\n<C>File"; const char *label = "File: "; fSelect = newCDKFselect (cdkscreen, CDKparamValue (¶ms, 'X', CENTER), CDKparamValue (¶ms, 'Y', CENTER), CDKparamValue (¶ms, 'H', 20), CDKparamValue (¶ms, 'W', 65), title, label, A_NORMAL, '_', A_REVERSE, "</5>", "</48>", "</N>", "</N>", CDKparamValue (¶ms, 'N', TRUE), CDKparamValue (¶ms, 'S', FALSE)); if (fSelect == 0) { destroyCDKScreen (cdkscreen); endCDK (); fprintf (stderr, "Cannot create fselect-widget\n"); ExitProgram (EXIT_FAILURE); } /* * Set the starting directory. This is not necessary because when * the file selector starts it uses the present directory as a default. */ setCDKFselect (fSelect, directory, A_NORMAL, '.', A_REVERSE, "</5>", "</48>", "</N>", "</N>", ObjOf (fSelect)->box); /* Activate the file selector. */ filename = activateCDKFselect (fSelect, 0); /* Check how the person exited from the widget. */ if (fSelect->exitType == vESCAPE_HIT) { /* Pop up a message for the user. */ mesg[0] = "<C>Escape hit. No file selected."; mesg[1] = ""; mesg[2] = "<C>Press any key to continue."; popupLabel (cdkscreen, (CDK_CSTRING2)mesg, 3); /* Exit CDK. */ destroyCDKFselect (fSelect); destroyCDKScreen (cdkscreen); endCDK (); ExitProgram (EXIT_SUCCESS); } } /* Create the file viewer to view the file selected. */ example = newCDKViewer (cdkscreen, CDKparamValue (¶ms, 'X', CENTER), CDKparamValue (¶ms, 'Y', CENTER), CDKparamValue (¶ms, 'H', 20), CDKparamValue (¶ms, 'W', -2), (CDK_CSTRING2)button, 2, A_REVERSE, CDKparamValue (¶ms, 'N', TRUE), CDKparamValue (¶ms, 'S', FALSE)); /* Could we create the viewer widget? */ if (example == 0) { /* Exit CDK. */ destroyCDKFselect (fSelect); destroyCDKScreen (cdkscreen); endCDK (); printf ("Cannot create viewer. Is the window too small?\n"); ExitProgram (EXIT_SUCCESS); } if (link_it) { info = (char **)calloc (2, sizeof (char *)); info[0] = (char *)malloc (5 + strlen (filename)); sprintf (info[0], "<F=%s>", filename); lines = -1; interp_it = TRUE; } else { setCDKViewer (example, "reading...", 0, 0, A_REVERSE, TRUE, TRUE, TRUE); /* Open the file and read the contents. */ lines = CDKreadFile (filename, &info); if (lines == -1) { endCDK (); printf ("Could not open \"%s\"\n", filename); ExitProgram (EXIT_FAILURE); } } /* Set up the viewer title, and the contents to the widget. */ sprintf (vTitle, "<C></B/21>Filename:<!21></22>%20s<!22!B>", filename); setCDKViewer (example, vTitle, (CDK_CSTRING2)info, lines, A_REVERSE, interp_it, TRUE, TRUE); CDKfreeStrings (info); /* Destroy the file selector widget. */ destroyCDKFselect (fSelect); /* Activate the viewer widget. */ selected = activateCDKViewer (example, 0); /* Check how the person exited from the widget. */ if (example->exitType == vESCAPE_HIT) { mesg[0] = "<C>Escape hit. No Button selected."; mesg[1] = ""; mesg[2] = "<C>Press any key to continue."; popupLabel (cdkscreen, (CDK_CSTRING2)mesg, 3); } else if (example->exitType == vNORMAL) { sprintf (temp, "<C>You selected button %d", selected); mesg[0] = temp; mesg[1] = ""; mesg[2] = "<C>Press any key to continue."; popupLabel (cdkscreen, (CDK_CSTRING2)mesg, 3); } /* Clean up. */ destroyCDKViewer (example); destroyCDKScreen (cdkscreen); endCDK (); ExitProgram (EXIT_SUCCESS); }
/* * This program demonstrates the Cdk selection widget. * * Options (in addition to normal CLI parameters): * -c create the data after the widget * -s SPOS location for the scrollbar * -t TEXT title for the widget * */ int main (int argc, char **argv) { /* Declare variables. */ CDKSCREEN *cdkscreen = 0; CDKSELECTION *selection = 0; WINDOW *cursesWin = 0; char *title = "<C></5>Pick one or more accounts."; char *choices[] = {" ", "-->"}; char **item = 0; char temp[256], *mesg[200]; struct passwd *ent; unsigned x, y; unsigned used = 0; unsigned count = 0; CDK_PARAMS params; CDKparseParams(argc, argv, ¶ms, "cs:t:" CDK_CLI_PARAMS); /* Use the account names to create a list. */ count = 0; while ((ent = getpwent ()) != 0) { used = CDKallocStrings(&item, ent->pw_name, count++, used); } endpwent(); count--; /* Set up CDK. */ cursesWin = initscr(); cdkscreen = initCDKScreen (cursesWin); /* Set up CDK Colors. */ initCDKColor(); /* Create the selection list. */ selection = newCDKSelection (cdkscreen, CDKparamValue(¶ms, 'X', CENTER), CDKparamValue(¶ms, 'Y', CENTER), CDKparsePosition(CDKparamString2(¶ms, 's', "RIGHT")), CDKparamValue(¶ms, 'H', 10), CDKparamValue(¶ms, 'W', 50), CDKparamString2(¶ms, 't', title), CDKparamNumber(¶ms, 'c') ? 0 : item, CDKparamNumber(¶ms, 'c') ? 0 : count, choices, 2, A_REVERSE, CDKparamValue(¶ms, 'N', TRUE), CDKparamValue(¶ms, 'S', FALSE)); /* Is the selection list null? */ if (selection == 0) { /* Exit CDK. */ destroyCDKScreen (cdkscreen); endCDK(); /* Print out a message and exit. */ printf ("Oops. Can;t seem to create the selection list. Is the window too small?\n"); ExitProgram (EXIT_FAILURE); } if (CDKparamNumber(¶ms, 'c')) { setCDKSelectionItems (selection, item, count); } /* Activate the selection list. */ activateCDKSelection (selection, 0); /* Check the exit status of the widget. */ if (selection->exitType == vESCAPE_HIT) { mesg[0] = "<C>You hit escape. No items selected."; mesg[1] = "", mesg[2] = "<C>Press any key to continue."; popupLabel (cdkscreen, mesg, 3); } else if (selection->exitType == vNORMAL) { mesg[0] = "<C>Here are the accounts you selected."; y = 1; for (x=0; x < count; x++) { if (selection->selections[x] == 1) { sprintf (temp, "<C></5>%.*s", (int)(sizeof(temp) - 20), item[x]); mesg[y++] = copyChar (temp); } } popupLabel (cdkscreen, mesg, y); /* Clean up. */ for (x=1; x < y; x++) { freeChar (mesg[x]); } } /* Clean up. */ CDKfreeStrings (item); destroyCDKSelection (selection); destroyCDKScreen (cdkscreen); endCDK(); ExitProgram (EXIT_SUCCESS); }
int main (int argc, char **argv) { /* *INDENT-EQLS* */ CDKSCREEN *cdkScreen = 0; CDKMENTRY *widget = 0; CDKBUTTONBOX *buttonWidget = 0; WINDOW *cursesWindow = 0; chtype *holder = 0; chtype fieldAttr = 0; char *answer = 0; char *CDK_WIDGET_COLOR = 0; char *temp = 0; char filler = '.'; EDisplayType dType = vMIXED; int buttonCount = 0; int selection = 0; int shadowHeight = 0; FILE *fp = stderr; char **buttonList = 0; int j1, j2; CDK_PARAMS params; boolean boxWidget; boolean shadowWidget; char *buttons; char *initValue; char *label; char *outputFile; char *tempFiller; char *title; int fieldWidth; int min; int screenRows; int virtualRows; int xpos; int ypos; CDKparseParams (argc, argv, ¶ms, "d:f:i:m:s:v:B:F:L:O:T:" CDK_MIN_PARAMS); /* *INDENT-EQLS* */ xpos = CDKparamValue (¶ms, 'X', CENTER); ypos = CDKparamValue (¶ms, 'Y', CENTER); boxWidget = CDKparamValue (¶ms, 'N', TRUE); shadowWidget = CDKparamValue (¶ms, 'S', FALSE); fieldWidth = CDKparamValue (¶ms, 'f', 0); min = CDKparamValue (¶ms, 'm', 0); screenRows = CDKparamValue (¶ms, 's', 0); virtualRows = CDKparamValue (¶ms, 'v', 0); initValue = CDKparamString (¶ms, 'i'); buttons = CDKparamString (¶ms, 'B'); tempFiller = CDKparamString (¶ms, 'F'); label = CDKparamString (¶ms, 'L'); outputFile = CDKparamString (¶ms, 'O'); title = CDKparamString (¶ms, 'T'); if ((temp = CDKparamString (¶ms, 'd')) != 0) dType = char2DisplayType (temp); /* Make sure all the command line parameters were provided. */ if ((fieldWidth <= 0) || (screenRows <= 0) || (virtualRows <= 0)) { fprintf (stderr, "Usage: %s %s\n", argv[0], FPUsage); ExitProgram (CLI_ERROR); } /* If the user asked for an output file, try to open it. */ if (outputFile != 0) { if ((fp = fopen (outputFile, "w")) == 0) { fprintf (stderr, "%s: Can not open output file %s\n", argv[0], outputFile); ExitProgram (CLI_ERROR); } } /* Set up CDK. */ cursesWindow = initscr (); cdkScreen = initCDKScreen (cursesWindow); /* Start color. */ initCDKColor (); /* Check if the user wants to set the background of the main screen. */ if ((temp = getenv ("CDK_SCREEN_COLOR")) != 0) { holder = char2Chtype (temp, &j1, &j2); wbkgd (cdkScreen->window, holder[0]); wrefresh (cdkScreen->window); freeChtype (holder); } /* Get the widget color background color. */ if ((CDK_WIDGET_COLOR = getenv ("CDK_WIDGET_COLOR")) == 0) { CDK_WIDGET_COLOR = 0; } /* If the set the filler character, set it now. */ if (tempFiller != 0) { holder = char2Chtype (tempFiller, &j1, &j2); fieldAttr = A_ATTRIBUTES & holder[0]; filler = (char)holder[0]; freeChtype (holder); } /* Create the mentry widget. */ widget = newCDKMentry (cdkScreen, xpos, ypos, title, label, fieldAttr, (chtype)filler | fieldAttr, dType, fieldWidth, screenRows, virtualRows, min, boxWidget, shadowWidget); /* Check to make sure we created the dialog box. */ if (widget == 0) { /* Shut down curses and CDK. */ destroyCDKScreen (cdkScreen); endCDK (); fprintf (stderr, "Error: Could not create the multiple line entry field. " "Is the window too small?\n"); ExitProgram (CLI_ERROR); } /* Split the buttons if they supplied some. */ if (buttons != 0) { buttonList = CDKsplitString (buttons, '\n'); buttonCount = (int)CDKcountStrings ((CDK_CSTRING2) buttonList); /* We need to create a buttonbox widget. */ buttonWidget = newCDKButtonbox (cdkScreen, getbegx (widget->win), (getbegy (widget->win) + widget->boxHeight - 1), 1, widget->boxWidth - 1, NULL, 1, buttonCount, (CDK_CSTRING2) buttonList, buttonCount, A_REVERSE, boxWidget, FALSE); CDKfreeStrings (buttonList); setCDKButtonboxULChar (buttonWidget, ACS_LTEE); setCDKButtonboxURChar (buttonWidget, ACS_RTEE); /* * We need to set the lower left and right * characters of the widget. */ setCDKMentryLLChar (widget, ACS_LTEE); setCDKMentryLRChar (widget, ACS_RTEE); /* * Bind the Tab key in the widget to send a * Tab key to the button box widget. */ bindCDKObject (vMENTRY, widget, KEY_TAB, widgetCB, buttonWidget); bindCDKObject (vMENTRY, widget, CDK_NEXT, widgetCB, buttonWidget); bindCDKObject (vMENTRY, widget, CDK_PREV, widgetCB, buttonWidget); /* Check if the user wants to set the background of the widget. */ setCDKButtonboxBackgroundColor (buttonWidget, CDK_WIDGET_COLOR); /* Draw the button widget. */ drawCDKButtonbox (buttonWidget, boxWidget); } /* * If the user asked for a shadow, we need to create one. Do this instead * of using the shadow parameter because the button widget is not part of * the main widget and if the user asks for both buttons and a shadow, we * need to create a shadow big enough for both widgets. Create the shadow * window using the widgets shadowWin element, so screen refreshes will draw * them as well. */ if (shadowWidget == TRUE) { /* Determine the height of the shadow window. */ shadowHeight = (buttonWidget == (CDKBUTTONBOX *)NULL ? widget->boxHeight : widget->boxHeight + buttonWidget->boxHeight - 1); /* Create the shadow window. */ widget->shadowWin = newwin (shadowHeight, widget->boxWidth, getbegy (widget->win) + 1, getbegx (widget->win) + 1); /* Make sure we could have created the shadow window. */ if (widget->shadowWin != 0) { widget->shadow = TRUE; /* * We force the widget and buttonWidget to be drawn so the * buttonbox widget will be drawn when the widget is activated. * Otherwise the shadow window will draw over the button widget. */ drawCDKMentry (widget, ObjOf (widget)->box); eraseCDKButtonbox (buttonWidget); drawCDKButtonbox (buttonWidget, ObjOf (buttonWidget)->box); } } /* Check if the user wants to set the background of the widget. */ setCDKMentryBackgroundColor (widget, CDK_WIDGET_COLOR); /* If there was an initial value, set it. */ if (initValue != 0) { setCDKMentryValue (widget, initValue); } /* Activate the widget. */ answer = copyChar (activateCDKMentry (widget, 0)); /* If there were buttons, get the button selected. */ if (buttonWidget != 0) { selection = buttonWidget->currentButton; destroyCDKButtonbox (buttonWidget); } /* End CDK. */ destroyCDKMentry (widget); destroyCDKScreen (cdkScreen); endCDK (); /* Print the value from the widget. */ if (answer != 0) { fprintf (fp, "%s\n", answer); freeChar (answer); } fclose (fp); /* Exit with the button number picked. */ ExitProgram (selection); }