UIMLView.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. if( e->hasAttribute( "title-height" ) )
  190. f->zTTextFeld()->setSize( f->zTTextFeld()->getBreite(), e->getAttributeValue( "title-height" ) );
  191. z = f;
  192. }
  193. // add general attributes
  194. if( z && e->hasAttribute( "tooltip" ) )
  195. z->setToolTipText( e->getAttributeValue( "tooltip" ), init.initParam.bildschirm );
  196. if( z && e->hasAttribute( "style" ) )
  197. z->setStyle( (__int64)e->getAttributeValue( "style" ) );
  198. if( z && e->hasAttribute( "hidden" ) )
  199. z->removeStyle( Zeichnung::Style::Sichtbar );
  200. if( z && e->hasAttribute( "disabled" ) )
  201. z->removeStyle( Zeichnung::Style::Erlaubt );
  202. if( z )
  203. members->set( id, z );
  204. }
  205. return z;
  206. }
  207. void UIMLView::layout( XML::Element *e, int pWidth, int pHeight )
  208. {
  209. Text id = e->getAttributeValue( "id" );
  210. Zeichnung *z = members->z( id );
  211. if( z )
  212. {
  213. int width = z->getBreite();
  214. int height = z->getHeight();
  215. if( e->hasAttribute( "width" ) )
  216. {
  217. Text w = e->getAttributeValue( "width" );
  218. width = w;
  219. if( w.getText()[ w.getLength() - 1 ] == '%' )
  220. width = (int)( ( pWidth / 100.0 ) * width );
  221. }
  222. if( e->hasAttribute( "height" ) )
  223. {
  224. Text h = e->getAttributeValue( "height" );
  225. height = h;
  226. if( h.getText()[ h.getLength() - 1 ] == '%' )
  227. height = (int)( ( pHeight / 100.0 ) * height );
  228. }
  229. z->setSize( width, height );
  230. if( e->hasAttribute( "align-left" ) )
  231. {
  232. Text la = e->getAttributeValue( "align-left" );
  233. int x;
  234. if( la.istGleich( "start" ) )
  235. x = 0;
  236. else if( la.istGleich( "end" ) )
  237. x = pWidth;
  238. else
  239. {
  240. XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", la );
  241. for( auto i = ed.getIterator(); i; i++ )
  242. layout( i, pWidth, pHeight );
  243. Zeichnung *laz = members->z( la );
  244. if( laz )
  245. x = laz->getX() + laz->getBreite();
  246. }
  247. if( e->hasAttribute( "margin-left" ) )
  248. {
  249. Text mt = e->getAttributeValue( "margin-left" );
  250. int m = mt;
  251. if( mt.getText()[ mt.getLength() - 1 ] == '%' )
  252. m = (int)( ( pWidth / 100.0 ) * m );
  253. x += m;
  254. }
  255. z->setX( x );
  256. }
  257. else if( e->hasAttribute( "align-right" ) )
  258. {
  259. Text ra = e->getAttributeValue( "align-right" );
  260. int x;
  261. if( ra.istGleich( "start" ) )
  262. x = -z->getBreite();
  263. else if( ra.istGleich( "end" ) )
  264. x = pWidth - z->getBreite();
  265. else
  266. {
  267. XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", ra );
  268. for( auto i = ed.getIterator(); i; i++ )
  269. layout( i, pWidth, pHeight );
  270. Zeichnung *raz = members->z( ra );
  271. if( raz )
  272. x = raz->getX() - z->getBreite();
  273. }
  274. if( e->hasAttribute( "margin-right" ) )
  275. {
  276. Text mt = e->getAttributeValue( "margin-right" );
  277. int m = mt;
  278. if( mt.getText()[ mt.getLength() - 1 ] == '%' )
  279. m = (int)( ( pWidth / 100.0 ) * m );
  280. x -= m;
  281. }
  282. z->setX( x );
  283. }
  284. if( e->hasAttribute( "align-top" ) )
  285. {
  286. Text ta = e->getAttributeValue( "align-top" );
  287. int y;
  288. if( ta.istGleich( "start" ) )
  289. y = 0;
  290. else if( ta.istGleich( "end" ) )
  291. y = pHeight;
  292. else
  293. {
  294. XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", ta );
  295. for( auto i = ed.getIterator(); i; i++ )
  296. layout( i, pWidth, pHeight );
  297. Zeichnung *taz = members->z( ta );
  298. if( taz )
  299. y = taz->getY() + taz->getHeight();
  300. }
  301. if( e->hasAttribute( "margin-top" ) )
  302. {
  303. Text mt = e->getAttributeValue( "margin-top" );
  304. int m = mt;
  305. if( mt.getText()[ mt.getLength() - 1 ] == '%' )
  306. m = (int)( ( pHeight / 100.0 ) * m );
  307. y += m;
  308. }
  309. z->setY( y );
  310. }
  311. else if( e->hasAttribute( "align-bottom" ) )
  312. {
  313. Text ba = e->getAttributeValue( "align-bottom" );
  314. int y;
  315. if( ba.istGleich( "start" ) )
  316. y = -z->getHeight();
  317. else if( ba.istGleich( "end" ) )
  318. y = pHeight - z->getHeight();
  319. else
  320. {
  321. XML::Editor ed = e->zParent()->selectChildsByAttribute( "id", ba );
  322. for( auto i = ed.getIterator(); i; i++ )
  323. layout( i, pWidth, pHeight );
  324. Zeichnung *baz = members->z( ba );
  325. if( baz )
  326. y = baz->getY() - z->getHeight();
  327. }
  328. if( e->hasAttribute( "margin-bottom" ) )
  329. {
  330. Text mt = e->getAttributeValue( "margin-bottom" );
  331. int m = mt;
  332. if( mt.getText()[ mt.getLength() - 1 ] == '%' )
  333. m = (int)( ( pHeight / 100.0 ) * m );
  334. y -= m;
  335. }
  336. z->setY( y );
  337. }
  338. int x = z->getX();
  339. int y = z->getY();
  340. if( e->hasAttribute( "x" ) )
  341. {
  342. Text xt = e->getAttributeValue( "x" );
  343. x = xt;
  344. if( xt.getText()[ xt.getLength() - 1 ] == '%' )
  345. x = (int)( ( pWidth / 100.0 ) * x );
  346. }
  347. if( e->hasAttribute( "y" ) )
  348. {
  349. Text yt = e->getAttributeValue( "y" );
  350. y = yt;
  351. if( yt.getText()[ yt.getLength() - 1 ] == '%' )
  352. y = (int)( ( pHeight / 100.0 ) * y );
  353. }
  354. z->setPosition( x, y );
  355. }
  356. if( z )
  357. {
  358. pWidth = z->getInnenBreite();
  359. pHeight = z->getInnenHeight();
  360. }
  361. // recursive layout
  362. for( auto i = e->getChilds(); i; i++ )
  363. layout( i, pWidth, pHeight );
  364. if( z )
  365. {
  366. if( e->getName().istGleich( "table" ) )
  367. {
  368. ObjTabelle *objT = (ObjTabelle*)z;
  369. if( objT->getZeilenAnzahl() > 0 )
  370. {
  371. for( int i = 0; i < objT->getSpaltenAnzahl(); i++ )
  372. {
  373. if( objT->zZeichnung( i, 0 ) )
  374. objT->setSpaltenBreite( i, objT->zZeichnung( i, 0 )->getBreite() );
  375. }
  376. }
  377. }
  378. }
  379. }
  380. // setzt den inhalt der view
  381. // uiml: Ein xml element gemät des ksg uiml standarts
  382. void UIMLView::setUIML( XML::Element *uiml )
  383. {
  384. if( dom )
  385. dom->release();
  386. dom = uiml;
  387. members->leeren();
  388. nextId = 0;
  389. if( dom )
  390. {
  391. for( auto i = dom->getChilds(); i; i++ )
  392. {
  393. parseElement( i._ );
  394. }
  395. }
  396. }
  397. // setzt den inhalt der view
  398. // uiml: Ein xml text gemät des ksg uiml standarts
  399. void UIMLView::setUIML( Text uiml )
  400. {
  401. setUIML( new XML::Element( uiml ) );
  402. }
  403. // Gibt eine zeichnung zurück, welche in uiml eine bestimmte id hat
  404. // id: die id der Zeichnung
  405. Zeichnung *UIMLView::zZeichnung( Text id )
  406. {
  407. return members->z( id );
  408. }
  409. // aktualisiert größe und position aller Zeichnungen gemäß den spezifikationen in UIML
  410. void UIMLView::layout()
  411. {
  412. if( dom )
  413. {
  414. for( auto i = dom->getChilds(); i; i++ )
  415. {
  416. layout( i._, this->getInnenBreite(), this->getInnenHeight() );
  417. }
  418. }
  419. }
  420. // fügt ein element hinzu
  421. // uiml: Ein xml text gemät des KSG UIML standarts, welcher das neue Objekt darstellt
  422. Text UIMLView::addMember( Text uiml )
  423. {
  424. XML::Element *e = new XML::Element( uiml );
  425. if( parseElement( e ) )
  426. dom->addChild( e );
  427. return e->getAttributeValue( "id" );
  428. }
  429. // fügt ein element zu einem Elternelement hinzu (funktioniert momentan nur mit frame Objekten)
  430. // uiml: Ein xml text gemät des KSG UIML standarts, welcher das neue Objekt darstellt
  431. Text UIMLView::addMember( Text uiml, Text parentId )
  432. {
  433. XML::Element *e = new XML::Element( uiml );
  434. XML::Editor ed = dom->selectChildren();
  435. while( ed.getIterator() )
  436. {
  437. XML::Editor ed2 = ed.whereAttributeEquals( "id", parentId );
  438. if( ed2.getIterator() )
  439. {
  440. if( ed2.getIterator()->getName().istGleich( "frame" ) )
  441. {
  442. Zeichnung *z = parseElement( e );
  443. if( z )
  444. {
  445. ( (Fenster*)members->z( parentId ) )->addMember( z->getThis() );
  446. ed2.getIterator()->addChild( e );
  447. }
  448. return e->getAttributeValue( "id" );
  449. }
  450. }
  451. ed = ed.selectChildren();
  452. }
  453. e->release();
  454. return "";
  455. }
  456. // Verarbeitet ein Maus Ereignis. Wird vom Framework automatisch aufgerufen.
  457. // me: Das Ereignis
  458. void UIMLView::doMausEreignis( MausEreignis &me )
  459. {
  460. bool verarbeitet = me.verarbeitet;
  461. ZeichnungHintergrund::doMausEreignis( me );
  462. me.verarbeitet = verarbeitet;
  463. if( dom )
  464. {
  465. for( auto i = dom->getChilds(); i; i++ )
  466. { // TODO render elements backwards
  467. Zeichnung *z = members->z( i->getAttributeValue( "id" ) );
  468. if( z )
  469. z->doMausEreignis( me );
  470. }
  471. }
  472. }
  473. // Verarbeitet ein Tastatur Ereignis. Wird vom Framework automatisch aufgerufen
  474. // te: Das Ereignis
  475. void UIMLView::doTastaturEreignis( TastaturEreignis &te )
  476. {
  477. bool verarbeitet = te.verarbeitet;
  478. ZeichnungHintergrund::doTastaturEreignis( te );
  479. te.verarbeitet = verarbeitet;
  480. if( dom )
  481. {
  482. for( auto i = dom->getChilds(); i; i++ )
  483. { // TODO render elements backwards
  484. Zeichnung *z = members->z( i->getAttributeValue( "id" ) );
  485. if( z )
  486. z->doTastaturEreignis( te );
  487. }
  488. }
  489. }
  490. // Updated den Zeichenhintergrund
  491. // tickVal: Die vergangene Zeit in Sekunden, die seit dem Letzten Aufruf dieser Funktion verstrichen ist
  492. // return: 1, wenn das Bild neu gezeichnet werden muss. 0 sonnst
  493. bool UIMLView::tick( double tickVal )
  494. {
  495. if( dom )
  496. {
  497. for( auto i = dom->getChilds(); i; i++ )
  498. { // TODO render elements backwards
  499. Zeichnung *z = members->z( i->getAttributeValue( "id" ) );
  500. if( z )
  501. rend |= z->tick( tickVal );
  502. }
  503. }
  504. return ZeichnungHintergrund::tick( tickVal );
  505. }
  506. // Zeichnet den Hintergrund eines Zeichnunges nach rObj
  507. void UIMLView::render( Bild &rObj )
  508. {
  509. ZeichnungHintergrund::render( rObj );
  510. if( dom )
  511. {
  512. for( auto i = dom->getChilds(); i; i++ )
  513. { // TODO render elements backwards
  514. Zeichnung *z = members->z( i->getAttributeValue( "id" ) );
  515. if( z )
  516. z->render( rObj );
  517. }
  518. }
  519. }