Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1023 lines
31 KiB

  1. /*****************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 2000
  4. *
  5. * TITLE: xmltools.cpp
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: LazarI
  10. *
  11. * DGetATE: 10/18/00
  12. *
  13. * DESCRIPTION: Class which encapsulates XML DOM for implementing
  14. * wizard templates
  15. *
  16. *****************************************************************************/
  17. #include "precomp.h"
  18. #pragma hdrstop
  19. /////////////////////////////////
  20. // CPhotoTemplates impl.
  21. // global strings
  22. static const TCHAR gszVersionGUID[] = TEXT("{352A15C4-1D19-4e93-AF92-D939C2812491}");
  23. static const TCHAR gszPatternDefs[] = TEXT("template-def");
  24. static const TCHAR gszPatternLocale[] = TEXT("template-definitions[@measurements = \"%s\"]");
  25. static const TCHAR gszPatternLocaleInd[] = TEXT("template-definitions[@measurements = \"locale-independent\"]");
  26. static const TCHAR gszPatternGUID[] = TEXT("template-def[@guid = \"%s\"]");
  27. static const TCHAR gszGUID[] = TEXT("guid");
  28. static const LPCTSTR arrCommonPropNames[CTemplateInfo::PROP_LAST] =
  29. {
  30. TEXT("guid"),
  31. TEXT("group"),
  32. TEXT("title"),
  33. TEXT("description"),
  34. TEXT("repeat-photos"),
  35. TEXT("use-thumbnails-for-printing"),
  36. TEXT("print-filename"),
  37. TEXT("can-rotate"),
  38. TEXT("can-crop"),
  39. };
  40. /////////////////////////////////////////////////////////////////////////////////////////////////
  41. // utility functions
  42. template <class T>
  43. HRESULT _GetProp(IXMLDOMElement *pElement, LPCTSTR pszName, T &value);
  44. // number convertions
  45. HRESULT _ConvertTo(LPCTSTR pszValue, LONG &lValue);
  46. HRESULT _ConvertTo(LPCTSTR pszValue, double &dValue);
  47. HRESULT _ConvertTo(LPCTSTR pszValue, BOOL &bValue);
  48. // attributes access
  49. HRESULT _GetAttribute(IXMLDOMElement *pElement, LPCTSTR pszAttrName, CComBSTR &bstr);
  50. template <class T>
  51. HRESULT _GetProp(IXMLDOMElement *pElement, LPCTSTR pszName, T &value)
  52. {
  53. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("_GetProp: %s"),(pszName ? pszName : TEXT("NULL POINTER!"))));
  54. HRESULT hr = E_FAIL;
  55. CComBSTR bstr;
  56. if( pElement &&
  57. SUCCEEDED(hr = _GetAttribute(pElement, pszName, bstr)) &&
  58. SUCCEEDED(hr = _ConvertTo(bstr, value)) )
  59. {
  60. hr = S_OK;
  61. }
  62. WIA_RETURN_HR(hr);
  63. }
  64. HRESULT _ConvertTo(LPCTSTR pszValue, LONG &lValue)
  65. {
  66. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("_ConvertTo(LONG): %s"),(pszValue ? pszValue : TEXT("NULL POINTER!"))));
  67. HRESULT hr = E_INVALIDARG;
  68. if( pszValue )
  69. {
  70. hr = S_OK;
  71. TCHAR *endptr = NULL;
  72. lValue = _tcstol(pszValue, &endptr, 10);
  73. if( ERANGE == errno || *endptr )
  74. {
  75. // conversion failed
  76. lValue = 0;
  77. hr = E_FAIL;
  78. }
  79. }
  80. WIA_RETURN_HR(hr);
  81. }
  82. HRESULT _ConvertTo(LPCTSTR pszValue, double &dValue)
  83. {
  84. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("_ConvertTo(double): %s"),(pszValue ? pszValue : TEXT("NULL POINTER!"))));
  85. HRESULT hr = E_INVALIDARG;
  86. if( pszValue )
  87. {
  88. hr = S_OK;
  89. TCHAR *endptr = NULL;
  90. dValue = _tcstod(pszValue, &endptr);
  91. if( ERANGE == errno || *endptr )
  92. {
  93. // conversion failed
  94. dValue = 0.0;
  95. hr = E_FAIL;
  96. }
  97. }
  98. WIA_RETURN_HR(hr);
  99. }
  100. HRESULT _ConvertTo(LPCTSTR pszValue, BOOL &bValue)
  101. {
  102. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("_ConvertTo(bool): %s"),(pszValue ? pszValue : TEXT("NULL POINTER!"))));
  103. HRESULT hr = E_INVALIDARG;
  104. if( pszValue )
  105. {
  106. hr = S_OK;
  107. // check for true first
  108. if( 0 == lstrcmp(pszValue, TEXT("yes")) ||
  109. 0 == lstrcmp(pszValue, TEXT("on")) )
  110. {
  111. bValue = true;
  112. }
  113. else
  114. {
  115. // check for false next
  116. if( 0 == lstrcmp(pszValue, TEXT("no")) ||
  117. 0 == lstrcmp(pszValue, TEXT("off")) )
  118. {
  119. bValue = false;
  120. }
  121. else
  122. {
  123. // not a boolean
  124. hr = E_FAIL;
  125. }
  126. }
  127. }
  128. WIA_RETURN_HR(hr);
  129. }
  130. HRESULT _GetAttribute(IXMLDOMElement *pElement, LPCTSTR pszAttrName, CComBSTR &bstr)
  131. {
  132. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("_GetAttribute(BSTR): %s"),(pszAttrName ? pszAttrName : TEXT("NULL POINTER!"))));
  133. HRESULT hr = E_INVALIDARG;
  134. CComVariant strAttr;
  135. if( pElement && pszAttrName &&
  136. SUCCEEDED(hr = pElement->getAttribute(CComBSTR(pszAttrName), &strAttr)) )
  137. {
  138. if( VT_BSTR == strAttr.vt )
  139. {
  140. bstr = strAttr.bstrVal;
  141. hr = S_OK;
  142. }
  143. else
  144. {
  145. hr = E_FAIL;
  146. }
  147. }
  148. WIA_RETURN_HR(hr);
  149. }
  150. HRESULT _GetChildElement(IXMLDOMElement *pElement, LPCTSTR pszName, IXMLDOMElement **ppChild)
  151. {
  152. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("_GetChildElement( %s )"),(pszName ? pszName : TEXT("NULL POINTER!"))));
  153. HRESULT hr = E_INVALIDARG;
  154. if( pElement && pszName && ppChild )
  155. {
  156. CComPtr<IXMLDOMNode> pNode;
  157. if( SUCCEEDED(hr = pElement->selectSingleNode(CComBSTR(pszName), &pNode)) && pNode)
  158. {
  159. //
  160. // query for IXMLDOMElement interface
  161. //
  162. hr = pNode->QueryInterface(IID_IXMLDOMElement, (void **)ppChild);
  163. }
  164. }
  165. WIA_RETURN_HR(hr);
  166. }
  167. /////////////////////////////////////////////////////////////////////////////////////////////////
  168. // construction/destruction
  169. CPhotoTemplates::CPhotoTemplates():
  170. _Measure(MEASURE_UNKNOWN)
  171. {
  172. }
  173. INT MyTemplateDestroyCallback( LPVOID pItem, LPVOID lpData )
  174. {
  175. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("MyTemplateDestroyCallback( 0x%x, 0x%x )"),pItem,lpData));
  176. if (pItem)
  177. {
  178. delete (CTemplateInfo *)pItem;
  179. }
  180. return TRUE;
  181. }
  182. CPhotoTemplates::~CPhotoTemplates()
  183. {
  184. CAutoCriticalSection lock(_csList);
  185. DPA_DestroyCallback( _hdpaTemplates, MyTemplateDestroyCallback, NULL );
  186. _hdpaTemplates = NULL;
  187. }
  188. HRESULT CPhotoTemplates::AddTemplates(LPCTSTR pLocale)
  189. {
  190. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("CPhotoTemplates::AddTemplates( %s )"),pLocale));
  191. HRESULT hr = E_INVALIDARG;
  192. CComPtr<IXMLDOMNodeList> pTemplates;
  193. CComPtr<IXMLDOMNode> pLocaleNode;
  194. CComBSTR bstrGUID;
  195. //
  196. // Initialize things as needed
  197. //
  198. if (!pLocale || !_pRoot)
  199. {
  200. return hr;
  201. }
  202. CAutoCriticalSection lock(_csList);
  203. //
  204. // Select the correct locale node in the XML document
  205. //
  206. if (_pRoot)
  207. {
  208. hr = _pRoot->selectSingleNode( CComBSTR(pLocale), &pLocaleNode );
  209. WIA_CHECK_HR(hr,"AddTempaltes: _pRoot->selectSingleNode()");
  210. if (SUCCEEDED(hr) && pLocaleNode)
  211. {
  212. //
  213. // Select the templates sub-node
  214. //
  215. hr = pLocaleNode->selectNodes(CComBSTR(gszPatternDefs), &pTemplates);
  216. WIA_CHECK_HR(hr,"AddTemplates: pLocalNode->selectNodes( )");
  217. if (SUCCEEDED(hr) && pTemplates)
  218. {
  219. //
  220. // update the GUIDs of each template to be uppercase, so we can query later
  221. //
  222. LONG lCount = 0;
  223. hr = pTemplates->get_length(&lCount);
  224. WIA_CHECK_HR(hr,"AddTemplates: pTemplates->get_length(&lCount)");
  225. if (SUCCEEDED(hr) && lCount)
  226. {
  227. //
  228. // Loop through all the template and add them to the
  229. // the array of templates...
  230. //
  231. WIA_TRACE((TEXT("AddTemplates: loaded section, adding %d templates.."),lCount));
  232. for( LONG l = 0; SUCCEEDED(hr) && (l < lCount); l++ )
  233. {
  234. //
  235. // Get the actual XML item for the template...
  236. //
  237. CComPtr<IXMLDOMNode> pNode;
  238. hr = pTemplates->get_item(l, &pNode);
  239. WIA_CHECK_HR(hr,"LoadTemplate: pTemplates->get_item( lRelativeIndex )");
  240. if (SUCCEEDED(hr) && pNode)
  241. {
  242. //
  243. // query IXMLDOMElement interface
  244. //
  245. CComPtr<IXMLDOMElement> pTheTemplate;
  246. hr = pNode->QueryInterface(IID_IXMLDOMElement, (void **)&pTheTemplate);
  247. if (SUCCEEDED(hr) && pTheTemplate)
  248. {
  249. //
  250. // Create template for this item...
  251. //
  252. CTemplateInfo * pTemplateInfo = (CTemplateInfo *) new CTemplateInfo( pTheTemplate );
  253. if (pTemplateInfo)
  254. {
  255. INT iRes = -1;
  256. if (_hdpaTemplates)
  257. {
  258. iRes = DPA_AppendPtr( _hdpaTemplates, (LPVOID)pTemplateInfo );
  259. }
  260. if (iRes == -1)
  261. {
  262. //
  263. // The item was not added to the DPA, delete it...
  264. //
  265. delete pTemplateInfo;
  266. hr = E_FAIL;
  267. }
  268. }
  269. }
  270. }
  271. }
  272. }
  273. pTemplates = NULL;
  274. }
  275. pLocaleNode = NULL;
  276. }
  277. }
  278. WIA_RETURN_HR(hr);
  279. }
  280. // public interface
  281. HRESULT CPhotoTemplates::Init(IXMLDOMDocument *pDoc)
  282. {
  283. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("CPhotoTemplates::Init()")));
  284. HRESULT hr = E_INVALIDARG;
  285. CComBSTR bstrGUID;
  286. LONG lCountCommon = 0;
  287. CAutoCriticalSection lock(_csList);
  288. //
  289. // If the dpa of item isn't initialized, do it now...
  290. //
  291. if (!_hdpaTemplates)
  292. {
  293. _hdpaTemplates = DPA_Create(10);
  294. }
  295. //
  296. // if we're being called twice to initialize, make sure that works...
  297. //
  298. _pRoot = NULL;
  299. //
  300. // get the root element & the version guid
  301. //
  302. if( pDoc &&
  303. SUCCEEDED(hr = pDoc->get_documentElement(&_pRoot)) &&
  304. SUCCEEDED(hr = _GetAttribute(_pRoot, TEXT("guid"), bstrGUID)) )
  305. {
  306. // check the version
  307. if (0==lstrcmp(bstrGUID, gszVersionGUID))
  308. {
  309. //
  310. // Add the local-independent items first
  311. //
  312. hr = AddTemplates( gszPatternLocaleInd );
  313. //
  314. // Add the local-specific templates second
  315. //
  316. hr = _GetLocaleMeasurements( &_Measure );
  317. if (SUCCEEDED(hr))
  318. {
  319. TCHAR szLocale[MAX_PATH];
  320. *szLocale = 0;
  321. hr = _BuildLocaleQueryString(_Measure, szLocale, MAX_PATH);
  322. if (SUCCEEDED(hr))
  323. {
  324. hr = AddTemplates( szLocale );
  325. }
  326. }
  327. }
  328. }
  329. WIA_RETURN_HR(hr);
  330. }
  331. HRESULT CPhotoTemplates::InitForPrintTo()
  332. {
  333. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("CPhotoTemplates::Init()")));
  334. HRESULT hr = S_OK;
  335. //
  336. // Our job here is simple -- create 1 template that is the equivalent
  337. // of full page. Don't need any icons, etc., just the dimensions
  338. // and properties...
  339. //
  340. _hdpaTemplates = DPA_Create(1);
  341. if (_hdpaTemplates)
  342. {
  343. CTemplateInfo * pTemplateInfo = (CTemplateInfo *) new CTemplateInfo( );
  344. if (pTemplateInfo)
  345. {
  346. INT iRes = DPA_AppendPtr( _hdpaTemplates, (LPVOID)pTemplateInfo );
  347. if (iRes == -1)
  348. {
  349. //
  350. // The item was not added to the DPA, delete it...
  351. //
  352. delete pTemplateInfo;
  353. hr = E_FAIL;
  354. }
  355. }
  356. }
  357. WIA_RETURN_HR(hr);
  358. }
  359. LONG CPhotoTemplates::Count()
  360. {
  361. LONG lCount = 0;
  362. CAutoCriticalSection lock(_csList);
  363. if (_hdpaTemplates)
  364. {
  365. lCount = (LONG)DPA_GetPtrCount( _hdpaTemplates );
  366. }
  367. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("CPhotoTemplates::Count( returning count as %d )"),lCount));
  368. return lCount;
  369. }
  370. HRESULT CPhotoTemplates::_GetLocaleMeasurements(int *pMeasurements)
  371. {
  372. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("CPhotoTemplates::_GetLocalMeasurements()")));
  373. TCHAR szMeasure[5];
  374. if( GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IMEASURE, szMeasure, ARRAYSIZE(szMeasure)) )
  375. {
  376. *pMeasurements = (TEXT('0') == szMeasure[0] ? MEASURE_METRIC : MEASURE_US);
  377. return S_OK;
  378. }
  379. WIA_ERROR((TEXT("GetLocaleInfo failed w/GLE = %d"),GetLastError()));
  380. return E_FAIL;
  381. }
  382. HRESULT CPhotoTemplates::_BuildLocaleQueryString(int Measure, LPTSTR pStr, UINT cch)
  383. {
  384. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("CPhotoTemplates::_BuildLocaleQueryString()")));
  385. CSimpleString strPatternString;
  386. HRESULT hr = E_INVALIDARG;
  387. LPCTSTR pszMeasure = MEASURE_METRIC == Measure ? TEXT("cm") :
  388. MEASURE_US == Measure ? TEXT("in") : NULL;
  389. WIA_TRACE((TEXT("pszMeasure = %s"),pszMeasure));
  390. // build simple XSL pattern query string based on the current locale measurements
  391. if (pszMeasure)
  392. {
  393. strPatternString.Format( gszPatternLocale, pszMeasure );
  394. if (pStr && (cch >= strPatternString.Length()))
  395. {
  396. hr = StringCchCopy(pStr,cch,strPatternString.String());
  397. }
  398. else
  399. {
  400. hr = E_INVALIDARG;
  401. }
  402. }
  403. else
  404. {
  405. hr = E_FAIL;
  406. }
  407. WIA_RETURN_HR(hr);
  408. }
  409. HRESULT CPhotoTemplates::_BuildGUIDQueryString(const GUID &guid, CComBSTR &bstr)
  410. {
  411. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("CPhotoTemplates::_BuildGUIDQueryString()")));
  412. TCHAR szGUID[128];
  413. HRESULT hr = StringFromGUID2(guid, szGUID, ARRAYSIZE(szGUID));
  414. if( SUCCEEDED(hr) )
  415. {
  416. TCHAR szPatternString[255];
  417. if( -1 != wnsprintf(szPatternString, ARRAYSIZE(szPatternString), gszPatternGUID, szGUID) )
  418. {
  419. bstr = szPatternString;
  420. hr = bstr ? S_OK : E_OUTOFMEMORY;
  421. }
  422. else
  423. {
  424. hr = E_FAIL;
  425. }
  426. }
  427. WIA_RETURN_HR(hr);
  428. }
  429. HRESULT CPhotoTemplates::GetTemplate(INT iIndex, CTemplateInfo ** ppTemplateInfo)
  430. {
  431. WIA_PUSH_FUNCTION_MASK((TRACE_XML,TEXT("CPhotoTemplates::GetTemplate()")));
  432. HRESULT hr = E_INVALIDARG;
  433. CAutoCriticalSection lock(_csList);
  434. if( ppTemplateInfo )
  435. {
  436. if ( _hdpaTemplates )
  437. {
  438. if (iIndex < DPA_GetPtrCount( _hdpaTemplates ))
  439. {
  440. //
  441. // Note: it's only okay to hand out pointers here because
  442. // we know that wizblob.cpp doesn't delete the CPhotoTemplates
  443. // class until all the background threads have exited, etc.
  444. //
  445. *ppTemplateInfo = (CTemplateInfo *) DPA_FastGetPtr( _hdpaTemplates, iIndex );
  446. hr = S_OK;
  447. }
  448. }
  449. else
  450. {
  451. hr = E_FAIL;
  452. }
  453. }
  454. WIA_RETURN_HR(hr);
  455. }
  456. // creates full page template info
  457. CTemplateInfo::CTemplateInfo()
  458. : _bRepeatPhotos(FALSE),
  459. _bUseThumbnailsToPrint(FALSE),
  460. _bPrintFilename(FALSE),
  461. _bCanRotate(TRUE),
  462. _bCanCrop(FALSE),
  463. _bPortrait(TRUE),
  464. _pStream(NULL)
  465. {
  466. //
  467. // Set imageable area
  468. //
  469. _rcImageableArea.left = -1;
  470. _rcImageableArea.top = -1;
  471. _rcImageableArea.right = -1;
  472. _rcImageableArea.bottom = -1;
  473. //
  474. // Set 1 item, takes up all of imageable area
  475. //
  476. RECT rcItem;
  477. rcItem.left = -1;
  478. rcItem.top = -1;
  479. rcItem.right = -1;
  480. rcItem.bottom = -1;
  481. _arrLayout.Append( rcItem );
  482. _strTitle.LoadString( IDS_FULL_PAGE_TITLE, g_hInst );
  483. _strDescription.LoadString( IDS_FULL_PAGE_DESC, g_hInst );
  484. }
  485. CTemplateInfo::CTemplateInfo( IXMLDOMElement * pTheTemplate )
  486. : _bRepeatPhotos(FALSE),
  487. _bUseThumbnailsToPrint(FALSE),
  488. _bPrintFilename(FALSE),
  489. _bCanRotate(FALSE),
  490. _bCanCrop(FALSE),
  491. _bPortrait(TRUE),
  492. _pStream(NULL)
  493. {
  494. HRESULT hr;
  495. if (pTheTemplate)
  496. {
  497. //
  498. // Make sure backing COM object doesn't go away on us...
  499. //
  500. pTheTemplate->AddRef();
  501. //
  502. // Get all the properties so we can construct an
  503. // initialized template for our list...
  504. //
  505. CComBSTR bstrGroup;
  506. CComBSTR bstrTitle;
  507. CComBSTR bstrDescription;
  508. hr = _GetAttribute( pTheTemplate, arrCommonPropNames[PROP_GROUP], bstrGroup );
  509. WIA_CHECK_HR(hr,"AddTemplate: couldn't get PROP_GROUP property");
  510. if (SUCCEEDED(hr))
  511. {
  512. _strGroup.Assign( CSimpleStringConvert::NaturalString(CSimpleStringWide(bstrGroup)) );
  513. }
  514. hr = _GetAttribute( pTheTemplate, arrCommonPropNames[PROP_TITLE], bstrTitle );
  515. WIA_CHECK_HR(hr,"AddTemplate: couldn't get PROP_TITLE property");
  516. if (SUCCEEDED(hr))
  517. {
  518. _strTitle.Assign( CSimpleStringConvert::NaturalString(CSimpleStringWide(bstrTitle)) );
  519. }
  520. hr = _GetAttribute( pTheTemplate, arrCommonPropNames[PROP_DESCRIPTION], bstrDescription );
  521. WIA_CHECK_HR(hr,"AddTemplate: couldn't get PROP_DESCRIPTION property");
  522. if (SUCCEEDED(hr))
  523. {
  524. _strDescription.Assign( CSimpleStringConvert::NaturalString(CSimpleStringWide(bstrDescription)) );
  525. }
  526. hr = _GetProp<BOOL>( pTheTemplate, arrCommonPropNames[PROP_REPEAT_PHOTOS], _bRepeatPhotos );
  527. WIA_CHECK_HR(hr,"AddTemplate: couldn't get PROP_REPEAT_PHOTOS property");
  528. hr = _GetProp<BOOL>( pTheTemplate, arrCommonPropNames[PROP_USE_THUMBNAILS_TO_PRINT], _bUseThumbnailsToPrint );
  529. WIA_CHECK_HR(hr,"AddTemplate: couldn't get PROP_USE_THUMBNAILS_TO_PRINT property");
  530. hr = _GetProp<BOOL>( pTheTemplate, arrCommonPropNames[PROP_PRINT_FILENAME], _bPrintFilename );
  531. WIA_CHECK_HR(hr,"AddTemplate: couldn't get PROP_USE_THUMBNAILS_TO_PRINT property");
  532. hr = _GetProp<BOOL>( pTheTemplate, arrCommonPropNames[PROP_CAN_ROTATE], _bCanRotate );
  533. WIA_CHECK_HR(hr,"AddTemplate: couldn't get PROP_CAN_ROTATE property");
  534. hr = _GetProp<BOOL>( pTheTemplate, arrCommonPropNames[PROP_CAN_CROP], _bCanCrop );
  535. WIA_CHECK_HR(hr,"AddTemplate: couldn't get PROP_CAN_CROP property");
  536. //
  537. // Get IStream to template preview (icon)
  538. //
  539. CComPtr<IXMLDOMElement> pImageInfo;
  540. hr = _GetChildElement(pTheTemplate, TEXT("preview-image"), &pImageInfo);
  541. WIA_CHECK_HR(hr,"_GetChildElement( preview-image ) failed");
  542. if( SUCCEEDED(hr) && pImageInfo )
  543. {
  544. CComBSTR bstrAttr;
  545. hr = _GetAttribute(pImageInfo, TEXT("url"), bstrAttr);
  546. if(SUCCEEDED(hr))
  547. {
  548. //
  549. // URL is provided - this overrides everything else
  550. //
  551. hr = CreateStreamFromURL(bstrAttr, &_pStream);
  552. WIA_CHECK_HR(hr,"CreateStreamFromURL failed!");
  553. }
  554. else
  555. {
  556. //
  557. // try getting resource info (module + resource name)
  558. //
  559. hr = _GetAttribute(pImageInfo, TEXT("res-name"), bstrAttr);
  560. if(SUCCEEDED(hr))
  561. {
  562. CComBSTR bstrModule, bstrType;
  563. LPCTSTR pszModule = SUCCEEDED(_GetAttribute(pImageInfo, TEXT("res-module"), bstrModule)) ? bstrModule : NULL;
  564. LPCTSTR pszType = SUCCEEDED(_GetAttribute(pImageInfo, TEXT("res-type"), bstrType)) ? bstrType : TEXT("HTML");
  565. //
  566. // filter out some of the standard resource types
  567. //
  568. pszType = (0 == lstrcmp(pszType, TEXT("HTML"))) ? RT_HTML :
  569. (0 == lstrcmp(pszType, TEXT("ICON"))) ? RT_ICON :
  570. (0 == lstrcmp(pszType, TEXT("BITMAP"))) ? RT_BITMAP : pszType;
  571. //
  572. // just create a memory stream on the specified resource
  573. //
  574. hr = CreateStreamFromResource(pszModule, pszType, bstrAttr, &_pStream);
  575. WIA_CHECK_HR(hr, "CreateStreamFromResource() failed");
  576. }
  577. }
  578. }
  579. //
  580. // Get the layout info for this template...
  581. //
  582. CComPtr<IXMLDOMElement> pLayoutInfo;
  583. hr = _GetChildElement( pTheTemplate, TEXT("layout"), &pLayoutInfo );
  584. WIA_CHECK_HR(hr,"_GetChildElement( layout ) failed");
  585. if (SUCCEEDED(hr) && pLayoutInfo)
  586. {
  587. //
  588. // Get imageable area for template...
  589. //
  590. CComPtr<IXMLDOMElement> pImageableArea;
  591. hr = _GetChildElement( pLayoutInfo, TEXT("imageable-area"), &pImageableArea );
  592. WIA_CHECK_HR(hr,"_GetChildElement( imageable-area ) failed");
  593. if (SUCCEEDED(hr) && pImageableArea)
  594. {
  595. ZeroMemory( &_rcImageableArea, sizeof(_rcImageableArea) );
  596. hr = _GetProp<LONG>(pImageableArea, TEXT("x"), _rcImageableArea.left);
  597. WIA_CHECK_HR(hr,"_GetProp( _rcImageableArea.left ) failed");
  598. hr = _GetProp<LONG>(pImageableArea, TEXT("y"), _rcImageableArea.top);
  599. WIA_CHECK_HR(hr,"_GetProp( _rcImageableArea.top ) failed");
  600. hr = _GetProp<LONG>(pImageableArea, TEXT("w"), _rcImageableArea.right);
  601. WIA_CHECK_HR(hr,"_GetProp( _rcImageableArea.right ) failed");
  602. hr = _GetProp<LONG>(pImageableArea, TEXT("h"), _rcImageableArea.bottom);
  603. WIA_CHECK_HR(hr,"_GetProp( _rcImageableArea.bottom ) failed");
  604. //
  605. // Check for special case of all -1's, which
  606. // means to scale to full size of printable
  607. // area...
  608. //
  609. WIA_TRACE((TEXT("_rcImageableArea was read as (%d by %d) at (%d,%d)"),_rcImageableArea.right,_rcImageableArea.bottom,_rcImageableArea.left,_rcImageableArea.top));
  610. if ((-1 != _rcImageableArea.left) ||
  611. (-1 != _rcImageableArea.top) ||
  612. (-1 != _rcImageableArea.right) ||
  613. (-1 != _rcImageableArea.bottom))
  614. {
  615. //
  616. // convert w, h to right & bootom
  617. //
  618. _rcImageableArea.right += _rcImageableArea.left;
  619. _rcImageableArea.bottom += _rcImageableArea.top;
  620. }
  621. }
  622. //
  623. // Get individual item rectangles for this template...
  624. //
  625. CComPtr<IXMLDOMNodeList> pListLayout;
  626. hr = pLayoutInfo->selectNodes(CComBSTR(TEXT("image-def")), &pListLayout);
  627. WIA_CHECK_HR(hr,"pLayoutInfo->selectNodes( image-def ) failed");
  628. if (SUCCEEDED(hr) && pListLayout)
  629. {
  630. LONG length = 0;
  631. hr = pListLayout->get_length(&length);
  632. WIA_CHECK_HR(hr,"pListLayout->get_length() failed");
  633. if (SUCCEEDED(hr))
  634. {
  635. if (length)
  636. {
  637. RECT rc;
  638. for( long l = 0; l < length; l++ )
  639. {
  640. CComPtr<IXMLDOMNode> pNode;
  641. CComPtr<IXMLDOMElement> pItem;
  642. hr = pListLayout->get_item(l, &pNode);
  643. WIA_CHECK_HR(hr,"pListLayout->get_item() failed");
  644. if (SUCCEEDED(hr) && pNode)
  645. {
  646. hr = pNode->QueryInterface(IID_IXMLDOMElement, (void **)&pItem);
  647. WIA_CHECK_HR(hr,"pNode->QI( item XMLDOMElement )");
  648. if (SUCCEEDED(hr) && pItem)
  649. {
  650. ZeroMemory( &rc, sizeof(rc) );
  651. hr = _GetProp<LONG>(pItem, TEXT("x"), rc.left);
  652. WIA_CHECK_HR(hr,"_GetProp( item x ) failed");
  653. hr = _GetProp<LONG>(pItem, TEXT("y"), rc.top);
  654. WIA_CHECK_HR(hr,"_GetProp( item y ) failed");
  655. hr = _GetProp<LONG>(pItem, TEXT("w"), rc.right);
  656. WIA_CHECK_HR(hr,"_GetProp( item w ) failed");
  657. hr = _GetProp<LONG>(pItem, TEXT("h"), rc.bottom);
  658. WIA_CHECK_HR(hr,"_GetProp( item h ) failed");
  659. //
  660. // Check for special case of all -1's, which
  661. // means to scale to full size of printable
  662. // area...
  663. //
  664. if ((-1 != rc.left) ||
  665. (-1 != rc.top) ||
  666. (-1 != rc.right) ||
  667. (-1 != rc.bottom))
  668. {
  669. // convert w, h to right & bootom
  670. rc.right += rc.left;
  671. rc.bottom += rc.top;
  672. }
  673. //
  674. // insert the image definition
  675. //
  676. if (-1 == _arrLayout.Append( rc ))
  677. {
  678. WIA_ERROR((TEXT("Error adding item rectangle to list")));
  679. }
  680. }
  681. }
  682. }
  683. }
  684. else
  685. {
  686. WIA_ERROR((TEXT("pListLayout->get_length() returned 0 length!")));
  687. }
  688. }
  689. }
  690. }
  691. //
  692. // Let go of backing COM object...
  693. //
  694. pTheTemplate->Release();
  695. }
  696. }
  697. CTemplateInfo::~CTemplateInfo()
  698. {
  699. if (_pStream)
  700. {
  701. _pStream->Release();
  702. _pStream = NULL;
  703. }
  704. }
  705. static void RotateHelper(RECT * pRect, int nNewImageWidth, int nNewImageHeight, BOOL bClockwise)
  706. {
  707. //
  708. // If we don't have a valid pointer, or all the coords are -1, then bail.
  709. // The -1 case denotes use all the area, and we don't want to muck with
  710. // that...
  711. //
  712. if (!pRect || ((pRect->left==-1) && (pRect->top==-1) && (pRect->right==-1) && (pRect->bottom==-1)))
  713. {
  714. return;
  715. }
  716. WIA_PUSH_FUNCTION_MASK((TRACE_TEMPLATE,TEXT("RotateHelper( pRect(%d,%d,%d,%d) nNewImageWidth=%d nImageHeight=%d bClockwise=%d )"),pRect->left,pRect->top,pRect->right,pRect->bottom,nNewImageWidth,nNewImageHeight,bClockwise));
  717. //
  718. // The incoming data is going to be 2 points -- first is the upper left
  719. // coord of the original rectangle. The second represents the
  720. // the width and the height. The first coord needs to be rotated
  721. // 90 degrees, and then put back to the upper left of the rectangle.
  722. //
  723. //
  724. // The width and the height need to be flipped.
  725. //
  726. int nNewItemWidth = pRect->bottom - pRect->top;
  727. int nNewItemHeight = pRect->right - pRect->left;
  728. int nNewX, nNewY;
  729. if (bClockwise)
  730. {
  731. nNewX = nNewImageWidth - pRect->bottom;
  732. nNewY = pRect->left;
  733. }
  734. else
  735. {
  736. nNewX = pRect->top;
  737. nNewY = nNewImageHeight - pRect->right;
  738. }
  739. pRect->left = nNewX;
  740. pRect->top = nNewY;
  741. pRect->right = nNewX + nNewItemWidth;
  742. pRect->bottom = nNewY + nNewItemHeight;
  743. WIA_TRACE((TEXT("On Exit: pRect(%d,%d,%d,%d)"),pRect->left,pRect->top,pRect->right,pRect->bottom));
  744. }
  745. HRESULT CTemplateInfo::RotateForLandscape()
  746. {
  747. HRESULT hr = S_OK;
  748. WIA_PUSH_FUNCTION_MASK((TRACE_TEMPLATE,TEXT("CTemplateInfo::RotateForLandscape()")));
  749. CAutoCriticalSection lock(_cs);
  750. if (_bPortrait)
  751. {
  752. //
  753. // We only want to change this if it's a defined rect
  754. // (i.e, not "use all area")
  755. //
  756. if ((_rcImageableArea.left != -1) &&
  757. (_rcImageableArea.top != -1) &&
  758. (_rcImageableArea.right != -1) &&
  759. (_rcImageableArea.bottom != -1))
  760. {
  761. //
  762. // The imageable area will just be a flip of width & height.
  763. // this relies on the fact the imageable area is always
  764. // described in terms of width & height (i.e., top & left
  765. // are always 0 in the RECT structure).
  766. //
  767. LONG oldWidth = _rcImageableArea.right;
  768. _rcImageableArea.right = _rcImageableArea.bottom;
  769. _rcImageableArea.bottom = oldWidth;
  770. }
  771. //
  772. // Now, map all the points for each item in the layout...
  773. //
  774. RECT * pRect;
  775. for (INT i=0; i < _arrLayout.Count(); i++)
  776. {
  777. pRect = &_arrLayout[i];
  778. RotateHelper( pRect, _rcImageableArea.right, _rcImageableArea.bottom, FALSE );
  779. }
  780. _bPortrait = FALSE;
  781. }
  782. else
  783. {
  784. WIA_TRACE((TEXT("Already in landscape mode, doing nothing...")));
  785. }
  786. WIA_RETURN_HR(hr);
  787. }
  788. HRESULT CTemplateInfo::RotateForPortrait()
  789. {
  790. HRESULT hr = S_OK;
  791. WIA_PUSH_FUNCTION_MASK((TRACE_TEMPLATE,TEXT("CTemplateInfo::RotateForPortrait()")));
  792. CAutoCriticalSection lock(_cs);
  793. if (!_bPortrait)
  794. {
  795. //
  796. // The imageable area will just be a flip of width & height.
  797. // this relies on the fact the imageable area is always
  798. // described in terms of width & height (i.e., top & left
  799. // are always 0 in the RECT structure).
  800. //
  801. LONG oldWidth = _rcImageableArea.right;
  802. _rcImageableArea.right = _rcImageableArea.bottom;
  803. _rcImageableArea.bottom = oldWidth;
  804. //
  805. // Now, map all the points for each item in the layout...
  806. //
  807. RECT * pRect;
  808. for (INT i=0; i < _arrLayout.Count(); i++)
  809. {
  810. pRect = &_arrLayout[i];
  811. RotateHelper( pRect, _rcImageableArea.right, _rcImageableArea.bottom, TRUE );
  812. }
  813. _bPortrait = TRUE;
  814. }
  815. else
  816. {
  817. WIA_TRACE((TEXT("Already in portrait mode, doing nothing...")));
  818. }
  819. WIA_RETURN_HR(hr);
  820. }