ubit
uelem.hpp
1 /************************************************************************
2  *
3  * uelem.hpp: Element Nodes (lightweight containers that can have children)
4  * Ubit GUI Toolkit - Version 6
5  * (C) 2009 | Eric Lecolinet | TELECOM ParisTech | http://www.enst.fr/~elc/ubit
6  *
7  * ***********************************************************************
8  * COPYRIGHT NOTICE :
9  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE
10  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
11  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU
12  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION;
13  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
14  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
15  * ***********************************************************************/
16 
17 #ifndef _uelem_hpp_
18 #define _uelem_hpp_ 1
19 #include <string.h> // memset
20 #include <ubit/uattr.hpp>
21 #include <ubit/uupdate.hpp>
22 #include <ubit/uargs.hpp>
23 #include <ubit/ustr.hpp>
24 namespace ubit {
25 
26  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44  class UElem: public UNode {
45  UElem& operator=(const UElem&);
47 
48  explicit UElem(const UElem&);
49 
50  public:
51  //UCLASS("#element", UElem, new UElem)
52  UCLASS(UElem)
53 
54  UElem(UArgs node_arglist = UArgs::none);
65  virtual ~UElem();
67 
68  virtual void destructs();
69 
70  virtual UElem* toElem() {return this;}
71  virtual const UElem* toElem() const {return this;}
72 
73  static UStyle* createStyle();
78  const UStyle& getStyle(UUpdateContext*) const;
86  virtual int getNodeType() const {return ELEMENT_NODE;}
88 
89  virtual const UStr& getNodeName() const {return getClass().getName();}
91 
92  virtual int getDisplayType() const {return INLINE;}
93 
94  virtual void initNode(UDoc* context) {}
96 
97 
98  // - - - - - - - - - - - - - - - - - - - - - - - -
99  // attributes.
100 
101  UElem& setAttr(UNode& attribute);
107  UElem& addAttr(const UArgs& attributes);
112  UElem& removeAttr(UNode& attribute, bool auto_delete = true);
119  UElem& removeAllAttrs(bool auto_delete = true);
126  bool getAttrValue(UStr& value, const UStr& attr_name) const;
132  UAttr* getAttr(const UStr& attr_name) const;
138  template <class ATTR>
139  ATTR* getAttr() const {ATTR* a; _attributes.findClass(a); return a;}
145  template <class ATTR>
146  ATTR& obtainAttr()
147  {ATTR* a; _attributes.findClass(a); if (!a) addAttr(a = new ATTR); return *a;}
154  // - - - - - - - - - - - - - - - - - - - - - - - - - -
155  // children.
156 
157  virtual bool isParentOf(const UNode& possible_child) const;
159 
160  UElem& add(const UArgs& children);
202  UElem& add(const UArgs& children, int position);
211  UElem& add(const UArgs& children, UChildIter iterator);
218  virtual UElem& addImpl(const UArgs& nodes, UChildIter pos, UChildren& in_list);
219  virtual bool addImpl1(const UChild& node, UChildIter pos, UChildren& in_list);
220 
221  // - - - -
222 
223  UElem& remove(UNode& child, bool auto_delete = true);
237  UElem& remove(int pos, bool auto_delete = true);
243  UElem& remove(UChildIter pos, bool auto_delete = true);
249  UElem& removeAll(bool auto_delete = true);
254  virtual UElem& removeImpl(UChildIter begin, int N, bool autodel, UChildren& in_list);
255  virtual bool removeImpl1(UChildIter pos, int auto_delete, UChildren& in_list);
256 
257  // - - - -
258 
259  UChildren& getChildren() const {return children();}
260  /* return the child list.
261  * @see: child(), getChild(), cbegin(), cend(), crbegin(), crend(), attributes().
262  */
263 
264  int getChildCount() const {return children().size();}
265  /* return the number of children.
266  * @see: child(), getChild(), cbegin(), cend(), crbegin(), crend(), attributes().
267  */
268 
269  UChildIter child(int pos) const {return children().at(pos);}
275  UNode* getChild(int pos) const;
281  UChildIter cbegin() const {return children().begin();}
293  UChildIter cend() const {return children().end();}
295 
296  UChildReverseIter crbegin() const {return children().rbegin();}
298 
299  UChildReverseIter crend() const {return children().rend();}
301 
302  // - - - text - - - - - - - - - - - - - - - - - - - - - - - - -
303 
304  virtual UStr retrieveText(bool recursive = true) const;
309  virtual void retrieveText(UStr& string, bool recursive = true) const;
314  // - - - graphics and layout - - - - - - - - - - - - - - - - - -
315 
316  virtual bool isShown() const;
325  bool isShowable() const {return emodes.IS_SHOWABLE;}
333  void setShowable(bool s) const {emodes.IS_SHOWABLE = s;}
335 
336  virtual void show(bool = true);
341  static void closeWin(UInputEvent&, int status);
348 
351 
352  virtual void update(const UUpdate& update_options, UDisp* = null);
360  void doUpdate() {doUpdate(UUpdate::layoutAndPaint, null);}
361 
362  virtual void doUpdate(const UUpdate&, UDisp* = null);
371  // - - - events - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
372 
373  UElem& ignoreEvents(bool state = true);
378  UElem& catchEvents(const UChild& condition_callback_expr);
405  UElem& observeChildrenEvents(const UChild& condition_callback_expr);
417  bool isIgnoringEvents() const {return emodes.IGNORE_EVENTS;}
419 
420  bool hasCallback(long callback_id) const;
422 
423  virtual bool fire(UEvent&) const;
425 
426  // - - - states - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
427 
428  UElem& enable(bool state = true) {return setEnabled(state);}
430 
431  virtual UElem& setEnabled(bool state = true, bool call_callbacks = true);
438  bool isEnabled() const;
440 
441  // - - - - -
442 
443  virtual UElem& setSelected(bool state = true, bool call_callbacks = true);
448  bool isSelected() const {return emodes.IS_SELECTED;}
449 
450  bool isSelectable() const {return emodes.IS_SELECTABLE;}
451  UElem& setSelectable(bool state = true);
452 
453  // - - - - -
454 
455  bool isArmed() const;
456 
457  bool isArmable() const {return emodes.IS_ARMABLE;}
463  UElem& setArmable(bool state = true);
464 
465  // - - - - -
466 
467  UElem& setAutoRepeat(bool state = true);
468  bool isAutoRepeat() const {return emodes.IS_AUTO_REPEAT;}
469 
470  bool isCrossable() const {return emodes.IS_CROSSABLE;}
471  UElem& setCrossable(bool state = true);
472 
473  UElem& setBrowsable(bool = true);
474  bool isBrowsable() const {return emodes.IS_BROWSABLE;}
475 
476  bool isDragged() const;
477  bool isDraggable() const {return emodes.IS_DRAGGABLE;}
478  UElem& setDraggable(bool = true);
479 
480  bool isDroppable() const {return emodes.IS_DROPPABLE;}
481  UElem& setDroppable(bool = true);
482 
483  // - - - - -
484 
485  bool isTextEditable() const {return emodes.IS_TEXT_EDITABLE;}
486  bool isFloating() const {return emodes.IS_FLOATING;}
487  bool isVertical() const {return emodes.IS_VERTICAL;} // && HAS_ORIENT ? !!!!
488  bool isWidthResizable() const {return !emodes.IS_WIDTH_UNRESIZABLE;}
489  bool isHeightResizable() const {return !emodes.IS_HEIGHT_UNRESIZABLE;}
490  virtual bool isSubWin() const {return false;} // redefined by USunWin => MUST be virtual!
491 
492  void disableMenuClosing(bool s = true) {emodes.DONT_CLOSE_MENUS = s;}
494 
495  bool isMenuClosingDisabled() const {return emodes.DONT_CLOSE_MENUS;}
497 
498  // - - - impl. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
499 #ifndef NO_DOC
500 
501  struct Modes {
502  unsigned IS_SHOWABLE:1;
503  unsigned IS_BROWSABLE:1;
504  unsigned IS_CROSSABLE:1;
505  unsigned IS_ARMABLE:1;
506  unsigned IS_AUTO_REPEAT:1;
507  unsigned IS_DRAGGABLE:1;
508  unsigned IS_DROPPABLE:1;
509  unsigned IS_SELECTABLE:1;
510  unsigned IS_SELECTED:1;
511  unsigned IS_TEXT_SELECTABLE:1;
512  unsigned IS_TEXT_EDITABLE:1;
513  unsigned IS_WIDTH_UNRESIZABLE:1;
514  unsigned IS_HEIGHT_UNRESIZABLE:1;
515  unsigned IS_FLOATING:1;
516  unsigned IS_VERTICAL:1;
517  unsigned HAS_ORIENT:1; // A REVOIR @@@@ !!!!.
518  unsigned HAS_CURSOR:1;
519  unsigned HAS_LAYOUT:1;
520  unsigned HAS_TIP:1;
521  unsigned DONT_CLOSE_MENUS:1;
522  unsigned OBSERVE_EVENTS:1;
523  unsigned IGNORE_EVENTS:1;
524  enum {
525  CATCH_MOUSE_MASK = 1<<0, CATCH_MOUSE_MOVE_MASK = 1<<1,
526  CATCH_WHEEL_MASK = 1<<2, CATCH_KEY_MASK = 1<<3
527  };
528  unsigned CATCH_EVENTS:4;
529  };
530 
531  enum {REMOVE_FROM_PARENTS = -1};
532 
533  bool isBrowsingGroup() {return emodes.IS_BROWSABLE;} // !!!
534  //virtual UElem* getBrowsingGroup() {return emodes.IS_BROWSABLE ? this : null;}
535 
536  //UElem& setConst() {UObject::setConst(); return *this;} // redefined from UObject.
537 
538  virtual UChildren& children() const {return _children;}
539  /*[impl] return the child list.
540  * @see: child(), getChild(), cbegin(), cend(), crbegin(), crend(), attributes().
541  */
542 
543  virtual UChildren& attributes() const {return _attributes;}
544  /*[impl] returns the attribute list.
545  * @see: abegin(), aend(), children().
546  */
547 
548  UChildIter abegin() const {return attributes().begin();}
549  /*[impl] returns an iterator to the beginning the ATTRIBUTE list.
550  * works in the same was as cbegin() cend() for the CHILD list.
551  * @see: aend(), attributes()
552  */
553 
554  UChildIter aend() const {return attributes().end();}
555  //[impl] returns an iterator to the end of the attribute list (@see abegin()).
556 
557  UObject::State getInterState() const {return ostate;}
558  //[impl] returns the internal interactive state.
559 
560  void setInterState(UObject::State);
561  //[impl] changes the internal interactive state.
562 
563  virtual const UStr* getTextSeparator() const;
565 
566  virtual void deleteViewsInside(const std::vector<UView*>& parent_views);
567 
568  virtual void initView(UView* parent_view);
569  virtual void initChildViews(UElem*);
570  virtual void highlight(bool state) {};
571 
572  virtual int retrieveRelatedViews(std::vector<UView*>& collating_vector) const;
573  /* returns the views that are related to this element.
574  * retrieves all the views where this element currently appears. This list is
575  * obtained by collating the views of all the UBox parents of this element
576  * (this may be indirect parents as this element may be a child of another element
577  * that is a child of a UBox, and so on).
578  *
579  * - 'collating_vector' is not emptied: the views are added to the end of what
580  * it already contains.
581  * - the function returns the number of views that were added (which may thus differ
582  * from the total size of 'collating_vector')
583  *
584  * Note that elements and boxes can have several parents, so that multiple wiews
585  * can be associated to the same widget (see UElem and UBox classes for details)
586  */
587 
588  UElem& _setArmableAndSelectable(bool state);
589 
590  /* [impl] behavioral functions.
591  * these functions are called when the object receives the corresponding events.
592  * Usually, these functions should not be redefined by clients. Instead, clients
593  * should add UOn::xxx conditions that are called by these functions
594  * @see class UOn.
595  */
596 
597  virtual void keyPressBehavior(UKeyEvent&);
598  virtual void keyReleaseBehavior(UKeyEvent&);
599  virtual void enterBehavior(UInputEvent&, bool is_browing);
600  virtual void leaveBehavior(UInputEvent&, bool is_browing);
601  virtual void armBehavior(UInputEvent&, bool is_browing);
602  virtual void disarmBehavior(UInputEvent&, bool is_browing);
603  virtual void actionBehavior(UInputEvent&);
604 
605  protected:
606  friend class UNode;
607  friend class UEdit;
608  friend class UChildEventFlag;
609  friend class UPos;
610  friend class USize;
611  friend class UOrient;
612  friend class UCursor;
613  friend class UTip;
614  friend class UStr;
615  friend class UWin;
616  friend class UOn;
617  friend class UEventFlow;
618  friend class UViewStyle;
619  friend class UUpdateContext;
620  friend class UWinUpdateContext;
621  //short istate; inherited from UObject
622  mutable Modes emodes;
623  long callback_mask;
624  mutable UChildren _children;
625  virtual int _getTextLength(bool recursive) const;
626  virtual char* _getTextData(char *ptr, bool recursive) const;
627 #endif
628  };
629 
630  //typedef UElem UGroup;
631  //[Ubit5 compatibility] UGroup is an obsolete synonym for UElem.
632 
633  inline UElem& uelem(UArgs args = UArgs::none) {return *new UElem(args);}
635 
636  inline UElem& ugroup(UArgs args = UArgs::none) {return *new UElem(args);}
638 
639 }
640 #endif
UChildReverseIter crbegin() const
returns a reverse iterator pointing to the beginning of the child list (
Definition: uelem.hpp:296
unsigned IS_FLOATING
has a UPos attr in floating mode.
Definition: uelem.hpp:515
Base class for attributes.
Definition: uattr.hpp:97
ATTR & obtainAttr()
retrieves or creates the element&#39;s attribute that derives from the "ATTR" C++ class.
Definition: uelem.hpp:146
keyboard events
Definition: uevent.hpp:316
bool isIgnoringEvents() const
returns true if events are ignored by this object;
Definition: uelem.hpp:417
Widget position.
Definition: uboxgeom.hpp:91
UElem & removeAttr(UNode &attribute, bool auto_delete=true)
removes/deletes an attribute (and its descendants) from the ATTRIBUTE list.
Definition: uelem.cpp:386
static const UUpdate layoutAndPaint
layout then paint: prefined constant for UUpdate(LAYOUT|PAINT);
Definition: uupdate.hpp:50
unsigned IS_DRAGGABLE
can be dragged (with button 1 or button2)
Definition: uelem.hpp:507
unsigned HAS_TIP
has a tool tip.
Definition: uelem.hpp:520
Box tool tip.
Definition: uattr.hpp:197
virtual void initNode(UDoc *context)
initialises the XML context of this node.
Definition: uelem.hpp:94
unsigned IS_TEXT_SELECTABLE
can select included text
Definition: uelem.hpp:511
virtual bool addImpl1(const UChild &node, UChildIter pos, UChildren &in_list)
Definition: uelem.cpp:166
static const UUpdate paint
paint only: prefined constant for UUpdate(PAINT);
Definition: uupdate.hpp:47
Base class of objects that can be added to the UBIT scene graph (SEE DETAILS!).
Definition: unode.hpp:38
Ubit Event class.
Definition: uevent.hpp:30
unsigned IS_SELECTABLE
can be selected (as for a checkbox)
Definition: uelem.hpp:509
UElem & enable(bool state=true)
shortcut for setEnabled(bool state = true).
Definition: uelem.hpp:428
unsigned IS_VERTICAL
has a vertical orientation (see HAS_ORIENT).
Definition: uelem.hpp:516
static UStyle * createStyle()
static function that returns the style of this class.
Definition: uelem.cpp:58
virtual UElem & setEnabled(bool state=true, bool call_callbacks=true)
specifies whether this object is enabled.
Definition: uelem.cpp:887
Display Context.
Definition: udisp.hpp:44
UChildReverseIter crend() const
returns a reverse iterator pionting to the end of the child list (
Definition: uelem.hpp:299
UElem & setAttr(UNode &attribute)
adds or replaces this attribute in the ATTRIBUTE list.
Definition: uelem.cpp:366
const UStyle & getStyle(UUpdateContext *) const
virtual function that returns the style of this object.
Definition: uelem.cpp:38
unsigned IS_CROSSABLE
detects (and shows) UOn::enter and UOn::leave conditions
Definition: uelem.hpp:504
UElem & removeAllAttrs(bool auto_delete=true)
removes/deletes all attributes (and their descendants) in the ATTRIBUTE list.
Definition: uelem.cpp:392
Definition: uupdatecontext.hpp:98
unsigned IS_SELECTED
this object is currently selected.
Definition: uelem.hpp:510
virtual UElem * toElem()
dynamic cast: returns this object if it derives from UElem and null otherwise.
Definition: uelem.hpp:70
virtual int getNodeType() const
returns the XML node type.
Definition: uelem.hpp:86
UElem & add(const UArgs &children)
adds one or several objects to the end of the CHILD list of this element.
Text editing and caret controller.
Definition: uedit.hpp:30
UChildIter cbegin() const
returns the child at this position.
Definition: uelem.hpp:281
UElem & ignoreEvents(bool state=true)
if arg is true, this element and (its children) ignore events.
Definition: uelem.cpp:717
specifies how to update UElem, UBox, UWin objects and subclasses.
Definition: uupdate.hpp:25
void disableMenuClosing(bool s=true)
this object and its children wont close menus when clicked if argument is true.
Definition: uelem.hpp:492
bool isArmable() const
specifies whether this object can be armed.
Definition: uelem.hpp:457
unsigned CATCH_EVENTS
this object can catch mouse events.
Definition: uelem.hpp:528
bool isMenuClosingDisabled() const
true if this object and its children do not close menus when clicked.
Definition: uelem.hpp:495
unsigned IS_BROWSABLE
can browse (as for lists and menus)
Definition: uelem.hpp:503
bool getAttrValue(UStr &value, const UStr &attr_name) const
retrieves the value of the attribute which class name is &#39;attr_name&#39;.
Definition: uelem.cpp:352
virtual bool fire(UEvent &) const
fires callback functions that match this event.
Definition: uelem.cpp:816
Generic Document.
Definition: udoc.hpp:122
lightweight general purpose container.
Definition: uelem.hpp:44
Child (or attribute) list.
Definition: uchild.hpp:79
virtual const UElem * toElem() const
dynamic cast: returns this object if it derives from UElem and null otherwise.
Definition: uelem.hpp:71
virtual bool isShown() const
returns true if this object is shown.
Definition: uelem.cpp:489
virtual const UStr & getNodeName() const
return the XML node name (which is the class name).
Definition: uelem.hpp:89
unsigned IS_ARMABLE
can be armed
Definition: uelem.hpp:505
virtual UElem & setSelected(bool state=true, bool call_callbacks=true)
specifies whether this object is selected.
Definition: uelem.cpp:896
Argument list (for passing arguments to constructor or add functions).
Definition: uargs.hpp:43
virtual void destructs()
unlinks the object from its parents and destroys its children.
Definition: uelem.cpp:109
Callback conditions.
Definition: uon.hpp:35
Widget Orientation.
Definition: uboxgeom.hpp:365
void update()
indicates that this object will be layed out and repainted (when the main loop becomes idle)...
Definition: uelem.hpp:349
unsigned HAS_LAYOUT
has a layout attr.
Definition: uelem.hpp:519
UElem & addAttr(const UArgs &attributes)
adds one or several attributes (deriving from UAttr) to the ATTRIBUTE list.
Definition: uelem.cpp:382
Event Flow.
Definition: ueventflow.hpp:34
void setShowable(bool s) const
synonym for show(bool).
Definition: uelem.hpp:333
Box View.
Definition: uview.hpp:65
ATTR * getAttr() const
retrieves the attribute node that derives from the "ATTR" C++ class.
Definition: uelem.hpp:139
Specifies the View Style of an UBox.
Definition: uview.hpp:42
UElem & observeChildrenEvents(const UChild &condition_callback_expr)
observe events that occur in children.
Definition: uelem.cpp:752
Definition: uupdatecontext.hpp:32
unsigned IGNORE_EVENTS
this object ignores events.
Definition: uelem.hpp:523
unsigned IS_TEXT_EDITABLE
can edit included text (ie. the group contains a UEdit brick)
Definition: uelem.hpp:512
virtual bool isParentOf(const UNode &possible_child) const
returns true if this object a direct or indirect parent of &#39;child&#39;.
Definition: uelem.cpp:296
Definition: uhardfont.hpp:31
bool isEnabled() const
returns true if this object is currently enabled;
Definition: uelem.cpp:665
virtual const UStr * getTextSeparator() const
returns the text separator used by retrieveText() for separating enclosed children ...
Definition: uelem.cpp:34
Base class for UMouseEvent and UKeyEvent Note that this class inherits from class UModifier that defi...
Definition: uevent.hpp:75
static const UArgs none
the empty arglist.
Definition: uargs.hpp:45
unsigned IS_SHOWABLE
can be shown if parents are shown (see isShowable() and isShown())
Definition: uelem.hpp:502
unsigned HAS_CURSOR
has a cursor attr.
Definition: uelem.hpp:518
unsigned IS_AUTO_REPEAT
auto-repeats UOn::arm while armed
Definition: uelem.hpp:506
UElem & removeAll(bool auto_delete=true)
removes/deletes all children (and their descendants) in the CHILD list.
Definition: uelem.cpp:335
unsigned IS_DROPPABLE
can be dropped
Definition: uelem.hpp:508
virtual const UClass & getClass() const
instance method that returns the metaclass of this object.
Definition: uobject.hpp:134
Base class for windows and menus.
Definition: uwin.hpp:47
bool hasCallback(long callback_id) const
true if thie object has such a callback.
Definition: uelem.cpp:812
bool isShowable() const
returns true if this object can be shown.
Definition: uelem.hpp:325
static void closeWin(UInputEvent &, int status)
closes the first window (UDialog, UMenu...) that contains this element.
Definition: uelem.cpp:438
Mouse cursor property.
Definition: ucursor.hpp:31
Widget Size.
Definition: uboxgeom.hpp:228
void repaint()
indicates that this object will be repainted (when the main loop becomes idle).
Definition: uelem.hpp:346
virtual UStr retrieveText(bool recursive=true) const
collates and returns the text that is enclosed in this object.
UChildIter findClass(CC *&c)
returns an iterator to the first child that derives from this class.
Definition: uchild.hpp:105
Compiled Object Style.
Definition: ustyle.hpp:44
virtual void show(bool=true)
shows/hides this object.
Definition: uelem.cpp:468
const UStr & getName() const
returns the class name.
Definition: uclass.hpp:95
virtual ~UElem()
destructor, note that children are recursively destroyed except if pointed elsewhere ( ...
Definition: uelem.cpp:95
Ubit String.
Definition: ustr.hpp:72
UElem & catchEvents(const UChild &condition_callback_expr)
catches certain events before they reach the object&#39;s children.
Definition: uelem.cpp:724
[impl] Internal implementation of a child node.
Definition: uchild.hpp:23
Definition: uelem.hpp:501
UNode * getChild(int pos) const
returns an iterator pointing to the child at this position.
Definition: uelem.cpp:300
UChildIter cend() const
returns a forward iterator pointing to the end of the child list (
Definition: uelem.hpp:293