input_file.vala 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. public class InputFile : InputField, Gtk.Button
  8. {
  9. public string? _path;
  10. public Gtk.FileChooserAction _action;
  11. public Gtk.Label _label;
  12. public void set_inconsistent(bool inconsistent)
  13. {
  14. }
  15. public bool is_inconsistent()
  16. {
  17. return false;
  18. }
  19. public GLib.Value union_value()
  20. {
  21. return this.value;
  22. }
  23. public void set_union_value(GLib.Value v)
  24. {
  25. this.value = (string)v;
  26. }
  27. public string? value
  28. {
  29. get
  30. {
  31. return _path;
  32. }
  33. set
  34. {
  35. if (value == null) {
  36. _path = null;
  37. _label.set_text("(None)");
  38. } else {
  39. GLib.File f = GLib.File.new_for_path(value);
  40. _path = f.get_path();
  41. _label.set_text(f.get_basename());
  42. }
  43. }
  44. }
  45. public InputFile(Gtk.FileChooserAction action = Gtk.FileChooserAction.OPEN)
  46. {
  47. _path = null;
  48. _action = action;
  49. _label = new Gtk.Label("(None)");
  50. _label.xalign = 0.0f;
  51. this.add(_label);
  52. this.clicked.connect(on_selector_clicked);
  53. }
  54. private void on_selector_clicked()
  55. {
  56. string label = _action == Gtk.FileChooserAction.SELECT_FOLDER ? "Folder" : "File";
  57. Gtk.FileChooserDialog dlg = new Gtk.FileChooserDialog("Select %s".printf(label)
  58. , (Gtk.Window)this.get_toplevel()
  59. , _action
  60. , "Cancel"
  61. , Gtk.ResponseType.CANCEL
  62. , "Open"
  63. , Gtk.ResponseType.ACCEPT
  64. );
  65. dlg.response.connect((response_id) => {
  66. if (response_id == Gtk.ResponseType.ACCEPT)
  67. this.value = dlg.get_file().get_path();
  68. dlg.destroy();
  69. });
  70. dlg.show_all();
  71. }
  72. }
  73. } /* namespace Crown */