input_file.vala 1.7 KB

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