GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Nov 01, 2014 1:56 am

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: how to activate submenu in popup
PostPosted: Mon Nov 28, 2011 10:20 pm 
Offline
GTK+ Geek

Joined: Mon Jan 05, 2009 9:37 am
Posts: 68
Location: Austria
Hi All,

I have created a popup menu in glade, and it is shown when I use the right mouse button on an image. The callback is executed properly, unless I try to do the same with a submenu. Here is the relevant part of my glade file
Code:
<child>
      <object class="GtkMenuItem" id="menuitem8">
        <property name="visible">True</property>   
        <property name="can_focus">False</property>
        <property name="tooltip_text" translatable="yes">Change colour of selection to green</property>
        <property name="use_action_appearance">False</property> 
        <property name="label" translatable="yes">Blue</property>
        <property name="use_underline">True</property>
        <signal name="activate" handler="on_popup_draw_activate" swapped="no"/>
      </object>
    </child>   
    <child>
      <object class="GtkMenuItem" id="menuitem9">
        <property name="visible">True</property>   
        <property name="can_focus">False</property>
        <property name="tooltip_text" translatable="yes">Change colour of selection to green</property>
        <property name="use_action_appearance">False</property>
        <property name="label" translatable="yes">Custom colour</property>
        <property name="use_underline">True</property>
        <signal name="activate" handler="on_popup_draw_activate" swapped="no"/>
      </object>
    </child>   
    <child>
      <object class="GtkMenuItem" id="menuitem10">
        <property name="visible">True</property>   
        <property name="can_focus">False</property>
        <property name="tooltip_text" translatable="yes">Set the colour of selection</property>
        <property name="use_action_appearance">False</property>
        <property name="label" translatable="yes">Colour</property>
        <property name="use_underline">True</property>
        <child type="submenu">
          <object class="GtkMenu" id="menu4">
            <property name="visible">True</property>   
            <property name="can_focus">False</property> 
            <property name="ubuntu_local">True</property>
            <child>
              <object class="GtkMenuItem" id="menuitem13">
                <property name="visible">True</property>   
                <property name="can_focus">False</property>
                <property name="use_action_appearance">False</property>
               <property name="label" translatable="yes">colour1</property>
                <property name="use_underline">True</property>
                <signal name="activate" handler="on_popup_draw_activate" swapped="no"/>
              </object>
            </child>   
          </object>   
        </child>       
      </object>       
    </child>

So, the first item is called 'Blue', and is attached to on_popup_draw_activate. After that I have menuitem9 (Custom colour),and then menuitem10 (Colour), which has a submenu, menu4, and that has menuitem13 (colour1). All are attached to the same callback function. Yet, when I click on 'Blue', the callback function is executed, while nothing happens, when I click on 'colour1'. I have also attached the select signal to colour1 (not shown in the code above), and when I select the item, the callback is executed again. So, it seems that I have problems with the activate signal only. Is this a bug, or there is something else that I am supposed to do?
Cheers,
Zoltán


Top
 Profile  
 
 Post subject: Re: how to activate submenu in popup
PostPosted: Tue Nov 29, 2011 12:07 am 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
Can you post the code of on_popup_draw_activate?

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


Top
 Profile  
 
 Post subject: Re: how to activate submenu in popup
PostPosted: Tue Nov 29, 2011 8:06 am 
Offline
GTK+ Geek

Joined: Mon Jan 05, 2009 9:37 am
Posts: 68
Location: Austria
Joel wrote:
Can you post the code of on_popup_draw_activate?

Sure. Here it is in a short version:

Code:
def on_popup_draw_activate(self, menu):
      print 'here'
      text = menu.get_label()
      print text
      return True

and in a long one
Code:
   
def on_popup_draw_activate(self, menu):
      print 'here'
      text = menu.get_label()
      print text
      contained = fujitsu.find_in_rectange(self.pages[self.page],
                           self.drag[0], self.drag[1], self.drag[2], self.drag[3])
      self.drag = []
      if text in ['Red', 'Green', 'Blue']:
         if text == 'Red':
            new_colour = [0.85, 0, 0, 0.9]
         if text == 'Green':
            new_colour = [0, 0.7, 0, 0.9]
         if text == 'Blue':
            new_colour = [0, 0, 0.9, 0.9]
               
         for i in range(len(self.pages[self.page].pagecontent)):
            if contained[i]:
               self.pages[self.page].pagecontent[i].colour = new_colour
      
      if text == 'Cut':
         pagecontent = []
         for i in range(len(self.pages[self.page].pagecontent)):
            if not contained[i]:
               pagecontent.append(self.pages[self.page].pagecontent[i])      
         self.pages[self.page].pagecontent = pagecontent
      
      if text == 'Thicken':
         new_width = 1.5
      if text == 'Thin':
         new_width = 1.0 / 1.5         
      if text in ['Thicken', 'Thin']:
         for i in range(len(self.pages[self.page].pagecontent)):
            if contained[i]:
               self.pages[self.page].pagecontent[i].thickness *= new_width
               
      self.draw_note(self.widget('draw'), self.surface, self.pages[self.page])
      self.draw_note(self.previews[self.page].da, self.previews[self.page].surface, self.pages[self.page])
      return True

With the submenu, 'here' is not printed.
Cheers,
Zoltán


Top
 Profile  
 
 Post subject: Re: how to activate submenu in popup
PostPosted: Tue Nov 29, 2011 8:51 pm 
Offline
GTK+ Geek

Joined: Mon Jan 05, 2009 9:37 am
Posts: 68
Location: Austria
Here is a minimal example, perhaps it is more useful. First, the glade file
Code:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkMenu" id="menu1">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="ubuntu_local">True</property>
    <child>
      <object class="GtkMenuItem" id="menuitem1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="use_action_appearance">False</property>
        <property name="label" translatable="yes">menuitem1</property>
        <property name="use_underline">True</property>
        <signal name="activate" handler="on_activate" swapped="no"/>
      </object>
    </child>
    <child>
      <object class="GtkMenuItem" id="menuitem2">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="use_action_appearance">False</property>
        <property name="label" translatable="yes">menuitem2</property>
        <property name="use_underline">True</property>
        <child type="submenu">
          <object class="GtkMenu" id="menu2">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="ubuntu_local">True</property>
            <child>
              <object class="GtkMenuItem" id="menuitem3">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="use_action_appearance">False</property>
                <property name="label" translatable="yes">menuitem3</property>
                <property name="use_underline">True</property>
                <signal name="activate" handler="on_activate" swapped="no"/>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <property name="has_resize_grip">False</property>
    <signal name="button-release-event" handler="on_press" swapped="no"/>
    <child>
      <placeholder/>
    </child>
  </object>
</interface>

and then the python code
Code:
#!/usr/bin/python
from gi.repository import Gtk, GObject, Gdk

class T:
   def __init__(self):
      self.builder = Gtk.Builder()
      self.builder.add_from_file('t2.glade')
      self.window = self.builder.get_object('window1')
      self.popup = self.builder.get_object('menu1')
      self.builder.connect_signals(self)
      self.window.set_events(self.window.get_events()
           | Gdk.EventMask.BUTTON_RELEASE_MASK)

   def on_activate(self, menu):
      print 'here'
      return True
   
   def on_press(self, widget, event):
      self.popup.popup(None, None, None, None, 3, event.time)
      return True
      
   pass

if __name__=='__main__':
   app = T()
   app.window.show()
   Gtk.main()

The observations are the same: on menuitem1, 'here' is printed, while on menuitem3, nothing happens. On the other hand, if I use the right mouse button to activate the item, then it prints 'here'. But it is not consistent with the behaviour of menuitem1, which is activated on either the left, or the right mouse click. Utterly weird...
Cheers,
Zoltán


Top
 Profile  
 
 Post subject: Re: how to activate submenu in popup
PostPosted: Wed Nov 30, 2011 1:36 am 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
From your glade file code:
Code:
<!-- interface-requires gtk+ 3.0 -->

Are you using python-gtk2?
//edit, I'm not python fan, but..this works for me:
gui.ui
Code:
<?xml version="1.0"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="window1">
    <signal name="destroy" handler="on_window1_destroy"/>
    <child>
      <object class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkMenuBar" id="menubar1">
            <property name="visible">True</property>
            <child>
              <object class="GtkMenuItem" id="menuitem1">
                <property name="visible">True</property>
                <property name="label" translatable="yes">_Archivo</property>
                <property name="use_underline">True</property>
                <signal name="activate" handler="on_activate"/>
                <child type="submenu">
                  <object class="GtkMenu" id="menu1">
                    <property name="visible">True</property>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem1">
                        <property name="label">gtk-new</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_activate"/>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem2">
                        <property name="label">gtk-open</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_activate"/>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem3">
                        <property name="label">gtk-save</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_activate"/>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem4">
                        <property name="label">gtk-save-as</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_activate"/>
                      </object>
                    </child>
                    <child>
                      <object class="GtkSeparatorMenuItem" id="separatormenuitem1">
                        <property name="visible">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem5">
                        <property name="label">gtk-quit</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_activate"/>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="menuitem2">
                <property name="visible">True</property>
                <property name="label" translatable="yes">_Editar</property>
                <property name="use_underline">True</property>
                <signal name="activate" handler="on_activate"/>
                <child type="submenu">
                  <object class="GtkMenu" id="menu2">
                    <property name="visible">True</property>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem6">
                        <property name="label">gtk-cut</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_activate"/>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem7">
                        <property name="label">gtk-copy</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_activate"/>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem8">
                        <property name="label">gtk-paste</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_activate"/>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem9">
                        <property name="label">gtk-delete</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_activate"/>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="menuitem3">
                <property name="visible">True</property>
                <property name="label" translatable="yes">_Ver</property>
                <property name="use_underline">True</property>
                <signal name="activate" handler="on_activate"/>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="menuitem4">
                <property name="visible">True</property>
                <property name="label" translatable="yes">Ay_uda</property>
                <property name="use_underline">True</property>
                <signal name="activate" handler="on_activate"/>
                <child type="submenu">
                  <object class="GtkMenu" id="menu3">
                    <property name="visible">True</property>
                    <child>
                      <object class="GtkImageMenuItem" id="imagemenuitem10">
                        <property name="label">gtk-about</property>
                        <property name="visible">True</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_activate"/>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
    </child>
  </object>
</interface>

gtk2.py
Code:
#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk

class T(object):
   def __init__(self):
      super(T, self).__init__()
      self.builder = gtk.Builder()
      self.builder.add_from_file('gui.ui')
      self.window = self.builder.get_object('window1')
      self.builder.connect_signals(self)
      self.builder = 0
      self.window.show_all()
      gtk.main()
      
   def on_activate(self, menu):
      print 'You are here: ' + menu.get_label();
   
   def on_window1_destroy(self, object):
      gtk.main_quit()

if __name__=='__main__':
   app = T()

AS you can see, every click on any menu item, displays the "here" text.

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


Top
 Profile  
 
 Post subject: Re: how to activate submenu in popup
PostPosted: Wed Nov 30, 2011 9:16 am 
Offline
GTK+ Geek

Joined: Mon Jan 05, 2009 9:37 am
Posts: 68
Location: Austria
Joel wrote:
From your glade file code:
Code:
<!-- interface-requires gtk+ 3.0 -->

Are you using python-gtk2?

No, I am using gi, as indicated in the import statement of my code. The reason for that is that the newest version of glade no longer supports pygtk, and I did not want to fix my UI file by hand every time I change something glade.

I have tried your example, but it does not address the problem in the OP, or in my minimal example. I have no problems using the standard file menu that you also use. That works for me, even in my code. The problem arises when I try to do the same thing with a popup menu. What I would like to establish is whether this is a problem in gi, in which case I would raise the issue with the developers, or whether something is wrong in my code. Could you add a popup menu to your UI file, and try out that? (And you could yank the file menu, so that the UI file would be shorter.)
Cheers,
Zoltán


Top
 Profile  
 
 Post subject: Re: how to activate submenu in popup
PostPosted: Wed Nov 30, 2011 3:43 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
See the attachment, still works, even with popup.


Attachments:
python_gtk2.zip [1.3 KiB]
Downloaded 33 times

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.
Top
 Profile  
 
 Post subject: Re: how to activate submenu in popup
PostPosted: Wed Nov 30, 2011 6:36 pm 
Offline
GTK+ Geek

Joined: Mon Jan 05, 2009 9:37 am
Posts: 68
Location: Austria
Joel wrote:
See the attachment, still works, even with popup.

While your gui does not have a popup with a submenu, I have modified it, and it still works. However, you connected the button press signal to your popup, while in the original post I had the button release event. If you attach the button release signal, your gui does not work either.
I am not sure I could use the button press signal, because I drag a rectangle over the screen, and I want the popup to appear only at the end of the operation, which is when I release the button.
Cheers,
Zoltán


Top
 Profile  
 
 Post subject: Re: how to activate submenu in popup
PostPosted: Wed Nov 30, 2011 7:30 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
You can detect if the user is drawing rectangle, and later on the button release, if the rectangle was drawed, display the popup, sorry as I told you not python fan...

Good luck

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 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