/* ----------- * Grid object * ----------- * * Usage: * * To create a new grid... * grid_define( "mygrid", cols, rows, xposition, yposition ); * where * mygrid = the name of the new grid * cols = number of columns * rows = number of rows * xposition = distance along x-axis from top-left of screen * yposition = distance along y-axis from top-left of screen * * To add the grid to the screen... * %scout * screen = mygrid_display; * * To change the contents of cell in col 2, row 4... * mygrid_data[4][2] = "new cell contents"; * * To change the heading of column 3... * mygrid_header[3] = "new heading"; * * Other editable properties (mygrid_property) * _font, _bgcolor, _fgcolor, _bdcolor, _relief, * _head_bgcolor, _head_fgcolor, _selected_bgcolor, _selected_fgcolor, * _sensitive, _cellheight, _border * * * Antony Harfield 23/02/2003 * * Based on the EM Spreadsheet by Chris Roe * * Updates: * - an individual row can be highlighted * - rows can be sensitive to clicks * - added 'grid_resize' proc * - swapped the array around (transposed it) * - removed 'grid_display' proc * - checks dimensions of grid * */ %scout string DFfont, DFbgcolor, DFfgcolor, DFbdcolor, DFrelief; integer DFalign, DFsensitive; %eden proc grid_define { para name, cols, rows, origin_x, origin_y; auto i, j, s; if (cols < 1 || rows < 0) error("Incorrect column/row numbers in grid component"); execute(name//"_cols = "//str(cols)//";"); execute(name//"_rows = "//str(rows)//";"); execute(name//"_data = array("//str(rows)//", array("//str(cols)//",\" \"));"); execute(name//"_header = array("//str(cols)//",\" \");"); execute("%scout integer "//name//"_charwidth = 8; integer "//name//"_cellheight = 14; integer "//name//"_border = 2; string "//name//"_font = DFfont; string "//name//"_bgcolor = \"white\"; string "//name//"_fgcolor = \"black\"; string "//name//"_bdcolor = \"black\"; string "//name//"_relief = \"flat\"; string "//name//"_head_bgcolor = \"grey\"; string "//name//"_head_fgcolor = \"black\"; string "//name//"_head_relief = \"groove\"; string "//name//"_selected_bgcolor = \"yellow\"; string "//name//"_selected_fgcolor = \"black\"; integer "//name//"_sensitive = DFsensitive; integer "//name//"_selected_row = 0; integer "//name//"_originx = "//str(origin_x)//"; integer "//name//"_originy = "//str(origin_y)//"; integer "//name//"_c0_width = 0; point "//name//"_c0_r0_point = {"//name//"_originx,"//name//"_originy}; "); /* define the col and row widths and heights */ for (i=1; i<=cols; i++) { execute("%scout\n integer "//name//"_c"//str(i)//"_width;\n"); s = name//"_c"//str(i)//"_width is "//name//"_charwidth * max("//name//"_c"//str(i)//"_r0_str#"; for (j=1; j<=rows; j++) { s = s // ", " // name//"_c"//str(i)//"_r"//str(j)//"_str#"; } s = s // ");"; execute(s); } for (j=0; j<=rows; j++) { execute("%scout\n integer "//name//"_r"//str(j)//"_height;\n"); s = name // "_r" // str(j) // "_height is "//name//"_cellheight;"; execute(s); } /* define the header */ for (i=1; i<=cols; i++) { execute("%scout point "//name//"_c"//str(i)//"_r0_point = "//name//"_c"//str(i-1)//"_r0_point + {"//name//"_c"//str(i-1)//"_width+2*"//name//"_border,0}; string "//name//"_c"//str(i)//"_r0_str; window "//name//"_c"//str(i)//"_r0_win = { type: TEXT frame: (["//name//"_c"//str(i)//"_r0_point, "//name//"_c"//str(i)//"_r0_point+{"//name//"_c"//str(i)//"_width,"//name//"_r0_height}]) string: "//name//"_c"//str(i)//"_r0_str fgcolor: "//name//"_head_fgcolor bgcolor: "//name//"_head_bgcolor font: "//name//"_font relief: "//name//"_head_relief bdcolor: "//name//"_bdcolor border: "//name//"_border sensitive: "//name//"_sensitive }; "); execute(name//"_c"//str(i)//"_r0_str is "//name//"_header["//str(i)//"];"); } /* define the windows/textboxes */ for (i=1; i<=cols; i++) { for (j=1; j<=rows; j++) { execute("%scout point "//name//"_c"//str(i)//"_r"//str(j)//"_point = "//name//"_c"//str(i)//"_r"//str(j-1)//"_point + {0,"//name//"_r"//str(j-1)//"_height+2*"//name//"_border}; string "//name//"_c"//str(i)//"_r"//str(j)//"_str; window "//name//"_c"//str(i)//"_r"//str(j)//"_win = { type: TEXT frame: (["//name//"_c"//str(i)//"_r"//str(j)//"_point, "//name//"_c"//str(i)//"_r"//str(j)//"_point+{"//name//"_c"//str(i)//"_width,"//name//"_r"//str(j)//"_height}]) string: "//name//"_c"//str(i)//"_r"//str(j)//"_str fgcolor: if "//name//"_selected_row == "//str(j)//" then "//name//"_selected_fgcolor else "//name//"_fgcolor endif bgcolor: if "//name//"_selected_row == "//str(j)//" then "//name//"_selected_bgcolor else "//name//"_bgcolor endif font: "//name//"_font relief: "//name//"_relief bdcolor: "//name//"_bdcolor border : "//name//"_border sensitive : "//name//"_sensitive }; "); execute(name//"_c"//str(i)//"_r"//str(j)//"_str is "//name//"_data["//str(j)//"]["//str(i)//"];"); } } /* sensitive cells */ for (j=0; j<=rows; j++) { for (i=1; i<=cols; i++) { s = "proc "//name//"_c"//str(i)//"_r"//str(j)//"_click : "//name//"_c"//str(i)//"_r"//str(j)//"_win_mouse_1 { if ("//name//"_c"//str(i)//"_r"//str(j)//"_win_mouse_1[2] == 4) "//name//"_selected_row = "//str(j)//"; }"; execute(s); } } /* assemble all cells into display */ s = "%scout\n display "//name//"_display = <"; for (i=1; i<=cols; i++) for (j=0; j<=rows; j++) s = s // ""//name//"_c"//str(i)//"_r"//str(j)//"_win /"; s = substr(s,1,s#-1) // ">;\n"; execute(s); } proc grid_resize { para name, cols, rows; auto i, j, s, olddata, oldheader; if (cols < 1 || rows < 0) error("Incorrect column/row numbers in grid component"); if ((`name//"_cols"` != cols) || (`name//"_rows"` != rows)) { /* remove dependencies */ for (i=`name//"_cols"`; i>=1; i--) { for (j=0; j<=`name//"_rows"`; j++) { execute(name//"_c"//str(i)//"_r"//str(j)//"_str = "//name//"_c"//str(i)//"_r"//str(j)//"_str;"); } } /* new column and row values */ `name//"_cols"` = cols; `name//"_rows"` = rows; /* trim or extend arrays */ oldheader = `name//"_header"`; olddata = `name//"_data"`; `name//"_header"` = array(cols," "); `name//"_data"` = array(rows, array(cols," ")); for (i=1; i<=cols; i++) { `name//"_header"`[i] = i > oldheader# ? " " : oldheader[i]; for (j=1; j<=rows; j++) { `name//"_data"`[j][i] = (j > olddata# || i > olddata[j]#) ? " " : olddata[j][i]; } } /* define the col and row widths and heights */ for (i=1; i<=cols; i++) { execute("%scout\n integer "//name//"_c"//str(i)//"_width;\n"); s = name//"_c"//str(i)//"_width is "//name//"_charwidth * max("//name//"_c"//str(i)//"_r0_str#"; for (j=1; j<=rows; j++) { s = s // ", " // name//"_c"//str(i)//"_r"//str(j)//"_str#"; } s = s // ");"; execute(s); } for (j=0; j<=rows; j++) { execute("%scout\n integer "//name//"_r"//str(j)//"_height;\n"); s = name // "_r" // str(j) // "_height is "//name//"_cellheight;"; execute(s); } /* define the header */ for (i=1; i<=cols; i++) { execute("%scout point "//name//"_c"//str(i)//"_r0_point = "//name//"_c"//str(i-1)//"_r0_point + {"//name//"_c"//str(i-1)//"_width+2*"//name//"_border,0}; string "//name//"_c"//str(i)//"_r0_str; window "//name//"_c"//str(i)//"_r0_win = { type: TEXT frame: (["//name//"_c"//str(i)//"_r0_point, "//name//"_c"//str(i)//"_r0_point+{"//name//"_c"//str(i)//"_width,"//name//"_r0_height}]) string: "//name//"_c"//str(i)//"_r0_str fgcolor: "//name//"_head_fgcolor bgcolor: "//name//"_head_bgcolor font: "//name//"_font relief: "//name//"_head_relief bdcolor: "//name//"_bdcolor border: "//name//"_border sensitive: "//name//"_sensitive }; "); execute(name//"_c"//str(i)//"_r0_str is "//name//"_header["//str(i)//"];"); } /* define the windows/textboxes */ for (i=1; i<=cols; i++) { for (j=1; j<=rows; j++) { execute("%scout point "//name//"_c"//str(i)//"_r"//str(j)//"_point = "//name//"_c"//str(i)//"_r"//str(j-1)//"_point + {0,"//name//"_r"//str(j-1)//"_height+2*"//name//"_border}; string "//name//"_c"//str(i)//"_r"//str(j)//"_str; window "//name//"_c"//str(i)//"_r"//str(j)//"_win = { type: TEXT frame: (["//name//"_c"//str(i)//"_r"//str(j)//"_point, "//name//"_c"//str(i)//"_r"//str(j)//"_point+{"//name//"_c"//str(i)//"_width,"//name//"_r"//str(j)//"_height}]) string: "//name//"_c"//str(i)//"_r"//str(j)//"_str fgcolor: if "//name//"_selected_row == "//str(j)//" then "//name//"_selected_fgcolor else "//name//"_fgcolor endif bgcolor: if "//name//"_selected_row == "//str(j)//" then "//name//"_selected_bgcolor else "//name//"_bgcolor endif font: "//name//"_font relief: "//name//"_relief bdcolor: "//name//"_bdcolor border : "//name//"_border sensitive : "//name//"_sensitive }; "); execute(name//"_c"//str(i)//"_r"//str(j)//"_str is "//name//"_data["//str(j)//"]["//str(i)//"];"); } } /* sensitive cells */ for (j=0; j<=rows; j++) { for (i=1; i<=cols; i++) { s = "proc "//name//"_c"//str(i)//"_r"//str(j)//"_click : "//name//"_c"//str(i)//"_r"//str(j)//"_win_mouse_1 { if ("//name//"_c"//str(i)//"_r"//str(j)//"_win_mouse_1[2] == 4) "//name//"_selected_row = "//str(j)//"; }"; execute(s); } } /* assemble all cells into display */ s = "%scout\n "//name//"_display = <"; for (i=1; i<=cols; i++) for (j=0; j<=rows; j++) s = s // ""//name//"_c"//str(i)//"_r"//str(j)//"_win /"; s = substr(s,1,s#-1) // ">;\n"; execute(s); } }