[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl-Toolbox] Re: [MacPerl-Modules] Help: Dynamic list dialog boxes from MacPerl



At 14:20 +0100 2000.07.26, Campbell-Lange wrote:
>I'm writing a little perl module that parses a css file for classes to
>offer a choice for input into BBEdit. The author may choose between div,
>span or standard elements.
>
>I would like a dialog box list to pop up with a list of elements. The list
>would be dependant on whether the user clicks a radio button selecting
>"DIV", "SPAN" or "Standard".
>
>I can do this pretty easily using the Dialog Director osax in Applescript.
>However I'd like to do this all through MacPerl if possible. I have no
>experience in writing custom dialog boxes through MacPerl, and would like
>some pointers.

Use Dialog Director, with Mac::Glue.  :)


>A Dialog Director list example (from Hyde's own set) is:
>
>dd install with fonts [
>    {name:"Geneva", size:9}, null, null, null, {style:underline}]
>    with grayscale
>    dd make dialog {size:[370, 190], style:standard window,
>    closeable:true, contents:[¬
>        {   class:list box, contents:Sort(l, order - 4), bounds:[8, 24,
>            362,182],
>            column widths:[140, 40, 80]}, ¬
>        {   class:poly push button, bounds:[12, 4, 38, 17]}, ¬
>        {   class:poly push button, bounds:[152, 4, 173, 17]}, ¬
>        {   class:poly push button, bounds:[192, 4, 214, 17]}, ¬
>        {   class:static text, contents:"Name", bounds:[12, 4, 38,
>            17],font:5}, ¬
>        {   class:static text, contents:"Size", bounds:[152, 4, 174, 17]},
>        {   class:static text, contents:"Date", bounds:[192, 4, 214, 17]} ¬
>    ], name:"Sorted List Demo"}

I opened up the complete example, and wrote this port to MacPerl with
Mac::Glue.  I modified it to get the ACTUAL dates and sizes instead of the
random ones the original provided, and then I added the code to actually
get the result.  Note that the sorting of the lists is much simpler in
Perl, and the rest of the code is about the same in Perl and AppleScript
(it is a little bit more verbose in Perl).

As much as possible, I try to make stuff in Mac::Glue very intuitive for
translating directly from AppleScript.  I think you'll be able to put the
two next to each other and see how I got stuff here, for the most part.
Slight differences exist (you need to explicitly call the obj() or prop()
method in Perl, or the enum() function, and instead of "null" we pass in
empty lists for the dd_install() method), but it is very similar.

Enjoy.

#!perl -w
use Mac::Files;
use Mac::Glue ':all';
use POSIX;
use strict;

my $order = 5;
my $path = FindFolder(kOnSystemDisk, kFontsFolderType);
my @list = get_files($path);
my $nlist;

my @sort = (
	sub { lc $a->[0] cmp lc $b->[0] },
	sub { $a->[1] <=> $b->[1] },
	sub { $a->[2] <=> $b->[2] }
);

my $glue = new Mac::Glue 'Finder';
$glue->activate;
$glue->dd_install(
	with_fonts => [{
		name	=> 'Geneva',
		size	=> 9
	}, {}, {}, {}, {
		style => enum('underline')
	}],
	grayscale => gTrue
);
die $^E if $^E;

END {
	$glue->dd_uninstall;
	warn $^E if $^E;
}

my $dd = $glue->dd_make_dialog({
	name		=> 'Sorted List Demo',
	size		=> [370, 190],
	style		=> enum('standard window'),
	closeable	=> gTrue,
	contents	=> [
		{
			class		=> 'list box',
			contents	=> list_sort(\@list, $order - 5),
			bounds		=> [8, 24, 362, 182],
			column_widths	=> [140, 40, 80]
		}, {
			class		=> 'poly push button',
			bounds		=> [12, 4, 38, 17]
		}, {
			class		=> 'poly push button',
			bounds		=> [152, 4, 173, 17]
		}, {
			class		=> 'poly push button',
			bounds		=> [192, 4, 214, 17]
		}, {
			class		=> 'static text',
			contents	=> 'Name',
			bounds		=> [12, 4, 38, 17],
			font		=> 5
		}, {
			class		=> 'static text',
			contents	=> 'Size',
			bounds		=> [152, 4, 174, 17]
		}, {
			class		=> 'static text',
			contents	=> 'Date',
			bounds		=> [192, 4, 214, 17]
		}
	]
});
die $^E if $^E;

while (1) {
	my $i = $glue->dd_interact_with_user;
	if ($i >= 2 && $i <= 4 && ($i + 3) != $order) {
		$glue->dd_set(
			$glue->prop(font => item => $order => dialog => 1),
			to => 4
		);
		$order = $i + 3;
		$glue->dd_set(
			$glue->prop(font => item => $order, dialog => 1),
			to => 5
		);
		$glue->dd_set(
			$glue->prop(contents => item  => 1, dialog => 1),
			to => list_sort(\@list, $order - 5)
		);
	} elsif ($i == -1) {
		last;
	}
}

my $item = $glue->dd_get( $glue->prop(value => item => 1 => $dd) ) - 1;
warn $^E if $^E;

for ([split /\t/, $nlist->[$item]]) {
	print "You selected $_->[0], $_->[1]K, last modified $_->[2].\n";
}


sub get_time {
	return $^T - ((-M $_[0]) * 86400);
}

sub get_size {
	my $cat = FSpGetCatInfo($_[0]);
	my $bytes = $cat->ioFlLgLen + $cat->ioFlRLgLen;
	return int($bytes / 1024);
}

sub get_files {
	local *PATH;
	my $path = shift;
	chdir $path or die $!;
	opendir(PATH, $path) or die $!;

	my @list = map {[
		$_, get_size($_), get_time($_),
		strftime("%A, %B %d, %Y", localtime(get_time($_))),
	]} readdir PATH;

	return @list;
}

sub list_sort {
	my($list, $i) = @_;
	$nlist = [map { join "\t", @{$_}[0,1,3] } sort { &{$sort[$i]} }
@$list];
	return $nlist;
}

__END__


-- 
Chris Nandor       |     pudge@pobox.com      |     http://pudge.net/
Andover.Net        | chris.nandor@andover.net | http://slashcode.com/

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-toolbox-request@macperl.org