source: svn/trunk/newcon3bcm2_21bu/dta/src/sim/gtk_app.c @ 2

Last change on this file since 2 was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
  • Property svn:executable set to *
File size: 7.8 KB
Line 
1#include <gtk/gtk.h>
2#include <assert.h>
3#include <stdlib.h>
4#include <string.h>
5#include "gtk_app.h"
6#include "gtk_cb.h"
7#include "gtk_app_p.h"
8#include "sim.h"
9
10//#define REDIRECT_STDOUT_TO_TEXTVIEW
11
12static struct gtk_app_t s_gtk_app;
13
14void gtk_app_update_osd(gtk_app_h h_gtk_app)
15{
16        APP_ASSERT(h_gtk_app);
17        APP_ASSERT(h_gtk_app->osd_widget);
18        APP_ASSERT(h_gtk_app->pixbuf);
19        gdk_threads_enter();
20        gtk_image_set_from_pixbuf(GTK_IMAGE(h_gtk_app->osd_widget), h_gtk_app->pixbuf );
21        gdk_threads_leave();
22}
23
24void gtk_app_get_osd_info(gtk_app_h h_gtk_app,
25                                                  unsigned char **pixels, int *pitch, int *width, int *height)
26{
27        APP_ASSERT(h_gtk_app);
28        APP_ASSERT(h_gtk_app->pixbuf);
29        APP_ASSERT(pixels);
30        APP_ASSERT(pitch);
31        APP_ASSERT(width);
32        APP_ASSERT(height);
33        gdk_threads_enter();
34        *pixels = gdk_pixbuf_get_pixels(h_gtk_app->pixbuf);
35        *pitch = gdk_pixbuf_get_rowstride(h_gtk_app->pixbuf);
36        *width = gdk_pixbuf_get_width(h_gtk_app->pixbuf);
37        *height = gdk_pixbuf_get_height(h_gtk_app->pixbuf);
38        gdk_threads_leave();
39}
40
41
42/* This is a callback function to watch data in standard output and write to a view text widget. */
43void gtk_app_input_callback( gpointer          data,
44                     gint              source,
45                     GdkInputCondition condition )
46{
47        gtk_app_h h_gtk_app = (gtk_app_h)data;
48    static gchar buf[1024];
49    gint chars_read;
50    GtkTextIter iter;
51    chars_read = 1024;
52        gdk_threads_enter();
53    gtk_text_buffer_get_end_iter(h_gtk_app->out_buffer, &iter);
54        gdk_threads_leave();
55
56    while (chars_read == 1024)
57        {
58      chars_read = read(h_gtk_app->fds_out[0], buf, 1024);
59      fprintf(stderr, "%i\n", chars_read);
60     // fprintf(stderr, "%i chars: %s\n", chars_read, buf);
61          gdk_threads_enter();
62      gtk_text_buffer_insert (h_gtk_app->out_buffer, &iter, buf, chars_read);
63          gdk_threads_leave();
64    }
65}
66
67
68static gboolean gtk_app_delete_event( GtkWidget *widget,
69                                                                          GdkEvent  *event,
70                                                                          gpointer   data )
71{
72        /* If you return FALSE in the "delete-event" signal handler,
73         * GTK will emit the "destroy" signal. Returning TRUE means
74         * you don't want the window to be destroyed.
75         * This is useful for popping up 'are you sure you want to quit?'
76         * type dialogs. */
77
78        g_print ("delete event occurred\n");
79
80        /* Change TRUE to FALSE and the main window will be destroyed with
81         * a "delete-event". */
82
83        return TRUE;
84}
85
86/* Another callback */
87static void gtk_app_destroy( GtkWidget *widget,
88                                                         gpointer   data )
89{
90        gtk_app_h h_gtk_app = (gtk_app_h)data;
91        gtk_app_done(h_gtk_app);
92}
93
94static int gtk_app_open_doc(gtk_app_h h_gtk_app)
95{
96        GtkWidget *dialog;
97        int result = -1;
98
99        dialog = gtk_file_chooser_dialog_new ("Open File",
100                                                                                  NULL,
101                                                                                  GTK_FILE_CHOOSER_ACTION_OPEN,
102                                                                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
103                                                                                  GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
104                                                                                  NULL);
105
106        if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
107        {
108                char *filename;
109
110                filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
111                // gtk_app_add_doc_item(h_gtk_app,filename);
112                g_free (filename);
113                result = 0;
114        }
115
116        gtk_widget_destroy (dialog);
117        return result;
118}
119
120#define MAX_DESC_LEN 64
121GtkListStore* gtk_app_screen_liststore(gtk_app_h h_gtk_app)
122{
123        GtkTreeIter    iter;
124        int i;
125        GtkListStore *list_store;
126
127        list_store = gtk_list_store_new(2,G_TYPE_INT, G_TYPE_STRING);
128       
129        for (i = 0; i < sim_screen_num(h_gtk_app->h_sim); ++i) 
130        {
131                char desc[MAX_DESC_LEN+1];
132                sim_screen_get_desc(h_gtk_app->h_sim,i,desc,MAX_DESC_LEN);
133                /* Append a row and fill in some data */
134                gtk_list_store_append (list_store, &iter);
135                gtk_list_store_set (list_store, &iter, 
136                                                        0, i,
137                                                        1, desc,
138                                                         -1);
139
140        }
141
142        return list_store;
143}
144
145
146void gtk_app_screen_populate(gtk_app_h h_gtk_app)
147{
148        GtkListStore *list_store;
149    list_store = gtk_app_screen_liststore(h_gtk_app);
150       
151        gtk_tree_view_set_model (GTK_TREE_VIEW (h_gtk_app->screen_list), GTK_TREE_MODEL(list_store));
152        g_object_unref(list_store);
153}
154
155
156static void gtk_app_sim_init(gtk_app_h h_gtk_app)
157{
158    GError     *error = NULL;
159    GtkBuilder *builder;
160        GtkCellRenderer     *renderer;
161        GtkTreeSelection *tvs;
162        int width,height;
163       
164        width = 720; height = 480;
165
166        /* Create new GtkBuilder object */
167        builder = gtk_builder_new();
168
169        /* Load UI from file. If error occurs, report it and quit application.
170         * Replace "tut.glade" with your saved project. */
171        if ( ! gtk_builder_add_from_file( builder, "../src/sim/sim.glade", &error ) )
172        {
173                g_warning( "%s", error->message );
174                g_free( error );
175                g_object_unref( G_OBJECT( builder ) );
176                return;
177        }
178
179        /* Get widget pointers */
180        h_gtk_app->window = GTK_WIDGET( gtk_builder_get_object( builder, "sim_window" ) );
181        APP_ASSERT(h_gtk_app->window);
182       
183        h_gtk_app->osd_widget = GTK_WIDGET( gtk_builder_get_object( builder, "OSD" ) );
184        APP_ASSERT(h_gtk_app->osd_widget);
185
186        h_gtk_app->remote = GTK_WIDGET( gtk_builder_get_object( builder, "remote_eventbox" ) );
187        APP_ASSERT(h_gtk_app->remote);
188       
189        h_gtk_app->about_dlg = GTK_DIALOG( gtk_builder_get_object( builder, "about_dialog" ) );
190        APP_ASSERT(h_gtk_app->about_dlg);
191
192#ifdef REDIRECT_STDOUT_TO_TEXTVIEW
193        h_gtk_app->out_textview = GTK_TEXT_VIEW( gtk_builder_get_object( builder, "output_textview" ) );
194        APP_ASSERT(h_gtk_app->out_textview);
195    h_gtk_app->out_buffer = gtk_text_view_get_buffer(h_gtk_app->out_textview);
196        APP_ASSERT(h_gtk_app->out_buffer);
197    gtk_text_buffer_set_text (h_gtk_app->out_buffer, "Hello, this is some text", -1);
198#endif
199        h_gtk_app->screen_list = GTK_TREE_VIEW( gtk_builder_get_object( builder, "screen_treeview" ) );
200        APP_ASSERT(h_gtk_app->screen_list);
201
202        tvs = gtk_tree_view_get_selection(h_gtk_app->screen_list);
203        gtk_tree_selection_set_mode(tvs,GTK_SELECTION_SINGLE);
204    g_signal_connect(h_gtk_app->screen_list, "row-activated", (GCallback) gtk_cb_screen_select, h_gtk_app);
205
206       
207        renderer = gtk_cell_renderer_text_new ();
208        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (h_gtk_app->screen_list),
209                                                                                                 0,     
210                                                                                                 "id", 
211                                                                                                 renderer,
212                                                                                                 "text", 0,
213                                                                                                 NULL);
214        renderer = gtk_cell_renderer_text_new ();
215        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (h_gtk_app->screen_list),
216                                                                                                 1,     
217                                                                                                 "Name", 
218                                                                                                 renderer,
219                                                                                                 "text", 1,
220                                                                                                 NULL);
221
222        gtk_confirm_dlg_init(&h_gtk_app->confirm_dlg,builder);
223
224        gtk_alert_dlg_init(&h_gtk_app->alert_dlg,builder);
225
226
227        gtk_widget_add_events(h_gtk_app->remote,GDK_BUTTON_PRESS_MASK);
228
229        g_signal_connect (G_OBJECT (h_gtk_app->remote), "button-press-event",
230                                          G_CALLBACK (gtk_cb_remote_clicked), h_gtk_app);
231
232#ifdef REDIRECT_STDOUT_TO_TEXTVIEW
233        gdk_input_add (h_gtk_app->fds_out[0],GDK_INPUT_READ,gtk_app_input_callback,h_gtk_app);
234#endif
235
236        /* Connect signals */
237        gtk_builder_connect_signals( builder, NULL );
238
239        gtk_cb_menu_init(h_gtk_app,builder);
240
241        g_object_unref( G_OBJECT( builder ) );
242
243        h_gtk_app->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,TRUE,8,width,height);
244        APP_ASSERT(h_gtk_app->pixbuf);
245
246        gtk_app_screen_populate(h_gtk_app);
247
248        /* Show window. All other widgets are automatically shown by GtkBuilder */
249        gtk_widget_show( h_gtk_app->window );
250
251        h_gtk_app->state = eAPP_STATE_INITIALIZED;
252}
253
254gtk_app_h gtk_app_init(int   argc, char *argv[])
255{
256        memset(&s_gtk_app,0,sizeof(s_gtk_app));
257        g_thread_init(NULL);
258    gdk_threads_init();
259
260        /* Init GTK+ */
261        gtk_init( &argc, &argv );
262
263        s_gtk_app.state = eAPP_STATE_INITIALIZED;
264
265        return &s_gtk_app;
266}
267void gtk_app_done(gtk_app_h h_gtk_app)
268{
269        gtk_main_quit ();
270}
271
272
273void gtk_app_run(gtk_app_h h_gtk_app)
274{
275#ifdef REDIRECT_STDOUT_TO_TEXTVIEW
276        pipe(h_gtk_app->fds_out);
277       
278        dup2(h_gtk_app->fds_out[1],STDOUT_FILENO);
279#endif
280        gtk_app_sim_init(h_gtk_app);
281
282        h_gtk_app->h_sim = sim_init(h_gtk_app);
283
284        /* Start main loop */
285        gdk_threads_enter();
286        gtk_main();
287        gdk_threads_leave();
288        h_gtk_app->state = eAPP_STATE_DEFAULT;
289}
290void gtk_app_new(void)
291{
292}
293void gtk_app_open(void)
294{
295}
296void gtk_app_quit(void)
297{
298        gtk_app_done(&s_gtk_app);
299}
300
Note: See TracBrowser for help on using the repository browser.