Associating Data with Windows

#include <stdio.h>
#include "graphapp.h"

void draw_shapes(Control *c, Graphics *g)
{
  Rect r = get_control_area(c);

  set_colour(g, RED);
  fill_rect(g, r);

  set_colour(g, BLUE);
  fill_ellipse(g, insetr(r,4));

  set_colour(g, GREEN);
  draw_line(g, pt(r.x,r.y),
            pt(r.x+r.width,r.y+r.height));
  draw_line(g, pt(r.x,r.y+r.height),
            pt(r.x+r.width,r.y));
}

void resize_control(Window *w)
{
  /* retrieve stored pointer */
  Rect r = get_window_area(w);
  Control *c = get_window_data(w);
  set_control_area(c, inset_rect(r,10));
}

int main(int argc, char *argv[])
{
  App *app;
  Window *w;
  Control *c; /* local */
  Rect r;

  app = new_app(argc, argv);
  w = new_window(app, rect(50,50,150,200),
                 "Rectangles", STANDARD_WINDOW);
  set_window_background(w, LIGHT_GREY);
  r = rect(10,10,100,100);
  c = new_control(w, r);
  on_control_redraw(c, draw_shapes);
  set_window_data(w, c); /* store pointer */
  on_window_resize(w, resize_control);
  show_window(w);
  main_loop(app);
  return 0;
}

Notes: