Author: sh-sh-sh
Date: Tue Jun 5 18:01:57 2007
New Revision: 38298
URL: http://svn.opensuse.org/viewcvs/yast?rev=38298&view=rev
Log:
children management in YWidget
Added:
branches/tmp/sh/mod-ui/core/libyui/src/YChildrenManager.h
Modified:
branches/tmp/sh/mod-ui/core/libyui/src/Makefile.am
branches/tmp/sh/mod-ui/core/libyui/src/YWidget.cc
branches/tmp/sh/mod-ui/core/libyui/src/YWidget.h
Modified: branches/tmp/sh/mod-ui/core/libyui/src/Makefile.am
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/mod-ui/core/libyui/src/Makefile.am?rev=38298&r1=38297&r2=38298&view=diff
==============================================================================
--- branches/tmp/sh/mod-ui/core/libyui/src/Makefile.am (original)
+++ branches/tmp/sh/mod-ui/core/libyui/src/Makefile.am Tue Jun 5 18:01:57 2007
@@ -21,6 +21,7 @@
YUIException.cc \
YProperty.cc \
\
+ YWidget.cc \
YAlignment.cc \
YBarGraph.cc \
YCheckBox.cc \
@@ -65,7 +66,6 @@
YTextEntry.cc \
YTime.cc \
YTree.cc \
- YWidget.cc \
YWizard.cc \
YSelectionWidget.cc
Added: branches/tmp/sh/mod-ui/core/libyui/src/YChildrenManager.h
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/mod-ui/core/libyui/src/YChildrenManager.h?rev=38298&view=auto
==============================================================================
--- branches/tmp/sh/mod-ui/core/libyui/src/YChildrenManager.h (added)
+++ branches/tmp/sh/mod-ui/core/libyui/src/YChildrenManager.h Tue Jun 5 18:01:57 2007
@@ -0,0 +1,213 @@
+/*---------------------------------------------------------------------\
+| |
+| __ __ ____ _____ ____ |
+| \ \ / /_ _/ ___|_ _|___ \ |
+| \ V / _` \___ \ | | __) | |
+| | | (_| |___) || | / __/ |
+| |_|\__,_|____/ |_| |_____| |
+| |
+| core system |
+| (C) SuSE GmbH |
+\----------------------------------------------------------------------/
+
+ File: YChildrenManager.h
+
+ Author: Stefan Hundhammer
+
+/-*/
+
+#ifndef YChildrenManager_h
+#define YChildrenManager_h
+
+#include <list>
+#include <algorithm>
+#include
+
+
+/**
+ * Abstract base template class for children management, such as child
+ * widgets.
+ **/
+template<class T> class YChildrenManager
+{
+protected:
+
+ /**
+ * Constructor.
+ *
+ * 'containerParent' is the class whose children are managed.
+ **/
+ YChildrenManager( T * containerParent )
+ : _container( containerParent )
+ {}
+
+ /**
+ * Destructor.
+ **/
+ virtual ~YChildrenManager() {}
+
+public:
+
+ typedef std::list ChildrenList;
+
+ /**
+ * Check if there are any children.
+ **/
+ bool hasChildren() const { return ! empty(); }
+
+ /**
+ * Check if the children list is empty, i.e. if there are no children.
+ **/
+ bool empty() const { return _children.empty(); }
+
+ /**
+ * Returns the number of children.
+ **/
+ int count() const { return _children.size(); }
+
+ /**
+ * Return an iterator that points to the first child.
+ **/
+ typename ChildrenList::const_iterator begin() const
+ { return _children.begin(); }
+
+ /**
+ * Returns an iterator that points after the last child.
+ **/
+ typename ChildrenList::const_iterator end() const
+ { return _children.end(); }
+
+ /**
+ * Returns the first child or 0 if there is none.
+ * Useful mostly for children managers that handle only one child.
+ **/
+ T * firstChild()
+ { return _children.empty() ? (T *) 0 : _children.front(); }
+
+ /**
+ * Add a new child.
+ *
+ * This may throw exceptions if more children are added than the class
+ * whose children are handled (the associated widget) can handle.
+ **/
+ virtual void add( T * child )
+ { _children.push_back( child ); }
+
+ /**
+ * Remove a child. This only removes the child from the children manager's
+ * list; it does not delete it.
+ **/
+ virtual void remove( T * child )
+ { _children.remove( child ); }
+
+ /**
+ * Remove all children. This only removes the children from the children
+ * manager's list; it does not delete them.
+ **/
+ virtual void clear()
+ { _children.clear(); }
+
+ /**
+ * Check if the children list contains the specified child.
+ * Returns 'true' if the children list contains the child,
+ * 'false' otherwise.
+ **/
+ bool contains( T * child ) const
+ {
+ return ( find( _children, _children.begin(), _children.end() )
+ != _children.end() );
+ }
+
+ /**
+ * Returns the associated container, i.e. the object whose children are
+ * handled here.
+ **/
+ T * container() const { return _container; }
+
+protected:
+
+ T * _container;
+ ChildrenList _children;
+};
+
+
+/**
+ * Children manager that can handle one single child (rejecting any more).
+ * Useful for YAlignment, YFrame etc.
+ **/
+template<class T> class YSingleChildManager: public YChildrenManager<T>
+{
+public:
+
+ YSingleChildManager( T * containerParent )
+ : YChildrenManager<T>( containerParent )
+ {}
+
+ /**
+ * Add a new child.
+ *
+ * Reimplemented from YChildrenManager.
+ *
+ * This will throw a YUITooManyChildrenException if there already is a
+ * child.
+ **/
+ virtual void add( T * child )
+ {
+ if ( this->empty() )
+ this->_children.push_back( child );
+ else
+ YUI_THROW( YUITooManyChildrenException<T>( this->container() ) );
+ }
+
+ /**
+ * Replace the previous child (if any) with a new one.
+ **/
+ void replace( T * newChild )
+ {
+ this->_children.clear();
+ this->_children.push_back( newChild );
+ }
+};
+
+
+/**
+ * Children manager that rejects all children.
+ *
+ * Useful for widget classes that can't handle children such as YPushButton,
+ * YSelectionBox etc.
+ **/
+template<class T> class YChildrenRejector: public YChildrenManager<T>
+{
+public:
+ /**
+ * Constructor.
+ **/
+ YChildrenRejector( T * containerParent )
+ : YChildrenManager<T>( containerParent )
+ {}
+
+ /**
+ * Add a new child.
+ *
+ * Reimplemented from YChildrenManager.
+ *
+ * Since this class is designed to reject children, this always throws a
+ * YUITooManyChildrenException.
+ **/
+ virtual void add( T * child )
+ { YUI_THROW( YUITooManyChildrenException<T>( this->container() ) ); }
+
+ /**
+ * Remove a child.
+ *
+ * Reimplemented from YChildrenManager.
+ *
+ * Since this class is designed to reject children, this always throws a
+ * YUIInvalidChildException.
+ **/
+ virtual void remove( T * child )
+ { YUI_THROW( YUIInvalidChildException<T>( this->container(), child ) ); }
+};
+
+
+#endif // YChildrenManager_h
Modified: branches/tmp/sh/mod-ui/core/libyui/src/YWidget.cc
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/mod-ui/core/libyui/src/YWidget.cc?rev=38298&r1=38297&r2=38298&view=diff
==============================================================================
--- branches/tmp/sh/mod-ui/core/libyui/src/YWidget.cc (original)
+++ branches/tmp/sh/mod-ui/core/libyui/src/YWidget.cc Tue Jun 5 18:01:57 2007
@@ -41,12 +41,31 @@
#define MAX_DEBUG_LABEL_LEN 50
+struct YWidgetPrivate
+{
+ /**
+ * Constructor
+ **/
+ YWidgetPrivate( YWidget * widget )
+ : childrenRejector( widget )
+ , childrenManager( childrenRejector )
+ {}
+
+ //
+ // Data members
+ //
+
+ YWidgetChildrenRejector childrenRejector; // default: no children
+ YWidgetChildrenManager & childrenManager;
+};
+
int YWidget::next_internal_widget_id = 0;
YWidget::YWidget( const YWidgetOpt & opt )
- : magic( YWIDGET_MAGIC )
+ : priv( new YWidgetPrivate( this ) )
+ , magic( YWIDGET_MAGIC )
, user_widget_id( YCPNull() )
, yparent(0)
, rep(0)
@@ -83,7 +102,36 @@
}
-string YWidget::debugLabel()
+YChildrenManager &
+YWidget::childrenManager() const
+{
+ return priv->childrenManager;
+}
+
+
+void
+YWidget::setChildrenManager( YChildrenManager & newChildrenManager )
+{
+ priv->childrenManager = newChildrenManager;
+}
+
+
+void
+YWidget::addChild( YWidget * child )
+{
+ childrenManager().addChild( child );
+}
+
+
+void
+YWidget::removeChild( YWidget * child )
+{
+ childrenManager().removeChild( child );
+}
+
+
+string
+YWidget::debugLabel()
{
string label = YShortcut::cleanShortcutString( YShortcut::getShortcutString( this ) );
Modified: branches/tmp/sh/mod-ui/core/libyui/src/YWidget.h
URL: http://svn.opensuse.org/viewcvs/yast/branches/tmp/sh/mod-ui/core/libyui/src/YWidget.h?rev=38298&r1=38297&r2=38298&view=diff
==============================================================================
--- branches/tmp/sh/mod-ui/core/libyui/src/YWidget.h (original)
+++ branches/tmp/sh/mod-ui/core/libyui/src/YWidget.h Tue Jun 5 18:01:57 2007
@@ -27,6 +27,7 @@
#include "YWidgetOpt.h"
#include "YUISymbols.h"
#include "YUIException.h"
+#include "YChildrenManager.h"
#include "ImplPtr.h"
#define YWIDGET_MAGIC 42 // what else? ;- )
@@ -44,6 +45,12 @@
typedef std::list::iterator YWidgetListIterator;
typedef std::list::const_iterator YWidgetListConstIterator;
+typedef YChildrenManager<YWidget> YWidgetChildrenManager;
+typedef YSingleChildManager<YWidget> YSingleWidgetChildManager;
+typedef YChildrenRejector<YWidget> YWidgetChildrenRejector;
+
+class YWidgetPrivate;
+
/**
* @short Abstract base class of all ui widgets
*/
@@ -79,6 +86,11 @@
**/
virtual std::string debugLabel();
+
+ //
+ // Property Management
+ //
+
/**
* Return this class's property set.
* This also initializes the property upon the first call.
@@ -134,6 +146,83 @@
virtual YPropertyValue getProperty( const std::string & propertyName );
// FIXME: = 0
+
+ //
+ // Children Management
+ //
+ // Even though many widget classes are leaf classes and thus cannot have
+ // children by design, it makes sense to have the children management in
+ // this base class: Then descending down a widget tree is transparent to
+ // the outside without the need to check for container widget classes,
+ // casting to those container widget classes and only calling child
+ // management methods in that case.
+ //
+ // By default, YWidget and derived classes have a YWidgetChildrenRejector
+ // as their children manager, i.e. any attempt to add a child will result
+ // in a YUITooManyChildrenException.
+ //
+ // Derived classes that can (semantically) handle children should set the
+ // children manager to one of
+ //
+ // - YWidgetChildrenManager: handles any number of child widgets;
+ // useful for VBox / HBox
+ //
+ // - YSingleWidgetChildManager: handles exactly one child
+ // useful for widgets like Alignment, Frame, Dialog
+ //
+
+
+ /**
+ * Returns 'true' if this widget has any children.
+ **/
+ bool hasChildren() const
+ { return childrenManager().hasChildren(); }
+ /**
+ * Returns the first child or 0 if there is none.
+ * Useful mostly for children managers that handle only one child.
+ **/
+ YWidget * firstChild() const
+ { return childrenManager().firstChild(); }
+
+ /**
+ * Return an iterator that points to the first child or to childrenEnd() if
+ * there are no children.
+ **/
+ YWidgetListConstIterator childrenBegin() const
+ { return childrenManager().begin(); }
+
+ /**
+ * Returns the current number of children.
+ **/
+ int childrenCount() const { return childrenManager().count(); }
+
+ /**
+ * Return an interator that points after the last child.
+ **/
+ YWidgetListConstIterator childrenEnd() const
+ { return childrenManager().end(); }
+
+ /**
+ * Add a new child.
+ *
+ * This may throw exceptions if more children are added than this widget
+ * can handle.
+ **/
+ virtual void addChild( YWidget * child );
+
+ /**
+ * Remove a child. This only removes the child from the children manager's
+ * list; it does not delete it.
+ **/
+ virtual void removeChild( YWidget * child );
+
+
+ //
+ // Misc
+ //
+
+
+
/**
* Checks whether or not this object is valid. This is to enable
* dangling pointer error checking ( i.e. this object is already
@@ -445,6 +534,27 @@
virtual void doneMultipleChanges() {}
+protected:
+
+ /**
+ * Returns this widget's children manager.
+ **/
+ YWidgetChildrenManager & childrenManager() const;
+
+ /**
+ * Sets a new children manager for this widget.
+ * The default children manager (a YWidgetChildrenRejector) rejects all
+ * children. This is useful for leaf widgets such as PushButton, ComboBox
+ * etc.
+ *
+ * Derived classes that can handle children might want to set the children
+ * manager to a YWidgetChildrenManager (the base class that does not reject
+ * children) or to a YSingleWidgetChildManager (the class that handles
+ * exactly one child widget).
+ **/
+ void setChildrenManager( YWidgetChildrenManager & manager );
+
+
private:
/**
@@ -528,7 +638,8 @@
private:
- static YPropertySet _propertySet;
+ ImplPtr priv;
+ static YPropertySet _propertySet;
public:
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org