/// <summary> /// Parses the given element and paints it onto the canvas. /// </summary> /// <param name="tagName">Name of the node to be parsed.</param> /// <param name="attrs">Attributes of the node to be parsed.</param> public void ParseElement(string tagName, Dictionary <string, Object> attrs) { if (canvas == null && tagName.ToLower().Equals("graph")) { scale = mxUtils.GetDouble(attrs, "scale", 1); canvas = CreateCanvas(attrs); if (canvas != null) { canvas.Scale = scale; } } else if (canvas != null) { bool edge = tagName.ToLower().Equals("edge"); bool group = tagName.ToLower().Equals("group"); bool vertex = tagName.ToLower().Equals("vertex"); if ((edge && attrs.ContainsKey("points")) || ((vertex || group) && attrs.ContainsKey("x") && attrs.ContainsKey("y") && attrs.ContainsKey("width") && attrs.ContainsKey("height"))) { mxCellState state = new mxCellState(null, null, attrs); string label = ParseState(state, edge); canvas.DrawCell(state); canvas.DrawLabel(label, state, false); } } }
/// <summary> /// Parses the given element and paints it onto the canvas. /// </summary> /// <param name="tagName">Name of the node to be parsed.</param> /// <param name="attrs">Attributes of the node to be parsed.</param> public void ParseElement(string tagName, Dictionary<string, Object> attrs) { if (canvas == null && tagName.ToLower().Equals("graph")) { scale = mxUtils.GetDouble(attrs, "scale", 1); canvas = CreateCanvas(attrs); if (canvas != null) { canvas.Scale = scale; } } else if (canvas != null) { bool edge = tagName.ToLower().Equals("edge"); bool group = tagName.ToLower().Equals("group"); bool vertex = tagName.ToLower().Equals("vertex"); if ((edge && attrs.ContainsKey("points")) || ((vertex || group) && attrs.ContainsKey("x") && attrs.ContainsKey("y") && attrs.ContainsKey("width") && attrs.ContainsKey("height"))) { mxCellState state = new mxCellState(null, null, attrs); string label = ParseState(state, edge); canvas.DrawCell(state); canvas.DrawLabel(label, state, false); } } }
/// <summary> /// Draws the given cell and label onto the specified canvas. No /// children or descendants are painted. /// </summary> public void DrawState(mxICanvas canvas, mxCellState state, String label) { Object cell = (state != null) ? state.Cell : null; if (cell != null && cell != model.Root && (model.IsVertex(cell) || model.IsEdge(cell))) { Object obj = canvas.DrawCell(state); Object lab = null; // Holds the current clipping region in case the label will // be clipped Region clip = null; Region newClip = new Region(state.GetRectangle()); // Indirection for image canvas that contains a graphics canvas mxICanvas clippedCanvas = (mxUtils.GetString(state.Style, mxConstants. STYLE_OVERFLOW, "").Equals("hidden")) ? canvas : null; if (clippedCanvas is mxImageCanvas) { clippedCanvas = ((mxImageCanvas) clippedCanvas).GdiCanvas; Point pt = ((mxImageCanvas) canvas).Translate; newClip.Translate(pt.X, pt.Y); } if (clippedCanvas is mxGdiCanvas) { Graphics g = ((mxGdiCanvas) clippedCanvas).Graphics; clip = g.Clip; g.Clip = newClip; } if (label != null && state.LabelBounds != null) { lab = canvas.DrawLabel(label, state, false); } // Restores the previous clipping region if (clippedCanvas is mxGdiCanvas) { ((mxGdiCanvas)clippedCanvas).Graphics.Clip = clip; } // Invokes the cellDrawn callback with the object which was created // by the canvas to represent the cell graphically if (obj != null) { // LATER: Add inner callback for rendering //CellDrawn(cell, obj, lab); } } }
/// <summary> /// Draws the graph onto the given canvas. /// </summary> /// <param name="canvas">Canvas onto which the graph should be drawn.</param> public void DrawGraph(mxICanvas canvas) { DrawCell(canvas, model.Root); }
/// <summary> /// Draws the given cell onto the specified canvas. /// </summary> /// <param name="canvas">Canvas onto which the cell should be drawn.</param> /// <param name="cell">Cell that should be drawn onto the canvas.</param> public void DrawCell(mxICanvas canvas, Object cell) { DrawState(canvas, view.GetState(cell), GetLabel(cell)); // Draws the children on top int childCount = model.GetChildCount(cell); for (int i = 0; i < childCount; i++) { DrawCell(canvas, model.GetChildAt(cell, i)); } }
/// <summary> /// Draws the given cells using a Graphics2D canvas and returns the buffered image /// that represents the cells. /// </summary> public static mxICanvas DrawCells(mxGraph graph, Object[] cells, double scale, mxRectangle clip, CanvasFactory factory) { mxICanvas canvas = null; if (cells == null) { cells = new Object[] { graph.Model.Root }; } if (cells != null) { // Gets the current state of the view mxGraphView view = graph.View; Dictionary <Object, mxCellState> states = view.States; double oldScale = view.Scale; // Keeps the existing translation as the cells might // be aligned to the grid in a different way in a graph // that has a translation other than zero bool eventsEnabled = view.IsEventsEnabled; // Disables firing of scale events so that there is no // repaint or update of the original graph view.IsEventsEnabled = false; try { // TODO: Factor-out into mxTemporaryCellStates class view.States = new Dictionary <Object, mxCellState>(); view.Scale = scale; // Validates the vertices and edges without adding them to // the model so that the original cells are not modified for (int i = 0; i < cells.Length; i++) { view.ValidateCellState(view.ValidateCell(cells[i])); } if (clip == null) { clip = graph.GetPaintBounds(cells); } if (clip != null && clip.Width > 0 && clip.Height > 0) { Rectangle rect = clip.GetRectangle(); canvas = factory.CreateCanvas(rect.Width + 1, rect.Height + 1); if (canvas != null) { double previousScale = canvas.Scale; Point previousTranslate = canvas.Translate; try { canvas.Translate = new Point(-rect.X, -rect.Y); canvas.Scale = view.Scale; for (int i = 0; i < cells.Length; i++) { graph.DrawCell(canvas, cells[i]); } } finally { canvas.Scale = previousScale; canvas.Translate = previousTranslate; } } } } finally { view.Scale = oldScale; view.States = states; view.IsEventsEnabled = eventsEnabled; } } return(canvas); }