summaryrefslogtreecommitdiff
path: root/src/vteapp.c
blob: 404cb90aa98fec7d24a9d51e8e8e76e71ade7495 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
/*
 * Copyright (C) 2001,2002 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 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 Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ident "$Id$"
#include "../config.h"
#include <sys/stat.h>
#include <string.h>
#include <gtk/gtk.h>
#include <glib-object.h>
#include "vte.h"

static void
window_title_changed(GtkWidget *widget, gpointer win)
{
	GtkWindow *window;

	g_return_if_fail(VTE_TERMINAL(widget));
	g_return_if_fail(GTK_IS_WINDOW(win));
	g_return_if_fail(VTE_TERMINAL (widget)->window_title != NULL);
	window = GTK_WINDOW(win);

	gtk_window_set_title(window, VTE_TERMINAL (widget)->window_title);
}

static void
char_size_changed(GtkWidget *widget, guint width, guint height, gpointer win)
{
	VteTerminal *terminal;
	GtkWindow *window;
	GdkGeometry geometry;
	g_return_if_fail(GTK_IS_WINDOW(win));
	g_return_if_fail(VTE_IS_TERMINAL(widget));
	terminal = VTE_TERMINAL(widget);
	window = GTK_WINDOW(win);
	geometry.base_width = 0;
	geometry.base_height = 0;
	geometry.min_width = terminal->char_width;
	geometry.min_height = terminal->char_height;
	geometry.width_inc = terminal->char_width;
	geometry.height_inc = terminal->char_height;
	gtk_window_set_geometry_hints(window, widget, &geometry,
				      GDK_HINT_RESIZE_INC |
				      GDK_HINT_BASE_SIZE |
				      GDK_HINT_MIN_SIZE);
}

static void
destroy_and_quit(GtkWidget *widget, gpointer data)
{
	if (GTK_IS_CONTAINER(data)) {
		gtk_container_remove(GTK_CONTAINER(data), widget);
	} else {
		gtk_widget_destroy(widget);
	}
	gtk_main_quit();
}

int
main(int argc, char **argv)
{
	GtkWidget *window, *hbox, *scrollbar, *widget;
	const char *message = "Launching interactive shell...\r\n";
	struct stat st;

	gtk_init(&argc, &argv);

	/* Create a window to hold the scrolling shell, and hook its
	 * delete event to the quit function.. */
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	g_signal_connect(G_OBJECT(window), "delete_event",
			 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);

	/* Create a box to hold everything. */
	hbox = gtk_hbox_new(0, FALSE);
	gtk_container_add(GTK_CONTAINER(window), hbox);

	/* Create the terminal widget and add it to the scrolling shell. */
	widget = vte_terminal_new();
	char_size_changed(widget,
			  (VTE_TERMINAL(widget)->char_width),
			  (VTE_TERMINAL(widget)->char_height),
			  window);
	gtk_box_pack_start(GTK_BOX(hbox), widget, TRUE, TRUE, 0);

	/* Connect to the "char_size_changed" signal to set geometry hints
	 * whenever the font used by the terminal is changed. */
	g_signal_connect_object(G_OBJECT(widget), "char_size_changed",
				G_CALLBACK(char_size_changed), window, 0);

	/* Connect to the "window_title_changed" signal to set the main
	 * window's title. */
	g_signal_connect(G_OBJECT(widget), "window_title_changed",
			 G_CALLBACK(window_title_changed), window);

	/* Connect to the "eof" signal to quit when the session ends. */
	g_signal_connect(G_OBJECT(widget), "eof",
			 G_CALLBACK(destroy_and_quit), widget);

	/* Create the scrollbar for the widget. */
	scrollbar = gtk_vscrollbar_new((VTE_TERMINAL(widget))->adjustment);
	gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, FALSE, 0);

	/* Set some defaults. */
	vte_terminal_set_audible_bell(VTE_TERMINAL(widget), TRUE);
	vte_terminal_set_cursor_blinks(VTE_TERMINAL(widget), TRUE);
	vte_terminal_set_scroll_on_output(VTE_TERMINAL(widget), FALSE);
	vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(widget), TRUE);
	vte_terminal_set_scrollback_lines(VTE_TERMINAL(widget), 100);
	if (stat("./background", &st) == 0) {
		vte_terminal_set_background_image_file(VTE_TERMINAL(widget),
						       "./background");
	} else {
		vte_terminal_set_background_transparent(VTE_TERMINAL(widget),
							TRUE);
	}

	/* Set the default font. */
	vte_terminal_set_font_from_string(VTE_TERMINAL(widget),
					  "fixed semi-condensed 12");

	/* Launch a shell. */
#ifdef VTE_DEBUG
	vte_terminal_feed(VTE_TERMINAL(widget), message, strlen(message));
#endif
	vte_terminal_fork_command(VTE_TERMINAL(widget), NULL, NULL);
	vte_terminal_feed_child(VTE_TERMINAL(widget), "pwd\n", -1);

	/* Go for it! */
	gtk_widget_show_all(window);
	gtk_main();

	return 0;
}