swallow.c 8.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * GNOME panel swallow module.
 * (C) 1997 The Free Software Foundation
 *
 * Author: George Lebl
 */

#include <config.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <gnome.h>
George Lebl's avatar
George Lebl committed
14 15
#include <gdk/gdkx.h>
#include <X11/Xlib.h>
16 17 18 19 20 21
#include "panel-widget.h"
#include "panel.h"
#include "panel_config_global.h"
#include "swallow.h"
#include "mico-glue.h"

22 23
extern PanelWidget *current_panel;

24 25 26 27 28 29
static int
ignore_x_error(Display* d, XErrorEvent* e)
{
	fprintf(stderr, "ignore_x_error called\n");
	return 0;
}
George Lebl's avatar
George Lebl committed
30

George Lebl's avatar
George Lebl committed
31
static int
32
get_window_id(Window win, char *title, guint32 *wid)
George Lebl's avatar
George Lebl committed
33 34 35 36 37
{
	Window root_return;
	Window parent_return;
	Window *children;
	unsigned int nchildren;
38
	unsigned int i;
George Lebl's avatar
George Lebl committed
39
	char *tit;
George Lebl's avatar
George Lebl committed
40
	int ret = FALSE;
41
	int (*oldErrorHandler)(Display*, XErrorEvent*);
George Lebl's avatar
George Lebl committed
42

43
		
George Lebl's avatar
George Lebl committed
44
	XQueryTree(GDK_DISPLAY(),
45
		   win,
George Lebl's avatar
George Lebl committed
46 47 48 49 50
		   &root_return,
		   &parent_return,
		   &children,
		   &nchildren);

51
	oldErrorHandler = XSetErrorHandler(ignore_x_error);
George Lebl's avatar
George Lebl committed
52
	for(i=0;i<nchildren;i++) {
53 54 55 56 57
		if (!XFetchName(GDK_DISPLAY(),
				children[i],
				&tit)) {
			continue;
		}
George Lebl's avatar
George Lebl committed
58 59 60
		if(tit) {
			if(strcmp(tit,title)==0) {
				XFree(tit);
61 62
				*wid = children[i];
				ret = TRUE;
George Lebl's avatar
George Lebl committed
63 64 65 66 67
				break;
			}
			XFree(tit);
		}
	}
68
	XSetErrorHandler(oldErrorHandler);
69 70
	for(i=0;!ret && i<nchildren;i++)
		ret=get_window_id(children[i],title,wid);
George Lebl's avatar
George Lebl committed
71 72
	if(children)
		XFree(children);
73
	return ret;
George Lebl's avatar
George Lebl committed
74 75
}

76
/*we should really do this differently but for now this is good enough*/
George Lebl's avatar
George Lebl committed
77
static int
78 79
socket_getwindow_timeout(Swallow *swallow)
{
80
	if(!get_window_id(GDK_ROOT_WINDOW(),swallow->title, &(swallow->wid)))
81 82 83 84 85
		return TRUE;
	gtk_socket_steal(GTK_SOCKET(swallow->socket),swallow->wid);
	return FALSE;
}

George Lebl's avatar
George Lebl committed
86
static int
87 88 89 90
socket_realized(GtkWidget *w, gpointer data)
{
	Swallow *swallow = gtk_object_get_user_data(GTK_OBJECT(w));

91
	g_return_val_if_fail(swallow->title!=NULL,FALSE);
92

93
	if(!get_window_id(GDK_ROOT_WINDOW(),swallow->title, &swallow->wid))
94 95
		gtk_timeout_add(500,(GtkFunction)socket_getwindow_timeout,
				swallow);
96
	else
97
		gtk_socket_steal(GTK_SOCKET(swallow->socket),swallow->wid);
98 99 100 101

	return FALSE;
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
static int
socket_destroyed(GtkWidget *w, gpointer data)
{
	Swallow *swallow = data;
	
	g_free(swallow->title);
	g_free(swallow);

	return FALSE;
}


static void
really_add_swallow(GtkWidget *d,int button, gpointer data)
{
	GtkWidget *title_e = gtk_object_get_data(GTK_OBJECT(d),"title_e");
	GtkWidget *exec_e = gtk_object_get_data(GTK_OBJECT(d),"exec_e");
	GtkWidget *width_s = gtk_object_get_data(GTK_OBJECT(d),"width_s");
	GtkWidget *height_s = gtk_object_get_data(GTK_OBJECT(d),"height_s");


	if(button!=0) {
		gtk_widget_destroy(d);
		return;
	}
	
	load_applet(SWALLOW_ID,
		    gtk_entry_get_text(GTK_ENTRY(exec_e)),
		    gtk_entry_get_text(GTK_ENTRY(title_e)),
		    gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(width_s)),
		    gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(height_s)),
		    NULL,NULL,
		    PANEL_UNKNOWN_APPLET_POSITION,
		    current_panel,NULL);
	gtk_widget_destroy(d);
}

static void
act_really_add_swallow(GtkWidget *w, gpointer data)
{
	GtkWidget *d = data;
	
	/*just call the above handler for the dialog*/
	really_add_swallow(d,0,NULL);
}

/*I couldn't resist the naming of this function*/
void
ask_about_swallowing(void)
{
	GtkWidget *d;

	GtkWidget *title_e;
	GtkWidget *exec_e;
	GtkWidget *width_s;
	GtkWidget *height_s;
	GtkWidget *w;
	GtkWidget *box;
	GtkAdjustment *adj;
	d = gnome_dialog_new(_("Create swallow applet"),
			     GNOME_STOCK_BUTTON_OK,
			     GNOME_STOCK_BUTTON_CANCEL,
			     NULL);
165 166
	gtk_window_position(GTK_WINDOW(d), GTK_WIN_POS_CENTER);
	gtk_window_set_policy(GTK_WINDOW(d), FALSE, FALSE, TRUE);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

	box = gtk_hbox_new(FALSE,5);
	gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(d)->vbox),box,
			   TRUE,TRUE,5);
	w = gtk_label_new(_("Title of application to swallow"));
	gtk_box_pack_start(GTK_BOX(box),w,FALSE,FALSE,0);
	w = gnome_entry_new("swallow_title");
	gtk_box_pack_start(GTK_BOX(box),w,TRUE,TRUE,0);
	title_e = gnome_entry_gtk_entry(GNOME_ENTRY(w));
	gtk_signal_connect(GTK_OBJECT(title_e),"activate",
			   GTK_SIGNAL_FUNC(act_really_add_swallow),d);

	box = gtk_hbox_new(FALSE,5);
	gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(d)->vbox),box,
			   TRUE,TRUE,5);
	w = gtk_label_new(_("Command (optional)"));
	gtk_box_pack_start(GTK_BOX(box),w,FALSE,FALSE,0);
	w = gnome_file_entry_new("execute",_("Browse"));
	gtk_box_pack_start(GTK_BOX(box),w,TRUE,TRUE,0);
	exec_e = gnome_file_entry_gtk_entry (GNOME_FILE_ENTRY (w));
	gtk_signal_connect(GTK_OBJECT(exec_e),"activate",
			   GTK_SIGNAL_FUNC(act_really_add_swallow),d);

	box = gtk_hbox_new(FALSE,5);
	gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(d)->vbox),box,
			   TRUE,TRUE,5);
	w = gtk_label_new(_("Width (optional)"));
	gtk_box_pack_start(GTK_BOX(box),w,FALSE,FALSE,0);
	adj = (GtkAdjustment *) gtk_adjustment_new (0.0, 0.0, 255.0, 1.0,
						    5.0, 0.0);
	width_s = gtk_spin_button_new(adj,0,0);
	gtk_box_pack_start(GTK_BOX(box),width_s,TRUE,TRUE,0);
	w = gtk_label_new(_("Height (optional)"));
	gtk_box_pack_start(GTK_BOX(box),w,FALSE,FALSE,0);
	adj = (GtkAdjustment *) gtk_adjustment_new (0.0, 0.0, 255.0, 1.0,
						    5.0, 0.0);
	height_s = gtk_spin_button_new(adj,0,0);
	gtk_box_pack_start(GTK_BOX(box),height_s,TRUE,TRUE,0);

	gtk_signal_connect(GTK_OBJECT(d),"clicked",
			   GTK_SIGNAL_FUNC(really_add_swallow),NULL);
	gtk_object_set_data(GTK_OBJECT(d),"title_e",title_e);
	gtk_object_set_data(GTK_OBJECT(d),"exec_e",exec_e);
	gtk_object_set_data(GTK_OBJECT(d),"width_s",width_s);
	gtk_object_set_data(GTK_OBJECT(d),"height_s",height_s);


	gnome_dialog_close_hides(GNOME_DIALOG(d),FALSE);

	gnome_dialog_set_default(GNOME_DIALOG(d),0);

	gtk_widget_grab_focus(title_e);

	gtk_widget_show_all(d);
}


224
Swallow *
225
create_swallow_applet(char *title, int width, int height, SwallowOrient orient)
226 227
{
	Swallow *swallow;
228
	GtkWidget *w;
229 230 231 232 233 234

	swallow = g_new(Swallow,1);

	swallow->table = gtk_table_new(2,2,FALSE);
	gtk_widget_show(swallow->table);

235
	swallow->handle_n = gtk_vbox_new(FALSE,0);
236 237 238 239 240
	gtk_table_attach(GTK_TABLE(swallow->table),swallow->handle_n,
			 1,2,0,1,
			 GTK_FILL|GTK_EXPAND|GTK_SHRINK,
			 GTK_FILL|GTK_EXPAND|GTK_SHRINK,
			 0,0);
241 242

	w = gtk_frame_new(NULL);
243
	gtk_widget_set_usize(w,48,0);
244 245 246 247
	gtk_frame_set_shadow_type(GTK_FRAME(w),GTK_SHADOW_OUT);
	gtk_box_pack_start(GTK_BOX(swallow->handle_n),w,TRUE,TRUE,0);
	gtk_widget_show(w);
	w = gtk_frame_new(NULL);
248
	gtk_widget_set_usize(w,48,0);
249 250 251 252
	gtk_frame_set_shadow_type(GTK_FRAME(w),GTK_SHADOW_OUT);
	gtk_box_pack_start(GTK_BOX(swallow->handle_n),w,TRUE,TRUE,0);
	gtk_widget_show(w);
	w = gtk_frame_new(NULL);
253
	gtk_widget_set_usize(w,48,0);
254 255 256 257 258 259
	gtk_frame_set_shadow_type(GTK_FRAME(w),GTK_SHADOW_OUT);
	gtk_box_pack_start(GTK_BOX(swallow->handle_n),w,TRUE,TRUE,0);
	gtk_widget_show(w);

	swallow->handle_w = gtk_hbox_new(FALSE,0);
	gtk_table_attach(GTK_TABLE(swallow->table),swallow->handle_w,
260 261 262 263 264
			 0,1,1,2,
			 GTK_FILL|GTK_EXPAND|GTK_SHRINK,
			 GTK_FILL|GTK_EXPAND|GTK_SHRINK,
			 0,0);

265
	w = gtk_frame_new(NULL);
266
	gtk_widget_set_usize(w,0,48);
267 268 269 270
	gtk_frame_set_shadow_type(GTK_FRAME(w),GTK_SHADOW_OUT);
	gtk_box_pack_start(GTK_BOX(swallow->handle_w),w,TRUE,TRUE,0);
	gtk_widget_show(w);
	w = gtk_frame_new(NULL);
271
	gtk_widget_set_usize(w,0,48);
272 273 274 275
	gtk_frame_set_shadow_type(GTK_FRAME(w),GTK_SHADOW_OUT);
	gtk_box_pack_start(GTK_BOX(swallow->handle_w),w,TRUE,TRUE,0);
	gtk_widget_show(w);
	w = gtk_frame_new(NULL);
276
	gtk_widget_set_usize(w,0,48);
277 278 279 280
	gtk_frame_set_shadow_type(GTK_FRAME(w),GTK_SHADOW_OUT);
	gtk_box_pack_start(GTK_BOX(swallow->handle_w),w,TRUE,TRUE,0);
	gtk_widget_show(w);
	
281
	swallow->socket=gtk_socket_new();
282 283
	if(width != 0 || height != 0)
		gtk_widget_set_usize(swallow->socket,width,height);
284 285
	gtk_signal_connect_after(GTK_OBJECT(swallow->socket),"realize",
			         GTK_SIGNAL_FUNC(socket_realized), NULL);
286 287
	gtk_signal_connect_after(GTK_OBJECT(swallow->socket),"destroy",
			         GTK_SIGNAL_FUNC(socket_destroyed), swallow);
288

289 290 291 292 293
	gtk_table_attach(GTK_TABLE(swallow->table),swallow->socket,
			 1,2,1,2,
			 GTK_FILL|GTK_EXPAND|GTK_SHRINK,
			 GTK_FILL|GTK_EXPAND|GTK_SHRINK,
			 0,0);
294
	
295
	gtk_widget_show(swallow->socket);
296

297 298
	gtk_object_set_user_data(GTK_OBJECT(swallow->socket),swallow);

299 300 301
	swallow->title = g_strdup(title);
	swallow->width = width;
	swallow->height = height;
302
	swallow->wid = -1;
303

304 305 306 307 308 309 310 311 312 313
	set_swallow_applet_orient(swallow, orient);

	return swallow;
}

void
set_swallow_applet_orient(Swallow *swallow, SwallowOrient orient)
{
	if(orient==SWALLOW_VERTICAL) {
		gtk_widget_show(swallow->handle_n);
314
		gtk_widget_hide(swallow->handle_w);
315 316
	} else {
		gtk_widget_hide(swallow->handle_n);
317
		gtk_widget_show(swallow->handle_w);
318 319
	}
}