# Open a context menu with actions in TextView widget

**URL:** <https://www.ruby-forum.com/t/open-a-context-menu-with-actions-in-textview-widget/246058>\
**Category:** Ruby-Gnome 2\
**Created:** [August 24, 2013, 2:30am UTC](https://www.ruby-forum.com/t/open-a-context-menu-with-actions-in-textview-widget/246058 "2013-08-24T02:30:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [August 24, 2013, 2:30am UTC](https://www.ruby-forum.com/t/open-a-context-menu-with-actions-in-textview-widget/246058/1 "2013-08-24T02:30:37Z")

</div>

Hi.

I have a question.

I have a window with three things - ScrolledWindow, TextBuffer and  
TextView.

I can select text just fine with my mouse.

Now I would like to press right mouse click on selection and popup a  
small context menu, with entries that I wish to define on my own. I.e.  
increase font size would be nice for the selection.

Does one of you have an idea how to get such a popup widget on right  
click?

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [August 24, 2013, 8:06am UTC](https://www.ruby-forum.com/t/open-a-context-menu-with-actions-in-textview-widget/246058/2 "2013-08-24T08:06:43Z")

</div>

Subject: [ruby-gnome2-devel-en] Open a context menu with actions in  
TextView widget  
Date: sab 24 ago 13 02:30:37 +0200

Quoting Marc H. ([removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)):

> click?  
> You can trap signals for your textview: signal ‘button-press-event’ is  
> emitted when a mouse button is pressed, and ‘button-release-event’ is  
> emitted when the button is released.

So, if you have something like this:

ebfr=Gtk::TextBuffer::new  
ebfr.text=\<\<-EOF  
…  
…  
…  
EOF

et=Gtk::TextView::new(ebfr)

you can add this:

et.signal\_connect(‘button-press-event’) do |tv,eb|  
…  
…  
end  
et.signal\_connect(‘button-release-event’) do |tv,eb|  
…  
…  
end

This specific processing only takes place when the focus of the mouse  
is within the textview.

The second parameter you receive when the signal is triggered is an  
EventButton instance. Check if eb.button==3, and do whatever you need  
to do.

Remember that the return value from the signal\_connect block has  
meaning. If you return true, it means you have made use of the signal,  
and it does not need further processing. If you return false, the  
signal is passed downwards.

So, if you want to do something on release of the third mouse button,  
first avoid the window manager processing of the click as follows:

et.signal\_connect(‘button-press-event’) do |tv,eb|  
eb.button==3 ? true : false  
end

Then do what you need to do at release

et.signal\_connect(‘button-release-event’) do |tv,eb|  
return false unless(eb.button==3)  
do\_what\_you\_need()  
true  
end

…  
…

def do\_what\_you\_need  
dialog=Gtk::Dialog::new(:title=\>‘my  
title’,:flags=\>Gtk::Dialog:🎏:MODAL)  
…  
…  
end

Hope this helps

Carlo

–

- 

```
    Se la Strada e la sua Virtu' non fossero state messe da 

```

parte,

- K \* Carlo E. Prelz - [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) che bisogno ci  
sarebbe
  - 

```
          di parlare tanto di amore e di rettitudine? 

```

(Chuang-Tzu)
