# Save window position

**URL:** <https://www.ruby-forum.com/t/save-window-position/203757>\
**Category:** Ruby-Gnome 2\
**Created:** [February 26, 2011, 3:13pm UTC](https://www.ruby-forum.com/t/save-window-position/203757 "2011-02-26T15:13:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 26, 2011, 3:13pm UTC](https://www.ruby-forum.com/t/save-window-position/203757/1 "2011-02-26T15:13:28Z")

</div>

How do I get window position?  
I want to save/restore position of window so then  
when I call my program again, it will display where it was closed

window.position always give me the same result, no matter where I move  
the  
form  
perhaps because i set my window to run initially at center of screen

if you know, also restoring size also helpful  
since I stuck with position  
I don’t feel like doing save/restore size

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [February 27, 2011, 2:42am UTC](https://www.ruby-forum.com/t/save-window-position/203757/2 "2011-02-27T02:42:48Z")

</div>

> I do use Gtk::Window#position an d Gtk::Window#move to save and  
> restore position. Do you have a small example that is not working ?

I know you use that from Zik source, that’s why I don’t understand the  
reason it is not working with mine. Or perhaps because the ruby-gnome2  
library I use? I use the one comes with my repository because I hate  
compiling something

Too bad I don’t have any small example, because I use some kind of  
self-made  
framework  
to build gtk application, while it’s make it easier for me, it also  
includes  
alot of files

I’ll try to dig some more, meanwhile if you don’t mind me asking  
something  
else  
in Zik, how do you monitor song progress to update the label and  
progress  
bar?

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [February 27, 2011, 1:14am UTC](https://www.ruby-forum.com/t/save-window-position/203757/3 "2011-02-27T01:14:15Z")

</div>

2011/2/26 hendra kusuma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I don’t feel like doing save/restore size
> 
> –  
> Suka linux?  
> Kunjungi blog saya [http://penguinroad.blogspot.com](http://penguinroad.blogspot.com)

I do use Gtk::Window#position an d Gtk::Window#move to save and  
restore position. Do you have a small example that is not working ?

–  
Vincent C.

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [February 27, 2011, 5:42am UTC](https://www.ruby-forum.com/t/save-window-position/203757/4 "2011-02-27T05:42:11Z")

</div>

hendra kusuma wrote in post #984167:

> …how do you monitor song progress to update the label and  
> progress bar?

hi hendra, jk here -

in my simple player, the progress bar works like this:  
#first, i create a progbar when i initialize the class  
@progBar = Gtk::ProgressBar.new  
#then, in my “playTrack” method, i start a thread  
@prog = Thread.new{self.progress}  
#my “progress” method looks like this (i use the gem mp3info for the  
total duration at the moment - trying to get rid of that - and gst for  
the rest

def progress  
Mp3Info.open(@atBat){|data| @dur = data.length}  
while Gst::STATE\_PLAYING  
q = Gst::QueryPosition.new(Gst::Format::TIME)  
@pipeline.query(q)  
pos = q.parse[1] / 1000000  
position = pos.to\_f  
duration = @dur \* 1000  
position = 1.0 if position \< 1.0  
ratio = position/duration  
@progBar.fraction=(ratio)  
end  
end #progress

#then i use these two methods to seek with the progress bar:

def position=(position\_in\_ms)  
@pipeline.send\_event(Gst::EventSeek.new(1.0,  
Gst::Format::Type::TIME,  
Gst::Seek::FLAG\_FLUSH.to\_i | Gst::Seek::FLAG\_KEY\_UNIT.to\_i,  
Gst::Seek::TYPE\_SET, position\_in\_ms \* 1000000,  
Gst::Seek::TYPE\_NONE, -1))  
end

def seekToPointer  
Mp3Info.open(@atBat){|data| @dur = data.length}  
pointer = @progBox.pointer[0] ###  
width = (@progBox.window.size[0]) \* 0.97  
pos = ((@dur \* pointer) / width) \* 1000  
self.position= pos  
self.position= pos  
end

### (@progBox is a Gtk::EventBox which the Gtk::Progress bar is inside,

@atBat is the current track file) ###

in the more complex player, i use an image (the tone arm of the record  
player) to show progress, so i wrote a custom “progress bar” rather than  
use the Gtk::ProgressBar - but it works basically the same way.

hope that helps some,

have fun,

j

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [February 28, 2011, 6:54am UTC](https://www.ruby-forum.com/t/save-window-position/203757/5 "2011-02-28T06:54:34Z")

</div>

On Sun, Feb 27, 2011 at 11:40 PM, Vincent C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> For the window position at what time do you call Gtk::Window#position  
> method ?  
> do you have try something like that ?  
> Gtk::Window#signal\_connect(“delete\_event”) {Gtk::Window#position}

Yes, I put it there,  
it gives me fixed value

if i have this line  
set\_window\_position Gtk:🪟:POS\_CENTER  
it return the posisiton center of screen  
it I delete that line  
it returns 0,0

is it library issue? or perhaps wm issue?  
I’m using ubuntu 10.10 with openbox replacing metacity  
I also use old ruby-gnome2 library since it comes with repository

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [February 27, 2011, 4:40pm UTC](https://www.ruby-forum.com/t/save-window-position/203757/6 "2011-02-27T16:40:56Z")

</div>

For the window position at what time do you call Gtk::Window#position  
method ?  
do you have try something like that ?  
Gtk::Window#signal\_connect(“delete\_event”) {Gtk::Window#position}

Word of advice:  
using Gtk methods and threads can be tricky. You should pay attention  
at what you do that is why I have prefered Glib::Timeout.

–  
Vincent C.

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [February 27, 2011, 9:02am UTC](https://www.ruby-forum.com/t/save-window-position/203757/7 "2011-02-27T09:02:51Z")

</div>

On Sun, Feb 27, 2011 at 12:42 PM, J. K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> rack" method, i start a thread  
> @prog = Thread.new{self.progress}

Thanks J.K.  
I basically solve this issue by following method used in Zik player  
by using Glib::Timeout instead of Thread  
the rest is similar with yours

I nearly finish my player  
I also implement the 2 playlist I write you about  
Now the rest is handling playlist operations such as delete and add  
multiple  
files  
should be easy enought if somehow tutorial of treeview over the net can  
be  
opened

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [February 28, 2011, 5:34pm UTC](https://www.ruby-forum.com/t/save-window-position/203757/8 "2011-02-28T17:34:44Z")

</div>

2011/2/28 hendra kusuma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Yes, I put it there,  
> it gives me fixed value
> 
> if i have this line  
> set\_window\_position Gtk:🪟:POS\_CENTER  
> it return the posisiton center of screen  
> it I delete that line  
> it returns 0,0

Are you sure you call the position method on the top level gtk window  
? Not on a gdk one.  
You should really try to write a small code with just an empty window.

> * * *
> 
> [https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

–  
Vincent C.

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [March 1, 2011, 3:38am UTC](https://www.ruby-forum.com/t/save-window-position/203757/9 "2011-03-01T03:38:07Z")

</div>

> Are you sure you call the position method on the top level gtk window  
> ? Not on a gdk one.  
> You should really try to write a small code with just an empty window.
> 
> >

OK, here it is  
the empty window  
please see attachment

if you find something wrong please correct me  
for most of code there created by my own code generator  
that was made without firm understanding of GTK

Thanks

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [March 1, 2011, 4:10am UTC](https://www.ruby-forum.com/t/save-window-position/203757/10 "2011-03-01T04:10:01Z")

</div>

On 1 March 2011 11:36, hendra kusuma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if you find something wrong please correct me  
> for most of code there created by my own code generator  
> that was made without firm understanding of GTK

Looking at your code, it looks like a bug in ruby-gnome  
to me. It seems to be returning window\_position from  
the Window#position method. I don’t have the source  
handy to check at the moment, though.

```
     MikeC

```

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [March 2, 2011, 9:28am UTC](https://www.ruby-forum.com/t/save-window-position/203757/11 "2011-03-02T09:28:45Z")

</div>

On Tue, Mar 1, 2011 at 12:04 PM, Vincent C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Gtk::Window#signal\_connect(“delete\_event”) {Gtk::Window#position}

Wow, It really works  
Thanks Vincent  
I’ll be sure to add this to my code generator so I won’t forget next  
time

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [March 1, 2011, 5:05am UTC](https://www.ruby-forum.com/t/save-window-position/203757/12 "2011-03-01T05:05:22Z")

</div>

2011/3/1 hendra kusuma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> OK, here it is  
> the empty window  
> please see attachment
> 
> if you find something wrong please correct me  
> for most of code there created by my own code generator  
> that was made without firm understanding of GTK

As I said use  
Gtk::Window#signal\_connect(“delete\_event”) {Gtk::Window#position}  
and not  
Gtk::Window#signal\_connect(“destroy”) {Gtk::Window#position}

Calling methods on a destroyed object is not a good idea.

The code beneath should work.

w=Gtk::Window.new  
w.signal\_connect(“delete\_event”) {w.position; false}  
w.signal\_connect(“destroy”) {Gtk.main\_quit}

Check documentation for more infos on these signals.

> Thanks
> 
> –  
> Suka linux?  
> Kunjungi blog saya [http://penguinroad.blogspot.com](http://penguinroad.blogspot.com)

–  
Vincent C.
