リッチテキストボックスコントロールを使用して、IDEのような(編集不可能な)プログラムを設計しようとしています。基本的に、ユーザーが+/-ボタンをクリックするたびにコードの特定の部分を展開/折りたたみするには、RTBの左側に配置されたツリービューが必要です。拡張可能な折りたたみ可能な範囲は、中括弧が表示される場所として定義されます。たとえば、RTBで次のようなものがあった場合:
int main()
{
if (...)
{
if (...)
{
}
}
else
{
}
}
一番上の中かっこをクリックすると、main関数内のすべてが折りたたまれます。基本的に、その中括弧の中に含まれているのは折りたたまれているものです。要約すると、if / else関数でも実行することを除けば、VisualStudioの展開/折りたたみコード関数に非常によく似たものを設計しようとしています。
私はブラケットマッチングアルゴリズムを知っており、ブラケットのどのペアが一致するかを知るためにスタックを実装しました(タプルのリストに格納されている行番号)。
私が主に抱えている問題は、実際のツリービューをどのように設計するかです。ツリービューを線形にする必要があります。この場合、ノードが別のノードの上に追加されることはありません。別のノードの上に子ノードを実際に追加せずに展開/折りたたみボタンを追加できるアプローチを私は知りません。
また、+ /-ボタンと単一の垂直線を除いて、ツリービューノードは編集不可、非表示、クリック不可である必要があります。
最後に、これは、上記の要件を満たしている場合、ツリービューも正しくスクロールするためにRTBの垂直スクロールイベントが必要であることを前提としています。つまり、Treeviewの折りたたみ/展開セクションは、RTBに表示されるコードの部分に基づいて更新されます。
ツリーを初期化するために使用しているコードのセクションは次のとおりです。
public partial class LogicSimulationViewerForm : Form
{
private List<Tuple<string,Boolean>> visibleLines = new List<Tuple<string,Boolean>>();
private List<Tuple<int, int>> collapseRange = new List<Tuple<int, int>>();
private void TreeInit()
{
TreeNode tn;
Stack<int> openBracketLine = new Stack<int>();
int i = 0;
TreeLogicCode.Nodes.Clear();
foreach (string s in rtbLogicCode.Lines)
{
visibleLines.Add(Tuple.Create(s, true));
if (s == "{")
{
openBracketLine.Push(i);
}
else if (s == "}")
{
collapseRange.Add(Tuple.Create(openBracketLine.Pop(),i));
}
i++;
}
}
これがDesigner.scのソースコードですが、これは実際には必要ではないと思いますが、念のために言っておきます。
namespace DDCUI
{
partial class LogicSimulationViewerForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.TreeLogicCode = new System.Windows.Forms.TreeView();
this.labelLogicCode = new System.Windows.Forms.Label();
this.rtbLogicCode = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// TreeLogicCode
//
this.TreeLogicCode.Dock = System.Windows.Forms.DockStyle.Left;
this.TreeLogicCode.Location = new System.Drawing.Point(50, 0);
this.TreeLogicCode.Name = "TreeLogicCode";
this.TreeLogicCode.Scrollable = false;
this.TreeLogicCode.Size = new System.Drawing.Size(40, 600);
this.TreeLogicCode.TabIndex = 4;
//
// labelLogicCode
//
this.labelLogicCode.BackColor = System.Drawing.Color.LightGray;
this.labelLogicCode.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.labelLogicCode.Dock = System.Windows.Forms.DockStyle.Left;
this.labelLogicCode.ForeColor = System.Drawing.SystemColors.ControlText;
this.labelLogicCode.Location = new System.Drawing.Point(0, 0);
this.labelLogicCode.Margin = new System.Windows.Forms.Padding(3);
this.labelLogicCode.Name = "labelLogicCode";
this.labelLogicCode.Padding = new System.Windows.Forms.Padding(3);
this.labelLogicCode.Size = new System.Drawing.Size(50, 600);
this.labelLogicCode.TabIndex = 3;
this.labelLogicCode.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// rtbLogicCode
//
this.rtbLogicCode.Dock = System.Windows.Forms.DockStyle.Fill;
this.rtbLogicCode.Location = new System.Drawing.Point(90, 0);
this.rtbLogicCode.Name = "rtbLogicCode";
this.rtbLogicCode.Size = new System.Drawing.Size(510, 600);
this.rtbLogicCode.TabIndex = 5;
this.rtbLogicCode.Text = "";
this.rtbLogicCode.VScroll += new System.EventHandler(this.rtbLogicCode_VScroll);
//
// LogicSimulationViewerForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(600, 600);
this.Controls.Add(this.rtbLogicCode);
this.Controls.Add(this.TreeLogicCode);
this.Controls.Add(this.labelLogicCode);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "LogicSimulationViewerForm";
this.Text = "LogicSimulationViewerForm";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TreeView TreeLogicCode;
private System.Windows.Forms.Label labelLogicCode;
private System.Windows.Forms.RichTextBox rtbLogicCode;
}
}
この問題を解決するためのガイダンスをいただければ幸いです。前もって感謝します。