96aa4483433918c39c25030b8a060ea7426d2b44
[vte.git] / src / vteapp.c
1 /*
2  * Copyright (C) 2001,2002 Red Hat, Inc.
3  *
4  * This is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Library General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #ident "$Id$"
20 #include "../config.h"
21 #include <string.h>
22 #include <gtk/gtk.h>
23 #include <glib-object.h>
24 #include "vte.h"
25
26 static void
27 set_window_title(GtkWidget *widget, const char *title, gpointer win)
28 {
29         GtkWindow *window;
30         g_return_if_fail(VTE_TERMINAL(widget));
31         g_return_if_fail(GTK_IS_WINDOW(win));
32         g_return_if_fail(title != NULL);
33         window = GTK_WINDOW(win);
34         gtk_window_set_title(window, title);
35 }
36
37 static void
38 char_size_changed(GtkWidget *widget, guint width, guint height, gpointer win)
39 {
40         VteTerminal *terminal;
41         GtkWindow *window;
42         GdkGeometry geometry;
43         g_return_if_fail(GTK_IS_WINDOW(win));
44         g_return_if_fail(VTE_IS_TERMINAL(widget));
45         terminal = VTE_TERMINAL(widget);
46         window = GTK_WINDOW(win);
47         geometry.base_width = 0;
48         geometry.base_height = 0;
49         geometry.min_width = terminal->char_width;
50         geometry.min_height = terminal->char_height;
51         geometry.width_inc = terminal->char_width;
52         geometry.height_inc = terminal->char_height;
53         gtk_window_set_geometry_hints(window, widget, &geometry,
54                                       GDK_HINT_RESIZE_INC |
55                                       GDK_HINT_BASE_SIZE |
56                                       GDK_HINT_MIN_SIZE);
57 }
58
59 int
60 main(int argc, char **argv)
61 {
62         GtkWidget *window, *hbox, *scrollbar, *widget;
63         const char *message = "Launching interactive shell...\r\n";
64
65         gtk_init(&argc, &argv);
66
67         /* Create a window to hold the scrolling shell, and hook its
68          * delete event to the quit function.. */
69         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
70         g_signal_connect(G_OBJECT(window), "delete_event",
71                          GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
72
73         /* Create a box to hold everything. */
74         hbox = gtk_hbox_new(0, FALSE);
75         gtk_container_add(GTK_CONTAINER(window), hbox);
76
77         /* Create the terminal widget and add it to the scrolling shell. */
78         widget = vte_terminal_new();
79         char_size_changed(widget,
80                           (VTE_TERMINAL(widget)->char_width),
81                           (VTE_TERMINAL(widget)->char_height),
82                           window);
83         gtk_box_pack_start(GTK_BOX(hbox), widget, TRUE, TRUE, 0);
84
85         /* Connect to the "char_size_changed" signal to set geometry hints
86          * whenever the font used by the terminal is changed. */
87         g_signal_connect_object(G_OBJECT(widget), "char_size_changed",
88                                 G_CALLBACK(char_size_changed), window, 0);
89
90         /* Connect to the "set_window_title" signal to set the main window's
91          * title. */
92         g_signal_connect(G_OBJECT(widget), "set_window_title",
93                          G_CALLBACK(set_window_title), window);
94
95         /* Connect to the "eof" signal to quit when the session ends. */
96         g_signal_connect(G_OBJECT(widget), "eof",
97                          G_CALLBACK(gtk_main_quit), NULL);
98
99         /* Create the scrollbar for the widget. */
100         scrollbar = gtk_vscrollbar_new((VTE_TERMINAL(widget))->adjustment);
101         gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, FALSE, 0);
102
103         /* Launch a shell. */
104 #ifdef VTE_DEBUG
105         vte_terminal_feed(VTE_TERMINAL(widget), message, strlen(message));
106 #endif
107         vte_terminal_fork_command(VTE_TERMINAL(widget), NULL, NULL);
108
109         /* Go for it! */
110         gtk_widget_show_all(window);
111         gtk_main();
112
113         return 0;
114 }