using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace RegexBuddyActionClientDemo
{
///
/// Summary description for WinForm1.
///
public class FormActionDemo : System.Windows.Forms.Form
{
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
private System.Windows.Forms.TextBox textRegex;
private System.Windows.Forms.TextBox textReplace;
private System.Windows.Forms.CheckBox checkDotAll;
private System.Windows.Forms.CheckBox checkCaseless;
private System.Windows.Forms.CheckBox checkMultiline;
private System.Windows.Forms.RadioButton radioMatch;
private System.Windows.Forms.RadioButton radioReplace;
private System.Windows.Forms.RadioButton radioSplit;
private System.Windows.Forms.Label labelLimit;
private System.Windows.Forms.Button btnInvoke;
private System.Windows.Forms.NumericUpDown numericLimit;
public FormActionDemo()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.textRegex = new System.Windows.Forms.TextBox();
this.textReplace = new System.Windows.Forms.TextBox();
this.checkDotAll = new System.Windows.Forms.CheckBox();
this.checkCaseless = new System.Windows.Forms.CheckBox();
this.checkMultiline = new System.Windows.Forms.CheckBox();
this.radioMatch = new System.Windows.Forms.RadioButton();
this.radioReplace = new System.Windows.Forms.RadioButton();
this.radioSplit = new System.Windows.Forms.RadioButton();
this.labelLimit = new System.Windows.Forms.Label();
this.btnInvoke = new System.Windows.Forms.Button();
this.numericLimit = new System.Windows.Forms.NumericUpDown();
((System.ComponentModel.ISupportInitialize)(this.numericLimit)).BeginInit();
this.SuspendLayout();
//
// textRegex
//
this.textRegex.Location = new System.Drawing.Point(8, 32);
this.textRegex.Multiline = true;
this.textRegex.Name = "textRegex";
this.textRegex.Size = new System.Drawing.Size(440, 64);
this.textRegex.TabIndex = 3;
this.textRegex.Text = "Regular Expression";
//
// textReplace
//
this.textReplace.Location = new System.Drawing.Point(8, 104);
this.textReplace.Multiline = true;
this.textReplace.Name = "textReplace";
this.textReplace.Size = new System.Drawing.Size(440, 64);
this.textReplace.TabIndex = 4;
this.textReplace.Text = "Replacement Text";
//
// checkDotAll
//
this.checkDotAll.Location = new System.Drawing.Point(8, 176);
this.checkDotAll.Name = "checkDotAll";
this.checkDotAll.Size = new System.Drawing.Size(128, 24);
this.checkDotAll.TabIndex = 5;
this.checkDotAll.Text = "Dot matches newline";
//
// checkCaseless
//
this.checkCaseless.Location = new System.Drawing.Point(152, 176);
this.checkCaseless.Name = "checkCaseless";
this.checkCaseless.Size = new System.Drawing.Size(120, 24);
this.checkCaseless.TabIndex = 6;
this.checkCaseless.Text = "Case insensitive";
//
// checkMultiline
//
this.checkMultiline.Location = new System.Drawing.Point(280, 176);
this.checkMultiline.Name = "checkMultiline";
this.checkMultiline.Size = new System.Drawing.Size(168, 24);
this.checkMultiline.TabIndex = 7;
this.checkMultiline.Text = "^ and $ match at line breaks";
//
// radioMatch
//
this.radioMatch.Checked = true;
this.radioMatch.Location = new System.Drawing.Point(8, 8);
this.radioMatch.Name = "radioMatch";
this.radioMatch.TabIndex = 0;
this.radioMatch.TabStop = true;
this.radioMatch.Text = "Match";
//
// radioReplace
//
this.radioReplace.Location = new System.Drawing.Point(120, 8);
this.radioReplace.Name = "radioReplace";
this.radioReplace.TabIndex = 1;
this.radioReplace.Text = "Replace";
//
// radioSplit
//
this.radioSplit.Location = new System.Drawing.Point(232, 8);
this.radioSplit.Name = "radioSplit";
this.radioSplit.TabIndex = 2;
this.radioSplit.Text = "Split";
//
// labelLimit
//
this.labelLimit.Location = new System.Drawing.Point(8, 212);
this.labelLimit.Name = "labelLimit";
this.labelLimit.Size = new System.Drawing.Size(32, 14);
this.labelLimit.TabIndex = 8;
this.labelLimit.Text = "Limit";
//
// btnInvoke
//
this.btnInvoke.Location = new System.Drawing.Point(8, 240);
this.btnInvoke.Name = "btnInvoke";
this.btnInvoke.Size = new System.Drawing.Size(440, 23);
this.btnInvoke.TabIndex = 10;
this.btnInvoke.Text = "Invoke RegexBuddy";
this.btnInvoke.Click += new System.EventHandler(this.btnInvoke_Click);
//
// numericLimit
//
this.numericLimit.Location = new System.Drawing.Point(40, 208);
this.numericLimit.Maximum = new System.Decimal(new int[] {
65535,
0,
0,
0});
this.numericLimit.Name = "numericLimit";
this.numericLimit.Size = new System.Drawing.Size(56, 20);
this.numericLimit.TabIndex = 9;
//
// FormActionDemo
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(456, 272);
this.Controls.Add(this.numericLimit);
this.Controls.Add(this.btnInvoke);
this.Controls.Add(this.labelLimit);
this.Controls.Add(this.radioSplit);
this.Controls.Add(this.radioReplace);
this.Controls.Add(this.radioMatch);
this.Controls.Add(this.checkMultiline);
this.Controls.Add(this.checkCaseless);
this.Controls.Add(this.checkDotAll);
this.Controls.Add(this.textReplace);
this.Controls.Add(this.textRegex);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "FormActionDemo";
this.Text = "RegexBuddy Action Client Demo";
((System.ComponentModel.ISupportInitialize)(this.numericLimit)).EndInit();
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new FormActionDemo());
}
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(uint hwnd);
// We maintain only one connection to RegexBuddy for the entire application using static members
// If an application creates more than one connection, it will launch more than once instance of RegexBuddy
// Note that even if the user closes the instance of RegexBuddy created by our connection,
// that instance will still remain resident in the background, until our application closes.
// Each call to InitAction will reuse the same instance of RegexBuddy launched by our connection.
static RegexBuddy.RegexBuddyIntfClass RegexBuddyConnection;
// Since we use one connection for the entire application, we need to remember in which context
// we called InitAction, so we know how to handle FinishAction.
static FormActionDemo ActiveDemoForm;
static void FinishAction(object Action)
{
if (ActiveDemoForm != null) {
// Retrieve the updated action from RegexBuddy
ActiveDemoForm.SetAction((object[])Action);
// FinishAction is only called once per call to InitAction
// So we should clean up this static reference
ActiveDemoForm = null;
}
}
private void btnInvoke_Click(object sender, System.EventArgs e)
{
if (RegexBuddyConnection == null) {
// Connect to RegexBuddy
try {
RegexBuddyConnection = new RegexBuddy.RegexBuddyIntfClass();
} catch (Exception ex) {
MessageBox.Show("Could not connect to RegexBuddy because of the following error:\r\n" +
ex.Message + "\r\n\r\n" +
"Make sure that RegexBuddy is installed on your computer, and has been run at least once.",
"Invoke RegexBuddy");
return;
}
// Connect FinishAction event handler
// Note: RegexBuddy supports only a single event handler per client connection.
// Therefore, you cannot handle both the FinishAction and FinishAction events
// using the event connection method used here.
RegexBuddy.IRegexBuddyIntfEvents_FinishActionEventHandler FinishActionEventHandler =
new RegexBuddy.IRegexBuddyIntfEvents_FinishActionEventHandler(FinishAction);
RegexBuddyConnection.FinishAction += FinishActionEventHandler;
// Make sure our instance of RegexBuddy makes it clear to the user that we're connected to it
RegexBuddyConnection.IndicateApp(Text, (uint)Handle);
}
// Since we use one connection for the entire application, we need to remember in which context
// we called InitAction, so we know how to handle FinishAction.
ActiveDemoForm = this;
// Send action to RegexBuddy
RegexBuddyConnection.InitAction(GetAction());
// Bring RegexBuddy to front.
// SetForegroundWindow only works when the calling thread has input focus.
// Since we have input focus when the user clicked our button,
// it is our job to bring RegexBuddy to front.
SetForegroundWindow(RegexBuddyConnection.GetWindowHandle());
}
object[] GetAction()
{
object[] Action = new object[9];
Action[0] = 1; // Version. Must be 1.
if (radioReplace.Checked) {
Action[1] = 1;
} else if (radioSplit.Checked) {
Action[1] = 2;
} else {
Action[1] = 0;
}
Action[2] = textRegex.Text;
Action[3] = textReplace.Text;
Action[4] = checkDotAll.Checked;
Action[5] = checkCaseless.Checked;
Action[6] = checkMultiline.Checked;
Action[7] = numericLimit.Value;
Action[8] = ""; // Description
return Action;
}
private void SetAction(object[] Action)
{
// Since we called InitRegex with Action[0] == 1,
// FinishRegex will always be called with Action[0] == 1
// But we check just in case
if ((int)Action[0] == 1) {
if ((int)Action[1] == 1) {
radioReplace.Checked = true;
} else if ((int)Action[1] == 2) {
radioSplit.Checked = true;
} else {
radioMatch.Checked = true;
}
textRegex.Text = (string)Action[2];
textReplace.Text = (string)Action[3];
checkDotAll.Checked = (bool)Action[4];
checkCaseless.Checked = (bool)Action[5];
checkMultiline.Checked = (bool)Action[6];
numericLimit.Value = (int)Action[7];
}
}
}
}