// Routines for displaying attribute values on coloured background /* Create Layout DXL with canvas, allowing to print attribute values on coloured background A typical usage is a combination of "create canvas" and subsequent calls of "print_on_canvas". "show_enumeration" displays enumeration data depending on the colours defined in the underlying type. Note that the type values must be increasing monotonically starting from 0 in steps of 1 as the values are used for indexing the defined colours Note that multiple lines are displayed */ int local_variable_canvas_line_counter = 0 // ----------------------------------------------------------------------------- // --------------------------------------------------------------- string replace(string input, Regexp r, string replace_by) { // Replace sub-strings in string variable string returnVal = null if (!null input) { // No null input while (r input) { // Loop through regular expression matches returnVal = returnVal input[0:start 0-1] replace_by input = input[end 0+1:] } // Loop through regular expression matches returnVal = returnVal input } // No null input return returnVal } // Replace sub-strings in string variable // --------------------------------------------------------------- string replace(string input, string replace_what, string replace_by) { // Replace sub-strings in string variable Regexp r = regexp replace_what return replace(input, r, replace_by) } // Replace sub-strings in string variable // ----------------------------------------------------------------------------- // // Depending on the background colour, return either // White or Black to be used as foreground colour // int fgColor(int realColor) { // Default foreground colour for given background colour int fgCol = realColor_Black if (realColor == 4){fgCol = realColor_White; return fgCol} if (realColor == 5){fgCol = realColor_White; return fgCol} if (realColor == 6){fgCol = realColor_White; return fgCol} if (realColor == 13){fgCol = realColor_White; return fgCol} if (realColor == 14){fgCol = realColor_White; return fgCol} if (realColor == 15){fgCol = realColor_White; return fgCol} if (realColor == 16){fgCol = realColor_White; return fgCol} if (realColor == 21){fgCol = realColor_White; return fgCol} if (realColor >= 26 && realColor <= 31){fgCol = realColor_White; return fgCol} if (realColor == 35){fgCol = realColor_White; return fgCol} return fgCol } // Default foreground colour for given background colour // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- // // Create canvas and fill with background colour "bg" // void create_canvas(int bg, int heightCanvas) { // Create canvas and fill with background colour "bg" DBE canvasDBE = getCanvas int rh if (canvasDBE == null){halt} rh = height canvasDBE if (rh < 0){setHeight (heightCanvas du)} if (bg <= 30){realBackground(canvasDBE, bg)} local_variable_canvas_line_counter = 0 } // Create canvas and fill with background colour "bg" // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- // // Display string on canvas // void print_on_canvas(string disp, int fg) { // Display string on canvas // Optionally, wrap text in canvas DBE canvasDBE = getCanvas int x0, y0, lineHeight, reqW, curW, y, i, lastSpaceChar string tmp bool borderReached //disp = replace(disp, "\n", " | ") if (canvasDBE == null){halt} realColor(canvasDBE, fg) x0 = 5 y0 = 5 lineHeight = height(canvasDBE, "A") reqW = width(canvasDBE) curW = 0 y = local_variable_canvas_line_counter * lineHeight + y0 + (lineHeight/2) curW = width(canvasDBE, disp) if (curW <= (reqW-(2*x0)) && !matches("\n", disp)) { // Text is shorter than canvas width, i.e. text fits into one line draw(canvasDBE, x0, y, disp) local_variable_canvas_line_counter ++ } // Text is shorter than canvas width, i.e. text fits into one line else { // Text is longer than canvas width, i.e. text does not fit into one line i = 0 lastSpaceChar = -1 tmp = null borderReached = false while (!borderReached) { // Put as much text as possible into one line // However, respect line breaks, and try not to break within words tmp = disp[0:i] if (tmp[i:i] == " "){lastSpaceChar = i} if (disp[i:i] == "\n"){borderReached = true} else { // No manual line break so far curW = width(canvasDBE, tmp) if (tmp == disp || curW >= (reqW-(3*x0))) { // End of string reached, or substring longer than canvas width borderReached = true if (lastSpaceChar >= 0) { // Go back to last space character if possible i = lastSpaceChar tmp = disp[0:i] } // Go back to last space character } // End of string reached, or substring longer than canvas width } // No manual line break so far i++ } // Put as much text as possible into one line draw(canvasDBE, x0, y, tmp) local_variable_canvas_line_counter++ tmp = disp[i:] print_on_canvas(tmp, fg) } // Text is longer than canvas width, i.e. text does not fit into one line setHeight ((local_variable_canvas_line_counter * lineHeight) du) } // Display string on canvas // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- void display(int bg, int fg, string s) { // Display coloured text on a coloured canvas create_canvas(bg, 15) print_on_canvas(s, fg) } // Display coloured text on a coloured canvas // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- void display(int bg, string s){display(bg, fgColor bg, s)} // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- // Display coloured canvas depending on an enumerated attribute // // This is an internal subroutine returning, if successful, the // background colour index // Note that you can choose whether it return real colors or logical // colors by setting "setRealColorOptionForTypes true|false" beforehand int backgroundColour(Object obj, string attName) { Module mod = module(obj) AttrDef ad = find(mod, attName) AttrType at int i if (null ad){display "No attribute " attName; return realColor_White} at = ad.type if (at.type "" != "Enumeration") {display "Attribute " attName "(" at.type ") is not of type Enumeration"; return realColor_White} if (!null obj.attName "" ) { i = obj.attName return at.colors[i] } else {return realColor_White} } // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- // Display coloured canvas depending on an enumerated attribute // Optionally print attribute value on top // void show_enumeration( Object objEnumeration, string attEnumeration, Object objDisplay, string attDisplay, int height ) { // This perm has the relevant object as parameter in order to allow // the display of linked object's attributes Module modEnumeration, modDisplay int bgCol, fgCol AttrDef adDisp if (null objEnumeration){display "Null object [Enumeration]"; return} if (null objDisplay){display "Null object [Display]"; return} modEnumeration = module objEnumeration modDisplay = module objDisplay bgCol = backgroundColour(objEnumeration, attEnumeration) if (bgCol < 0){return} setRealColorOptionForTypes true create_canvas(bgCol, height) fgCol = fgColor(bgCol) if (!null attDisplay) { adDisp = find(modDisplay, attDisplay) if (null adDisp){print_on_canvas("No attribute " attDisplay, fgCol); return} print_on_canvas(objDisplay.attDisplay "", fgCol) } } // ----------------------------------------------------------------------------- void show_enumeration(Object objEnumDisp, string attName, dispAttName, int height) { // This perm has the relevant object as parameter in order to allow // the display of linked object's attributes show_enumeration(objEnumDisp, attName, objEnumDisp, dispAttName, height) } // ----------------------------------------------------------------------------- void show_enumeration(string attName, dispAttName, int height){show_enumeration(obj, attName, obj, dispAttName, height)} void show_enumeration(string attName, dispAttName){show_enumeration(obj, attName, obj, dispAttName, 18)} void show_enumeration(string attName, int height){show_enumeration(obj, attName, obj, "", height)} void show_enumeration(string attName){show_enumeration(obj, attName, obj, "", 18)} void show_enumeration(Object o1, string attName, Object o2, string dispAttName){show_enumeration(o1, attName, o2, dispAttName, 18)} void show_enumeration(Object o, string attName, dispAttName){show_enumeration(o, attName, dispAttName, 18)} void show_enumeration(Object o, string attName, int height){show_enumeration(o, attName, obj, "", height)} void show_enumeration(Object o, string attName){show_enumeration(o, attName, obj, "", 18)} // ----------------------------------------------------------------------------- // Overloaded perm: show linked attribute void show_enumeration(string LinkMod, FormalMod, enumAttrib, dispAttrib) { Module mod Link l if (exists module FormalMod) { if (open module FormalMod){mod = module item FormalMod} else {mod = read(FormalMod, false)} if (!null mod) { for l in obj <- LinkMod do { show_enumeration(source(l), enumAttrib, dispAttrib) break } } } } // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- // Plot arrow on canvas void arrow(DBE cnvs, int x0, y0, x1, y1, symSize) { if (y0 == y1) { symSize = symSize/2 line(cnvs, x0, y0, x1, y0) line(cnvs, x0, y0, x0+symSize, y0-symSize) line(cnvs, x0, y0, x0+symSize, y0+symSize) line(cnvs, x1, y0, x1-symSize, y0-symSize) line(cnvs, x1, y0, x1-symSize, y0+symSize) } } // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- // Usage examples // ----------------------------------------------------------------------------- void canvasExample_01() { // Colour canvas column based on enumerated attribute show_enumeration("enum") } // Colour canvas column based on enumerated attribute // ----------------------------------------------------------------------------- void canvasExample_02() { // Colour canvas column based on enumerated attribute // and overlay second attribute's value show_enumeration("enum", "Object Text") } // Colour canvas column based on enumerated attribute // ----------------------------------------------------------------------------- void canvasExample_03() { // Colour canvas column based on enumerated attribute // and overlay second attribute's value display(realColor_Grey77, realColor_Red, "hello world\nThis is text with\nmultiple lines.") } // Colour canvas column based on enumerated attribute // ----------------------------------------------------------------------------- ------------------------