Window centering continued




I was the guy who started the thread about window centering.  Thanks to
Havoc for suggestions about gtk_widget_set_uposition() but a few things
have come up..

The core issue was that if you use
gtk_window_set_position(GTK_WINDOW(some_widget), GTK_WIN_POS_CENTER);
It works great on a lot of window managers, but some versions of
enlightenment just put the window wherever they feel like it.

Now if I want to center the window by gtk_widget_set_uposition, I'm going
to need some coordinates.  So I wrote this function:  (coords is a
structure that holds an int x, int y)

static coords center_window_get_coords(GtkWidget *input)
{
     int rh, rw, newx, newy, oh, ow, x, y, depth;
     coords result;

     /* Initialize all that good stuff */
     x = y = depth = newx = newy = rh = rw = oh = ow = 0;

     /* x,y is our position, depth is a throwaway, newx,newy is where were
      * moving to.  rh is root window's height, rw is it's width.  oh is
      * the current window's height, ow it's width.
     */
     gtk_widget_realize(input);  /* So input->window is != NULL */

     gdk_window_get_geometry(input->window, &x, &y, &ow, &oh, &depth);

     rw = gdk_screen_width();
     rh = gdk_screen_height();

     /* the center of the screen is rw/2, rh/2, but we have to subtract
      * half the width and height of our window because the coordinates
      * we're going to correspond to the upper left hand corner and the
      * upper left hand corner of the root window is 0,0
      * The casting is probably just paranoia on my part
      */
     newx = ((int)(rw / 2) - (int)(ow / 2)); 
     newy = ((int)(rh / 2) - (int)(oh / 2));

     /* Check for an "illegal" result */
     if(newx < 0 || newy < 0 || newx > rw || newy > rh)
     {
	  result.x = 0;      /* Coordinates out of range */
          result.y = 0;      /* bomb on us */
          return(result);
     } /* End if */

     result.x = newx;
     result.y = newy;
     return(result);
} /* End center_window_get_coords */

I would then use gtk_widget_set_uposition() on this.  Oh and by the way,
depth isn't *technically* a throw away value - more accurately it's
getting thrown away since I don't know what it corresponds to or if it
impacts this situatoin.  :)  The call after the fact is:

foovariable = center_window(somewidget);

gtk_widget_set_uposition(somewidget, foovariable.x, foovariable.y);

I've also tried gdk_window_move() with the same results described below...

Now there's another problem.  If the widget is not visible, (i.e. I
haven't called gtk_widget_show() on it) then the get_geometry() call lies
to me and tells me the window is 20x22 every time.  This sort of makes
sense, since it's hard to tell how big a window is if it isn't on screen.
So, I have to show the widget before I can pull this off.  But then, if I
do that, then the window "jumps" from wherever the wm put it originally to
the spot I specify after figuring out what the coordinates should be.  To
complicate things, sometimes the get_geometry call gives bad width/height
even AFTER showing the widget.  (I've also tried gtk_widget_show_all()
with the same result)

A few extra things that may have nothing to do with the situation - if a
widget has been shown, the get_geometry call seems to return bad values
under a specific situatoin I can see - when a window has a multiline label
widget somewhere in it.  I can't imagine that would have anything to do
with it, but it's an extra piece of "observation"

For the record, I agree with Havoc in that if an application wants to
enforce wherever it wants to be rather than just suggesting where it wants
to be and letting the wm have the final say, the app is broken.
Unfortunately, the spec for this application *requires* that all windows
pop up completely centered.  Just using the gtk_window_set_position works
for the vast majority of window managers, but E doesn't want to play nice
for some reason.

Any pointers on what could be going wrong or how to get around this I
would really appreciate.

David Allen

http://opop.nols.com/ Free Software Development
==========================================================================
Der Horizont vieler Menschen ist ein Kreis mit Radius Null -- und das
nennen sie ihren Standpunkt.
==========================================================================
perl -e 'print $i=pack(c5,(41*2),sqrt(7056),(unpack(c,H)-2),oct(115),10);'



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]