Esempio n. 1
0
        private IVsWindowFrame GetVsWindowFrameByTypeAndMoniker(Guid editorTypeId, string moniker)
        {
            IEnumWindowFrames enumWindowFrames;

            VsUiShell.GetDocumentWindowEnum(out enumWindowFrames);

            var  frames = new IVsWindowFrame[1];
            uint numFrames;

            IVsWindowFrame m_frame;

            while (enumWindowFrames.Next(1, frames, out numFrames) == VSConstants.S_OK && numFrames == 1)
            {
                m_frame = frames[0] as IVsWindowFrame;

                object monikerObject;
                m_frame.GetProperty((int)__VSFPROPID.VSFPROPID_pszMkDocument, out monikerObject);

                Guid editorTypeIdGuid;
                m_frame.GetGuidProperty((int)__VSFPROPID.VSFPROPID_guidEditorType, out editorTypeIdGuid);

                if (monikerObject != null && monikerObject.Equals(moniker) && editorTypeIdGuid.Equals(editorTypeId))
                {
                    return(m_frame);
                }
            }

            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Loads the file content into the editor (into the controls representing the UI).
        /// </summary>
        /// <param name="pszFilename">
        /// Pointer to the full path name of the file to load.
        /// </param>
        /// <param name="grfMode">File format mode.</param>
        /// <param name="fReadOnly">
        /// determines if the file should be opened as read only.
        /// </param>
        /// <returns>S_OK if the method succeeds.</returns>
        int IPersistFileFormat.Load(string pszFilename, uint grfMode, int fReadOnly)
        {
            // --- A valid file name is required.
            if ((String.IsNullOrEmpty(pszFilename) && String.IsNullOrEmpty(FileName)) ||
                (!String.IsNullOrEmpty(pszFilename) && !File.Exists(pszFilename)))    // pszFilename might not exist.
            {
                return(VSConstants.E_INVALIDARG);
            }

            _loading = true;
            try
            {
                // --- If the new file name is null, then this operation is a reload
                bool isReload = String.IsNullOrEmpty(pszFilename);

                // --- Show the wait cursor while loading the file
                VsUiShell.SetWaitCursor();

                // --- Set the new file name
                if (!isReload)
                {
                    // --- Unsubscribe from the notification of the changes in the previous file.
                    FileName = pszFilename;
                }
                // --- Load the file
                LoadFile(FileName);
                _isDirty = false;

                // --- Notify the load or reload
                NotifyDocChanged();
            }
            finally
            {
                _loading = false;
            }
            return(VSConstants.S_OK);
        }
Esempio n. 3
0
        /// <summary>
        /// Saves the document data. Before actually saving the file, we first need to
        /// indicate to the environment that a file is about to be saved. This is done
        /// through the "SVsQueryEditQuerySave" service. We call the "QuerySaveFile"
        /// function on the service instance and then proceed depending on the result
        /// returned as follows:
        ///
        /// If result is QSR_SaveOK - We go ahead and save the file and the file is not
        /// read only at this point.
        ///
        /// If result is QSR_ForceSaveAs - We invoke the "Save As" functionality which will
        /// bring up the Save file name dialog.
        ///
        /// If result is QSR_NoSave_Cancel - We cancel the save operation and indicate that
        /// the document could not be saved by setting the "pfSaveCanceled" flag.
        ///
        /// If result is QSR_NoSave_Continue - Nothing to do here as the file need not be
        /// saved.
        /// </summary>
        /// <param name="dwSave">Flags which specify the file save options:
        /// VSSAVE_Save        - Saves the current file to itself.
        /// VSSAVE_SaveAs      - Prompts the User for a filename and saves the file to
        ///                      the file specified.
        /// VSSAVE_SaveCopyAs  - Prompts the user for a filename and saves a copy of the
        ///                      file with a name specified.
        /// VSSAVE_SilentSave  - Saves the file without prompting for a name or confirmation.
        /// </param>
        /// <param name="pbstrMkDocumentNew">Pointer to the path to the new document.</param>
        /// <param name="pfSaveCanceled">Value 1 if the document could not be saved.</param>
        /// <returns>S_OK if the method succeeds.</returns>
        int IVsPersistDocData.SaveDocData(VSSAVEFLAGS dwSave, out string pbstrMkDocumentNew, out int pfSaveCanceled)
        {
            pbstrMkDocumentNew = null;
            pfSaveCanceled     = 0;
            int hr;

            switch (dwSave)
            {
            case VSSAVEFLAGS.VSSAVE_Save:
            case VSSAVEFLAGS.VSSAVE_SilentSave:
            {
                var queryEditQuerySave = (IVsQueryEditQuerySave2)GetService(typeof(SVsQueryEditQuerySave));
                // --- Call QueryEditQuerySave
                hr = queryEditQuerySave.QuerySaveFile(
                    FileName,         // filename
                    0,                // flags
                    null,             // file attributes
                    out uint result); // result

                if (ErrorHandler.Failed(hr))
                {
                    return(hr);
                }

                // Process according to result from QuerySave
                switch ((tagVSQuerySaveResult)result)
                {
                case tagVSQuerySaveResult.QSR_NoSave_Cancel:
                    // --- This is also case tagVSQuerySaveResult.QSR_NoSave_UserCanceled because these
                    // --- two tags have the same value.
                    pfSaveCanceled = ~0;
                    break;

                case tagVSQuerySaveResult.QSR_SaveOK:
                {
                    // Call the shell to do the save for us
                    hr = VsUiShell.SaveDocDataToFile(dwSave, this, FileName,
                                                     out pbstrMkDocumentNew, out pfSaveCanceled);
                    if (ErrorHandler.Failed(hr))
                    {
                        return(hr);
                    }
                }
                break;

                case tagVSQuerySaveResult.QSR_ForceSaveAs:
                {
                    // Call the shell to do the SaveAS for us
                    hr = VsUiShell.SaveDocDataToFile(VSSAVEFLAGS.VSSAVE_SaveAs, this, FileName,
                                                     out pbstrMkDocumentNew, out pfSaveCanceled);
                    if (ErrorHandler.Failed(hr))
                    {
                        return(hr);
                    }
                }
                break;

                case tagVSQuerySaveResult.QSR_NoSave_Continue:
                    // In this case there is nothing to do.
                    break;

                default:
                    throw new COMException("Unsupported result from QEQS");
                }
                break;
            }

            case VSSAVEFLAGS.VSSAVE_SaveAs:
            case VSSAVEFLAGS.VSSAVE_SaveCopyAs:
            {
                // Make sure the file name as the right extension
                if (String.Compare(FileExtensionUsed, Path.GetExtension(FileName), true, CultureInfo.CurrentCulture) != 0)
                {
                    FileName += FileExtensionUsed;
                }
                // Call the shell to do the save for us
                hr = VsUiShell.SaveDocDataToFile(dwSave, this, FileName, out pbstrMkDocumentNew,
                                                 out pfSaveCanceled);
                if (ErrorHandler.Failed(hr))
                {
                    return(hr);
                }
                break;
            }

            default:
                throw new ArgumentException("Unsupported Save flag.");
            }
            return(VSConstants.S_OK);
        }