applets/dvorak/dvorak.c



[did this get through, or am i too offtopic here?]
Hi,

Tonight i made a rather braindead attempt to replace my tcl/tk
keyboard layout switching script with a gnome panel applet:
two radiobuttons that invoke my xdv/xqw scripts (that in turn
do a xmodmap, of course).

There'll probably be a real keyboard layout applet soon, so i
don't expect this thing to be around too long, but at the moment
it's just what i need...

It kind of works now, but i have two questions: 

* when i select another layout, there are lots of messages:

      Gdk-WARNING **: xwindow(0) lookup reveals NULL

 how do i fix this?

* how do i add a keyboard binding to this switch?

Thanks,

Jan.

Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond - The music typesetter
http://www.xs4all.nl/~jantien       | http://www.xs4all.nl/~jantien/lilypond

/*
 * GNOME dvorak switch
 * (C) 1998 Jan Nieuwenhuizen
 *
 * based on:
 * GNOME clock/webcontrol
 *
 * Author: Jan Nieuwenhuizen
 *
 */

#include <config.h>
#include <gnome.h>
#include <applet-widget.h>
#include <sys/types.h>
#include <sys/wait.h>

typedef struct _dvorak_properties dvorak_properties;

struct _dvorak_properties
  {
    gint dvorak;
  };

typedef struct _Dvorak Dvorak;

struct _Dvorak
  {
    dvorak_properties properties;
    GtkWidget *applet;
    GtkWidget *dvorak;
    GtkWidget *qwerty;
  };

static Dvorak DV =
{
  {TRUE},
  {-1},
  NULL,
  NULL,
  NULL
};

/* the most important dialog in the whole application */
/* shamelessly jacked from the fish applet, eh, make that */
/* webcontrol applet.  --jcn */
void
about_cb (AppletWidget * widget, gpointer data)
{
  GtkWidget *about;
  gchar *authors[2];
  const gchar *author_format = _ ("%s the Fish");

  authors[0] = _ ("Jan Nieuwenhuizen <janneke@gnu.org>");
  authors[1] = NULL;

  about = gnome_about_new (_ ("Keyboard switch"), "0.1",
			   "(C) 1998 Jan Nieuwenhuizen",
			   authors,
			   _ ("This applet switches between "
			      "dvorak and qwerty keyboard layouts."),
			   NULL);
  gtk_widget_show (about);

  return;
}

void
set_layout (GtkWidget * w, int *data)
{
  int status;

  DV.properties.dvorak = (int)data;
  if (fork () == 0)
    {
      if(DV.properties.dvorak) 
	{
	  status = execlp("xdv", "xdv", NULL);
	}
      else
	{
	  status = execlp("xqw", "xqw", NULL);
	}
      if (status)
	{			/* command didn't work */
	  char *argv[2];
	  argv[0] = DV.properties.dvorak ? "xdv" : "xqw";
	  argv[1] = NULL;
	  gnome_execute_async (NULL, 1, argv);
	}
    }
}

void
create_widget ()
{
  GtkWidget *vbox;

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (vbox);

  DV.dvorak = gtk_radio_button_new_with_label(NULL, _("dvorak"));
  gtk_widget_show (DV.dvorak);
  DV.qwerty = gtk_radio_button_new_with_label(
	     gtk_radio_button_group(GTK_RADIO_BUTTON(DV.dvorak)),
					  _("qwerty"));
  gtk_widget_show (DV.qwerty);

  gtk_signal_connect (GTK_OBJECT (DV.dvorak), "toggled",
		      GTK_SIGNAL_FUNC (set_layout),
		     (gpointer) 1);

  gtk_signal_connect (GTK_OBJECT (DV.qwerty), "toggled",
		      GTK_SIGNAL_FUNC (set_layout),
		     (gpointer) 0);

  gtk_box_pack_start (GTK_BOX (vbox), DV.dvorak, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), DV.qwerty, FALSE, FALSE, 0);

  /* add the widget to the applet-widget, and thereby actually
     putting it "onto" the panel */
  applet_widget_add (APPLET_WIDGET (DV.applet), vbox);

}

static void
apply_cb (GnomePropertyBox * pb, gint page, gpointer data)
{
  if (page != -1)
    return;			/* Only honor global apply */

  gtk_widget_queue_resize (DV.applet);
}

void
properties_cb (AppletWidget * widget, gpointer data)
{
  GtkWidget *pb;
  GtkWidget *vbox;

  pb = gnome_property_box_new ();

  gtk_window_set_title (GTK_WINDOW (pb), _ ("Dvorak Properties"));

  vbox = gtk_vbox_new (GNOME_PAD, FALSE);
  gtk_container_border_width (GTK_CONTAINER (vbox), GNOME_PAD);

  gnome_property_box_append_page (GNOME_PROPERTY_BOX (pb), vbox,
				  gtk_label_new (_ ("Look")));

  gtk_signal_connect (GTK_OBJECT (pb), "apply", GTK_SIGNAL_FUNC (apply_cb),
		      NULL);

  gtk_widget_show_all (pb);
}

/* sesion save signal handler */
static gint
applet_save_session (GtkWidget * w,
		     const char *privcfgpath,
		     const char *globcfgpath)
{
  gnome_config_push_prefix (privcfgpath);
  gnome_config_set_bool ("dvorak/dvorak", DV.properties.dvorak);
  gnome_config_pop_prefix ();

  gnome_config_sync ();
  /* you need to use the drop_all here since we're all writing to
     one file, without it, things might not work too well */
  gnome_config_drop_all ();

  /* make sure you return FALSE, otherwise your applet might not
     work compeltely, there are very few circumstances where you
     want to return TRUE. This behaves similiar to GTK events, in
     that if you return FALSE it means that you haven't done
     everything yourself, meaning you want the panel to save your
     other state such as the panel you are on, position,
     parameter, etc ... */
  return FALSE;
}

int
main (int argc, char **argv)
{
  GtkWidget *input;
  GtkWidget *hbox, *vbox;

  /* Initialize the i18n stuff */
  bindtextdomain (PACKAGE, GNOMELOCALEDIR);
  textdomain (PACKAGE);

  /* intialize, this will basically set up the applet, corba and
     call gnome_init */
  applet_widget_init_defaults ("dvorak_applet", NULL, argc, argv, 0,
			       NULL, argv[0]);

  /* create a new applet_widget */
  DV.applet = applet_widget_new ();
  /* in the rare case that the communication with the panel
     failed, error out */
  if (!DV.applet)
    g_error ("Can't create applet!\n");

  gnome_config_push_prefix (APPLET_WIDGET (DV.applet)->privcfgpath);
  DV.properties.dvorak = gnome_config_get_bool ("dvorak/dvorak=true");
  gnome_config_pop_prefix ();

  create_widget ();

  /* bind the session save signal */
  gtk_signal_connect (GTK_OBJECT (DV.applet), "save_session",
		      GTK_SIGNAL_FUNC (applet_save_session),
		      NULL);

  /* add an item to the applet menu */
  applet_widget_register_stock_callback (APPLET_WIDGET (DV.applet),
					 "about",
					 GNOME_STOCK_MENU_ABOUT,
					 _ ("About..."),
					 about_cb,
					 NULL);

  /* add an item to the applet menu */
  applet_widget_register_stock_callback (APPLET_WIDGET (DV.applet),
					 "properties",
					 GNOME_STOCK_MENU_PROP,
					 _ ("Properties..."),
					 properties_cb,
					 NULL);

  /* add the widget to the applet-widget, and thereby actually
     putting it "onto" the panel */
  gtk_widget_show (DV.applet);

  /* special corba main loop */
  applet_widget_gtk_main ();

  return 0;
}



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