/*---------------------------------------------*/
/*                                             */
/* ドローエリアに Pixmap を書くです            */
/*              Programed Rero2(K.Kunikane)    */
/*                                             */
/*---------------------------------------------*/

/*--------------------*/
/*  画像データ        */
/*--------------------*/
#include <gtk/gtk.h>
#include "paint.h"

#include "../picture/pic1.xpm"
#include "../picture/pic2.xpm"


/*--------------------*/
/* ソース内関数定義   */
/*--------------------*/
GtkWidget *RR_GetParts(GtkWidget *, gchar *);


/*--------------------*/
/*  変数              */
/*--------------------*/

/* --- 表示している画像 */
gint  graphic_no;

/* --- 画像を表示すべきドローイングエリアヴィジェット */
GtkWidget  *canvas;

GdkPixmap *pixmap[2];
GdkBitmap *mask[2];

GdkGC     *win_gc;
GtkWidget *window;

/*--------------------*/
/*  各種初期化        */
/*--------------------*/
void  PaintInit(GtkWidget *mainwin)
{
  /* Pixmap の作成 */
  pixmap[0] = gdk_pixmap_create_from_xpm_d(mainwin->window,
					   &mask[0],
					   NULL,
					   pic1_xpm);
  pixmap[1] = gdk_pixmap_create_from_xpm_d(mainwin->window,
					   &mask[1],
					   NULL,
					   pic2_xpm);

  /* ドローアブルエリアのチェック */
  canvas = RR_GetParts(mainwin, "GtkDrawingArea");

  /* GC の作成 */
  win_gc = gdk_gc_new(mainwin->window);

  window = mainwin;
  graphic_no = 0;
}


/*--------------------*/
/*  各種開放          */
/*--------------------*/
void  PaintClose(void)
{
  gdk_pixmap_unref(pixmap[0]);
  gdk_pixmap_unref(pixmap[1]);
  gdk_bitmap_unref(mask[0]);
  gdk_bitmap_unref(mask[1]);
  gdk_gc_unref(win_gc);
}


/*--------------------*/
/*  再描画            */
/*--------------------*/
void  PaintUpdate(void)
{
  PaintDraw(graphic_no);
}


/*--------------------*/
/*  絵の交換          */
/*--------------------*/
void  PaintDraw(gint n)
{
  gint  w, h;

  graphic_no = n;

  gdk_window_get_size(pixmap[n], &w, &h);
  gdk_gc_set_clip_mask(win_gc, mask[n]);
  gdk_gc_set_clip_origin(win_gc, 0, 0);
  gdk_draw_pixmap(canvas->window, win_gc, pixmap[n], 0, 0, 0, 0, w, h);
  gdk_gc_set_clip_mask(win_gc, NULL);
}


/*---------------------------------------------*/
/*  指定名のヴィジェットをウィンドウ内から得る */
/*---------------------------------------------*/
GtkWidget *RR_GetParts(GtkWidget *mainwin, gchar *name)
{
  GtkWidget *parts = NULL;
  GList *wlist, *tmp;
  gint  n, i;
  gint max;

  wlist = gtk_container_children(GTK_CONTAINER(mainwin));
  max = g_list_length(wlist);
  n = -1;
  for(i=0; i<max; i++) {
    tmp = g_list_nth(wlist, i);
    parts = GTK_WIDGET(tmp->data);
    if (strcmp((char*)gtk_widget_get_name(parts), name) == 0) {
      /* -- みーつけた */
      n = i;
      break;
    }
  }
  if (n == -1) {
    parts = NULL;
  }

  return(parts);
}



