UIMLView.cpp 17 KB

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