proc codeContractViolation() {
    execerror("\n\n    Bug in the program: Code contract violation", "\n    Please report this problem to the developer along with the call stack shown below\n")
}

proc sourcePythonCode() { local status
    strdef relDirPath, importPyCommands, pyCommand
    
    relDirPath = $s1
    importPyCommands = $s2
    
    sprint(pyCommand, "import sys\nimport os\nsys.path.append(os.getcwd() + '%s')\n%s", relDirPath, importPyCommands)
    
    status = nrnpython(pyCommand)
    if (!status) {
        codeContractViolation()
    }
}

// Find and remove the item from the List; raise an error if it's not there
// $o1 - The List to remove from
// $o2 - The item to remove
proc removeItemFromList() { local idxOrMinus1, isFound localobj list, item
    
    list = $o1
    item = $o2
    
    idxOrMinus1 = list.index(item)
    isFound = (idxOrMinus1 != -1)
    if (isFound) {
        list.remove(idxOrMinus1)
    } else {
        codeContractViolation()
    }
}

// An iterator to simplify looping over all elements of the List a bit.
// Basically, we replace this:
//     local idx localobj item
//     for idx = 0, list.count() - 1 {
//         item = list.o(idx)
//         do_smth(item)
//     }
// with this:
//     localobj item
//     for eachItemInList(item, list) {
//         do_smth(item)
//     }
// There are some limitations:
//  1. iterator_statement cannot contain "return" (otherwise we'll catch Segmentation violation)
//  2. We cannot create another iterator which calls eachItemInList inside (otherwise the outer iterator "forgets" its input arguments on 2nd iteration)
// Also, calling "execerror" inside the iterator crashes NEURON
// $o1 (output) - An element of the List
// $o2 - The List
// $3 (optional) - The first index (0 by default)
iterator eachItemInList() { local numArg, firstIdx, idx localobj list
    list = $o2
    numArg = numarg()
    if (numArg == 2) {
        firstIdx = 0
    } else if (numArg == 3) {
        firstIdx = $3
    } else {
        codeContractViolation()
    }
    for idx = firstIdx, list.count() - 1 {
        $o1 = list.o(idx)
        iterator_statement
    }
}

objref runControlPanel

proc showRunControlPanel() {
    runControlPanel = new Deck()
    runControlPanel.intercept(1)
    {
        nrncontrolmenu()
    }
    runControlPanel.intercept(0)
    runControlPanel.flip_to(0)
    runControlPanel.map("RunControl", 0, 125, -1, -1)
}