libyui-gtk  2.42.2
 All Classes
YGImage.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 "ygdkmngloader.h"
8 #include "YGUI.h"
9 #include "YGWidget.h"
10 #include "YImage.h"
11 #include "ygtkimage.h"
12 #include <string.h>
13 
14 static inline bool endsWith (const std::string &str1, const char *str2)
15 {
16  size_t len = strlen (str2);
17  if (str1.size() < len) return false;
18  return str1.compare (str1.size()-len, len, str2, len) == 0;
19 }
20 
21 class YGImage : public YImage, public YGWidget
22 {
23 public:
24  YGImage (YWidget *parent, const std::string &filename, bool animated)
25  : YImage (NULL, filename, animated),
26  YGWidget (this, parent, YGTK_TYPE_IMAGE, NULL)
27  {
28  YGtkImage *image = YGTK_IMAGE (getWidget());
29  const char *stock = NULL;
30  if (endsWith (filename, "/msg_question.png"))
31  stock = GTK_STOCK_DIALOG_QUESTION;
32  else if (endsWith (filename, "/msg_info.png"))
33  stock = GTK_STOCK_DIALOG_INFO;
34  else if (endsWith (filename, "/msg_warning.png"))
35  stock = GTK_STOCK_DIALOG_WARNING;
36  else if (endsWith (filename, "/msg_error.png"))
37  stock = GTK_STOCK_DIALOG_ERROR;
38 
39  GtkStyleContext *ctx;
40  ctx = gtk_widget_get_style_context(m_widget);
41 
42  if (stock && gtk_style_context_lookup_icon_set (ctx, stock)) {
43  GdkPixbuf *pixbuf = gtk_widget_render_icon_pixbuf (m_widget, stock, GTK_ICON_SIZE_DIALOG);
44  ygtk_image_set_from_pixbuf (image, pixbuf);
45  }
46  else
47  ygtk_image_set_from_file (image, filename.c_str(), animated);
48  }
49 
50  virtual void setAutoScale (bool scale)
51  {
52  YGtkImageAlign align = CENTER_IMAGE_ALIGN;
53  if (scale)
54  align = SCALE_IMAGE_ALIGN;
55  ygtk_image_set_props (YGTK_IMAGE (getWidget()), align, NULL);
56  }
57 
58  YGWIDGET_IMPL_COMMON (YImage)
59 };
60 
61 YImage *YGWidgetFactory::createImage (YWidget *parent, const std::string &filename, bool animated)
62 { return new YGImage (parent, filename, animated); }
63