home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / EXAMPLES / FLOWERS.LF < prev    next >
Text File  |  1996-06-04  |  21KB  |  651 lines

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4. %                                                                             %
  5. %                                                                             %
  6. %                 Drawing Flowers with Rewrite Systems                        %
  7. %                                                                             %
  8. %                                                                             %
  9. %                                                                             %
  10. %                                                                             %
  11. % This program is  based on the following book:                               %
  12. %                                                                             %
  13. %               THE ALGORITHMIC BEAUTY OF PLANTS                              %
  14. %                                                                             %
  15. %     by Przemyslaw Prusinkiewicz and Aristid Lindenmayer                     %
  16. %                (Springer Verlag,1990)                                       %
  17. %                                                                             %
  18. % This book shows how the growth and shape of plants may be modeled using     %
  19. % rewrite systems called Lindenmayer Systems (L-systems for short), and       %
  20. % how these systems can be used to generate images.                           %
  21. %                                                                             %
  22. % In this implementation, a flower may be specified thanks to an L-system,    %
  23. % using a syntax which is very easy to derive from that described in the      %
  24. % book. This L-system is then translated into a Life program thanks to a      %
  25. % transaltor (in flo_gram.lf). Running the obtained program draws the flower. %
  26. %                                                                             %
  27. %                                                                             %
  28. %                                                                             %
  29. % This file contains the definition of the user interface and the definition  %
  30. % of the L-System used to draw the flowers.                                   %
  31. %                                                                             %
  32. % The other files are:                                                        %
  33. % - flo_utils.lf : contains a number of utilities.                            %
  34. % - flo_gram.lf  : contains the L-Systems translator and the predefined       %
  35. %                  symbols definitions                                        %
  36. % - flo_xtools.lf: contains all the xToolkit: definition of button types,...  %
  37. % - flo_custom.lf: contains all the customizable stuff: look of the interface,%
  38. %                  default values for the flowers.                            %
  39. %                                                                             %
  40. %                                                                             %
  41. % This is the main file of the demo. All the other files are automatically    %
  42. % loaded if they are in the same directory.                                   %
  43. %                                                                             %
  44. % Notice: This is quite an 'old' Life program. It means that features have    %
  45. %         been added to the language since it was written, especially global  %
  46. %         and persistent variables: most constant functions (reset using setq)%
  47. %         could be changed to persistent terms, saving memory and time.       %
  48. %                                                                             %
  49. %         The toolkit is an old version of that given under the name "xtools".%
  50. %         We recommend using the new one to build new applications.           %
  51. %                                                                             
  52. % Author: Bruno Dumant                                                        %
  53. %                                                                             %
  54. % Copyright 1992 Digital Equipment Corporation                                %
  55. % All Rights Reserved                                                         %
  56. %                                                                             %
  57. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  58. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  59. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  60.  
  61. module("flowers") ?
  62. public(flowers) ?
  63.  
  64. import("x") ?
  65.  
  66. %%% loading utilities and customization files
  67.  
  68. load("flo_utils"), open("utils") ?
  69. load("flo_gram"), open("rewrite_trans") ?
  70. load("flo_flowerdef"),open("flower_def") ?
  71. load("flo_xtools"), open("xtools_flo") ?
  72. load("flo_custom") ?
  73.  
  74. %%%
  75. %%% MAIN PROCEDURE
  76. %%%
  77.  
  78.  
  79. flowers :-
  80.     (
  81.         catch(enter_flowers),
  82.         init_default_values,
  83.         create_main_panel,
  84.         create_draw_init_panel,
  85.         create_palette,
  86.         create_postscript_panel,
  87.         create_drawing_pad,
  88.         open_panel(main_panel)
  89.     ;
  90.         succeed
  91.     ).
  92.  
  93. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  94. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  95. %
  96. % MAIN PANEL
  97. %
  98. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  99. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  100.  
  101. %%% panel creation
  102.  
  103. create_main_panel :-
  104.     create_new_panel( 100,100, 220, 210, 
  105.                       choice => choose_button,
  106.               title  => "Flowers",
  107.               name   => MainPanel:main_panel),
  108.     (
  109.         B1 = oo_led_txt( 10, 10, 200, 30, 
  110.                          text => "Drawing init...",
  111.                  action => turtle_settings(B1)),
  112.         add_button( B1, panel => MainPanel),
  113.         B2 = oo_led_txt( 10, 45, 200, 30, 
  114.                          text => "Palette...",
  115.                  action => select_color(B2)),
  116.         add_button( B2, panel => MainPanel),
  117.         B3 = oo_led_txt( 10, 80, 200, 30, 
  118.                           text => "PostScript Output...",
  119.                   action => post_out(B3) ),
  120.         add_button( B3, panel => MainPanel),
  121.  
  122.         B4 = tog_led_txt( 10, 135, 200, 30, 
  123.                          text => "Draw",
  124.                          action => draw_flower(B4) ),
  125.             add_button( B4, panel => MainPanel),
  126.  
  127.         B5 = tog_led_txt( 10, 170, 200, 30, 
  128.                           text => "Quit",
  129.                   action => quit_flowers),
  130.             add_button( B5, panel => MainPanel),
  131.  
  132.         draw_buttons([B1,B2,B3,B4,B5]),
  133.         fail
  134.     ;
  135.         write("main panel created ..."),nl
  136.     ).
  137.  
  138. %%% button types
  139.  
  140. tog_led_txt <| ledandtext.
  141. tog_led_txt <| toggle_button.
  142.  
  143. oo_led_txt <| ledandtext.
  144. oo_led_txt <| on_off_button.
  145.  
  146.  
  147. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  148. %
  149. % ACTIONS ASSOCIATED WITH COMMANDS IN THE MAIN PANEL
  150. %
  151. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  152.  
  153. %%% postscript output
  154.  
  155. post_out(B):-
  156.     cond( B.on,
  157.           open_panel(postscript_panel),
  158.           close_panel(postscript_panel)).
  159.  
  160. %%% drawing initialization
  161.  
  162. turtle_settings(B) :- 
  163.     cond( B.on,
  164.           open_panel(draw_init_panel),
  165.           close_panel(draw_init_panel)).
  166.  
  167.  
  168. %%% Colors
  169.  
  170. select_color(B) :-
  171.     cond( B.on,
  172.           open_panel(palette),
  173.           close_panel(palette)).
  174.  
  175. %%% Drawing
  176.  
  177. draw_flower(B) :-
  178.     xShowWindow(draw_window),
  179.     draw_a_flower.
  180.  
  181. draw_a_flower :- 
  182.     initSingle, 
  183.     (
  184.         catch(draw_start),          % label setting
  185.         axiomflower(deriv_num),
  186.         fail 
  187.     ; 
  188.         setq(drawing,false)         % drawing is a boolean function used to
  189.                     % stop the drawing of a flower.
  190.     ).
  191.  
  192. initSingle :-
  193.         DP:draw_init_panel&@(selected_text => B), %% if a button is still
  194.         cond(   B :== true,                       %% selected in
  195.                 (                                 %% draw_init_panel, its
  196.             But:(B.1).action,           %% value is used.
  197.             But.on <- false,
  198.             B <- false,
  199.             refresh_button(But),
  200.             update_panel(DP)
  201.             )),
  202.  
  203.         Ratio = pi/180,
  204.             initAngle(pi/10),
  205.             initThick(2*zoom_fact),
  206.         initDirection(aystart*Ratio,
  207.                       axstart*Ratio,
  208.               azstart*Ratio),
  209.         initPosition(xstart,ystart,0),
  210.         setq(realDistance,20*zoom_fact),
  211.         setq(drColor,defaultColor),
  212.         setq(drawing,true),
  213.         setq(stop_drawing,false).
  214.     
  215. %%% Quit
  216.  
  217. quit_flowers :-
  218.     throw(enter_flowers).
  219.  
  220.  
  221. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  222. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  223. %
  224. % POSTSCRIPT OUTPUT
  225. %
  226. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  227. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  228.  
  229. create_postscript_panel :-
  230.     create_new_panel( 620,100, 440, 50, 
  231.                       choice => choose_button,
  232.               title  => "PostScript Output",
  233.               name   => PostPanel:postscript_panel),
  234.     postscript_panel&@(window => PW),
  235.     setq( postscript_window, PW),
  236.     (
  237.         xDrawString( PW, 10, 29, "File Name:", font => button_font,
  238.                      color => text_color(on_off_color)),
  239.  
  240.         B1 = textfield_look( 100, 10, 150, 30, action => set_file(B1),
  241.                              text => postout_filename ),
  242.         add_button( B1, panel => PostPanel),
  243.  
  244.         B2 = tog_led_txt( 260, 10, 170, 30, 
  245.                           text => "Save PostScript",
  246.                   action => save_postscript),
  247.         add_button( B2, panel => PostPanel),
  248.  
  249.         draw_buttons([B1,B2]),
  250.         fail
  251.     ;   
  252.         write("PostScript panel created ... "),nl
  253.     ).
  254.  
  255. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  256. %
  257. % ACTIONS ASSOCIATED WITH COMMANDS IN THE POSTSCRIPT OUTPUT PANEL
  258. %
  259. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  260.  
  261. %%% file name setting
  262.  
  263. set_file(B1:@( text => T )) :-
  264.     setq(postout_filename, T).
  265.  
  266.  
  267. %%% Save postscript in a file
  268.  
  269. save_postscript(panel => P:@(selected_text => B)) :-
  270.     cond(   B :== true,
  271.             (
  272.             But:(B.1).action,
  273.             B <- false,
  274.             But.on <- false,
  275.             refresh_button(But)
  276.         )),
  277.     (
  278.         xPostScriptWindow( draw_window, postout_filename),!
  279.     ;
  280.         write( " Error: Bad File Name "),nl
  281.     ).
  282.  
  283. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  284. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  285. %
  286. % DRAWING INIT
  287. %
  288. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  289. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  290.  
  291. create_draw_init_panel :-
  292.     create_new_panel( 330, 100, 280, 150, 
  293.                       choice => choose_button,
  294.               title  => "Drawing Init",
  295.               name   => DrawInitPanel:draw_init_panel),
  296.     draw_init_panel&@(window => PW),
  297.     setq( draw_init_window, PW),
  298.     (
  299.         xDrawString( PW, 45, 29, "X:", font => button_font,
  300.                      color => text_color(on_off_color)),
  301.         xDrawString( PW, 155, 29, "Y:", font => button_font,
  302.                      color => text_color(on_off_color)),
  303.         xDrawString( PW, 20, 79, "ax:", font => button_font,
  304.                      color => text_color(on_off_color)),
  305.         xDrawString( PW, 105, 79, "ay:", font => button_font,
  306.                      color => text_color(on_off_color)),
  307.         xDrawString( PW, 190, 79, "az:", font => button_font,
  308.                      color => text_color(on_off_color)),
  309.         xDrawString( PW, 20, 129, "Complexity:", font => button_font,
  310.                      color => text_color(on_off_color)),
  311.         xDrawString( PW, 170, 129, "Zoom:", font => button_font,
  312.                      color => text_color(on_off_color)),
  313.  
  314.         B1 = textfield_look( 75, 10, 50, 30, 
  315.                              action => set_value_init(xstart,B1,"X"),
  316.                              text => int2str(xstart) ),
  317.         add_button( B1, panel => DrawInitPanel),
  318.         B2 = textfield_look( 185, 10, 50, 30,
  319.                              action => set_value_init(ystart,B2,"Y")  ,
  320.                              text => int2str(ystart)),
  321.         add_button( B2, panel => DrawInitPanel),
  322.         B3 = textfield_look( 50, 60, 40, 30,  
  323.                              action => set_value_init(axstart,B3,"ax"),
  324.                              text => int2str(axstart)),
  325.         add_button( B3, panel => DrawInitPanel),        
  326.         B4 = textfield_look( 135, 60, 40, 30, 
  327.                              action => set_value_init(aystart,B4,"ay"),
  328.                              text => int2str(aystart)),
  329.         add_button( B4, panel => DrawInitPanel),
  330.         B5 = textfield_look( 220, 60, 40, 30, 
  331.                              action => set_value_init(azstart,B5,"az"),
  332.                              text => int2str(azstart)),
  333.         add_button( B5, panel => DrawInitPanel),
  334.         B6 = textfield_look( 120, 110, 25, 30, 
  335.                              action => set_deriv_num(B6),
  336.                              text => int2str(deriv_num,2)),
  337.         add_button( B6, panel => DrawInitPanel),
  338.         B7 = textfield_look( 220, 110, 40, 30, 
  339.                              action => set_zoom_fact(B7),
  340.                              text => real2str(zoom_fact,2)),
  341.         add_button( B7, panel => DrawInitPanel),
  342.         draw_buttons([B1,B2,B3,B4,B5,B6,B7]),
  343.         fail
  344.     ;
  345.         write("draw_init panel created ..."),nl
  346.     ).
  347.  
  348. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  349. %
  350. % ACTIONS ASSOCIATED WITH COMMANDS IN THE DRAWING INIT PANEL
  351. %
  352. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  353.  
  354. %%% give a function the value (if real) of a text field.
  355.  
  356. non_strict(set_value_init) ?
  357. set_value_init( Func, B:@(text => Text), FieldName) :-
  358.     Y = parse(Text),
  359.     T = eval(Y),
  360.     cond(   T :< real,
  361.             setq(Func,T),
  362.         (
  363.             write( "*** Error: Bad value in field ",FieldName," ***"),
  364.             nl
  365.             )
  366.         ),
  367.     cond( Y :\< real,
  368.           (   
  369.           Text <- real2str(eval(Func),2),
  370.           refresh_button(B)
  371.           )).
  372.  
  373. set_deriv_num( B:@(text => Text)) :-
  374.     Y = parse(Text),
  375.     T = eval(Y),
  376.     cond(   T :< real,
  377.             (
  378.             cond( T =< 2,
  379.                   Val = 2,
  380.               cond( T >= 8,
  381.                     Val = 8,
  382.                 Val = floor(T))),
  383.             setq(`deriv_num, Val)
  384.         ),
  385.         (
  386.             write( "*** Error: Bad value in field Complexity ***"),
  387.             nl
  388.             )),
  389.     cond( Y :\== D:deriv_num ,
  390.           (   
  391.           Text <- int2str(D),
  392.           refresh_button(B)
  393.           )).
  394.  
  395. set_zoom_fact( B:@(text => Text)) :-
  396.     Y = parse(Text),
  397.     T = eval(Y),
  398.     cond(   T :< real, 
  399.                 cond(   T > 0,
  400.                 setq(`zoom_fact,T),
  401.             (
  402.                 write( "*** Error: Bad value in field Zoom ***"),
  403.                 nl
  404.             )
  405.             ),
  406.             (
  407.             write( "*** Error: Bad value in field Zoom ***"),
  408.             nl
  409.         )
  410.         ),
  411.     cond( Y :\== Z:eval(zoom_fact),
  412.           (   
  413.           Text <- real2str(Z,2),
  414.           refresh_button(B)
  415.           )).
  416.  
  417.  
  418.  
  419. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  420. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  421. %
  422. % PALETTE
  423. %
  424. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  425. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  426.  
  427. create_palette :-
  428.         create_new_panel(200,160, 600, 500,
  429.                  choice => palette_choice,
  430.                  name   => Palette:palette,
  431.                  title  => "Palette"),
  432.     root_sort(Palette)&@(window => PW),
  433.         setq(palette_window,PW),
  434.     (
  435.         fill_colors(all_colors,50,50,0,PW),
  436.         B1 = color_button( 2, 0, 196, 100,
  437.                            text => "Leaves",
  438.                    action => element_choice(B1),
  439.                    color_block => ns(leaf_color)),
  440.         add_button( B1, panel => Palette),
  441.         B2 = color_button( 2, 100, 196, 100,
  442.                        text => "Petals",
  443.                action => element_choice(B2), 
  444.                color_block => ns(petal_color)), 
  445.             add_button( B2, panel => Palette),
  446.         B3 = color_button( 2, 200, 196, 100,
  447.                        text => "Stamens",
  448.                action => element_choice(B3), 
  449.                    color_block => ns(stamen_color)),
  450.         add_button( B3, panel => Palette),
  451.         B4 = color_button( 2, 300, 196, 100,
  452.                        text => "Stems",
  453.                action => element_choice(B4), 
  454.                color_block => ns(defaultColor)),
  455.         add_button( B4, panel => Palette),
  456.         B5 = color_button( 2, 400, 196, 100,
  457.                        text => "Background",
  458.                action => element_choice(B5), 
  459.                color_block => ns(background_color)),
  460.         add_button( B5, panel => Palette),
  461.         draw_buttons([B1,B2,B3,B4,B5]),
  462.         fail
  463.     ;
  464.         write("palette created ..."),nl
  465.     ).
  466.  
  467. % definition of color button
  468.  
  469. color_button <| oo_led_txt.
  470.  
  471. draw_static( color_button(X0,Y0,DX,DY,color_block => @(C),window=>W)) :-
  472.     draw_basic_button( X0+DX/2+25, Y0+20, DX/2-38, DY-40,
  473.                        depth => 2,
  474.                color => eval(C),
  475.                shade => highlight_color(on_off_color), 
  476.                highlight => shade_color(on_off_color),
  477.                window => W),
  478.     fail.
  479.  
  480.  
  481. %%% choice predicate attached to the palette
  482.  
  483. palette_choice(X,Y,Palette) :-
  484.     (
  485.         selected_color(X,Y,Palette),!
  486.     ;
  487.         find_chosen_button(Palette.buttons,X,Y,Palette)
  488.     ).
  489.  
  490.  
  491. %%% finding a color square in the palette
  492.  
  493. selected_color(X,Y,
  494.            @(selected => S, window => Win)) :-
  495.     show_color(X,Y,Win,C),
  496.     ( 
  497.         S :== true,!,
  498.         @(Color) = (Button:(S.1)).color_block,
  499.         setq(Color,C),
  500.         cond( Color :== `background_color,
  501.               xSetWindowColor( draw_window, C)),
  502.         change_color(Button)
  503.     ;
  504.         succeed
  505.     ).
  506.  
  507. %%% show the name of the color clicked on.
  508.  
  509. show_color(X,Y,Win,C) :- 
  510.         find_in_inthash2("colors",
  511.                      floor((X - 200)/U:50),
  512.              floor(Y/U),
  513.                  C:@(name => CN)),
  514.     xFillRectangle(Win,200,400,400,100,
  515.                    color => color_name_textfield_color),
  516.     xDrawString(Win,300,456,CN,font => color_name_font).
  517.  
  518.  
  519. %%% change the color of a button
  520.  
  521. change_color( @(X0,Y0,DX,DY,color_block => @(C),window=>W)) :-
  522.     xFillRectangle( W, X0+DX/2+27, Y0+22, DX/2-42, DY-44,
  523.                     color => eval(C)).
  524.  
  525.  
  526.  
  527. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  528. %
  529. % ACTIONS ASSOCIATED WITH COMMANDS IN THE PALETTE
  530. %
  531. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  532.  
  533. element_choice(B:@(on => Bool, color_block => @(C)), 
  534.            panel => @(selected => S, window => Win)) :-
  535.     (
  536.         Bool :== true,!,
  537.         cond( S :== true,
  538.               switch_off(S.1) ),
  539.         S <- true(B),
  540.         (
  541.         xFillRectangle(Win,200,400,400,100,
  542.                        color => color_name_textfield_color),
  543.         xDrawString(Win,300,456,
  544.                     eval(C).name,font => color_name_font),
  545.         fail
  546.             
  547.         ;
  548.             succeed
  549.         )
  550.     ;
  551.         S <- false
  552.     ).
  553.  
  554.  
  555. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  556. %
  557. % palette drawing
  558. %
  559. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  560.  
  561. %%% initialization of the palette colors
  562.  
  563. Colors = bagof(X,color(X)),
  564. setq(all_colors,Colors),
  565. fail ;
  566. succeed ?
  567.  
  568. %%% fill all the colored squares
  569.  
  570. fill_colors([C|Cs],W,H,N,Win) :- 
  571.     !,
  572.     xFillRectangle(Win,
  573.                    X:(200 + Col:floor(N/8)*W),
  574.                Y:(Row:(N mod 8)*H),
  575.                W,H,
  576.                color => C:@(name=>CN)),
  577.     add_in_inthash2("colors",Col,Row,C),
  578.     xDrawRectangle(Win,X,Y,W,H,color=> white),
  579.     fill_colors(Cs,W,H,N+1,Win).
  580. fill_colors([]).
  581.  
  582.  
  583. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  584. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  585. %
  586. % DRAWING PAD
  587. %
  588. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  589. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  590.  
  591. %%% 32772 = xExposureMask \/ xButtonPressMask
  592.  
  593. create_drawing_pad :-
  594.     xCreateWindow( default_display,150,130,700,700,DrWin, 
  595.                    color => background_color, 
  596.                windowtitle => "Drawing Pad",
  597.                eventmask => 32772,
  598.                show => false),
  599.     xSetWindowBorderWidth(DrWin,2),
  600.     setq(draw_window,DrWin),
  601.     catch_draw_event(DrWin).
  602.  
  603. %%% events handler
  604.  
  605. handle_draw_event ( button_event (x => X, y => Y), DrWin) ->      
  606.     true 
  607.     |
  608.         handle_draw_mouse(X,Y),
  609.         handle_draw_event (xGetEvent (DrWin,
  610.                               eventmask => 32772), DrWin).
  611.  
  612. handle_draw_event (expose_event, DrWin) ->      
  613.     true 
  614.     |
  615.         xRefreshWindow(DrWin),
  616.         handle_draw_event (xGetEvent (DrWin,
  617.                                   eventmask => 32772 ), DrWin).
  618. catch_draw_event(DrWin) :-
  619.     handle_draw_event (xGetEvent (DrWin,
  620.                       eventmask => 32772), DrWin).
  621.  
  622.  
  623. %%% mouse-events: the action depends on whether a flower is being drawn.
  624.  
  625. handle_draw_mouse(X,Y) :-
  626.     drawing,!,
  627.     setq(stop_drawing,true)
  628.     ;
  629.     setq(xstart,X),
  630.     setq(ystart,Y),
  631.     DrInitPanel:draw_init_panel&@(buttons => [_,_,_,_,_,YB,XB]),
  632.     XB.text <- int2str(X),
  633.     YB.text <- int2str(Y),
  634.     refresh_button(XB),
  635.     refresh_button(YB),
  636.     update_panel(DrInitPanel).
  637.     
  638.  
  639. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  640. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  641.  
  642. module("user") ?
  643. open("flowers") ?
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.