Monday, November 28, 2011

PowerShell and GTK-Server

Another little five-finger exercise, porting the example VBScript stdin driver to PowerShell. The only annoying thing is that the Start-Process cmdlet doesn't give you direct access to the streams, so we have to drop into .net to fire up the GTK server process.

<# 
.SYNOPSIS 
    demo to use the GTK-server using STDIN. 
    
.DESCRIPTION 
    This script is based on the VBScript example by
    Peter van Eerten published at http://www.gtk-server.org/demo-stdin.vbs.txt
        
.NOTES 
    File Name  : Try-GTKServer.ps1 
    Requires   : PowerShell Version 1.0

.PARAMETER Help

Show this help text.
#> 
param ( 
    [switch] $Help)

if ($help)
{
    Get-Help $MyInvocation.MyCommand.Definition
    return
}

# hard-coded rather than passed in as a parameter
# I'm feeling lazy tonight...
$gtkpath = "path\to\gtk-server.exe"

## As we want to get at the process stdin, stdout we can't use the Start-Process cmdlet
## $gtkserver = Start-Process -FilePath $gtkpath -ArgumentList "-stdin" -PassThru
## which only allows file I/O
$gtkinfo = new-object System.Diagnostics.ProcessStartInfo @($gtkpath, "-stdin")
$gtkinfo.UseShellExecute = $false
$gtkinfo.RedirectStandardInput = $true
$gtkinfo.RedirectStandardOutput = $true
$gtkinfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden

$gtkserver = [System.Diagnostics.Process]::start($gtkinfo)

Function Invoke-GTK 
{
Param([string] $command)
$gtkserver.StandardInput.WriteLine($command)
$line = $gtkserver.StandardOutput.ReadLine()
if ($line -cne "ok") { $line }
}


#Define GUI
Invoke-GTK("gtk_init NULL NULL")
$win = Invoke-GTK("gtk_window_new 0")
Invoke-GTK("gtk_window_set_title $win `"PowerShell Script demo program using STDIN`"")
Invoke-GTK("gtk_widget_set_usize $win 450 400")
$table = Invoke-GTK("gtk_table_new 50 50 1")
Invoke-GTK("gtk_container_add $win $table" )
$button = Invoke-GTK("gtk_button_new_with_label Exit")
Invoke-GTK("gtk_table_attach_defaults $table $button 41 49 45 49")
$entry = Invoke-GTK("gtk_entry_new")
Invoke-GTK("gtk_table_attach_defaults $table $entry 1 40 45 49")
$text = Invoke-GTK("gtk_text_new NULL NULL")
Invoke-GTK("gtk_table_attach_defaults $table $text 1 49 8 44")

$radio1 = Invoke-GTK("gtk_radio_button_new_with_label_from_widget NULL Yes")
Invoke-GTK("gtk_table_attach_defaults $table $radio1 1 10 1 4")
$radio2 = Invoke-GTK("gtk_radio_button_new_with_label_from_widget $radio1 No")
Invoke-GTK("gtk_table_attach_defaults $table $radio2 1 10 4 7")
Invoke-GTK("gtk_widget_show_all $win" )
Invoke-GTK("gtk_widget_grab_focus $entry" )

# message/event loop
do {
    $event = Invoke-GTK("gtk_server_callback wait")
    if ($event -ceq $entry) {
    $tmp = Invoke-GTK("gtk_entry_get_text $entry" )
    if($tmp.Length -gt 1) {
            Invoke-GTK("gtk_text_insert $text NULL NULL NULL `"$tmp`n`" -1")
       }
    # Empty entry field
       Invoke-GTK("gtk_editable_delete_text $entry 0 -1")
    }
} while ($event -cne $button)

Invoke-GTK("gtk_server_exit")


0 comments: