# How to trap mouse middle button clicks on a Histogram in a TCanvas

**URL:** <https://root-forum.cern.ch/t/how-to-trap-mouse-middle-button-clicks-on-a-histogram-in-a-tcanvas/19456>\
**Category:** ROOT\
**Created:** [July 1, 2015, 1:05pm UTC](https://root-forum.cern.ch/t/how-to-trap-mouse-middle-button-clicks-on-a-histogram-in-a-tcanvas/19456 "2015-07-01T13:05:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abyroot2014](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7ab992/32.png) [@Abyroot2014](https://root-forum.cern.ch/u/Abyroot2014)\
**Post date:** [July 1, 2015, 1:05pm UTC](https://root-forum.cern.ch/t/how-to-trap-mouse-middle-button-clicks-on-a-histogram-in-a-tcanvas/19456/1 "2015-07-01T13:05:31Z")

</div>

TRAPPING MOUSE MIDDLE BUTTON CLICKS ON A HISTOGRAM IN A TCanvas

I have a window in which I have a TCanvas embedded in a TRootEmbeddedCanvas.  
I then draw a histogram using TH1I in the TCanvas.  
Now my software is a standalone executable using ROOT libraries and  
also has the following function where the window messages are processed :  
Bool\_t HistWin::ProcessMessage (Long\_t msg, Long\_t parm1, Long\_t parm2)  
Now I want to trap the mouse middle button clicks made on the histogram  
and want to capture the index i.e the x-axis value of the point and  
print the corresponding y-axis value.  
I had a look at the files :  
1.root/tutorials/gui/exec3.c  
2.root/tutorials/hist/exec2.c  
3.root/tutorials/hist/exec1.c  
I could not locate the corresponding demo file. ![#-o](https://root-forum.cern.ch/uploads/default/original/1X/378b417f7f25e635458e14f511257ba15acc7a37.gif "d'oh!")  
I tried to understand it to incorporate the code in my program without much progress. ![#-o](https://root-forum.cern.ch/uploads/default/original/1X/378b417f7f25e635458e14f511257ba15acc7a37.gif "d'oh!")  
I guess I need help to make progress. 😕

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 2, 2015, 7:35am UTC](https://root-forum.cern.ch/t/how-to-trap-mouse-middle-button-clicks-on-a-histogram-in-a-tcanvas/19456/2 "2015-07-02T07:35:30Z")

</div>

See:

> [@gPad-\>GetEventX() to axis range](https://root-forum.cern.ch/t/gpad-geteventx-to-axis-range/9838/2):
>
> Have a look at $ROOTSYS/tutorials/graphics/mandelbrot.C

> [@How too find out which bin was clicked in a TH2D histogram](https://root-forum.cern.ch/t/how-too-find-out-which-bin-was-clicked-in-a-th2d-histogram/7012/2):
>
> Hi, You can take a look at these tutorials: graphics/triangles.C, hist/exec2.C, hist/DynamicSlice.C and graphics/mandelbrot.C Cheers, Bertrand.

> [@Mandelbrot macro](https://root-forum.cern.ch/t/mandelbrot-macro/4520/1):
>
> Dear all, while preparing a small ROOT course, I have written the following macro, that draws the fractal Mandelbrot set, and allows for zoom/unzoom operations. Runs with root mandelbrot.C+ I attach the macro and a screenhsot, just in case someone is interested (maybe a new ROOT tutorial on TExec/TComplex ? [8)] ) Bye Luigi [mandelbrot.C](https://root-forum.cern.ch/uploads/default/original/2X/8/8a1d3d612e8ad03cebed5a7931fcee757e2a8dfa.C) (3.24 KB)

---

<div class="post-metadata">

**Author:** ![Abyroot2014](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7ab992/32.png) [@Abyroot2014](https://root-forum.cern.ch/u/Abyroot2014)\
**Post date:** [July 20, 2015, 12:00pm UTC](https://root-forum.cern.ch/t/how-to-trap-mouse-middle-button-clicks-on-a-histogram-in-a-tcanvas/19456/3 "2015-07-20T12:00:22Z")

</div>

Hi, I made some progress as can be seen by running attached code.  
BUT now a new problem : after closing the histogram window if i re-open it again and then if I CLICK the middle button then the following error messages appear :

Error in : BadDrawable (invalid Pixmap or Window parameter) (TGCompositeFrame XID: 71303530, XREQ: 62)  
TGCompositeFrame: 71303530  
TGViewPort: 71303521  
TRootEmbeddedCanvas: 71303520  
TGVerticalFrame: 71303519  
TGCompositeFrame: 71303518  
HistWin: 71303564  
Error in : BadWindow (invalid Window parameter) (TGCompositeFrame XID: 71303530, XREQ: 2)  
TGCompositeFrame: 71303530  
TGViewPort: 71303521  
TRootEmbeddedCanvas: 71303520  
TGVerticalFrame: 71303519  
TGCompositeFrame: 71303518  
…  
To compile attached code do as follows:

1. make clean
2. make dict
3. make
4. ./nscope to run the code

Kindly teach how to debug such error messages , can gdb be used ? or any other ? like ddd ?  
or any other debugging method kindly suggest so I can debug on my own.  
Help and guidance greatly appreciated. THANKS VERY MUCH.  
[nscopeIJuly2015.tar.gz](https://root-forum.cern.ch/uploads/default/original/2X/4/4a4f77bd40b42fa2f26b5139e7043b8e917fad90.gz) (16.5 KB)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 20, 2015, 2:13pm UTC](https://root-forum.cern.ch/t/how-to-trap-mouse-middle-button-clicks-on-a-histogram-in-a-tcanvas/19456/4 "2015-07-20T14:13:15Z")

</div>

Hi,

You should add:` delete fRootEmbeddedCanvas799;`in the destructor (TRootEmbeddedCanvas is not properly deleted by the Cleaup() method).  
And you can use gdb, or your preferred debugger to debug your application…

Cheers, Bertrand.

---

<div class="post-metadata">

**Author:** ![Abyroot2014](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7ab992/32.png) [@Abyroot2014](https://root-forum.cern.ch/u/Abyroot2014)\
**Post date:** [July 21, 2015, 5:22am UTC](https://root-forum.cern.ch/t/how-to-trap-mouse-middle-button-clicks-on-a-histogram-in-a-tcanvas/19456/5 "2015-07-21T05:22:51Z")

</div>

Hi,  
The code was still crashing sometimes when the HistWin window was closed.  
So I added the line as follows and now it is much more stable:  
…  
In Destructor  
…  
if (fRootEmbeddedCanvas799)  
delete fRootEmbeddedCanvas799;  
}

Thank you Bertrand for solving my problem.  
Cheers, Abraham
