utility.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. namespace hiro {
  2. #if defined(Hiro_Color)
  3. static auto CreateColor(const Color& color) -> GdkColor {
  4. GdkColor gdkColor;
  5. gdkColor.pixel = (color.red() << 16) | (color.green() << 8) | (color.blue() << 0);
  6. gdkColor.red = (color.red() << 8) | (color.red() << 0);
  7. gdkColor.green = (color.green() << 8) | (color.green() << 0);
  8. gdkColor.blue = (color.blue() << 8) | (color.blue() << 0);
  9. return gdkColor;
  10. }
  11. #endif
  12. static auto CreatePixbuf(image icon, bool scale = false) -> GdkPixbuf* {
  13. if(!icon) return nullptr;
  14. if(scale) icon.scale(15, 15);
  15. icon.transform(0, 32, 255u << 24, 255u << 0, 255u << 8, 255u << 16); //GTK stores images in ABGR format
  16. auto pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, icon.width(), icon.height());
  17. memory::copy(gdk_pixbuf_get_pixels(pixbuf), icon.data(), icon.size());
  18. if(scale) {
  19. auto scaled = gdk_pixbuf_scale_simple(pixbuf, 15, 15, GDK_INTERP_BILINEAR);
  20. g_object_unref(pixbuf);
  21. pixbuf = scaled;
  22. }
  23. return pixbuf;
  24. }
  25. static auto CreateImage(const image& icon, bool scale = false) -> GtkImage* {
  26. auto pixbuf = CreatePixbuf(icon, scale);
  27. auto gtkIcon = (GtkImage*)gtk_image_new_from_pixbuf(pixbuf);
  28. g_object_unref(pixbuf);
  29. return gtkIcon;
  30. }
  31. static auto DropPaths(GtkSelectionData* data) -> vector<string> {
  32. gchar** uris = gtk_selection_data_get_uris(data);
  33. if(uris == nullptr) return {};
  34. vector<string> paths;
  35. for(uint n = 0; uris[n] != nullptr; n++) {
  36. gchar* pathname = g_filename_from_uri(uris[n], nullptr, nullptr);
  37. if(pathname == nullptr) continue;
  38. string path = pathname;
  39. g_free(pathname);
  40. if(directory::exists(path) && !path.endsWith("/")) path.append("/");
  41. paths.append(path);
  42. }
  43. g_strfreev(uris);
  44. return paths;
  45. }
  46. }