GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Aug 24, 2016 8:28 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: problem in g_signal connect
PostPosted: Mon Jan 25, 2016 11:55 am 
Offline
Familiar Face

Joined: Thu Jul 02, 2015 7:30 am
Posts: 18
Hi,
here i attached my code i know i made mistake in code. i don't know how to do this. my concept is i have create two window when i press "1" key on key board it will show second window.
from the second window when i press "2" key on keyboard it destroy the second window and pass value to "key_event_cb" function and compare value and display some text on label. i know i made many mistake.
my problem is how to use "g_signal_connect" function with in function below i shown in "gtk_img" function .

please help me.

Code:
#include<gtk/gtk.h>

void secwindow();
GtkWidget *window;
GtkWidget *Window2;
GtkWidget *label;

static gboolean key_event_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
{
const gchar *va=gdk_keyval_name(((GdkEventKey*)event)->keyval);
if(!(g_strcmp0(va,"1")))
{
secwindow();
}
if(!(g_strcmp0(va,"2")))
{

}
if(!(g_strcmp0(data,"3")))
{
gtk_label_set_text(GTK_LABEL(label),"A");
}

    return FALSE;
}
static gboolean gtk_img(GtkWidget *widget, GdkEventKey *event, gpointer data)
{
const gchar *va=gdk_keyval_name(((GdkEventKey*)event)->keyval);
if(!(g_strcmp0(va,"1")))
{
gtk_image_set_from_file(GTK_IMAGE(data),"/home/yyy.jpg);
}
if(!(g_strcmp0(va,"2")))
{
gtk_widget_destroy(window2);
g_signal_connect(window2,"key_release_event",G_CALLBACK(key_event_cb),(gpointer)"3");
window2=NULL;
}
    return FALSE;
}

void secwindow()
{
    window2=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window2),1920,1080);
    g_signal_connect(window2,"destroy",G_CALLBACK(gtk_main_quit),NULL);
  GtkWidget *img=gtk_image_new_from_file("/home/xxx.jpg");
   g_signal_connect(window2,"destroy",G_CALLBACK(gtk_img),img);
   gtk_container_add(GTK_CONTAINER(window2), da);
    gtk_widget_show_all(window2);
}
int main(int argc,char *argv[])
{
    gtk_init(&argc,&argv);
    window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window),1920,1080);
    g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);
label=gtk_label_new("");
   g_signal_connect(window, "key_release_event", G_CALLBACK(key_event_cb),NULL);
    gtk_container_add(GTK_CONTAINER(window), label);
    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}


thank you


Top
 Profile  
 
 Post subject: Re: problem in g_signal connect
PostPosted: Tue Jan 26, 2016 3:08 am 
Offline
Never Seen the Sunlight

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 416
Try to make some sense out of your code. Where you are connecting a signal, you first destroy the window2 you want to connect the signal to and then set the window2 to NULL. You have nothing to connect to. You don't even have a window2 there to destroy. Window2 is also destroyed in two other places.

If you need to create another window after your top level window use a dialog. You can branch from your main window to as many windows as you need. Test out a dialog example and understand how that works. It is a lot easier to figure out a dialog than unusual specialty keyboard shortcuts.

Also, consistent code formatting is very helpful when someone tries to read your code. C doesn't enforce a format like Python does but having consistent indentation is very helpful in reading code. Keep at it.

Code:
//gcc -Wall dialog1.c -o dialog1 `pkg-config --cflags --libs gtk+-3.0`

#include <gtk/gtk.h>

static void open_dialog(GtkWidget *widget, gpointer data);

int main(int argc, char **argv)
  {     
    gtk_init(&argc, &argv);   

    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 200, 100);
    gtk_window_set_title(GTK_WINDOW(window), "Open Dialog");
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkWidget *button1=gtk_toggle_button_new_with_label("Open a Dialog");
    gtk_widget_set_hexpand(button1, TRUE);
    gtk_widget_set_vexpand(button1, TRUE);
    g_signal_connect(button1, "clicked", G_CALLBACK(open_dialog), window);
     
    GtkWidget *grid=gtk_grid_new();
    gtk_grid_attach(GTK_GRID(grid), button1, 0, 0, 1, 1);

    gtk_container_add(GTK_CONTAINER(window), grid);

    gtk_widget_show_all(window);                 
    gtk_main();
    return 0;
  }
static void open_dialog(GtkWidget *widget, gpointer data)
  {
    //Non modal dialog. Open several dialog windows from the main window.
    static gint i=1;
    gchar *dialog_label=g_strdup_printf("Dialog Window%i", i);
    GtkWidget *dialog=gtk_dialog_new_with_buttons("Dialog", GTK_WINDOW(data), GTK_DIALOG_DESTROY_WITH_PARENT, "Close", GTK_RESPONSE_CLOSE, NULL);                                 
    GtkWidget *content_area=gtk_dialog_get_content_area(GTK_DIALOG(dialog));
    GtkWidget *label=gtk_label_new(dialog_label);
    gtk_container_add(GTK_CONTAINER(content_area), label);
    g_signal_connect_swapped(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog);
    gtk_widget_show_all(dialog); 

    //Move the windows.
    gint width = gdk_screen_width();
    gint height = gdk_screen_height();
    gint sections_x = width / 100;
    gint sections_y = height / 100;
    gint s1 = (i - 1) % (sections_x * sections_y);
    gint move_x = (s1 % sections_x) * 100;
    gint move_y = (s1 / sections_x) * 100;   
    g_print("Move %i %i %i %i %i %i\n", width, height, sections_x, sections_y, move_x, move_y);
    gtk_window_move(GTK_WINDOW(dialog), move_x, move_y);

    i++;
    g_free(dialog_label);       
  }


Last edited by cecashon on Tue Jan 26, 2016 10:07 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: problem in g_signal connect
PostPosted: Tue Jan 26, 2016 7:45 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 1045
Location: UK
Hallo,

When you post code here there is the BB codes you can use so that the formatting remains. I changed it for you. :)

With regards to formatting your own code it is best to remain consistent as that would make it easier for others to read and for you when you come back to it the following year. :) cecashon has remained constant with his formatting although I would like to see more spaces put in between functions and operators. For example
Code:
  an_integer = 10;
  an_integer += 10;

  /* this is harder to read and has a mistake*/
  an+integer+=10;


With getting the key presses you can use
Code:
#include <gdk/gdkkeysyms.h>   /*get the keyboard symbols */

static gboolean key_event_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
{
  switch (event->key_val) {
  case GDK_KEY_1:
    /* Do something for keypress 1 */
    break;
  case GDK_KEY_2:
    /* Do something for keypress 2 */
    break;
  case GDK_KEY_3:
    /* Do something for keypress 3 */
    break;
  }

   return FALSE;
}
Is is better to use the "event->keyval" directly with the GDK_KEY_* values as there are keys that have no printed equivalent. The best way is in GTK+ documents at https://developer.gnome.org/gtk3/stable/checklist-modifiers.html

Creating and deleting windows constantly even without key presses is not a good practice. What happens is the window manager is free to place the window anywhere on the screen and you may not have any control over this. If you want the window to stay in the same place you should just change the contents. And give the user many options such as using the pointer device (mouse).

_________________
E.


Top
 Profile  
 
 Post subject: Re: problem in g_signal connect
PostPosted: Tue Jan 26, 2016 10:09 pm 
Offline
Never Seen the Sunlight

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 416
Errol, for moving windows on the screen, with gtk_window_move() the documentation states "honor requests after the window has already been shown." If I move the window after gtk_widget_show_all() will that always work? It seems to work fine on Ubuntu14.04. Just added gtk_window_move() to the code above. Added some extra formatting space as well.

I usually don't create many windows like this but have used a series of dialogs that I have left open for allowing some numerical comparisons. Maybe a better way to do this but it does allow you to look at the numbers before sending them to the database.


Top
 Profile  
 
 Post subject: Re: problem in g_signal connect
PostPosted: Wed Jan 27, 2016 8:07 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 1045
Location: UK
The first paragraph from the documentation for gtk_window_mode() is
Quote:
Asks the window manager to move window to the given position. Window managers are free to ignore this; most window managers ignore requests for initial window positions (instead using a user-defined placement algorithm) and honor requests after the window has already been shown.
What this means is MOST window managers will ignore the initial window placement and when it is shown MOST will move the window but not all.

There is a part there which states that the window manager is free to ignore all requests to move the window.

If an application set the window position it must not assume that is where it is placed. Also getting the window position can not be assumed to be where the window currently is. Between the application getting and using the window position the user could have moved the window :-0.

Thanks for adding the spaces :). Its much easier to read just like sentences. Thankyouverymuch ;)

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google Adsense [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group