Difference between revisions of "Console UI Core Classes - OOP344 20111"

From CDOT Wiki
Jump to: navigation, search
(CDialog)
(C)
Line 532: Line 532:
 
   ~CText();
 
   ~CText();
 
};
 
};
</syntaxhighlight></big>
 
 
 
==C==
 
<big><syntaxhighlight lang="cpp">
 
 
 
</syntaxhighlight></big>
 
</syntaxhighlight></big>
  

Revision as of 23:42, 9 March 2011


OOP344 | Weekly Schedule | Student List | Teams | Project | Student Resources
Under Construction!

Before anything, go to your repository/branches/fardad/bio_additions and open bio.c and bio.h, then copy and apply the additional functions and changes to your own bio.c and bio.h in trunk. Make sure it compiles and runs properly. These changes add buffering support to be able to prevent flickering. The functionalities added will not change the console behavior or the bio functions. After applying the changes, everything should work they way they worked before.

Your objective at this stage is to create series of core classes designed to interact with the user. These Core Classes then can be used in development of any interactive application.

Please note that the class definitions here are minimum requirement for the Core Classes and you are free to add any enhancements or features you find useful. However make sure that you discuss these enhancements with your professor to make sure they are feasible before implementation.

It is highly recommended to develop the classes in the order they are stated here. You must create your won tester programs fore each class (if possible); However, close to due date of each release, a tester program is provided to help you verify the functionality of your classes. Executables of the test programs are available on matrix to show you how it is supposed to run.

Start by creating mock-up classes (class declaration and definition with empty methods that only compiles and don't do anything). Each class MUST have its own header file to hold its declaration and "cpp" file to hold its implementation. To make sure you do not do circular includes follow these simple guidelines:

  • Add recompilation safeguards to all your header files.
  • Always use forward declaration if possible instead of including a class header-file.
  • Use includes only in files in which the actual header file code is used.
  • Avoid "just in case" includes.

General Internal Header file (cgh.h R0.1)

The general header file holds the common setting and definition between all the Core Classes.

#ifndef ___CGH_H__
#define ___CGH_H__
 
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#endif
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
 
#define C_MAX_NO_FIELDS 100
#define C_BUTTON_HIT 1
#define C_MAX_LINE_CHARS  (1024u)

#define C_FULL_FRAME -1
#define C_NO_FRAME 0

#define C_BORDER_CHARS  "/-\\|/-\\|"
enum CDirection {bio_centre, bio_left, bio_right, bio_up, bio_down};

extern "C"{
#include "bio.h"
};

enum MessageStatus{ClearMessage,SetMessage};


#ifdef NO_HELPFUNC
# undef NO_HELPFUNC
#endif
#define NO_HELPFUNC ((void(*)(MessageStatus, CDialog&))(0))
#ifdef NO_VALDFUNC
# undef NO_VALDFUNC
#endif
#define NO_VALDFUNC ((bool(*)(const char*, CDialog&))(0))


#define C_MAX_LINE_CHARS  (1024u)
#define C_INITIAL_NUM_OF_LINES (100u)

#endif

File Names

Use the following rules to create filenames for your class.

  • Each class MUST have its own header file and cpp file for implementation
  • Use the class name for the name of the file but make sure it is all lowercase.
    For example CFrame class should have cframe.h and cframe.cpp files for its implementation.

Hierarchy

CFrame
 |
 |---CDialog
 |
 |
 |---CField
       |
       |-------- CLabel
       |
       |
       |-------- CButton
       |
       |
       |-------- CLineEdit
       |         |
       |         |-------CValEdit
       |
       |-------- CText
       |
       |
       |-------- CCheck
       |
       |
       |-------- CCheckList
       |
       |
       |-------- CCMenuItem 
       |
       |
       |-------- CCMenu 

Basic (BIO) Encapsulating Classes

CFrame

The code for this class is provided. You must understand and use it to develop your core classes.

#pragma once
#include "cgh.h"

class CFrame{
  int _row;
  int _col;
  int _height;
  int _width;
  char _border[9];
  bool _visible;
  CFrame* _frame;
  char* _covered;
  void capture();
protected:
  int absRow()const;
  int absCol()const;
public:
  CFrame(int Row=-1, int Col=-1, int Width=-1,int Height=-1,
    bool Visible = false,
    const char* Border=C_BORDER_CHARS,
    CFrame* Frame = (CFrame*)0);
  
  virtual void draw(int fn=C_FULL_FRAME);
  virtual void move(CDirection dir);
  virtual void hide();

  virtual ~CFrame();
  
  /* setters and getters: */

  bool fullscreen()const;

  void visible(bool val);
  bool visible()const;

  void frame(CFrame* theContainer);
  CFrame* frame();
  
  void row(int val);
  int row()const;

  void col(int val);
  int col()const;

  void height(int val);
  int height()const;

  void width(int val);
  int width()const;
};

Properties

int _row, holds the relative coordinate of top row of this border with respect to its container.
int _col, same as _row, but for _col.
int _height, height of the entity.
int _width, width of the entity.
char _border[9], characters used to draw the border:

_border[0], left top
_border[1], top side
_border[2], right top
_border[3], right side
_border[4], right bottom
_border[5], bottom side
_border[6], bottom left
_border[7], left side

bool _visible; Indicates if the border surrounding the entity is to be drawn or not.
CFrame* _frame; holds the container (another CFrame) which has opened this one (owner or container of the current CFrame). _frame will be NULL if this CFrame does not have a container, in which case, it will be full screen and no matter what the values of row, col, width and height are, CFrame will be Full Screen (no border will be drawn)
char* _covered; is a pointer to a character array that hold what was under this frame before being drawn. When the CFrame wants to hides itself, it simple copies the content of this array back on the screen on its own coordinates.

Methods and Constructors

Private Methods

void capture();
if _covered pointer is not pointing to any allocated memory, it will call the bio_capture function to capture the area that is going to be covered by this frame and keeps its address in _covered.

Protected Methods

  • int absRow()const; returns the absolute row of the frame based on the screen's left/top
    it returns the sum of row() of this border plus all the row()s of the _containters
  • int absCol()const; returns the absolute column of the frame based on the screen's left/top
    it returns the sum of col() of this border plus all the col()s of the _containters

Public Methods

  CFrame(int Row=-1, int Col=-1, int Width=-1,int Height=-1,
    bool Visible = false,
    const char* Border=C_BORDER_CHARS,
    CFrame* Frame = (CFrame*)0);
Sets the corresponding attributes to the incoming values in the argument list and set _covered to null
  virtual void draw(int fn=C_FULL_FRAME);
  • First it will capture() the coordinates it is supposed to cover
  • If frame is fullscreen() then it just clears the screen and exits.

Otherwise:

  • If the _visible flag is true, it will draw a box at _row and _col, with size of _width and _height using the _border characters and fills it with spaces. Otherwise it will just draw a box using spaces at the same location and same size.


  virtual void move(CDirection dir);

First it will hide the Frame, then adjust the row and col to more to the "dir" direction and then draws the Frame back on screen.

  virtual void hide();

using bio_restore()it restores the characters behind the Frame back on screen. It will also free the memory pointed by _covered;

  virtual ~CFrame();

It will make sure allocated memories are freed.

  bool fullscreen()const;
  void visible(bool val);
  bool visible()const;
  void frame(CFrame* theContainer);
  CFrame* frame();
  void row(int val);
  int row()const;
  void col(int val);
  int col()const;
  void height(int val);
  int height()const;
  void width(int val);
  int width()const;

These functions set and get the attributes of the CFrame.

CField

CField is an abstract base class that encapsulates the commonalities of all Input Outputs Console Fields which are placeable on a CDialog. All Fields could be Framed, therefore a CField is int

#include "cframe.h"
class CDialog;
class CField : public CFrame{
protected:
  void* _data;
public:
  CField(int Row = 0, int Col = 0,
         int Width = 0, int Height =0,
         void* Data = (void*) 0,
         bool Bordered = false,
         const char* Border=C_BORDER_CHARS);
  ~CField();
  virtual int edit() = 0;
  virtual bool editable() const = 0;
 
 
  virtual void set(const void* data) = 0;
  virtual void* data();
 
  void container(CDialog* theContainer);
  CDialog* container();
};

Attributes

  void* _data;

Will hold the address of any type of data a CField can hold.

Constructors and Methods

  CField(int Row = 0, int Col = 0,
         int Width = 0, int Height =0,
         void* Data = (void*) 0,
         bool Bordered = false,
         const char* Border=C_BORDER_CHARS);

Passes the corresponding attributes to it's parents constructor and then sets the _data attribute to the incoming Data argument.

  ~CField();

Empty Destructor

  virtual int edit() = 0;
  virtual bool editable() const = 0;
  virtual void set(const void* data) = 0;

Enforce the children to implement;

  • an edit() method
  • an editable() method that returns true if the class is to edit data and false if the class is to only display data.
  • a set() method to set the _data attribute to the data the class is to work with.
  virtual void* data();

Returns _data.

  void container(CDialog* theContainer);
  CDialog* container();

Sets and Gets the _frame attribute of CFrame by calling CFrame::frame() method. Make sure to cast The CDialog to CFrame when setting and cast CFrame to CDialog when getting

CLabel

A readonly Field that encapsulates bio_display() function. (i.e it is responsible to display a short character string on the display) CLable although, by inheritance is Frame, but it is never '''bordered'''.
#include "cfield.h"
class CLabel :  public CField{
   int _length;
 public:
   CLabel(const char *Str, int Row, int Col,
     int Len = -1);
   CLabel(int Row, int Col, int Len);
   CLabel(const CLabel& L);
   ~CLabel();
   void draw(int fn=C_NO_FRAME) ;
   int edit();
   bool editable()const;
   void set(const void* str);
};

Attributes

   int _length;

Constructors / Destructor

   CLabel(const char *Str, int Row, int Col,
     int Len = -1);
   CLabel(int Row, int Col, int Len);
   CLabel(const CLabel& L);
   ~CLabel();

Methods

   void draw(int fn=C_NO_FRAME) ;
   int edit();
   bool editable()const;
   void set(const void* str);

CDialog

#include "cgh.h"
#include "cframe.h"
class CField;
 
class CDialog: public CFrame{
  private:
  int _fnum;
/* Suggestion: */
  int _curidx;
  CField* _fld[C_MAX_NO_FIELDS];
  bool _dyn[C_MAX_NO_FIELDS];
/* you can do this part any way you like only if it covers the minimum requirements of the assignment*/
/* talk to your professor if you want to do it differently */
  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();
};

CLineEdit

#pragma once
#include "cfield.h"

class CLineEdit: public CField{
  bool _dyn;
  int _maxdatalen;
  int* _insertmode;
  int _curpos;
  int _offset;
public:
  CLineEdit(char* Str, int Row, int Col, int Width,
    int Maxdatalen, int* Insertmode, 
    bool Bordered = false,
          const char* Border=C_BORDER_CHARS);
  CLineEdit(int Row, int Col, int Width,
    int Maxdatalen, int* Insertmode, 
    bool Bordered = false,
          const char* Border=C_BORDER_CHARS);
  ~CLineEdit();
  void draw(int Refresh = C_FULL_FRAME);
  int edit();
  bool editable()const;
  void  set(const void* Str);
};

CButton

#pragma once
#include "cfield.h"
 class  CButton: public CField{
   int _length;
 public:
   CButton(const char *Str, int Row, int Col, 
            bool Bordered = true,
            const char* Border=C_BORDER_CHARS);
   virtual ~CButton();
   void draw(int rn=C_FULL_FRAME);
   int edit();
   bool editable()const;
   void set(const void* str);
 };

CCheck

#include "cfield.h"
#include "clabel.h"
class CCheck : public CField{
  int _flag;
  int _radio;
  char _format[4];
  CLabel Label;
public:
  CCheck(bool Checked,const char* Format, const char* Text, int Row, int Col, int Width, bool IsRadio = false);
  CCheck(const CCheck& C);
  void draw(int fn = C_NO_FRAME) ;
  int edit();
  bool editable()const;
  void set(const void* flag);
  void *data();
  bool checked()const;
  void checked(bool val);
};

CMenuItem

#include "cfield.h"
class CMenuItem:public CField{
  int  _selected;
  char _format[3];
  char* _text;
public:
  CMenuItem(bool Selected,const char* Format, const char* Text, int Row, int Col, int Width);
  CMenuItem(const CMenuItem &CM);
  virtual ~CMenuItem(void);
  void draw(int fn = C_NO_FRAME) ;
  int edit();
  bool editable()const;
  void set(const void* Selected);
  bool selected()const;
  void selected(bool val);
  const char* Text();
};

CValEdit

#include "clineedit.h"

class CValEdit: public CLineEdit{
  void (*_help)(MessageStatus, CDialog&);
  bool (*_validate)(const char*, CDialog&);
public:
  CValEdit(char* Str, int Row, int Col, int Width,
        int Maxdatalen, int* Insertmode,
        bool (*Validate)(const char* , CDialog&) = NO_VALDFUNC, 
        void (*Help)(MessageStatus, CDialog&) = NO_HELPFUNC,
        bool Bordered = false,
        const char* Border=C_BORDER_CHARS);
  CValEdit(int Row, int Col, int Width,
        int Maxdatalen, int* Insertmode,
        bool (*Validate)(const char* , CDialog&) = NO_VALDFUNC, 
        void (*Help)(MessageStatus, CDialog&) = NO_HELPFUNC,
        bool Bordered = false,
        const char* Border=C_BORDER_CHARS);
  int edit();
};

CText

#include "strarr.h"
#include "cfield.h"
class BFrame;

class CText:public CField{
  WHATEVER _lines;  // a collection of lines in a text 
  bool _readOnly;
  int _curpos;
  int _fieldlen;
  int _maxdatalen; 
  int* _insertmode;
  int _offset;
  int _lcurpos;
  int _loffset;
 // bool _dynamic;
public:
  CText(int Row, int Col, int Width, int Height, bool Readonly,
    int* Insertmode, const char* Border=C_BORDER_CHARS);
  CText(const char* Str, int Row, int Col, int Width, int Height, bool Readonly,
    int* Insertmode, const char* Border=C_BORDER_CHARS);
  void draw(int fn = C_FULL_FRAME);

  void set(const void *Str);
  void *data();

  int edit();
  bool  editable()const;
  bool readOnly();
  void readOnly(bool val);
  ~CText();
};


C

#include "cfield.h"
#include "ccheck.h"

class CCheckList;
class CCheckList : public CField{
  ?????? 
  bool _radio;
  char _format[4];
  int _cnt;
  unsigned int _flags;
public:
  CCheckList(const char* Format, int Row, int Col, int Width,bool radio, bool Bordered = true,const char* Border=C_BORDER_CHARS);
  CCheckList& add(const char* Text, bool selected = false);
  void draw(int fn = C_FULL_FRAME);
  int edit();
  void* data();
  void set(const void* data);
  bool& operator[](unsigned int index);
  bool editable()const;
  bool radio();
  void radio(bool val);
  ~CCheckList(void);
};
  1. include "cfield.h"
  2. include "cmenuitem.h"

class Cmenu;

==class CMenu : public CField{

 ????? 

public:

 CMenu(const char* Format, int Row, int Col, int Width, bool Bordered = true,const char* Border=C_BORDER_CHARS);
 CMenu& add(const char* Text, bool selected = false);
 void draw(int fn = C_FULL_FRAME);
 int edit();
 void* data();
 void set(const void* data);
 int selectedIndex();
 int selectedIndex(unsigned int index);
 const char* selectedText();
 void selectedText(char *text);
 bool editable()const;
 ~CMenu(void);

};

Make File

prj: iol.o fwborder.o fwfield.o fwlabel.o fwlineedit.o fwbutton.o  fwcheck.o fwveditline.o fwdialog.o fwtext.o strarr.o testmains.o
	c++  iol.o fwborder.o fwfield.o fwlabel.o fwlineedit.o fwbutton.o  fwcheck.o fwveditline.o fwdialog.o fwtext.o strarr.o testmains.o \
	-lncurses -oprj

iol.o: iol.c iol.h
	cc -c iol.c

fwborder.o: fwborder.cpp fwborder.h confw.hiol.h
	c++ -c fwborder.cpp 

fwfield.o: fwfield.cpp fwfield.h fwborder.h confw.h iol.h
	c++ -c fwfield.cpp 

fwlabel.o: fwlabel.cpp fwlabel.h fwfield.h fwborder.h confw.h iol.h
	c++ -c fwlabel.cpp 

fwlineedit.o: fwlineedit.cpp fwlineedit.h fwfield.h fwborder.h confw.h iol.h
	c++ -c fwlineedit.cpp 

fwbutton.o: fwbutton.cpp fwbutton.h fwfield.h fwborder.h confw.h iol.h
	c++ -c fwbutton.cpp 

fwcheck.o: fwcheck.cpp fwcheck.h fwlabel.h fwfield.h fwborder.h confw.h iol.h
	c++ -c fwcheck.cpp 

fwveditline.o: fwveditline.cpp fwveditline.h fwlineedit.h fwfield.h fwborder.h confw.h iol.h
	c++ -c fwveditline.cpp 
	 
fwtext.o: fwtext.cpp fwtext.h fwlineedit.h strarr.h fwfield.h fwborder.h confw.h iol.h
	c++ -c fwtext.cpp          

strarr.o: strarr.cpp strarr.h 
	c++ -c strarr.cpp          

fwdialog.o: fwdialog.cpp fwdialog.h fwfield.h fwborder.h confw.h iol.h
	c++ -c fwdialog.cpp 
	 
testmains.o: testmains.cpp fwdialog.h strarr.h fwtext.h fwveditline.h fwcheck.h fwbutton.h fwlineedit.h fwlabel.h fwfield.h fwborder.h iol.h
	c++ -c testmains.cpp