Text

Gamma/GTK: Using Gamma™ with GTK, Version 0.9
Prev | Appendix A. Example Code | Next

# A.16. Text

    function text_insert_random (text)
    {
      local i, c;
      for (i = 0; i < 10; i++)
        {
          c = int('A') + (random() * 24);
          text.set_point(1);
          text.set_point(random() * text.get_length());
          text.insert(nil, nil, nil, char(c), 1);
        }
    }

    function make_color(r, g, b)
    {
      local color = new(GdkColor);
      color.red = r;
      color.green = g;
      color.blue = b;
      color;
    }

    function create_text ()
    {
      local box1, box2, hbox, button, check, separator, scrolled_window,
      text, font, infile, text_colors;

      win_text = new (GtkWindow);
      win_text.signal ("destroy", #win_text = nil);
      win_text.name = "text window";
      win_text.set_policy(TRUE, TRUE, FALSE);
      win_text.set_usize(500, 500);
      win_text.title = "test";
      win_text.border_width = 0;

      box1 = new(GtkVBox);
      win_text.add(box1);
      box1.show();

      box2 = new(GtkVBox);
      box2.spacing = 10;
      box2.border_width = 10;
      box1.pack_start(box2, TRUE, TRUE, 0);
      box2.show();

      scrolled_window = new(GtkScrolledWindow);
      box2.pack_start(scrolled_window, TRUE, TRUE, 0);
      scrolled_window.set_policy(GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
      scrolled_window.show();

      text = new(GtkText);
      text.set_editable(TRUE);
      scrolled_window.add(text);
      text.grab_focus();
      text.show();

      font = gdk_font_load ("-adobe-courier-medium-r-normal--*-120-*-*-*-*-*-*");

      text.freeze();

      /* Create and insert the colored foreground/background display text. */

      text_colors = array(cons("black", make_color(0,0,0)),
    		      cons("white", make_color(0xffff,0xffff,0xffff)),
    		      cons("red", make_color(0xffff,0,0)),
    		      cons("green", make_color(0, 0xffff,0)),
    		      cons("blue", make_color(0, 0, 0xffff)),
    		      cons("cyan", make_color(0, 0xffff, 0xffff)),
    		      cons("magenta", make_color(0xffff, 0, 0xffff)),
    		      cons("yellow", make_color(0xffff, 0xffff, 0)));

      with i in text_colors do
        {
          text.insert(font, nil, nil, car(i), -1);
          text.insert(font, nil, nil, "\t", -1);

          with j in text_colors do
    	text.insert(font, cdr(j), cdr(i), "XYZ", -1);

          text.insert(font, nil, nil, "\n", -1);
        }

      /* Read and insert the sample text. */

      if ((infile = open("gtkenums.h", "r")) ||
          (infile = open("../gtkenums.h", "r")))
        {
          line = nil;
          while(line != _eof_)
    	{
    	  line = read_line(infile);
    	  line_ret = string(line,"\n");
    	  if (line_ret != "Unexpected end of file\n")
    	    text.insert(font, nil, nil, line_ret, -1);
    	}
        }

      text.thaw();

      hbox = new(GtkHButtonBox);
      box2.pack_start(hbox, FALSE, FALSE, 0);
      hbox.show();

      check = new(GtkCheckButton);
      check.label = "Editable";
      hbox.pack_start(check, FALSE, FALSE, 0);
      check.signal("toggled", `(@text).set_editable((@check).get_active()));
      check.set_active(TRUE);
      check.show();

      check = new(GtkCheckButton);
      check.label = "Wrap Words";
      hbox.pack_start(check, FALSE, FALSE, 0);
      check.signal("toggled", `(@text).set_word_wrap((@check).get_active()));
      check.set_active(FALSE);
      check.show();

      separator = new(GtkHSeparator);
      box1.pack_start(separator, FALSE, TRUE, 0);
      separator.show();

      box2 = new(GtkVBox);
      box2.spacing = 10;
      box2.border_width = 10;
      box1.pack_start(box2, FALSE, TRUE, 0);
      box2.show();

      button = new(GtkButton);
      button.label = "insert random";
      button.signal("clicked", `text_insert_random(@text));
      box2.pack_start(button, TRUE, TRUE, 0);
      button.show();

      button = new (GtkButton);
      button.label = "close";
      button.signal ("clicked", `(@win_text).destroy());
      box2.pack_start (button, TRUE, TRUE, 0);
      button.can_default = TRUE;
      button.grab_default();

      win_text.show_all();
      win_text;
    }

    function main ()
    {
      local window, win_text;
      TRUE=1;
      FALSE=0;
      window = create_text ();
      window.signal ("destroy", #exit_program(0));
      init_ipc("text", "textq");
      gtk_main ();
    }

Prev | Home | Next
Spin Button | Up | Toggle Buttons

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.