/* Terminality - a portable terminal handling library * Copyright (C) 1998-2002, Emil Mikulic. * This is LGPL - look at COPYING.LIB */ /* Project: Terminality/GUI * File: radio.cpp * Author: Michal Safranek * Description: Implements the radio class and radioGroup class */ #include const char radio_rcsid[] = "$Id: radio.cpp,v 1.10 2002/07/26 01:39:40 darkmoon Exp $"; // radioGroup class implementation ... // Constructor radioGroup::radioGroup() { members = NULL; selected_id = -1; } // Add a button to list void radioGroup::add(class radio *me) { if (members) tn_list_add(members,(void *)me); else members = tn_list_new((void *)me); } // Fix buttons after change void radioGroup::fixup(int id) { selected_id = id; // Draw static objects for (int i = 0; iradio_id != selected_id) && (r->selected)) { r->selected = false; r->draw(); } } } // Destructor radioGroup::~radioGroup() { if (members != NULL) { tn_list_free(members); tn_list_kill(members); } } // radio class implementation ... #define REDEF_CHARS() LBOX_CHR = '<'; RBOX_CHR = '>'; \ CHECK_UNCHECKED_CHR = ' '; CHECK_CHECKED_CHR = 'o'; // Construct radio at radio::radio(class radioGroup *rg, const int id, const int xx, const int yy, bool sel) : checkbox(xx,yy,sel) { radio_id = id; rgroup = rg; type_id = Radio; if(rgroup) rgroup->add(this); if(sel){ if(rgroup) rgroup->fixup(radio_id); } REDEF_CHARS(); } // Construct radio at using for a buffer radio::radio(class radioGroup *rg, const int id, const int xx, const int yy, bool sel, const char *s) : checkbox(xx,yy,sel,s) { radio_id = id; rgroup = rg; type_id = Radio; if(rgroup) rgroup->add(this); if(sel){ if(rgroup) rgroup->fixup(radio_id); } REDEF_CHARS(); } // Construct radio at using for a buffer, label's width radio::radio(class radioGroup *rg, const int id, const int xx, const int yy, bool sel, const char *s, const int w) : checkbox(xx,yy,sel,s,w) { radio_id = id; rgroup = rg; type_id = Radio; if(rgroup) rgroup->add(this); if(sel){ if(rgroup) rgroup->fixup(radio_id); } REDEF_CHARS(); } // Destroy radio radio::~radio() { } // Draw radio void radio::draw(void) { if (!visible) return; checkbox::draw(); } /* Non-blocking get function * Returns: 0 - enter, 27 - ESC, -1 - up, 1 - down, 2 - tab */ int radio::getnb(void) { key c; int result = 101; cursor cs = get_cursor(); bool old_vis = visible; visible = true; set_cursor(none); focus = true; draw(); /* Get the input */ do { // Update screen update(); /* Get letter */ c = readkey(); c = keyhandler(c, Radio); c = handle_key(c, Radio, this); /* Handle input */ switch (c) { // filter case KEY_ENTER: result = 0; break; case KEY_ESC: result = 27; break; case KEY_UP: result = -1; break; case KEY_DOWN: result = 1; break; case KEY_TAB: result = 2; break; case ' ': if (!selected) { selected = true; if (rgroup) rgroup->fixup(radio_id); if (spc_handle) spc_handle(radio_id); draw(); } if(onchange) onchange(Radio, this); break; case KEY_NOTHING: break; default: beep(); } } while (result == 101); visible = old_vis; set_cursor(cs); focus = false; return result; } // Blocking get (cannot be aborted) bool radio::get(void) { while (1) { switch (getnb()) { case 27: return 0; case 0: return selected; } } }