On Wed, 2010-08-25 at 13:40 +0200, Rampage wrote:
> Hello everyone :)
> Does anyone have any sort of tutorial/example on how to implement 
> context menus? like: right click on an item of a treeview and i get 
> access to functions that are related to that specific item?
> the simpliest the better, since i'm at the beginning with gtk# and gtk 
> in general, i'm looking for something that even a monkey can understand :D

In your NodeView / TreeView  (this example is from a NodeView)
##########
  [GLib.ConnectBefore]          
  public void OnButonPressEvent(object _sender, 
                                ButtonPressEventArgs _args)

    ITreeNode           node;
    Enterprise          enterprise;

    if (PopupEnabled)
    {
      /* Get the selected list node from the nodeview
         and bail out if result is not a EnterpriseListNode */
      node = (_sender as NodeView).NodeSelection.SelectedNode;
      if (node is EnterpriseListNode)
        enterprise = ((EnterpriseListNode)node).Enterprise;
      else return;
      /* Is the click a right-button */
     if (_args.Event.Button == 3) 
     {
       PopupMenuBuilder  menu;
       menu.Append("Edit", enterprise, 
                   newEventHandler (EnterpriseEdit), 
                   (OnEnterpriseEdit != null));
   ....
      menu.Run();
  }

##########
I cheat a little bit by having my own PopupMenuBuilder class; mostly
because the delegate needs the item represented by the node/row that was
selected in the view.

-- 
Adam Tauno Williams <awill...@whitemice.org> LPIC-1, Novell CLA
<http://www.whitemiceconsulting.com>
OpenGroupware, Cyrus IMAPd, Postfix, OpenLDAP, Samba
/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awill...@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using Whitemice.ZOGI;
using Gtk;

namespace Whitemice.ZOGI.GtkWidgets
{  
  public class PopupMenuBuilder
  {   
    protected Menu          menu;
    
    public PopupMenuBuilder()
    {
      menu = new Menu ();
    }
    
    public void Append(string text, Entity context, EventHandler handler, bool sensitivity)
    {
      PopUpMenuItem item;
      
      item = new PopUpMenuItem (context, text);
      item.Sensitive = sensitivity;
      item.Activated += handler;
      menu.Append(item);
    }
    
    public void AppendSeperator()
    {
      menu.Append(new SeparatorMenuItem ());
    }

    public void AppendSeperatorIf(bool condition)
    {
      if (condition)
        menu.Append(new SeparatorMenuItem ());
    }
    
    public int Count
    {
      get { return menu.Children.Length; }
    }
    
    public Gtk.Menu Menu
    {
      get
      {
        return menu;
      }
    }
    
    public void Run()
    {
	  	menu.Popup (null, null, null, 3, Gtk.Global.CurrentEventTime);
      menu.ShowAll();    
    }
  }
}
_______________________________________________
Gtk-sharp-list maillist  -  Gtk-sharp-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/gtk-sharp-list

Reply via email to