Library globals

Source canvas . gui . dialogs . InputDialog.nas

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
var InputDialog = {
  Ok:             0x0001,
  Cancel:         0x0002,
  new: func( title = "Input",
             label = "",
             text  = "" )
  {
    return {
      parents: [InputDialog],
      _title: title,
      _label: label,
      _text: text
    };
  },
  setTitle: func(title)
  {
    me._title = title;
    return me;
  },
  setLabel: func(label)
  {
    me._label = label;
    return me;
  },
  setText: func(text)
  {
    me._text = text;
    return me;
  },
  exec: func(cb = nil)
  {
    var MARGIN = 12;
    var dlg = canvas.Window.new([300,120], "dialog")
                           .setTitle(me._title);
    var root = dlg.getCanvas(1)
                  .set("background", style.getColor("bg_color"))
                  .createGroup();
    var vbox = VBoxLayout.new();
    vbox.setContentsMargin(MARGIN);
    dlg.setLayout(vbox);

    vbox.addItem(
      gui.widgets.Label.new(root, style, {wordWrap: 1})
                       .setText(me._label)
    );

    var input = gui.widgets.LineEdit.new(root, style, {});
    vbox.addItem(input);
    input.setText(me._text);
    input.setFocus();

    var button_box = HBoxLayout.new();
    vbox.addItem(button_box);

    button_box.addStretch(1);
    foreach(var button; [me.Ok, me.Cancel])
    {
      (func{
        var b_id = button;
        button_box.addItem(
          gui.widgets.Button.new(root, style, {})
                            .setText(me._button_names[button])
                            .listen("clicked", func {
                              dlg.del();
                              if( cb != nil )
                                cb(b_id, b_id == me.Ok ? input.text() : nil);
                            })
        );
      })();
    }

    var w = math.max(300, vbox.sizeHint()[0]);
    dlg.setSize(w, vbox.heightForWidth(w));

    return me;
  },
  # Show an input dialog to get a text string
  #
  # @param title
  # @param label
  # @param cb       Dialog close callback
  # @param text     Default text
  getText: func(title, label, cb, text = "")
  {
    var dlg = InputDialog.new(title, label, text);
    return dlg.exec(cb);
  },
# private:
  _button_names: {}
};

# Standard button names
InputDialog._button_names[ InputDialog.Ok     ] = "Ok";
InputDialog._button_names[ InputDialog.Cancel ] = "Cancel";