/*
 *  Login.app
 * 
 *  Copyright (c) 1997-2000 Per Liden
 * 
 *  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 <X11/Xlib.h>
#include <wraster.h>
#include "util.h"
#include "panel.h"
#include "preferences.h"


RContext* ctx = 0;


BOOL initGraphicsContext() {
   ctx = RCreateContext(dpy, DefaultScreen(dpy), 0);

   if (!ctx) {
      printError("could not initialize graphics library context (%s)", RMessageForError(RErrorCode));
      return NO;
   }

   return YES;
}

unsigned long getColorByName(const char* name) {
   XWindowAttributes attributes;
   XColor            color;
    
   XGetWindowAttributes(dpy, root, &attributes);
   color.pixel = 0;
    
   if (!XParseColor(dpy, attributes.colormap, name, &color)) {
      printError("can not parse color %s", name);
   } else if (!XAllocColor(dpy, attributes.colormap, &color)) {
      printError("can not allocate color %s", name);
   }
    
   return color.pixel;
}

void setWindowImage(Window win, RImage* image) {
   Pixmap pix;

   RConvertImage(ctx, image, &pix);
   XSetWindowBackgroundPixmap(dpy, win, pix);
   XResizeWindow(dpy, win, image->width, image->height);
   XClearWindow(dpy, win);
   XFreePixmap(dpy, pix);
   XFlush(dpy);
}

void setCombinedWindowImage(Window win, RImage* bottom, RImage* top) {
   RImage* img;

   img = RCloneImage(bottom);
   RCombineImages(img, top);
   setWindowImage(win, img);
   RDestroyImage(img);
}

void drawText(Window win, XFontStruct* font, int justification, int x, int y, 
	      unsigned long color, const char* text) {
   int width;
   int length;
   GC  gc;

   gc = XCreateGC(dpy, win, 0, 0);
   XSetForeground(dpy, gc, color);
   XSetFont(dpy, gc, font->fid);

   length = strlen(text);
   width = XTextWidth(font, text, length);
    
   if (justification == JUSTIFICATION_RIGHT) {
      x -= width;
   } else if (justification == JUSTIFICATION_CENTER) {
      x -= width/2;
   }

   XDrawString(dpy, win, gc, x, y, text, length);
   XFreeGC(dpy, gc);
}

