Exemplo n.º 1
0
        public WordCompletionForm(WindowListener windowListener)
        {
            InitializeComponent();

            this.lstItems.Font = new System.Drawing.Font(this.lstItems.Font.FontFamily, FontSize);
            this.Enabled       = GlobalEnabled;

            if (windowListener == null)
            {
                throw new ArgumentNullException("windowListener");
            }

            this.windowListener = windowListener;

            windowListener.AddKeyboardIgnoreCode(33); // PgUp
            windowListener.AddKeyboardIgnoreCode(34); // PgDn
            windowListener.AddKeyboardIgnoreCode(38); // Up
            windowListener.AddKeyboardIgnoreCode(40); // Dn
            windowListener.AddKeyboardIgnoreCode(13); // Enter
            windowListener.AddKeyboardIgnoreCode(9);  // tab
            //windowListener.AddKeyboardIgnoreCode(32); // space

            this.windowListener.KeyDown          += new WindowListener.WinMessageEventHandler(windowListener_KeyDown);
            this.windowListener.KeyPressed       += new WindowListener.WinMessageEventHandler(windowListener_KeyPressed);
            this.windowListener.LostFocus        += new WindowListener.WinMessageEventHandler(windowListener_LostFocus);
            this.windowListener.PositionChanging += new WindowListener.WinMessageEventHandler(windowListener_PositionChanging);
            this.windowListener.Paint            += new WindowListener.WinMessageEventHandler(windowListener_Paint);
        }
Exemplo n.º 2
0
        public static void WordCompletionEventHandler(object sender, EventArgs e)
        {
            // get the window handle which has the focus
            IntPtr focusedHandle = User32.GetFocus();

            // if it's needed to create a new instance of window-listener
            // i.e. if it's null or it is listening to another window
            if (s_windowListenerInstance == null || s_windowListenerInstance.Handle != focusedHandle)
            {
                // dispose the window-listener which is listening to another window
                if (s_windowListenerInstance != null)
                {
                    s_windowListenerInstance.Dispose();
                }

                s_windowListenerInstance = new WindowListener();
                s_windowListenerInstance.SetWindowHandle(focusedHandle);
            }

            if (s_frmInstance == null)
            {
                s_frmInstance = new WordCompletionForm(s_windowListenerInstance);
            }
            else if (s_frmInstance.windowListener.Handle != s_windowListenerInstance.Handle)
            {
                s_frmInstance.Dispose();
                s_frmInstance = new WordCompletionForm(s_windowListenerInstance);
            }

            Microsoft.Office.Interop.Word.Application wordApp = Globals.ThisAddIn.Application;

            try
            {
                string text;
                string selectedSug = null;
                Range  r           = RangeUtils.GetWordBeforeCursor(wordApp.Selection);
                if (!RangeUtils.IsRangeEmpty(r))
                {
                    string originalText = r.Text;
                    text = StringUtil.RefineAndFilterPersianWord(originalText);

                    string[] sugs = GetWordCompletionSuggestion(text);
                    if (sugs.Length <= 0)
                    {
                        selectedSug = null;
                    }
                    else if (sugs.Length == 1)
                    {
                        selectedSug = sugs[0];
                    }
                    else
                    {
                        s_frmInstance.SetItems(originalText, sugs);
                        s_frmInstance.ShowAtCaret();
                    }

                    if (selectedSug != null)
                    {
                        ChangeRangeForWordCompletion(r, selectedSug);
                    }
                }
                else // if range is empty
                {
                    LogHelper.Debug("Range is empty");
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorException("Error in word completion event handler", ex);
            }
        }