Objective Edit : Chapter 7 Objective Edit Tutorial : STEP 2 - Add Support for Syntax Highlighting
STEP 2 - Add Support for Syntax Highlighting
In this step, we add syntax coloring to the 1stEdit skeleton application that we created in Step 1. The procedure for adding syntax coloring requires four steps, which are covered in detail in the following four sections.
Modify CMy1stEditApp::InitInstance()
The first step in adding syntax coloring to our application is to modify the function CMy1stEditApp::InitInstance(). Here is the procedure:
1. Open 1stEdit.cpp.
2. Add the following macro to the InitInstance() implementation:
 
SECEDIT_REGISTER_INI_LANGUAGE(_T("DevStudio"), _T(".\\DevStudio.ini"));
This macro sets the configuration source to be an .ini file. There are other macros that allow information to be read in from either a binary file or resources embedded in the application, and persisted to either a binary file or the registry. For example,
 
// Read and write from a bin configuration file
SECEDIT_REGISTER_BIN_LANGUAGE(lang, file)
 
// Read from a resource and then the registry, write to the
// registry
SECEDIT_REGISTER_RES_REG_LANGUAGE(lang, file, key)
 
// Read from an ini and then the registry, write to the
// registry
SECEDIT_REGISTER_INI_REG_LANGUAGE(lang, file, key)
 
// Read from a bin and then the registry, write to the
// registry
SECEDIT_REGISTER_BIN_REG_LANGUAGE(lang, file, key)
NOTE >> This macro can be added anywhere in the method, except after the call to ShowWindow() near the bottom of the method.
3. For clipboard support, add code to initialize the OLE libraries in the application object's InitInstance(). Add a call to AfxOleInit() at the beginning of the InitInstance().
 
AfxOleInit();
NOTE >> This call to AfxOleInit() is very important for clipboard support; its absence will result in inexplicable failures. For example, leaving this call out might generate the error message “CoInitialize has not been called.”
Modify CMy1stEditDoc:: OnOpenDocument() and OnNewDocument()
In this step, we continue to modify functions. Still in 1stEditDoc.cpp, edit OnOpenDocument()and OnNewDocument() to look like the following:
 
BOOL CMy1stEditDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
 
m_Edit.SetLanguage(_T("DevStudio"));
return (m_Edit.ReadFile(lpszPathName));
}
 
BOOL CMy1stEditDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
 
m_Edit.SetLanguage(_T("DevStudio"));
m_Edit.Init();
 
return TRUE;
}
In the OnNewDocument() override, calling SetLanguage() before calling Init() is recommended.
Copy the C Language Initialization File into the Project Directory
Copy the DevStudio.ini file from the Samples\Edit\MdiApp directory into the project directory. This file contains language and keyword information to support highlighting.
Build and Run Step 2
You may now build and run Step 2. Load some .cpp files, or type in some code. Figure 29 shows Step 2 in action.
Figure 29 – 1stEdit showing syntax highlighting in action