# # window positioning -- remember a window's position between invocations # of a program. # use Gtk2 -init; my $pos; # load from disk if it exists. if (-f 'saved') { eval join "", `cat saved`; } my $window = Gtk2::Window->new; $window->signal_connect (delete_event => sub {Gtk2->main_quit; 0}); # if we have previous position information, then apply if after the window # is created, but before it is shown -- this avoids the flicker of showing # and then relocating and resizing the window. # # there are two approaches to take, the gtk+-only approach, and the # gdk-only approach. either works. the gdk approach requires that the # GdkWindow be created; most GtkWidgets which have GdkWindows create those # windows in realize, so you'd have to use move_resize on the GdkWindow # *after* the normal realize handler has run, like this: # # $window->signal_connect_after (realize => sub { # $window->window->move_resize ($pos->{x}, $pos->{y}, # $pos->{width}, $pos->{height}); # }) # if $pos; # # a GtkWindow, on the other hand, will queue operations that require a # GdkWindow and apply them once it has been created, so it's usually easier # and more straightforward to have Gtk do the work for you, like this: # $window->signal_connect (realize => sub { $window->move ($pos->{x}, $pos->{y}); $window->resize ($pos->{width}, $pos->{height}); }) if $pos; # # A toplevel window gets a configure event whenever the window is # "configured", that is, whenever aspects of the window, such as position # and size, change. Here i've connected to the event_after signal, # which is fired after an event (duh), and store the actual position # and size of the window. You could get that information from the # event passed to a configure handler, but i'm paranoid and want to # get the information that's actually used, not something that may be # mangled. # $window->signal_connect (event_after => sub { my (undef, $ev) = @_; return unless $ev->type eq 'configure'; my ($x, $y) = $window->get_position; $pos->{x} = $x; $pos->{y} = $y; $pos->{width} = $window->allocation->width; $pos->{height} = $window->allocation->height; }); $window->show; Gtk2->main; # store to disk. open OUT, ">saved"; use Data::Dumper; print OUT Data::Dumper->Dump([$pos], ['$pos']); close OUT;