/*
Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions: The above copyright notice and this
permission notice shall be included in all copies or substantial
portions of the Software.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.


Except as contained in this notice, the names of The Open Group and/or
Sun Microsystems, Inc. shall not be used in advertising or otherwise to
promote the sale, use or other dealings in this Software without prior
written authorization from The Open Group and/or Sun Microsystems,
Inc., as applicable.


X Window System is a trademark of The Open Group

OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
logo, LBX, X Window System, and Xinerama are trademarks of the Open
Group. All other trademarks and registered trademarks mentioned herein
are the property of their respective owners. No right, title or
interest in or to any trademark, service mark, logo or trade name of
Sun Microsystems, Inc. or its licensors is granted.

*/
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include "XIMProtocol.hh"
#include "XIMPInputContext.hh"
#include "Xfactory.hh"

void
XIMPInputContext::set_preedit_spot_location(long x, long y) {
  attr_mask |= XIMP_PRE_SPOTL_MASK4;
  preedit_attr4.spot_location.x = x;
  preedit_attr4.spot_location.y = y;

  // as XIM client
  XVaNestedList preedit_attr;
  XPoint spot;
  spot.x = x; spot.y = y;
  preedit_attr = XVaCreateNestedList(NULL,
				     XNSpotLocation, &spot,
				     NULL);
  XSetICValues(xic,
	       XNPreeditAttributes, preedit_attr,
	       0);
  XFree(preedit_attr);
  return;
}
void
XIMPInputContext::set_preedit_area(long x, long y, unsigned long width,
				   unsigned long height) {
  // as XIM client
  XVaNestedList preedit_attr;
  XRectangle area;
  area.x = x; area.y = y; area.width = width; area.height = height;
  preedit_attr = XVaCreateNestedList(NULL,
				     XNArea, &area,
				     NULL);
  XSetICValues(xic,
	       XNPreeditAttributes, preedit_attr,
	       0);
  XFree(preedit_attr);
  return;
}
void
XIMPInputContext::set_preedit_area_needed(unsigned long width,
					  unsigned long height) {
  // as XIM client
  XVaNestedList preedit_attr;
  XRectangle area;
  area.x = 0; area.y = 0; area.width = width; area.height = height;
  preedit_attr = XVaCreateNestedList(NULL,
				     XNAreaNeeded, &area,
				     NULL);
  XSetICValues(xic,
	       XNPreeditAttributes, preedit_attr,
	       0);
  XFree(preedit_attr);
  if (width == 0 || height == 0) {
    // do geometry negotiation
    XRectangle *area2 = (XRectangle*)0;
    preedit_attr = XVaCreateNestedList(NULL,
				       XNAreaNeeded, &area2,
				       NULL);
    XGetICValues(xic,
		 XNPreeditAttributes, preedit_attr,
		 0);
    XFree(preedit_attr);
    if (area2) {
      if (width == 0) {
	preedit_attr4.area_needed.width = area2->width;
      }
      if (height == 0) {
	preedit_attr4.area_needed.height = area2->height;
      }
      XFree(area2);
    }
    // fallback to avoid setting 0
    if (preedit_attr4.area_needed.width == 0) {
      XFontSet font_set = XFactory::create_fontset(dpy, preedit_fontname);
      XFontSetExtents *fsx = XExtentsOfFontSet(font_set);
      preedit_attr4.area_needed.width = fsx->max_logical_extent.width *10;
      XFactory::free_fontset(dpy, font_set);
    }
    if (preedit_attr4.area_needed.height == 0) {
      XFontSet font_set = XFactory::create_fontset(dpy, preedit_fontname);
      XFontSetExtents *fsx = XExtentsOfFontSet(font_set);
      preedit_attr4.area_needed.height = fsx->max_logical_extent.height;
      XFactory::free_fontset(dpy, font_set);
    }
  }
  return;
}
void
XIMPInputContext::set_preedit_colormap(Colormap colormap) {
  // as XIM client
  XVaNestedList preedit_attr;
  preedit_attr = XVaCreateNestedList(NULL,
				     XNColormap, colormap,
				     NULL);
  XSetICValues(xic,
	       XNPreeditAttributes, preedit_attr,
	       0);
  XFree(preedit_attr);
  return;
}
void
XIMPInputContext::set_preedit_bg_pixmap(Pixmap bg_pixmap) {
  // as XIM client
  XVaNestedList preedit_attr;
  preedit_attr = XVaCreateNestedList(NULL,
				     XNBackgroundPixmap, bg_pixmap,
				     NULL);
  XSetICValues(xic,
	       XNPreeditAttributes, preedit_attr,
	       0);
  XFree(preedit_attr);
}
void
XIMPInputContext::set_preedit_line_spacing(long line_spacing) {
  // as XIM client
  XVaNestedList preedit_attr;
  preedit_attr = XVaCreateNestedList(NULL,
				     XNLineSpace, line_spacing,
				     NULL);
  XSetICValues(xic,
	       XNPreeditAttributes, preedit_attr,
	       0);
  XFree(preedit_attr);
}
void
XIMPInputContext::set_preedit_cursor(Cursor cursor) {
  // as XIM client
  XVaNestedList preedit_attr;
  preedit_attr = XVaCreateNestedList(NULL,
				     XNCursor, cursor,
				     NULL);
  XSetICValues(xic,
	       XNPreeditAttributes, preedit_attr,
	       0);
  XFree(preedit_attr);
}
void
XIMPInputContext::set_preedit_bg(unsigned long bg) {
  // as XIM client
  XVaNestedList preedit_attr;
  preedit_attr = XVaCreateNestedList(NULL,
				     XNBackground, bg,
				     NULL);
  XSetICValues(xic,
	       XNPreeditAttributes, preedit_attr,
	       0);
  XFree(preedit_attr);
}
void
XIMPInputContext::set_preedit_fg(unsigned long fg) {
  XVaNestedList preedit_attr;
  preedit_attr = XVaCreateNestedList(NULL,
				     XNForeground, fg,
				     NULL);
  XSetICValues(xic,
	       XNPreeditAttributes, preedit_attr,
	       0);
  XFree(preedit_attr);
}
Bool
XIMPInputContext::get_preedit_font() {
  if (!ximp_protocol->get_preedit_font(client_win,
				       (char **)&preedit_fontname)) {
    return False;
  }

  // as XIM client
  XVaNestedList preedit_attr;
  XFontSet font_set = XFactory::create_fontset(dpy, preedit_fontname);
  preedit_attr = XVaCreateNestedList(NULL,
				     XNFontSet, font_set,
				     NULL);
  XSetICValues(xic,
	       XNPreeditAttributes, preedit_attr,
	       0);
  XFree(preedit_attr);
  return True;
}
