Drawing Text
Prev | Chapter 14. The Drawing Area Widget | Next

## Drawing Text

The two methods `draw_string()` and `draw_text()` are nearly the same. The first argument is the `Gdk::Font` with which to render the text. The second argument is our regular `Gdk::GC`. The third argument is the far left edge of the first character, and the fourth argument is the baseline of the string of text. This means that 'g's, 'p's, 'y's and other low-hanging characters will fall below the pixel location specified in the fourth argument. The fifth argument is the text to be rendered. The difference between `draw_string()` and `draw_text()` is that the latter has an extra argument to set the number of characters to actually be rendered. `draw_string()` will always draw all of the string.

Here's a sample program using all of the drawing methods shown so far:

    #include <gtkmm/main.h>
    #include <gtkmm/window.h>
    #include <gtkmm/drawingarea.h>
    #include <gtkmm/style.h>
    #include <gdk--/colormap.h>
    #include <gdk--/color.h>
    #include <gdk--/gc.h>
    #include <gdk--/types.h>
    #include <stdio.h>

    //Custom drawing area with modified expose_event.
    class cust_draw_area: public Gtk::DrawingArea
    {
      int width, height;
      public:
        cust_draw_area(int x_size = 0, int y_size = 0);
        int on_expose_event(GdkEventExpose *event);
    };

    //Constructor.
    cust_draw_area::cust_draw_area(int x_size, int y_size)
    : DrawingArea(), width(x_size), height(y_size)
    {
      size(width, height);
    }

    //Expose_event method.
    int cust_draw_area::on_expose_event(GdkEventExpose *event)
    {
      Gdk::GC some_gc;
      some_gc.create(get_window());
      Gdk::Color some_color;
      Gdk::Colormap some_colormap(Gdk::Colormap::get_system());
      some_color.set_red(65535);
      some_color.set_green(65535);
      some_color.set_blue(0);
      some_colormap.alloc(some_color);
      some_gc.set_foreground(some_color);

      //Draw pac-man.
      get_window().draw_arc(some_gc, true, 30, 100, 50, 50, 2880, 17280); //2880==45*64, 17280==270*64

      //Draw pellets.
      get_window().draw_rectangle(this->get_style()->get_white_gc(), true, 80, 120, 15, 10);
      get_window().draw_rectangle(this->get_style()->get_white_gc(), true, 110, 120, 15, 10);
      get_window().draw_rectangle(this->get_style()->get_white_gc(), true, 140, 120, 15, 10);

      //Draw some lines.
      get_window().draw_line(this->get_style()->get_black_gc(), 5, 2, 5, 20);
      get_window().draw_line(this->get_style()->get_black_gc(), 5, 11, 10, 11);
      get_window().draw_line(this->get_style()->get_black_gc(), 10, 2, 10, 20);
      get_window().draw_line(this->get_style()->get_black_gc(), 15, 2, 21, 2);
      get_window().draw_line(this->get_style()->get_black_gc(), 18, 2, 18, 20);
      get_window().draw_line(this->get_style()->get_black_gc(), 15, 20, 21, 20);

      //Draw a diamond.
      GdkPoint some_point[]={{100, 10}, {50, 60}, {100, 110}, {150, 60}};
      get_window().draw_polygon(this->get_style()->get_black_gc(), true, some_point, 4);

      //Draw some text.
      Gdk::Font text_font("10x20");
      get_window().draw_string(text_font, this->get_style()->get_black_gc(), 30, 170, "Hello, World!");
    }

    class test_window : public Gtk::Window
    {
      cust_draw_area some_draw_area;
      public:
        test_window();
    };

    test_window::test_window()
    :  some_draw_area(200, 200)
    {
      add(some_draw_area);
      show_all();
    }

    int main(int argc, char *argv[])
    {
      Gtk::Main main_runner(argc, argv);
      test_window foo;
      main_runner.run();
      return(0);
    }

The structure of the program is the same as the first one, except that this one includes examples of the drawing elements discussed up to now.

Prev | Up | Next
Drawing Ellipses and Arcs | Home | Draw Images