private void OpenDocument(word.Application application) { word.Selection selection = application.Selection; if (selection == null) { MessageBox.Show(@"Selection is null"); return; } switch (selection.Type) { case word.WdSelectionType.wdSelectionNormal: case word.WdSelectionType.wdSelectionIP: { var range = _document.Range(); range.Select(); richTextBox_Document.Text = range.Text; TextParseManager.FullWordText = range.Text; toolStripStatusLabel_Главы.Text = @"Главы: " + WordHelpers.FindChapters(_document); var array = WordHelpers.SplitByChapters(TextParseManager.FullWordText); break; } default: MessageBox.Show(@"Неподдерживаемый тип Selection"); break; } _document.RemoveDocumentInformation(word.WdRemoveDocInfoType.wdRDIAll); application.Documents.Save(NoPrompt: true, OriginalFormat: true); }
private void HighlightUsages_CheckedChanged(object sender, EventArgs e) { WordHelpers.RunActionWithWaitCursor(() => { ResetTheCounters(); var isFieldChecked = HighlightAllUsages.Checked; if (isFieldChecked) { if (_corectDefinitionOccurences.Count != 0) { HighLightCorrectUsages(); NextDefinition.Enabled = true; } else { if (HighlightIncorrectUsages.Checked && _incorrectDefinitionOccurences.Count != 0) { NextDefinition.Enabled = true; } } return; } if (!HighlightIncorrectUsages.Checked) { NextDefinition.Enabled = false; PreviousDefiniton.Enabled = false; } DocumentHelpers.ClearHightlightOnAllInstancesOfTerm(_corectDefinitionOccurences); }); }
private void DefinitionBox_FormClosing(object sender, FormClosingEventArgs e) { ThisAddIn.DecreaseDialogCoords(); WordHelpers.RunActionWithWaitCursor(() => { if (HighlightAllUsages.Checked) { if (_corectDefinitionOccurences.Count != 0) { DocumentHelpers.ClearHightlightOnAllInstancesOfTerm(_corectDefinitionOccurences); } } if (HighlightIncorrectUsages.Checked) { if (_incorrectDefinitionOccurences.Count != 0) { DocumentHelpers.ClearHightlightOnAllInstancesOfTerm(_incorrectDefinitionOccurences); } } if (_currentPosition != null) { Navigate.ToRange(_currentPosition); } }); }
private void ShowSubDefinition_Click(object sender, EventArgs e) { WordHelpers.RunActionWithWaitCursor(() => { var selection = StringExtensions.RefineSelectionText(richTextBoxEx1.SelectedText.Trim()); var newDefinition = Globals.ThisAddIn.AnalyzersByDocument[Globals.ThisAddIn.Application.ActiveDocument.FullName].GetWordDefinition(selection); if (newDefinition == null) { MessageBox.Show(new Form() { TopMost = true }, @"Definition could not be found"); return; } if (newDefinition.Locations.Count > 1) { var listOfDefinitions = new ListOfDefinitions(newDefinition, Globals.ThisAddIn.Application.Selection.Range, Globals.ThisAddIn.Application.ActiveDocument.FullName); listOfDefinitions.Show(); } else { var definitionBox = new DefinitionBox { Definition = newDefinition, SourceFile = Globals.ThisAddIn.Application.ActiveDocument.FullName, _currentPosition = Globals.ThisAddIn.Application.Selection.Range }; definitionBox.Show(); } }); LogHelper.Info("Nested definiton shown."); }
private void ОткрытьToolStripMenuItem_Click(object sender, EventArgs e) { var openFileDialog = new OpenFileDialog { Filter = @"Файлы word|*.docx;*.doc" }; if (openFileDialog.ShowDialog() != DialogResult.OK) { return; } Form1_FormClosing(sender, null); _application = new word.Application(); try { _document = WordHelpers.IsWordFileOpened() ? _application.Documents.OpenOld(openFileDialog.FileName, ReadOnly: false) : _application.Documents.Open(openFileDialog.FileName, ReadOnly: false, Visible: false); toolStripStatusLabel_OpenedFileName.Text = @"Файл: " + _document.Name; OpenDocument(_application); } catch (Exception ex) { MessageBox.Show(@"Ошибка при открытии документа. " + ex.Message); toolStripStatusLabel_OpenedFileName.Text = @"Файл: -error-"; } }
public static IEnumerable <IQuestion> GetAllPossibleQuestions(IReadOnlyList <IQuestion> questions) { return (from addressedTo in PositionHelpers.AllPositions() from question in questions from answer in WordHelpers.AllWords() select new IsAnswerQuestion(addressedTo, question, answer)); }
public void ToHexAndDecimal_Can_Format_Output_Correctly() { // Arrange // Act var output = WordHelpers.ToHexAndDecimal(0x2142); // Assert Assert.Equal("0x2142 (8514)", output); }
public void ToDecimalAndHex_Can_Format_Output_Correctly() { // Arrange // Act var output = WordHelpers.ToDecimalAndHex(0x2142); // Assert Assert.Equal("8514 (0x2142)", output); }
private void ReportBtn_Click(object sender, RibbonControlEventArgs e) { LogHelper.Info("Report generation requested"); WordHelpers.RunActionWithWaitCursor(() => { new ReportBox().Show(); }); LogHelper.Info("Report generation complete"); }
private void richTextBoxEx1_LinkClicked(object sender, LinkClickedEventArgs e) { WordHelpers.RunActionWithWaitCursor(() => { var link = e.LinkText; var text = link; var match = Regex.Match(text, ReferencePattern); if (match.Groups.Count < 4) { MessageBox.Show(new Form() { TopMost = true }, @"Such Clause or Schedule was not found"); return; } var title = match.Groups[3].Value; var listFormatValue = match.Groups[2].Value.Trim(); listFormatValue = Regex.Replace(listFormatValue, @"\p{C}+", string.Empty); Document document = Globals.ThisAddIn.Application.ActiveDocument; Range rng = document.Content; rng.Find.ClearFormatting(); rng.Find.Forward = true; rng.Find.Text = title; rng.Find.Execute(); while (rng.Find.Found) { var listFormatFromWord = rng.ListFormat.ListString.Trim('.'); listFormatFromWord = Regex.Replace(listFormatFromWord, @"\p{C}+", string.Empty); if (listFormatFromWord.Equals(listFormatValue)) { Navigate.ToRange(rng); return; } rng.Find.Execute(); } MessageBox.Show(new Form() { TopMost = true }, @"Such Clause or Schedule was not found"); }); }
private void ShowDefinitionBtn_Click(object sender, RibbonControlEventArgs e) { LogHelper.Info($"Showing definition"); WordHelpers.RunActionWithWaitCursor(() => { var activeDocument = Globals.ThisAddIn.Application.ActiveDocument; LogHelper.Debug($"Getting selected text..."); var selectedText = Globals.ThisAddIn.Application.Selection.Text.RefineSelectionText(); LogHelper.Debug($"Selected text: {selectedText}"); if (string.IsNullOrWhiteSpace(selectedText)) { return; } var definition = Globals.ThisAddIn.AnalyzersByDocument[activeDocument.FullName].GetWordDefinition(selectedText); // TODO Leave here commented, we will re-implement this as a final fallback //if (string.IsNullOrWhiteSpace(definition.Text)) //{ // definition = FindFirstInstanceOfTerm(selection); //} if (definition == null) { MessageBox.Show(new Form() { TopMost = true }, $"Cannot find the definition '{selectedText}'"); return; } if (definition.Locations.Count > 1) { var listOfDefinitions = new ListOfDefinitions(definition, Globals.ThisAddIn.Application.Selection.Range, activeDocument.FullName); listOfDefinitions.Show(); } else { LogHelper.Info("Borders: start=" + definition.Locations[0].Start + " ; end=" + definition.Locations[0].End); var definitionBox = new DefinitionBox { Definition = definition, SourceFile = activeDocument.FullName, _currentPosition = Globals.ThisAddIn.Application.Selection.Range }; definitionBox.Show(); } }); }
private void ApplicationOnWindowSelectionChange(Selection sel) { if (Globals.ThisAddIn.ShouldIgnoreNextSelection) { Globals.ThisAddIn.ShouldIgnoreNextSelection = false; return; } if (Globals.ThisAddIn.FlagOfSelection) { WordHelpers.RunActionWithWaitCursor(() => { var selectionRange = Globals.ThisAddIn.Application.Selection.Range; var selection = selectionRange.Text.RefineSelectionText(); LogHelper.Info(string.Format("Find Definition Request for '{0}'", selection)); if (string.IsNullOrWhiteSpace(selection)) { return; } var start = DateTime.Now; var activeDocument = Globals.ThisAddIn.Application.ActiveDocument; var definition = AnalyzersByDocument[activeDocument.FullName].GetWordDefinition(selection); if (definition == null) { MessageBox.Show(new Form() { TopMost = true }, @"Definition could not be found"); return; } var finish = (DateTime.Now - start); LogHelper.Info($"Word \"{selection}\" was found in {finish.TotalSeconds} seconds"); if (definition.Locations.Count > 1) { var listOfDefinitions = new ListOfDefinitions(definition, selectionRange, activeDocument.FullName); listOfDefinitions.Show(); } else { var definitionBox = new DefinitionBox { Definition = definition, SourceFile = activeDocument.FullName, _currentPosition = selectionRange }; definitionBox.Show(); } }); } }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (_application == null || _document == null) { return; } WordHelpers.DisposeWordInstance(_application, _document); try { _document.Close(SaveChanges: false, OriginalFormat: false, RouteDocument: false); _application.Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false); } catch (Exception) {} System.Runtime.InteropServices.Marshal.ReleaseComObject(_application); }
private static IDictionary <Word, IList <ProblemMapping> > GetQuestionResults(IQuestion question, IEnumerable <ProblemMapping> problemMappings = null) { problemMappings = problemMappings ?? ProblemMapping.AllProblemMappings; var wordToMapping = WordHelpers.AllWords() .ToDictionary <Word, Word, IList <ProblemMapping> >(word => word, word => new List <ProblemMapping>()); foreach (var problemMapping in problemMappings) { var answers = question.GetPossibleAnswers(problemMapping).OrderBy(a => a); foreach (var answer in answers) { wordToMapping[problemMapping.AnswerMapping[answer]].Add(problemMapping); } } return(wordToMapping); }
private void ShowOccurancesButton_Click(object sender, EventArgs e) { WordHelpers.RunActionWithWaitCursor(() => { SetDefinitionOccurences(); var amountOfCorrectOccurences = _corectDefinitionOccurences.Count; var amountOfIncorrectOccurences = _incorrectDefinitionOccurences.Count; if (amountOfCorrectOccurences > 0) { HighlightAllUsages.Enabled = true; HighlightAllUsages.Text = $"Highlight Correct Usages ({amountOfCorrectOccurences})"; } if (amountOfIncorrectOccurences > 0) { HighlightIncorrectUsages.Enabled = true; HighlightIncorrectUsages.Text = $"Highlight Incorrect Usages ({amountOfIncorrectOccurences})"; } CountLabel.Text = $"Occurrences: {_definitionOccurences.Count}"; }); }
private void ScanDocumentBtn_Click(object sender, RibbonControlEventArgs e) { LogHelper.Info("Scanning document..."); WordHelpers.RunActionWithWaitCursor(() => WordHelpers.RunActionWithDisablingScreenUpdating(() => { var activeDocument = Globals.ThisAddIn.Application.ActiveDocument; var analyzer = Globals.ThisAddIn.AnalyzersByDocument[activeDocument.FullName]; try { analyzer.Analyze(); } catch (Exception ex) { LogHelper.Fatal("Failed to analyze doc.", ex); } }) ); }
private void UpdateFormField(FormFieldData field, string value) { var text = field.Descendants <TextInput>().First(); WordHelpers.SetFormFieldValue(text, value); }
public Bitmap Read(byte[] data) { int width; int height; byte[] pixelData; width = WordHelpers.GetInt16Le(data, 0); height = WordHelpers.GetInt16Le(data, 2); pixelData = new byte[width * height]; // doom images seem to use index // 255 for transparency, so we'll // set the image data to transparent // by default for the entire image for (int i = 0; i < pixelData.Length; i++) { pixelData[i] = 255; } // each column is represented by one or // more sub columns, called "posts" in the // DOOM FAQ. Each post has a starting row, // and a height, followed by a seemly unused // value, the pixel data and then another // unused value for (int column = 0; column < width; column++) { int pointer; pointer = WordHelpers.GetInt32Le(data, (column * 4) + 8); do { int row; int postHeight; row = data[pointer]; if (row != 255 && (postHeight = data[++pointer]) != 255) { pointer++; // unused value for (int i = 0; i < postHeight; i++) { if (row + i < height && pointer < data.Length - 1) { pixelData[((row + i) * width) + column] = data[++pointer]; } } pointer++; // unused value } else { break; } } while (pointer < data.Length - 1 && data[++pointer] != 255); } return(this.CreateIndexedBitmap(width, height, pixelData)); }
public void AddRanges(HexViewer control, byte[] buffer) { control.Clear(); if (buffer.Length > 8) { int[] pointer; short width; short height; width = WordHelpers.GetInt16Le(buffer, 0); height = WordHelpers.GetInt16Le(buffer, 2); // do some basic sanity checking if (width > 0 && width <= 320 && height > 0 && height <= 200) { control.AddRange(0, 2, Color.MediumSeaGreen, Color.White, "Width"); // width control.AddRange(2, 2, Color.SeaGreen, Color.White, "Height"); // height control.AddRange(4, 2, Color.DeepPink, Color.White, "X-Offset"); // x offset control.AddRange(6, 2, Color.HotPink, Color.White, "Y-Offset"); // y offset pointer = new int[width]; for (int i = 0; i < width; i++) { int index; index = 8 + (i * 4); pointer[i] = WordHelpers.GetInt32Le(buffer, index); control.AddRange(new HexViewer.ByteGroup { Start = index, Length = 4, ForeColor = Color.White, BackColor = Color.Orange, Pointer = pointer[i], Type = "Pointer #" + i.ToString() }); // column pointer } for (int i = 0; i < width; i++) { int index; byte length; int sourceIndex; index = pointer[i]; length = buffer[index + 1]; sourceIndex = 8 + (i * 4); // post if (_showPrimaryPosts) { this.AddPost(control, index, length, sourceIndex, true); } if (length != 255) { index = this.IncrementColumn(buffer, index + 3, length); //index += length + 4; // row, length, 2xunused, pixel data if (buffer[index] == 255) { this.AddColumnEndMarker(control, index, pointer[i]); } else { while (true) { length = buffer[index + 1]; if (length == 255 || index + length + 4 > buffer.Length) { break; } else if (_showSecondaryPosts) { this.AddPost(control, index, length, sourceIndex, false); } index = this.IncrementColumn(buffer, index + 3, length); // index += length + 4; if (buffer[index] == 255) { this.AddColumnEndMarker(control, index, pointer[i]); break; } } } } } } } control.Invalidate(); }