/*======================================================================
//  Makefile:
//
//     CFLAGS = `pkg-config --cflags gtk+-2.0`           
//     LIBS = `pkg-config --libs gtk+-2.0`            
//                                                    
//     draw-star: draw-star.o                         
//             $(CC) -o $@ $@.o $(LIBS)               
//
//----------------------------------------------------------------------
//
//  An example that draws line widths in a star. This file is in the
//  public domain.
//
//  Dov Grobgeld <dov@imagic.weizmann.ac.il>
//----------------------------------------------------------------------*/
#include <glib.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <math.h>

typedef struct {
    GtkWidget *window;
    GtkWidget *drawing_area;
    GdkGC *drawing_area_gc;
} MyApp;

void draw_star(GdkWindow *window,
               GdkGC *gc,
               int star_center_x,
               int star_center_y,
               double line_width,
               double r1,
               double r2,
               int delta_angle
               )
{
    int angle;
    gdk_gc_set_line_attributes(gc,
                               line_width,
                               GDK_LINE_SOLID,
                               GDK_CAP_ROUND,
			       GDK_JOIN_ROUND);
    
    for (angle=0; angle<360; angle+=delta_angle) {
        double angle_rad = 1.0*angle/180 * M_PI;
        int x1 = (int)(star_center_x + r1 * cos(angle_rad)+0.5);
        int y1 = (int)(star_center_y + r1 * sin(angle_rad)+0.5);
        int x2 = (int)(star_center_x + r2 * cos(angle_rad)+0.5);
        int y2 = (int)(star_center_y + r2 * sin(angle_rad)+0.5);

        gdk_draw_line(window,
                      gc,
                      x1,y1,
                      x2,y2);
        
    }
}

void draw_graphics(MyApp *app)
{
    int lw_idx;
    
    for (lw_idx=0; lw_idx<9; lw_idx++) {
        double col_idx = lw_idx % 3;
        double row_idx = lw_idx / 3;

        draw_star(app->drawing_area->window,
                  app->drawing_area_gc,
                  64+128*col_idx,64+128*row_idx,
                  lw_idx,10,60, 15 );
    }
}

gboolean event_handler (GtkWidget *drawing_area,
                        GdkEvent *event,
                        gpointer client_data)
{
  if (event->type == GDK_EXPOSE)
      draw_graphics((MyApp*)client_data);

  return 1;
}

gint main (gint argc,gchar *argv[])
{
    MyApp app;

    gtk_init (&argc, &argv);

    app.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_signal_connect (GTK_OBJECT (app.window), "destroy",
                        GTK_SIGNAL_FUNC (gtk_main_quit), NULL);

    app.drawing_area = gtk_drawing_area_new ();
    gtk_drawing_area_size (GTK_DRAWING_AREA(app.drawing_area),
                           128*3, 128*3);

    gtk_widget_set_events (app.drawing_area,
                           GDK_EXPOSURE_MASK);

    gtk_signal_connect (GTK_OBJECT(app.drawing_area),
                        "event",
                        GTK_SIGNAL_FUNC(event_handler),
                        &app);

    gtk_container_add (GTK_CONTAINER(app.window),
                       app.drawing_area);

    gtk_widget_realize (app.window);
    app.drawing_area_gc = gdk_gc_new (app.window->window);

    gtk_widget_show_all (app.window);

    gtk_main ();

    return 0;
}
