GME  13
AnnotatorTextPart.cpp
Go to the documentation of this file.
00001 //################################################################################################
00002 //
00003 // Annotator text part class
00004 //      AnnotatorTextPart.cpp
00005 //
00006 //################################################################################################
00007 
00008 #include "StdAfx.h"
00009 #include "AnnotationDefs.h"
00010 #include "AnnotatorTextPart.h"
00011 #include "..\MgaUtil\AnnotationUtil.h"
00012 #include "DecoratorExceptions.h"
00013 
00014 
00015 namespace AnnotatorDecor {
00016 
00017 //################################################################################################
00018 //
00019 // CLASS : AnnotatorTextPart
00020 //
00021 //################################################################################################
00022 
00023 AnnotatorTextPart::AnnotatorTextPart(DecoratorSDK::PartBase* pPart, CComPtr<IMgaCommonDecoratorEvents>& eventSink):
00024         TextPart                (pPart, eventSink),
00025         m_regRoot               (NULL)
00026 {
00027         m_crText                                        = AN_DEFAULT_COLOR;
00028         memset(&m_logFont, 0, sizeof(LOGFONT));
00029 
00030 }
00031 
00032 AnnotatorTextPart::~AnnotatorTextPart()
00033 {
00034 }
00035 
00036 void AnnotatorTextPart::SetParam(const CString& strName, VARIANT vValue)
00037 {
00038         if (!strName.CompareNoCase(AN_PARAM_ROOTNODE)) {
00039                 CComVariant     variantval(vValue);
00040                 try {
00041                         if (!m_regRoot) {
00042                                 COMTHROW(variantval.ChangeType(VT_UNKNOWN));
00043                                 CComPtr<IUnknown> unk(variantval.punkVal);
00044                                 COMTHROW(unk.QueryInterface(&m_regRoot));
00045                         }
00046                         ReadPreferences();
00047                 }
00048                 catch (hresult_exception&) {
00049                         throw DecoratorException((DecoratorExceptionCode)E_DECORATOR_UNKNOWN_PARAMETER);
00050                 }
00051         } else {
00052                 throw DecoratorException((DecoratorExceptionCode)E_DECORATOR_UNKNOWN_PARAMETER);
00053         }
00054 }
00055 
00056 bool AnnotatorTextPart::GetParam(const CString& strName, VARIANT* pvValue)
00057 {
00058         if (!strName.CompareNoCase(AN_PARAM_ROOTNODE)) {
00059                 CComVariant     variantval;
00060                 variantval.Attach(pvValue);
00061                 variantval = m_regRoot;
00062         } else {
00063                 throw DecoratorException((DecoratorExceptionCode)E_DECORATOR_UNKNOWN_PARAMETER);
00064         }
00065         return true;
00066 }
00067 
00068 CSize AnnotatorTextPart::GetPreferredSize(void) const
00069 {
00070         CDC     dc;
00071         dc.CreateCompatibleDC(NULL);
00072         Gdiplus::Font myFont(dc.m_hDC, &m_logFont);
00073         CSize size = DecoratorSDK::getFacilities().MeasureText(NULL, &myFont, m_strText);
00074         size.cx += 2 * AN_MARGIN;
00075         size.cy += 2 * AN_MARGIN;
00076         return size;
00077 }
00078 
00079 void AnnotatorTextPart::Draw(CDC* pDC, Gdiplus::Graphics* gdip)
00080 {
00081         if (m_bTextEnabled) {
00082                 CRect loc = GetLocation();
00083                 loc.InflateRect(-AN_MARGIN, -AN_MARGIN, 0, 0);
00084                 Gdiplus::Font myFont(pDC->m_hDC, &m_logFont);
00085                 DecoratorSDK::getFacilities().DrawString(gdip,
00086                                                                                                  m_strText,
00087                                                                                                  CRect(loc.left, loc.top, loc.right, loc.bottom),
00088                                                                                                  &myFont,
00089                                                                                                  (m_bActive) ? m_crText : DecoratorSDK::COLOR_GRAYED_OUT,
00090                                                                                                  TA_LEFT | TA_TOP,
00091                                                                                                  -1, // annotation texts should not be cropped
00092                                                                                                  "",
00093                                                                                                  "",
00094                                                                                                  false);
00095         }
00096         if (m_spFCO)
00097                 resizeLogic.Draw(pDC, gdip);
00098 }
00099 
00100 // New functions
00101 void AnnotatorTextPart::InitializeEx(CComPtr<IMgaProject>& pProject, CComPtr<IMgaMetaPart>& pPart, CComPtr<IMgaFCO>& pFCO,
00102                                                                          HWND parentWnd, DecoratorSDK::PreferenceMap& preferences)
00103 {
00104         preferences[DecoratorSDK::PREF_LABELLENGTH]                     = DecoratorSDK::PreferenceVariant((long)-1);
00105         preferences[DecoratorSDK::PREF_TEXTOVERRIDE]            = DecoratorSDK::PreferenceVariant(true);
00106         preferences[DecoratorSDK::PREF_TEXTCOLOROVERRIDE]       = DecoratorSDK::PreferenceVariant(true);
00107         preferences[DecoratorSDK::PREF_MULTILINEINPLACEEDIT]= DecoratorSDK::PreferenceVariant(true);
00108         TextPart::InitializeEx(pProject, pPart, pFCO, parentWnd, preferences);
00109 }
00110 
00111 CPoint AnnotatorTextPart::GetTextPosition(CDC* pDC, Gdiplus::Graphics* gdip) const
00112 {
00113         return GetTextLocation(pDC, gdip).TopLeft();
00114 }
00115 
00116 CRect AnnotatorTextPart::GetTextLocation(CDC* pDC, Gdiplus::Graphics* gdip) const
00117 {
00118         return GetLocation();
00119 }
00120 
00121 void AnnotatorTextPart::ExecuteOperation(const CString& newString)
00122 {
00123         // transaction operation begin
00124         try {
00125                 m_strText = newString;
00126                 CString tmpstr = CAnnotationUtil::ResolveNewLinesToLF(newString);
00127                 CComBSTR bstr;
00128                 CopyTo(tmpstr, bstr);
00129                 COMTHROW(m_regRoot->put_Value(bstr));
00130         }
00131         catch (hresult_exception&) {
00132 //              m_strText = "Unable to write annotation!";
00133         }
00134         // transaction operation end
00135 }
00136 
00137 void AnnotatorTextPart::ReadPreferences(void)
00138 {
00139         try {
00140                 CComBSTR bstr;
00141                 COMTHROW(m_regRoot->get_Value(&bstr));
00142                 m_strText = bstr;
00143                 m_strText = CAnnotationUtil::ResolveNewLinesToCRLF(m_strText);
00144         }
00145         catch (hresult_exception&) {
00146                 m_strText = "Unable to read annotation !";
00147         }
00148 
00149         try {
00150                 CComBSTR bstr;
00151                 COMTHROW(m_regRoot->get_Name(&bstr));
00152                 m_strName = bstr;
00153         }
00154         catch (hresult_exception&) {
00155                 m_strName = "Unknown";
00156         }
00157 
00158         try {
00159                 CComBSTR bstr;
00160                 CComPtr<IMgaRegNode> lfNode;
00161                 CComBSTR lfName(AN_FONT_PREF);
00162                 COMTHROW(m_regRoot->get_SubNodeByName(lfName, &lfNode));
00163                 if (lfNode != NULL) {
00164                         COMTHROW(lfNode->get_Value(&bstr));
00165                 }
00166                 CString str(bstr);
00167                 if (!CAnnotationUtil::LogfontDecode(str, &m_logFont)) {
00168                         // throw hresult_exception();
00169                         CAnnotationUtil::FillLogFontWithDefault(&m_logFont);
00170                 }
00171         }
00172         catch (hresult_exception&) {
00173                 CAnnotationUtil::FillLogFontWithDefault(&m_logFont);
00174         }
00175 
00176         try {
00177                 CComBSTR bstr;
00178                 CComPtr<IMgaRegNode> colNode;
00179                 CComBSTR colName(AN_COLOR_PREF);
00180                 COMTHROW(m_regRoot->get_SubNodeByName(colName, &colNode));
00181                 if (colNode != NULL) {
00182                         COMTHROW(colNode->get_Value(&bstr));
00183                 }
00184                 CString strVal(bstr);
00185                 unsigned int val;
00186                 if (_stscanf(strVal,_T("%x"),&val) == 1) {
00187                         unsigned int r = (val & 0xff0000) >> 16;
00188                         unsigned int g = (val & 0xff00) >> 8;
00189                         unsigned int b = val & 0xff;
00190                         m_crText = RGB(r,g,b);
00191                 } else {
00192                         m_crText = AN_DEFAULT_COLOR;
00193                 }
00194         }
00195         catch (hresult_exception&) {
00196                 m_crText = AN_DEFAULT_COLOR;
00197         }
00198 }
00199 
00200 }; // namespace AnnotatorDecor