XFCE-PANEL-PLUGIN development using Python/Javascript – Part 2

0
3 min read

As we already discussed that we will be moving forward with GtkSocket and GtkPlug approach, so I will try to give you an idea how can you use this approach to develop xfce panel plugin.

Previous Part Blog link: XFCE-PANEL-PLUGIN development using Python/Javascript – Part 1

GtkSocket gives the capacity to insert widget from one process into another process in a such a manner that is straightforward to the user. One process create a GtkSocket gadget and afterward we passes that widget’s window ID to the other process, which then, at that point makes a GtkPlug with that window ID. Any widget contained in the GtkPlug then, at that point will show up inside the primary application’s window.

GtkSocket in C

#include <gtk/gtk.h>
#include <gtk/gtkx.h>
#include <stdio.h>
#include <stdlib.h>

int main(int ac, char **av)
{
    GtkWidget *window;
    GtkWidget *socket;

    gtk_init(&ac, &av);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    socket = gtk_socket_new();
    gtk_window_set_title(window, "some socket");
    gtk_window_set_position(window, GTK_WIN_POS_CENTER);

    gtk_container_add(window, socket);// added socket into window
    g_signal_connect(GTK_WIDGET(window), "destroy", gtk_main_quit, NULL);

    printf("socket ID: %d\n", gtk_socket_get_id(socket));

    char buffer[1024];
    sprintf(buffer, "python3 ./plug.py %d &", gtk_socket_get_id(socket));
    system(buffer);

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

In the above example, we have created one GtkWindow, then we have created new socket through gtk_socket_new(), through gtk_container_add we have added socket into window.

gtk_socket_get_id() will return int socket id, which will be passed to plug.py (Python file containing GtkPlug code) through sprintf.

GtkPlug in Python

#!/bin/python3
# -*- utf:8 -*-

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import sys


socket_id = sys.argv[1]
plug = Gtk.Plug.new(int(socket_id))

plug.connect('destroy', Gtk.main_quit)


class SocketDialogWindow(Gtk.Window):
    def __init__(self):
        box = Gtk.Box(spacing=6)
     
        button1 = Gtk.Button(label="Information")
        button1.connect("clicked", self.on_info_clicked)
        box.add(button1)

        plug.add(box) # box widget added to python plug
        plug.show_all()

    def on_info_clicked(self, widget):
        dialog = Gtk.MessageDialog(
            transient_for=self,
            flags=0,
            message_type=Gtk.MessageType.INFO,
            buttons=Gtk.ButtonsType.OK,
            text="Info about Python Sample Plugin",
        )
        dialog.format_secondary_text(
            "Copyright \xc2\xa9 2006-2019 Xfce development team\n"
        )
        dialog.run()
        print("INFO dialog closed")

        dialog.destroy()


SocketDialogWindow()

sys.argv is used to get argument through which socket id will be received, In this plug example, we have added one button to box widget, later on which is added into plug.

So to test out above example, we simply have to run GtkSocket file in C.

I hope so you would have better idea now, how does GtkSocket in C and GtkPlug in python work together.

Till now we have not registered our plugin or does any type of installation into xfce-panel, Further part of this blog will include further information , till that time Happy Coding 🙂

Choose your Reaction!
Leave a Comment