libyui-gtk  2.42.2
 All Classes
YGDialog.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 "YGDialog.h"
9 #include "YGUtils.h"
10 #include <YDialogSpy.h>
11 #include <gdk/gdkkeysyms.h>
12 #include <math.h> // easter
13 #include <string.h>
14 #include "ygtkwindow.h"
15 
16 /* In the main dialog case, it doesn't necessarly have a window of its own. If
17  there is already a main window, it should replace its content -- and when closed,
18  the previous dialog restored.
19 
20  Therefore, we have a YGDialog (the YDialog implementation), and a YGWindow
21  that does the windowing work and has a YWidget has its children, which can
22  be a YGDialog and is swap-able.
23 */
24 
25 //#define DEFAULT_WIDTH 750
26 //#define DEFAULT_HEIGHT 650
27 #define DEFAULT_CHAR_WIDTH 60
28 #define DEFAULT_CHAR_HEIGHT 28
29 #define DEFAULT_PIXEL_WIDTH 330
30 #define DEFAULT_PIXEL_HEIGHT 200
31 
32 class YGWindow;
33 static YGWindow *main_window = 0;
34 
35 class YGWindow
36 {
37  GtkWidget *m_widget;
38  int m_refcount;
39  // we keep a pointer of the child just for debugging
40  // (ie. dump yast tree)
41  YWidget *m_child;
42  GdkCursor *m_busyCursor;
43  bool m_isBusy;
44 
45 public:
46  YGWindowCloseFn m_canClose;
47  void *m_canCloseData;
48 
49  YGWindow (bool _main_window, YGDialog *ydialog)
50  {
51  m_widget = ygtk_window_new();
52  gtk_container_set_resize_mode (GTK_CONTAINER (m_widget), GTK_RESIZE_PARENT);
53  g_object_ref_sink (G_OBJECT (m_widget));
54  gtk_window_set_has_resize_grip (GTK_WINDOW (m_widget), TRUE);
55 
56  m_refcount = 0;
57  m_child = NULL;
58  m_canClose = NULL;
59  m_busyCursor = NULL;
60  m_isBusy = false;
61 
62  {
63  std::stack<YDialog *> &stack = YDialog::_dialogStack;
64  YDialog *ylast = stack.size() ? stack.top() : 0;
65  if (ylast == ydialog) {
66  if (stack.size() > 1) {
67  YDialog *t = ylast;
68  stack.pop();
69  ylast = stack.top();
70  stack.push (t);
71  }
72  else
73  ylast = NULL;
74  }
75 
76  GtkWindow *parent = NULL;
77  if (ylast) {
78  YGDialog *yglast = static_cast <YGDialog *> (ylast);
79  parent = GTK_WINDOW (yglast->m_window->getWidget());
80  }
81  GtkWindow *window = GTK_WINDOW (m_widget);
82 
83  if (parent) {
84  // if there is a parent, this would be a dialog
85  gtk_window_set_title (window, "");
86  gtk_window_set_modal (window, TRUE);
87  gtk_window_set_transient_for (window, parent);
88  gtk_window_set_type_hint (window, GDK_WINDOW_TYPE_HINT_DIALOG);
89  AtkObject *peer = gtk_widget_get_accessible (GTK_WIDGET (window));
90  if (peer != NULL)
91  atk_object_set_role (peer, ATK_ROLE_DIALOG);
92  }
93  else {
94 #ifdef LIBYUI_VERSION_NUM
95  #if LIBYUI_VERSION_AT_LEAST(2,42,3)
96  gtk_window_set_title (window, YUI::app()->applicationTitle().c_str());
97  GdkPixbuf *pixbuf = YGUtils::loadPixbuf (YUI::app()->applicationIcon());
98  if (pixbuf) { // default window icon
99  gtk_window_set_default_icon (pixbuf);
100  g_object_unref (G_OBJECT (pixbuf));
101  }
102  #else
103  // to be back compatible
104  gtk_window_set_title (window, "YaST");
105  #endif
106 #else
107  // to be back compatible
108  gtk_window_set_title (window, "YaST");
109 #endif
110  if (YGUI::ui()->unsetBorder())
111  gtk_window_set_decorated (window, FALSE);
112  }
113 
114  if (_main_window) {
115  // window default width is calculated as a proportion of a default
116  // char and pixel width to compensate for the fact that each widget's
117  // required size comes from a proportion of both parameters
118  int width = YGUtils::getCharsWidth (m_widget, DEFAULT_CHAR_WIDTH);
119  width += DEFAULT_PIXEL_WIDTH;
120  int height = YGUtils::getCharsHeight (m_widget, DEFAULT_CHAR_HEIGHT);
121  height += DEFAULT_PIXEL_HEIGHT;
122 
123  if (YGUI::ui()->isSwsingle())
124  height += YGUtils::getCharsHeight (m_widget, 10);
125 
126  width = MIN (width, YUI::app()->displayWidth());
127  height = MIN (height, YUI::app()->displayHeight());
128 
129  gtk_window_set_default_size (window, width, height);
130  if (YGUI::ui()->setFullscreen())
131  gtk_window_fullscreen (window);
132  else if (YUI::app()->displayWidth() <= 800 || YUI::app()->displayHeight() <= 600)
133  // maximize window for small displays
134  gtk_window_maximize (window);
135  }
136 
137  gtk_window_set_role (window, "yast2");
138  }
139 
140  if (_main_window)
141  main_window = this;
142 
143  g_signal_connect (G_OBJECT (m_widget), "delete-event",
144  G_CALLBACK (close_window_cb), this);
145  g_signal_connect_after (G_OBJECT (m_widget), "key-press-event",
146  G_CALLBACK (key_pressed_cb), this);
147  g_signal_connect (G_OBJECT (m_widget), "focus-in-event",
148  G_CALLBACK (focus_in_event_cb), this);
149  // set busy cursor at start
150  g_signal_connect_after (G_OBJECT (m_widget), "realize",
151  G_CALLBACK (realize_cb), this);
152  }
153 
154  ~YGWindow()
155  {
156  setChild (NULL);
157  if (m_busyCursor)
158  g_object_unref (G_OBJECT (m_busyCursor));
159  gtk_widget_destroy (m_widget);
160  g_object_unref (G_OBJECT (m_widget));
161  }
162 
163  void show()
164  { gtk_widget_show (m_widget); }
165 
166  void normalCursor()
167  {
168  if (m_isBusy)
169  gdk_window_set_cursor (gtk_widget_get_window(m_widget), NULL);
170  m_isBusy = false;
171  }
172 
173  void busyCursor()
174  {
175  if (!m_busyCursor) {
176  GdkDisplay *display = gtk_widget_get_display (m_widget);
177  m_busyCursor = gdk_cursor_new_for_display (display, GDK_WATCH);
178  g_object_ref (G_OBJECT (m_busyCursor));
179  }
180  if (!m_isBusy)
181  gdk_window_set_cursor (gtk_widget_get_window(m_widget), m_busyCursor);
182  m_isBusy = true;
183  }
184 
185  void setChild (YWidget *new_child)
186  {
187  GtkWidget *child = gtk_bin_get_child (GTK_BIN (m_widget));
188  if (child)
189  gtk_container_remove (GTK_CONTAINER (m_widget), child);
190  if (new_child) {
191  child = YGWidget::get (new_child)->getLayout();
192  gtk_container_add (GTK_CONTAINER (m_widget), child);
193  }
194  m_child = new_child;
195  }
196 
197  static void ref (YGWindow *window)
198  {
199  window->m_refcount++;
200  }
201 
202  static void unref (YGWindow *window)
203  {
204  if (--window->m_refcount == 0) {
205  bool is_main_window = (window == main_window);
206  delete window;
207  if (is_main_window)
208  main_window = NULL;
209  }
210  }
211 
212  // Y(G)Widget-like methods
213  GtkWidget *getWidget() { return m_widget; }
214  YWidget *getChild() { return m_child; }
215 
216 private:
217  void close()
218  {
219  if (!m_canClose || m_canClose (m_canCloseData))
220  YGUI::ui()->sendEvent (new YCancelEvent());
221  }
222 
223  static gboolean close_window_cb (GtkWidget *widget, GdkEvent *event,
224  YGWindow *pThis)
225  {
226  // never let GTK+ destroy the window! just inform YCP, and let it
227  // do its thing.
228  pThis->close();
229  return TRUE;
230  }
231 
232  static gboolean key_pressed_cb (GtkWidget *widget, GdkEventKey *event,
233  YGWindow *pThis)
234  {
235  // if not main dialog, close it on escape
236  if (event->keyval == GDK_KEY_Escape &&
237  /* not main window */ main_window != pThis) {
238  pThis->close();
239  return TRUE;
240 
241  }
242 
243  if (event->state & GDK_SHIFT_MASK) {
244  switch (event->keyval) {
245  case GDK_KEY_F8:
246  YGUI::ui()->askSaveLogs();
247  return TRUE;
248  default:
249  break;
250  }
251  }
252  if ((event->state & GDK_CONTROL_MASK) && (event->state & GDK_SHIFT_MASK)
253  && (event->state & GDK_MOD1_MASK)) {
254  yuiMilestone() << "Caught YaST2 magic key combination\n";
255  int ret = -1;
256  switch (event->keyval) {
257  case GDK_KEY_S:
258  YGUI::ui()->makeScreenShot();
259  return TRUE;
260  case GDK_KEY_M:
261  YGUI::ui()->toggleRecordMacro();
262  return TRUE;
263  case GDK_KEY_P:
264  YGUI::ui()->askPlayMacro();
265  return TRUE;
266  case GDK_KEY_D:
267  YGUI::ui()->sendEvent (new YDebugEvent());
268  return TRUE;
269  case GDK_KEY_X:
270  yuiMilestone() << "Starting xterm\n";
271  ret = system ("/usr/bin/xterm &");
272  if (ret != 0)
273  yuiError() << "Can't launch xterm (error code" << ret << ")" << std::endl;
274  return TRUE;
275  case GDK_KEY_Y:
276  yuiMilestone() << "Opening dialog spy" << std::endl;
277  YDialogSpy::showDialogSpy();
278  YGUI::ui()->normalCursor();
279  break;
280  default:
281  break;
282  }
283  }
284  return FALSE;
285  }
286 
287  static gboolean focus_in_event_cb (GtkWidget *widget, GdkEventFocus *event)
288  { gtk_window_set_urgency_hint (GTK_WINDOW (widget), FALSE); return FALSE; }
289 
290  static void realize_cb (GtkWidget *widget, YGWindow *pThis)
291  { pThis->busyCursor(); }
292 };
293 
294 YGDialog::YGDialog (YDialogType dialogType, YDialogColorMode colorMode)
295  : YDialog (dialogType, colorMode),
296  YGWidget (this, NULL, GTK_TYPE_HBOX, NULL)
297 {
298  setBorder (0);
299  m_stickyTitle = false;
300  m_containee = gtk_event_box_new();
301  if (dialogType == YMainDialog && main_window)
302  m_window = main_window;
303  else
304  m_window = new YGWindow (dialogType == YMainDialog, this);
305  YGWindow::ref (m_window);
306 
307  if (colorMode != YDialogNormalColor) {
308  // emulate a warning / info dialog
309  GtkWidget *icon = gtk_image_new_from_stock
310  (colorMode == YDialogWarnColor ? GTK_STOCK_DIALOG_WARNING : GTK_STOCK_DIALOG_INFO,
311  GTK_ICON_SIZE_DIALOG);
312  gtk_misc_set_alignment (GTK_MISC (icon), 0.5, 0);
313  gtk_misc_set_padding (GTK_MISC (icon), 0, 12);
314 
315  gtk_box_pack_start (GTK_BOX (getWidget()), icon, FALSE, FALSE, 12);
316  gtk_box_pack_start (GTK_BOX (getWidget()), m_containee, TRUE, TRUE, 0);
317  }
318  else
319  gtk_box_pack_start (GTK_BOX (getWidget()), m_containee, TRUE, TRUE, 0);
320  gtk_widget_show_all (getWidget());
321 
322  // NOTE: we need to add this containter to the window right here, else
323  // weird stuff happens (like if we set a pango font description to a
324  // GtkLabel, size request would output the size without that description
325  // set...)
326  m_window->setChild (this);
327 }
328 
329 YGDialog::~YGDialog()
330 {
331  YGWindow::unref (m_window);
332 }
333 
334 void YGDialog::openInternal()
335 {
336  m_window->show();
337 }
338 
339 void YGDialog::activate()
340 {
341  m_window->setChild (this);
342 }
343 
344 void YGDialog::present()
345 {
346  GtkWindow *window = GTK_WINDOW (m_window->getWidget());
347  if (!gtk_window_is_active (window))
348  gtk_window_set_urgency_hint (window, TRUE);
349 }
350 
351 YGDialog *YGDialog::currentDialog()
352 {
353  YDialog *ydialog = YDialog::currentDialog (false);
354  if (ydialog)
355  return static_cast <YGDialog *> (ydialog);
356  return NULL;
357 }
358 
359 GtkWindow *YGDialog::currentWindow()
360 {
361  YGDialog *ydialog = YGDialog::currentDialog();
362  if (ydialog)
363  return GTK_WINDOW (ydialog->m_window->getWidget());
364  return NULL;
365 }
366 
367 void YGDialog::setCloseCallback (YGWindowCloseFn canClose, void *canCloseData)
368 {
369  m_window->m_canClose = canClose;
370  m_window->m_canCloseData = canCloseData;
371 }
372 
373 void YGDialog::unsetCloseCallback()
374 {
375  m_window->m_canClose = NULL;
376 }
377 
378 void YGDialog::normalCursor()
379 {
380  m_window->normalCursor();
381 }
382 
383 void YGDialog::busyCursor()
384 {
385  m_window->busyCursor();
386 }
387 
388 // YWidget
389 
390 void YGDialog::doSetSize (int width, int height)
391 {
392  // libyui calls YDialog::setSize() to force a geometry recalculation as a
393  // result of changed layout properties
394  bool resize = false;
395  GtkWidget *window = m_window->getWidget();
396  if (gtk_widget_get_realized (window)) {
397  gtk_widget_queue_resize (window);
398  width = MIN (width, YUI::app()->displayWidth());
399  height = MIN (height, YUI::app()->displayHeight());
400  if (isMainDialog()) {
401  GtkAllocation allocation;
402  gtk_widget_get_allocation(window, &allocation);
403  if (allocation.width < width || allocation.height < height) {
404  resize = true;
405  width = MAX (width, allocation.width),
406  height = MAX (height, allocation.height);
407  }
408  }
409  else
410  resize = true;
411  }
412  if (resize)
413  gtk_window_resize (GTK_WINDOW (window), width, height);
414  else
415  gtk_window_set_default_size (GTK_WINDOW (window), width, height);
416 }
417 
418 void YGDialog::highlight (YWidget *ywidget)
419 {
420  struct inner {
421  static gboolean draw_highlight_cb (GtkWidget *widget, cairo_t *cr)
422  {
423  int w = gtk_widget_get_allocated_width(widget);
424  int h = gtk_widget_get_allocated_height(widget);
425 
426  cairo_rectangle (cr, 0, 0, w, h);
427  cairo_set_source_rgb (cr, 0xff/255.0, 0x88/255.0, 0);
428  cairo_fill (cr);
429  return FALSE;
430  }
431 
432  static bool hasWindow (GtkWidget *widget)
433  {
434  if (gtk_widget_get_has_window(widget))
435  return true;
436  // widgets like GtkButton add their windows to parent's
437  for (GList *children = gdk_window_peek_children (gtk_widget_get_window(widget));
438  children; children = children->next) {
439  GdkWindow *child = (GdkWindow *) children->data;
440  gpointer data;
441  gdk_window_get_user_data (child, &data);
442  if ((GtkWidget *) data == widget)
443  return true;
444  }
445  return false;
446  }
447 
448  };
449  static YWidget *previousWidget = NULL;
450  if (previousWidget && previousWidget->isValid()) {
451  YGWidget *prev = YGWidget::get (previousWidget);
452  if (prev) {
453  GtkWidget *widget = prev->getWidget();
454  if (inner::hasWindow (widget)) {
455  gtk_widget_override_background_color (widget, GTK_STATE_FLAG_NORMAL, NULL);
456  gtk_widget_override_color (widget, GTK_STATE_FLAG_NORMAL, NULL);
457  }
458  else {
459  g_signal_handlers_disconnect_by_func (widget,
460  (gpointer) inner::draw_highlight_cb, NULL);
461  gtk_widget_queue_draw (widget);
462  }
463  }
464  }
465  if (ywidget) {
466  YGWidget *ygwidget = YGWidget::get (ywidget);
467  if (ygwidget) {
468  GtkWidget *widget = ygwidget->getWidget();
469  if (inner::hasWindow (widget)) {
470  GdkRGBA bg_color = { 0, 0xffff, 0xaaaa, 0 };
471  GdkRGBA base_color = { 0, 0xffff, 0xeeee, 0 };
472  gtk_widget_override_background_color (widget, GTK_STATE_FLAG_NORMAL, &bg_color);
473  gtk_widget_override_color (widget, GTK_STATE_FLAG_NORMAL, &base_color);
474  }
475  else {
476  g_signal_connect (G_OBJECT (widget), "draw",
477  G_CALLBACK (inner::draw_highlight_cb), NULL);
478  gtk_widget_queue_draw (widget);
479  }
480  }
481  }
482  previousWidget = ywidget;
483 }
484 
485 void YGDialog::setTitle (const std::string &title, bool sticky)
486 {
487  if (title.empty())
488  return;
489  if (!m_stickyTitle || sticky) {
490  GtkWindow *window = GTK_WINDOW (m_window->getWidget());
491  gchar *str = g_strdup_printf ("%s - YaST", title.c_str());
492  gtk_window_set_title (window, str);
493  g_free (str);
494  m_stickyTitle = sticky;
495  }
496  present();
497 }
498 
499 extern "C" {
500  void ygdialog_setTitle (const gchar *title, gboolean sticky);
501 };
502 
503 void ygdialog_setTitle (const gchar *title, gboolean sticky)
504 {
505  YGDialog::currentDialog()->setTitle (title, sticky);
506 }
507 
508 void YGDialog::setIcon (const std::string &icon)
509 {
510  GtkWindow *window = GTK_WINDOW (m_window->getWidget());
511  GdkPixbuf *pixbuf = YGUtils::loadPixbuf (icon);
512  if (pixbuf) {
513  gtk_window_set_icon (window, pixbuf);
514  g_object_unref (G_OBJECT (pixbuf));
515  }
516 }
517 
518 typedef bool (*FindWidgetsCb) (YWidget *widget, void *data) ;
519 
520 static void findWidgets (
521  std::list <YWidget *> *widgets, YWidget *widget, FindWidgetsCb find_cb, void *cb_data)
522 {
523  if (find_cb (widget, cb_data))
524  widgets->push_back (widget);
525  for (YWidgetListConstIterator it = widget->childrenBegin();
526  it != widget->childrenEnd(); it++)
527  findWidgets (widgets, *it, find_cb, cb_data);
528 }
529 
530 static bool IsFunctionWidget (YWidget *widget, void *data)
531 { return widget->functionKey() == GPOINTER_TO_INT (data); }
532 
533 YWidget *YGDialog::getFunctionWidget (int key)
534 {
535  std::list <YWidget *> widgets;
536  findWidgets (&widgets, this, IsFunctionWidget, GINT_TO_POINTER (key));
537  return widgets.empty() ? NULL : widgets.front();
538 }
539 
540 static bool IsClassWidget (YWidget *widget, void *data)
541 { return !strcmp (widget->widgetClass(), (char *) data); }
542 
543 std::list <YWidget *> YGDialog::getClassWidgets (const char *className)
544 {
545  std::list <YWidget *> widgets;
546  findWidgets (&widgets, this, IsClassWidget, (void *) className);
547  return widgets;
548 }
549 
550 YDialog *YGWidgetFactory::createDialog (YDialogType dialogType, YDialogColorMode colorMode)
551 { return new YGDialog (dialogType, colorMode); }
552 
553 YEvent *YGDialog::waitForEventInternal (int timeout_millisec)
554 { return YGUI::ui()->waitInput (timeout_millisec, true); }
555 
556 YEvent *YGDialog::pollEventInternal()
557 { return YGUI::ui()->waitInput (0, false); }
558