///
The document could not be displayed.
"); sw.WriteLine("{0}
", error); sw.WriteLine(""); Core.WebBrowser.ShowHtml( sw.ToString(), WebSecurityContext.Restricted, null ); } private void LoadRtf( string fileName ) { Core.WebBrowser.Visible = false; _richTextBox.Visible = true; _richTextBox.Clear(); FileStream fs = new FileStream( fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ); try { StreamReader reader = new StreamReader( fs, Encoding.Default ); _richTextBox.Rtf = Utils.StreamReaderReadToEnd( reader ); } finally { fs.Close(); } } private void LoadHtml( string fileName ) { _richTextBox.Visible = false; Core.WebBrowser.Visible = true; Core.WebBrowser.ShowHtml( "Please wait while the document is being converted…", WebSecurityContext.Restricted, null ); _killedConverter = false; _converterProcess = WordDocPlugin.CreateWvWareProcess( fileName, "wvHtml.xml", false ); _converterProcess.EnableRaisingEvents = true; _converterProcess.Exited += _converterProcess_OnExited; try { if(!_converterProcess.Start()) throw new Exception(); } catch { _converterProcess = null; Core.UIManager.QueueUIJob( new ShowErrorDelegate( ShowError ), "WVWare conversion failed. Could not start convertion process: converter not found."); return; } _convertedFileName = Path.Combine( WordDocPlugin.TempDir, Path.GetFileNameWithoutExtension( fileName ) + ".html" ); _converterOutputReader = new StreamReader( _converterProcess.StandardOutput.BaseStream, Encoding.UTF8 ); _converterOutputWriter = new StreamWriter( _convertedFileName, false, Encoding.UTF8 ); new Thread( ProcessConverterOutput ).Start(); } private void ProcessConverterOutput() { try { string str; while ((str = _converterOutputReader.ReadLine()) != null) { _converterOutputWriter.Write( str ); } } finally { _converterOutputWriter.Close(); } } private void _converterProcess_OnExited( object sender, EventArgs e ) { if ( _killedConverter ) return; if ( _converterProcess.ExitCode == 0 ) { Core.WebBrowser.Stop(); // If we do not, hilite will be applied to the "please wait" banner Core.WebBrowser.NavigateInPlace( _convertedFileName ); if ( _wordsToHighlight != null ) { Core.WebBrowser.HighlightWords( _wordsToHighlight, 0 ); } } else if ( _converterProcess.ExitCode == -2 ) { Core.UIManager.QueueUIJob( new ShowErrorDelegate( ShowError ), Path.GetFileName( _sourceFileName ) + " is a password-protected document." ); } else if ( _converterProcess.ExitCode == -5 ) { Core.UIManager.QueueUIJob( new ShowErrorDelegate( ShowError ), Path.GetFileName( _sourceFileName ) + " is not a valid Microsoft Word document or a Rich Text Format file." ); } else if ( _converterProcess.ExitCode == -7 ) { Core.UIManager.QueueUIJob( new ShowErrorDelegate( ShowError ), "No space left on disk to convert " + Path.GetFileName( _sourceFileName ) + "." ); } else { string error = Utils.StreamReaderReadToEnd( _converterProcess.StandardError ); Core.UIManager.QueueUIJob( new ShowErrorDelegate( ShowError ), "WVWare conversion failed. " + error ); } Core.FileResourceManager.CleanupSourceFile( _resource, _sourceFileName ); } private delegate void ShowErrorDelegate( string error ); public void HighlightWords( WordPtr[] words ) { words = DocumentSection.RestrictResults( words, DocumentSection.BodySection ); if ( _richTextBox.Visible ) _richTextBox.HighlightWords( words ); else { Core.WebBrowser.HighlightWords( words, 0 ); _wordsToHighlight = words; } } // TODO: remove? /* private void HighlightRtfWords( WordPtr[] words ) { // we use plain text for indexing, so the offsets will not match => // use token-based highlighting _wordsToHighlight = words; _lastHighlightOffset = 0; for( _lastHighlightIndex = 0; _lastHighlightIndex < words.Length; _lastHighlightIndex++ ) { // if there are too many words to highlight - use async highlighting, in // order not to lock up the UI for too long if ( _lastHighlightIndex == 5 ) { _wordHighlightTimer.Start(); break; } HighlightNextWord(); } } */ private void HighlightNextWord() { if ( _wordsToHighlight [_lastHighlightIndex].Section != DocumentSection.BodySection ) { return; } int offset = _richTextBox.Find( _wordsToHighlight [_lastHighlightIndex].Text, _lastHighlightOffset, RichTextBoxFinds.WholeWord ); if ( offset >= 0 ) { int highlightLength = _wordsToHighlight [_lastHighlightIndex].Text.Length; _lastHighlightOffset = offset + highlightLength; _richTextBox.Select( offset, highlightLength ); CHARFORMAT2 fmt = new CHARFORMAT2(); fmt.cbSize = Marshal.SizeOf( fmt ); fmt.dwMask = CFM.BACKCOLOR | CFM.COLOR; fmt.crBackColor = ColorTranslator.ToWin32( SystemColors.Highlight ); fmt.crTextColor = ColorTranslator.ToWin32( SystemColors.HighlightText ); Win32Declarations.SendMessage( _richTextBox.Handle, EditMessage.SETCHARFORMAT, SCF.SELECTION, ref fmt ); } } private void _wordHighlightTimer_Tick( object sender, EventArgs e ) { if ( _lastHighlightIndex < _wordsToHighlight.Length ) { HighlightNextWord(); _lastHighlightIndex++; if ( _lastHighlightIndex == _wordsToHighlight.Length ) { _wordHighlightTimer.Stop(); } } } public void EndDisplayResource( IResource resource ) { if ( _converterProcess != null ) { bool exited = true; try { exited = _converterProcess.HasExited; } catch( InvalidOperationException ) { // Really, not started at all. _killedConverter = true; } if( ! exited ) { _converterProcess.Kill(); _killedConverter = true; } } } public void DisposePane() { Controls.Remove( Core.WebBrowser ); Core.WebBrowser.Visible = true; Dispose(); } public string GetSelectedText( ref TextFormat format ) { if ( _richTextBox.Visible ) { format = TextFormat.Rtf; return _richTextBox.SelectedRtf; } else { format = TextFormat.Html; return Core.WebBrowser.SelectedHtml; } } public string GetSelectedPlainText() { if ( _richTextBox.Visible ) { return _richTextBox.SelectedText; } else { return Core.WebBrowser.SelectedText; } } public bool CanExecuteCommand( string command ) { if ( _richTextBox.Visible ) { return _richTextBox.CanExecuteCommand( command ); } else { return Core.WebBrowser.CanExecuteCommand( command ); } } public void ExecuteCommand( string command ) { if ( _richTextBox.Visible ) { _richTextBox.ExecuteCommand( command ); } else { Core.WebBrowser.ExecuteCommand( command ); } } private void WordDisplayPane_KeyDown(object sender, KeyEventArgs e) { e.Handled = Core.ActionManager.ExecuteKeyboardAction( new ActionContext( ActionContextKind.Keyboard, null, _resource.ToResourceList() ), e.KeyData ); } #region IContextProvider Members public IActionContext GetContext( ActionContextKind kind ) { return new ActionContext(kind, this, (_resource != null ? _resource.ToResourceList() : null)); } #endregion } }