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