pro open_themis_document,info=info,filename=filename,statuscode=statuscode,statusmsg=statusmsg

catch,Error_status

if (Error_status NE 0) then begin
   statusmsg = !ERROR_STATE.MSG
   statuscode = -3
   catch,/cancel
   return
endif

historywin=info.historywin
statustext=info.statusbar
guiId = info.master

tgt_dirname=file_dirname(filename)

fi=file_info(filename)
if (fi.directory) then begin
   statusmsg=string(filename,format='("open_themis_document: Open failed: ",A," is a directory.")')
   statuscode=-1
   return
endif else if (~fi.exists) then begin
   statusmsg=string(filename,format='("open_themis_document: Open failed. File ",A," does not exist.")')
   statuscode=-1
   return
endif

statustext->Update,'Opening THEMIS document '+filename
widget_control, /hourglass

; Load XML document from the filename
xmldoc=obj_new('IDLffXMLDOMDocument')
xmldoc->Load,filename=filename

; Create thm_ui_document object to receive XML data, using the
; no-arguments constructor

doc_to_load=obj_new('thm_ui_document')

; Drill down into the DOM tree to get the first non-whitespace child
; of <body> element

body=xmldoc->GetFirstChild() ; should be 'body'
if (body->GetNodeName() NE 'body') then begin
  message,'Expected body node, got '+body->GetNodeName()
endif

sib=body->GetFirstChild()
; Skip any extraneous text elements (newlines, etc)
while (sib->GetNodeName() EQ '#text')  do begin
   sib=sib->GetNextSibling()
endwhile

; sib should be a THM_UI_DOCUMENT node
if (sib->GetNodeName() NE 'THM_UI_DOCUMENT') then begin
  message,'Expected THM_UI_DOCUMENT node, got '+sib->GetNodeName()
endif

doc_to_load->BuildFromDOMElement,sib

; We're done with the XML DOM tree

obj_destroy,xmldoc

; Invoke the onLoad method, passing in the windowstorage, loadedData,
; and windowMenus objects.
; This sets the loadedData element of the newly created
; callSequence object, replays the calls, and adds all the window objects
; to windowStorage while keeping windowMenus updated.

; Reset loadedData object (there may be other objects that hold references
; to the loadedData object, so obj_destroy/obj_new is too drastic).

info.loadedData->reset

doc_to_load->onLoad,windowStorage=info.windowStorage,windowMenus=info.windowMenus,loadedData=info.loadedData,historywin=historywin,statustext=statustext,guiId=guiId

; Update the draw window
info.drawObject->update,info.windowStorage, info.loadedData
info.drawObject->draw

statuscode=0
statusmsg=STRING(filename,format='("THEMIS document successfully read from ",A)')
return
end