%angel

eddiquerywin = window {
   title = "Eddi Query Viewer";
   content = [frameinput, frametable] };

frameinput = frame {
   side = "top";
   content = [lblquery, tbquery, butquery] };

frametable = frame ();

lblquery = label {
   text = "Eddi query:";
   side = "left" };

tbquery = textbox {
   height=1;
   width = 20;
   side = "left" };

butquery = button {
   text = "Fetch";
   side = "right" };


%eden

installeddi();

resulthead=[];
resultset=[];
resultsetrows is resultset#;
resultsetcols is resultset# > 0 ? resultset[1]# : 0;

cellbg="white";
cellrelief="sunken";
cellheadbg="grey";
cellheadrelief="groove";

proc update_resultset : resultset {
   auto i, j, framecontent, colcontent;
   framecontent = [];
   
   for (i=1; i<=resultsetcols; i++) {
   
      colcontent = [];
      
      ## create header cell
      execute("%angel\n cellc"//str(i)//"head = label {
	 background=cellheadbg; relief=cellheadrelief; fill=\"x\" };");
      if (i<=resulthead#)
         `"cellc"//str(i)//"head_text"` = resulthead[i];
      append colcontent, "cellc"//str(i)//"head";
      
      ## create cells
      for (j=1; j<=resultsetrows; j++) {
         execute("%angel\n cellc"//str(i)//"r"//str(j)//" = label {
	    background=cellbg; relief=cellrelief; fill=\"x\" };");
	 `"cellc"//str(i)//"r"//str(j)//"_text"` = resultset[j][i];
	 append colcontent, "cellc"//str(i)//"r"//str(j);
      }
      
      ## create column containing cells
      execute("%angel\n framecol"//str(i)//" = frame {side=\"left\"};");
      `"framecol"//str(i)//"_content"` = colcontent;
      append framecontent, "framecol"//str(i);
   }
   
   ## place columns in the frame
   frametable_content = framecontent;
}

proc showrel {
	para rel;
	auto temp;
	
	if (rel==@)
	{
		writeln("EDEN: ERROR: showrel has been given an "//
		"undefined table.");
		return [[[]]];
	}
	
	autocalc = 0;
	curr_rel = rel;
	display_list(mkdisplist_rel());

	if (rel# == 2)
		writeln("(1 row)");
	else
		writeln("(" // str(rel#-1) // " rows)");
	
	temp = rel[1];
	shift temp;
	resulthead = temp;
	shift rel;
	resultset = rel;
	
	curr_rel = _DUMMY;
	autocalc = 1;
};


proc butquery_trigger : butquery_mouseclick {
   if (butquery_mouseclick == 0) {
      execute("%eddi\n"//tbquery_text//"\n");
   }
}
