您的位置:首页 > 编程语言

scintilla 中的代码折叠功能的使用

2007-12-17 17:59 861 查看
本文翻译自http://sphere.sourceforge.net/flik/docs/scintilla-folding.html

使用 scintilla 中的代码折叠功能
scintilla 是一个脚本编辑组件,您可以到 http://www.scintilla.org 这个网站上看看。

--------------------------------------------------------------------------------
下面介绍如何使用 scintilla 的代码折叠功能

--------------------------------------------------------------------------------

首先咋们来创建一些我们稍后会用到的一些常量。
static const int MARGIN_SCRIPT_FOLD_INDEX = 1;

为窗口注册 margin Event (即用户执行折叠和展开代码时,触发的事件)事件(仅用于 windows 环境)

static const int WINDOW_ID = 900;

BEGIN_MESSAGE_MAP(CDocumentWindow, CDocumentWindowsBaseClass)
ON_NOTIFY(SCN_MARGINCLICK, WINDOW_ID, OnMarginClicked)
END_MESSAGE_MAP()

(这里的 WINDOW_ID 就是我们调用 CreateWindow 创建窗口时经常用到的)

设置我们需要的 lexer(词法识别器,用于识别代码格式)...
(可以直接使用已有的常量,也可以自己手动创建...)

SendEditor(SCI_SETLEXER, SCLEX_CPP);
SendEditor(SCI_SETSTYLEBITS, 5);

设置我们用到的 lexer 的一些属性

SendEditor(SCI_SETPROPERTY, (WPARAM)"fold", (LPARAM)"1");
SendEditor(SCI_SETPROPERTY, (WPARAM)"fold.compact", (LPARAM)"0");

(这里的 fold.compact 选项就是折叠必要的代码行;我不太喜欢这个特性,但是这个属性缺省值就是"1")
下面这样也不错:

SendEditor(SCI_SETPROPERTY, (WPARAM)"fold.comment", (LPARAM)"1");
SendEditor(SCI_SETPROPERTY, (WPARAM)"fold.preprocessor", (LPARAM)"1");

现在让我们将所有的 margins 重置
(可以通过 RecalcLineMargin 函数处理...)

SendEditor(SCI_SETMARGINWIDTHN, MARGIN_SCRIPT_FOLD_INDEX, 0);

接着,设置 margin 类型和 margin 掩码(mask),并重置...

SendEditor(SCI_SETMARGINTYPEN, MARGIN_SCRIPT_FOLD_INDEX, SC_MARGIN_SYMBOL);
SendEditor(SCI_SETMARGINMASKN, MARGIN_SCRIPT_FOLD_INDEX, SC_MASK_FOLDERS);
SendEditor(SCI_SETMARGINWIDTHN, MARGIN_SCRIPT_FOLD_INDEX, 20);

设置一些其他的选项

SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDER, SC_MARK_PLUS);
SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPEN, SC_MARK_MINUS);
SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEREND, SC_MARK_EMPTY);
SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_EMPTY);
SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPENMID, SC_MARK_EMPTY);
SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERSUB, SC_MARK_EMPTY);
SendEditor(SCI_MARKERDEFINE, SC_MARKNUM_FOLDERTAIL, SC_MARK_EMPTY);

SendEditor(SCI_SETFOLDFLAGS, 16, 0); // 16 如果没有展开就在折叠行的下面划一条横线

我们注册一下通知事件,这样当用户点击 margin 时,scintilla 就会通知我们。

SendEditor(SCI_SETMARGINSENSITIVEN, MARGIN_SCRIPT_FOLD_INDEX, 1);

响应 SCN_MARGINCLICK 事件...(请注意函数原型)

afx_msg void
CDocumentWindow::OnMarginClick(NMHDR* nmhdr, LRESULT* result)
{
SCNotification* notify = (SCNotification*)nmhdr;

const int modifiers = notify->modifiers;
const int position = notify->position;
const int margin = notify->margin;
const int line_number = SendEditor(SCI_LINEFROMPOSITION, position, 0);

switch (margin)
{
case MARGIN_SCRIPT_FOLD_INDEX:
{
SendEditor(SCI_TOGGLEFOLD, line_number, 0);
}break;
}
}

有了上面这些应该能够工作了。
You'll want to play around with it until you're happy with the way the folding works,
scintilla seems to use a much more complicated MarginClick...
Luckily harvesting the MarginClick code from scintilla is easy:

首先修改一下 OnMarginClick:

SendEditor(SCI_TOGGLEFOLD, line_number, 0);

修改为:
MarginClick(position, modifiers);

接着在你的头文件中...
Then in your header file...

bool MarginClick(int position, int modifiers);
void Expand(int &line, bool doExpand,
bool force = false, int visLevels = 0, int level = -1);
void FoldAll();

接着到 SciTEBase.cxx 中找一些你需要的代码,直接粘贴到你自己的文件中(译者注:没想到作者要我这样写代码:))

对了,你最好赶快更新一下你的 Goto Line 函数...(译者:跳转到某一行功能,可能是这里比较容易出错)

SendEditor(SCI_ENSUREVISIBLEENFORCEPOLICY, line_number);
SendEditor(SCI_GOTOLINE, line_number);

就这些了,enjoy.

如果对此有什么问题或者建议你可以发邮件到 vascy@hotmail.com

libbyliugang translate to chinese.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐