EAIIG HD
㈱東アジア国際産業グループ
ホールディングス
コラム:ソフトウェア技術公開
( 社外向けページ )
( From Company to Outer )
( From Company to Others )
EAIIG HD
㈱東アジア国際産業グループ
ホールディングス
( 社外向けページ )
( From Company to Outer )
( From Company to Others )
EAIIG HD >コラム:ソフトウェア公開
!催奇性薬品は絶対禁止!
10:53 2022/02/21 子供(コドモ:Codomo)=Child, Children である。
// #define _DEBUG
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//add
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Windows.Threading;
using System.Runtime.InteropServices;
using System.Collections; // v0.98.0070.03
using System.Security.Permissions;// v0.98.0070
namespace PeacePadEditor01
{
partial class PPad1_Form1
{
//----------------------------------------------------------------------------------------------
// 編集メニュー
//----------------------------------------------------------------------------------------------
// 再検討
/***********************************************************************/
/// <summary>
/// 編集メニューの活性/非活性の制御
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 編集EToolStripMenuItem_DropDownOpened( object sender, EventArgs e )
{
// 検討:活性/非活性制御
// 「元に戻す」の活性/非活性制御
if( this.getCurrentTextBox().CanUndo.Equals( true ) )
{
this.元に戻すUToolStripMenuItem.Enabled = true;
}
// 「切り取り」の活性/非活性制御
if( false == this.getCurrentTextBox().SelectedText.Equals( "" ) )
{
this.切り取りTToolStripMenuItem.Enabled = true;
}
// 「コピー」の活性/非活性制御
if( false == this.getCurrentTextBox().SelectedText.Equals( "" ) )
{
this.コピーCToolStripMenuItem.Enabled = true;
}
// 「貼り付け」の活性/非活性制御
if ( false == Clipboard.GetText().Equals( "" ) ) //( true == Clipboard.ContainsText() ) v0.98.0087.02
{
// this.貼り付けPToolStripMenuItem.Enabled = true; v0.98.0088
// v0.98.0087.02
if ( true == this.編集ロックToolStripMenuItem.Checked ) {
this.貼り付けPToolStripMenuItem.Enabled = false;
}
else {
this.貼り付けPToolStripMenuItem.Enabled = true;
}
// v0.98.0088
if ( true == this.getCurrentTextBox2().isReadOnly() ) {
this.貼り付けPToolStripMenuItem.Enabled = false;
}
}
// 「すべて選択」の活性/非活性制御
//if( false == this.getCurrentTextBox2().Text.Equals( "" ) )
//{
// this.すべて選択AToolStripMenuItem.Enabled = true;
//}
}
// 再検討
/***********************************************************************/
/// <summary>
/// 編集メニュー > 元に戻す
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 元に戻すUToolStripMenuItem_Click( object sender, EventArgs e )
{
// フォーカスがあれば
if( true == this.getCurrentTextBox().Focused )
{
// メインのテキストボックス
if( true == this.getCurrentTextBox().CanUndo )
{
this.getCurrentTextBox().Undo();
//this.getCurrentTextBox().ClearUndo();
}
}
else if( true == this.comboBox1.Focused )
{
// 検索ボックス 検討中
;
}
else if( true == this.comboBox2.Focused )
{
// 置換えボックス 検討中
;
}
}
// 再検討
/***********************************************************************/
/// <summary>
/// 編集メニュー>切り取り
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 切り取りTToolStripMenuItem_Click( object sender, EventArgs e )
{
//
if( true == this.getCurrentTextBox().Focused )
{
// 文メインのテキストボックス
this.getCurrentTextBox().Cut();
}
else if( true == this.comboBox1.Focused )
{
// 検索ボックス
//Clipboard.SetText( this.comboBox1.SelectedText, TextDataFormat.UnicodeText );
//this.comboBox1.SelectedText = "";
}
else if( true == this.comboBox2.Focused )
{
// 置換えボックス
//Clipboard.SetText( this.comboBox2.SelectedText, TextDataFormat.UnicodeText );
//this.comboBox2.SelectedText = "";
}
}
// 再検討
/***********************************************************************/
/// <summary>
/// 編集メニュー>コピー
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void コピーCToolStripMenuItem_Click( object sender, EventArgs e )
{
//
if( true == this.getCurrentTextBox().Focused )
{
// 文字列のコピー
this.getCurrentTextBox().Copy();
}
else if( true == this.comboBox1.Focused )
{
// 検索ボックスへからコピー
//Clipboard.SetText( this.comboBox1.SelectedText, TextDataFormat.UnicodeText );
}
else if( true == this.comboBox2.Focused )
{
// 置換えボックスへからコピー
//Clipboard.SetText( this.comboBox2.SelectedText, TextDataFormat.UnicodeText );
}
}
// 再検討
/***********************************************************************/
/// <summary>
/// 編集メニュー>貼り付け
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 貼り付けPToolStripMenuItem_Click( object sender, EventArgs e )
{
// 検討 テキストデータがないなら戻る
if( !Clipboard.ContainsText() ) return;
// 張り込み
if( true == this.getCurrentTextBox().Focused )
{
// 貼り付け
// this.getCurrentTextBox2().Paste2();
// v0.98.0070
// this.setEditLocInfFromSelectionTextWithPaste( Clipboard.GetText() );
this.getCurrentTextBox().Paste();
}
else if( true == this.comboBox1.Focused )
{
// 検索ボックスへの張り込み
//this.comboBox1.SelectedText = Clipboard.GetText( TextDataFormat.UnicodeText );
}
else if( true == this.comboBox2.Focused )
{
// 置換えボックスへの張り込み
//this.comboBox2.SelectedText = Clipboard.GetText( TextDataFormat.UnicodeText );
}
}
// 再検討
/***********************************************************************/
/// <summary>
/// 編集メニュー>すべて選択
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void すべて選択AToolStripMenuItem_Click( object sender, EventArgs e )
{
//
if ( true == this.getCurrentTextBox().Focused )
{
// メインのテキストボックス
this.getCurrentTextBox().SelectAll();
}
else if ( true == this.comboBox1.Focused )
{
// 検索ボックス
//this.comboBox1.SelectAll();
}
else if ( true == this.comboBox2.Focused )
{
// 置換えボックス
//this.comboBox2.SelectAll();
}
}
/***********************************************************************/
/// <summary>
/// 編集メニュー>日付と時刻 イベントハンドラ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 日付と時刻ToolStripMenuItem_Click( object sender, EventArgs e )
{
// 日付と時刻の書式
string fmt = Properties.Settings.Default.FORMAT_DATA_TIME;
// 現在日時
string now = System.DateTime.Now.ToString( fmt );
// v0.98.0070
this.str_inp = now;
this.bl_date_flg = true;
this.getCurrentTextBox().Paste( now );
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// 排他編集モード Click イベントハンドラ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 排他編集モードToolStripMenuItem_Click( object sender, EventArgs e )
{
if ( false == this.排他編集モードToolStripMenuItem.Checked )
{
this.排他編集モードToolStripMenuItem.Checked = true;
CS_MsgBox.warningShow(
PPad1_Message.Default.MES_WARN_FILE_READONLY_LOCK );
}
else
{
this.排他編集モードToolStripMenuItem.Checked = false;
}
//
Properties.Settings.Default.bl_file_readonly_lock
= this.排他編集モードToolStripMenuItem.Checked;
Properties.Settings.Default.Save();
}
// ファイルオープン時
// ファイル名確定時
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// checkOnReadonlyToFile()
/// </summary>
/// <param name="str_fname"></param>
private void checkOnReadonlyToFile( string str_fname )
{
if ( (null == str_fname) && str_fname.Trim().Equals( "" ) ) return;
if ( true == this.排他編集モードToolStripMenuItem.Checked )
{
FileInfo fInfo = new FileInfo( str_fname );
if ( fInfo.IsReadOnly )
{
// readonly ファイルならフラグOFF
this.getCurrentTextBox2().setLockFlg( false );
}
else
{
File.SetAttributes( str_fname,
File.GetAttributes( str_fname ) | FileAttributes.ReadOnly );
this.getCurrentTextBox2().setLockFlg( true );
}
}
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// checkOffReadonlyToFile()
/// </summary>
/// <param name="str_fname"></param>
private void checkOffReadonlyToFile( string str_fname )
{
if ( (null == str_fname) && str_fname.Trim().Equals( "" ) )
{
this.getCurrentTextBox2().setLockFlg( false );
return;
}
//if ( true == this.排他編集モードToolStripMenuItem.Checked )
//{
if ( true == this.getCurrentTextBox2().getLockFlg() )
{
if ( File.Exists( str_fname ) )
{
File.SetAttributes( str_fname,
File.GetAttributes( str_fname ) & (~FileAttributes.ReadOnly) );
}
this.getCurrentTextBox2().setLockFlg( false );
}
//}
}
/***********************************************************************/
/// <summary>
/// CRLF変換
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cRLF変換ToolStripMenuItem_Click( object sender, EventArgs e )
{
//
// this.getCurrentTextBox2().convertTextCRLF2();
}
//--------------------------------------------------
// 編集位置のマーク
//--------------------------------------------------
// 編集位置 v0.98.0070.03
private ArrayList edit_Loc_inf = new ArrayList();
//
private string str_inp = "";
private bool bl_date_flg = false;
// 編集前のテキストボックス(TextBox)の文字数
private int before_text_length_textbox = 0;
private int BeforeTextLengthOfTextBox
{
get { return this.before_text_length_textbox; }
// set は、都合により未使用。
}
// setter before_text_length_textbox
private void setBeforeTextLengthOfTextBox()
{
this.before_text_length_textbox = this.getCurrentTextBox2().getTextLength();
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// テキスト変更イベントハンドラ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void momeChou_TextEditBox011_TextChanged( object sender, EventArgs e )
{
// v0.98.0070.11
if ( true == this.getCurrentTextBox2().getSkipTextChangedFlg() )
{
this.edit_Loc_inf.Clear();
this.panel3.Refresh();
return;
}
// 編集中フラグ
this.getCurrentTextBox2().setEditingFlg( true );
// テキストボックスのキャレットの位置を表示
this.setCaretLocationToStatusLabel2();
// v0.98.0070.03
// TextBox.Clear() でのイベント対応(次の0記録をスキップ)。
//if ( this.getCurrentTextBox2().getText().Length <= 0 )
//{
// return;
//}
// v0.98.0070.10
this.adjustEditLocationInf();
this.str_inp = "";
this.bl_date_flg = false;
// v0.98.0070.03
// 2019/10/22 00:29:00 文字数の増減を算出して、その値により編集位置を計算
// this.edit_Loc_inf.Add( this.getCurrentTextBox2().getEditLoc() - this.getPlusLengthWithText() );
// テキスト文字数の保存 v0.98.0070.10
this.setBeforeTextLengthOfTextBox();
// v0.98.0070.03
this.panel3.Refresh();
}
// v0.98.0070.17.03
/// <summary>
///
/// </summary>
/// <returns></returns>
//private int getPlusLengthWithText()
//{
// int num_char = this.getCurrentTextBox2().getTextLength() - this.BeforeTextLengthOfTextBox;
// if ( 0 < num_char ) return num_char;
// return 0;
//}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// 編集位置情報の調整 // v0.98.0070
/// </summary>
private void adjustEditLocationInf()
{
// 2019/10/01 07:56:06, 2019/10/01 11:46:29
// 文字、文字列を追加のとき
int text_Length_diff = this.getCurrentTextBox2().getTextLength() - this.BeforeTextLengthOfTextBox;
int idx_diff = text_Length_diff;
if ( 0 < idx_diff ) // 増えた
{
// 編集位置より下側の位置情報を修正する
int idx_start = this.getCurrentTextBox2().selectionStart() - idx_diff;
this.shiftIdxForEditLocInf( idx_start, idx_diff );
// v0.98.0070
if ( 1 < idx_diff )
{
string str_add = "";
if ( this.str_inp.Equals( "\r\n" ) ) // 改行
{
str_add = this.str_inp;
this.edit_Loc_inf.Add( this.getCurrentTextBox2().getEditLoc() -2 ); // - this.getPlusLengthWithText() );
// this.edit_Loc_inf.Add( this.getCurrentTextBox2().getEditLoc() ); // 2019/11/05 08:57:47
}
else if ( true == this.bl_date_flg ) // F2 での日付時刻
{
str_add = this.str_inp;
this.edit_Loc_inf.Add( this.getCurrentTextBox2().getEditLoc() ); // - this.getPlusLengthWithText() );
}
else
{
str_add = this.getCurrentTextBox2().getText().Substring(
this.getCurrentTextBox2().selectionStart() - idx_diff, idx_diff );
//Console.WriteLine( "debug=" + str_add );
// str_add = Clipboard.GetText(); // ペースト
this.setEditLocInfFromSelectionTextWithPaste2( idx_start, str_add );
}
// end
}
else
{
this.edit_Loc_inf.Add( this.getCurrentTextBox2().getEditLoc() ); // - this.getPlusLengthWithText() );
}
return;
}
else if ( idx_diff < 0 ) // 減ったので減算
{
// v0.98.0070, v0.98.0070.13
if (idx_diff < -1)
{
this.deleteEditLocInf( Math.Abs( idx_diff ) );
}
else
{
// v0.98.0070.13
this.subtractEditLocInf( Math.Abs( idx_diff ) );
}
this.edit_Loc_inf.Add( this.getCurrentTextBox2().getEditLoc() ); // - this.getPlusLengthWithText() );
return;
}
else
{
; // 調査中
}
return;
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// 編集位置情報の idx の追加調整(ペースト時) v0.98.0070.10
/// </summary>
/// <param name="idx_add"></param>
private void shiftIdxForEditLocInf( int idx_start, int idx_add )
{
// 2019/10/01 07:16:23
// 編集位置情報内を検索して現在位置より大きい位置データに+1
// 検索では、sort()した方がわかりやすいが速度に疑問があるため
// これをせず、1つずつ、(int)idx < TextBox.SelectionStart を調べる。
// このとき、object を int に変換して比較する。int.Parse( obj.ToString() );
// foreach()を検討。→ foreach_ref が動作しないため変更。
// this.edit_Loc_inf.indexOf()
// 2019/10/03 19:42:22
// 編集位置の追加を検討。
// ロジックの検討 foreach_ref が動作しないため変更。
for ( int i = 0; i < this.edit_Loc_inf.Count; i++ )
{
// Object obj = this.edit_Loc_inf[ i ];
int char_idx = (int)this.edit_Loc_inf[ i ]; // int.Parse( obj.ToString() );
// 2019/10/21 14:19:09 確認必要 v0.98.0070.15
if ( idx_start <= char_idx )
{
char_idx += idx_add;
this.edit_Loc_inf[ i ] = char_idx; // (Object)char_idx.ToString();
}
}
}
// v0.98.0070.17.03
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// テキストペースト時の編集位置記録 v0.98.0070
/// </summary>
/// <param name="text"></param>
//private void setEditLocInfFromSelectionTextWithPaste( int idx_start, string text )
//{
// bool bl_enter_flg = false;
// for ( int idx = 0; idx < text.Length - 1; idx++ )
// {
// if ( text[ idx ].Equals( '\r' ) && text[ idx + 1 ].Equals( '\n' ) )
// {
// bl_enter_flg = true;
// // (selectionStart()の位置が適切な再調査)
// // 2019/10/21 14:16:04 再検討 debug により確認
// this.edit_Loc_inf.Add( this.getCurrentTextBox2().selectionStart() + idx - 1 - text.Length );
// continue;
// }
// bl_enter_flg = false;
// }
// //
// if( false == bl_enter_flg ) this.edit_Loc_inf.Add( this.getCurrentTextBox2().selectionStart() );
// this.panel3.Refresh();
//}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// テキストペースト時の編集位置記録2 v0.98.0070
/// </summary>
/// <param name="text"></param>
private void setEditLocInfFromSelectionTextWithPaste2( int idx_start, string text )
{
//bool bl_enter_flg = false;
for ( int idx = 0; idx < text.Length - 1; idx++ )
{
this.edit_Loc_inf.Add( this.getCurrentTextBox2().selectionStart() + idx - text.Length );
}
this.panel3.Refresh();
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
///
/// </summary>
/// <param name="num_del_char"></param>
private void deleteEditLocInf( int num_del_char )
{
for ( int i = 0; i < this.edit_Loc_inf.Count; i++ )
{
// Object obj = this.edit_Loc_inf[ i ];
int char_idx = (int)this.edit_Loc_inf[ i ]; // int.Parse( obj.ToString() );
// 2019/10/21 17:43:37
// 削除開始 idx と 削除終了 idx の間にあるなら、編集位置 idx を削除。
// v0.98.0070.14
int idx_start = this.getCurrentTextBox2().selectionStart();
int idx_end = idx_start + num_del_char;
if ( (idx_start <= char_idx) && (char_idx < idx_end) ) // v0.98.0070.16
{
// v0.98.0070.14
this.edit_Loc_inf.RemoveAt( i );
if ( 0 == this.edit_Loc_inf.Count ) break;
i -= 1; // v0.98.0070.18
// if ( i < 0 ) i = -1;
continue;
}
// 削除終了 idx より大きいなら、その idx から、削除文字数分 idx を減算。
if ( idx_end < char_idx )
{
int idx_new = char_idx - num_del_char;
this.edit_Loc_inf[ i ] = idx_new; // (object)idx_new.ToString();
}
}
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// 編集位置の減算 v0.98.0070.13
/// </summary>
/// <param name="num_del_char"></param>
private void subtractEditLocInf( int num_del_char )
{
for ( int i = 0; i < this.edit_Loc_inf.Count; i++ )
{
// Object obj = this.edit_Loc_inf[ i ];
int char_idx = (int)this.edit_Loc_inf[ i ]; // int.Parse( obj.ToString() );
int idx_end = this.getCurrentTextBox2().selectionStart();
// 削除終了 idx より大きいなら、その idx から、削除文字数分 idx を減算。
if ( idx_end < char_idx )
{
int idx_new = char_idx - num_del_char;
this.edit_Loc_inf[ i ] = idx_new; // (object)idx_new.ToString();
}
}
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// 編集位置マークの描画 v0.98.0070.03
/// </summary>
/// <param name="e"></param>
private void drowEditLocationMark( PaintEventArgs e )
{
//
if ( null == this.edit_Loc_inf ) return;
if ( this.edit_Loc_inf.Count <= 0 ) return;
// パネル描を初期化。バックカラーで塗りつぶす。
//SolidBrush brush0 = new SolidBrush( this.panel3.BackColor );
//RectangleF rectf0 = new RectangleF( 0, 0, this.panel3.Width - 1, this.panel3.Height );
//e.Graphics.FillRectangle( brush0, rectf0 );
e.Graphics.Clear( this.panel3.BackColor );
//if( 0 <= this.edit_Loc_inf.IndexOf( "open" ) )
//{
// this.edit_Loc_inf.Clear();
// // this.edit_Loc_inf.Remove( "open" );
// return;
//}
// 編集位置にマークを描画
int line_loc_before = -1; // v0.98.0070.12
foreach ( Object obj in this.edit_Loc_inf )
{
// v0.98.0070.07.04
int line_loc // v0.98.0070.18
= this.getCurrentTextBox2().getLineFromCharIndex( (int)obj ); //int.Parse( obj.ToString() ) );
// v0.98.0070.18
if ( line_loc < 0 ) continue;
// v0.98.0070.12
if ( line_loc_before.Equals( line_loc ) ) continue;
line_loc_before = line_loc;
// ここで、line_loc を、パネル上のテキスト位置に変換。// v0.98.0070.07.02
int panel_height = this.getCurrentTextBox2().clientSize().Height - 60;
int text_Line_length = this.getCurrentTextBox2().getLinesLength();
// 画面の最大行数と比較
int font_height = this.getCurrentTextBox2().font().Height;
if( font_height <= 0 ) continue; // 0除算ならスキップ
// テキスト行数が、現在のテキストボックスの表示行数より小さければ
int textbox_Line_length = this.getCurrentTextBox2().clientSize().Height / font_height;
if( text_Line_length < textbox_Line_length )
{
text_Line_length = textbox_Line_length;
}
// 編集位置の表示パネルの縦幅とテキスト行数との比率から
// パネル上の表示位置を計算。
if( text_Line_length <= 0 ) continue; // 0除算ならスキップ
float f_ratio = (float)panel_height / (float)text_Line_length;
int loc = (int)( f_ratio * line_loc ) + 10;
//
RectangleF rectf = new RectangleF( 1, loc, this.panel3.Width-2, 5 );
Color customColor = Color.LightGray;
SolidBrush shadowBrush = new SolidBrush( customColor );
if ( e.Graphics.ClipBounds.Height <= rectf.Height ) continue;
// 表示
e.Graphics.FillRectangle( shadowBrush, rectf );
}
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// Panel3 のペイントハンドラ v0.98.0070.03
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Panel3_Paint( object sender, PaintEventArgs e )
{
// 編集位置マークの描画 v0.98.0070.03
this.drowEditLocationMark( e );
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// Panel3 の リサイズイベントハンドラ v0.98.0070
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Panel3_Resize( object sender, EventArgs e )
{
// v0.98.0070
this.panel3.Refresh();
}
// v0.98.0070
// private bool key_down_ctrl_v = false;
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// テキストボックスのキーダウン ハンドラ v0.98.0070
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void momeChou_TextEditBox011_KeyDown( object sender, KeyEventArgs e )
{
// v0.98.0070
if ( Keys.Enter == e.KeyCode ) this.str_inp = "\r\n";
// 反転選択のとき、Enterキーで文字列が削除されずに改行。
// v0.98.0079.01
if ( Keys.Enter == e.KeyCode ) {
if ( !this.getCurrentTextBox2().getSelectedText().Equals( "" ) ) {
//this.getCurrentTextBox().SelectedText = "";
this.getCurrentTextBox().SelectionStart += this.getCurrentTextBox().SelectionLength;
this.getCurrentTextBox().SelectionLength = 0;
}
}
// コントロールキーとVキー、つまりペーストキーが押されたとき
//if ( (Keys.Control == Control.ModifierKeys) && (Keys.V == e.KeyCode) )
//{
// // v0.98.0070
// //this.setEditLocInfFromSelectionTextWithPaste( Clipboard.GetText() );
// // this.key_down_ctrl_v = true;
//}
//else
//{
// // this.key_down_ctrl_v = false;
//}
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// 編集位置を表示 v0.98.0070.18
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 編集位置を表示ToolStripMenuItem_Click( object sender, EventArgs e )
{
if ( true == this.編集位置を表示ToolStripMenuItem.Checked )
{
this.panel3.Enabled = false;
this.panel3.Visible = false;
this.編集位置を表示ToolStripMenuItem.Checked = this.panel3.Enabled;
}
else
{
this.panel3.Enabled = true;
this.panel3.Visible = true;
this.編集位置を表示ToolStripMenuItem.Checked = this.panel3.Enabled;
}
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// 編集ロック v0.98.0087
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 編集ロックToolStripMenuItem_Click( object sender, EventArgs e )
{
if ( true == this.編集ロックToolStripMenuItem.Checked ) {
this.setEnabledObjectForLockEdit( false );
}
else {
this.setEnabledObjectForLockEdit( true );
}
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// ヘルパ:編集ロック用関連オブジェクトの非活性 v0.98.0087.02
/// </summary>
/// <param name="bl"></param>
private void setEnabledObjectForLockEdit( bool bl )
{
this.編集ロックToolStripMenuItem.Checked = bl;
this.getCurrentTextBox().Enabled = ( !bl );
//
this.置換RToolStripMenuItem.Enabled = ( !bl );
this.連続で置換えtoolStripMenuItem1.Enabled = ( !bl );
// v0.98.0087.02
this.貼り付けPToolStripMenuItem.Enabled = ( !bl );
this.日付と時刻ToolStripMenuItem.Enabled = ( !bl );
}
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// フォームクリックハンドラ
/// 編集ロックの確認メッセージを表示 v0.98.0087
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PPad1_Form1_Click( object sender, EventArgs e )
{
// Textbox.Enabled プロパティが true のとき
// TextBox を クリックすると 情報がフォームに送られ
// このハンドラが動作する。
if ( true == this.編集ロックToolStripMenuItem.Checked ) {
CS_MsgBox.warningShow(
PPad1_Message.Default.MES_WARN_LOCK_EDIT_TextBox );
}
}
}// end
//// Creates a message filter.
//[SecurityPermission( SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode )]
//public class TestMessageFilter : IMessageFilter
//{
// //public bool PreFilterMessage( ref Message m )
// //{
// // // Blocks all the messages relating to the left mouse button.
// // if ( m.Msg >= 513 && m.Msg <= 515 )
// // {
// // Console.WriteLine( "Processing the messages : " + m.Msg );
// // return true;
// // }
// // return false;
// //}
// private int WM_PASTE = 0x0302;
// public bool PreFilterMessage( ref Message m )
// {
// // Blocks all the messages relating to the left mouse button.
// // debug
// // Console.WriteLine( "Processing the messages : " + m.Msg );
// if ( this.WM_PASTE == m.Msg )
// {
// // debug
// Console.WriteLine( "Processing the messages : " + m.Msg );
// return true;
// }
// return false;
// }
//}
}
// #define _DEBUG
// PPad1_Form1_SaveFileHistory.cs
using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
//using System.Drawing;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
//add
//using System.Reflection;
using System.IO;
//
using System.Diagnostics;
using System.Collections;
using System.Threading;
//using System.Windows.Threading;
//using System.Runtime.InteropServices;
namespace PeacePadEditor01
{
partial class PPad1_Form1
{
/// <summary>
/// 未使用:タイマートライアル
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick( object sender, EventArgs e )
{
Debug.WriteLine( DateTime.Now + ": timer1_Tick()" );
Debug.WriteLine( DateTime.Now.ToString( "_yyyy-MM-dd_HH:mm:ss" ) + ": timer1_Tick()" );
// this.timer1.Interval = 3000;
this.timer1.Stop();
//Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
return;
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// 未使用:ファイル履歴保存のタイマー環境を設定
/// </summary>
private void setupAndStartTimerForSaveingFileHistory()
{
// 履歴ファイル保存メニューがチェックされているか?
if ( false == this.ファイル履歴を保存toolStripMenuItem1.Checked ) {
// タイマー停止
this.timer1.Stop();
return;
}
// ファイル名が確定しているか?
string file_name = this.getCurrentFileName();
if ( true == file_name.Equals( "" ) ) {
// タイマー停止
this.timer1.Stop();
return;
}
// タイマー開始
this.timer1.Interval = 1000; // 1秒に1度動作。
this.timer1.Start();
return;
}
/// <summary>
/// 調査用
/// </summary>
private void tmp()
{
Path.GetDirectoryName( "tmp" );
//string str_tmp = "abc";
//int i_hash = str_tmp.GetHashCode();
//byte b_data = 0;
DateTime dt = File.GetLastWriteTime( "./tmp.txt" );
// "_YYYY-MM-DD_hh:mm:ss"
// FileAttributes fa = File.GetAttributes( "./tmp.txt" );
//FileInfo finf = new FileInfo( "./tmp.txt" );
//finf.Length
// Path.GetDirectoryName
// Path.GetFileName
// Path.GetExtension();
// Path.GetFileNameWithoutExtension
//Application.
// string my_doc_folder = Environment.GetFolderPath( Environment.SpecialFolder.MyDocuments );
//string tmp = Path.GetDirectoryName();
string tmp = "c:\\";
tmp.Replace( ":", ":" );
// File.Copy()
// tmp.Replace()
string file_path = "c:\\tmp\\test.txt";
FileInfo fi = new FileInfo( file_path );
long file_size = fi.Length;
FileStream fs = File.OpenRead( file_path );
byte[] b = new byte[ file_size ];
fs.Read( b, 0, b.Length );
int hash = b.GetHashCode();
fs.Close();
Debug.WriteLine( "hash=" + hash.ToString() );
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// 「ファイル履歴を保存」メニューをクリック
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ファイル履歴を保存toolStripMenuItem1_Click( object sender, EventArgs e )
{
//if ( false == this.ファイル履歴を保存toolStripMenuItem1.Checked ) return;
// ファイル履歴保存、タイマー処理を起動 v0.98.0090.02
// this.setupAndStartTimerForSaveingFileHistory();
// 設定ファイルへの値の設定 v0.98.0090.02
Properties.Settings.Default.bl_file_history_checked
= this.ファイル履歴を保存toolStripMenuItem1.Checked;
Properties.Settings.Default.Save();
// ファイル履歴のチェックと作成 v0.98.0090.02
this.chkAndMakeFileHistory();
return;
}
// ---------------------------------------------------------------
// 履歴フォルダクラス
private PPad1_FileHistory ppad1_FileHistory = null;
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// 履歴フォルダの確認と作成(ラッパー)
/// </summary>
private bool chkAndMakeFileHistory()
{
if ( false == this.ファイル履歴を保存toolStripMenuItem1.Checked ) return false;
if ( null == this.ppad1_FileHistory ) {
ppad1_FileHistory = new PPad1_FileHistory( this );
}
ppad1_FileHistory.chkNewAndSaveHistoryFile();
return true;
}
}// PPad1_Form1
/////////////////////////////////////
// public class PPad1_FileHistory
/////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// ファイル履歴クラス
/// </summary>
public class PPad1_FileHistory
{
// 履歴ファイルフォルダ名の定義など
private const string FILE_HISTORY_FOLDER_NAME = "PeacePad_FileHistory";
// 履歴フォルダの最上位のフルパス
private readonly string FILE_HISTORY_FOLDER_PATH_TOP
= Environment.GetFolderPath( Environment.SpecialFolder.MyDocuments )
+ "\\" + FILE_HISTORY_FOLDER_NAME;
private const string FILE_NAME_SEPARATOR = "_;;";
private const string FILE_NAME_DATE_FMT
= FILE_NAME_SEPARATOR + "yyyy-MM-dd_HH:mm:ss";
// 現行ファイルに対応するフォルダパス
private string current_file_history_folder_path = "";
// トップフォームのプロパティ
public PPad1_Form1 form1 = null;
private PPad1_Form1 Form1
{
get { return this.form1; }
set { this.form1 = value; }
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// ファイル履歴クラスのコンストラクタ
/// </summary>
/// <param name="tmp_form1"></param>
public PPad1_FileHistory( PPad1_Form1 tmp_form1 )
{
this.Form1 = tmp_form1;
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// ログ
/// </summary>
/// <returns></returns>
private IPPad1_Log01 ppad_Log1()
{
{ return this.Form1.ppad_Log1; }
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// メインロジック:現行ファイルを履歴ファイルとして保存
/// </summary>
public void chkNewAndSaveHistoryFile()
{
// カレントファイル名の確認
string file_name = this.Form1.getCurrentFileName();
if ( true == file_name.Equals( "" ) ) return;
// PeacePad の ログファイルならスキップ
string log_file_name = this.ppad_Log1().getLogFileName();
string file_name_only = Path.GetFileName( file_name );
if ( log_file_name.Equals( file_name_only ) ) return;
// 履歴フォルダの確認と作成
if ( false == this.chkAndMakeFileHistoryFolder2() ) return;
// 最新履歴ファイル取得
string recent_history_file_path = this.getRecentHistoryFileName( file_name );
// 現在ファイルと最新履歴ファイルの比較
if ( this.compFileForCurrentAndRecentHistory2( recent_history_file_path ) ) {
// 同一ファイルなら保存の必要なし
return;
}
// 現行ファイルを履歴ファイルに保存
// 更新日付時刻から、ファイル名の後端を生成
string str_now_date = DateTime.Now.ToString( FILE_NAME_DATE_FMT );
// コピー先ファイル名
string new_history_file_path
= this.current_file_history_folder_path + "\\"
+ Path.GetFileNameWithoutExtension( file_name )
+ str_now_date + Path.GetExtension( file_name );
// ファイルをコピー、上書き無し
File.Copy( file_name, new_history_file_path );
// ファイルの readonly チェックON
File.SetAttributes( new_history_file_path,
File.GetAttributes( new_history_file_path ) | FileAttributes.ReadOnly );
this.ppad_Log1().outLog05(
"履歴ファイルをコピー:" + PPad1_Func.getShortenFilePath2( new_history_file_path ) );
return;
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// 履歴フォルダの確認と作成2
/// </summary>
private bool chkAndMakeFileHistoryFolder2()
{
// 履歴フォルダのトップフォルダ
// ドキュメントフォルダ下の確認と生成
if ( false == Directory.Exists( this.FILE_HISTORY_FOLDER_PATH_TOP ) ) {
Directory.CreateDirectory( this.FILE_HISTORY_FOLDER_PATH_TOP );
this.ppad_Log1().outLog05(
"履歴TOPフォルダを作成:"
+ PPad1_Func.getShortenFilePath2( this.FILE_HISTORY_FOLDER_PATH_TOP ) );
}
// 現行ファイルパスから、履歴フォルダ名を作り確認。無ければ作成。
string current_file_path = this.Form1.getCurrentFileName();
if ( true == current_file_path.Equals( "" ) ) return false;
// 現行ファイルパスから、部分パス文字列を作る。
string current_path = Path.GetDirectoryName( current_file_path );
current_path = current_path.Replace( ":", ":" ); // ドライブコロンを全角に。
// 履歴フォルダパスは、this.FILE_HISTORY_FOLDER_PATHに、現行ファイルパスを。
string history_folder_path
= this.FILE_HISTORY_FOLDER_PATH_TOP + "\\" + current_path;
// 現行ファイルに対応するフォルダの確認と生成
if ( false == Directory.Exists( history_folder_path ) ) {
Directory.CreateDirectory( history_folder_path );
this.ppad_Log1().outLog05(
"履歴フォルダを作成:"
+ PPad1_Func.getShortenFilePath2( history_folder_path ) );
}
// 現行ファイルに対応するフォルダパス
this.current_file_history_folder_path = history_folder_path;
return true;
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// 日付時刻の最新の履歴ファイル
/// </summary>
/// <param name="file_name"></param>
/// <returns></returns>
private string getRecentHistoryFileName( string file_name )
{
// ファイル名が確定しているか?
if ( true == file_name.Equals( "" ) ) return "";
// カレントフォルダパス、ファイル名、拡張子
// string path_file_folder = Path.GetDirectoryName( file_name );
string only_file_name = Path.GetFileNameWithoutExtension( file_name );
string file_ext = Path.GetExtension( file_name );
// 履歴ファイル名は、現行ファイル名_日付_時刻.拡張子
string search_file_str = only_file_name
+ FILE_NAME_SEPARATOR +"*" + file_ext;
// -------------------- by sample
var txtFiles = Directory.EnumerateFiles(
this.current_file_history_folder_path, // クラスメンバ
search_file_str );
// 2021/03/12 14:32:43
int count = 0;
ArrayList files = new ArrayList();
foreach ( string currentFile in txtFiles ) {
// string fileName = currentFile.Substring( sourceDirectory.Length + 1);
// Directory.Move(currentFile, Path.Combine(archiveDirectory, fileName));
files.Add( currentFile );
count++;
}
if ( count < 1 ) return ""; // 無い
files.Sort(); // 並び替え
string recent_file = files[ files.Count - 1 ].ToString();
// さらに、規定本数を超えていたら削除できるか?
int num_history_file = files.Count;
int num_del_file = num_history_file
- Properties.Settings.Default.num_max_history_file;
if ( 0 < num_del_file ) {
for ( int i = 0; i < num_del_file - 1; i++ ) {
string del_file_name = files[ i ].ToString();
// 読み取り専用を解除
File.SetAttributes( del_file_name,
File.GetAttributes( del_file_name ) & ( ~FileAttributes.ReadOnly ) );
// 削除
File.Delete( del_file_name );
this.ppad_Log1().outLog05(
"履歴ファイルを削除:"
+ PPad1_Func.getShortenFilePath2( del_file_name ) );
}
}
return recent_file;
}
//////////////////////////////////////////////////////////////////////////////
/// <summary>
/// 現在のファイルと履歴ファイルを比較
/// </summary>
/// <param name="recent_history_file_path"></param>
/// <returns></returns>
private bool compFileForCurrentAndRecentHistory2( string recent_history_file_path )
{
//
if ( recent_history_file_path.Equals( "" ) ) return false;
// 現在のファイル
string current_file_path = this.Form1.getCurrentFileName();
if ( current_file_path.Equals( "" ) ) return false;
// ファイル名の比較
// ファイル名の拡張子無しで比較。現行ファイル名 が履歴ファイル名の部分文字列のはず。
// しかし、完全ではない
string current_file_name = Path.GetFileNameWithoutExtension( current_file_path );
string recent_history_file_name = Path.GetFileNameWithoutExtension( recent_history_file_path );
int idx = recent_history_file_name.IndexOf( current_file_name, 0 );
if ( 0 < idx ) return false; // ファイル名の先頭部分が異なる
string file_path1 = recent_history_file_path; // "c:\\tmp\\ANSI_テスト30万行1.txt";
FileInfo fi1 = new FileInfo( file_path1 );
long file_size1 = fi1.Length;
Debug.WriteLine( "file_path=" + file_path1 );
Debug.WriteLine( "file_size1=" + file_size1.ToString() );
string file_path2 = current_file_path; // "c:\\tmp\\ANSI_テスト30万行2.txt";
FileInfo fi2 = new FileInfo( file_path2 );
long file_size2 = fi2.Length;
Debug.WriteLine( "file_path2=" + file_path2 );
Debug.WriteLine( "file_size2=" + file_size2.ToString() );
// ここでサイズなどを比較して同じならファイルのバイト比較に進むようにする
if ( file_size1 != file_size2 ) return false;
using ( FileStream fs1 = File.OpenRead( file_path1 ) ) {
using ( FileStream fs2 = File.OpenRead( file_path2 ) ) {
int buf_size = 4096;
byte[] buf1 = new byte[ buf_size ];
//buf1.Initialize();
byte[] buf2 = new byte[ buf1.Length ];
//buf2.Initialize();
int iret = 0;
while ( 0 <= iret ) {
//buf1.Initialize();
//buf2.Initialize();
// init
for ( int i = 0; i < buf1.Length; i++ ) {
buf1[ i ] = 0; buf2[ i ] = 0;
}
try {
iret = fs1.Read( buf1, 0, buf1.Length );
if ( iret <= 0 ) break;
int iret2 = 0;
iret2 = fs2.Read( buf2, 0, buf2.Length );
if ( iret2 <= 0 ) break;
}
catch ( Exception ex ) {
Debug.WriteLine( ex.StackTrace.ToString() );
new Exception( ex.Message, ex.InnerException );
break;
}
for ( int i = 0; i <= iret - 1; i++ ) {
if ( buf1[ i ] == buf2[ i ] ) {
continue;
}
else {
return false;
}
}
}// while
} // using2
}// using1
return true;
}
}// class PPad1_FileHistory
}