libyui-gtk  2.49.0
YGWizard.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 #include "ygtkwizard.h"
11 #include "YWizard.h"
12 #include "YPushButton.h"
13 #include "YAlignment.h"
14 #include "YReplacePoint.h"
15 #include "YGDialog.h"
16 
17 class YGWizard : public YWizard, public YGWidget
18 {
19  YReplacePoint *m_replacePoint;
20 
21  /* YCP requires us to allow people to use YPushButton API on the wizard buttons.
22  Wizard already has handlers for them; this seems like bad design.
23 
24  We could support wrapping right in our framework. One way, would be to subclass
25  YWidgetOpt to have a wrapping field where we would set a GtkWidget*. Then,
26  classes should pass opt to YGWidget and it would create a shallow instance
27  around it. However, this isn't really doable. The problem is that, like
28  in this case, the API isn't really exact; events must be sent as YWizard events.
29  */
30  struct YGWButton : public YPushButton {
31  /* Thin class; just changes the associated button label and keeps track
32  of id change. */
33  YGWButton (YGWizard *parent, GtkWidget *widget, const std::string &label)
34  : YPushButton (parent, label), m_widget (widget), m_wizard (parent)
35  {
36  setWidgetRep (NULL);
37  setLabel (label);
38  ygtk_wizard_set_button_ptr_id (getWizard(), widget, this);
39  }
40 
41  virtual void setLabel (const std::string &label)
42  {
43  YPushButton::setLabel (label);
44 
45  // notice: we can't use functionKey() to deduce the icon because yast
46  // tools code differ from the text-mode to the GUIs when setting buttons
47  // up, and the opt_f10 and so on will not be set in the GUI code
48  YGtkWizard *wizard = getWizard();
49  std::string _label = YGUtils::mapKBAccel (label);
50  ygtk_wizard_set_button_label (wizard, getWidget(), _label.c_str(), NULL);
51  }
52 
53  virtual void setEnabled (bool enable)
54  {
55  YWidget::setEnabled (enable);
56  ygtk_wizard_enable_button (getWizard(), getWidget(), enable);
57  }
58 
59  virtual bool setKeyboardFocus()
60  {
61  gtk_widget_grab_focus (getWidget());
62  return gtk_widget_is_focus (getWidget());
63  }
64 
65  virtual int preferredWidth() { return 0; }
66  virtual int preferredHeight() { return 0; }
67  virtual void setSize (int w, int h) {}
68 
69  inline GtkWidget *getWidget() { return m_widget; }
70  inline YGtkWizard *getWizard() { return YGTK_WIZARD (m_wizard->getWidget()); }
71 
72  virtual void activate()
73  {
74  gtk_button_clicked(GTK_BUTTON (getWidget()));
75  }
76 
77  private:
78  GtkWidget *m_widget;
79  YGWizard *m_wizard;
80  };
81 
82  YGWButton *m_back_button, *m_abort_button, *m_next_button, *m_notes_button;
83  // release notes button would be a little more hassle to support; yast-qt
84  // doesn't support it too anyway.
85 
86 public:
87  YGWizard (YWidget *parent, const std::string &backButtonLabel,
88  const std::string &abortButtonLabel, const std::string &nextButtonLabel,
89  YWizardMode wizardMode)
90  : YWizard (NULL, backButtonLabel, abortButtonLabel, nextButtonLabel, wizardMode)
91  , YGWidget (this, parent, YGTK_TYPE_WIZARD, NULL)
92  {
93  setBorder (0);
94  YGtkWizard *wizard = getWizard();
95 
96  //** Application area
97  {
98  YAlignment *align = YUI::widgetFactory()->createAlignment (this,
99  YAlignCenter, YAlignCenter);
100  align->setStretchable (YD_HORIZ, true);
101  align->setStretchable (YD_VERT, true);
102 
103  m_replacePoint = YUI::widgetFactory()->createReplacePoint ((YWidget *) align);
104  YUI::widgetFactory()->createEmpty ((YWidget *) m_replacePoint);
105  m_replacePoint->showChild();
106  }
107 
108  //** Steps/tree pane
109  bool steps_enabled = wizardMode == YWizardMode_Steps;
110  bool tree_enabled = wizardMode == YWizardMode_Tree;
111  if (steps_enabled && tree_enabled) {
112  yuiError() << "YGWizard doesn't support both steps and tree enabled at the "
113  "same time.\nDisabling the steps...\n";
114  steps_enabled = false;
115  }
116  if (steps_enabled)
117  ygtk_wizard_enable_steps (wizard);
118  if (tree_enabled)
119  ygtk_wizard_enable_tree (wizard);
120 
121  //** Setting the bottom buttons
122  m_back_button = new YGWButton (this, wizard->back_button, backButtonLabel);
123  m_abort_button = new YGWButton (this, wizard->abort_button, abortButtonLabel);
124  m_next_button = new YGWButton (this, wizard->next_button, nextButtonLabel);
125  m_notes_button = new YGWButton (this, wizard->release_notes_button, std::string());
126  ygtk_wizard_set_default_button (wizard, wizard->next_button);
127 
128  //** All event are sent through this signal together with an id
129  g_signal_connect (G_OBJECT (getWidget()), "action-triggered",
130  G_CALLBACK (action_triggered_cb), this);
131  }
132 
133  virtual ~YGWizard()
134  {
135  // m_back/abort/next_button are added as children and
136  // so will be freed by ~YContainerWidget
137  }
138 
139  inline YGtkWizard *getWizard()
140  { return YGTK_WIZARD (getWidget()); }
141 
142  virtual YReplacePoint *contentsReplacePoint() const
143  { return m_replacePoint; }
144 
145  virtual YPushButton *backButton() const
146  { return m_back_button; }
147  virtual YPushButton *abortButton() const
148  { return m_abort_button; }
149  virtual YPushButton *nextButton() const
150  { return m_next_button; }
151 
152  virtual void setButtonLabel (YPushButton *button, const std::string &label)
153  {
154  button->setLabel (label);
155  }
156 
157  virtual void setHelpText (const std::string &_text)
158  {
159  std::string productName = YUI::app()->productName();
160  std::string text(_text);
161  YGUtils::replace (text, "&product;", 9, productName.c_str());
162  ygtk_wizard_set_help_text (getWizard(), text.c_str());
163  }
164 
165  virtual void setDialogIcon (const std::string &icon)
166  {
167  if (!ygtk_wizard_set_header_icon (getWizard(), icon.c_str()))
168  yuiWarning() << "YGWizard: could not load image: " << icon << std::endl;
169  YGDialog::currentDialog()->setIcon (icon);
170  }
171 
172  virtual void setDialogHeading (const std::string &heading)
173  {
174  ygtk_wizard_set_header_text (getWizard(), heading.c_str());
175  YGDialog::currentDialog()->setTitle (heading, false);
176  }
177 
178  virtual std::string getDialogHeading()
179  {
180  #warning YGWizard::getDialogHeading() not implemented yet
181  yuiWarning() << "YGWizard::getDialogHeading() not implemented yet" << std::endl;
182  return std::string();
183  }
184 
185  virtual void setDialogTitle (const std::string &title)
186  {
187  YGDialog::currentDialog()->setTitle (title, true);
188  }
189 
190  virtual std::string getDialogTitle()
191  {
192  #warning YGWizard::getDialogTitle() not implemented yet
193  yuiWarning() << "YGWizard::getDialogTitle() not implemented yet" << std::endl;
194  return std::string();
195  }
196 
197  virtual void addStepHeading (const std::string &text)
198  {
199  ygtk_wizard_add_step_header (getWizard(), text.c_str());
200  }
201 
202  virtual void addStep (const std::string &text, const std::string &id)
203  {
204  ygtk_wizard_add_step (getWizard(), text.c_str(), id.c_str());
205  }
206 
207  virtual void setCurrentStep (const std::string &id)
208  {
209  if (!ygtk_wizard_set_current_step (getWizard(), id.c_str()))
210  yuiError() << "YGWizard: there is no step with id " << id << std::endl;
211  }
212 
213  virtual void deleteSteps()
214  {
215  ygtk_wizard_clear_steps (getWizard());
216  }
217 
218  virtual void updateSteps()
219  {}
220 
221  virtual void addTreeItem (const std::string &parentID, const std::string &text,
222  const std::string &id)
223  {
224  if (!ygtk_wizard_add_tree_item (getWizard(), parentID.c_str(),
225  text.c_str(), id.c_str()))
226  yuiError() << "YGWizard: there is no tree item with id " << parentID << std::endl;
227  }
228 
229  virtual void selectTreeItem (const std::string &id)
230  {
231  if (!ygtk_wizard_select_tree_item (getWizard(), id.c_str()))
232  yuiError() << "YGWizard: there is no tree item with id " << id << std::endl;
233  }
234 
235  virtual std::string currentTreeSelection()
236  {
237  const char *selected = ygtk_wizard_get_tree_selection (getWizard());
238  if (selected)
239  return selected;
240  return std::string();
241  }
242 
243  virtual void deleteTreeItems()
244  {
245  ygtk_wizard_clear_tree (getWizard());
246  }
247 
248  virtual void addMenu (const std::string &text, const std::string &id)
249  {
250  std::string str = YGUtils::mapKBAccel (text);
251  ygtk_wizard_add_menu (getWizard(), str.c_str(), id.c_str());
252  }
253 
254  virtual void addSubMenu (const std::string &parentID, const std::string &text,
255  const std::string &id)
256  {
257  std::string str = YGUtils::mapKBAccel(text);
258  if (!ygtk_wizard_add_sub_menu (getWizard(), parentID.c_str(), str.c_str(),
259  id.c_str()))
260  yuiError() << "YGWizard: there is no menu item with id " << parentID << std::endl;
261  }
262 
263  virtual void addMenuEntry (const std::string &parentID, const std::string &text,
264  const std::string &id)
265  {
266  std::string str = YGUtils::mapKBAccel (text);
267  if (!ygtk_wizard_add_menu_entry (getWizard(), parentID.c_str(),
268  str.c_str(), id.c_str()))
269  yuiError() << "YGWizard: there is no menu item with id " << parentID << std::endl;
270  }
271 
272  virtual void addMenuSeparator (const std::string & parentID)
273  {
274  if (!ygtk_wizard_add_menu_separator (getWizard(), parentID.c_str()))
275  yuiError() << "YGWizard: there is no menu item with id " << parentID << std::endl;
276  }
277 
278  virtual void deleteMenus()
279  {
280  ygtk_wizard_clear_menu (getWizard());
281  }
282 
283  virtual void showReleaseNotesButton (const std::string &label, const std::string &id)
284  {
285  std::string str = YGUtils::mapKBAccel (label.c_str());
286  ygtk_wizard_set_button_label (getWizard(), m_notes_button->getWidget(), str.c_str(), NULL);
287  ygtk_wizard_set_button_str_id (getWizard(), m_notes_button->getWidget(), id.c_str());
288  }
289 
290  virtual void hideReleaseNotesButton()
291  {
292  ygtk_wizard_set_button_label (getWizard(), m_notes_button->getWidget(), NULL, NULL);
293  }
294 
295  virtual void retranslateInternalButtons()
296  {}
297 
298  static void action_triggered_cb (YGtkWizard *wizard, gpointer id,
299  gint id_type, YGWizard *pThis)
300  {
301  if ((GType) id_type == G_TYPE_STRING)
302  YGUI::ui()->sendEvent (new YMenuEvent ((char *) id));
303  else
304  YGUI::ui()->sendEvent (new YWidgetEvent ((YWidget *) id, YEvent::Activated));
305  }
306 
307  // YGWidget
308  virtual void doAddChild (YWidget *ychild, GtkWidget *container)
309  {
310  if (ychild->widgetRep()) // don't actually add the button wrappers
311  ygtk_wizard_set_child (getWizard(), YGWidget::get (ychild)->getLayout());
312  }
313 
314  YGWIDGET_IMPL_CONTAINER (YWizard)
315 };
316 
317 YWizard *YGOptionalWidgetFactory::createWizard (YWidget *parent,
318  const std::string &backButtonLabel, const std::string &abortButtonLabel,
319  const std::string &nextButtonLabel, YWizardMode wizardMode)
320 {
321  return new YGWizard (parent, backButtonLabel, abortButtonLabel, nextButtonLabel,
322  wizardMode);
323 }
324 
YGWizard
Definition: YGWizard.cc:18
_YGtkWizard
Definition: ygtkwizard.h:92
YGWidget
Definition: YGWidget.h:14