UIMLView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. #include "UIMLView.h"
  2. #include "XML.h"
  3. #include "TextFeld.h"
  4. #include "Knopf.h"
  5. #include "Tabelle.h"
  6. #include "Schrift.h"
  7. #include "Bildschirm.h"
  8. using namespace Framework;
  9. // Erstellt eine UIML View
  10. UIMLView::UIMLView()
  11. : ZeichnungHintergrund()
  12. {
  13. members = new Trie< Zeichnung >();
  14. dom = 0;
  15. nextId = 0;
  16. memset( &init, 0, sizeof( UIInit ) );
  17. }
  18. // Erstellt eine UIML View zu einem UIML Text
  19. // uiml: Ein xml element gemät des ksg uiml standarts
  20. UIMLView::UIMLView( XML::Element *uiml, UIInit &init )
  21. : ZeichnungHintergrund()
  22. {
  23. this->init = init;
  24. members = new Trie< Zeichnung >();
  25. dom = 0;
  26. nextId = 0;
  27. setUIML( uiml );
  28. }
  29. // Erstellt eine UIML View zu einem UIML Text
  30. // uiml: Ein xml text gemät des ksg uiml standarts
  31. UIMLView::UIMLView( Text uiml, UIInit &init )
  32. {
  33. this->init = init;
  34. members = new Trie< Zeichnung >();
  35. dom = 0;
  36. setUIML( uiml );
  37. }
  38. UIMLView::~UIMLView()
  39. {
  40. if( dom )
  41. dom->release();
  42. members->release();
  43. }
  44. void UIMLView::parseTable( Iterator<XML::Element*> childs, ObjTabelle *table )
  45. {
  46. for( auto i = childs; i; i++ )
  47. {
  48. Text id;
  49. if( i->hasAttribute( "id" ) )
  50. id = i->getAttributeValue( "id" );
  51. else
  52. id = Text( "_" ) += nextId++;
  53. if( i->getName().istGleich( "tr" ) )
  54. {
  55. table->addZeile( id );
  56. Text line = id;
  57. int c = 1;
  58. for( auto j = i->getChilds(); j; j++ )
  59. {
  60. Zeichnung *z = parseElement( j._ );
  61. if( table->getSpaltenAnzahl() < c )
  62. table->addSpalte( Text( c - 1 ) );
  63. if( z )
  64. table->setZeichnungZ( c - 1, line, z->getThis() );
  65. c++;
  66. }
  67. }
  68. }
  69. }
  70. Zeichnung *UIMLView::parseElement( XML::Element *e )
  71. {
  72. Text id;
  73. if( e->hasAttribute( "id" ) )
  74. id = e->getAttributeValue( "id" );
  75. else
  76. {
  77. id = Text( "_" ) += nextId++;
  78. e->setAttribute( "id", id );
  79. }
  80. Zeichnung *z = members->z( id );
  81. if( !z )
  82. {
  83. // precompute attributes
  84. if( e->hasAttribute( "margin" ) )
  85. {
  86. Text m = e->getAttributeValue( "margin" );
  87. if( !e->hasAttribute( "margin-left" ) )
  88. e->setAttribute( "margin-left", m );
  89. if( !e->hasAttribute( "margin-top" ) )
  90. e->setAttribute( "margin-top", m );
  91. if( !e->hasAttribute( "margin-right" ) )
  92. e->setAttribute( "margin-right", m );
  93. if( !e->hasAttribute( "margin-bottom" ) )
  94. e->setAttribute( "margin-bottom", m );
  95. }
  96. if( e->hasAttribute( "class" ) )
  97. {
  98. Text c = e->getAttributeValue( "class" );
  99. while( 1 )
  100. {
  101. Text *t;
  102. if( c.hat( "," ) )
  103. t = c.getTeilText( 0, c.positionVon( ',' ) );
  104. else
  105. t = new Text( c );
  106. XML::Editor ce = dom->selectChildsByName( "class" ).whereAttributeEquals( "id", *t );
  107. for( auto i = ce.getIterator(); i; i++ )
  108. {
  109. for( auto j = i->getAttributeNames(), k = i->getAttributeValues(); j && k; j++, k++ )
  110. {
  111. if( !e->hasAttribute( j->getText() ) )
  112. e->setAttribute( j->getText(), i->getText() );
  113. }
  114. }
  115. t->release();
  116. if( c.hat( "," ) )
  117. c.remove( 0, c.positionVon( ',' + 1 ) );
  118. else
  119. break;
  120. }
  121. }
  122. if( e->hasAttribute( "text-align" ) )
  123. {
  124. if( !e->hasAttribute( "text-align-horizontal" ) )
  125. e->setAttribute( "text-align-horizontal", e->getAttributeValue( "text-align" ) );
  126. if( !e->hasAttribute( "text-align-vertical" ) )
  127. e->setAttribute( "text-align-vertical", e->getAttributeValue( "text-align" ) );
  128. }
  129. // create objects
  130. if( e->getName().istGleich( "textfield" ) ||
  131. e->getName().istGleich( "text" ) ||
  132. e->getName().istGleich( "textarea" ) )
  133. {
  134. TextFeld *t = init.createTextFeld( init.initParam );
  135. if( e->getName().istGleich( "textfield" ) )
  136. t->setStyle( TextFeld::Style::TextFeld );
  137. if( e->getName().istGleich( "text" ) )
  138. t->setStyle( TextFeld::Style::Text );
  139. if( e->getName().istGleich( "textarea" ) )
  140. t->setStyle( TextFeld::Style::TextGebiet );
  141. t->setText( e->getText() );
  142. if( e->hasAttribute( "font-size" ) )
  143. t->setSchriftSize( (int)e->getAttributeValue( "font-size" ) );
  144. if( e->hasAttribute( "text-align-horizontal" ) )
  145. {
  146. if( e->getAttributeValue( "text-align-horizontal" ).istGleich( "center" ) )
  147. t->addStyle( TextFeld::Style::HCenter );
  148. }
  149. if( e->hasAttribute( "text-align-vertical" ) )
  150. {
  151. if( e->getAttributeValue( "text-align-vertical" ).istGleich( "center" ) )
  152. t->addStyle( TextFeld::Style::VCenter );
  153. }
  154. z = t;
  155. }
  156. if( e->getName().istGleich( "button" ) )
  157. {
  158. Knopf *k = init.createKnopf( init.initParam );
  159. k->setText( e->getText() );
  160. if( e->hasAttribute( "font-size" ) )
  161. k->setSchriftSize( (int)e->getAttributeValue( "font-size" ) );
  162. z = k;
  163. }
  164. if( e->getName().istGleich( "table" ) )
  165. {
  166. ObjTabelle *t = init.createObjTabelle( init.initParam );
  167. parseTable( e->getChilds(), t );
  168. z = t;
  169. }
  170. // add general attributes
  171. if( z && e->hasAttribute( "tooltip" ) )
  172. z->setToolTipText( e->getAttributeValue( "tooltip" ), init.initParam.bildschirm );
  173. if( z && e->hasAttribute( "style" ) )
  174. z->setStyle( (__int64)e->getAttributeValue( "style" ) );
  175. if( z )
  176. members->set( id, z );
  177. }
  178. return z;
  179. }
  180. void UIMLView::layout( XML::Element *e, Zeichnung *parent )
  181. {
  182. Text id = e->getAttributeValue( "id" );
  183. Zeichnung *z = members->z( id );
  184. if( z )
  185. {
  186. int width = z->getBreite();
  187. int height = z->getHeight();
  188. if( e->hasAttribute( "width" ) )
  189. {
  190. Text w = e->getAttributeValue( "width" );
  191. width = w;
  192. if( w.getText()[ w.getLength() - 1 ] == '%' )
  193. width = (int)( ( parent->getBreite() / 100.0 ) * width );
  194. }
  195. if( e->hasAttribute( "height" ) )
  196. {
  197. Text h = e->getAttributeValue( "height" );
  198. height = h;
  199. if( h.getText()[ h.getLength() - 1 ] == '%' )
  200. height = (int)( ( parent->getHeight() / 100.0 ) * height );
  201. }
  202. z->setSize( width, height );
  203. if( e->hasAttribute( "align-left" ) )
  204. {
  205. Text la = e->getAttributeValue( "align-left" );
  206. int x;
  207. if( la.istGleich( "start" ) )
  208. x = 0;
  209. else if( la.istGleich( "end" ) )
  210. x = parent->getBreite();
  211. else
  212. {
  213. XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", la );
  214. for( auto i = ed.getIterator(); i; i++ )
  215. layout( i, parent );
  216. Zeichnung *laz = members->z( la );
  217. if( laz )
  218. x = laz->getX() + laz->getBreite();
  219. }
  220. if( e->hasAttribute( "margin-left" ) )
  221. {
  222. Text mt = e->getAttributeValue( "margin-left" );
  223. int m = mt;
  224. if( mt.getText()[ mt.getLength() - 1 ] == '%' )
  225. m = (int)( ( parent->getBreite() / 100.0 ) * m );
  226. x += m;
  227. }
  228. z->setX( x );
  229. }
  230. else if( e->hasAttribute( "align-right" ) )
  231. {
  232. Text ra = e->getAttributeValue( "align-right" );
  233. int x;
  234. if( ra.istGleich( "start" ) )
  235. x = -z->getBreite();
  236. else if( ra.istGleich( "end" ) )
  237. x = parent->getBreite() - z->getBreite();
  238. else
  239. {
  240. XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", ra );
  241. for( auto i = ed.getIterator(); i; i++ )
  242. layout( i, parent );
  243. Zeichnung *raz = members->z( ra );
  244. if( raz )
  245. x = raz->getX() - z->getBreite();
  246. }
  247. if( e->hasAttribute( "margin-right" ) )
  248. {
  249. Text mt = e->getAttributeValue( "margin-right" );
  250. int m = mt;
  251. if( mt.getText()[ mt.getLength() - 1 ] == '%' )
  252. m = (int)( ( parent->getBreite() / 100.0 ) * m );
  253. x -= m;
  254. }
  255. z->setX( x );
  256. }
  257. if( e->hasAttribute( "align-top" ) )
  258. {
  259. Text ta = e->getAttributeValue( "align-top" );
  260. int y;
  261. if( ta.istGleich( "start" ) )
  262. y = 0;
  263. else if( ta.istGleich( "end" ) )
  264. y = parent->getHeight();
  265. else
  266. {
  267. XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", ta );
  268. for( auto i = ed.getIterator(); i; i++ )
  269. layout( i, parent );
  270. Zeichnung *taz = members->z( ta );
  271. if( taz )
  272. y = taz->getY() + taz->getHeight();
  273. }
  274. if( e->hasAttribute( "margin-top" ) )
  275. {
  276. Text mt = e->getAttributeValue( "margin-top" );
  277. int m = mt;
  278. if( mt.getText()[ mt.getLength() - 1 ] == '%' )
  279. m = (int)( ( parent->getHeight() / 100.0 ) * m );
  280. y += m;
  281. }
  282. z->setY( y );
  283. }
  284. else if( e->hasAttribute( "align-bottom" ) )
  285. {
  286. Text ba = e->getAttributeValue( "align-bottom" );
  287. int y;
  288. if( ba.istGleich( "start" ) )
  289. y = -z->getHeight();
  290. else if( ba.istGleich( "end" ) )
  291. y = parent->getHeight() - z->getHeight();
  292. else
  293. {
  294. XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", ba );
  295. for( auto i = ed.getIterator(); i; i++ )
  296. layout( i, parent );
  297. Zeichnung *baz = members->z( ba );
  298. if( baz )
  299. y = baz->getY() - z->getHeight();
  300. }
  301. if( e->hasAttribute( "margin-bottom" ) )
  302. {
  303. Text mt = e->getAttributeValue( "margin-bottom" );
  304. int m = mt;
  305. if( mt.getText()[ mt.getLength() - 1 ] == '%' )
  306. m = (int)( ( parent->getHeight() / 100.0 ) * m );
  307. y -= m;
  308. }
  309. z->setY( y );
  310. }
  311. int x = z->getX();
  312. int y = z->getY();
  313. if( e->hasAttribute( "x" ) )
  314. {
  315. Text xt = e->getAttributeValue( "x" );
  316. x = xt;
  317. if( xt.getText()[ xt.getLength() - 1 ] == '%' )
  318. x = (int)( ( parent->getBreite() / 100.0 ) * x );
  319. }
  320. if( e->hasAttribute( "y" ) )
  321. {
  322. Text yt = e->getAttributeValue( "y" );
  323. y = yt;
  324. if( yt.getText()[ yt.getLength() - 1 ] == '%' )
  325. y = (int)( ( parent->getHeight() / 100.0 ) * y );
  326. }
  327. z->setPosition( x, y );
  328. }
  329. // recursive layout
  330. for( auto i = e->getChilds(); i; i++ )
  331. layout( i, z ? z : parent );
  332. if( z )
  333. {
  334. if( e->getName().istGleich( "table" ) )
  335. {
  336. ObjTabelle *objT = (ObjTabelle*)z;
  337. if( objT->getZeilenAnzahl() > 0 )
  338. {
  339. for( int i = 0; i < objT->getSpaltenAnzahl(); i++ )
  340. {
  341. if( objT->zZeichnung( i, 0 ) )
  342. objT->setSpaltenBreite( i, objT->zZeichnung( i, 0 )->getBreite() );
  343. }
  344. }
  345. }
  346. }
  347. }
  348. // setzt den inhalt der view
  349. // uiml: Ein xml element gemät des ksg uiml standarts
  350. void UIMLView::setUIML( XML::Element *uiml )
  351. {
  352. if( dom )
  353. dom->release();
  354. dom = uiml;
  355. members->leeren();
  356. nextId = 0;
  357. if( dom )
  358. {
  359. for( auto i = dom->getChilds(); i; i++ )
  360. {
  361. parseElement( i._ );
  362. }
  363. }
  364. }
  365. // setzt den inhalt der view
  366. // uiml: Ein xml text gemät des ksg uiml standarts
  367. void UIMLView::setUIML( Text uiml )
  368. {
  369. setUIML( new XML::Element( uiml ) );
  370. }
  371. // Gibt eine zeichnung zurück, welche in uiml eine bestimmte id hat
  372. // id: die id der Zeichnung
  373. Zeichnung *UIMLView::zZeichnung( Text id )
  374. {
  375. return members->z( id );
  376. }
  377. // aktualisiert größe und position aller Zeichnungen gemäß den spezifikationen in UIML
  378. void UIMLView::layout()
  379. {
  380. if( dom )
  381. {
  382. for( auto i = dom->getChilds(); i; i++ )
  383. {
  384. layout( i._, this );
  385. }
  386. }
  387. }
  388. // Verarbeitet ein Maus Ereignis. Wird vom Framework automatisch aufgerufen.
  389. // me: Das Ereignis
  390. void UIMLView::doMausEreignis( MausEreignis &me )
  391. {
  392. bool verarbeitet = me.verarbeitet;
  393. ZeichnungHintergrund::doMausEreignis( me );
  394. me.verarbeitet = verarbeitet;
  395. if( dom )
  396. {
  397. for( auto i = dom->getChilds(); i; i++ )
  398. { // TODO render elements backwards
  399. Zeichnung *z = members->z( i->getAttributeValue( "id" ) );
  400. if( z )
  401. z->doMausEreignis( me );
  402. }
  403. }
  404. }
  405. // Verarbeitet ein Tastatur Ereignis. Wird vom Framework automatisch aufgerufen
  406. // te: Das Ereignis
  407. void UIMLView::doTastaturEreignis( TastaturEreignis &te )
  408. {
  409. bool verarbeitet = te.verarbeitet;
  410. ZeichnungHintergrund::doTastaturEreignis( te );
  411. te.verarbeitet = verarbeitet;
  412. if( dom )
  413. {
  414. for( auto i = dom->getChilds(); i; i++ )
  415. { // TODO render elements backwards
  416. Zeichnung *z = members->z( i->getAttributeValue( "id" ) );
  417. if( z )
  418. z->doTastaturEreignis( te );
  419. }
  420. }
  421. }
  422. // Updated den Zeichenhintergrund
  423. // tickVal: Die vergangene Zeit in Sekunden, die seit dem Letzten Aufruf dieser Funktion verstrichen ist
  424. // return: 1, wenn das Bild neu gezeichnet werden muss. 0 sonnst
  425. bool UIMLView::tick( double tickVal )
  426. {
  427. if( dom )
  428. {
  429. for( auto i = dom->getChilds(); i; i++ )
  430. { // TODO render elements backwards
  431. Zeichnung *z = members->z( i->getAttributeValue( "id" ) );
  432. if( z )
  433. rend |= z->tick( tickVal );
  434. }
  435. }
  436. return ZeichnungHintergrund::tick( tickVal );
  437. }
  438. // Zeichnet den Hintergrund eines Zeichnunges nach rObj
  439. void UIMLView::render( Bild &rObj )
  440. {
  441. ZeichnungHintergrund::render( rObj );
  442. if( dom )
  443. {
  444. for( auto i = dom->getChilds(); i; i++ )
  445. { // TODO render elements backwards
  446. Zeichnung *z = members->z( i->getAttributeValue( "id" ) );
  447. if( z )
  448. z->render( rObj );
  449. }
  450. }
  451. }