InterThread.cs
上传用户:szltgg
上传日期:2019-05-16
资源大小:604k
文件大小:7k
- /*
- * Copyright (c) 2005 Poderosa Project, All Rights Reserved.
- * $Id: InterThread.cs,v 1.2 2005/04/20 08:45:46 okajima Exp $
- */
- using System;
- using System.Threading;
- using System.Windows.Forms;
- using Poderosa.Connection;
- using Poderosa.Communication;
- using Poderosa.ConnectionParam;
- using Poderosa.Text;
- using Poderosa.Config;
- using Poderosa.Forms;
- namespace Poderosa
- {
- //庴怣僗儗僢僪偑捈愙儊僢僙乕僕儃僢僋僗側偳傪弌偟偵偄偔偺偼壗偐偲僩儔僽儖偺壏彴側偺偱丄
- //儊僀儞偺僗儗僢僪偲庴偗搉偟傪偟側偑傜張棟偡傞丅
- //偨偲偊偽丄僆僾僔儑儞僟僀傾儘僌傪弌偟偰偄傞娫偵僒乕僶偑愗抐傪愗偭偰偒偨偲偒側偳丄GFrame傪恊偵MessageBox傪屇傫偱偟傑偄丄偦偺屻偺摦嶌偑偪傚偭偲晄怰偵側偭偨丅
- public class InterThreadUIService {
- private string _msg;
- private Exception _exception;
- private DialogResult _dialogResult;
- private TerminalConnection _connection;
- protected IntPtr _mainFrameHandle;
- public enum Service {
- Warning,
- Information,
- AskUserYesNo,
- DisconnectedFromServer,
- ToggleSelectionMode,
- IndicateBell,
- CriticalError,
- InvalidCharDetected,
- UnsupportedCharSetDetected,
- UnsupportedEscapeSequence,
- InvalidDocumentOperation,
- RefreshConnection,
- End
- }
- public IntPtr MainFrameHandle {
- get {
- return _mainFrameHandle;
- }
- set {
- _mainFrameHandle = value;
- }
- }
- //偙傟傜偺奺儊僜僢僪偼偦傟偧傟儘僢僋偟側偑傜幚峴偝傟傞
- internal void Warning(TerminalDocument doc, string msg) {
- Monitor.Exit(doc);
- lock(this) {
- _msg = msg;
- SendMessageCore(Service.Warning);
- }
- Monitor.Enter(doc);
- }
- public void Warning(string msg) {
- lock(this) {
- _msg = msg;
- SendMessageCore(Service.Warning);
- }
- }
- public void Information(string msg) {
- lock(this) {
- _msg = msg;
- SendMessageCore(Service.Information);
- }
- }
- public DialogResult AskUserYesNo(string msg) {
- lock(this) {
- _msg = msg;
- SendMessageCore(Service.AskUserYesNo);
- }
- return _dialogResult;
- }
- internal void ReportCriticalError(TerminalDocument doc, Exception ex) {
- Monitor.Exit(doc);
- lock(this) {
- _exception = ex;
- SendMessageCore(Service.CriticalError);
- }
- Monitor.Enter(doc);
- }
- public void ReportCriticalError(Exception ex) {
- lock(this) {
- _exception = ex;
- SendMessageCore(Service.CriticalError);
- }
- }
- public void DisconnectedFromServer(TerminalConnection con) {
- lock(this) {
- _connection = con;
- SendMessageCore(Service.DisconnectedFromServer);
- }
- }
- internal void IndicateBell(TerminalDocument doc) {
- Monitor.Exit(doc);
- lock(this) {
- SendMessageCore(Service.IndicateBell);
- }
- Monitor.Enter(doc);
- }
- internal void InvalidCharDetected(TerminalDocument doc, string msg) {
- Monitor.Exit(doc);
- lock(this) {
- _msg = msg;
- SendMessageCore(Service.InvalidCharDetected);
- }
- Monitor.Enter(doc);
- }
- internal void UnsupportedCharSetDetected(TerminalDocument doc, string msg) {
- Monitor.Exit(doc);
- lock(this) {
- _msg = msg;
- SendMessageCore(Service.UnsupportedCharSetDetected);
- }
- Monitor.Enter(doc);
- }
- internal void UnsupportedEscapeSequence(TerminalDocument doc, string msg) {
- Monitor.Exit(doc);
- lock(this) {
- _msg = msg;
- SendMessageCore(Service.UnsupportedEscapeSequence);
- }
- Monitor.Enter(doc);
- }
- internal void InvalidDocumentOperation(TerminalDocument doc, string msg) {
- Monitor.Exit(doc);
- lock(this) {
- _msg = msg;
- SendMessageCore(Service.InvalidDocumentOperation);
- }
- Monitor.Enter(doc);
- }
- public void RefreshConnection(ConnectionTag tag) {
- TerminalDocument doc = tag.Document;
- Monitor.Exit(doc);
- lock(this) {
- _connection = tag.Connection;
- SendMessageCore(Service.RefreshConnection);
- }
- Monitor.Enter(doc);
- }
- protected void SendMessageCore(Service svc) {
- Win32.SendMessage(_mainFrameHandle, GConst.WMG_UIMESSAGE, new IntPtr((int)svc), IntPtr.Zero);
- }
- internal void AdjustIMEComposition(IntPtr hWnd, TerminalDocument doc) {
- Monitor.Exit(doc);
- lock(this) {
- Win32.SendMessage(hWnd, GConst.WMG_MAINTHREADTASK, new IntPtr(GConst.WPG_ADJUSTIMECOMPOSITION), IntPtr.Zero);
- }
- Monitor.Enter(doc);
- }
- internal void ToggleTextSelectionMode(IntPtr hWnd, TerminalDocument doc) {
- Monitor.Exit(doc);
- lock(this) {
- Win32.SendMessage(hWnd, GConst.WMG_MAINTHREADTASK, new IntPtr(GConst.WPG_TOGGLESELECTIONMODE), IntPtr.Zero);
- }
- Monitor.Enter(doc);
- }
- //儊僀儞僗儗僢僪偐傜幚峴
- public virtual void ProcessMessage(IntPtr wParam, IntPtr lParam) {
- switch((Service)(int)wParam) {
- case Service.Warning:
- GUtil.Warning(GEnv.Frame, _msg);
- break;
- case Service.Information:
- GUtil.Warning(GEnv.Frame, _msg, MessageBoxIcon.Information);
- break;
- case Service.AskUserYesNo:
- _dialogResult = GUtil.AskUserYesNo(GEnv.Frame, _msg);
- break;
- case Service.DisconnectedFromServer:
- DisconnectedFromServer();
- break;
- case Service.IndicateBell:
- GEnv.Frame.IndicateBell();
- break;
- case Service.CriticalError:
- GUtil.ReportCriticalError(_exception);
- break;
- case Service.InvalidCharDetected: //偄傑偼偙偺俁偮偼儊僢僙乕僕僥僉僗僩偱偟偐嬫暿偟側偄
- case Service.UnsupportedCharSetDetected:
- case Service.UnsupportedEscapeSequence:
- case Service.InvalidDocumentOperation:
- BadCharDetected();
- break;
- case Service.RefreshConnection:
- GEnv.Frame.RefreshConnection(GEnv.Connections.FindTag(_connection));
- break;
- }
- }
- private void DisconnectedFromServer() {
- /*
- if(!_connection.IsClosed && (_connection.Param is TCPTerminalParam)) {
- string host = ((TCPTerminalParam)_connection.Param).Host;
- string msg = String.Format(GEnv.Strings.GetString("Message.InterThread.Disconnected"), host);
- if(GEnv.Options.DisconnectNotification==DisconnectNotification.MessageBox)
- GUtil.Warning(GEnv.Frame, msg);
- else if(GEnv.Options.DisconnectNotification==DisconnectNotification.StatusBar)
- GEnv.Frame.SetStatusBarText(msg);
- }
- * */
- _connection.Close();
- _connection.WriteChars("disconnected".ToCharArray());
- /*
- * GEnv.Connections.FindTag(_connection).IsTerminated = true;
- if(GEnv.Options.CloseOnDisconnect)
- GEnv.GetConnectionCommandTarget(_connection).Close();
- else
- GEnv.Frame.RefreshConnection(GEnv.Connections.FindTag(_connection));
- if(GEnv.Options.QuitAppWithLastPane && GEnv.Connections.Count==0)
- GEnv.Frame.AsForm().Close();
- */
-
- }
- private void BadCharDetected() {
- switch(GEnv.Options.WarningOption) {
- case WarningOption.StatusBar:
- GEnv.Frame.SetStatusBarText(_msg);
- break;
- case WarningOption.MessageBox: {
- WarningWithDisableOption dlg = new WarningWithDisableOption(_msg);
- if(GUtil.ShowModalDialog(GEnv.Frame, dlg)==DialogResult.OK && dlg.CheckedDisableOption)
- GEnv.Options.WarningOption = WarningOption.Ignore;
- break;
- }
- }
- }
- }
- }