libyui-gtk  2.44.9
YGFrame.cc
1 /********************************************************************
2  * YaST2-GTK - http://en.opensuse.org/YaST2-GTK *
3  ********************************************************************/
4 
5 #define YUILogComponent "gtk"
6 #include <yui/Libyui_config.h>
7 #include "YGUI.h"
8 #include "YGWidget.h"
9 #include "YGUtils.h"
10 
11 // Instead of traditional looking frames, we use Gnome convention for the
12 // frame's look. That is: don't draw a frame, use bold header and pad the child.
13 #define CHILD_INDENTATION 8
14 
15 class YGBaseFrame : public YGWidget
16 {
17 protected:
18 // a GtkAlignment to set some indentation on the child
19 GtkWidget *m_containee;
20 
21 public:
22  YGBaseFrame (YWidget *ywidget, YWidget *parent)
23  : YGWidget (ywidget, parent,
24  GTK_TYPE_FRAME, "shadow-type", GTK_SHADOW_NONE, NULL)
25  {
26  m_containee = gtk_widget_new (GTK_TYPE_FRAME, NULL);
27 
28  // gtk_alignment_set_padding (GTK_ALIGNMENT (m_containee),
29  // 0, 0, CHILD_INDENTATION, 0);
30  gtk_widget_set_margin_top (m_containee, 0);
31  gtk_widget_set_margin_bottom (m_containee, 0);
32  gtk_widget_set_margin_start (m_containee, CHILD_INDENTATION);
33  gtk_widget_set_margin_end (m_containee, 0);
34 
35  gtk_widget_show (m_containee);
36  gtk_container_add (GTK_CONTAINER (getWidget()), m_containee);
37  }
38 
39  virtual GtkWidget *getContainer()
40  { return m_containee; }
41 };
42 
43 #include "YFrame.h"
44 
45 static GtkWidget *findFirstFocusable (GtkContainer *container)
46 {
47  g_return_val_if_fail (container != NULL, NULL);
48 
49  for (GList *l = gtk_container_get_children (container);
50  l; l = l->next) {
51  if (gtk_widget_get_can_focus (GTK_WIDGET(l->data)))
52  return GTK_WIDGET (l->data);
53  else if (GTK_IS_CONTAINER (l->data)) {
54  GtkWidget *ret = findFirstFocusable (GTK_CONTAINER (l->data));
55  if (ret)
56  return ret;
57  }
58  }
59  return NULL;
60 }
61 
62 extern "C" {
63  static gboolean
64  frame_label_mnemonic_activate (GtkWidget *widget,
65  gboolean group_cycling,
66  GtkContainer *frame_container)
67  {
68  GtkWidget *focusable = findFirstFocusable (frame_container);
69  if (focusable == NULL) {
70  g_warning ("no focusable widgets for mnemonic");
71  return FALSE;
72  } else
73  return gtk_widget_mnemonic_activate (focusable, group_cycling);
74  }
75 }
76 
77 class YGFrame : public YFrame, public YGBaseFrame
78 {
79 public:
80  YGFrame (YWidget *parent, const std::string &label)
81  : YFrame (NULL, label),
82  YGBaseFrame (this, parent)
83  {
84  GtkWidget *label_widget = gtk_label_new_with_mnemonic ("");
85  g_signal_connect (G_OBJECT (label_widget), "mnemonic_activate",
86  G_CALLBACK (frame_label_mnemonic_activate),
87  getWidget());
88  YGUtils::setWidgetFont (GTK_WIDGET (label_widget), PANGO_STYLE_NORMAL,
89  PANGO_WEIGHT_BOLD, PANGO_SCALE_MEDIUM);
90  gtk_widget_show (label_widget);
91  gtk_frame_set_label_widget (GTK_FRAME (getWidget()), label_widget);
92  setLabel (label);
93  }
94 
95  // YFrame
96  virtual void setLabel (const std::string &_str)
97  {
98  GtkWidget *label = gtk_frame_get_label_widget (GTK_FRAME (getWidget()));
99  std::string str (YGUtils::mapKBAccel (_str));
100  gtk_label_set_text_with_mnemonic (GTK_LABEL (label), str.c_str());
101  YFrame::setLabel (_str);
102  }
103 
104  YGWIDGET_IMPL_CONTAINER (YFrame)
105 
106  // YGWidget
107  virtual std::string getDebugLabel() const
108  { return label(); }
109 };
110 
111 
112 YFrame *YGWidgetFactory::createFrame (YWidget *parent, const std::string &label)
113 { return new YGFrame (parent, label); }
114 
115 #include "YCheckBoxFrame.h"
116 
117 class YGCheckBoxFrame : public YCheckBoxFrame, public YGBaseFrame
118 {
119 public:
120  YGCheckBoxFrame (YWidget *parent, const std::string &label, bool checked)
121  : YCheckBoxFrame (NULL, label, checked),
122  YGBaseFrame (this, parent)
123  {
124  GtkWidget *button = gtk_check_button_new_with_mnemonic("");
125  YGUtils::setWidgetFont (gtk_bin_get_child (GTK_BIN (button)), PANGO_STYLE_NORMAL,
126  PANGO_WEIGHT_BOLD, PANGO_SCALE_MEDIUM);
127  gtk_widget_show_all (button);
128  gtk_frame_set_label_widget (GTK_FRAME (getWidget()), button);
129 
130  setLabel (label);
131  setValue (checked);
132  connect (button, "toggled", G_CALLBACK (toggled_cb), this);
133  }
134 
135  // YCheckBoxFrame
136  virtual void setLabel (const std::string &_str)
137  {
138  GtkWidget *button = gtk_frame_get_label_widget (GTK_FRAME (getWidget()));
139  GtkLabel *label = GTK_LABEL (gtk_bin_get_child(GTK_BIN (button)));
140 
141  std::string str (YGUtils::mapKBAccel (_str));
142  gtk_label_set_text_with_mnemonic (label, str.c_str());
143  YCheckBoxFrame::setLabel (_str);
144  }
145 
146  bool value()
147  {
148  GtkWidget *button = gtk_frame_get_label_widget (GTK_FRAME (getWidget()));
149  return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
150  }
151 
152  void setValue (bool value)
153  {
154  BlockEvents (this);
155  GtkWidget *button = gtk_frame_get_label_widget (GTK_FRAME (getWidget()));
156  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), value);
157  }
158 
159  // YGWidget
160  virtual void doSetEnabled (bool enabled)
161  {
162  GtkWidget *frame = getWidget();
163  if (enabled) {
164  gtk_widget_set_sensitive (frame, TRUE);
165  handleChildrenEnablement (value());
166  }
167  else {
168  gtk_widget_set_sensitive (frame, FALSE);
169  YWidget::setChildrenEnabled (false);
170  }
171  YWidget::setEnabled (enabled);
172  }
173 
174  YGWIDGET_IMPL_CONTAINER (YCheckBoxFrame)
175 
176 private:
177  static void toggled_cb (GtkWidget *widget, YGCheckBoxFrame *pThis)
178  {
179  pThis->setEnabled (true);
180  if (pThis->notify())
181  YGUI::ui()->sendEvent (new YWidgetEvent (pThis, YEvent::ValueChanged));
182  }
183 };
184 
185 YCheckBoxFrame *YGWidgetFactory::createCheckBoxFrame (
186  YWidget *parent, const std::string &label, bool checked)
187 { return new YGCheckBoxFrame (parent, label, checked); }
188 
YGFrame
Definition: YGFrame.cc:77
BlockEvents
Definition: YGWidget.h:76
YGCheckBoxFrame
Definition: YGFrame.cc:117
YGWidget
Definition: YGWidget.h:13
YGBaseFrame
Definition: YGFrame.cc:15