;+
; 
;             NAME : eis_setup_xwindow
; 
;          PURPOSE : To make an window of a particular size. 
;
; CALLING SEQUENCE : eis_setup_xwindow,xsize,ysize,[w_id,title=title]
; 
;           INPUTS : xsize and ysize - the size of the window in
;                    screeen pixels.
; 
;  OPTIONAL INPUTS : w_id - the window id number.
; 
;         KEYWORDS : title - the title of the window.
; 
;          OUTPUTS : An window. 
;
; OPTIONAL OUTPUTS : None.
;
;    COMMON BLOCKS : None.
; 
;          EXAMPLE : eis_setup_xwindow,600,400,10,title="MyWindow"
;
;    SPECIAL CALLS : None.
;
;          WRITTEN : HPW Many Moons ago.
;
;         MODIFIED : HPW 01-OCT-97 To include check on window ID.
;                  : HPW 16-JUL-98 To set font size for vector drawn
;                    fonts (6x9).
;                  : HPW 31-DEC-07 Fixed zbuffer for 24 bit
;                    color. Renamed to eis_setup_xwindow.
;                  : HPW 05-MAR-08 Added check for IDL release. 24 bit
;                    zbuffer only works with IDL > 6.3
; 
;-

pro eis_setup_xwindow,x,y,w_id,title=title,zbuff=zbuff,refresh=refresh,$
                  image=image

;; --- the first parameter could be the window id
if n_params() eq 1 then w_id = x

;; --- set window id to 0 if it is not input
if n_params() eq 2 then w_id = 0

;; --- window size from image array
if keyword_set(image) then begin
  x = (size(image))[1]
  y = (size(image))[2]
  if n_elements(w_id) eq 0 then w_id = 0
endif

;; --- setup the zbuffer
if keyword_set(zbuff) then begin
  set_plot,'Z'
  device,set_resolution=[x,y]
  device,set_character_size=[6,9]  ;; set vector font size
  if float(!version.release) ge 6.4 then begin
    device,set_pixel_depth=24
    device,decompose=0
  endif
  return
endif   

;; --- if not xwindows or MS windows then do nothing but return
match = where(!d.name eq ['X','WIN'],count)
if (count eq 0) then return

;; --- is window already created but not active?
device,window=window
open = where(window eq 1,c)
if c ne 0 then begin
  ;; --- some windows exist
  mm = where(w_id eq open,c)
  if c ne 0 then begin
    ;; --- requested window exists, set it
    wset,w_id
  endif
endif

;; --- set title
if not(keyword_set(title)) then title = 'IDL '+trim(w_id)

;; --- setup window, but only if necessary
if (!d.x_size ne x or !d.y_size ne y or $
    w_id ne !d.window or $
    keyword_set(refresh)) then begin
  window,w_id,xsize=x,ysize=y,title=title	;; draw window
  wset,w_id					;; set as current window
  device,set_character_size=[6,9]		;; set vector font size
  device,retain=2
  device,decompose=0
endif 

return
end
