home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-385-Vol-1of3.iso
/
g
/
gina15.zip
/
demos
/
hello2.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-02-27
|
6KB
|
229 lines
/* @(#)hello2.C 1.9 1/23/92 */
/*
Dies ist eine 'Hello-world'-Anwendung.
Auf Mouseclick wird ein Hi-Button erzeugt.
*/
#include <Gina/Gina.h>
#ifdef INCLUDE_SRC
#include "Gina.C"
#endif
//---------------------------------------------------------------------
class MyPair : public GnObject {
public:
int x, y;
MyPair ( int px, int py ) { x = px; y = py; }
};
DefGenericListWithIterator(MyPairList,MyPair,MyPairListIterator);
ImplGenericListWithIterator(MyPairList,MyPairListIterator);
//---------------------------------------------------------------------
class hello_world_application : public GnApplication {
public:
GnDocument* create_document ();
char * app_class () { return "C++HelloWorld2" ; }
char * app_signature() { return "c++hello2" ; }
char * app_file_type() { return ".hello2" ; }
};
class hello_world_document : public GnDocument {
public:
GnView *create_view();
void create_windows (int new_width, int new_height);
void clear_all_cb( caddr_t, caddr_t );
void clear_all() { hi_list.Clear(); }
void set_all( MyPairList );
void add_hi( MyPair *p ) { hi_list.Prepend( p ); }
MyPair * rem_hi() { return ( ( hi_list.Length() > 0 ) ?
hi_list.RemoveFirst() : 0 ); }
void draw_his();
Boolean write_to_stream( ostream & );
Boolean read_from_stream( istream & );
private:
MyPairList hi_list;
};
class hello_world_view : public GnView {
public:
hello_world_view ( GnDocument *doc );
void draw ( int count, int x, int y, Dimension width, Dimension height );
void button_press ( int code, int repetition, int x, int y );
};
class add_hi : public GnCommand {
public:
add_hi( int x, int y, hello_world_document *, hello_world_view * );
char * name() { return "Add Hi!"; }
protected:
void doit();
void undoit();
MyPair *p;
};
class clear_all_command : public GnCommand {
public:
clear_all_command( MyPairList, hello_world_document *, hello_world_view * );
char * name() { return "Clear All"; }
protected:
void doit();
void undoit();
MyPairList save_list;
};
//---------------------------------------------------------------------
GnDocument * hello_world_application ::
create_document () { return new hello_world_document; }
//---------------------------------------------------------------------
GnView * hello_world_document ::
create_view () { return (new hello_world_view( this )); }
void hello_world_document ::
create_windows (int new_width, int new_height)
{
main_shell = new GnDocumentShellWithScroller( this, new_width, new_height );
main_shell->create( GnApplication::get()->get_application_shell(),
"mainShell" );
main_shell->add_menu_command( "Hello", "Clear All",
CALLBACK(hello_world_document,clear_all_cb,this) );
}
void hello_world_document ::
clear_all_cb( caddr_t, caddr_t )
{
(new clear_all_command(hi_list,this,(hello_world_view*)main_view))
->submit();
}
void hello_world_document ::
set_all( MyPairList new_list )
{
hi_list.Clear();
for(MyPairListIterator it(new_list); ! it.Off(); it.Forth() )
hi_list.Prepend( it.Item() );
}
void hello_world_document ::
draw_his()
{
MyPair *xy;
for(MyPairListIterator it(hi_list); ! it.Off(); it.Forth() ) {
xy = it.Item();
main_view->draw_glyphs (xy->x, xy->y, "Hi!");
}
}
Boolean hello_world_document ::
write_to_stream( ostream &ofs )
{
MyPair *xy;
ofs << hi_list.Length() << endl;
for( MyPairListIterator it(hi_list); ! it.Off(); it.Forth() ) {
xy = it.Item();
ofs << xy->x << " " << xy->y << "\n";
}
return True;
}
Boolean hello_world_document ::
read_from_stream ( istream &ifs )
{
int no_obj, x, y;
ifs >> no_obj;
for ( int i = 0; i < no_obj; i++ ) {
ifs >> x >> y;
MyPair *xy = new MyPair(x,y);
hi_list.Prepend( xy );
}
return True;
}
//---------------------------------------------------------------------
add_hi ::
add_hi( int x, int y, hello_world_document *doc, hello_world_view *view )
: GnCommand( doc, view )
{
p = new MyPair(x, y);
}
void add_hi ::
doit()
{
Gina_Debug_NL ("add_hi :: doit");
((hello_world_document*)document)->add_hi( p );
view->force_redraw();
}
void add_hi ::
undoit()
{
Gina_Debug_NL ("add_hi :: undoit");
(void)((hello_world_document*)document)->rem_hi();
view->force_redraw();
}
//---------------------------------------------------------------------
clear_all_command ::
clear_all_command( MyPairList old_list,
hello_world_document *doc, hello_world_view *view )
: GnCommand( doc, view )
{
save_list.Clear();
MyPair *xy;
while ( xy = doc->rem_hi() )
save_list.Prepend( xy );
}
void clear_all_command ::
doit()
{
Gina_Debug_NL ("clear_all_command :: doit");
((hello_world_document*)document)->clear_all();
view->force_redraw();
}
void clear_all_command ::
undoit()
{
Gina_Debug_NL ("clear_all_command :: undoit");
((hello_world_document*)document)->set_all(save_list);
view->force_redraw();
}
//---------------------------------------------------------------------
hello_world_view ::
hello_world_view ( GnDocument *doc )
: GnView( doc )
{}
void hello_world_view ::
draw ( int, int, int, Dimension, Dimension )
{
Gina_Debug_NL ("hello_world_view :: draw");
draw_rectangle (24, 16, 87, 19);
draw_glyphs (30, 30, "Hello, world!");
((hello_world_document*)document)->draw_his();
}
void hello_world_view ::
button_press ( int, int, int x, int y )
{
Gina_Debug_NL( "hello_world_view :: button_press" );
(new add_hi( x, y, (hello_world_document*)document, this ))->submit();
force_redraw();
}
//---------------------------------------------------------------------
int main (unsigned int argc, char ** argv)
{
hello_world_application hwa;
#ifdef GINA_DEBUG
hwa.set_sync();
#endif
hwa.run( argc, argv );
}