ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/gtk.pl
Revision: 1.4
Committed: Sat May 31 11:12:22 2003 UTC (21 years ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.3: +7 -4 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 package gtk;
2
3 use Carp;
4
5 our $text_renderer = new Gtk2::CellRendererText;
6 our $int_renderer = new Gtk2::CellRendererText;
7 $int_renderer->set (xalign => 1);
8
9 our $state = $util::state->{gtk} || {};
10
11 # shows the properties of a glib object
12 sub info {
13 my ($idx, $obj) = @_;
14 last if $seen{$idx}++;
15 print "\n$idx\n";
16 for ($obj->list_properties) {
17 printf "%-16s %-24s %-24s %s\n", $_->{name}, $_->{type}, (join ":", @{$_->{flags}}), $_->{descr};
18 }
19 }
20
21 # grr... more gtk+ brokenness
22 my %get = (
23 window_size => sub { [ ($_[0]->allocation->values)[2,3] ] },
24 #window_pos => sub { die KGS::Listener::Debug::dumpval [ $_[0]->get_root_origin ] },
25 clist_column_widths => sub {
26 $_[0]{column_widths};
27 },
28 );
29
30 my %set = (
31 window_size => sub { $_[0]->set_default_size(@{$_[1]}) },
32 #window_pos => sub { $_[0]->set_uposition(@{$_[1]}) if @{$_[1]} },
33 clist_column_widths => sub {
34 my ($w, $v) = @_;
35 $v->[$_] && $w->set_column_width($_, $v->[$_]) for 0..$#$v;
36 $w->{column_widths} = $v;
37 $w->signal_connect(resize_column => sub { $v->[$_[1]] = $_[2]; });
38 },
39 );
40
41 sub state {
42 my ($widget, $class, $instance, %attr) = @_;
43
44 while (my ($k, $v) = each %attr) {
45 my ($set, $get) = $k =~ /=/ ? split /=/, $k : ($k, $k);
46 $v = $state->{$class}{"*"}{$get} if exists $state->{$class}{"*"}{$get};
47 $v = $state->{$class}{$instance}{$get} if exists $state->{$class}{$instance}{$get};
48 $set{$get} ? $set{$get}->($widget, $v) : $widget->set($set => $v);
49 }
50
51 $widget = [$widget, $class, $instance, \%attr];
52 Scalar::Util::weaken $widget->[0];
53
54 @widgets = (grep $_->[0], @widgets, $widget);
55 }
56
57 sub save_state {
58 for (@widgets) {
59 if ($_->[0]) {
60 my ($widget, $class, $instance, $attr) = @$_;
61
62 $widget->realize if $widget->isa(Gtk2::Widget::);
63
64 while (my ($k, $v) = each %$attr) {
65 my ($set, $get) = $k =~ /=/ ? split /=/, $k : ($k, $k);
66 $v = $get{$get} ? $get{$get}->($widget) : $widget->get($get);
67
68 warn "$class : $get = $v\n";#d#
69 $state->{$class}{"*"}{$get} = $v;
70 $state->{$class}{$instance}{$get} = $v;
71 }
72 }
73 ::status("save_state", "layout saved");
74 }
75 }
76
77 # make a clist unselectable
78 sub clist_autosort {
79 my $w = shift;
80 my ($c, $o) = (-1);
81 for (0..$w->columns-1) {
82 $w->signal_connect(click_column => sub {
83 if ($_[1] != $c) {
84 $c = $_[1];
85 $o = 0;
86 } else {
87 $o = !$o;
88 }
89 $w->set_sort_column($c);
90 $w->set_sort_type($o ? "descending" : "ascending");
91 $w->sort;
92 });
93 }
94 }
95
96 package gtk::widget;
97
98 # hacked gtk pseudo-widget
99
100 sub new {
101 my $class = shift;
102 bless { @_ }, $class;
103 }
104
105 sub widget { $_[0]{widget} }
106
107 sub AUTOLOAD {
108 $AUTOLOAD =~ /::([^:]+)$/ or Carp::confess "$AUTOLOAD: no such method (illegal name)";
109 my $method = $_[0]{widget}->can($1)
110 or Carp::confess "$AUTOLOAD: no such method";
111 # do NOT cache.. we are fats enough this way
112 unshift @_, shift->{widget};
113 &$method;
114 }
115
116 package gtk::text;
117
118 use base gtk::widget;
119
120 sub new {
121 my $class = shift;
122 my $self = $class->SUPER::new(@_);
123
124 $self->{buffer} = new Gtk2::TextBuffer undef;
125
126 $self->{widget} = new Gtk2::TextView;
127
128 $self;
129 }
130
131 sub append_text {
132 my ($self, $text);
133 }
134
135 sub set_text {
136 my ($self, $text) = @_;
137
138 #clear
139 #$self->add_text ($text);
140 }
141
142 1;
143