From 4f7d148f06a15091f4410368756efa3c9cc18338 Mon Sep 17 00:00:00 2001
From: Tu Dinh <1257909+dinhngtu@users.noreply.github.com>
Date: Mon, 15 Jan 2024 22:54:03 +0100
Subject: [PATCH] config: add default ime override feature
---
VietTypeConfig/Form1.cs | 17 +++++++++++++----
VietTypeConfig/Form1.en.resx | 3 +++
VietTypeConfig/Form1.vi.resx | 3 +++
VietTypeConfig/NativeMethods.cs | 5 +++++
VietTypeConfig/Program.cs | 4 ++--
VietTypeConfig/Settings.cs | 7 +++++++
6 files changed, 33 insertions(+), 6 deletions(-)
diff --git a/VietTypeConfig/Form1.cs b/VietTypeConfig/Form1.cs
index fc5173d..41f5d5d 100644
--- a/VietTypeConfig/Form1.cs
+++ b/VietTypeConfig/Form1.cs
@@ -3,17 +3,19 @@
using System;
using System.Resources;
-using System.Runtime;
using System.Runtime.InteropServices;
using System.Windows.Forms;
-using VietTypeConfig.Properties;
using static VietTypeConfig.VietTypeRegistrar;
namespace VietTypeConfig {
public partial class Form1 : Form {
private readonly ResourceManager rm = new ResourceManager(typeof(Form1));
+ private bool oobe = false;
- public Form1() {
+ public Form1(string[] args) {
+ if (args.Length > 0) {
+ oobe = string.Equals(args[0], "-oobe", StringComparison.InvariantCultureIgnoreCase);
+ }
InitializeComponent();
udOptimizeMultilang.MouseWheel += (sender, e) => ((HandledMouseEventArgs)e).Handled = true;
}
@@ -64,6 +66,13 @@ private void Activate(bool newState) {
var result = newState ? ActivateProfiles() : DeactivateProfiles();
if (result == S_OK) {
if (newState) {
+ if (MessageBox.Show(rm.GetString("setDefaultQuestion"), "VietType", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
+ try {
+ Settings.SetDefault();
+ } catch (Exception ex) {
+ MessageBox.Show(string.Format(rm.GetString("cannotSaveSettingsMessage"), ex.Message), "VietType", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
+ }
MessageBox.Show(rm.GetString("enableSuccessMessage"), "VietType", MessageBoxButtons.OK, MessageBoxIcon.Information);
} else {
MessageBox.Show(rm.GetString("disableSuccessMessage"), "VietType", MessageBoxButtons.OK, MessageBoxIcon.Information);
@@ -77,7 +86,7 @@ private void Activate(bool newState) {
private void Form1_Shown(object sender, EventArgs e) {
if (IsProfileActivated() != S_OK) {
- if (MessageBox.Show(rm.GetString("notEnabledQuestion"), "VietType", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
+ if (oobe || MessageBox.Show(rm.GetString("notEnabledQuestion"), "VietType", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
Activate(true);
}
}
diff --git a/VietTypeConfig/Form1.en.resx b/VietTypeConfig/Form1.en.resx
index 34b5586..8ec874e 100644
--- a/VietTypeConfig/Form1.en.resx
+++ b/VietTypeConfig/Form1.en.resx
@@ -179,4 +179,7 @@ VietType is free software, licensed under the GNU General Public License. VietTy
VietType is not enabled. Enable it now?
+
+ Set VietType as default input method?
+
\ No newline at end of file
diff --git a/VietTypeConfig/Form1.vi.resx b/VietTypeConfig/Form1.vi.resx
index 4ccee49..9ac7b01 100644
--- a/VietTypeConfig/Form1.vi.resx
+++ b/VietTypeConfig/Form1.vi.resx
@@ -183,4 +183,7 @@ VietType là phần mềm tự do, bạn có thể cung cấp lại và/hoặc c
Mức tối ưu gõ nhiều ngôn ngữ (&O):
+
+ Đặt VietType làm bộ gõ mặc định?
+
\ No newline at end of file
diff --git a/VietTypeConfig/NativeMethods.cs b/VietTypeConfig/NativeMethods.cs
index 77e899c..349d00d 100644
--- a/VietTypeConfig/NativeMethods.cs
+++ b/VietTypeConfig/NativeMethods.cs
@@ -52,4 +52,9 @@ internal static class VietTypeRegistrar {
[DllImport("VietTypeATL32.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int IsProfileActivated();
}
+
+ internal static class NativeMethods {
+ [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
+ public static extern int SetInputMethodOverride(string TipString);
+ }
}
diff --git a/VietTypeConfig/Program.cs b/VietTypeConfig/Program.cs
index 92f085e..b40820f 100644
--- a/VietTypeConfig/Program.cs
+++ b/VietTypeConfig/Program.cs
@@ -12,10 +12,10 @@ internal static class Program {
/// The main entry point for the application.
///
[STAThread]
- static void Main() {
+ static void Main(string[] args) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new Form1());
+ Application.Run(new Form1(args));
}
}
}
diff --git a/VietTypeConfig/Settings.cs b/VietTypeConfig/Settings.cs
index de5cf71..115f1f4 100644
--- a/VietTypeConfig/Settings.cs
+++ b/VietTypeConfig/Settings.cs
@@ -10,6 +10,9 @@ internal class Settings : INotifyPropertyChanged {
private const string Subkey = "Software\\VietType";
private static readonly Guid GUID_SystemNotifyCompartment = Guid.Parse("{B2FBD2E7-922F-4996-BE77-21085B91A8F0}");
private static readonly Guid CLSID_TF_ThreadMgr = Guid.Parse("{529a9e6b-6587-4f23-ab9e-9c7d683e3c50}");
+ private static readonly Guid CLSID_TextService = Guid.Parse("{c0dd01a1-0deb-454b-8b42-d22ced1b4b23}");
+ private static readonly Guid GUID_Profile = Guid.Parse("{8D93D10A-203B-4C5F-A122-8898EF9C56F5}");
+ private const ushort TextServiceLangId = 0x42a;
bool default_enabled = false;
public bool DefaultEnabled {
@@ -147,5 +150,9 @@ public static void SaveSettings(Settings settings) {
}
}
}
+
+ public static void SetDefault() {
+ NativeMethods.SetInputMethodOverride(string.Format("{0:X4}:{{{1}}}{{{2}}}", TextServiceLangId, CLSID_TextService.ToString(), GUID_Profile.ToString()));
+ }
}
}