How to get mouse pointer position with respect to a GtkImageHow do you find the absolute position of a GTK widget in a window?How do you set, clear, and toggle a single bit?Retrieve the position (X,Y) of an HTML elementHow do function pointers in C work?How to overlay one div over another divMove the mouse pointer to a specific position?Problem with linked libraries and GTK+Fixed position but relative to containerGet global mouse position and use in python program?C: Tracking mouse movement with GTk+GTK Basic Window App on Mac - Warning, Deprecated

Why does processed meat contain preservatives, while canned fish needs not?

Did Henry V’s archers at Agincourt fight with no pants / breeches on because of dysentery?

Where does the labelling of extrinsic semiconductors as "n" and "p" come from?

What word means to make something obsolete?

Can a creature tell when it has been affected by a Divination wizard's Portent?

Will tsunami waves travel forever if there was no land?

Transfer over $10k

What's the metal clinking sound at the end of credits in Avengers: Endgame?

If Earth is tilted, why is Polaris always above the same spot?

Where did the extra Pym particles come from in Endgame?

Weird result in complex limit

How to back up a running remote server?

Is it possible to measure lightning discharges as Nikola Tesla?

Pressure to defend the relevance of one's area of mathematics

"ne paelici suspectaretur" (Tacitus)

Phrase for the opposite of "foolproof"

Airbnb - host wants to reduce rooms, can we get refund?

Help, my Death Star suffers from Kessler syndrome!

What is the strongest case that can be made in favour of the UK regaining some control over fishing policy after Brexit?

Why do computer-science majors learn calculus?

Python "triplet" dictionary?

How does a Swashbuckler rogue "fight with two weapons while safely darting away"?

How to replace the "space symbol" (squat-u) in listings?

Build a trail cart



How to get mouse pointer position with respect to a GtkImage


How do you find the absolute position of a GTK widget in a window?How do you set, clear, and toggle a single bit?Retrieve the position (X,Y) of an HTML elementHow do function pointers in C work?How to overlay one div over another divMove the mouse pointer to a specific position?Problem with linked libraries and GTK+Fixed position but relative to containerGet global mouse position and use in python program?C: Tracking mouse movement with GTk+GTK Basic Window App on Mac - Warning, Deprecated






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I develop in C a GTK graphical user interface consisting in a main window embedding a GtkImage widget.
I cannot retrieve the correct mouse pointer coordinates with respect to the GtkImage widget inside the click event handler when clicking on my GtkImage widget.



I tried both solutions given by the link given below but none of them work:
How do you find the absolute position of a GTK widget in a window?



See below my code :



static void activate(GtkApplication *app, gpointer user_data)

GtkWidget *window;
GtkWidget *grid;

id.rows = ROWS;
id.cols = COLS;
id.stride = COLS * BYTES_PER_PIXEL;
id.stride += (4 - id.stride % 4) % 4; // ensure multiple of 4

// Allocate a bitmap
guchar *pixels = (guchar *)calloc(ROWS * id.stride, 1);
// Create a GDK Pix buffer from a pre-allocated pixel array
GdkPixbuf *pixelBuf = gdk_pixbuf_new_from_data(pixels,
GDK_COLORSPACE_RGB, // colorspace
0, // has_alpha
8, // bits-per-sample
COLS, ROWS, // cols, rows
id.stride, // rowstride
free_pixels, // destroy_fn
NULL // destroy_fn_data
);

// Create theGtkImage widget
GtkImage *image = GTK_IMAGE(gtk_image_new_from_pixbuf(pixelBuf));;
// Create the main win
window = gtk_application_window_new(app);
// Create a grid to automatically place the child widgets
grid = gtk_grid_new();
// Stick this grid to the main window
gtk_container_add(GTK_CONTAINER(window), grid);
// Create an event box to catch mouse event
GtkWidget *event_box = gtk_event_box_new();
// gtk image does'nt handle events, => attach it the event box
gtk_container_add(GTK_CONTAINER(event_box), GTK_WIDGET(image));
// Insert the event box into the grid
gtk_grid_attach(GTK_GRID(grid), GTK_WIDGET(event_box), 0, 0, 1, 1);
// Connect mouse event to the event box
g_signal_connect(GTK_WIDGET(event_box), "button-press-event", G_CALLBACK(on_mouse_press),
pixValEntry);
// LEt's start the show !
gtk_widget_show_all(window);



static gboolean on_mouse_press(GtkWidget *widget, GdkEvent *event,
gpointer user_data)

GdkEventButton *mouse_click = (GdkEventButton *)event;
char str[20];

(void)widget;
(void)user_data;

cout << gtk_widget_get_name(widget) << endl;

if (mouse_click->type == GDK_BUTTON_PRESS)

cout << "mouse_click->x = " << mouse_click->x << endl;
cout << "mouse_click->y = " << mouse_click->y << endl;




(x,y) coordinates shown in the mouse click event handler are shifted and depend on the main window size.
I do not find the way to move from the actual reference system to GtkImage coordinate system.



Many thanks for your help and hints.



Cheers










share|improve this question
























  • gtk_widget_translate_coordinates() does what you need like the linked answer says. I don't think anyone will fix your code since your example won't compile as it is... Maybe show your work with gtk_widget_translate_coordinates(), someone can probably tell what's going wrong.

    – jku
    Mar 23 at 9:19


















0















I develop in C a GTK graphical user interface consisting in a main window embedding a GtkImage widget.
I cannot retrieve the correct mouse pointer coordinates with respect to the GtkImage widget inside the click event handler when clicking on my GtkImage widget.



I tried both solutions given by the link given below but none of them work:
How do you find the absolute position of a GTK widget in a window?



See below my code :



static void activate(GtkApplication *app, gpointer user_data)

GtkWidget *window;
GtkWidget *grid;

id.rows = ROWS;
id.cols = COLS;
id.stride = COLS * BYTES_PER_PIXEL;
id.stride += (4 - id.stride % 4) % 4; // ensure multiple of 4

// Allocate a bitmap
guchar *pixels = (guchar *)calloc(ROWS * id.stride, 1);
// Create a GDK Pix buffer from a pre-allocated pixel array
GdkPixbuf *pixelBuf = gdk_pixbuf_new_from_data(pixels,
GDK_COLORSPACE_RGB, // colorspace
0, // has_alpha
8, // bits-per-sample
COLS, ROWS, // cols, rows
id.stride, // rowstride
free_pixels, // destroy_fn
NULL // destroy_fn_data
);

// Create theGtkImage widget
GtkImage *image = GTK_IMAGE(gtk_image_new_from_pixbuf(pixelBuf));;
// Create the main win
window = gtk_application_window_new(app);
// Create a grid to automatically place the child widgets
grid = gtk_grid_new();
// Stick this grid to the main window
gtk_container_add(GTK_CONTAINER(window), grid);
// Create an event box to catch mouse event
GtkWidget *event_box = gtk_event_box_new();
// gtk image does'nt handle events, => attach it the event box
gtk_container_add(GTK_CONTAINER(event_box), GTK_WIDGET(image));
// Insert the event box into the grid
gtk_grid_attach(GTK_GRID(grid), GTK_WIDGET(event_box), 0, 0, 1, 1);
// Connect mouse event to the event box
g_signal_connect(GTK_WIDGET(event_box), "button-press-event", G_CALLBACK(on_mouse_press),
pixValEntry);
// LEt's start the show !
gtk_widget_show_all(window);



static gboolean on_mouse_press(GtkWidget *widget, GdkEvent *event,
gpointer user_data)

GdkEventButton *mouse_click = (GdkEventButton *)event;
char str[20];

(void)widget;
(void)user_data;

cout << gtk_widget_get_name(widget) << endl;

if (mouse_click->type == GDK_BUTTON_PRESS)

cout << "mouse_click->x = " << mouse_click->x << endl;
cout << "mouse_click->y = " << mouse_click->y << endl;




(x,y) coordinates shown in the mouse click event handler are shifted and depend on the main window size.
I do not find the way to move from the actual reference system to GtkImage coordinate system.



Many thanks for your help and hints.



Cheers










share|improve this question
























  • gtk_widget_translate_coordinates() does what you need like the linked answer says. I don't think anyone will fix your code since your example won't compile as it is... Maybe show your work with gtk_widget_translate_coordinates(), someone can probably tell what's going wrong.

    – jku
    Mar 23 at 9:19














0












0








0








I develop in C a GTK graphical user interface consisting in a main window embedding a GtkImage widget.
I cannot retrieve the correct mouse pointer coordinates with respect to the GtkImage widget inside the click event handler when clicking on my GtkImage widget.



I tried both solutions given by the link given below but none of them work:
How do you find the absolute position of a GTK widget in a window?



See below my code :



static void activate(GtkApplication *app, gpointer user_data)

GtkWidget *window;
GtkWidget *grid;

id.rows = ROWS;
id.cols = COLS;
id.stride = COLS * BYTES_PER_PIXEL;
id.stride += (4 - id.stride % 4) % 4; // ensure multiple of 4

// Allocate a bitmap
guchar *pixels = (guchar *)calloc(ROWS * id.stride, 1);
// Create a GDK Pix buffer from a pre-allocated pixel array
GdkPixbuf *pixelBuf = gdk_pixbuf_new_from_data(pixels,
GDK_COLORSPACE_RGB, // colorspace
0, // has_alpha
8, // bits-per-sample
COLS, ROWS, // cols, rows
id.stride, // rowstride
free_pixels, // destroy_fn
NULL // destroy_fn_data
);

// Create theGtkImage widget
GtkImage *image = GTK_IMAGE(gtk_image_new_from_pixbuf(pixelBuf));;
// Create the main win
window = gtk_application_window_new(app);
// Create a grid to automatically place the child widgets
grid = gtk_grid_new();
// Stick this grid to the main window
gtk_container_add(GTK_CONTAINER(window), grid);
// Create an event box to catch mouse event
GtkWidget *event_box = gtk_event_box_new();
// gtk image does'nt handle events, => attach it the event box
gtk_container_add(GTK_CONTAINER(event_box), GTK_WIDGET(image));
// Insert the event box into the grid
gtk_grid_attach(GTK_GRID(grid), GTK_WIDGET(event_box), 0, 0, 1, 1);
// Connect mouse event to the event box
g_signal_connect(GTK_WIDGET(event_box), "button-press-event", G_CALLBACK(on_mouse_press),
pixValEntry);
// LEt's start the show !
gtk_widget_show_all(window);



static gboolean on_mouse_press(GtkWidget *widget, GdkEvent *event,
gpointer user_data)

GdkEventButton *mouse_click = (GdkEventButton *)event;
char str[20];

(void)widget;
(void)user_data;

cout << gtk_widget_get_name(widget) << endl;

if (mouse_click->type == GDK_BUTTON_PRESS)

cout << "mouse_click->x = " << mouse_click->x << endl;
cout << "mouse_click->y = " << mouse_click->y << endl;




(x,y) coordinates shown in the mouse click event handler are shifted and depend on the main window size.
I do not find the way to move from the actual reference system to GtkImage coordinate system.



Many thanks for your help and hints.



Cheers










share|improve this question
















I develop in C a GTK graphical user interface consisting in a main window embedding a GtkImage widget.
I cannot retrieve the correct mouse pointer coordinates with respect to the GtkImage widget inside the click event handler when clicking on my GtkImage widget.



I tried both solutions given by the link given below but none of them work:
How do you find the absolute position of a GTK widget in a window?



See below my code :



static void activate(GtkApplication *app, gpointer user_data)

GtkWidget *window;
GtkWidget *grid;

id.rows = ROWS;
id.cols = COLS;
id.stride = COLS * BYTES_PER_PIXEL;
id.stride += (4 - id.stride % 4) % 4; // ensure multiple of 4

// Allocate a bitmap
guchar *pixels = (guchar *)calloc(ROWS * id.stride, 1);
// Create a GDK Pix buffer from a pre-allocated pixel array
GdkPixbuf *pixelBuf = gdk_pixbuf_new_from_data(pixels,
GDK_COLORSPACE_RGB, // colorspace
0, // has_alpha
8, // bits-per-sample
COLS, ROWS, // cols, rows
id.stride, // rowstride
free_pixels, // destroy_fn
NULL // destroy_fn_data
);

// Create theGtkImage widget
GtkImage *image = GTK_IMAGE(gtk_image_new_from_pixbuf(pixelBuf));;
// Create the main win
window = gtk_application_window_new(app);
// Create a grid to automatically place the child widgets
grid = gtk_grid_new();
// Stick this grid to the main window
gtk_container_add(GTK_CONTAINER(window), grid);
// Create an event box to catch mouse event
GtkWidget *event_box = gtk_event_box_new();
// gtk image does'nt handle events, => attach it the event box
gtk_container_add(GTK_CONTAINER(event_box), GTK_WIDGET(image));
// Insert the event box into the grid
gtk_grid_attach(GTK_GRID(grid), GTK_WIDGET(event_box), 0, 0, 1, 1);
// Connect mouse event to the event box
g_signal_connect(GTK_WIDGET(event_box), "button-press-event", G_CALLBACK(on_mouse_press),
pixValEntry);
// LEt's start the show !
gtk_widget_show_all(window);



static gboolean on_mouse_press(GtkWidget *widget, GdkEvent *event,
gpointer user_data)

GdkEventButton *mouse_click = (GdkEventButton *)event;
char str[20];

(void)widget;
(void)user_data;

cout << gtk_widget_get_name(widget) << endl;

if (mouse_click->type == GDK_BUTTON_PRESS)

cout << "mouse_click->x = " << mouse_click->x << endl;
cout << "mouse_click->y = " << mouse_click->y << endl;




(x,y) coordinates shown in the mouse click event handler are shifted and depend on the main window size.
I do not find the way to move from the actual reference system to GtkImage coordinate system.



Many thanks for your help and hints.



Cheers







c position gtk mouse






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 20:10







sylwa37

















asked Mar 22 at 19:17









sylwa37sylwa37

155




155












  • gtk_widget_translate_coordinates() does what you need like the linked answer says. I don't think anyone will fix your code since your example won't compile as it is... Maybe show your work with gtk_widget_translate_coordinates(), someone can probably tell what's going wrong.

    – jku
    Mar 23 at 9:19


















  • gtk_widget_translate_coordinates() does what you need like the linked answer says. I don't think anyone will fix your code since your example won't compile as it is... Maybe show your work with gtk_widget_translate_coordinates(), someone can probably tell what's going wrong.

    – jku
    Mar 23 at 9:19

















gtk_widget_translate_coordinates() does what you need like the linked answer says. I don't think anyone will fix your code since your example won't compile as it is... Maybe show your work with gtk_widget_translate_coordinates(), someone can probably tell what's going wrong.

– jku
Mar 23 at 9:19






gtk_widget_translate_coordinates() does what you need like the linked answer says. I don't think anyone will fix your code since your example won't compile as it is... Maybe show your work with gtk_widget_translate_coordinates(), someone can probably tell what's going wrong.

– jku
Mar 23 at 9:19













0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306450%2fhow-to-get-mouse-pointer-position-with-respect-to-a-gtkimage%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306450%2fhow-to-get-mouse-pointer-position-with-respect-to-a-gtkimage%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript