toplevel_window.c 3.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include "gui.h"
#include "gui_defs.h"
#include "x.h"
#include <stdio.h>
#include <stdlib.h>

/**********************************************************************/
/*                         callback functions                         */
/**********************************************************************/

static void repack(gui *g, widget *_this)
{
Cedric Roux's avatar
Cedric Roux committed
13
  LOGD("REPACK toplevel_window\n");
14 15 16 17 18 19 20 21 22 23 24
  struct toplevel_window_widget *this = _this;
  if (this->common.children == NULL) ERR("toplevel window has no child\n");
  if (this->common.children->next != NULL)
    ERR("toplevel window has too much children\n");
  this->common.children->item->allocate(g, this->common.children->item,
      0 /* x */, 0 /* y */, this->common.width, this->common.height);
  send_event(g, DIRTY, this->common.id);
}

static void add_child(gui *_gui, widget *_this, widget *child, int position)
{
Cedric Roux's avatar
Cedric Roux committed
25
  LOGD("ADD_CHILD toplevel_window\n");
26 27 28 29 30 31 32 33 34 35 36 37
  struct widget *this = _this;
  if (this->children != NULL) {
    WARN("toplevel window already has a child\n");
    return;
  }
  widget_add_child_internal(_gui, _this, child, 0); /* this does the REPACK */
}

/* called when the underlying window is resized by the user or the system */
static void allocate(
    gui *_gui, widget *_this, int x, int y, int width, int height)
{
Cedric Roux's avatar
Cedric Roux committed
38
  LOGD("ALLOCATE toplevel_window\n");
39 40 41 42 43 44 45 46 47 48 49
  struct toplevel_window_widget *this = _this;
  this->common.width = width;
  this->common.height = height;
//  repack(_gui, _this);
  send_event(_gui, REPACK, this->common.id);
}

static void paint(gui *_gui, widget *_this)
{
  struct gui *g = _gui;
  struct toplevel_window_widget *this = _this;
Cedric Roux's avatar
Cedric Roux committed
50
  LOGD("PAINT toplevel_window (%d %d)\n", this->common.width, this->common.height);
51 52 53 54 55 56 57
  x_fill_rectangle(g->x, this->x, BACKGROUND_COLOR,
      0, 0, this->common.width, this->common.height);
  g->xwin = this->x;
  this->common.children->item->paint(_gui, this->common.children->item);
  g->xwin = NULL;    /* TODO: remove? it's just in case */
}

58 59
static void button(gui *_g, widget *_this, int x, int y,
    int key_modifiers, int button, int up)
60 61 62 63 64
{
  struct gui *g = _g;
  struct toplevel_window_widget *this = _this;
  g->xwin = this->x;
  this->common.children->item->button(_g, this->common.children->item,
65
      x, y, key_modifiers, button, up);
66 67 68
  g->xwin = NULL;    /* TODO: remove? it's just in case */
}

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/**********************************************************************/
/*                              creation                              */
/**********************************************************************/

widget *new_toplevel_window(gui *_gui, int width, int height, char *title)
{
  struct gui *g = _gui;
  struct toplevel_window_widget *w;

  glock(g);

  w = new_widget(g, TOPLEVEL_WINDOW, sizeof(struct toplevel_window_widget));

  w->common.width  = width;
  w->common.height = height;

  w->x = x_create_window(g->x, width, height, title);

  w->common.repack    = repack;
  w->common.add_child = add_child;
  w->common.allocate  = allocate;
  w->common.paint     = paint;

92 93
  w->common.button    = button;

94 95 96 97
  gunlock(g);

  return w;
}