OSDN Git Service

Some features of Dialog class are modified; Dialog#item now returns DialogItem object...
authortoshinagata1964 <toshinagata1964@a2be9bc6-48de-4e38-9406-05402d4bc13c>
Sat, 20 Mar 2010 10:38:41 +0000 (10:38 +0000)
committertoshinagata1964 <toshinagata1964@a2be9bc6-48de-4e38-9406-05402d4bc13c>
Sat, 20 Mar 2010 10:38:41 +0000 (10:38 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/molby/trunk@24 a2be9bc6-48de-4e38-9406-05402d4bc13c

MolLib/Ruby_bind/ruby_dialog.c
Scripts/commands.rb
Scripts/dialog.rb
Scripts/gamess.rb
Scripts/md.rb

index 968e297..9fbb772 100644 (file)
@@ -396,7 +396,7 @@ s_RubyDialog_Initialize(int argc, VALUE *argv, VALUE self)
 }
 
 static int
-s_RubyDialog_ItemIndexForTag(VALUE self, VALUE tag)
+s_RubyDialog_ItemIndexForTagNoRaise(VALUE self, VALUE tag)
 {
        VALUE items = rb_iv_get(self, "_items");
        int len = RARRAY_LEN(items);
@@ -405,21 +405,38 @@ s_RubyDialog_ItemIndexForTag(VALUE self, VALUE tag)
        if (FIXNUM_P(tag)) {
                i = NUM2INT(tag);
                if (i < 0 || i >= len)
-                       rb_raise(rb_eMolbyError, "item number (%d) out of range", i);
-               return i;
+                       return -1;
+               else return i;
        }
        for (i = 0; i < len; i++) {
                if (rb_equal(tag, rb_ivar_get(ptr[i], SYM2ID(sTagSymbol))) == Qtrue)
                        return i;
        }
-       rb_raise(rb_eStandardError, "RubyDialog has no item with tag %s", StringValuePtr(tag));
-       return -1; /* Not reached */
+       return -2;
+}
+
+static int
+s_RubyDialog_ItemIndexForTag(VALUE self, VALUE tag)
+{
+       int i = s_RubyDialog_ItemIndexForTagNoRaise(self, tag);
+       if (i == -1)
+               rb_raise(rb_eStandardError, "item number (%d) out of range", i);
+       else if (i == -2)
+               rb_raise(rb_eStandardError, "Dialog has no item with tag %s", StringValuePtr(tag));
+       return i;
 }
 
+/*
+ *  call-seq:
+ *     item_at_index(index) -> DialogItem
+ *
+ *  Get the dialog item at index. If no such item exists, exception is raised.
+ */
 static VALUE
-s_RubyDialog_ItemAtIndex(VALUE self, int idx)
+s_RubyDialog_ItemAtIndex(VALUE self, VALUE ival)
 {
        VALUE items;
+       int idx = NUM2INT(rb_Integer(ival));
        items = rb_iv_get(self, "_items");
        if (idx < 0 || idx >= RARRAY_LEN(items))
                rb_raise(rb_eRangeError, "item index (%d) out of range", idx);
@@ -428,6 +445,22 @@ s_RubyDialog_ItemAtIndex(VALUE self, int idx)
 
 /*
  *  call-seq:
+ *     item_with_tag(tag) -> DialogItem
+ *
+ *  Get the dialog item which has the given tag. If no such item exists, returns nil.
+ */
+static VALUE
+s_RubyDialog_ItemWithTag(VALUE self, VALUE tval)
+{
+       int idx = s_RubyDialog_ItemIndexForTagNoRaise(self, tval);
+       if (idx >= 0) {
+               VALUE items = rb_iv_get(self, "_items");
+               return RARRAY_PTR(items)[idx];
+       } else return Qnil;
+}
+
+/*
+ *  call-seq:
  *     set_attr(tag, hash)
  *
  *  Set the attributes given in the hash.
@@ -448,7 +481,7 @@ s_RubyDialog_SetAttr(VALUE self, VALUE tag, VALUE hash)
                VALUE val = rb_hash_aref(hash, key);
                s_RubyDialogItem_SetAttr(item, key, val);
        }
-       return Qnil;
+       return item;
 }
 
 /*
@@ -484,6 +517,8 @@ s_RubyDialog_Run(VALUE self)
        retval = RubyDialogCallback_runModal(dref);
        Ruby_SetInterruptFlag(iflag);
        RubyDialogCallback_close(dref);
+       return rb_iv_get(self, "_retval");
+#if 0
        if (retval == 0) {
                VALUE items = rb_iv_get(self, "_items");
                int len = RARRAY_LEN(items);
@@ -504,6 +539,7 @@ s_RubyDialog_Run(VALUE self)
                return hash;
        } else
                return Qfalse;
+#endif
 }
 
 /*
@@ -519,7 +555,7 @@ s_RubyDialog_Run(VALUE self)
 static VALUE
 s_RubyDialog_Layout(int argc, VALUE *argv, VALUE self)
 {
-       VALUE items, oval, *opts;
+       VALUE items, oval, *opts, new_item;
        int row, col, i, j, n, itag, nitems, *itags;
        RubyDialog *dref;
        float *widths, *heights;
@@ -644,45 +680,6 @@ s_RubyDialog_Layout(int argc, VALUE *argv, VALUE self)
        layoutFrame.origin.y = margin;
 /*     printf("layoutFrame = [%f,%f,%f,%f]\n", layoutFrame.origin.x, layoutFrame.origin.y, layoutFrame.size.width, layoutFrame.size.height); */
 
-#if 0
-       /*  Resize the window  */
-       /*  Not necessary for wxWidgets, because contentSizer and buttonSizer will take care of the window resize
-           automatically  */
-       {
-               RDSize winSize;
-               RDRect bframe[2];
-               winSize.width = layoutFrame.size.width + margin * 2;
-               if (winSize.width < contentMinSize.width)
-                       winSize.width = contentMinSize.width;
-               winSize.height = layoutFrame.size.height + margin * 2;
-               for (i = 0; i < 2; i++) {
-                       /*  OK(0), Cancel(1) buttons  */
-                       RDItem *button = RubyDialogCallback_dialogItemAtIndex(dref, i);
-                       if (RubyDialogCallback_indexOfItem(dref, RubyDialogCallback_superview(button)) < 0 && !RubyDialogCallback_isItemHidden(button)) {
-                               bframe[i] = RubyDialogCallback_frameOfItem(button);
-                               if (layoutFrame.origin.y == margin) {
-                                       //  Move the layoutView up
-                                       layoutFrame.origin.y += margin + bframe[i].size.height;
-                               //      RubyDialogCallback_setFrameOfItem(layoutView, layoutFrame);
-                                       winSize.height += margin + bframe[i].size.height;
-                               }
-                               bframe[i].origin.y = winSize.height - margin - bframe[i].size.height;
-                               bframe[i].origin.x = (i == 0 ? winSize.width - bframe[i].size.width - margin : margin);
-                       } else {
-                               static RDRect zeroRect = {0, 0, 0, 0};
-                               bframe[i] = zeroRect;
-                       }
-               }
-               RubyDialogCallback_setWindowSize(dref, winSize);
-               for (i = 0; i < 2; i++) {
-                       if (bframe[i].size.width > 0) {
-                               RDItem *button = RubyDialogCallback_dialogItemAtIndex(dref, i);
-                               RubyDialogCallback_setFrameOfItem(button, bframe[i]);
-                       }
-               }
-       }
-#endif
-       
        /*  Create a layout view  */
        layoutView = RubyDialogCallback_createItem(dref, "view", "", layoutFrame);
 
@@ -769,26 +766,23 @@ s_RubyDialog_Layout(int argc, VALUE *argv, VALUE self)
 
        /*  Create a new hash for the layout view and push to _items */
        {
-               VALUE new_item = rb_class_new_instance(0, NULL, rb_cDialogItem);
+               new_item = rb_class_new_instance(0, NULL, rb_cDialogItem);
                rb_ivar_set(new_item, SYM2ID(sTypeSymbol), sViewSymbol);
                rb_ivar_set(new_item, SYM2ID(sDialogSymbol), self);
                rb_ivar_set(new_item, SYM2ID(sIndexSymbol), INT2NUM(itag));
                rb_ary_push(items, new_item);
        }
        
-       /*  Returns the integer tag  */
-       return INT2NUM(itag);
+       return new_item;
 }
 
 /*
  *  call-seq:
  *     item(type, hash) -> DialogItem
- *     item(index) -> DialogItem
  *
  *  Create a dialog item. Type is one of the following symbols; <tt>:text, :textfield, :radio,
  *  :checkbox, :popup</tt>. Hash is the attributes that can be set by set_attr.
  *  Returns an integer that represents the item. (0 and 1 are reserved for "OK" and "Cancel")
- *  The second form returns the index-th item.
  */
 static VALUE
 s_RubyDialog_Item(int argc, VALUE *argv, VALUE self)
@@ -804,7 +798,7 @@ s_RubyDialog_Item(int argc, VALUE *argv, VALUE self)
        RubyDialog *dref;
 
        if (argc == 1 && FIXNUM_P(argv[0])) {
-               return s_RubyDialog_ItemAtIndex(self, NUM2INT(argv[0]));
+               return s_RubyDialog_ItemAtIndex(self, argv[0]);
        }
 
        dref = s_RubyDialog_GetController(self);
@@ -942,7 +936,8 @@ s_RubyDialog_EachItem(VALUE self)
  *  call-seq:
  *     radio_group(Array)
  *
- *  Group radio buttons as a mutually exclusive group.
+ *  Group radio buttons as a mutually exclusive group. The array elements can be
+ *  DialogItems, Integers (item index) or other values (item tag).
  */
 static VALUE
 s_RubyDialog_RadioGroup(VALUE self, VALUE aval)
@@ -957,10 +952,16 @@ s_RubyDialog_RadioGroup(VALUE self, VALUE aval)
        /*  Build a new array with checked arguments  */
        gval = rb_ary_new2(n);
        for (i = 0; i < n; i++) {
-               j = s_RubyDialog_ItemIndexForTag(self, RARRAY_PTR(aval)[i]);
-               if (j < 0 || j >= nitems)
-                       break;
-               if (rb_ivar_get(RARRAY_PTR(items)[j], SYM2ID(sTypeSymbol)) != sRadioSymbol)
+               VALUE tval = RARRAY_PTR(aval)[i];
+               if (rb_obj_is_kind_of(tval, rb_cDialogItem)) {
+                       j = NUM2INT(s_RubyDialogItem_Attr(tval, sIndexSymbol));
+               } else {
+                       j = s_RubyDialog_ItemIndexForTag(self, tval);
+                       if (j < 0 || j >= nitems)
+                               break;
+                       tval = RARRAY_PTR(items)[j];
+               }
+               if (rb_ivar_get(tval, SYM2ID(sTypeSymbol)) != sRadioSymbol)
                        break;
                rb_ary_push(gval, INT2NUM(j));
        }
@@ -983,6 +984,58 @@ s_RubyDialog_RadioGroup(VALUE self, VALUE aval)
 
 /*
  *  call-seq:
+ *     end_modal(item = 0, retval = nil) -> nil
+ *
+ *  End the modal session. The argument item is either the DialogItem object or
+ *  the index (0 for OK, 1 for Cancel). If the second argument is given, it will
+ *  be the return value of Dialog#run. Otherwise, the return value will be a hash
+ *  including the key-value pairs for all "tagged" dialog items plus :status=>true
+ *  (if OK is pressed) or false (if Cancel is pressed).
+ *  This method itself returns nil.
+ */
+static VALUE
+s_RubyDialog_EndModal(int argc, VALUE *argv, VALUE self)
+{
+       int flag;
+       VALUE retval = Qundef;
+       if (argc == 0) {
+               flag = 0;
+       } else {
+               if (rb_obj_is_kind_of(argv[0], rb_cDialogItem)) {
+                       flag = NUM2INT(s_RubyDialogItem_Attr(argv[0], sIndexSymbol));
+               } else {
+                       flag = NUM2INT(rb_Integer(argv[0]));
+               }
+               if (argc > 1)
+                       retval = argv[1];
+       }
+       if (retval == Qundef) {
+               /*  The default return value  */
+               VALUE items = rb_iv_get(self, "_items");
+               int len = RARRAY_LEN(items);
+               VALUE *ptr = RARRAY_PTR(items);
+               int i;
+               retval = rb_hash_new();
+               /*  Get values for controls with defined tags  */
+               for (i = 2; i < len; i++) {
+                       /*  Items 0, 1 are OK/Cancel buttons  */
+                       /*      VALUE type = rb_hash_aref(ptr[i], sTypeSymbol); */
+                       VALUE tag = rb_ivar_get(ptr[i], SYM2ID(sTagSymbol));
+                       if (tag != Qnil) {
+                               VALUE val;
+                               val = s_RubyDialogItem_Attr(ptr[i], sValueSymbol);
+                               rb_hash_aset(retval, tag, val);
+                       }
+               }
+               rb_hash_aset(retval, ID2SYM(rb_intern("status")), INT2NUM(flag));
+       }
+       rb_iv_set(self, "_retval", retval);
+       RubyDialogCallback_endModal(s_RubyDialog_GetController(self), (flag ? 1 : 0));
+       return Qnil;
+}
+
+/*
+ *  call-seq:
  *     action(item)
  *
  *  Do the default action for the dialog item. The item is given as the argument.
@@ -1148,24 +1201,22 @@ RubyDialog_validateItemContent(RubyValue self, RDItem *ip, const char *s)
        return 1;
 }
 
-/*  Action for dialog items.
- Get the item number, and call "action" method of the RubyDialog object with
- the item number (integer) as the argument. The default "action" method is
- defined as s_RubyDialog_action.  */
-void
-RubyDialog_doItemAction(RubyValue self, RDItem *ip)
+static VALUE
+s_RubyDialog_doItemAction(VALUE val)
 {
-       int status;
-       VALUE ival, itval;
-       RubyDialog *dref = s_RubyDialog_GetController((VALUE)self);
-       VALUE items = rb_iv_get(((VALUE)self), "_items");
+       void **vp = (void **)val;
+       VALUE self = (VALUE)vp[0];
+       RDItem *ip = (RDItem *)vp[1];
+       VALUE ival, itval, actval;
+       RubyDialog *dref = s_RubyDialog_GetController(self);
+       VALUE items = rb_iv_get(self, "_items");
        int nitems = RARRAY_LEN(items);
        int idx = RubyDialogCallback_indexOfItem(dref, ip);
        if (idx < 0)
-               return;
+               return Qnil;
        ival = INT2NUM(idx);
-       itval = s_RubyDialog_ItemAtIndex((VALUE)self, idx);
-
+       itval = s_RubyDialog_ItemAtIndex(self, ival);
+       
        /*  Handle radio group  */
        {
                VALUE gval = s_RubyDialogItem_Attr(itval, sRadioGroupSymbol);
@@ -1182,7 +1233,36 @@ RubyDialog_doItemAction(RubyValue self, RDItem *ip)
                }
        }
        
-       Ruby_funcall2_protect((VALUE)self, rb_intern("action"), 1, &itval, &status);
+       /*  If the item has the "action" attribute, call it  */
+       actval = s_RubyDialogItem_Attr(itval, sActionSymbol);
+       if (actval != Qnil) {
+               if (TYPE(actval) == T_SYMBOL)
+                       rb_funcall(self, SYM2ID(actval), 1, itval);
+               else
+                       rb_funcall(actval, rb_intern("call"), 1, itval);
+       } else if (rb_respond_to(itval, SYM2ID(sActionSymbol))) {
+               /*  If "action" method is defined, then call it without arguments  */
+               rb_funcall(itval, SYM2ID(sActionSymbol), 0);
+       } else {
+               /*  Default action (only for default buttons)  */
+               if (idx == 0 || idx == 1)
+                       s_RubyDialog_EndModal(1, &itval, self);
+       }
+       return Qnil;
+}
+
+/*  Action for dialog items.
+ Get the item number, and call "action" method of the RubyDialog object with
+ the item number (integer) as the argument. The default "action" method is
+ defined as s_RubyDialog_action.  */
+void
+RubyDialog_doItemAction(RubyValue self, RDItem *ip)
+{
+       int status;
+       void *vp[2];
+       vp[0] = (void *)self;
+       vp[1] = ip;
+       rb_protect(s_RubyDialog_doItemAction, (VALUE)vp, &status);
        if (status != 0)
                Molby_showError(status);
 }
@@ -1200,14 +1280,17 @@ RubyDialogInitClass(void)
        rb_define_private_method(rb_cDialog, "initialize", s_RubyDialog_Initialize, -1);
        rb_define_method(rb_cDialog, "run", s_RubyDialog_Run, 0);
        rb_define_method(rb_cDialog, "item", s_RubyDialog_Item, -1);
+       rb_define_method(rb_cDialog, "item_at_index", s_RubyDialog_ItemAtIndex, 1);
+       rb_define_method(rb_cDialog, "item_with_tag", s_RubyDialog_ItemWithTag, 1);
        rb_define_method(rb_cDialog, "layout", s_RubyDialog_Layout, -1);
        rb_define_method(rb_cDialog, "_items", s_RubyDialog_Items, 0);
        rb_define_method(rb_cDialog, "nitems", s_RubyDialog_Nitems, 0);
        rb_define_method(rb_cDialog, "each_item", s_RubyDialog_EachItem, 0);
        rb_define_method(rb_cDialog, "set_attr", s_RubyDialog_SetAttr, 2);
        rb_define_method(rb_cDialog, "attr", s_RubyDialog_Attr, 2);
-       rb_define_method(rb_cDialog, "radio_group", s_RubyDialog_RadioGroup, 1);
+       rb_define_method(rb_cDialog, "radio_group", s_RubyDialog_RadioGroup, -2);
        rb_define_method(rb_cDialog, "action", s_RubyDialog_Action, 1);
+       rb_define_method(rb_cDialog, "end_modal", s_RubyDialog_EndModal, -1);
        rb_define_singleton_method(rb_cDialog, "save_panel", s_RubyDialog_SavePanel, -1);
        rb_define_singleton_method(rb_cDialog, "open_panel", s_RubyDialog_OpenPanel, -1);
 
index fadc57e..b71b38e 100755 (executable)
@@ -26,7 +26,7 @@ class Molecule
                item(:text, :title=>"New residue name/number\n(like \"RES.1\")\nfor atoms #{atoms}"),
            item(:textfield, :width=>120, :tag=>"residue"))
     }
-    if hash
+    if hash[:status] == 0
          residue = hash["residue"]
          assign_residue(sel, residue)
        end
@@ -43,7 +43,7 @@ class Molecule
                item(:text, :title=>"Offset residue number:\nfor atoms #{atoms}"),
            item(:textfield, :width=>120, :tag=>"offset"))
     }
-       if hash
+       if hash[:status] == 0
          offset = hash["offset"].to_i
          offset_residue(sel, offset)
        end
@@ -147,7 +147,7 @@ end_of_header
          layout(1,
            item(:text, :title=>"Create GAMESS input with LANL2DZ effective core potentials"))
     }
-    if hash
+    if hash[:status] == 0
       fname = File.basename(self.path, ".*") + ".inp"
          fname = Dialog.save_panel(nil, self.dir, fname)
          if fname
@@ -190,7 +190,7 @@ end_of_header
                  item(:textfield, :width=>100, :height=>20, :tag=>"stepy", :value=>grid[5].to_s),
                  item(:textfield, :width=>100, :height=>20, :tag=>"stepz", :value=>grid[6].to_s)))
        }
-       if hash
+       if hash[:status] == 0
          origin = Vector3D[hash["originx"], hash["originy"], hash["originz"]]
          dx = hash["deltax"]
          dy = hash["deltay"]
@@ -224,18 +224,8 @@ end_of_header
   
   def Molecule.cmd_load_remote(mol)  #  mol is not used
     hash = Dialog.run {
-         def action(item1)
-           n = item1[:index]
-           if n == 0 || n == 1
-                 self.each_item { |item2|
-                   if (type = item2[:type]) == :textfield || type == :textview
-                     val = item2[:value]
-                     tag = item2[:tag]
-                     set_global_settings("load_remote.#{tag}", val)
-                       end
-                 }
-               end
-               if n == 0  #  OK
+         def button_action(it)   #  Action for OK and Cancel buttons
+               if it[:index] == 0  #  OK
                  local = File.expand_path(value("local"))
                  if value("local") != ""
                    #  Check whether the local file already exists
@@ -258,32 +248,33 @@ end_of_header
                          hash = Dialog.run {
                            layout(1, item(:text, :title=>msg, :width=>240, :height=>60))
                          }
-                         return if !hash  #  No call of super -> dialog is not dismissed
+                         return if hash[:status] != 0  #  No call of super -> dialog is not dismissed
                        end
                  end
-               elsif n != 0 && n != 1
-             if value("host") != "" && value("directory") != "" && value("sfile") != ""
-                   set_attr(0, :enabled=>true)
-                 else
-                   set_attr(0, :enabled=>false)
-                 end
            end
-           super
+               end_modal(it)
+         end
+         def text_action(it)
+               if value("host") != "" && value("directory") != "" && value("sfile") != ""
+                 set_attr(0, :enabled=>true)
+               else
+                 set_attr(0, :enabled=>false)
+               end       
          end
          layout(2,
                item(:text, :title=>"Remote host"),
-               item(:textfield, :width=>280, :height=>20, :tag=>"host"),
+               item(:textfield, :width=>280, :height=>20, :tag=>"host", :action=>:text_action),
                item(:text, :title=>"Directory"),
-               item(:textfield, :width=>280, :height=>20, :tag=>"directory"),
+               item(:textfield, :width=>280, :height=>20, :tag=>"directory", :action=>:text_action),
                item(:text, :title=>"Structure File"),
-               item(:textfield, :width=>280, :height=>20, :tag=>"sfile"),
+               item(:textfield, :width=>280, :height=>20, :tag=>"sfile", :action=>:text_action),
                item(:text, :title=>"Coordinate File"),
                item(:textfield, :width=>280, :height=>20, :tag=>"cfile"),
                item(:text, :title=>"File List"),
                item(:textview, :width=>280, :height=>80, :tag=>"list", :editable=>false),
                nil,
                [ item(:button, :title=>"Update",
-                       :action=>proc { |item1
+                       :action=>proc { |it| 
                          list = Dialog.list_remote_files(value("host"), value("directory"))
                          set_value("list", list)
                        }
@@ -294,7 +285,7 @@ end_of_header
                item(:textfield, :width=>280, :height=>20, :tag=>"local"),
                nil,
                [ item(:button, :title=>"Choose...",
-                   :action=>proc { |item1|
+                   :action=>proc { |it|
                          dir = Dialog.open_panel(nil, nil, nil, true)
                          if dir
                            set_value("local", dir)
@@ -303,6 +294,8 @@ end_of_header
                  ), {:align=>:right} ]
        #       layout(1, 2, 1, 0)
          )
+         set_attr(0, :action=>:button_action)
+         set_attr(1, :action=>:button_action)
          set_attr(0, :enabled=>false)
          self.each_item { |it|
                tag = it[:tag]
@@ -314,7 +307,11 @@ end_of_header
                end
          }
     }
-       if hash
+       hash.each_pair { |key, value|
+         next if key == :status
+         set_global_settings("load_remote.#{key}", value)
+       }
+       if hash[:status] == 0
          sfile = hash["sfile"]
          cfile = hash["cfile"]
          host = hash["host"]
@@ -363,7 +360,7 @@ end_of_header
                item(:text, :title=>"Step"),
                item(:textfield, :width=>120, :tag=>"step", :value=>"0"))
        }
-       if hash
+       if hash[:status] == 0
          sframe = Integer(hash["start"])
          eframe = Integer(hash["end"])
          step = Integer(hash["step"])
@@ -412,7 +409,7 @@ end_of_header
                item(:text, :title=>"Exclusion limit distance:"),
                item(:textfield, :width=>"120", :tag=>"limit", :value=>"3.0"))
        }
-       if hash
+       if hash[:status] == 0
          solvate(solv[hash["solvent"]], [hash["x"], hash["y"], hash["z"]], hash["limit"])
        end
   end
@@ -424,7 +421,7 @@ end_of_header
            item(:text, :title=>"Number of graphite rings for each direction:\n(0 to suppress display)"),
            item(:textfield, :width=>120, :tag=>"graphite", :value=>n.to_s))
        }
-       if hash
+       if hash[:status] == 0
          self.show_graphite(hash["graphite"])
        end
   end
index e682c19..7d1e53a 100755 (executable)
@@ -34,6 +34,7 @@ class Dialog
   
   def set_value(tag, value)
     set_attr(tag, :value=>value)
+       value
   end
   
 end
index de2be94..e1c41bc 100755 (executable)
@@ -215,18 +215,6 @@ class Molecule
          "secondary_basis"=>8, "esp"=>0}
 
     hash = Dialog.run("GAMESS Export") {
-         def action(item1)
-           n = item1[:index]
-           if n == 0 || n == 1
-             each_item { |item2|
-               tag = item2[:tag]
-                       if tag
-                         set_global_settings("gamess.#{tag}", item2[:value])
-                       end
-                 }
-               end
-               super
-         end
          layout(4,
                item(:text, :title=>"SCF type"),
                item(:popup, :subitems=>["RHF", "ROHF", "UHF"], :tag=>"scftype"),
@@ -239,7 +227,7 @@ class Molecule
                item(:textfield, :width=>80, :tag=>"mult"),
 
                item(:checkbox, :title=>"Use DFT", :tag=>"dft",
-                 :action=>proc { |item1| set_attr("dfttype", :enabled=>(item1[:value] != 0)) } ),
+                 :action=>proc { |it| set_attr("dfttype", :enabled=>(it[:value] != 0)) } ),
                -1,
                item(:text, :title=>"DFT type"),
                item(:popup, :subitems=>dft_desc, :tag=>"dfttype"),
@@ -252,8 +240,8 @@ class Molecule
                -1, -1,
 
                item(:checkbox, :title=>"Use secondary basis set", :tag=>"use_secondary_basis",
-                 :action=>proc { |item1|
-                   flag = (item1[:value] != 0)
+                 :action=>proc { |it|
+                   flag = (it[:value] != 0)
                        set_attr("secondary_elements", :enabled=>flag)
                        set_attr("secondary_basis", :enabled=>flag)
                  }),
@@ -285,7 +273,11 @@ class Molecule
          set_attr("secondary_basis", :enabled=>(values["use_secondary_basis"] == 1))
          set_attr("dfttype", :enabled=>(values["dft"] == 1))
        }
-       if hash
+       hash.each_pair { |key, value|
+         next if key == :status
+         set_global_settings("gamess.#{key}", value)
+       }
+       if hash[:status] == 0
          #  Specify basis by internal keys
          hash["basis"] = bset_internal[hash["basis"]]
          hash["secondary_basis"] = bset_internal[hash["secondary_basis"]]
index 223e2da..ba95841 100755 (executable)
@@ -44,17 +44,17 @@ class Molecule
            items.push(item(:text, :title=>k.to_s))
                it = item(:textfield, :width=>120, :value=>arena[k].to_s)
                if enabled
-                 set_attr(it, :tag=>k)
+                 it[:tag] = k
                else
-                 set_attr(it, :enabled=>false)
+                 it[:enabled] = false
                end
                items.push(it)
          }
          layout(4, *items)
        }
-       if hash
+       if hash[:status] == 0
          hash.keys.each { |k|
-           next if read_only.include?(k)
+           next if k == :status || read_only.include?(k)
            arena[k] = hash[k]
          }
 #        arena.prepare
@@ -127,7 +127,7 @@ class Molecule
          items.push item(:text, :title=>"Log file")
          items.push item(:textfield, :width=>120, :value=>(arena.log_file || ""), :tag=>"log_file")
          items.push item(:button, :title=>"Advanced...",
-                    :action=>proc { |item1|
+                    :action=>proc { |it|
                           if mol.cmd_md_sub
                             if !minimize
                               set_value("timestep", arena[:timestep].to_s)
@@ -140,11 +140,12 @@ class Molecule
          layout(2, *items)
        }
        dirstr = (self.dir || document_home)
-       if hash
+       status = hash[:status]
+       if status == 0
          arena[:log_file] = hash[:log_file]
        end
        [:log_file, :coord_file, :vel_file, :force_file, :debug_file].each { |k|
-         if hash
+         if status == 0
            s = arena[k]
            if s != nil && s != ""
              arena[k] = dirstr + "/" + s
@@ -155,7 +156,7 @@ class Molecule
            arena[k] = files_save[k]
       end
        }
-       if !hash
+       if status != 0
          return -1
        end
        if !minimize
@@ -198,14 +199,14 @@ class Molecule
                @mol.set_box(ax, bx, cx, ox)
                return @mol
          end
-         def action(item1)
-           if item1[:index] == 0
-                 if !set_box_value(item1)
-                   return  #  Cannot set box: dialog is not dismissed
-                 end
-               end
-               super
-         end
+#        def action(item1)
+#          if item1[:index] == 0
+#                if !set_box_value(item1)
+#                  return  #  Cannot set box: dialog is not dismissed
+#                end
+#              end
+#              super
+#        end
          box = @mol.box
          layout(4,
            item(:text, :title=>"Unit cell:"),
@@ -229,6 +230,7 @@ class Molecule
                item(:button, :title=>"Set", :action=>:set_box_value),
                item(:text, :title=>"(Ruby expressions are allowed as the values)"),
                -1, -1)
+         set_attr(0, :action=>proc { |it| set_box_value(it) && end_modal(it) })
        }
   end
 
@@ -236,7 +238,7 @@ class Molecule
     mol = self
     hash = Dialog.run("Show Periodic Image") {
          @mol = mol
-         def set_periodic_image(n)
+         def set_periodic_image(it)
            a = []
            ["amin", "amax", "bmin", "bmax", "cmin", "cmax"].each_with_index { |k, i|
                  s = value(k)
@@ -248,12 +250,6 @@ class Molecule
                }
            @mol.show_periodic_image(a)
          end
-         def action(item1)
-           if item1[:index] == 0
-                 set_periodic_image(item1)
-               end
-               super
-         end
          pimage = @mol.show_periodic_image
          layout(4,
            item(:text, :title=>"Show Periodic Image:"),
@@ -271,6 +267,7 @@ class Molecule
                item(:text, :title=>"to"),
                item(:textfield, :width=>80, :tag=>"cmax", :value=>pimage[5].to_s),
                item(:button, :title=>"Set", :action=>:set_periodic_image))
+         set_attr(0, :action=>proc { |it| set_periodic_image(it); end_modal(it) } )
        }
   end
 
@@ -331,7 +328,7 @@ class Molecule
            item(:text, :title=>"Fluctuate cell orientation"),
                item(:textfield, :width=>100, :tag=>"orient", :value=>arena.pressure_fluctuate_cell_orientation.to_s))
     }
-       if hash
+       if hash[:status] == 0
          arena.pressure_freq = hash["freq"]
          arena.pressure_coupling = hash["coupling"]
          arena.pressure = [hash["pa"], hash["pb"], hash["pc"]]
@@ -361,65 +358,22 @@ class Molecule
       def valid_antechamber_dir(s)
            FileTest.exist?(s + "/" + @toolname)
          end
-         def action(item1)
-           n = item1[:index]
-           if n == 4
-                 if valid_antechamber_dir(item1[:value])
-                   set_attr(0, :enabled=>true)
-                 end
-               elsif n == 0 || n == 1
-                 #  Settings are stored in the global settings (even if Cancel is pressed)
-                 self.each_item { |item2|
-                   if (tag = item2[:tag])
-                         value = item2[:value]
-                 v = [["log_none", "none"], ["log_error_only", "error_only"], ["log_keep_latest", "latest"], ["log_all", "all"]].assoc(tag)
-                         if v 
-                           next if value != 1
-                           tag = "log_level"
-                               value = v[1]
-                         end
-                         set_global_settings("antechamber.#{tag}", value)
-                       end
-                 }
-               end
-               super
-         end
          layout(2,
-=begin
-               item(:text, :title=>"Ambertools directory:"),
-               [ item(:button, :title=>"Choose...",
-                       :action=>proc { |n|
-                       #  print "action method called\n"
-                         dir = Dialog.open_panel(nil, nil, nil, true)
-                         if dir
-                               if valid_antechamber_dir(dir)
-                                 set_value("ante_dir", dir)
-                         set_attr(0, :enabled=>true)
-                               else
-                                 Dialog.run {
-                                   layout(1,
-                                         item(:text, :title=>"Cannot find #{tool} in #{dir}."))
-                                 }
-                                 set_attr(0, :enabled=>false)
-                               end
-                         end
-                       }
-                 ), {:align=>:right} ],
-               item(:textfield, :width=>360, :height=>40, :tag=>"ante_dir", :value=>ante_dir),
-               -1,
-=end
-               item(:checkbox, :title=>"Optimize structure and calculate charges (may be slow)", :tag=>"calc_charge", :value=>(get_global_settings("antechamber.calc_charge") || 0),
-                 :action=>proc { |item1| set_attr("nc", :enabled=>(item1[:value] != 0)) } ),
+               item(:checkbox, :title=>"Optimize structure and calculate charges (may be slow)", :tag=>"calc_charge",
+                 :value=>(get_global_settings("antechamber.calc_charge") || 0),
+                 :action=>proc { |it| set_attr("nc", :enabled=>(it[:value] != 0)) } ),
                -1,
                item(:text, :title=>"      Net Molecular Charge:"),
                item(:textfield, :width=>"80", :tag=>"nc", :value=>(get_global_settings("antechamber.nc") || "0")),
-               item(:checkbox, :title=>"Use the residue information", :tag=>"use_residue", :value=>(get_global_settings("antechamber.use_residue") || 0)),
+               item(:checkbox, :title=>"Use the residue information", :tag=>"use_residue",
+                 :value=>(get_global_settings("antechamber.use_residue") || 0),
+                 :action=>proc { |it| valid_antechamber_dir(it[:value]) && set_attr(0, :enabled=>true) } ),
                -1,
                item(:line),
                -1,
                item(:text, :title=>"Log directory:"),
                [ item(:button, :title=>"Choose...",
-                       :action=>proc { |item1|
+                       :action=>proc { |it|
                          dir = Dialog.open_panel(nil, nil, nil, true)
                          if dir
                                set_value("log_dir", dir)
@@ -445,9 +399,20 @@ class Molecule
          )
          set_attr("nc", :enabled=>(attr("calc_charge", :value) == "1"))
     }
+       hash.each_pair { |key, value|
+         next if key == :status
+         v = [["log_none", "none"], ["log_error_only", "error_only"], ["log_keep_latest", "latest"], ["log_all", "all"]].assoc(key)
+      if v 
+           next if value != 1
+           tag = "log_level"
+           value = v[1]
+      end
+      set_global_settings("antechamber.#{tag}", value)
+    }
+
        #  The hash values are set in the action() method
 #      print "retval = #{hash ? 1 : 0}\n"
-       return (hash ? 1 : 0)
+       return (hash[:status] == 0 ? 1 : 0)
   end
   
   def create_ante_log_dir(name, key)
@@ -777,7 +742,7 @@ class Molecule
          layout(1,
            item(:text, :title=>"Step 1:\nCreate GAMESS input for ESP calculation"),
                [item(:button, :title=>"Create GAMESS Input...",
-                 :action=>proc { |item1|
+                 :action=>proc { |it|
                    esp_save = get_global_settings("gamess.esp")
                        set_global_settings("gamess.esp", 1)
                        mol.cmd_create_gamess_input
@@ -787,7 +752,7 @@ class Molecule
                item(:line),
                item(:text, :title=>"Step 2:\nImport GAMESS .dat file"),
                [item(:button, :title=>"Import GAMESS dat...",
-                 :action=>proc { |item1|
+                 :action=>proc { |it|
                    fname = Dialog.open_panel("Select GAMESS .dat file", nil, "*.dat")
                        if fname
                          errmsg = nil
@@ -808,17 +773,17 @@ class Molecule
                item(:line),
                item(:text, :title=>"Step 3:\nRun RESP for charge fitting"),
                [item(:button, :title=>"Run RESP...", :tag=>"resp",
-                 :action=>proc { |item1|
+                 :action=>proc { |it|
                    if mol.cmd_run_resp
                          if get_global_settings("antechamber.log_level") == "latest"
                            mol.clean_ante_log_dir(Integer(get_global_settings("antechamber.log_keep_number")))
                          end
-                         action(item(0))
+                         end_modal(0)
                        end
                  }),
                  {:align=>:right}],
                item(:line),
-               [item(:button, :title=>"Close", :action=>proc { |item1| action(item(1)) }),
+               [item(:button, :title=>"Close", :action=>proc { |it| end_modal(1) }),
                  {:align=>:center}]
          )
          if mol.nelpots == 0
@@ -864,7 +829,7 @@ class Molecule
                )
          )
        }
-       if hash
+       if hash[:status] == 0
          pref = pen.lookup(hash["types"], :create, :local, :missing, :nobasetype, :nowildcard)
          raise "Cannot create new #{ptype} parameter" if !pref
          k.each { |key| pref.set_attr(key, hash[key]) }