RE: probable gtk_window_present and pango help

2012-05-18 Thread Tilton, James C. (GSFC-6063)
Also consider "hiding" your window (with a call to window->hide()) instead of 
destroying it, and making  it reappear with a call to window->show().

Jim Tilton

-Original Message-
From: gtk-app-devel-list-boun...@gnome.org 
[mailto:gtk-app-devel-list-boun...@gnome.org] On Behalf Of David Necas
Sent: Friday, May 18, 2012 2:19 PM
To: Rudra Banerjee
Cc: gtk-app-devel-list
Subject: Re: probable gtk_window_present and pango help

On Fri, May 18, 2012 at 09:12:45PM +0530, Rudra Banerjee wrote:
> Here is a minimal example of a program, where if i click the button, a 
> pop up window appears. I am posting both the call back function and 
> the main routine (table.c).
> I am facing 2 problem.

Unfortunately, you are facing much more problems.

For start

gtk_container_add(GTK_CONTAINER(window1), vbox1);

is called with an unitialised variable window1 (i.e. no window1 is ever 
created).  This leads to a CRITICAL message to console and/or crash.

And if window1 existed then vbox1 would be packed into two different containers 
(window1 and window) which is not possible.  So I'm ignoring that part 
altogether.

The callback should look like

void
callback_it(GtkWidget *button, gpointer user_data) {
   ...
}

where in user_data the callback receives the last argument you passed to 
g_signal_connect().  It is rarely useful to pass a constant such as "Call" 
there.

The callback is *not* another main() function; it should *not* call
gtk_init() again, etc.  Please read the signals section in the Gtk+ tutorial

http://developer.gnome.org/gtk-tutorial/2.90/x159.html

There are several other highly suspicious things:
- redeclaration of global variables (such as window) inside a function;
  are you aware this creates a new local variable window that has nothing
  to do with the global one?
- inclusion of .c files instead of separate compilation + linking them
  together
- calling gtk_widget_show() on individual widgets and then again showing
  everything using gtk_widget_show_all()
- not terminating the Gtk+ main loop when the main window is destroyed;
  this is usually the first thing to set up in a Gtk+ program, see

http://developer.gnome.org/gtk-tutorial/2.90/c39.html#SEC-HELLOWORLD

etc.

> 1) The problem is evry time I click the button "main", a new window 
> appears(obviously). What I want to achive is, if the window is already 
> present, it should not open again; rather it should focus that window. 
> I believe, this can be achived by gtk_window_present(may be I am wrong).
> But I don't know how to use it.

You use it just by calling gtk_window_present() on the window object.
To that meaningfully you need not only to keep the window object around but 
also get notified when the window is destroyed.  Either by connecting to the 
"destroy" signal or, if you just want to set a pointer to NULL once it is gone, 
by using g_object_add_weak_pointer().

> 2) In the callback function, I have C_1 and C_2. What I want to achive 
> is C1 etc via pango(or any other).

For simple things you can sometimes just use UTF-8.  But generally, you need to 
use Pango markup.  If the widget does not have function to set the markup it 
has a function to obtain the label widget so that you can use 
gtk_label_set_markup() on that.

See the attached code with main problems fixed (and merged to one file).
Please go through the Gtk+ tutorial farther that you perhaps have done.

Yeti

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: probable gtk_window_present and pango help

2012-05-18 Thread David Nečas
On Fri, May 18, 2012 at 09:12:45PM +0530, Rudra Banerjee wrote:
> Here is a minimal example of a program, where if i click the button, a
> pop up window appears. I am posting both the call back function and the
> main routine (table.c).
> I am facing 2 problem.

Unfortunately, you are facing much more problems.

For start

gtk_container_add(GTK_CONTAINER(window1), vbox1);

is called with an unitialised variable window1 (i.e. no window1 is ever
created).  This leads to a CRITICAL message to console and/or crash.

And if window1 existed then vbox1 would be packed into two different
containers (window1 and window) which is not possible.  So I'm ignoring
that part altogether.

The callback should look like

void
callback_it(GtkWidget *button, gpointer user_data)
{
   ...
}

where in user_data the callback receives the last argument you passed to
g_signal_connect().  It is rarely useful to pass a constant such as
"Call" there.

The callback is *not* another main() function; it should *not* call
gtk_init() again, etc.  Please read the signals section in the Gtk+
tutorial

http://developer.gnome.org/gtk-tutorial/2.90/x159.html

There are several other highly suspicious things:
- redeclaration of global variables (such as window) inside a function;
  are you aware this creates a new local variable window that has nothing
  to do with the global one?
- inclusion of .c files instead of separate compilation + linking them
  together
- calling gtk_widget_show() on individual widgets and then again showing
  everything using gtk_widget_show_all()
- not terminating the Gtk+ main loop when the main window is destroyed;
  this is usually the first thing to set up in a Gtk+ program, see

http://developer.gnome.org/gtk-tutorial/2.90/c39.html#SEC-HELLOWORLD

etc.

> 1) The problem is evry time I click the button "main", a new window
> appears(obviously). What I want to achive is, if the window is already
> present, it should not open again; rather it should focus that window. I
> believe, this can be achived by gtk_window_present(may be I am wrong).
> But I don't know how to use it.

You use it just by calling gtk_window_present() on the window object.
To that meaningfully you need not only to keep the window object around
but also get notified when the window is destroyed.  Either by
connecting to the "destroy" signal or, if you just want to set a pointer
to NULL once it is gone, by using g_object_add_weak_pointer().

> 2) In the callback function, I have C_1 and C_2. What I want to achive
> is C1 etc via pango(or any other).

For simple things you can sometimes just use UTF-8.  But generally, you
need to use Pango markup.  If the widget does not have function to set
the markup it has a function to obtain the label widget so that you can
use gtk_label_set_markup() on that.

See the attached code with main problems fixed (and merged to one file).
Please go through the Gtk+ tutorial farther that you perhaps have done.

Yeti

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

probable gtk_window_present and pango help

2012-05-18 Thread Rudra Banerjee
Dear Friends,
Here is a minimal example of a program, where if i click the button, a
pop up window appears. I am posting both the call back function and the
main routine (table.c).
I am facing 2 problem.
1) The problem is evry time I click the button "main", a new window
appears(obviously). What I want to achive is, if the window is already
present, it should not open again; rather it should focus that window. I
believe, this can be achived by gtk_window_present(may be I am wrong).
But I don't know how to use it.

2) In the callback function, I have C_1 and C_2. What I want to achive
is C1 etc via pango(or any other).

I am not a C programmer. And this is my *first* GTK as well.
So, I am posting both the code. I will be grateful if anybody take some
time to suggest me how to get things done.
Regards and Best,

//###
//#   FILE TABLE.C
//
#include 
#include 
#include 
#include 
GtkAccelGroup *menuGroup;
GtkWidget  *window1 ;
GtkWidget *window;
GtkWidget *button;
GtkWidget *table;
GtkWidget  *vbox1 ;
GtkWidget  *menubar ;
GtkWidget  *helpmenu ;
GtkWidget  *helpmenu_menu ;
GtkWidget  *helphelp ;
GtkWidget  *helpabout ;

#include "prop_label.c"

int main( int   argc,
  char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *table;

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Main");
gtk_container_set_border_width (GTK_CONTAINER (window), 20);


/*
 * MENU BAR
 */
vbox1 = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window1), vbox1);

menubar = gtk_menu_bar_new ();
gtk_box_pack_start (GTK_BOX (vbox1), menubar, FALSE, FALSE, 2);


/* INITIALIZE THE TABLE
 */
table = gtk_table_new (1, 1, TRUE);
gtk_box_pack_start (GTK_BOX (vbox1), table, FALSE, FALSE, 2);
gtk_container_add (GTK_CONTAINER (window), vbox1);

/* Color Scheme */
GdkColor colorBlue = { 0x, 3598, 57054, 61937 };

/* Create first button */
button = gtk_button_new_with_label ("main");
g_signal_connect (button, "clicked",
  G_CALLBACK (callback_it), (gpointer) "Call");
gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 0, 1);
gtk_widget_modify_bg( button, GTK_STATE_NORMAL, &colorBlue );
gtk_widget_show (button);



gtk_widget_show (table);
gtk_widget_show (window);
gtk_widget_show_all (window);

gtk_main ();

return 0;
}

/*###
#   FILE PROP_LABEL.C
*/

#include 

int callback_it( int   argc,
  char *argv[] )
{
  static GtkWidget *window = NULL;
  GtkWidget *hbox;
  GtkWidget *vbox;
  GtkWidget *frame;
  GtkWidget *label;


  /* Initialise GTK */
  gtk_init(&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Callback");
  vbox = gtk_vbox_new (FALSE, 0);
  hbox = gtk_hbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), hbox);
  gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (window), 25);


  frame = gtk_frame_new ("C_1");
  label = gtk_label_new ("1\n2\n3");
  gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
  gtk_container_add (GTK_CONTAINER (frame), label);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

  frame = gtk_frame_new ("C_2");
  label = gtk_label_new ("1\n2\n3");

  gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
  gtk_container_add (GTK_CONTAINER (frame), label);
  gtk_box_pack_start (GTK_BOX (hbox), frame, FALSE, FALSE, 0);

  gtk_window_set_resizable(GTK_WINDOW (window), FALSE);
  gtk_widget_show_all (window);
}



___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list