/* Terminality - a portable terminal handling library * Copyright (C) 1998-2002, Emil Mikulic. * This is LGPL - look at COPYING.LIB */ /* Project: Terminality * File: demo.cpp * Author: Emil Mikulic, Michal Safranek * Description: Demonstrate the Terminality GUI */ /* Define this if you want the 'theme preview' function: */ #define THEME_FOREVER #include #include #include #include "myscheme.h" #include #ifdef USE_NCURSES /* alias we're not under Windoze */ //#include #endif const char rcsid[] = "$Id: demo.cpp,v 1.19 2002/07/26 01:39:43 darkmoon Exp $"; void doabout(void) { /* Paint the screen */ #ifndef __DJGPP__ fillscreen(DarkGray, Black, BLOCK_1); #else fillscreen(DarkGray, Black, 176); #endif /* Draw a 3D box */ drawbox_out(12,11,CON_COLS-12,14, Green); /* Write in it */ setcolor(White, Green); center(12,"Terminality"); center(13,"Copyright (C) 1998-2002, Emil, Volker and Michal."); /* Centertext */ setcolor(LightRed, Black); center(18, "Demo Program"); /* Press any key */ setcolor(White, Black); center(20, "Press any key to continue"); update(); while (!keypressed()) {}; readkey(); } key local_kh(key k, element_type et, void *ptr){ ((textbox *) ptr)->set_buf("Great cheat! :)"); ((textbox *) ptr)->draw(); return KEY_NOTHING; } void dodlg(void) { button *b = NULL; form *myForm = new form(30,4,36,19); myForm->addstatic(new staticLabel(32,7,"Text:")); myForm->addstatic(new staticLabel(32,8,"Numbers:")); textbox *tbx = new textbox(41,7," It's a cow-dog. " "It says \"moof\"!", 20, 50); #ifndef _WIN32 tbx->bind(KEY_F(1), &local_kh, 0); #endif myForm->addobject(tbx); textbox *mybox = new textbox(41,8,"12", 20); mybox->charmap = "0123456789"; myForm->addobject(mybox); myForm->addobject(new checkbox(32,10,false,"I'm a CheckBox")); myForm->addstatic(new staticLabel(32,12,"Choose your gender:")); radioGroup *rg = new radioGroup; myForm->addobject(new radio(rg,1,34,13,false,"Male")); myForm->addobject(new radio(rg,2,34,14,false,"Female")); myForm->addstatic(new staticLabel(32,16,"Choose your race:")); radioGroup *rg2 = new radioGroup; myForm->addobject(new radio(rg2,1,34,17,false,"Human")); myForm->addobject(new radio(rg2,2,34,18,false,"Elf")); myForm->addobject(new radio(rg2,3,34,19,false,"Dwarf")); myForm->addobject(new radio(rg2,4,50,17,false,"Orc")); myForm->addobject(new radio(rg2,5,50,18,false,"Troll")); myForm->addobject(new radio(rg2,6,50,19,false,"Drakeling")); myForm->addobject(b = new button(52,21,6,1,"OK",1)); b->dflt = true; myForm->run(); /* * Short note about gathering results: if you want to get result * from radio buttons in a group, use their 'radioGroup' instead of * travelling through them and checking 'selected'. * * for example: printw("Radio group1: %d\n",rg2->selected_id); */ delete myForm; delete rg; delete rg2; } void hshow(char *text) { char *buf = (char*) xmalloc(strlen(text) + 25); form *myForm = new form(20,3,40,5); sprintf(buf, "You pressed the %s item", text); myForm->addstatic(new staticLabel(21+(40-strlen(buf))/2,4,buf)); myForm->addobject(new button(37,6,6,1,"OK",1)); myForm->run(); delete myForm; xfree(buf); } void dodlg2(void) { clrscr(); hMenu *myMenu = new hMenu( new hMenuItem("First",1,1), new hMenuItem("Second",7,92), new hMenuItem("Third",14,13), NULL); myMenu->y = 1; myMenu->visible = true; int ch,die=0; do { clrscr(); ch = myMenu->getchoice(); die = 1; switch(ch) { case 1: hshow("first"); break; case 92: hshow("second"); break; case 13: hshow("third"); break; case -1: break; default: die = 0; break; }; } while(die != 1); delete myMenu; } key my_listing_handle(key k, void *l, int num) { if(k == ' ') { gotoxy(28, 25); setcolor(White, Black); printw("listing_handle(' '): %.3d", num); update(); return KEY_NOTHING; } else { return k; } } void dodlg3(void) { tn_list *it1 = NULL, *it2 = NULL, *it3 = NULL, *cl3 = NULL; char *tmp; color *ctmp; int i, x, y; clrscr(); srand(time(0)); #define SIZE 30 #define CREATE_DATA(x,h) tmp = (char *)xmalloc(SIZE + 1); \ sprintf(tmp,"%d",h); if (x) tn_list_add(x, tmp); else \ x = tn_list_new(tmp); #define GENERATE_DATA(y) x = (int) (1000.0*rand()/(RAND_MAX+1.0)); \ CREATE_DATA(y,x); #define ADD_CLR(x,y) ctmp = (color *)xmalloc(sizeof(color)*4); \ ctmp[0] = ctmp[1] = y; ctmp[2] = Default; ctmp[3] = Default; \ if (x) tn_list_add(x,ctmp); else x = tn_list_new(ctmp); #define ADD_ODD(x) ADD_CLR(x,LightBlue); #define ADD_EVEN(x) ADD_CLR(x,Red); for (i = 0; i < 100; i++) { y = 0; GENERATE_DATA(it1); y += x; GENERATE_DATA(it2); y += x; CREATE_DATA(it3, y); if (!(y%2)) { ADD_EVEN(cl3); } else { ADD_ODD(cl3); } } listing *myListing = new listing (2, 2, 78, 23, new listingSeparator(2), new listingItem(3,24,"A",it1), new listingSeparator(27), new listingItem(28,25,"B",it2), new listingSeparator(53), new listingItem(54,25,"A+B (even=red, odd=blue)",it3,cl3), new listingSeparator(79, cl3), NULL); myListing->set_handle(&my_listing_handle); myListing->visible = true; myListing->run(); delete myListing; #define KILL_LIST(x) \ for(i = 0; i < tn_list_size(x); i++) { xfree(tn_list_getdata(x,i)); } \ tn_list_free(x); tn_list_kill(x); KILL_LIST(it1); KILL_LIST(it2); KILL_LIST(it3); KILL_LIST(cl3); #undef KILL_LIST #undef SIZE #undef CREATE_DATA #undef GENERATE_DATA #undef ADD_CLR #undef ADD_ODD #undef ADD_EVEN } void dodlg4(void) { form *myForm = new form(30,4,36,19); button *b = NULL; myForm->addstatic(new staticLabel(46,7,"Text:")); myForm->addobject(new textarea(37,11,20,7,"Wow! It says \"moof\"! " "That's great!")); myForm->addobject(b = new button(52,21,6,1,"OK",1)); b->dflt = true; myForm->run(); delete myForm; } int dflt_glkh(key k, element_type et, void *ptr) { gotoxy(1, et==Hmenu?25:1); printw("dflt_glkh(): called: %d, %d, %p (accept)", k, et, ptr); update(); return KEY_NOTHING; } int dflt_glkh2(key k, element_type et, void *ptr) { /* Function keys are broken on Win32 right at the moment */ #ifndef _WIN32 if (k != KEY_F(2) && k != KEY_F(3)) { return KEY_ENTER; } if (k == KEY_F(2)) default_scheme(); else set_scheme(my_color_scheme); #endif return KEY_NOTHING; } void doviewer(void) { /* Paint the screen */ #ifndef __DJGPP__ fillscreen(DarkGray, Black, BLOCK_1); #else fillscreen(DarkGray, Black, 176); #endif setcolor(White, Blue); textviewer(30,10,20,4, "Arithmetic is being able to count up to twenty " "without taking off your shoes.\n\n -- Mickey Mouse"); } void doscheme(void) { menu *scheme_menu = new menu( new menuItem("Original", 1), new menuItem("Blue", 2), new menuSeparator(), #ifdef THEME_FOREVER new menuItem("Exit", 99), #else new menuItem("Unchanged", 99), #endif NULL); scheme_menu->x = 30; scheme_menu->y = 10; scheme_menu->visible = true; #ifdef THEME_FOREVER while(1) { #endif switch(scheme_menu->getchoice()) { case 1: default_scheme(); break; case 2: set_scheme(my_color_scheme); break; case -1: case 99: delete scheme_menu; return; default: beep(); break; } #ifdef THEME_FOREVER } #endif } bool perc = true, alt = false; progressBar *pb = NULL; form *myForm = NULL; void perc_handle(int sel) { perc = sel?true:false; pb->show_perc = perc; myForm->draw(); } void alt_handle(int sel) { alt = sel?true:false; pb->alt_clr = alt; myForm->draw(); } void dopbar(void) { int die = 0; checkbox *cb = NULL; button *b = NULL; myForm = new form(27, 10, 33, 10); pb = new progressBar(29, 12, 27, 1); myForm->remember_id = true; pb->show_perc = perc; pb->alt_clr = alt; myForm->addstatic(pb); myForm->addobject(new button(29, 14, 6, 1, " ->", 3)); myForm->addobject(new button(52, 14, 6, 1, " |<-", 1)); myForm->addobject(cb = new checkbox(29, 18, perc, "Draw percents")); cb->spc_handle = perc_handle; myForm->addobject(cb = new checkbox(29, 19, alt, "Alternate colors")); cb->spc_handle = alt_handle; myForm->addobject(b = new button(52, 18, 6, 1, " OK", 4)); b->dflt = true; die = 0; while (!die) { switch (myForm->run()) { case 1:/* |<- */ pb->setcur(0); break; case 3:/* -> */ if(!pb->advance()) beep(); break; case 0:/* ESC */ case 4:/* OK */ die = 1; break; } } delete myForm; } void dotextview(char *av0) { winTextview *wtw = new winTextview(20, 5, 40, 15, "winTextview"); if(wtw->import_file("sth.xxx")){ wtw->set_content("\n\n\n\n\n\n Can't open file: " "\033f4sth.xxx\033f! :("); } wtw->run(); delete wtw; } void bottom_line_dr(void){ center(CON_ROWS, "\033b0\033f8\x0b0\x0b1\x0b2" "\033f7\x0b1\x0b2\x0db" "\033b7\033fFUse the arrow keys!" "\033b0\033f7\x0db\x0b2\x0b1" "\033f8\x0b2\x0b1\x0b0"); } #define NUMPHASES 4 char phases[NUMPHASES] = {'|', '/', '-', '\\'}; int current_phase = 0; key supme(key k, element_type et, void *ptr){ gotoxy(CON_COLS, 1); writech(phases[current_phase++]); current_phase %= NUMPHASES; return k; } int main(int argc, char **argv) { if(argc>1) { xmem_want_debug = 1; } // init console initcons(); clrscr(); #ifndef _WIN32 register_key(KEY_F(1), dflt_glkh, 0); register_key(KEY_F(2), dflt_glkh2, 0); register_key(KEY_F(3), dflt_glkh2, 0); register_key(KEY_F(4), dflt_glkh2, 0); #endif register_key(KEY_SUPERVISE, supme, 0); menu *myMenu = new menu( new menuItem("About", 1), new menuItem("Test Dialog", 2), new menuItem("Test Horizontal Menu", 3), new menuItem("Test Listing", 4), new menuItem("Test TextArea", 5), new menuItem("Test Progress Bar", 6), new menuItem("Test Textviewer", 7), new menuItem("Test Textview (new)", 9), new menuItem("Theme change", 8), new menuSeparator(), new menuItem("Quit", 99), NULL); myMenu->x = 3; myMenu->y = 10; custom *bottom_line = new custom(&bottom_line_dr, NULL); bottom_line->visible = true; int ch; do { setcolor(LightGray, Black); clrscr(); bottom_line->draw(); ch = myMenu->getchoice(); switch (ch) { case 1:/* About */ doabout(); break; case 2:/* Dialog */ myMenu->visible = true; dodlg(); myMenu->visible = false; break; case 3:/* HMenu */ bottom_line->visible = false; dodlg2(); bottom_line->visible = true; break; case 4:/* Listing */ bottom_line->visible = false; dodlg3(); bottom_line->visible = true; break; case 5:/* TextArea */ myMenu->visible = true; dodlg4(); myMenu->visible = false; break; case 6:/* pBar */ myMenu->visible = true; dopbar(); myMenu->visible = false; break; case 7:/* Textviewer */ bottom_line->visible = false; doviewer(); bottom_line->visible = true; break; case 8:/* Theme selector */ myMenu->visible = true; doscheme(); myMenu->visible = false; break; case 9:/* Textview */ myMenu->visible = true; dotextview(argv[0]); myMenu->visible = false; break; #if 0 case -1:/* ESC hit */ ch = 99; break; #endif default: break; }; } while(ch != 99); delete myMenu; delete bottom_line; // Clear screen - it looks better setcolor(LightGray, Black); set_cursor(line); clrscr(); update(); // finish up donecons(); return 0; }