[Vala] Question about a functions and GTK.



Is this functions correct from GTK & Vala side (not algo, but internal
knowlege)? Or they are need to be adjusted to work correctly without
memleaks?

    private static void apply_menu_dnd(Gtk.MenuItem item, MenuModel
section, int model_item)
    {
        // Make the this widget a DnD source.
        Gtk.drag_source_set (
                item,                      // widget will be drag-able
                Gdk.ModifierType.BUTTON1_MASK, // modifier that will start
a drag
                MENU_TARGETS,               // lists of target to support
                Gdk.DragAction.COPY            // what to do with data
after dropped
            );
        var data = new DragData(item,section,model_item);
        data.ref();
        item.drag_begin.connect(data.begin);
        item.drag_data_get.connect(data.get);
        Signal.connect(item,"destroy",(GLib.Callback)DragData.destroy,data);
    }
    public static void apply_menu_properties(List<unowned Widget> w,
MenuModel menu)
    {
        unowned List<unowned Widget> l = w;
        for(var i = 0; i < menu.get_n_items(); i++)
        {
            var jumplen = 1;
            if (l.data is SeparatorMenuItem) l = l.next;
            unowned Gtk.MenuItem shell = l.data as Gtk.MenuItem;
            unowned string? str = null;
            var has_section = false;
            var has_submenu = false;
            unowned MenuShell menuw = shell.submenu;
            MenuLinkIter iter = menu.iterate_item_links(i);
            MenuModel? link_menu;
            while (iter.get_next(out str, out link_menu))
            {
                has_section = has_section || (str ==
GLib.Menu.LINK_SECTION);
                has_submenu = has_submenu || (str ==
GLib.Menu.LINK_SUBMENU);
                if (menuw != null && has_submenu)
                    apply_menu_properties(menuw.get_children(),link_menu);
                else if (has_section)
                {
                    jumplen += (link_menu.get_n_items() - 1);
                    apply_menu_properties(l,link_menu);
                }
            }
            Variant? val = null;
            MenuAttributeIter attr_iter = menu.iterate_item_attributes(i);
            while(attr_iter.get_next(out str,out val))
            {
                if (str == GLib.Menu.ATTRIBUTE_ICON && (has_submenu ||
has_section))
                {
                    var icon = Icon.deserialize(val);
                    shell.set("icon",icon);
                }
                if (str == ATTRIBUTE_TOOLTIP)
                    shell.set_tooltip_text(val.get_string());
                if (str == ATTRIBUTE_DND_SOURCE && val.get_boolean())
                    apply_menu_dnd(l.data as Gtk.MenuItem, menu, i);
            }
            l = l.nth(jumplen);
            if (l == null) break;
        }
    }

DragData class here:
    [Compact,CCode (ref_function = "menu_maker_drag_data_ref",
unref_function = "menu_maker_drag_data_unref")]
    private class DragData
    {
        internal unowned MenuModel section;
        internal unowned Gtk.MenuItem menuitem;
        internal int item_pos;
        internal Volatile ref_count;
        internal static void destroy (Widget w, DragData data)
        {
            SignalHandler.disconnect_by_data(data.menuitem,data);
            Gtk.drag_source_unset(data.menuitem);
            data.ref_count = 1;
            data.unref();
        }
        internal DragData(Gtk.MenuItem item, MenuModel section, int
model_item)
        {
            this.section = section;
            this.menuitem = item;
            item_pos = model_item;
            this.ref_count = 1;
        }
        internal void @get (Gdk.DragContext context, SelectionData data,
uint info, uint time_)
        {
            string[]? uri_list = null;
            string action,target;

section.get_item_attribute(item_pos,GLib.Menu.ATTRIBUTE_ACTION,"s",out
action);

section.get_item_attribute(item_pos,GLib.Menu.ATTRIBUTE_TARGET,"s",out
target);
            if (action == "app.launch-id")
            {
                try
                {
                    var appinfo = new DesktopAppInfo(target);
                    target = Filename.to_uri(appinfo.get_filename());
                } catch (GLib.Error e){}
            }
            uri_list = new string[1];
            uri_list[0] = target;
            data.set_uris(uri_list);
        }
        internal void begin(Gdk.DragContext context)
        {
            var val =
section.get_item_attribute_value(item_pos,GLib.Menu.ATTRIBUTE_ICON,null);
            var icon = Icon.deserialize(val);
            if (icon != null)
                Gtk.drag_source_set_icon_gicon(menuitem,icon);
            else

Gtk.drag_source_set_icon_name(menuitem,"system-run-symbolic");
        }
        internal unowned DragData @ref ()
        {
            GLib.AtomicInt.inc (ref this.ref_count);
            return this;
        }
        internal void unref ()
        {
            if (GLib.AtomicInt.dec_and_test (ref this.ref_count))
                this.free ();
        }
        private extern void free ();
    }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]