Initial import from Charles Yates' v0.2 tarball.
[rugen] / src / page_command.c
1 /*
2 * page_command.c -- Command Page Handling
3 * Copyright (C) 2002-2003 Charles Yates <charles.yates@pandora.be>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <gtk/gtk.h>
27
28 #include "interface.h"
29 #include "support.h"
30 #include "dv1394app.h"
31 #include "util.h"
32 #include "page.h"
33
34 typedef struct page_command_t
35 {
36 struct page_t parent;
37 dv1394app app;
38 GtkWidget *widget;
39 }
40 *page_command;
41
42 static GtkWidget *this_page_get_widget( page_command );
43
44 /** Main window - command tab - command to be executed.
45 */
46
47 static gboolean on_command_pressed( GtkWidget *button, gpointer user_data )
48 {
49 page_command this = user_data;
50 GtkWidget *widget;
51 GtkTextBuffer *buffer;
52 char *command;
53 PangoFontDescription *font_desc;
54
55 if ( dv1394app_get_parser( this->app ) != NULL )
56 {
57 int index = 0;
58 valerie_response response = NULL;
59 GtkTextIter iter;
60
61 widget = lookup_widget( this_page_get_widget( this ), "entry_command" );
62 command = (char *)gtk_entry_get_text( GTK_ENTRY( widget ) );
63
64 widget = lookup_widget( this_page_get_widget( this ), "textview_command" );
65
66 font_desc = pango_font_description_from_string( "Courier 9" );
67 gtk_widget_modify_font( widget, font_desc );
68 pango_font_description_free( font_desc );
69 buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW( widget ) );
70 gtk_text_buffer_get_end_iter( buffer, &iter );
71 gtk_text_buffer_place_cursor( buffer, &iter );
72 gtk_text_buffer_insert_at_cursor( buffer, "> ", -1 );
73 gtk_text_buffer_insert_at_cursor( buffer, command, -1 );
74 gtk_text_buffer_insert_at_cursor( buffer, "\n", -1 );
75 valerie_execute( dv1394app_get_command( this->app ), 1024, "%s", command );
76 response = valerie_get_last_response( dv1394app_get_command( this->app ) );
77 for ( index = 0; index < valerie_response_count( response ); index ++ )
78 {
79 if ( index != valerie_response_count( response ) - 1 ||
80 strcmp( valerie_response_get_line( response, index ), "" ) )
81 {
82 gtk_text_buffer_insert_at_cursor( buffer, valerie_response_get_line( response, index ), -1 );
83 gtk_text_buffer_insert_at_cursor( buffer, "\n", -1 );
84 }
85 }
86 gtk_text_buffer_insert_at_cursor( buffer, "\n", -1 );
87 gtk_text_view_scroll_mark_onscreen( GTK_TEXT_VIEW( widget ), gtk_text_buffer_get_insert( buffer ) );
88 widget = lookup_widget( this_page_get_widget( this ), "entry_command" );
89 gtk_entry_set_text( GTK_ENTRY( widget ), "" );
90 gtk_widget_grab_focus( widget );
91 }
92 else
93 {
94 beep( );
95 }
96
97 return FALSE;
98 }
99
100 /** Main window - command tab - clear button pressed.
101 */
102
103 static gboolean on_command_cleared( GtkWidget *button, gpointer user_data )
104 {
105 page_command this = user_data;
106 GtkWidget *widget = lookup_widget( this_page_get_widget( this ), "textview_command" );
107 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW( widget ) );
108 gtk_text_buffer_set_text( buffer, "", -1 );
109 return FALSE;
110 }
111
112 static GtkWidget *this_page_get_widget( page_command this )
113 {
114 if ( this->widget == NULL )
115 this->widget = create_page_shell( );
116 return this->widget;
117 }
118
119 static void this_page_get_toolbar_info( page this, GtkIconSize size, GtkWidget **icon, char **label )
120 {
121 *icon = gtk_image_new_from_stock( "gtk-dialog-info", size );
122 *label = "_Shell";
123 }
124
125 static void this_on_connect( page_command this )
126 {
127 }
128
129 static void this_on_disconnect( page_command this )
130 {
131 }
132
133 static void this_close( page_command this )
134 {
135 if ( this != NULL )
136 free( this );
137 }
138
139 page page_command_init( dv1394app app )
140 {
141 GtkWidget *widget;
142 page_command this = calloc( 1, sizeof( struct page_command_t ) );
143
144 this->parent.get_widget = ( GtkWidget *(*)( page ) )this_page_get_widget;
145 this->parent.get_toolbar_info = this_page_get_toolbar_info;
146 this->parent.on_connect = ( void (*)( page ) )this_on_connect;
147 this->parent.on_disconnect = ( void (*)( page ) )this_on_disconnect;
148 this->parent.close = ( void (*)( page ) )this_close;
149 this->app = app;
150
151 /* Command execution handling */
152 widget = lookup_widget( this_page_get_widget( this ), "entry_command" );
153 gtk_signal_connect( GTK_OBJECT( widget ), "activate", GTK_SIGNAL_FUNC( on_command_pressed ), this );
154 widget = lookup_widget( this_page_get_widget( this ), "button_command" );
155 gtk_signal_connect( GTK_OBJECT( widget ), "clicked", GTK_SIGNAL_FUNC( on_command_pressed ), this );
156 widget = lookup_widget( this_page_get_widget( this ), "button_command_clear" );
157 gtk_signal_connect( GTK_OBJECT( widget ), "clicked", GTK_SIGNAL_FUNC( on_command_cleared ), this );
158
159 return ( page )this;
160 }
161