Using the new way of writing lxpanel plugins (wiki was outdated)

This commit is contained in:
Fabio Manganiello 2022-01-25 16:18:18 +01:00
parent 5172a60c80
commit 1d3d7d84f7
3 changed files with 58 additions and 234 deletions

2
.gitignore vendored
View file

@ -67,10 +67,12 @@ depcomp
install-sh install-sh
missing missing
Makefile.in Makefile.in
Makefile.in.in
Makefile Makefile
intltool-extract.in intltool-extract.in
intltool-merge.in intltool-merge.in
intltool-update.in intltool-update.in
libtool libtool
.deps/ .deps/
.libs/

View file

@ -23,6 +23,9 @@ LXPANEL_DATADIR=$(pkg-config --variable=datadir lxpanel)/lxpanel
AC_SUBST(LXPANEL_LIBDIR) AC_SUBST(LXPANEL_LIBDIR)
AC_SUBST(LXPANEL_DATADIR) AC_SUBST(LXPANEL_DATADIR)
LXPANEL_CPPFLAGS=$(pkg-config --cflags --libs lxpanel)
AC_SUBST(LXPANEL_CPPFLAGS)
GETTEXT_PACKAGE=$PACKAGE GETTEXT_PACKAGE=$PACKAGE
AC_SUBST(GETTEXT_PACKAGE) AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE,"$GETTEXT_PACKAGE", [Gettext package.]) AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE,"$GETTEXT_PACKAGE", [Gettext package.])

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright (c) 2009 LxDE Developers, see the file AUTHORS for details. * Copyright (c) 2022 Fabio Manganiello, see the file AUTHORS for details.
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -18,242 +18,61 @@
* *
*/ */
#include <sys/types.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <glib/gi18n.h>
#include <string.h>
#include <lxpanel/plugin.h> #include <lxpanel/plugin.h>
typedef struct { #define LabelSize 32
unsigned int timer;
Plugin * plugin;
GtkWidget *main;
GtkWidget *widget;
GtkTooltips *tip;
gchar* text;
gint speed;
gint wait;
gint cnt;
gint color;
gboolean bool;
gchar* file;
gchar* file2;
} Example;
gint colors[] = GtkWidget *constructor(LXPanel *panel, config_setting_t *settings)
{ {
0xFF0000, /* panel is a pointer to the panel and
0x00FF00, settings is a pointer to the configuration data
0xFF00FF, since we don't use it, we'll make sure it doesn't
0xFFFF00 give us an error at compilation time */
}; (void)panel;
(void)settings;
gboolean button_press_event( GtkWidget *widget, GdkEventButton* event, Plugin* plugin)
{ // make a label out of the hostname
ENTER2; char cIdBuf[LabelSize+1] = {'\0'};
FILE *fp;
if( event->button == 1 ) /* left button */ fp = fopen("/etc/hostname", "r");
{ fgets(cIdBuf, LabelSize, fp);
/* write here */ fclose(fp);
}
else if( event->button == 2 ) /* middle button */ // create a label widget instance
{ GtkWidget *pLabel = gtk_label_new(cIdBuf);
/* write here */
} // set the label to be visible
else if( event->button == 3 ) /* right button */ gtk_widget_show(pLabel);
{
GtkMenu* popup = (GtkMenu*)lxpanel_get_panel_menu( plugin->panel, plugin, FALSE ); /* lxpanel_menu, can be reimplemented */ // need to create a container to be able to set a border
gtk_menu_popup( popup, NULL, NULL, NULL, NULL, event->button, event->time ); GtkWidget *p = gtk_event_box_new();
return TRUE;
} // our widget doesn't have a window...
// it is usually illegal to call gtk_widget_set_has_window() from application but for GtkEventBox it doesn't hurt
RET2(TRUE); gtk_widget_set_has_window(p, FALSE);
}
// set border width
gboolean scroll_event (GtkWidget *widget, GdkEventScroll *event, Plugin* plugin) gtk_container_set_border_width(GTK_CONTAINER(p), 1);
{
ENTER2; // add the label to the container
RET2(TRUE); gtk_container_add(GTK_CONTAINER(p), pLabel);
}
// set the size we want
static gint timer_event(Example *egz) // gtk_widget_set_size_request(p, 100, 25);
{
char buffer [60]; // success!!!
return p;
if(egz->bool) return TRUE; }
if(--egz->wait) return TRUE;
egz->wait = egz->speed; FM_DEFINE_MODULE(lxpanel_gtk, test)
egz->cnt++; /* Plugin descriptor. */
if( egz->cnt >= strlen(egz->text) ) egz->cnt = 0; LXPanelPluginInit fm_module_init_lxpanel_gtk = {
.name = "CutiePi battery",
sprintf(buffer, "<span color=\"#%06x\"><b>%c</b></span>", colors[egz->color], egz->text[egz->cnt]); .description = "Displays the current state of the CutiePi battery",
gtk_label_set_markup (GTK_LABEL(egz->widget), buffer) ; .one_per_system = 1,
egz->color++; // assigning our functions to provided pointers.
if( egz->color > 3 ) egz->color = 0; .new_instance = constructor
return TRUE;
}
static gint update_tooltip(Example *egz)
{
char *tooltip;
ENTER;
tooltip = g_strdup_printf(egz->text);
gtk_tooltips_set_tip(egz->tip, egz->main, tooltip, NULL);
g_free(tooltip);
RET(TRUE);
}
static int example_constructor(Plugin *p, char** fp)
{
Example *egz;
char buffer [60];
ENTER;
/* initialization */
egz = g_new0(Example, 1);
egz->plugin = p;
egz->text = g_strdup("Example");
egz->wait = egz->speed = 1;
p->priv = egz;
p->pwid = gtk_event_box_new();
GTK_WIDGET_SET_FLAGS( p->pwid, GTK_NO_WINDOW );
gtk_container_set_border_width( GTK_CONTAINER(p->pwid), 2 );
egz->widget = gtk_label_new(" ");
sprintf(buffer, "<span color=\"#%06x\"><b>%c</b></span>", colors[3], egz->text[0]);
gtk_label_set_markup (GTK_LABEL(egz->widget), buffer) ;
gtk_container_add(GTK_CONTAINER(p->pwid), egz->widget);
egz->main = p->pwid;
egz->tip = gtk_tooltips_new();
update_tooltip(egz);
g_signal_connect (G_OBJECT (p->pwid), "button_press_event", G_CALLBACK (button_press_event), (gpointer) p);
g_signal_connect (G_OBJECT (p->pwid), "scroll_event", G_CALLBACK (scroll_event), (gpointer) p);
line s;
s.len = 256;
if (fp)
{
while (lxpanel_get_line(fp, &s) != LINE_BLOCK_END)
{
if (s.type == LINE_NONE) {
ERR( "example: illegal token %s\n", s.str);
goto error;
}
if (s.type == LINE_VAR) {
if (!g_ascii_strcasecmp(s.t[0], "text")){
g_free(egz->text);
egz->text = g_strdup(s.t[1]);
}else if (!g_ascii_strcasecmp(s.t[0], "speed")){
egz->speed = atoi(s.t[1]);
}else if (!g_ascii_strcasecmp(s.t[0], "bool")){
egz->bool = atoi(s.t[1]); /* 0=false, 1=true */
}else if (!g_ascii_strcasecmp(s.t[0], "file1")){
egz->file = g_strdup(s.t[1]);
}else if (!g_ascii_strcasecmp(s.t[0], "file2")){
egz->file2 = g_strdup(s.t[1]);
}else {
ERR( "example: unknown var %s\n", s.t[0]);
continue;
}
}
else {
ERR( "example: illegal in this context %s\n", s.str);
goto error;
}
}
}
egz->timer = g_timeout_add(500, (GSourceFunc) timer_event, (gpointer)egz); /* set timer */
gtk_widget_show(egz->widget); /* show plugin on panel */
RET(TRUE);
error:
destructor( p );
RET(FALSE);
}
static void applyConfig(Plugin* p)
{
ENTER;
Example *egz = (Example *) p->priv;
gchar buffer [60];
egz->cnt = 0;
egz->color = 0;
if(egz->speed == 0) egz->speed = 1;
egz->wait = egz->speed;
sprintf(buffer, "<span color=\"#%06x\"><b>%c</b></span>", colors[3], egz->text[0]);
update_tooltip(egz);
RET();
}
static void config(Plugin *p, GtkWindow* parent) {
ENTER;
GtkWidget *dialog;
Example *egz = (Example *) p->priv;
dialog = create_generic_config_dlg(_(p->class->name),
GTK_WIDGET(parent),
(GSourceFunc) applyConfig, (gpointer) p,
_("Text"), &egz->text, CONF_TYPE_STR,
_("Speed"), &egz->speed, CONF_TYPE_INT,
_("Stop"), &egz->bool, CONF_TYPE_BOOL,
_("File"), &egz->file, CONF_TYPE_FILE,
_("File"), &egz->file2, CONF_TYPE_FILE_ENTRY,
NULL);
gtk_window_present(GTK_WINDOW(dialog));
RET();
}
static void example_destructor(Plugin *p)
{
ENTER;
Example *egz = (Example *)p->priv;
g_source_remove(egz->timer);
g_free(egz->text);
g_free(egz);
RET();
}
static void save_config( Plugin* p, FILE* fp )
{
ENTER;
Example *egz = (Example *)p->priv;
lxpanel_put_str( fp, "text", egz->text );
lxpanel_put_int( fp, "speed", egz->speed );
lxpanel_put_bool( fp, "bool", egz->bool );
lxpanel_put_str( fp, "file1", egz->file );
lxpanel_put_str( fp, "file2", egz->file2 );
RET();
}
PluginClass example_plugin_class = {
PLUGINCLASS_VERSIONING,
type : "cutiepi-battery",
name : N_("CutiePi battery indicator"),
version: "0.1",
description : N_("Shows the current state of the CutiePi tablet battery"),
constructor : example_constructor,
destructor : example_destructor,
config : config,
save : save_config,
panel_configuration_changed : NULL
}; };