Category : Files from Magazines
Archive   : MAR93.ZIP
Filename : CPROG.ASC

 
Output of file : CPROG.ASC contained in archive : MAR93.ZIP
_C PROGRAMMING COLUMN_
by Al Stevens

[LISTING ONE]

// -------- listbox.h

#ifndef LISTBOX_H
#define LISTBOX_H

#include "textbox.h"

const int LISTSELECTOR = 4; // selected list box entry
class ListBox : public TextBox {
Bool addmode; // adding extended selections mode
int anchorpoint; // anchor point for extended selections
int selectcount; // count of selected items
virtual void SetColors();
protected:
int selection; // current selection
virtual void ClearSelection();
public:
ListBox(char *ttl, int lf, int tp, int ht, int wd,
DFWindow *par) : TextBox(ttl, lf, tp, ht, wd, par)
{ OpenWindow(); }
ListBox(char *ttl, int ht, int wd, DFWindow *par)
: TextBox(ttl, ht, wd, par)
{ OpenWindow(); }
ListBox(int lf, int tp, int ht, int wd, DFWindow *par)
: TextBox(lf, tp, ht, wd, par)
{ OpenWindow(); }
ListBox(int ht, int wd, DFWindow *par) : TextBox(ht,wd,par)
{ OpenWindow(); }
ListBox(char *ttl) : TextBox(ttl)
{ OpenWindow(); }
virtual ~ListBox()
{ if (windowstate != CLOSED) CloseWindow(); }
// -------- listbox API messages
virtual void OpenWindow();
virtual void CloseWindow();
virtual void Paint();
virtual void Keyboard(int key);
virtual void SetSelection(int sel);
virtual void ButtonReleased(int mx, int my);
virtual void LeftButton(int mx, int my);
virtual void DoubleClick(int mx, int my);
virtual void Choose();
virtual void ScrollUp();
virtual void ScrollDown();
virtual void ScrollRight();
virtual void ScrollLeft();
virtual void PageUp();
virtual void PageDown();
virtual void PageRight();
virtual void PageLeft();
};
#endif




[LISTING TWO]

// ------------ listbox.cpp
#include "listbox.h"
#include "keyboard.h"

// ----------- common constructor code
void ListBox::OpenWindow()
{
windowtype = ListboxWindow;
if (windowstate == CLOSED)
TextBox::OpenWindow();
selection = -1;
addmode = False;
anchorpoint = -1;
selectcount = 0;
SetColors();
}
// -------- set the fg/bg colors for the window
void ListBox::SetColors()
{
colors.fg = YELLOW;
colors.bg = BLUE;
colors.sfg = LIGHTGRAY;
colors.sbg = BLACK;
colors.ffg = LIGHTGRAY;
colors.fbg = BLUE;
colors.hfg = BLACK;
colors.hbg = LIGHTGRAY;
}
void ListBox::CloseWindow()
{
TextBox::CloseWindow();
}
void ListBox::ClearSelection()
{
if (selection != -1)
WriteTextLine(selection, colors.fg, colors.bg);
}
void ListBox::SetSelection(int sel)
{
ClearSelection();
if (sel >= 0 && sel < wlines) {
selection = sel;
WriteTextLine(sel, colors.sfg, colors.sbg);
}
}
void ListBox::Keyboard(int key)
{
int sel = selection; // (ClearSelection changes selection)
switch (key) {
case UP:
if (sel > 0) {
ClearSelection();
if (sel == wtop)
ScrollDown();
SetSelection(sel-1);
}
return;
case DN:
if (sel < wlines-1) {
ClearSelection();
if (sel == wtop+ClientHeight()-1)
ScrollUp();
SetSelection(sel+1);
}
return;
case '\r':
Choose();
return;
default:
break;
}
TextBox::Keyboard(key);
}
// ---------- paint the listbox
void ListBox::Paint()
{
TextBox::Paint();
if (text != NULL)
WriteTextLine(selection, colors.sfg, colors.sbg);
}
void ListBox::ScrollUp()
{
TextBox::ScrollUp();
WriteTextLine(selection, colors.sfg, colors.sbg);
}
void ListBox::ScrollDown()
{
TextBox::ScrollDown();
WriteTextLine(selection, colors.sfg, colors.sbg);
}
void ListBox::ScrollRight()
{
TextBox::ScrollRight();
WriteTextLine(selection, colors.sfg, colors.sbg);
}
void ListBox::ScrollLeft()
{
TextBox::ScrollLeft();
WriteTextLine(selection, colors.sfg, colors.sbg);
}
void ListBox::PageUp()
{
TextBox::PageUp();
WriteTextLine(selection, colors.sfg, colors.sbg);
}
void ListBox::PageDown()
{
TextBox::PageDown();
WriteTextLine(selection, colors.sfg, colors.sbg);
}
void ListBox::PageRight()
{
TextBox::PageRight();
WriteTextLine(selection, colors.sfg, colors.sbg);
}
void ListBox::PageLeft()
{
TextBox::PageLeft();
WriteTextLine(selection, colors.sfg, colors.sbg);
}
// ---------- Left mouse button was clicked
void ListBox::LeftButton(int mx, int my)
{
if (my != prevmouseline) {
if (ClientRect().Inside(mx, my)) {
int y = my - ClientTop();
if (wlines && y < wlines-wtop)
SetSelection(wtop+y);
}
}
DFWindow::LeftButton(mx, my);
}
void ListBox::DoubleClick(int mx, int my)
{
if (ClientRect().Inside(mx, my)) {
my -= ClientTop();
if (wlines && my < wlines-wtop)
Choose();
}
DFWindow::DoubleClick(mx, my);
}
void ListBox::ButtonReleased(int, int)
{
prevmouseline = -1;
}
void ListBox::Choose()
{
// --- does nothing yet
}



[LISTING THREE]


// ----------- pbutton.h
#ifndef PBUTTON_H
#define PBUTTON_H

#include "textbox.h"
class PushButton : public TextBox {
virtual void SetColors();
Bool pressed;
DFWindow *owner; // window that gets the command
void (DFWindow::*cmdfunction)(); // selection function
public:
PushButton(char *lbl, int lf, int tp, DFWindow *par);
virtual ~PushButton()
{ if (windowstate != CLOSED) CloseWindow(); }
// -------- API messages
virtual void OpenWindow();
virtual void CloseWindow();
virtual Bool SetFocus();
virtual void ResetFocus();
virtual void Paint();
virtual void Shadow();
virtual void Keyboard(int key);
virtual void LeftButton(int mx, int my);
virtual void ButtonReleased(int mx, int my);
virtual void MouseMoved(int mx, int my);
virtual void KeyReleased();
virtual void PressButton();
virtual void ReleaseButton();
virtual void ButtonCommand();
void SetButtonFunction(DFWindow *wnd,
void (DFWindow::*cmdfunc)())
{ owner = wnd; cmdfunction = cmdfunc; }
};
#endif


[LISTING FOUR]

// ------------- pbutton.cpp
#include "pbutton.h"
#include "desktop.h"

PushButton::PushButton(char *lbl, int lf, int tp, DFWindow *par)
: TextBox(lf, tp, 1, strlen(lbl)+2, par)
{
OpenWindow();
String lb(" ");
lb += lbl;
lb += " ";
SetText(lb);
}
// ----------- common constructor code
void PushButton::OpenWindow()
{
windowtype = PushButtonWindow;
if (windowstate == CLOSED)
TextBox::OpenWindow();
SetColors();
pressed = False;
cmdfunction = NULL;
}
void PushButton::CloseWindow()
{
TextBox::CloseWindow();
}
// -------- set the fg/bg colors for the window
void PushButton::SetColors()
{
colors.fg = BLACK;
colors.bg = CYAN;
colors.sfg = WHITE;
colors.sbg = CYAN;
colors.ffg = BLACK;
colors.fbg = CYAN;
colors.hfg = DARKGRAY; // Inactive FG
colors.hbg = CYAN; // Inactive BG
shortcutfg = RED;
}
void PushButton::Paint()
{
if (visible) {
COLORS fg;
COLORS bg;
if (!pressed) {
if (isEnabled()) {
if (this == desktop.InFocus())
fg = colors.sfg;
else
fg = colors.fg;
WriteShortcutLine(0, fg, colors.bg);
}
else
WriteTextLine(0, colors.hfg, colors.hbg);
}
else {
// ---- display a pressed button
fg = ClientBG();
bg = Parent()->ClientBG();
int wd = Width();
WriteWindowChar(' ', 0, 0, fg, bg);
for (int x = 0; x < wd; x++) {
WriteWindowChar(220, x+1, 0, fg, bg);
WriteWindowChar(223, x+1, 1, fg, bg);
}
}
}
}
void PushButton::Shadow()
{
if (visible && (attrib & SHADOW)) {
COLORS bg = Parent()->ClientBG();
int wd = Width();
WriteWindowChar(220, wd, 0, BLACK, bg);
for (int x = 1; x <= wd; x++)
WriteWindowChar(223, x, 1, BLACK, bg);
}
}
Bool PushButton::SetFocus()
{
TextBox::SetFocus();
Paint();
return True;
}
void PushButton::ResetFocus()
{
TextBox::ResetFocus();
Paint();
}
void PushButton::Keyboard(int key)
{
if (key == '\r')
PressButton();
else
TextBox::Keyboard(key);
}
void PushButton::LeftButton(int mx, int my)
{
if (ClientRect().Inside(mx,my)) {
PressButton();
CaptureFocus();
}
}
void PushButton::MouseMoved(int mx, int my)
{
if (desktop.FocusCapture() == this)
if (ClientRect().Inside(mx,my))
PressButton();
else
ReleaseButton();
}
void PushButton::ButtonReleased(int, int)
{
ReleaseFocus();
ButtonCommand();
}
void PushButton::KeyReleased()
{
ButtonCommand();
}
void PushButton::ButtonCommand()
{
if (pressed) {
ReleaseButton();
if (cmdfunction != NULL && owner != NULL)
(owner->*cmdfunction)();
}
}
void PushButton::PressButton()
{
if (!pressed) {
pressed = True;
Paint();
}
}
void PushButton::ReleaseButton()
{
if (pressed) {
pressed = False;
Paint();
Shadow();
}
}



[LISTING FIVE]

// ----------- button.h
#ifndef BUTTON_H
#define BUTTON_H

#include "textbox.h"
class Button : public TextBox {
virtual void SetColors();
Bool setting;
protected:
char setchar;
Button(char *lbl, int lf, int tp, DFWindow *par);
virtual ~Button()
{ if (windowstate != CLOSED) CloseWindow(); }
public:
// -------- API messages
virtual void OpenWindow();
virtual void CloseWindow();
virtual Bool SetFocus();
virtual void ResetFocus();
virtual void Paint();
virtual void Keyboard(int key);
virtual void LeftButton(int mx, int my);
virtual void InvertButton();
virtual void PushButton();
virtual void ReleaseButton();
Bool Setting() { return setting; }
};
#endif

[LISTING SIX]

// ------------- button.cpp
#include "button.h"
#include "desktop.h"
Button::Button(char *lbl, int lf, int tp, DFWindow *par)
: TextBox(lf, tp, 1, strlen(lbl)+5, par)
{
OpenWindow();
String lb("( ) ");
lb += lbl;
lb += " ";
SetText(lb);
}
// ----------- common constructor code
void Button::OpenWindow()
{
if (windowstate == CLOSED)
TextBox::OpenWindow();
SetColors();
setting = False;
setchar = ' ';
}
void Button::CloseWindow()
{
TextBox::CloseWindow();
}
// -------- set the fg/bg colors for the window
void Button::SetColors()
{
colors.fg =
colors.sfg =
colors.ffg =
colors.hfg = Parent()->ClientFG();
colors.bg =
colors.sbg =
colors.fbg =
colors.hbg = Parent()->ClientBG();
shortcutfg = RED;
}
void Button::Paint()
{
if (visible) {
(*text)[1] = setting ? setchar : ' ';
if (isEnabled())
WriteShortcutLine(0, colors.fg, colors.bg);
else
WriteTextLine(0, colors.hfg, colors.hbg);
}
}
Bool Button::SetFocus()
{
TextBox::SetFocus();
desktop.cursor().normalcursor();
desktop.cursor().SetPosition(Left()+1, Top());
desktop.cursor().Show();
return True;
}
void Button::ResetFocus()
{
TextBox::ResetFocus();
desktop.cursor().Hide();
}
void Button::Keyboard(int key)
{
if (key == ' ')
InvertButton();
else
TextBox::Keyboard(key);
}
void Button::LeftButton(int mx, int my)
{
if (ClientRect().Inside(mx,my))
InvertButton();
}
void Button::InvertButton()
{
if (setting)
ReleaseButton();
else
PushButton();
}
void Button::PushButton()
{
setting = True;
Paint();
}
void Button::ReleaseButton()
{
setting = False;
Paint();
}

[LISTING SEVEN]

// ----------- radio.h
#ifndef RADIO_H
#define RADIO_H

#include "button.h"
class RadioButton : public Button {
public:
RadioButton(char *lbl, int lf, int tp, DFWindow *par);
virtual ~RadioButton()
{ if (windowstate != CLOSED) CloseWindow(); }
// -------- API messages
virtual void InvertButton();
virtual void PushButton();
virtual void OpenWindow();
};
#endif


[LISTING EIGHT]

// ------------- radio.cpp
#include "radio.h"
#include "desktop.h"

RadioButton::RadioButton(char *lbl, int lf, int tp,
DFWindow *par) : Button(lbl, lf, tp, par)
{
OpenWindow();

setchar = 7;
}
void RadioButton::OpenWindow()
{
if (windowstate == CLOSED)
Button::OpenWindow();
windowtype = RadioButtonWindow;
}
void RadioButton::InvertButton()
{
PushButton();
}
void RadioButton::PushButton()
{
int ht = desktop.screen().Height();
DFWindow **rd = new DFWindow *[ht];
for (int i = 0; i < ht; i++)
rd[i] = NULL;
// - build a table of radio buttons at the same x coordiate
DFWindow *sib = Parent()->First();
while (sib != NULL) {
if (sib->WindowType() == RadioButtonWindow) {
if (sib->Left() == Left()) {
int tp = sib->Top();
if (tp < ht)
rd[tp] = sib;
}
}
sib = sib->Next();
}
// ----- find the start of the radiobutton group
i = Top();
while (i >= 0 && rd[i] != NULL)
--i;
// ---- ignore everthing before the group
while (i >= 0)
rd[i--] = NULL;
// ----- find the end of the radiobutton group
i = Top();
while (i < ht && rd[i] != NULL)
i++;
// ---- ignore everthing past the group
while (i < ht)
rd[i++] = NULL;
// ------ release all the radio buttons in the group
for (i = 0; i < ht; i++)
if (rd[i] != NULL)
((RadioButton *)rd[i])->ReleaseButton();
delete [] rd;
// ----- set the chosen radio button
Button::PushButton();
}

[LISTING NINE]

// ----------- checkbox.h
#ifndef CHECKBOX_H
#define CHECKBOX_H

#include "button.h"
class CheckBox : public Button {
public:
CheckBox(char *lbl, int lf, int tp, DFWindow *par);
virtual ~CheckBox()
{ if (windowstate != CLOSED) CloseWindow(); }
// -------- API messages
virtual void OpenWindow();
};
#endif



[LISTING TEN]

// ------------- checkbox.cpp
#include "checkbox.h"
#include "desktop.h"
CheckBox::CheckBox(char *lbl, int lf, int tp, DFWindow *par) :
Button(lbl, lf, tp, par)
{
OpenWindow();
setchar = 'X';
(*text)[0] = '[';
(*text)[2] = ']';
}
// ----------- common constructor code
void CheckBox::OpenWindow()
{
if (windowstate == CLOSED)
Button::OpenWindow();
windowtype = CheckBoxWindow;
}



  3 Responses to “Category : Files from Magazines
Archive   : MAR93.ZIP
Filename : CPROG.ASC

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/