UIMLView.cpp 19 KB

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