Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

The SourceInfo Add-In

As an example of a Developer Studio add-in, the CD-ROM that accompanies this book includes SourceInfo—an add-in that displays information about source files currently open in Developer studio.

The following pseudocode shows the algorithm used by the add-in:

For each open file
   if the file is a text file
      increase the file counter
      count the number of lines in the file
      add the number of lines to the line counter
      count the number of semicolons
      add the number of semicolons to the semicolon counter
    end if
end for
Report the number of files, lines, and semicolons

The SourceInfo add-in was created using the Developer Studio Add-In Wizard. Figure 17.4 shows the wizard page used to create the SourceInfo add-in.

17fig04.gif

Figure 17.4 The Developer Studio Add-In Wizard used to create the SourceInfo add-in.

The SourceInfo add-in was created using the following options:

As with other wizard-created projects, the SourceInfo project has basic functionality right away. To make SourceInfo really useful, you need to perform three steps:

  1. Create a dialog box to be used to display source information to the user.
  2. Modify the command-handling code to perform the steps outlined in the pseudocode earlier in this section.
  3. Modify the toolbar icon associated with the add-in.

Adding the Source Information Dialog Box to the Project

The IDD_SOURCEINFO dialog box was added to the project to display information about open source files. Figure 17.5 shows the IDD_SOURCEINFO dialog box.

17fig05.jpg

Figure 17.5 The IDD_SOURCEINFO dialog box resource.

In addition to the OK button, which is added automatically to the dialog box, the IDD_SOURCEINFO dialog box has six controls. Three of the new controls are static text controls, and three controls are edit controls. Table 17.1 lists the properties of the edit controls. Each of the controls also has the read-only property.

Table 17.1. Edit Control Properties

Name Resource ID
Open Files IDC_FILES
Lines IDC_LINES
Semicolons IDC_SEMICOLONS

The IDD_SOURCEINFO dialog box is managed by the CDlgSourceInfo class. Each of the edit controls is associated with a member variable in the CDlgSourceInfo class, as Table 17.2 shows.

Table 17.2. Edit Control Properties

        Variable[sr]
Resource ID Class Category Type Name
IDC_FILES CDlgSourceInfo Value DWORD m_dwFiles
IDC_LINES CDlgSourceInfo Value DWORD m_dwLines
IDC_SEMICOLONS CDlgSourceInfo Value DWORD m_dwSemiColons

Adding Command-Handling Code to the Project

As discussed earlier in this chapter, command-handling code is located in the CCommands class. The implementation file for this class is commands.cpp. The SourceInfo project's command code is handled by the CCommands::SourceInfoCommandMethod shown in Listing 17.5.

Example 17.5. The Command-Handling Code for the SourceInfo Project

STDMETHODIMP CCommands::SourceInfoCommandMethod()
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
    CDlgSourceInfo dlg;

    CComPtr<IDispatch> pDocsDisp;
    VERIFY_OK(m_pApplication->get_Documents(&pDocsDisp));
    CComQIPtr<IDocuments, &IID_IDocuments> pDocs(pDocsDisp);

    long totalDocsRead  = 0;
    long totalLineCount = 0;
    long totalSemiCount = 0;

    // The number of documents in the collection includes
    // non-text documents.
    long docCount;
    VERIFY_OK(pDocs->get_Count(&docCount));

    // The documents collection is 1-based, rather than the
    // traditional 0-based.
    for(long ndxDoc = 1 ; ndxDoc <= docCount ; ndxDoc++)
    {
        CComVariant        varItemNum = ndxDoc;
        CComPtr<IDispatch> pDocDisp;
        VERIFY_OK(pDocs->Item(varItemNum, &pDocDisp));
        CComQIPtr<ITextDocument, &IID_ITextDocument> pTextDoc(pDocDisp);

        if(!pTextDoc) continue;

        CComVariant  varMove = dsMove;
        CComVariant  varSearchOpt = dsMatchForward;
        CComBSTR     bstrSearch("");
        VARIANT_BOOL vbool = VARIANT_TRUE;
        long         lineCount = 0;

        CComPtr<IDispatch> pSelDisp;
        VERIFY_OK(pTextDoc->get_Selection(&pSelDisp));
        CComQIPtr<ITextSelection, &IID_ITextSelection> pSel(pSelDisp);

        totalDocsRead++;

        // Count lines in file...
        VERIFY_OK(pSel->EndOfDocument(varMove));
        VERIFY_OK(pSel->get_BottomLine(&lineCount));
        totalLineCount += lineCount;

        // Move the selection to the top of the file
        VERIFY_OK(pSel->StartOfDocument(varMove));

        // Count the number of semicolons in the file...
        long prevLine = -1;
        long prevCol = -1;
        while(1)
        {
            pSel->FindText(bstrSearch, varSearchOpt, &vbool);
            if(vbool != VARIANT_FALSE)
            {
                // If the new selection hasn't moved down the file
                // break out of the search.
                long currentLine;
                long currentCol;
                pSel->get_CurrentLine(&currentLine);
                pSel->get_CurrentColumn(&currentCol);

                if(currentLine < prevLine) break;
                if((currentLine == prevLine)&&(currentCol <= prevCol)) break;

                prevLine = currentLine;
                prevCol  = currentCol;
                totalSemiCount++;
            }
            else
            {
                break;
            }
        }
    }

    dlg.m_dwFiles = totalDocsRead;
    dlg.m_dwLines = totalLineCount;
    dlg.m_dwSemiColons = totalSemiCount;
    dlg.DoModal();

    VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
    return S_OK;
}

The code provided in Listing 17.5 uses the CDlgProjInfo class to display project information. To compile the commands.cpp file, the following line must be added just after the existing #include directives at the top of the file:

#include "dlgprojinfo.h"

The source code in Listing 17.5 follows the pseudocode presented earlier in this chapter. The Developer Studio IDocuments interface is used to iterate over the collection of files opened in Developer Studio. Next, the ITextDocument interface is queried for. If this interface cannot be retrieved for an open document, the document isn't a source file; for example, open resource files are not capable of returning ITextDocument interface pointers. Next, the number of lines and semicolons in the document is calculated. Finally, after all documents have tested, the totals for all open source files are displayed to the user.

Modifying the Toolbar

The SourceInfo add-in toolbar icon is modified to make it more easily recognizable among other Developer Studio toolbar icons. Figure 17.6 shows the bitmap for the new toolbar icons.

17fig06.jpg

Figure 17.6 The toolbar icons used for the SourceInfo Developer Studio add-in.

Note that two bitmaps are used for the toolbar—one for large 32x32–pixel buttons and one for 16x16–pixel buttons.

Using the SourceInfo Add-in

To begin using the SourceInfo add-in, you must compile the SourceInfo project and then copy the add-in to the Common\MSDev98\AddIns subdirectory under the Visual Studio installation directory.

Like the macro files discussed in the first half of this chapter, add-ins are not loaded automatically. You must specify that the add-in is to be loaded by following these steps:

  1. Choose Customize from the Developer Studio Tools menu. The Customize dialog box appears.
  2. Select the Add-Ins and Macro Files tab. One of the items displayed on this property page is a list of the currently loaded add-ins and macro files.
  3. Add-ins and macros that are loaded by Developer Studio have a check mark next to their icon in the list box. Click on the check box next to the SourceInfo icon to mark the add-in to be loaded.
  4. Click the Close button to exit the Customize dialog box. The SourceInfo toolbar appears inside Developer Studio.

Open one or more source files and click the SourceInfo icon. The SourceInfo dialog box appears, along with information about open source files. Figure 17.7 shows an example of the Source Information dialog box.

17fig07.gif

Figure 17.7 The SourceInfo Developer Studio add-in.

Share ThisShare This

Informit Network