Re: Open a window on top of another



Tara M wrote:

Hi Tara,

   you were crystal clear. Thanks :-)


I have produced this piece of code, and it works.
But I'd like to know why some "strange" things happens.
There are some functions returning unexpected (to me, at least) results.
Could you please have a look at my source code and tell me were I am wrong?

Thanks,

 Fabio

#include <gtk/gtk.h>

#include <stdio.h>

// This function center the windows win on the parent_win
//
// It has some strange things due to the fact that some calls
// seem to return unexpected results.
void center_window_on_parent ( GtkWidget * parent_win, GtkWidget * win )
{
  guint parent_xpos,  parent_ypos;
  guint parent_width, parent_height;
  guint win_width,    win_height;
  gint  xpos, ypos;
  guint tmp, tmp1, tmp2;
  
        // WHY?: this function does not return xpos and ypos correctly
        //       (you should expect gdk_window x,y position, but it
  //        always returns 0,0 - maybe it is returning the ROOT x,y pos? )
  gdk_window_get_geometry ( parent_win->window,
                            &tmp1,         &tmp2,
                            &parent_width, &parent_height,
                            &tmp );

        // This function is needed because the x,y pos of the parent_win cannot be
        // retrieved using gtk_window_get_geometry.
  gdk_window_get_root_origin ( parent_win->window, 
                               &parent_xpos, &parent_ypos );

        // Fortunately, width and height are returned correctly :-)
  gdk_window_get_geometry ( win->window,
                            &tmp1,         &tmp2,
                            &win_width,    &win_height,
                            &tmp ); 

        // NOTICE: I should have used gdk_window_get_size(), but it doesn't seem to work either :-(
        //
        //                               Almost for sure, it's me missing something...

  xpos = ( parent_width / 2 ) - ( win_width / 2 ) + parent_xpos;
  ypos = ( parent_height / 2 ) - ( win_height / 2 ) + parent_ypos;

  printf ( "Parent: xpos: %d - ypos: %d - width: %d - height: %d\n", parent_xpos, parent_ypos, parent_width, 
parent_height );
  printf ( "Window: width: %d - height: %d\n", win_width, win_height );
  printf ( "Pos: width: %d - height: %d\n", xpos, ypos );

  gdk_window_move ( win->window, xpos, ypos );
}

// Create the window that will appear on top of the parent
GtkWidget *  create_win ( GtkWidget * parent_win )
{
  GtkWidget * win;

  win = gtk_window_new ( GTK_WINDOW_TOPLEVEL );

  // This is just to have a smaller window to center on top of the original
  gtk_window_set_default_size ( GTK_WINDOW ( win ), 180, 100 );

        // CRAP: gtk_widget_realize() is not enough, because the
        // gdk_window_move() will not change the position of the window if
        // it hasn't been shown (for real) yet.
  gtk_widget_show_all ( win );
        gtk_widget_hide ( win );

        // Center the window on the parent
        center_window_on_parent ( parent_win, win );

        // and show the window again ... (crap)...
        gtk_widget_show ( win );

  return ( win );
}

// Create the parent window
GtkWidget * create_parent_win ()
{
  GtkWidget * win;

  win = gtk_window_new ( GTK_WINDOW_TOPLEVEL );

  gtk_window_set_default_size ( GTK_WINDOW ( win ), 320, 200 );

  gtk_signal_connect ( GTK_OBJECT ( win ), "destroy", gtk_main_quit, NULL );

  gtk_widget_show_all ( win );

  return ( win );
}

int main ()
{
  GtkWidget * parent_win;
  GtkWidget * win;
  
  gtk_init ( NULL, NULL );

  parent_win = create_parent_win ();
  win        = create_win ( parent_win );

  gtk_main ( );

  return ( 0 );
}


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