#include "Screen.h" #include "stdio.h" void response_function(Screen *sptr) { sptr->add(dtioRows(), 0, dtioColumns(), "A response has been sent", false); } int main(void) { dtioStart(); Screen screen; Field field(0, 10, 30, "Blah blah crap", false); Label label(1, 10, 30, "Blah blah crap"); screen.add(field); screen.add(label); screen.display(); Response response(0, 0, "YyNnMm", '', response_function); screen.add(response); (screen.getField(2))->edit(&screen); fprintf(stderr, "response.data():%c\n", ((screen.getField(2))->data())); dtioGetchar(); dtioStop(); return 0; } Field::Field(int r, int c, int l, const char *s, bool edit) { row = 0; col = 0; len = 0; c_pos = 0; d = NULL; init(r, c, l, s, edit); } /* copy constructor */ Field::Field(const Field &right):row(0), col(0), len(0), c_pos(-1), d(NULL) { *this = right; } /* default constructor -- set member data to safe values */ Field::Field():row(0), col(0), len(0), c_pos(-1), d(NULL) { setData("", len); } /* garbage collection */ Field::~Field() { if (d) { free(d); } } /* assignment operator * returns a reference to the left operand */ const Field& Field::operator=(const Field &right) { /* if the operands are different instances of Field */ if (this != &right) { /* initialize the left operand to be the same as the right */ init(right.getRow(), right.getCol(), right.getLen(), (char*)right.data(), right.editable()); } return *this; } /* is-equal-to operator, necessary for the use of deques * a Field is considered equal to another field if: * the starting coordinates are the same, the length is the same, * the data is the same, and the editability is the same * returns 1 for true and 0 for false */ bool Field::operator==(const Field &right) { bool ret = false; if ((*this).row == right.row && (*this).col == right.col && (*this).len == right.len && (*this).is_editable == right.is_editable && (strcmp((*this).d, right.d) == 0)) { ret = true; } return ret; } bool Field::operator!=(const Field &right) { bool ret = false; if ((*this).row != right.row || (*this).col != right.col || (*this).len != right.len || (*this).is_editable != right.is_editable || (strcmp((*this).d, right.d) != 0)) { ret = true; } return ret; } /* initialize this field */ void Field::init(int r, int c, int width, const char *s, bool edit) { setRow(r); setCol(c); setLen(width); /* if a value is specified for s, set the data to it */ if (s) { setData(s, len); } /* otherwise, set it to a safe value */ else { setData("\0", len); } is_editable = edit; } /* display this field */ void Field::display() { dtioDisplay(d, row, col, len); } /* displays the field and, if possible, puts it into edit mode * returns the key used to exit edit mode if editable * returns 0 if not editable */ int Field::edit(Screen *sptr) { int ret = 0; /* is the field editable? */ if (is_editable) { /* set c_pos to 0 if it is the first time being edited */ if (c_pos == -1) { setcPos(0); } /* if the requested cursor position is longer than the string * put c_pos at the end of the string */ else if (c_pos > len) { c_pos = len; } /* edit the string */ ret = dtioEdit(d, row, col, len, &c_pos); } else { display(); } return ret; } /* returns a pointer to a new copy of this field */ Field* Field::clone()const { return new(std::nothrow) Field(row, col, len, d, is_editable); } /* START OF RESPONSE CLASS */ Response::Response(int r, int c, const char *s, int ch, void (*fptr)(Screen*)) { valid_chars = NULL; fptr = NULL; init(r, c, s, ch, fptr); } Response::~Response() { if (valid_chars) { delete [] valid_chars; } } void Response::init(int r, int c, const char *s, int ch, void (*fptr)(Screen *)) { setRow(r); setCol(c); setLen(1); setData(ch); valid_chars = new char[strlen(s)]; strcpy(valid_chars, s); response_f = fptr; is_editable = true; } int Response::edit(Screen *sptr) { display(); dtioCursor(row, col); int new_d = dtioGetchar(); for (int i = 0; i < strlen(valid_chars); i++) { if (new_d = valid_chars[i]) { d = new_d; break; } } if ((d != new_d) && response_f) { (*response_f)(sptr); } } void Response::display() { dtioCursor(row, col); dtioPutchar(d); } Response* Response::clone()const { return new(std::nothrow) Response(row, col, valid_chars, d, response_f); } Response::init (this=0x8061830, r=0, c=0, s=0x8061820 "YyNnMm", ch=111, fptr=0) at Screen.cpp:491 491 valid_chars = new char[strlen(s)]; (gdb) 492 strcpy(valid_chars, s); (gdb) 493 response_f = fptr; (gdb) 494 is_editable = true; (gdb) 495 } (gdb) Response (this=0x8061830, r=0, c=0, s=0x8061820 "YyNnMm", ch=111, fptr=0) at Screen.cpp:477 477 } (gdb) Response::clone (this=0xbff2941c) at Screen.cpp:520 520 } (gdb) std::deque >::push_back (this=0xbff293f0, __x=@0x8061830) at stl_deque.h:1041 1041 if (this->_M_impl._M_finish._M_cur (gdb) 1044 this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x); (gdb) __gnu_cxx::__mt_alloc_base::construct (this=0xbff293f0, __p=0x80614f0, __val=@0x8061830) at mt_allocator.h:611 611 { ::new(__p) _Tp(__val); } (gdb) operator new (__p=0x80614f0) at new:94 94 inline void* operator new(std::size_t, void* __p) throw() { return __p; } (gdb) 0x0804b2ba in __gnu_cxx::__mt_alloc_base::construct (this=0xbff293f0, __p=0x80614f0, __val=@0x8061830) at mt_allocator.h:611 611 { ::new(__p) _Tp(__val); } (gdb) Field (this=0x80614f0, right=@0x8061830) at Screen.cpp:34 34 Field::Field(const Field &right):row(0), col(0), len(0), c_pos(-1), d(NULL) { (gdb) 35 *this = right;