SourceCode/PROMS/Volian.Controls.Library/frmImportWordContents.cs

329 lines
9.2 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using LBWordLibrary;
namespace Volian.Controls.Library
{
public partial class frmImportWordContents : Form
{
LBApplicationClass _WordApp;
bool _initializing = false;
private StepRTB _MyStepRTB = null;
public StepRTB MyStepRTB
{
get { return _MyStepRTB; }
set { _MyStepRTB = value; }
}
public frmImportWordContents()
{
_initializing = true;
InitializeComponent();
// C2019-021 Allow the Number field to be edited.
txbWordFile.Text = Properties.Settings.Default.ImportWordFilePath;
disableButtons();
_initializing = false;
btnOpen.Enabled = (txbWordFile.Text.Length > 0);
}
private void disableButtons()
{
btnOpen.Enabled = false;
btnNext.Enabled = false;
btnPage.Enabled = false;
btnPrev.Enabled = false;
}
private void ofd_FileOk(object sender, CancelEventArgs e)
{
_initializing = true;
txbWordFile.Text = ofd.FileName;
_initializing = false;
}
private void btnBrowse_Click(object sender, EventArgs e)
{
// if we have a word doc open, then close it and reset WordApp
if (_WordApp != null)
{
try
{
_WordApp.Quit();
}
catch
{
// will error if user already closed Word
}
_WordApp = null;
disableButtons();
}
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
btnOpen_Click(sender, e);
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (!File.Exists(txbWordFile.Text))
{
MessageBox.Show(string.Format("{0}", txbWordFile.Text), "File Does Not Exist", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
disableButtons();
return;
}
_WordApp = new LBApplicationClass();
_WordApp.Documents.Open(txbWordFile.Text);
_WordApp.Visible = true;
btnNext.Enabled = true;
btnPage.Enabled = true;
btnPrev.Enabled = true;
Properties.Settings.Default.ImportWordFilePath = txbWordFile.Text;
Properties.Settings.Default.Save();
}
private void btnPage_Click(object sender, EventArgs e)
{
_WordApp.Selection.MoveDown(LBWdUnits.wdScreen, 1, 0);
}
private void btnPrev_Click(object sender, EventArgs e)
{
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 0);
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 1);
try
{
// C2019-021 Use generic function CopyWordText
CopyWordText();
if (txbWrdText.Text.TrimEnd("\r\n\f".ToCharArray()) == "")
btnPrev_Click(sender, e);
else
Clipboard.SetText(txbWrdText.Text);
}
catch
{ }
}
private void btnNext_Click(object sender, EventArgs e)
{
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 0);
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1);
try
{
// C2019-021 Use Generic CopyWordText
CopyWordText();
// C2019-021If the word text only contains whitespace skip to next
if (txbWrdText.Text.TrimEnd("\f\r\n".ToCharArray()) == "")
btnNext_Click(sender, e);
else
Clipboard.SetText(txbWrdText.Text);
}
catch
{ }
}
// C2019-021 Generic CopyWordText (Copy Text, Style and Number to fields on form)
private void CopyWordText()
{
txvStyle.Text = _WordApp.Selection.Style.NameLocal;
txbLevel.Text = _WordApp.Selection.Style.ListLevelNumber.ToString();
tbxNumber.Text = _WordApp.Selection.Range.ListFormat.ListString;
int idx = -1;
if (_WordApp.Selection.Text.EndsWith("\t(\r") || _WordApp.Selection.Text.EndsWith("\t___\r"))
idx = _WordApp.Selection.Text.LastIndexOf("\t");
if (idx > 0)
txbWrdText.Text = _WordApp.Selection.Text.Substring(0, idx);
else
txbWrdText.Text = _WordApp.Selection.Text;
txbWrdText.Text = txbWrdText.Text.Replace('\xA0', ' '); // replace hard space with regular space
txbWrdText.Text = txbWrdText.Text.Replace('\x0B', ' '); // replace hard return with regular space
// C2019-021 Don't replace tabs
//txbWrdText.Text = txbWrdText.Text.Replace("\t", " "); // replace tabs with 5 regular spaces
}
private void txbWordFile_TextChanged(object sender, EventArgs e)
{
btnOpen.Enabled = !_initializing;
}
private void frmImportWordContents_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
if (_WordApp != null)
_WordApp.Quit();
}
catch
{
// incase user manually closed word
}
}
//C2019-021 Remove old method.
//private void btnNextIns_Click(object sender, EventArgs e)
//{
// if (MyStepRTB != null)
// {
// if (MyStepRTB.MyItemInfo.IsSection)
// {
// MyStepRTB.RtbSendKeys("^{DOWN}"); // <Ctrl><down arrow> - next edit window
// MyStepRTB.RtbSendKeys("^+b"); // insert previous
// }
// else
// MyStepRTB.RtbSendKeys("^+i"); // insert next (same type and level)
// MyStepRTB.RtbSendKeys("^v"); // clipboard paste
// Application.DoEvents();
// }
// this.Focus(); // set focus back to this form
//}
//private void btnNextRpl_Click(object sender, EventArgs e)
//{
// if (MyStepRTB != null)
// {
// MyStepRTB.RtbSendKeys("^{DOWN}"); // <Ctrl><down arrow> - next edit window
// MyStepRTB.RtbSendKeys("^a"); // select all
// MyStepRTB.RtbSendKeys("^v"); // clipboard paste
// Application.DoEvents();
// }
// this.Focus(); // set focus back to this form
//}
// C2019-021 New function to replace the PROMS text with the word text and move to the next paragraph in word.
private void btnReplaceNext_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
if (MyStepRTB.MyItemInfo.IsSection && tbxNumber.Text != "")
{
using (VEPROMS.CSLA.Library.Item ii = MyStepRTB.MyItemInfo.Get())
{
ii.MyContent.Text = txbWrdText.Text.Trim("\r\n\f".ToCharArray());
ii.MyContent.Number = tbxNumber.Text;
ii.MyContent.Save();
}
EditItem ei = MyStepRTB.Parent as EditItem;
ei.RefreshTab();
}
else
{
MyStepRTB.Text = txbWrdText.Text.Trim("\r\n".ToCharArray());
MyStepRTB.Select(MyStepRTB.TextLength, 0);
}
btnNext_Click(sender, e);
}
}
// C2019-021 New function to insert the Word text into PROMS and move to the next paragraph in Word
private void btnInsertNext_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
MyStepRTB.SelectedText = txbWrdText.Text.Trim("\r\n\f".ToCharArray());
MyStepRTB.Select(MyStepRTB.TextLength, 0);
btnNext_Click(sender, e);
}
}
// C2019-021 New Function to create a after node in PROMS
private void btnAfter_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
EditItem ei = MyStepRTB.Parent as EditItem;
if (ei != null)
{
ei.AddSiblingAfter();
}
}
}
// C2019-021 Add a New Section in PROMS (Default Type = 10000)
private void btnSect_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
EditItem ei = MyStepRTB.Parent as EditItem;
if (ei.MyItemInfo.IsProcedure)
{
ei.AddChild(VEPROMS.CSLA.Library.E_FromType.Section, 10000);// TODO: Need Type for Procedure Steps
return;
}
while (ei != null && !ei.MyItemInfo.IsSection)
if(ei.MyParentEditItem != null)
ei = ei.MyParentEditItem;
else
ei=ei.MyPreviousEditItem;
if (ei != null)
{
ei.AddSiblingAfter();
}
}
}
// C201-021 New Function Add a new High level step
private void btnHigh_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
EditItem ei = MyStepRTB.Parent as EditItem;
if (ei.MyItemInfo.IsSection)
{
ei.AddChild(VEPROMS.CSLA.Library.E_FromType.Step, 20002);
return;
}
while (ei != null && !ei.MyItemInfo.IsHigh)
if (ei.MyParentEditItem != null)
ei = ei.MyParentEditItem;
else
ei = ei.MyPreviousEditItem;
if (ei != null)
{
ei.AddSiblingAfter();
}
}
}
// C2019-021 New Function Add a new sequential substep
private void btnSEQ_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
EditItem ei = MyStepRTB.Parent as EditItem;
ei.AddChild(VEPROMS.CSLA.Library.E_FromType.Step, 20001);
}
}
// C2019-021 New Function Add a new parent step after
private void btnParentAfter_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
EditItem ei = MyStepRTB.Parent as EditItem;
while (ei.MyPreviousEditItem != null)
ei = ei.MyPreviousEditItem;
if (ei != null && ei.MyParentEditItem != null)
{
ei.MyParentEditItem.AddSiblingAfter();
}
}
}
// C2019-021 New Function Split the text in the current proms window into two
// The text before the selection is kept in the current PROMS window
// The selected text is deleted
// the text after the selection is cut to the clipboard
private void btnSplit_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
MyStepRTB.SelectedText = "";
MyStepRTB.Select(MyStepRTB.SelectionStart, MyStepRTB.TextLength - MyStepRTB.SelectionStart);
Clipboard.SetText(MyStepRTB.SelectedText);
MyStepRTB.SelectedText = "";
}
}
// C2019-021 New Function - Performs a paste in the current window
private void btnPaste_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
MyStepRTB.SelectedText = Clipboard.GetText();
}
}
}
}