
/*
 * Stonage: a puzzle game
 * Copyright (C) 2001 Luc Saillard <luc@saillard.org>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */

#include "stonage.h"
#include "window_map.h"
#include "level.h"
#include <gtk/gtk.h>
#include <gtk/gtkdrawingarea.h>

#define DFT_SPRITE_SIZE 32

/* Callbacks use in this file */

static void quit_editor_cb( GtkWidget *widget, gpointer data);
static int on_expose_event(GtkWidget *drawing_area, GdkEventExpose *evt, struct map_info *info);

  
  
/**
 *
 *
 */
struct map_info *
create_map_window(void)
{
  GtkWidget *window, *scroll, *image;
  
  struct map_info *info;

  info = g_new0(struct map_info,1);
  info->level = Level_New(DEFAULT_MAP_WIDTH,DEFAULT_MAP_HEIGHT);

  info->window = window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window),_("Map"));
  gtk_signal_connect (GTK_OBJECT (window), "destroy",
      GTK_SIGNAL_FUNC (quit_editor_cb), NULL);

  scroll = gtk_scrolled_window_new(NULL,NULL);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
      GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
  gtk_container_add(GTK_CONTAINER(window),scroll);
  gtk_widget_show(scroll);

#if 0
  table = gtk_table_new (info->sizex, info->sizey, FALSE);
  gtk_widget_set_usize(GTK_WIDGET(table),MIN_WIN_HEIGHT,MIN_WIN_HEIGHT);
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW (scroll), table);
  gtk_widget_show(table);
#else
  info->image = gdk_image_new (
      GDK_IMAGE_FASTEST,
      gdk_rgb_get_visual(),
      info->level->sizex * DFT_SPRITE_SIZE,
      info->level->sizey * DFT_SPRITE_SIZE);
      
  image = gtk_image_new (info->image,NULL);
  gtk_widget_set_usize(GTK_WIDGET(scroll),
      info->level->sizex * DFT_SPRITE_SIZE,
      info->level->sizey * DFT_SPRITE_SIZE);
  
  info->drawing_area = gtk_drawing_area_new();
  gtk_drawing_area_size (GTK_DRAWING_AREA (info->drawing_area),
      info->level->sizex * DFT_SPRITE_SIZE,
      info->level->sizey * DFT_SPRITE_SIZE);
  gtk_signal_connect(GTK_OBJECT(info->drawing_area), "expose_event",
      GTK_SIGNAL_FUNC(on_expose_event), info);

//  gtk_widget_set_events (preview, PATTERN_EVENT_MASK);
/*
  gtk_signal_connect (GTK_OBJECT (psp->preview), "event",
		      (GtkSignalFunc) pattern_select_events,
		      psp);
*/
  //gtk_container_add (GTK_CONTAINER (psp->frame), psp->preview);
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW (scroll), image);
  gtk_widget_show (image);


#endif


  return info;
}


static void
quit_editor_cb( GtkWidget *widget, gpointer data)
{
  gtk_main_quit();
}


static int on_expose_event(GtkWidget *drawing_area, GdkEventExpose *evt, struct map_info *info)
{
  trace("");
  Level_Draw2Screen(info->level,info->squares,drawing_area);


  return FALSE;
}


