Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force F# interactive to reference Gtk# by default?

I am mostly playing with F# on Linux and would like to get all the necessary GUI libraries (Gtk, Gdk, Atk, Glib, Pango, Cairo) to be referenced by default so that I can simply use: open Gtk;; without any additional typing. My best guess would modifying the fsi launching script, which at the moment looks like that:


    #!/bin/sh  
    exec /usr/bin/mono /usr/local/src/fsharp/bin/fsi.exe $@

Update: working version of the script as in Stephen's suggestion:


    #!/bin/sh
    exec /usr/bin/mono /usr/local/src/fsharp/bin/fsi.exe -r "/usr/lib/cli/atk-sharp-2.0/atk-sharp.dll" -r "/usr/lib/cli/glib-sharp-2.0/glib-sharp.dll" -r "/usr/lib/cli/gdk-sharp-2.0/gdk-sharp.dll" -r "/usr/lib/cli/gtk-sharp-2.0/gtk-sharp.dll" -r "/usr/lib/cli/pango-sharp-2.0/pango-sharp.dll" -r "/usr/lib/mono/2.0/Mono.Cairo.dll" $@
like image 695
Piotr Zurek Avatar asked Nov 13 '10 21:11

Piotr Zurek


People also ask

How do you find force F?

The force (F) required to move an object of mass (m) with an acceleration (a) is given by the formula F = m x a. So, force = mass multiplied by acceleration.

What is the general simple equation for force F?

The standard force formula states that the net external force on an object is equal to the product of its mass and acceleration. The force equation is F=ma F = m a where F is the force, m is mass and a is acceleration. In other words, force equals mass times acceleration.

Is it F Kx or F =- KX?

Hooke's law is formulated as either F = -kx with a minus symbol or as F = kx without the minus symbol. If the minus is present, F indicates a restoring force, that is, the force that allows the object to return to its original shape and position.


2 Answers

I wrote a little script that allows you to use Gtk# from F# Interactive (see below). It references the necessary Gtk# assemblies (you may need to modify the paths) and it also configures F# Interactive event loop, so that you can create and display widgets (such as Window) interactively.

If you want to get the support automatically, you'll need to run fsi.exe with a parameter to load the script on start mono /.../fsi.exe --load:load-gtk.fsx (assuming that you save the script as load-gtk.fsx)

[<AutoOpen>]
module GtkSharp

// Load some common Gtk# assemblies (from /usr/lib/mono/2.0/../gtk-sharp-2.0)
#r "../gtk-sharp-2.0/gtk-sharp.dll"
#r "../gtk-sharp-2.0/gdk-sharp.dll"
#r "../gtk-sharp-2.0/glib-sharp.dll"
#r "../gtk-sharp-2.0/atk-sharp.dll"

open Gtk

Application.Init()
fsi.EventLoop <- 
 { new Microsoft.FSharp.Compiler.Interactive.IEventLoop with
   member x.Run() = Application.Run() |> ignore; false
   member x.Invoke f = 
     let res = ref None
     let evt = new System.Threading.AutoResetEvent(false)
     Application.Invoke(new System.EventHandler(fun _ _ ->
       res := Some(f())
       evt.Set() |> ignore ))
     evt.WaitOne() |> ignore
     res.Value.Value 
   member x.ScheduleRestart() = () }
like image 166
Tomas Petricek Avatar answered Nov 02 '22 19:11

Tomas Petricek


It may be a little different in Linux, but in Windows you can reference assemblies on fsi startup by using -r. e.g.

#!/bin/sh
exec /usr/bin/mono /usr/local/src/fsharp/bin/fsi.exe -r /usr/somedll.dll $@ 
like image 24
Stephen Swensen Avatar answered Nov 02 '22 19:11

Stephen Swensen