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 >
C/C++ Source or Header  |  1992-02-27  |  6KB  |  229 lines

  1. /*  @(#)hello2.C    1.9 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 ( int px, int py ) { x = px; y = py; }
  18. };
  19.  
  20. DefGenericListWithIterator(MyPairList,MyPair,MyPairListIterator);
  21. ImplGenericListWithIterator(MyPairList,MyPairListIterator);
  22.  
  23. //---------------------------------------------------------------------
  24. class hello_world_application : public GnApplication {
  25.   public:
  26.     GnDocument* create_document ();
  27.  
  28.     char * app_class () { return "C++HelloWorld2" ; }
  29.     char * app_signature() { return "c++hello2" ; }
  30.     char * app_file_type() { return ".hello2" ; }
  31. };
  32.  
  33. class hello_world_document : public GnDocument {
  34.   public:
  35.     GnView *create_view();
  36.     void create_windows (int new_width, int new_height);
  37.     void clear_all_cb( caddr_t, caddr_t );
  38.     void clear_all() { hi_list.Clear(); }
  39.     void set_all( MyPairList );
  40.     void add_hi( MyPair *p ) { hi_list.Prepend( p ); }
  41.     MyPair * rem_hi() { return ( ( hi_list.Length() > 0 ) ?
  42.                  hi_list.RemoveFirst() : 0 ); }
  43.     void draw_his();
  44.     Boolean write_to_stream( ostream & );
  45.     Boolean read_from_stream( istream & );
  46.   private:
  47.     MyPairList hi_list;
  48. };
  49.  
  50. class hello_world_view : public GnView {
  51.   public:
  52.     hello_world_view ( GnDocument *doc );
  53.     void draw ( int count, int x, int y, Dimension width, Dimension height );
  54.     void button_press ( int code, int repetition, int x, int y );
  55. };
  56.  
  57. class add_hi : public GnCommand {
  58.   public:
  59.     add_hi( int x, int y, hello_world_document *, hello_world_view * );
  60.     char * name() { return "Add Hi!"; }
  61.   protected:
  62.     void doit();
  63.     void undoit();
  64.     MyPair *p;
  65. };
  66.  
  67. class clear_all_command : public GnCommand {
  68.   public:
  69.     clear_all_command( MyPairList, hello_world_document *, hello_world_view * );
  70.     char * name() { return "Clear All"; }
  71.   protected:
  72.     void doit();
  73.     void undoit();
  74.     MyPairList save_list;
  75. };
  76.  
  77. //---------------------------------------------------------------------
  78. GnDocument * hello_world_application ::
  79. create_document () { return new hello_world_document; }
  80.  
  81. //---------------------------------------------------------------------
  82. GnView * hello_world_document ::
  83. create_view () { return (new hello_world_view( this )); }
  84.  
  85. void hello_world_document ::
  86. create_windows (int new_width, int new_height)
  87. {
  88.     main_shell = new GnDocumentShellWithScroller( this, new_width, new_height );
  89.     main_shell->create( GnApplication::get()->get_application_shell(),
  90.                 "mainShell" );
  91.     main_shell->add_menu_command( "Hello", "Clear All",
  92.                  CALLBACK(hello_world_document,clear_all_cb,this) );
  93. }
  94.  
  95. void hello_world_document ::
  96. clear_all_cb( caddr_t, caddr_t )
  97. {
  98.     (new clear_all_command(hi_list,this,(hello_world_view*)main_view))
  99.     ->submit();
  100. }
  101.  
  102. void hello_world_document ::
  103. set_all( MyPairList new_list )
  104. {
  105.     hi_list.Clear();
  106.     for(MyPairListIterator it(new_list); ! it.Off(); it.Forth() )
  107.     hi_list.Prepend( it.Item() );
  108. }
  109.  
  110. void hello_world_document ::
  111. draw_his()
  112. {
  113.     MyPair *xy;
  114.     for(MyPairListIterator it(hi_list); ! it.Off(); it.Forth() ) {
  115.     xy = it.Item();
  116.     main_view->draw_glyphs (xy->x, xy->y, "Hi!");
  117.     }
  118. }
  119.  
  120. Boolean hello_world_document ::
  121. write_to_stream( ostream &ofs )
  122. {
  123.     MyPair *xy;
  124.     ofs << hi_list.Length() << endl;
  125.     for( MyPairListIterator it(hi_list); ! it.Off(); it.Forth() ) {
  126.     xy = it.Item();
  127.     ofs << xy->x << " " << xy->y << "\n";
  128.     }
  129.     return True;
  130. }
  131.  
  132. Boolean hello_world_document ::
  133. read_from_stream ( istream &ifs )
  134. {
  135.     int no_obj, x, y;
  136.     ifs >> no_obj;
  137.     for ( int i = 0; i < no_obj; i++ ) {
  138.     ifs >> x >> y;
  139.     MyPair *xy = new MyPair(x,y);
  140.     hi_list.Prepend( xy );
  141.     }
  142.     return True;
  143. }
  144.  
  145. //---------------------------------------------------------------------
  146. add_hi ::
  147. add_hi( int x, int y, hello_world_document *doc, hello_world_view *view )
  148. : GnCommand( doc, view )
  149. {
  150.     p = new MyPair(x, y);
  151. }
  152.  
  153. void add_hi ::
  154. doit()
  155. {
  156.     Gina_Debug_NL ("add_hi :: doit");
  157.     ((hello_world_document*)document)->add_hi( p );
  158.     view->force_redraw();
  159. }
  160.  
  161. void add_hi ::
  162. undoit()
  163. {
  164.     Gina_Debug_NL ("add_hi :: undoit");
  165.     (void)((hello_world_document*)document)->rem_hi();
  166.     view->force_redraw();
  167. }
  168.  
  169. //---------------------------------------------------------------------
  170. clear_all_command ::
  171. clear_all_command( MyPairList old_list,
  172.            hello_world_document *doc, hello_world_view *view )
  173. : GnCommand( doc, view )
  174. {
  175.     save_list.Clear();
  176.     MyPair *xy;
  177.     while ( xy = doc->rem_hi() )
  178.     save_list.Prepend( xy );
  179. }
  180.  
  181. void clear_all_command ::
  182. doit()
  183. {
  184.     Gina_Debug_NL ("clear_all_command :: doit");
  185.     ((hello_world_document*)document)->clear_all();
  186.     view->force_redraw();
  187. }
  188.  
  189. void clear_all_command ::
  190. undoit()
  191. {
  192.     Gina_Debug_NL ("clear_all_command :: undoit");
  193.     ((hello_world_document*)document)->set_all(save_list);
  194.     view->force_redraw();
  195. }
  196.  
  197. //---------------------------------------------------------------------
  198. hello_world_view ::
  199. hello_world_view ( GnDocument *doc )
  200. : GnView( doc )
  201. {}
  202.  
  203. void hello_world_view ::
  204. draw ( int, int, int, Dimension, Dimension )
  205. {
  206.     Gina_Debug_NL ("hello_world_view :: draw");
  207.     draw_rectangle (24, 16, 87, 19);
  208.     draw_glyphs (30, 30, "Hello, world!");
  209.     ((hello_world_document*)document)->draw_his();
  210. }
  211.  
  212. void hello_world_view ::
  213. button_press ( int, int, int x, int y )
  214. {
  215.     Gina_Debug_NL( "hello_world_view :: button_press" );
  216.     (new add_hi( x, y, (hello_world_document*)document, this ))->submit();
  217.     force_redraw();
  218. }
  219.  
  220. //---------------------------------------------------------------------
  221. int main (unsigned int argc, char ** argv)
  222. {
  223.     hello_world_application hwa;
  224. #ifdef GINA_DEBUG
  225.     hwa.set_sync();
  226. #endif
  227.     hwa.run( argc, argv );
  228. }
  229.