Thanks for the sample application, I have done something similar using WPF.

#light

open Microsoft.Win32
open System
open System.Diagnostics
open System.IO
open System.Windows
open System.Windows.Controls
open System.Windows.Data
open System.Windows.Input
open System.Windows.Media
open System.Windows.Threading

let inline add_list (collection: ^c) (items: ^i list) =
List.iter (fun i -> (^c : (member Add: ^i -> 'a) (collection, i)) |> ignore) items

type FSGridLength =
| Auto
| Pixel of float
| Star of float
with
static member CreateRowDefinition l =
RowDefinition (
Height =
match l with
| Auto -> GridLength.Auto
| Pixel v -> GridLength (v)
| Star v -> GridLength (v, GridUnitType.Star)
)

let add_row_definitions (grid: Grid) heights =
add_list (grid.RowDefinitions) (List.map (FSGridLength.CreateRowDefinition) heights)

[<STAThread; EntryPointAttribute>]
let main args =
let app = Application ()

use fsi_proc =
new Process (
StartInfo = ProcessStartInfo (
FileName = @"C:\Program Files\FSharp-1.9.6.2\bin\fsi.exe",
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
ErrorDialog = false
),
EnableRaisingEvents = true
)

let inputTextBox =
TextBox (
AcceptsReturn = true,
FontFamily = FontFamily("Courier New")
)
ScrollViewer.SetHorizontalScrollBarVisibility (inputTextBox, ScrollBarVisibility.Auto)
ScrollViewer.SetVerticalScrollBarVisibility (inputTextBox, ScrollBarVisibility.Auto)

let outputTextBox =
TextBox (
AcceptsReturn = true,
IsReadOnly = true,
FontFamily = FontFamily("Courier New")
)
ScrollViewer.SetHorizontalScrollBarVisibility (outputTextBox, ScrollBarVisibility.Auto)
ScrollViewer.SetVerticalScrollBarVisibility (outputTextBox, ScrollBarVisibility.Auto)

fsi_proc.Start () |> ignore

let newl = Environment.NewLine

fsi_proc.OutputDataReceived.AddHandler (fun s e ->
app.Dispatcher.Invoke(DispatcherPriority.Input, Action (fun () ->
outputTextBox.AppendText (e.Data + newl))
) |> ignore
)
fsi_proc.BeginOutputReadLine ()

fsi_proc.ErrorDataReceived.AddHandler (fun s e ->
app.Dispatcher.Invoke(DispatcherPriority.Input, Action (fun () ->
outputTextBox.AppendText (e.Data + newl))
) |> ignore
)
fsi_proc.BeginErrorReadLine ()

let runInteractiveCommand = RoutedUICommand ()

let win =
Window (
Content =
let grid = Grid () in
add_row_definitions grid [Auto; Star 1.0; Pixel 5.0; Star 1.0];
add_list (grid.Children) [
(let menu = Menu () in
add_list (menu.Items) [
MenuItem (Header = "Open", Command = ApplicationCommands.Open);
MenuItem (Header = "Save", Command = ApplicationCommands.Save);
MenuItem (Header = "Execute", Command = runInteractiveCommand);
MenuItem (Header = "Exit", Command = ApplicationCommands.Close)
];
menu :> UIElement);
(Grid.SetRow (inputTextBox, 1);
inputTextBox :> UIElement);
(let gridSplitter =
GridSplitter (
HorizontalAlignment = HorizontalAlignment.Stretch,
Background = Brushes.LightGray
) in
Grid.SetRow (gridSplitter, 2);
gridSplitter :> UIElement)
(Grid.SetRow (outputTextBox, 3);
outputTextBox :> UIElement)
];
grid
)

let openFileDialog = OpenFileDialog (Filter = "F# script files (*.fsx)|*.fsx|All Files (*.*)|*.*")
openFileDialog.FileOk.AddHandler (fun s e ->
use sr = new StreamReader (openFileDialog.FileName)
sr.ReadToEnd () |> inputTextBox.AppendText
sr.Close ()
);

let saveFileDialog = SaveFileDialog (Filter = "F# script files (*.fsx)|*.fsx|All Files (*.*)|*.*")
saveFileDialog.FileOk.AddHandler (fun e s ->
use sw = new StreamWriter (saveFileDialog.FileName)
sw.Write (inputTextBox.Text)
sw.Close ()
);

add_list (win.InputBindings) [
KeyBinding(ApplicationCommands.Open, Key.S, ModifierKeys.Control)
KeyBinding(ApplicationCommands.Save, Key.O, ModifierKeys.Control)
KeyBinding(ApplicationCommands.Close, Key.X, ModifierKeys.Control)
KeyBinding(runInteractiveCommand, Key.Enter, ModifierKeys.Alt)
]

add_list (win.CommandBindings) [
CommandBinding(ApplicationCommands.Open, fun s e -> openFileDialog.ShowDialog() |> ignore)
CommandBinding(ApplicationCommands.Save, fun s e -> saveFileDialog.ShowDialog() |> ignore)
CommandBinding(ApplicationCommands.Close, fun s e -> win.Close())
CommandBinding(runInteractiveCommand, fun s e ->
inputTextBox.SelectedText + ";;" + newl |> fsi_proc.StandardInput.Write
)
]

app.Run win

By on 12/22/2008 9:31 AM ()

Thanks to you! In fact it is for some time that I have not look into F# and I do not know WPF well enough to work with fluently. It is good to see more F# code on .NET!

By on 1/29/2009 10:27 AM ()

Just for the record this implementation is much better to study. It has two implementations in F# and C# using WPF one can compare those languages.

By on 12/14/2010 2:43 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper