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 / hello.C < prev    next >
C/C++ Source or Header  |  1992-02-27  |  5KB  |  181 lines

  1. /*  @(#)hello.C    1.21 1/23/92 */
  2. /*
  3.   Dies ist eine 'Hello-world'-Anwendung.
  4.   Auf Mouseclick wird ein Hi-Button erzeugt.
  5. */
  6.  
  7. #include <Gina/Gina.h>
  8.  
  9. #ifdef INCLUDE_SRC
  10. #include "Gina.C"
  11. #endif
  12.  
  13. //---------------------------------------------------------------------
  14. class MyPair : public GnObject {
  15.   public:
  16.     int x, y;
  17.     MyPair () { x = 0; y = 0; }
  18.     MyPair ( int px, int py ) { x = px; y = py; }
  19. };
  20.  
  21. DefGenericListWithIterator(MyPairList,MyPair,MyPairListIterator);
  22. ImplGenericListWithIterator(MyPairList,MyPairListIterator);
  23.  
  24. //---------------------------------------------------------------------
  25. class hello_world_application : public GnApplication {
  26.   public:
  27.     GnDocument* create_document ();
  28.  
  29.     char * app_class () { return "HelloWorld" ; }
  30.     char * app_signature() { return "hello" ; }
  31.     char * app_file_type() { return ".hello" ; }
  32. };
  33.  
  34. class hello_world_document : public GnDocument {
  35.   public:
  36.     GnView *create_view();
  37.     void create_windows (int new_width = 0, int new_height = 0);
  38.     void add_hi (int x, int y);
  39.     void rem_hi ( caddr_t, caddr_t );
  40.     void clear_all ( caddr_t, caddr_t );
  41.     Boolean write_to_stream ( ostream & );
  42.     Boolean read_from_stream ( istream & );
  43.     void draw_his();
  44.   private:
  45.     MyPairList hi_list;
  46. };
  47.  
  48.  
  49. class hello_world_view : public GnView {
  50.   public:
  51.     hello_world_view ( GnDocument *doc );
  52.     void draw ( int count, int x, int y, Dimension width, Dimension height );
  53.     void button_press ( int code, int repetition, int x, int y );
  54. };
  55.  
  56. //---------------------------------------------------------------------
  57. GnDocument * hello_world_application ::
  58. create_document () { return new hello_world_document; }
  59.  
  60. //---------------------------------------------------------------------
  61. GnView * hello_world_document ::
  62. create_view () { return (new hello_world_view( this )); }
  63.  
  64. void hello_world_document ::
  65. create_windows (int new_width, int new_height)
  66. {
  67.     Gina_Debug_NL ("hello_world_document :: create_windows");
  68.     main_shell = new GnDocumentShellWithScroller( this, new_width, new_height );
  69.     main_shell->create( GnApplication::get()->get_application_shell(),
  70.                 "mainShell" );
  71.     main_shell->add_menu_command( "Hello", "Remove Last Hi",
  72.                   CALLBACK(hello_world_document,rem_hi,this) );
  73.     main_shell->add_menu_command( "Hello", "Clear All",
  74.                     CALLBACK(hello_world_document,clear_all,this) );
  75. }
  76.  
  77. void hello_world_document ::
  78. add_hi (int x, int y)
  79. {
  80.     Gina_Debug_NL ("hello_world_document :: add_hi");
  81.     MyPair *xy = new MyPair( x, y );
  82.     hi_list.Prepend( xy );
  83. }
  84.  
  85. void hello_world_document ::
  86. rem_hi ( caddr_t, caddr_t )
  87. {
  88.     Gina_Debug_NL ("hello_world_document :: rem_hi");
  89.     if ( hi_list.Length() > 0 ) {
  90.     MyPair *xy = hi_list.RemoveFirst();
  91.     main_view->invalidate_rectangle( xy->x, xy->y-18, 20, 20 );
  92.     delete( xy );
  93.     main_view->force_redraw();
  94.     }
  95. }
  96.  
  97. void hello_world_document ::
  98. clear_all ( caddr_t, caddr_t )
  99. {
  100.     if ( hi_list.Length() > 0 ) {
  101.     MyPair *xy;
  102.     while ( hi_list.Length() > 0 ) {
  103.         xy = hi_list.RemoveFirst();
  104.         delete( xy );
  105.     }
  106.     main_view->invalidate_rectangle( 0, 0, 200, 200 );
  107.     main_view->force_redraw();
  108.     }
  109. }
  110.  
  111. Boolean hello_world_document ::
  112. write_to_stream( ostream &ofs )
  113. {
  114.     MyPair *xy;
  115.     ofs << hi_list.Length() << endl;
  116.     for( MyPairListIterator it(hi_list); ! it.Off(); it.Forth() ) {
  117.     xy = it.Item();
  118.     ofs << xy->x << " " << xy->y << endl;
  119.     }
  120.     return True;
  121. }
  122.  
  123. Boolean hello_world_document ::
  124. read_from_stream ( istream &ifs )
  125. {
  126.     int no_obj, x, y;
  127.     ifs >> no_obj;
  128.     for ( int i = 0; i < no_obj; i++ ) {
  129.     ifs >> x >> y;
  130.     MyPair *xy = new MyPair(x,y);
  131.     hi_list.Prepend( xy );
  132.     }
  133.     return True;
  134. }
  135.  
  136. void hello_world_document ::
  137. draw_his()
  138. {
  139.     MyPair *xy;
  140.     for(MyPairListIterator it(hi_list); ! it.Off(); it.Forth() ) {
  141.     xy = it.Item();
  142.     main_view->draw_glyphs (xy->x, xy->y, "Hi!");
  143.     }
  144. }
  145.  
  146. //---------------------------------------------------------------------
  147. hello_world_view ::
  148. hello_world_view ( GnDocument *doc )
  149. : GnView( doc )
  150. {
  151.     setR_width( 400 );
  152.     setR_height( 400 );
  153. }
  154.  
  155. void hello_world_view ::
  156. draw ( int, int, int, Dimension, Dimension )
  157. {
  158.     Gina_Debug_NL ("hello_world_view :: draw");
  159.     draw_rectangle (24, 16, 87, 19);
  160.     draw_glyphs (30, 30, "Hello, world!");
  161.     ((hello_world_document*)document)->draw_his();
  162. }
  163.  
  164. void hello_world_view ::
  165. button_press ( int, int, int x, int y )
  166. {
  167.     Gina_Debug_NL( "hello_world_view :: button_press" );
  168.     ((hello_world_document*)document)->add_hi( x, y );
  169.     force_redraw();
  170. }
  171.  
  172. //---------------------------------------------------------------------
  173. int main (unsigned int argc, char ** argv)
  174. {
  175.     hello_world_application hwa;
  176. #ifdef GINA_DEBUG
  177.     hwa.set_sync();
  178. #endif
  179.     hwa.run( argc, argv );
  180. }
  181.