UIMLView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. // create objects
  123. if( e->getName().istGleich( "textfield" ) ||
  124. e->getName().istGleich( "text" ) ||
  125. e->getName().istGleich( "textarea" ) )
  126. {
  127. TextFeld *t = init.createTextFeld( init.initParam );
  128. if( e->getName().istGleich( "textfield" ) )
  129. t->setStyle( TextFeld::Style::TextFeld );
  130. if( e->getName().istGleich( "text" ) )
  131. t->setStyle( TextFeld::Style::Text );
  132. if( e->getName().istGleich( "textarea" ) )
  133. t->setStyle( TextFeld::Style::TextGebiet );
  134. t->setText( e->getText() );
  135. if( e->hasAttribute( "font-size" ) )
  136. t->setSchriftSize( (int)e->getAttributeValue( "font-size" ) );
  137. z = t;
  138. }
  139. if( e->getName().istGleich( "button" ) )
  140. {
  141. Knopf *k = init.createKnopf( init.initParam );
  142. k->setText( e->getText() );
  143. if( e->hasAttribute( "font-size" ) )
  144. k->setSchriftSize( (int)e->getAttributeValue( "font-size" ) );
  145. z = k;
  146. }
  147. if( e->getName().istGleich( "table" ) )
  148. {
  149. ObjTabelle *t = init.createObjTabelle( init.initParam );
  150. parseTable( e->getChilds(), t );
  151. z = t;
  152. }
  153. // add general attributes
  154. if( z && e->hasAttribute( "tooltip" ) )
  155. z->setToolTipText( e->getAttributeValue( "tooltip" ), init.initParam.bildschirm );
  156. if( z && e->hasAttribute( "style" ) )
  157. z->setStyle( (__int64)e->getAttributeValue( "style" ) );
  158. if( z )
  159. members->set( id, z );
  160. }
  161. return z;
  162. }
  163. void UIMLView::layout( XML::Element *e )
  164. {
  165. Text id = e->getAttributeValue( "id" );
  166. Zeichnung *z = members->z( id );
  167. if( z )
  168. {
  169. int width = z->getBreite();
  170. int height = z->getHeight();
  171. if( e->hasAttribute( "width" ) )
  172. {
  173. Text w = e->getAttributeValue( "width" );
  174. width = w;
  175. if( w.getText()[ w.getLength() - 1 ] == '%' )
  176. width = ( getBreite() / 100 ) * width;
  177. }
  178. if( e->hasAttribute( "height" ) )
  179. {
  180. Text h = e->getAttributeValue( "height" );
  181. height = h;
  182. if( h.getText()[ h.getLength() - 1 ] == '%' )
  183. height = ( getHeight() / 100 ) * height;
  184. }
  185. z->setSize( width, height );
  186. if( e->hasAttribute( "align-left" ) )
  187. {
  188. Text la = e->getAttributeValue( "align-left" );
  189. int x;
  190. if( la.istGleich( "start" ) )
  191. x = 0;
  192. else if( la.istGleich( "end" ) )
  193. x = getBreite();
  194. else
  195. {
  196. XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", la );
  197. for( auto i = ed.getIterator(); i; i++ )
  198. layout( i );
  199. Zeichnung *laz = members->z( la );
  200. if( laz )
  201. x = laz->getX() + laz->getBreite();
  202. }
  203. if( e->hasAttribute( "margin-left" ) )
  204. {
  205. Text mt = e->getAttributeValue( "margin-left" );
  206. int m = mt;
  207. if( mt.getText()[ mt.getLength() - 1 ] == '%' )
  208. m = ( getBreite() / 100 ) * m;
  209. x += m;
  210. }
  211. z->setX( x );
  212. }
  213. else if( e->hasAttribute( "align-right" ) )
  214. {
  215. Text ra = e->getAttributeValue( "align-right" );
  216. int x;
  217. if( ra.istGleich( "start" ) )
  218. x = -z->getBreite();
  219. else if( ra.istGleich( "end" ) )
  220. x = getBreite() - z->getBreite();
  221. else
  222. {
  223. XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", ra );
  224. for( auto i = ed.getIterator(); i; i++ )
  225. layout( i );
  226. Zeichnung *raz = members->z( ra );
  227. if( raz )
  228. x = raz->getX() - z->getBreite();
  229. }
  230. if( e->hasAttribute( "margin-right" ) )
  231. {
  232. Text mt = e->getAttributeValue( "margin-right" );
  233. int m = mt;
  234. if( mt.getText()[ mt.getLength() - 1 ] == '%' )
  235. m = ( getBreite() / 100 ) * m;
  236. x -= m;
  237. }
  238. z->setX( x );
  239. }
  240. if( e->hasAttribute( "align-top" ) )
  241. {
  242. Text ta = e->getAttributeValue( "align-top" );
  243. int y;
  244. if( ta.istGleich( "start" ) )
  245. y = 0;
  246. else if( ta.istGleich( "end" ) )
  247. y = getHeight();
  248. else
  249. {
  250. XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", ta );
  251. for( auto i = ed.getIterator(); i; i++ )
  252. layout( i );
  253. Zeichnung *taz = members->z( ta );
  254. if( taz )
  255. y = taz->getY() + taz->getHeight();
  256. }
  257. if( e->hasAttribute( "margin-top" ) )
  258. {
  259. Text mt = e->getAttributeValue( "margin-top" );
  260. int m = mt;
  261. if( mt.getText()[ mt.getLength() - 1 ] == '%' )
  262. m = ( getHeight() / 100 ) * m;
  263. y += m;
  264. }
  265. z->setY( y );
  266. }
  267. else if( e->hasAttribute( "align-bottom" ) )
  268. {
  269. Text ba = e->getAttributeValue( "align-bottom" );
  270. int y;
  271. if( ba.istGleich( "start" ) )
  272. y = -z->getHeight();
  273. else if( ba.istGleich( "end" ) )
  274. y = getHeight() - z->getHeight();
  275. else
  276. {
  277. XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", ba );
  278. for( auto i = ed.getIterator(); i; i++ )
  279. layout( i );
  280. Zeichnung *baz = members->z( ba );
  281. if( baz )
  282. y = baz->getY() - z->getHeight();
  283. }
  284. if( e->hasAttribute( "margin-bottom" ) )
  285. {
  286. Text mt = e->getAttributeValue( "margin-bottom" );
  287. int m = mt;
  288. if( mt.getText()[ mt.getLength() - 1 ] == '%' )
  289. m = ( getHeight() / 100 ) * m;
  290. y -= m;
  291. }
  292. z->setY( y );
  293. }
  294. int x = z->getX();
  295. int y = z->getY();
  296. if( e->hasAttribute( "x" ) )
  297. {
  298. Text xt = e->getAttributeValue( "x" );
  299. x = xt;
  300. if( xt.getText()[ xt.getLength() - 1 ] == '%' )
  301. x = ( getBreite() / 100 ) * x;
  302. }
  303. if( e->hasAttribute( "y" ) )
  304. {
  305. Text yt = e->getAttributeValue( "y" );
  306. y = yt;
  307. if( yt.getText()[ yt.getLength() - 1 ] == '%' )
  308. y = ( getHeight() / 100 ) * y;
  309. }
  310. z->setPosition( x, y );
  311. }
  312. }
  313. // setzt den inhalt der view
  314. // uiml: Ein xml element gemät des ksg uiml standarts
  315. void UIMLView::setUIML( XML::Element *uiml )
  316. {
  317. if( dom )
  318. dom->release();
  319. dom = uiml;
  320. members->leeren();
  321. nextId = 0;
  322. if( dom )
  323. {
  324. for( auto i = dom->getChilds(); i; i++ )
  325. {
  326. parseElement( i._ );
  327. }
  328. }
  329. }
  330. // setzt den inhalt der view
  331. // uiml: Ein xml text gemät des ksg uiml standarts
  332. void UIMLView::setUIML( Text uiml )
  333. {
  334. setUIML( new XML::Element( uiml ) );
  335. }
  336. // Gibt eine zeichnung zurück, welche in uiml eine bestimmte id hat
  337. // id: die id der Zeichnung
  338. Zeichnung *UIMLView::zZeichnung( Text id )
  339. {
  340. return members->z( id );
  341. }
  342. // aktualisiert größe und position aller Zeichnungen gemäß den spezifikationen in UIML
  343. void UIMLView::layout()
  344. {
  345. if( dom )
  346. {
  347. for( auto i = dom->getChilds(); i; i++ )
  348. {
  349. layout( i._ );
  350. }
  351. }
  352. }
  353. // Verarbeitet ein Maus Ereignis. Wird vom Framework automatisch aufgerufen.
  354. // me: Das Ereignis
  355. void UIMLView::doMausEreignis( MausEreignis &me )
  356. {
  357. bool verarbeitet = me.verarbeitet;
  358. ZeichnungHintergrund::doMausEreignis( me );
  359. me.verarbeitet = verarbeitet;
  360. if( dom )
  361. {
  362. for( auto i = dom->getChilds(); i; i++ )
  363. { // TODO render elements backwards
  364. Zeichnung *z = members->z( i->getAttributeValue( "id" ) );
  365. if( z )
  366. z->doMausEreignis( me );
  367. }
  368. }
  369. }
  370. // Verarbeitet ein Tastatur Ereignis. Wird vom Framework automatisch aufgerufen
  371. // te: Das Ereignis
  372. void UIMLView::doTastaturEreignis( TastaturEreignis &te )
  373. {
  374. bool verarbeitet = te.verarbeitet;
  375. ZeichnungHintergrund::doTastaturEreignis( te );
  376. te.verarbeitet = verarbeitet;
  377. if( dom )
  378. {
  379. for( auto i = dom->getChilds(); i; i++ )
  380. { // TODO render elements backwards
  381. Zeichnung *z = members->z( i->getAttributeValue( "id" ) );
  382. if( z )
  383. z->doTastaturEreignis( te );
  384. }
  385. }
  386. }
  387. // Updated den Zeichenhintergrund
  388. // tickVal: Die vergangene Zeit in Sekunden, die seit dem Letzten Aufruf dieser Funktion verstrichen ist
  389. // return: 1, wenn das Bild neu gezeichnet werden muss. 0 sonnst
  390. bool UIMLView::tick( double tickVal )
  391. {
  392. if( dom )
  393. {
  394. for( auto i = dom->getChilds(); i; i++ )
  395. { // TODO render elements backwards
  396. Zeichnung *z = members->z( i->getAttributeValue( "id" ) );
  397. if( z )
  398. rend |= z->tick( tickVal );
  399. }
  400. }
  401. return ZeichnungHintergrund::tick( tickVal );
  402. }
  403. // Zeichnet den Hintergrund eines Zeichnunges nach rObj
  404. void UIMLView::render( Bild &rObj )
  405. {
  406. ZeichnungHintergrund::render( rObj );
  407. if( dom )
  408. {
  409. for( auto i = dom->getChilds(); i; i++ )
  410. { // TODO render elements backwards
  411. Zeichnung *z = members->z( i->getAttributeValue( "id" ) );
  412. if( z )
  413. z->render( rObj );
  414. }
  415. }
  416. }