libyui-gtk  2.42.2
 All Classes
YGWidget.h
1 /********************************************************************
2  * YaST2-GTK - http://en.opensuse.org/YaST2-GTK *
3  ********************************************************************/
4 
5 #ifndef YGWIDGET_H
6 #define YGWIDGET_H
7 
8 #include <gtk/gtk.h>
9 #include <stdarg.h>
10 #include "YGUI.h"
11 #include "YEvent.h"
12 
13 class YGWidget
14 {
15 public:
16  YGWidget (YWidget *ywidget, YWidget *yparent,
17  GType type, const char *property_name, ...);
18  virtual ~YGWidget();
19 
20  // get the YGWidget associated with a YWidget
21  static YGWidget *get (YWidget *y_widget);
22 
23  virtual inline GtkWidget *getWidget() { return m_widget; }
24  GtkWidget *getLayout() { return m_adj_size; } // add this to a container
25  virtual GtkWidget *getContainer() { return m_widget; } // add children here
26 
27  // overload YWidget methods with these ones
28  virtual bool doSetKeyboardFocus();
29  virtual void doSetEnabled (bool enabled);
30  virtual void doSetUseBoldFont (bool useBold);
31  virtual void doAddChild (YWidget *child, GtkWidget *container);
32  virtual void doRemoveChild (YWidget *child, GtkWidget *container);
33 
34  // layout
35  virtual int doPreferredSize (YUIDimension dimension);
36  virtual void doSetSize (int width, int height);
37 
38  // debug
39  const char *getWidgetName() const { return m_ywidget->widgetClass(); }
40  virtual std::string getDebugLabel() const // let YWidget::debugLabel() be overloaded
41  { if (m_ywidget->hasChildren()) return std::string(); return m_ywidget->debugLabel(); }
42 
43  // aesthetics
44  void setBorder (unsigned int border); // in pixels
45  virtual unsigned int getMinSize (YUIDimension dim) { return 0; }
46 
47 protected:
48  // event emission
49  enum EventFlags
50  { DELAY_EVENT = 2, IGNORE_NOTIFY_EVENT = 4, IF_NOT_PENDING_EVENT = 8 };
51  void emitEvent (YEvent::EventReason reason, EventFlags flags = (EventFlags) 0);
52 
53  // signal registration; use "BlockEvents (this)" to temp-ly block all signals
54  friend struct BlockEvents;
55  void connect (gpointer object, const char *name,
56  GCallback callback, gpointer data, bool after = true);
57  void blockSignals();
58  void unblockSignals();
59  struct Signals;
60  friend struct Signals;
61  Signals *m_signals;
62 
63  void construct (YWidget *ywidget, YWidget *yparent,
64  GType type, const char *property_name, va_list args);
65 
66  // data
67  GtkWidget *m_widget, *m_adj_size; // associated GtkWidget, and adjustment for borders
68  YWidget *m_ywidget; // associated YWidget
69 };
70 
72 {
73  BlockEvents (YGWidget *widget) : m_widget (widget)
74  { m_widget->blockSignals(); }
75  ~BlockEvents()
76  { m_widget->unblockSignals(); }
77 
78  private: YGWidget *m_widget;
79 };
80 
81 /*
82  * Macros to help implement proxies between common YWidget virtual
83  * methods and the (multiply inherited) YGWidget base implementation
84  * for GTK+.
85  */
86 #define YGWIDGET_IMPL_COMMON(ParentClass) \
87  virtual bool setKeyboardFocus() { \
88  return doSetKeyboardFocus(); } \
89  virtual void setEnabled (bool enabled) { \
90  ParentClass::setEnabled (enabled); \
91  doSetEnabled (enabled); \
92  } \
93  virtual int preferredWidth() { return doPreferredSize (YD_HORIZ); } \
94  virtual int preferredHeight() { return doPreferredSize (YD_VERT); } \
95  virtual void setSize (int width, int height) { doSetSize (width, height); }
96 
97 #define YGWIDGET_IMPL_USE_BOLD(ParentClass) \
98  virtual void setUseBoldFont (bool useBold) { \
99  ParentClass::setUseBoldFont (useBold); \
100  doSetUseBoldFont (useBold); \
101  }
102 
103 #define YGWIDGET_IMPL_CONTAINER(ParentClass) \
104  YGWIDGET_IMPL_COMMON (ParentClass) \
105  virtual void addChild (YWidget *ychild) { \
106  ParentClass::addChild (ychild); \
107  doAddChild (ychild, getContainer()); \
108  } \
109  virtual void removeChild (YWidget *ychild) { \
110  ParentClass::removeChild (ychild); \
111  doRemoveChild (ychild, getContainer()); \
112  }
113 
114 /* This is a convenience class that allows for a label next to the
115  intended widget. It should be used, in case you have the need for
116  such, as it gives an uniform API. */
117 class YGLabeledWidget : public YGWidget
118 {
119  public:
120  YGLabeledWidget(YWidget *ywidget, YWidget *yparent,
121  const std::string &label_text, YUIDimension label_ori,
122  GType type, const char *property_name, ...);
123  virtual ~YGLabeledWidget () {}
124 
125  virtual inline GtkWidget* getWidget() { return m_field; }
126 
127  void setLabelVisible (bool show);
128  void setBuddy (GtkWidget *widget);
129  virtual void doSetLabel (const std::string &label);
130 
131  YUIDimension orientation() { return m_orientation; }
132  GtkWidget *getLabelWidget() { return m_label; }
133 
134  protected:
135  GtkWidget *m_label, *m_field;
136  YUIDimension m_orientation;
137 };
138 
139 #define YGLABEL_WIDGET_IMPL(ParentClass) \
140  YGWIDGET_IMPL_COMMON (ParentClass) \
141  virtual void setLabel (const std::string &label) { \
142  ParentClass::setLabel (label); \
143  doSetLabel (label); \
144  }
145 
146 /* This is a convenience class for widgets that need scrollbars. */
148 {
149  public:
150  YGScrolledWidget(YWidget *ywidget, YWidget *yparent,
151  GType type, const char *property_name, ...);
152  // if you want a label, use:
153  YGScrolledWidget(YWidget *ywidget, YWidget *yparent,
154  const std::string &label_text, YUIDimension label_ori,
155  GType type, const char *property_name, ...);
156  virtual ~YGScrolledWidget () {}
157 
158  virtual inline GtkWidget *getWidget() { return m_widget; }
159 
160  // you should use this method, not gtk_scrolled_window_set...
161  void setPolicy (GtkPolicyType hpolicy, GtkPolicyType vpolicy);
162 
163  protected:
164  void construct(GType type, const char *property_name, va_list args);
165  GtkWidget *m_widget;
166 };
167 
168 #endif /*YGWIDGET_H*/
169