From c9deae38a7015e7966b30007e4c1322a1912d2a0 Mon Sep 17 00:00:00 2001 From: mdb Date: Sat, 18 Aug 2001 02:34:06 +0000 Subject: [PATCH] Modified code to allow running as an applet or standalone. It auto detects whether to operate in applet mode based on the command line arguments. git-svn-id: https://samskivert.googlecode.com/svn/trunk@297 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- projects/lookuplet/configure.in | 5 +- projects/lookuplet/src/Makefile.am | 3 +- projects/lookuplet/src/c/lookuplet.c | 106 ++++++++++++++++++++----- projects/lookuplet/src/c/lookuplet.h | 28 +++++++ projects/lookuplet/src/c/preferences.c | 8 +- projects/lookuplet/src/c/preferences.h | 6 +- projects/lookuplet/src/c/querybox.c | 71 +++++++++-------- 7 files changed, 172 insertions(+), 55 deletions(-) create mode 100644 projects/lookuplet/src/c/lookuplet.h diff --git a/projects/lookuplet/configure.in b/projects/lookuplet/configure.in index 86a98b48..df5e6b0d 100644 --- a/projects/lookuplet/configure.in +++ b/projects/lookuplet/configure.in @@ -1,4 +1,4 @@ -dnl $Id: configure.in,v 1.3 2001/08/16 19:25:56 mdb Exp $ +dnl $Id: configure.in,v 1.4 2001/08/18 02:34:06 mdb Exp $ dnl dnl Process this file with autoconf to produce a configure script. @@ -34,12 +34,11 @@ AM_MAINTAINER_MODE AM_ACLOCAL_INCLUDE(macros) AC_ISC_POSIX -GNOME_INIT +GNOME_INIT(applets) dnl Check for programs AC_PROG_CC AM_PROG_CC_STDC -AC_ARG_PROGRAM AM_PROG_LIBTOOL dnl this should come after `AC_PROG_CC' diff --git a/projects/lookuplet/src/Makefile.am b/projects/lookuplet/src/Makefile.am index 03869307..ba7fa6de 100644 --- a/projects/lookuplet/src/Makefile.am +++ b/projects/lookuplet/src/Makefile.am @@ -1,5 +1,5 @@ # -# $Id: Makefile.am,v 1.3 2001/08/16 00:12:11 mdb Exp $ +# $Id: Makefile.am,v 1.4 2001/08/18 02:34:06 mdb Exp $ bin_PROGRAMS = lookuplet @@ -20,6 +20,7 @@ INCLUDES = \ lookuplet_LDADD = \ $(GNOME_LIBDIR) \ $(GNOMEUI_LIBS) \ + $(GNOME_APPLETS_LIBS) \ $(INTLLIBS) # currently the installation of these bitmaps wouldn't be necessary diff --git a/projects/lookuplet/src/c/lookuplet.c b/projects/lookuplet/src/c/lookuplet.c index ae31d801..66632bcc 100644 --- a/projects/lookuplet/src/c/lookuplet.c +++ b/projects/lookuplet/src/c/lookuplet.c @@ -1,5 +1,5 @@ /** - * $Id: lookuplet.c,v 1.5 2001/08/16 20:25:09 mdb Exp $ + * $Id: lookuplet.c,v 1.6 2001/08/18 02:34:06 mdb Exp $ * * lookuplet - a utility for quickly looking up information * Copyright (C) 2001 Michael Bayne @@ -20,22 +20,62 @@ */ #include +#include #include #include "querybox.h" #include "preferences.h" +/* Used to track whether or not we're being run as an applet or + * standalone. */ +int applet_mode = 0; + static void exit_lookuplet (GtkWidget* widget, gpointer data) { gtk_main_quit(); } +static void +about_box_cb (AppletWidget *applet, gpointer data) +{ + static GtkWidget* about_box = NULL; + const gchar* authors[] = { + (gchar*) "Michael Bayne ", (gchar*)NULL }; + + if (about_box != NULL) { + gdk_window_show(about_box->window); + gdk_window_raise(about_box->window); + return; + } + + about_box = gnome_about_new( + _("lookuplet"), VERSION, "(C) 2001 Michael Bayne", authors, + _("This GNOME applet provides a simple means by which web and " + "other queries can be launched with minimal typing.\n\n\n" + "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 the Free Software Foundation; either version 2 of " + "the License, or (at your option) any later version."), + (gchar*)NULL); + gtk_signal_connect(GTK_OBJECT(about_box), "destroy", + GTK_SIGNAL_FUNC(gtk_widget_destroyed), &about_box); + gtk_widget_show(about_box); +} + int main (int argc, char** argv) { - GtkWidget* window; + GtkWidget* top; GtkWidget* contents; + int i; + + /* figure out if we're running as an applet */ + for (i = 0; i < argc; i++) { + if (strstr(argv[i], "activate-goad-server")) { + applet_mode = 1; + } + } #ifdef ENABLE_NLS /* initialize the i18n stuff */ @@ -43,34 +83,64 @@ main (int argc, char** argv) textdomain(PACKAGE); #endif -#ifdef APPLET_MODE - /* initialize gtk */ - gtk_init(&argc, &argv); -#else - /* initialize gnome */ - gnome_init(PACKAGE, VERSION, argc, argv); -#endif + if (applet_mode) { + /* intialize; this will basically set up the applet, corba and + call gnome_init() */ + applet_widget_init(PACKAGE, VERSION, argc, argv, NULL, 0, NULL); + + /* create our applet_widget */ + if (!(top = applet_widget_new(PACKAGE))) { + g_error("Can't create applet widget.\n"); + exit(1); + } + + } else { + /* initialize gnome */ + gnome_init(PACKAGE, VERSION, argc, argv); + + /* create a new window */ + top = gtk_window_new(GTK_WINDOW_TOPLEVEL); + gtk_signal_connect(GTK_OBJECT(top), "destroy", + GTK_SIGNAL_FUNC(exit_lookuplet), NULL); + } /* initialize our preferences */ lk_prefs_init(); - /* create a new window */ - window = gtk_window_new(GTK_WINDOW_TOPLEVEL); - gtk_signal_connect(GTK_OBJECT(window), "destroy", - GTK_SIGNAL_FUNC(exit_lookuplet), NULL); - /* create our contents and set them up real proper like */ contents = lk_querybox_create(); - gtk_container_add(GTK_CONTAINER(window), contents); - gtk_container_set_border_width(GTK_CONTAINER(window), GNOME_PAD_SMALL); + + if (applet_mode) { + applet_widget_add(APPLET_WIDGET(top), contents); + + } else { + /* we only want a border if we're not an applet */ + gtk_container_set_border_width(GTK_CONTAINER(top), GNOME_PAD_SMALL); + gtk_container_add(GTK_CONTAINER(top), contents); + } + gtk_widget_show_all(contents); - gtk_widget_show(window); + gtk_widget_show(top); + + /* set up our applet menu */ + if (applet_mode) { + applet_widget_register_stock_callback( + APPLET_WIDGET(top), "properties", GNOME_STOCK_MENU_PROP, + _("Properties..."), lk_prefs_display_applet, NULL); + applet_widget_register_stock_callback( + APPLET_WIDGET(top), "about", GNOME_STOCK_MENU_ABOUT, + _("About..."), about_box_cb, NULL); + } /* initialize the querybox (which handles about everything) */ lk_querybox_init(); /* main loop */ - gtk_main(); + if (applet_mode) { + applet_widget_gtk_main(); + } else { + gtk_main(); + } return 0; } diff --git a/projects/lookuplet/src/c/lookuplet.h b/projects/lookuplet/src/c/lookuplet.h new file mode 100644 index 00000000..0973ecda --- /dev/null +++ b/projects/lookuplet/src/c/lookuplet.h @@ -0,0 +1,28 @@ +/** + * $Id: lookuplet.h,v 1.1 2001/08/18 02:34:06 mdb Exp $ + * + * lookuplet - a utility for quickly looking up information + * Copyright (C) 2001 Michael Bayne + * + * 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 the + * Free Software Foundation; either version 2.1 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _LOOKUPLET_H_ +#define _LOOKUPLET_H_ + +/* Used to indicate whether or not we're running in applet mode. */ +extern int applet_mode; + +#endif /* _LOOKUPLET_H_ */ diff --git a/projects/lookuplet/src/c/preferences.c b/projects/lookuplet/src/c/preferences.c index 68c35d86..ad4c145e 100644 --- a/projects/lookuplet/src/c/preferences.c +++ b/projects/lookuplet/src/c/preferences.c @@ -1,5 +1,5 @@ /** - * $Id: preferences.c,v 1.2 2001/08/16 20:25:09 mdb Exp $ + * $Id: preferences.c,v 1.3 2001/08/18 02:34:06 mdb Exp $ * * lookuplet - a utility for quickly looking up information * Copyright (C) 2001 Michael Bayne @@ -453,3 +453,9 @@ lk_prefs_display () /* and finally show our property box */ gtk_widget_show_all(_prefBox); } + +void +lk_prefs_display_applet (AppletWidget* applet, gpointer data) +{ + lk_prefs_display(); +} diff --git a/projects/lookuplet/src/c/preferences.h b/projects/lookuplet/src/c/preferences.h index 7cf7fa0c..47570181 100644 --- a/projects/lookuplet/src/c/preferences.h +++ b/projects/lookuplet/src/c/preferences.h @@ -1,5 +1,5 @@ /** - * $Id: preferences.h,v 1.3 2001/08/16 20:25:09 mdb Exp $ + * $Id: preferences.h,v 1.4 2001/08/18 02:34:06 mdb Exp $ * * lookuplet - a utility for quickly looking up information * Copyright (C) 2001 Michael Bayne @@ -22,6 +22,7 @@ #ifndef _PREFERENCES_H_ #define _PREFERENCES_H_ +#include #include extern void @@ -36,4 +37,7 @@ lk_prefs_cleanup (void); extern void lk_prefs_display (void); +extern void +lk_prefs_display_applet (AppletWidget* applet, gpointer data); + #endif /* _PREFERENCES_H_ */ diff --git a/projects/lookuplet/src/c/querybox.c b/projects/lookuplet/src/c/querybox.c index 0dc11c3e..5e468e01 100644 --- a/projects/lookuplet/src/c/querybox.c +++ b/projects/lookuplet/src/c/querybox.c @@ -1,5 +1,5 @@ /** - * $Id: querybox.c,v 1.6 2001/08/16 20:25:09 mdb Exp $ + * $Id: querybox.c,v 1.7 2001/08/18 02:34:06 mdb Exp $ * * lookuplet - a utility for quickly looking up information * Copyright (C) 2001 Michael Bayne @@ -29,6 +29,7 @@ #include "launcher.h" #include "keysym-util.h" #include "preferences.h" +#include "lookuplet.h" #include "querybox.h" static GtkWidget* _query; @@ -88,12 +89,12 @@ key_pressed (GtkWidget* widget, GdkEvent* event, gpointer callback_data) /* launch the appropriate thing */ lk_launcher_launch(binding, search_text); g_free(search_text); -#ifdef APPLET_MODE - /* clear out the contents of the entry box */ - gtk_entry_set_text(GTK_ENTRY(widget), ""); -#else - gtk_exit(0); -#endif + if (applet_mode) { + /* clear out the contents of the entry box */ + gtk_entry_set_text(GTK_ENTRY(widget), ""); + } else { + gtk_exit(0); + } handled = TRUE; break; } @@ -120,12 +121,12 @@ launch (GtkWidget* widget, gpointer data) lk_launcher_launch(NULL, search_text); g_free(search_text); -#ifdef APPLET_MODE - /* clear out the contents of the entry box */ - gtk_entry_set_text(GTK_ENTRY(widget), ""); -#else - gtk_exit(0); -#endif + if (applet_mode) { + /* clear out the contents of the entry box */ + gtk_entry_set_text(GTK_ENTRY(widget), ""); + } else { + gtk_exit(0); + } } GtkWidget* @@ -145,18 +146,22 @@ lk_querybox_create (void) gtk_signal_connect(GTK_OBJECT(_query), "activate", GTK_SIGNAL_FUNC(launch), NULL); - /* create our preferences button */ - prefs = gtk_button_new_with_label(_("Prefs")); - gtk_signal_connect (GTK_OBJECT(prefs), "clicked", - GTK_SIGNAL_FUNC(prefs_clicked), NULL); - - /* create a vertical box to hold our query input box and the label */ + /* create a horizontal box to hold our query input box and the + * label */ hbox = gtk_hbox_new(FALSE, GNOME_PAD_SMALL); /* pack the query box and label into the hbox */ gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox), _query, TRUE, TRUE, 0); - gtk_box_pack_start(GTK_BOX(hbox), prefs, FALSE, FALSE, 0); + + /* create our preferences button (only if we're not an applet, if + * we're an applet we'll have a preferences menu item instead) */ + if (!applet_mode) { + prefs = gtk_button_new_with_label(_("Prefs")); + gtk_signal_connect (GTK_OBJECT(prefs), "clicked", + GTK_SIGNAL_FUNC(prefs_clicked), NULL); + gtk_box_pack_start(GTK_BOX(hbox), prefs, FALSE, FALSE, 0); + } return hbox; } @@ -170,18 +175,22 @@ lk_querybox_init (void) gtk_signal_connect(GTK_OBJECT(_query), "key_press_event", GTK_SIGNAL_FUNC(key_pressed), NULL); - /* focus the text entry box */ - gtk_widget_grab_focus(_query); + /* if we're not an applet, we want to request focus and grab the + current X selection */ + if (!applet_mode) { + /* focus the text entry box */ + gtk_widget_grab_focus(_query); - /* register a signal handler that will select the selection when it - arrives */ - gtk_signal_connect_after(GTK_OBJECT(_query), "selection_received", - GTK_SIGNAL_FUNC(selection_received), NULL); + /* register a signal handler that will select the selection when + it arrives */ + gtk_signal_connect_after(GTK_OBJECT(_query), "selection_received", + GTK_SIGNAL_FUNC(selection_received), NULL); - /* request the selection as type "STRING" */ - if (targets_atom == GDK_NONE) { - targets_atom = gdk_atom_intern ("STRING", FALSE); + /* request the selection as type "STRING" */ + if (targets_atom == GDK_NONE) { + targets_atom = gdk_atom_intern ("STRING", FALSE); + } + gtk_selection_convert(_query, GDK_SELECTION_PRIMARY, targets_atom, + GDK_CURRENT_TIME); } - gtk_selection_convert(_query, GDK_SELECTION_PRIMARY, targets_atom, - GDK_CURRENT_TIME); }