home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Acorn User 2
/
AUCD2.iso
/
program
/
vista.arc
/
c
/
test
< prev
next >
Wrap
Text File
|
1996-02-01
|
17KB
|
729 lines
//
// main test
//
// this program is used as a test for Vista development. It is
// not a particularly good example of the use of the library
// but does use much of the facilities. Hopefully
// it will suffice until I write somethine better.
// David Allison 30/3/95
#include "Vista:vista.h"
#include <stdio.h>
#include <kernel.h>
#include <swis.h>
#include <string.h>
//
// a little counter to show threading
//
class Counter: public Window, public Thread
{
public:
Counter(Task *) ;
~Counter() ;
void run() ;
void close () ;
private:
int count ;
Icon *display ;
int running ;
} ;
Counter::Counter(Task *t)
: Window (t, "counter"),
Thread ("counter")
{
running = 1 ;
count = 0 ;
display = new Icon (this, 0) ;
}
Counter::~Counter()
{
}
void Counter::run()
{
do_open() ;
while (running)
{
display->print ("%d",count++) ;
sleep (100) ;
}
do_close() ;
}
void Counter::close()
{
running = 0 ;
wakeup() ;
}
//
// this class saves something using the DataSave protocol
//
class Saver: public DataSave
{
public:
Saver(Task *task) ;
~Saver() ;
void receive (int action, int task, int my_ref, int your_ref, int data_length, void *data) ; // receive a message
void save(int window, int icon, int x, int y, char *leaf) ;
void save (char *path) ;
} ;
Saver::Saver (Task *task) : DataSave (task)
{
}
Saver::~Saver()
{
}
void Saver::receive (int action, int task, int my_ref, int your_ref, int data_length, void *data)
{
switch ((Task::events)action)
{
case Task::Message_DataSaveAck:
{ // save to this file
DataSave::saveackdata *s = (DataSave::saveackdata *)data ;
save (s->pathname) ;
dataload (task, your_ref, my_ref, data_length, data) ;
break ;
}
case Task::Message_DataLoadAck:
// delete this ;
break ;
}
}
void Saver::save (int window, int icon, int x, int y, char *leaf)
{
datasave (window, icon, x, y, 1024, 0xfff, leaf) ;
}
void Saver::save (char *path)
{
FILE *fp = fopen (path, "w") ;
if (fp == NULL)
throw ("cant open file") ;
fprintf (fp,"hello world\n") ;
fclose (fp) ;
}
class Loader ;
//
// an icon grid
//
class LibraryDisplay : public IconGrid
{
public:
LibraryDisplay (Task *t, char *tname, int ticon, char *menu = 0) : IconGrid (t,tname,ticon,menu) { set_flag (SORTED) ; }
void menu (MenuItem items[]) ;
void drag_icon (int mx, int my, int buttons, Icon *icon) ;
void drag (int x0, int y0, int x1, int y1, int id) ;
void drag_selection (int mx, int my, int buttons, int x0, int y0, int x1, int y1) ;
} ;
void LibraryDisplay::menu (MenuItem items[])
{
}
void LibraryDisplay::drag_icon (int mx, int my, int buttons, Icon *icon)
{
icon->drag (mx, my, buttons) ;
task->register_drag (this,1) ;
}
void LibraryDisplay::drag_selection (int mx, int my, int buttons,int x0, int y0, int x1, int y1)
{
do_drag (5, x0, y0, x1, y1) ;
task->register_drag (this,1) ;
}
void LibraryDisplay::drag (int x0, int y0, int x1, int y1, int id)
{
if (id != 1) // not my drag?
{
IconGrid::drag (x0,y0,x1,y1,id) ;
return ;
}
int block[5] ;
_kernel_swi_regs r ;
_kernel_oserror *e ;
r.r[1] = (int)block ;
if ((e = _kernel_swi (Wimp_GetPointerInfo, &r, &r)) != NULL)
throw (e) ;
Saver *saver = new Saver(task) ;
saver->save (block[3], block[4], block[0], block[1], "newfile") ;
}
//
// this class loads files into the icon grid
//
class Loader: public DataSave
{
public:
Loader(Task *task, IconGrid *grid) ;
~Loader() ;
void receive (int action, int task, int my_ref, int your_ref, int data_length, void *data) ; // receive a message
private:
IconGrid *grid ;
} ;
Loader::Loader (Task *task, IconGrid *grid) : DataSave (task)
{
this->grid = grid ;
}
Loader::~Loader()
{
}
void Loader::receive (int action, int task, int my_ref, int your_ref, int data_length, void *data)
{
switch ((Task::events)action)
{
case Task::Message_DataSave: // save to this file
datasaveack (your_ref, (DataSave::saveackdata *)data, "<Wimp$Scrap>") ;
break ;
case Task::Message_DataLoad:
{
DataSave::saveackdata *s = (DataSave::saveackdata *)data ;
char *leaf = strrchr (s->pathname, '.') ;
if (leaf == NULL)
leaf = s->pathname ;
else
leaf++ ;
grid->insert_icon (leaf) ;
dataloadack (task, your_ref, my_ref, data_length, data) ;
break ;
}
}
}
//
// declare a class which is the main task
//
class Vista : public Task
{
public:
Vista() ;
void click(int x, int y, int button, int icon) ; // I want iconbar clicks
char *get_iconbar_menu (int icon) ;
void menu (MenuItem items[]) ; // iconbar menu hit
char *help (int mx, int my, int buttons, int w, int i) ;
Window *mainwin ; // main window
Window *win2 ; // another window
LibraryDisplay *lib ; // the icon grid
Window *txt ; // a text window
Loader *loader ; // icon grid file loader
} ;
//
// declare the main task variable
//
Vista *vista ;
//
// this class is for an action button
//
class Compile : public Icon
{
public:
Compile(Window *w, int icon) ;
void click(int mx, int my, int button, int icon) ;
char *help (int mx, int my, int buttons) ;
} ;
Compile::Compile(Window *w, int icon) : Icon (w,icon)
{
}
void Compile::click(int mx, int my, int button, int icon)
{
// Counter *c = new Counter (window->task) ;
// c->start() ;
window->task->show_threads() ;
Window *w = new Window (window->task, "counter") ;
Icon *ic = new Icon (w, 0) ;
w->do_open() ;
for (int i = 0 ; i < 20 ; i++)
{
ic->print ("%d",i) ;
window->task->sleep (100) ;
}
w->do_close() ;
delete ic ;
delete w ;
}
char *Compile::help (int mx, int my, int buttons)
{
return "This is the counter icon|MClick select to start a new counter" ;
}
//
// this class is for an action button which adds a new icon to the
// scrolling list
//
class Add : public Icon
{
public:
Add(Window *w, int icon, IconGrid *scroll) ;
void click(int mx, int my, int button, int icon) ;
private:
IconGrid *scroll ;
int num ;
} ;
Add::Add(Window *w, int icon, IconGrid *scroll) : Icon (w,icon)
{
this->scroll = scroll ;
num = 0 ;
}
void Add::click(int mx, int my, int button, int icon)
{
char buf[256] ;
sprintf (buf,"Icon %d",num++) ;
scroll->insert_icon (buf) ;
}
//
// this class adds a new icon to the icon grid
//
class AddIcon : public Icon
{
public:
AddIcon(Window *w, int icon, IconGrid *grid) ;
void click(int mx, int my, int button, int icon) ;
private:
IconGrid *grid ;
int num ;
} ;
AddIcon::AddIcon(Window *w, int icon, IconGrid *grid) : Icon (w,icon)
{
this->grid = grid ;
num = 0 ;
}
void AddIcon::click(int mx, int my, int button, int icon)
{
char buf[256] ;
sprintf (buf,"Icon %d",num++) ;
grid->insert_icon (buf) ;
grid->set_title ("%d icons",num) ;
}
//
// a small sub window (a scrolling list)
//
class SubWindow: public IconGrid
{
public:
SubWindow (Window *w, int ticon) : IconGrid (w, "sub1", ticon, "submenu") {}
void menu (MenuItem items[]) ;
char *help (int mx, int my, int buttons, int icon) ;
} ;
void SubWindow::menu (MenuItem items[])
{
}
char *SubWindow::help (int mx, int my, int buttons, int icon)
{
return "This is a scrolling window" ;
}
//
// a dialogue box
//
class TestBox : public DialogueBox
{
public:
TestBox (Task *task) ;
~TestBox() ;
private:
char string[32] ; // string attribute
int number ; // integer attribute
bool option ; // option attribute
bool choices[3] ;
Icon *text, *num ;
} ;
TestBox::TestBox (Task *task)
: DialogueBox (task, "testbox", 10, 11)
{
strcpy (string, "a big hello") ;
number = 100 ;
option = true ;
choices[0] = false ;
choices[1] = true ;
choices[2] = false ;
int x = 1234 ;
text = new Icon (this, 1) ;
text->print ("hello world") ;
num = new Icon (this, 3) ;
num->print ("%d",x) ;
create_attribute (5, string) ;
create_attribute (7, number) ;
create_attribute (8, option) ;
create_attribute (3, choices, 9, 12, 13) ;
}
TestBox::~TestBox()
{
}
//
// action button to open the dialogue box
//
class BoxIcon : public Icon
{
public:
BoxIcon(Window *w, int icon, TestBox *box) ;
void click(int mx, int my, int button, int icon) ;
private:
TestBox *box ;
} ;
BoxIcon::BoxIcon(Window *w, int icon, TestBox *box) : Icon (w,icon)
{
this->box = box ;
}
void BoxIcon::click(int mx, int my, int button, int icon)
{
box->show() ;
}
//
// the main window class
//
class MainWindow : public Window
{
public:
MainWindow(Vista *task) ;
void menu(MenuItem items[]) ;
void pre_menu (Menu *menu, int x, int y, int button, int icon) ;
char *help (int, int, int, int) ;
private:
Icon *compile ; // compile icon
Icon *link ; // link icon
Icon *adjuster ; // adjuster icon
Icon *slider ; // slider icon
Icon *add ; // add to scroll icon
Icon *add_icon ; // add to grid
IconGrid *scroll ; // sub window
TestBox *box ;
Icon *boxicon ;
Window *toolbarl ; // toolbar left
Window *toolbarr ; // toolbar right
Window *toolbart ; // toolbar top
Window *toolbarb ; // toolbar bottom
Icon *popup ;
} ;
char *MainWindow::help (int mx, int my, int buttons, int icon)
{
return "This is the main window" ;
}
MainWindow::MainWindow(Vista *task) : Window (task, "main", "mainmenu")
{
#ifdef __EASY_C
try // catch exceptions
#endif
{
compile = new Compile (this, 0) ;
link = new Icon (this, 1) ;
adjuster = new Adjuster (this, 4,3,5) ;
slider = new Slider (this, 9, 10, 8, 7, 100) ;
scroll = new SubWindow (this, 0) ;
add = new Add (this, 14, scroll) ;
add_icon = new AddIcon (this, 15, task->lib) ;
box = new TestBox (task) ;
boxicon = new BoxIcon (this, 16, box) ;
toolbarl = new Window (this, "anchorl", Window::A_LEFT) ;
toolbarr = new Window (this, "anchorr", Window::A_RIGHT) ;
toolbart = new Window (this, "anchort", Window::A_TOP) ;
toolbarb = new Window (this, "anchorb", Window::A_BOTTOM) ;
popup = new Popup (this, 17, 18, "popup") ;
}
#ifdef __EASY_C
catch (_kernel_oserror *e)
{
_kernel_swi_regs r ;
r.r[0] = (int)e ;
r.r[1] = 0 ;
r.r[2] = (int)task->program ;
_kernel_swi (Wimp_ReportError, &r, &r) ;
task->exit (1) ;
}
catch (char *s)
{
_kernel_oserror err ;
_kernel_swi_regs r ;
err.errnum = 0 ;
strcpy (err.errmess, s) ;
r.r[0] = (int)&err ;
r.r[1] = 0 ;
r.r[2] = (int)task->program ;
_kernel_swi (Wimp_ReportError, &r, &r) ;
task->exit (1) ;
}
#endif
}
//
// menu hit on main window
//
void MainWindow::menu(MenuItem items[])
{
#ifdef __EASY_C
try
#endif
{
switch (items[0])
{
case 1:
{
task->debug->print ("creating save box") ;
Saver saver (task) ;
SaveBox save (task, "xfer_send", "", "File", 0xfff, &saver) ;
save.set_title ("new saver") ;
save.show() ;
task->sleep (&save) ; // sleep until save terminates
::print ("woke up") ;
break ;
}
case 2:
if (items[0].is_ticked())
items[0].untick() ;
else
items[0].tick() ;
break ;
case 5:
{
char str[256] ;
items[1].read (str) ;
char *s = items[1] ;
::print ("item 4: %s, %s",s,str) ;
break ;
}
}
}
#ifdef __EASY_C
catch (char *s)
{
_kernel_oserror err ;
_kernel_swi_regs r ;
err.errnum = 0 ;
strcpy (err.errmess, s) ;
r.r[0] = (int)&err ;
r.r[1] = 0 ;
r.r[2] = (int)"Test" ;
_kernel_swi (Wimp_ReportError, &r, &r) ;
}
#endif
}
//
// a chance to adjust the menu
//
void MainWindow::pre_menu (Menu *menu, int x, int y, int button, int icon)
{
static int i = 0 ;
menu->print ("item1", "Item '%d'", i++) ;
}
//
// construct the task
//
Vista::Vista () : Task ("Test task", "Test", "!Test", "iconbar")
//Vista::Vista () : Task ("Test task", "Test")
{
char buffer [1024] ;
lib = new LibraryDisplay (this, "grid", 0, 0) ;
loader = new Loader (this, lib) ; // a new loader
mainwin = new MainWindow (this) ; // construct the main window
win2 = new MainWindow (this) ;
// make the text window
txt = new Window (this,"txtwin") ;
// a text object
#if 1
TextObject *text = new TextObject (txt, "text1", "a piece of\ntext\nat the top of a\nwindow", 100,-100) ;
// another one
#endif
#if 0
TextObject *text2 = new TextObject (txt, "text2", "another\npiece\nof\ntext", 2000,-2000) ;
#endif
// a line object
LineObject *line1 = new LineObject (txt, "line1", 100, -1000, 1000, -100, 0, 10) ;
#if 1
int font_handle = open_font ("Homerton.Medium.Oblique", 16, 16) ;
int bold_handle = open_font ("Homerton.Bold", 20, 20) ;
FontObject *font = new FontObject (txt, "font1", font_handle, 100, -1000, 900) ;
font->set_indent (300) ;
font->set_font (font_handle) ;
font->add_text ("The ") ;
font->set_colour (255,255,255,255,0,0) ;
font->begin_underline() ;
font->add_text ("quick") ;
font->end_underline() ;
font->set_font (bold_handle) ;
font->set_colour (255,255,255,84,43,14) ;
font->add_text (" Brown") ;
font->set_colour (255,255,255,255,0,0) ;
font->set_font (font_handle) ;
font->set_colour (255,255,255,255,128,0) ;
font->add_text (" fox jumps over the lazy dog. ") ;
font->begin_underline() ;
font->add_text ("Now is the time for all good men to ") ;
font->set_colour (255,255,255,0,0,0) ;
font->set_font (bold_handle) ;
font->add_text ("come") ;
font->set_font (font_handle) ;
font->add_text (" to the aid of the party...") ;
font->add_text ("And again, the quick brown fox jumps over the lazy dog.") ;
font->finish() ;
#endif
SpriteObject *sprite = new SpriteObject (txt, "sprite1", "plane", 1500, -1500, 1) ;
SpriteObject *sprite2 = new SpriteObject (txt, "sprite2", "!test", 600, -600, 1) ;
DrawObject *draw = new DrawObject (txt,"draw", "<Test$Dir>.Draw", 300, -800) ;
SpriteObject *sprite3 = new SpriteObject (txt, "sprite3", "!test1", 300, -800, 1) ;
set_mask (1 << ENULL) ;
}
//
// iconbar click
//
void Vista::click (int x, int y, int button, int icon)
{
lib->do_open() ;
txt->do_open() ;
if (button & 1) // right button?
mainwin->do_open() ;
else
win2->do_open() ;
}
char *Vista::get_iconbar_menu (int icon)
{
return "iconbar" ;
}
//
// menu hit on the icon bar icon
//
void Vista::menu(MenuItem items[])
{
switch (items[0])
{
case 0:
{
DialogueBox proginfo (this, "ProgInfo") ;
proginfo.show() ;
sleep (&proginfo) ;
break ;
}
case 1:
exit() ;
break ;
default:
{
char str[64] ;
sprintf (str,"unknown item %d\n",(int)items[0]) ;
throw (str) ;
}
}
}
char *Vista::help (int mx, int my, int buttons, int w, int i)
{
// debug->print ("Help") ;
return "This is Vista" ;
}
//
// main program: simply construct the task and run it
//
main()
{
#ifdef __EASY_C
try
#endif
{
vista = new Vista() ;
vista->run() ;
}
#ifdef __EASY_C
catch (_kernel_oserror *e)
{
_kernel_swi_regs r ;
r.r[0] = (int)e ;
r.r[1] = 0 ;
r.r[2] = (int)"Test" ;
_kernel_swi (Wimp_ReportError, &r, &r) ;
}
catch (char *s)
{
_kernel_oserror err ;
_kernel_swi_regs r ;
err.errnum = 0 ;
strcpy (err.errmess, s) ;
r.r[0] = (int)&err ;
r.r[1] = 0 ;
r.r[2] = (int)"Test" ;
_kernel_swi (Wimp_ReportError, &r, &r) ;
}
#endif
}