📄 connect.cs
字号:
}
catch(Exception)
{
pt = Control.MousePosition;
}
Control wnd = new Control();
wnd.CreateControl();
TemplatePopupMenu menu = m_tplMgr.GetTemplatePopupMenuForActiveDocument();
menu.EnableTemplateItems = TemplateItemsEnabled(); // Only enable template items if there's a text file open
menu.Show(wnd, pt);
}
/// <summary>
///
/// </summary>
private void DoInsertTemplateCommand()
{
Document doc = Application.ActiveDocument;
if(doc == null)
return;
TextSelection sel = (TextSelection)doc.Selection;
if(sel == null)
return;
Boolean success = false;
using(UndoContextGuard ctx = new UndoContextGuard(Application.UndoContext, "Template insertion"))
{
if(sel.IsEmpty)
sel.WordLeft(true, 1);
if(sel.IsEmpty)
return;
String templateID = sel.Text;
TemplateFile tplFile = m_tplMgr.GetTemplateFileForActiveDocument();
TemplateMenuItem mi = tplFile.GetTemplateByID(templateID);
if(mi == null)
{
sel.MoveToPoint(sel.BottomPoint, false); // Move caret to end of selection
Application.ExecuteCommand("Edit.CompleteWord", ""); // Forward processing
return;
}
success = InsertTemplate(MakeDialogCaption(mi.Text), mi.TemplateText);
}
if(!success)
doc.Undo();
}
/// <summary>
///
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
private String MakeDialogCaption(String text)
{
Int32 pos = 0;
while((pos = text.IndexOf('&', pos)) != -1)
{
text = text.Remove(pos, 1);
if(text[pos] == '&')
++pos;
}
if((pos = text.IndexOf('\t')) != -1)
text = text.Substring(0, pos);
return text;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnClick(Object sender, EventArgs e)
{
try
{
TemplateMenuItem mi = (TemplateMenuItem)sender;
if(mi.Id == TemplateFile.OpenTemplateFileMenuID)
{
TemplateFile tplFile = m_tplMgr.GetTemplateFileForActiveDocument();
Application.Documents.Open(tplFile.BaseFilePath, "Text", false);
}
else if(mi.Id == TemplateFile.AboutMenuID)
{
AboutBox dlg = new AboutBox();
dlg.ShowDialog(this);
}
else if(mi.Id == TemplateFile.RebindKeyboardMenuID)
CommandTools.RebindKeyboard(s_crInsertTemplate);
else if(mi.Id == TemplateFile.OpenHelpMenuID)
System.Diagnostics.Process.Start(Configuration.HelpFilePath);
else
InsertTemplate(MakeDialogCaption(mi.Text), mi.TemplateText);
}
catch(Exception ex) { MessageBox.Show(ex.Message, ex.Source); }
}
/// <summary>
///
/// </summary>
/// <param name="caption"></param>
/// <param name="text"></param>
private Boolean InsertTemplate(String caption, String text)
{
String formattedText = TemplateFormatter.Format(caption, text, m_tplMgr.GetTemplateFileForActiveDocument(), this);
if(formattedText == null)
return false;
Document doc = Application.ActiveDocument;
if(doc == null)
return false;
TextSelection sel = (TextSelection)doc.Selection;
if(sel == null)
return false;
using(UndoContextGuard ctx = new UndoContextGuard(Application.UndoContext, "Template insertion"))
{
EditPoint startPoint = sel.TopPoint.CreateEditPoint();
EditPoint endPoint = startPoint.CreateEditPoint();
if(!sel.IsEmpty)
{
EditPoint ep = sel.BottomPoint.CreateEditPoint();
endPoint.Delete(ep);
}
// Indent the template if it not inserted in the first column
Int32 indentChars = startPoint.DisplayColumn - 1;
if(indentChars > 0)
{
String filler = new String(' ', indentChars);
StringBuilder sb = new StringBuilder(formattedText);
sb.Replace("\r\n", "\r\n" + filler);
formattedText = sb.ToString();
}
endPoint.Insert(formattedText);
// Select the inserted text
sel.MoveToLineAndOffset(startPoint.Line, startPoint.LineCharOffset, false);
sel.MoveToLineAndOffset(endPoint.Line, endPoint.LineCharOffset, true);
// Check if the template has a cursor reposition token
Boolean found = sel.FindText(Delim.CursorPos, (Int32) vsFindOptions.vsFindOptionsFromStart);
// If the cursor reposition token was found, locate the cursor in that position
// by deleting the token
if(found)
sel.Delete(1);
if(Configuration.SmartFormat)
startPoint.SmartFormat(endPoint);
}
return true;
}
private class TemplateFileManager
{
class Info
{
public Info(String filePath, EventHandler handler)
{
try
{
m_tpl = new TemplateFile(filePath);
m_menu = new TemplatePopupMenu();
m_menu.EventHandler += handler;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, ex.Source);
}
}
public TemplateFile TemplateFile
{
get
{
EnsureLatestVersion();
return m_tpl;
}
}
public TemplatePopupMenu Menu
{
get
{
EnsureLatestVersion();
return m_menu;
}
}
private void EnsureLatestVersion()
{
if(m_tpl.Changed)
m_tpl.ReadFile(m_menu);
}
private TemplateFile m_tpl;
private TemplatePopupMenu m_menu;
}
public TemplateFileManager(Connect parent)
{
m_parent = parent;
m_templates = new Hashtable();
}
private String GetTemplateFilePath()
{
Document doc = Application.ActiveDocument;
String docName = String.Empty;
if(doc != null && doc.Type == "Text")
docName = doc.FullName;
return Configuration.GetTemplateFilePathFor(docName).ToUpper();
}
private Info GetInfoForActiveDocument()
{
String s = GetTemplateFilePath();
Info info = (Info)m_templates[s];
if(info == null)
{
info = new Info(s, new EventHandler(m_parent.OnClick));
m_templates.Add(s, info);
}
return info;
}
public TemplateFile GetTemplateFileForActiveDocument()
{
return GetInfoForActiveDocument().TemplateFile;
}
public TemplatePopupMenu GetTemplatePopupMenuForActiveDocument()
{
return GetInfoForActiveDocument().Menu;
}
private Hashtable m_templates;
private Connect m_parent;
}
#region IWin32Window Members
public System.IntPtr Handle
{
get
{
return new IntPtr(s_app.MainWindow.HWnd);
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -