unit X11_Windows;

// TODO: XSetTextProperty/XGetTextProperty gateway.
// TODO: icon name, ...

interface

uses X11_Objects, X11_Drawables, X11, xlib, type_fixes;

type  
  TWindow = class(TDrawable, IDrawable, IWindow, X11_Objects.IObject, IInterface)
  protected
    procedure DeleteUnderlyingObject; override;    
    function GetBackgroundPixmap : IPixmap;
    procedure SetBackgroundPixmap(const aValue : IPixmap);
    function GetTitle : ANSIString;
    procedure SetTitle(aValue : ANSIString);
  published
    constructor Create(aDisplay : PDisplay; aParentWindow : IWindow; aX, aY : TInteger; aWidth, aHeight, aBorderWidth : TCardinal {FIXME unsigned long border?! unsigned long background});
    procedure Clear;
    procedure Map;
    procedure Unmap;
    procedure UnmapSubWindows;
    procedure Raise_;
    procedure Lower;
    property BackgroundPixmap : IPixmap read GetBackgroundPixmap write SetBackgroundPixmap;
    property Title : ANSIString read GetTitle write SetTitle;
  end;

implementation

procedure TWindow.DeleteUnderlyingObject;
begin
  if fHandle <> 0 then begin
    ensureOK(XDestroyWindow(Display, fHandle));
    fHandle := 0;
  end;
end;

procedure TWindow.Clear;
begin
  ensureOK(XClearWindow(Display, fHandle));
end;


procedure TWindow.Map;
begin
  ensureOK(XMapWindow(Display, fHandle));
end;

procedure TWindow.Unmap;
begin
  ensureOK(XUnmapWindow(Display, fHandle));
end;

procedure TWindow.UnmapSubWindows;
begin
  ensureOK(XUnmapSubwindows(Display, fHandle));
end;

procedure TWindow.Raise_;
begin
  ensureOK(XRaiseWindow(Display, fHandle));
end;

procedure TWindow.Lower;
begin
  ensureOK(XLowerWindow(Display, fHandle));
end;

function TWindow.GetBackgroundPixmap : IPixmap;
begin
  //FIXME;
  Result := nil;
end;

procedure TWindow.SetBackgroundPixmap(const aValue : IPixmap);
begin
  XSetWindowBackgroundPixmap(Display, fHandle, aValue.Handle);
  // FIXME pin down background pixmap.
end;

{$ASSERTIONS ON}

constructor TWindow.Create(aDisplay : PDisplay; aParentWindow : IWindow; aX, aY : TInteger; aWidth, aHeight, aBorderWidth : TCardinal {FIXME unsigned long border?! unsigned long background});
var
  VParent : TExternalHandle;  
  VWindow : TUINT32;
begin
  assert(Assigned(aDisplay));
  assert(Assigned(aParentWindow));

//  VParent := 0;
{  if Assigned(aParentWindow) then}
    VParent := aParentWindow.Handle;

  VWindow := (XCreateSimpleWindow(aDisplay, VParent, aX, aY, aWidth, aHeight, aBorderWidth, 0, 0));
  Reuse(aDisplay, VWindow, True);
  //fHandle := blah(aDisplay, VParent);
end;

function TWindow.GetTitle : ANSIString;
var
  fTitle : PChar;
begin
  CheckError(XFetchName(Display, fHandle, @fTitle));
  Result := fTitle;
  XFree(fTitle);
end;

procedure TWindow.SetTitle(aValue : ANSIString);
begin
  XStoreName(Display, fHandle, PChar(aValue));
  // FIXME flush?
end;

end.