UIMLView.cpp 19 KB

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