Changes

Jump to: navigation, search

Console UI Core Classes - OOP344 20121

6,653 bytes added, 15:51, 15 February 2012
CDialog
if _length is greater than zero, it will copy the string pointed by str into the string pointed by _data upto _length characters.
if _length is zero,<br /> It will delete the memory pointed by _data and reallocates enough memory for str and copies the string pointed by str into the newly allocated memory pointed by _data.
==CDialog==
Organizes CField objects on the screen, displays them and then lets the user edit them one by one.
 
<big><syntaxhighlight lang="cpp">
#include "cuigh.h"
#include "cframe.h"
namespace cio{
class CField;
class CDialog: public CFrame{
private:
int _fnum;
int _curidx;
CField* _fld[C_MAX_NO_FIELDS];
bool _dyn[C_MAX_NO_FIELDS];
bool _editable;
public:
CDialog(CFrame *Container = (CFrame*)0,
int Row = -1, int Col = -1,
int Width = -1, int Height = -1,
bool Borderd = false,
const char* Border=C_BORDER_CHARS);
virtual ~CDialog();
void draw(int fn = C_FULL_FRAME);
int edit(int fn = C_FULL_FRAME);
 
int add(CField* field, bool dynamic = true);
int add(CField& field, bool dynamic = false);
CDialog& operator<<(CField* field);
CDialog& operator<<(CField& field);
 
bool editable();
int fieldNum()const;
int curIndex()const;
 
CField& operator[](unsigned int index);
CField& curField();
};
}
</syntaxhighlight></big>
===Attributes===
<big><syntaxhighlight lang="cpp">
int _fnum;
</syntaxhighlight></big>
Holds the number of Fields added to the Dialog
<big><syntaxhighlight lang="cpp">
bool _editable;
</syntaxhighlight></big>
will be set to true if any of the Fields added are editable.
This is optional because it depends on how you are going to implement the collection of CFields:
<big><syntaxhighlight lang="cpp">
int _curidx;
</syntaxhighlight></big>
Holds the index of the Field that is currently being edited.
<big><syntaxhighlight lang="cpp">
CField* _fld[C_MAX_NO_FIELDS];
</syntaxhighlight></big>
Array of CField pointers to hold the address of the CField objects added to the screen. The maximum number of CFields that can be added to this CDialog is C_MAX_NO_FIELDS. Remove this restriction for additional mark.
<big><syntaxhighlight lang="cpp">
bool _dyn[C_MAX_NO_FIELDS];
</syntaxhighlight></big>
Holds series of boolean to the exact number of fields, and each boolean here will hold false if the corresponding field pointed by _fld is allocated dynamically or not.
This array will later on be used by destructor to decide which object is dynamic and to be deleted.
 
===Constructors/Destructors===
<big><syntaxhighlight lang="cpp">
CDialog(CFrame *Container = (CFrame*)0,
int Row = -1, int Col = -1,
int Width = -1, int Height = -1,
bool Borderd = false,
const char* Border=C_BORDER_CHARS);
</syntaxhighlight></big>
The constructor, passes all the incoming arguments to the corresponding arguments of the apparent constructor '''CFrame'''.<br />
Then it will set called a attributes to their default values and then sets all the field pointers (_fld) to NULL.
It also sets all the dynamic (_dyn) flags to false.
<big><syntaxhighlight lang="cpp">
virtual ~CDialog();
</syntaxhighlight></big>
The destructor, will loop through all the field pointers and if the corresponding dynamic flag is true then it will delete the field pointed to by the field pointer.
 
===Methods===
 
<big><syntaxhighlight lang="cpp">
void draw(int fn = C_FULL_FRAME);
</syntaxhighlight></big>
If '''fn''' is '''C_FULL_FRAME''', it will call its parent draw. Then It will draw all the '''Fields''' in the '''Dialog'''. <br />
If '''fn''' is not '''C_FULL_FRAME''', then it will just draw all the '''Fields''' in the '''Dialog'''.<br />
If '''fn''' is a non-zero positive value, then it will only draw '''Field''' number '''fn''' in the dialog. (First added '''Field''' is field number one.)
<big><syntaxhighlight lang="cpp">
int edit(int fn = C_FULL_FRAME);
</syntaxhighlight></big>
If '''CDialog''' is not editable (all fields are non-editable), it will just display the Dialog and then waits for the user to enter a key and then terminates the function returning the key.<br />
If fn is '''0''' or less, then before editing, the draw method is called with '''fn''' as its argument and then editing begins from the first editable Field.
 
If fn is greater than '''0''' then editing begins from the first editable key on or after Field number '''fn'''.
 
Note that fn is the sequence number of field and not the index. (First field number is one)
 
Start editing from field number '''fn''';
 
Call the edit of each field and depending on the value returned, do the following:<br />
# For '''ENTER_KEY''', '''TAB_KEY''' and '''DOWN_KEY''', go to next editable Field , if this is the last editable Field then restart from Field number one.
# For '''UP_KEY''' go to the previous editable Field, if there is no previous editable Field, go to the last editable Field in the Dialog.
# For any other key, terminate the edit function returning the character which caused the termination.
 
<big><syntaxhighlight lang="cpp">
int add(CField* field, bool dynamic = true);
</syntaxhighlight></big>
Adds the '''CField''' pointed by '''field''' to the Fields of the Dialog; by appending the value of the field pointer after the last added '''field'' in the _fld array , setting the corresponding _dyn element to the value of dynamic argument and then increasing _fnum by one and returning <del>it</del> the index of added Field in the CDialog object.<br />
'''important note:<br />
Make sure that add() sets the container of the added CField to this CDialog object, using the container() method of CField'''
<big><syntaxhighlight lang="cpp">
int add(CField& field, bool dynamic = false);
</syntaxhighlight></big>
Makes a direct call to the first add method.
<big><syntaxhighlight lang="cpp">
CDialog& operator<<(CField* field);
</syntaxhighlight></big>
Makes a direct call to the first add method, ignoring the second argument and then returns the owner (current CDialog).
<big><syntaxhighlight lang="cpp">
CDialog& operator<<(CField& field);
</syntaxhighlight></big>
Makes a direct call to the second add method, ignoring the second argument and then returns the owner (current CDialog).
 
<big><syntaxhighlight lang="cpp">
bool editable();
</syntaxhighlight></big>
Returns '''_editable''';
<big><syntaxhighlight lang="cpp">
int fieldNum()const;
</syntaxhighlight></big>
returns '''_fnum'''.
<big><syntaxhighlight lang="cpp">
int curIndex()const;
</syntaxhighlight></big>
returns '''_curidx''';
 
<big><syntaxhighlight lang="cpp">
CField& operator[](unsigned int index);
</syntaxhighlight></big>
Returns the reference of the Field with incoming index. (Note that here, the first field index is '''0''')
<big><syntaxhighlight lang="cpp">
CField& curField();
</syntaxhighlight></big>
Returns the reference of the Field that was just being edited.

Navigation menu