UIMLView.cpp 19 KB

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