Search
lxdream.org :: lxdream/src/gtkui/main_win.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/main_win.c
changeset 606:23029426ab8f
prev561:533f6b478071
next608:4f588e52bce0
author nkeynes
date Fri Jan 25 05:52:51 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Detect if we failed to obtain a GLX visual and abort rather than crashing
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Define the main (emu) GTK window, along with its menubars,
     5  * toolbars, etc.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #include <assert.h>
    21 #include <sys/types.h>
    22 #include <sys/stat.h>
    23 #include <unistd.h>
    24 #include <string.h>
    25 #include <stdio.h>
    26 #include <stdlib.h>
    28 #include <gtk/gtk.h>
    29 #include <gdk/gdkx.h>
    30 #include <X11/Xutil.h>
    32 #include "dream.h"
    33 #include "gtkui/gtkui.h"
    34 #include "drivers/video_glx.h"
    37 struct main_window_info {
    38     GtkWidget *window;
    39     GtkWidget *video;
    40     GtkWidget *menubar;
    41     GtkWidget *toolbar;
    42     GtkWidget *statusbar;
    43     GtkActionGroup *actions;
    44 };
    46 static gboolean on_main_window_deleted( GtkWidget *widget, GdkEvent event, gpointer user_data )
    47 {
    48     exit(0);
    49 }
    51 static void on_main_window_state_changed( GtkWidget *widget, GdkEventWindowState *state, 
    52 					  gpointer userdata )
    53 {
    54     main_window_t win = (main_window_t)userdata;
    55     if( state->changed_mask & GDK_WINDOW_STATE_FULLSCREEN ) {
    56 	gboolean fs = (state->new_window_state & GDK_WINDOW_STATE_FULLSCREEN);
    57 	GtkWidget *frame = gtk_widget_get_parent(win->video);
    58 	if( frame->style == NULL ) {
    59 	    gtk_widget_set_style( frame, gtk_style_new() );
    60 	}
    61 	if( fs ) {
    62 	    gtk_widget_hide( win->menubar );
    63 	    gtk_widget_hide( win->toolbar );
    64 	    gtk_widget_hide( win->statusbar );
    66 	    frame->style->xthickness = 0;
    67 	    frame->style->ythickness = 0;
    68 	} else {
    69 	    frame->style->xthickness = 2;
    70 	    frame->style->ythickness = 2;
    71 	    gtk_widget_show( win->menubar );
    72 	    gtk_widget_show( win->toolbar );
    73 	    gtk_widget_show( win->statusbar );
    74 	}
    75 	gtk_widget_queue_draw( win->window );
    76     }
    77 }
    79 main_window_t main_window_new( const gchar *title, GtkWidget *menubar, GtkWidget *toolbar,
    80 			       GtkAccelGroup *accel_group )
    81 {
    82     GtkWidget *vbox;
    83     GtkWidget *frame;
    84     main_window_t win = g_malloc0( sizeof(struct main_window_info) );
    86     win->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    87     win->menubar = menubar;
    88     win->toolbar = toolbar;
    89     gtk_window_set_title( GTK_WINDOW(win->window), title );
    90     gtk_window_add_accel_group (GTK_WINDOW (win->window), accel_group);
    92     gtk_toolbar_set_style( GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS );
    94     Display *display = gdk_x11_display_get_xdisplay( gtk_widget_get_display(win->window));
    95     Screen *screen = gdk_x11_screen_get_xscreen( gtk_widget_get_screen(win->window));
    96     int screen_no = XScreenNumberOfScreen(screen);
    97     if( !video_glx_init(display, screen_no) ) {
    98         ERROR( "Unable to initialize GLX, aborting" );
    99 	exit(3);
   100     }
   102     XVisualInfo *visual = video_glx_get_visual();
   103     GdkVisual *gdkvis = gdk_x11_screen_lookup_visual( gtk_widget_get_screen(win->window), visual->visualid );
   104     GdkColormap *colormap = gdk_colormap_new( gdkvis, FALSE );
   105     win->video = gtk_drawing_area_new();
   106     gtk_widget_set_colormap( win->video, colormap );
   107     GTK_WIDGET_SET_FLAGS(win->video, GTK_CAN_FOCUS|GTK_CAN_DEFAULT);
   108     gtk_widget_set_size_request( win->video, 640, 480 ); 
   109     frame = gtk_frame_new(NULL);
   110     gtk_frame_set_shadow_type( GTK_FRAME(frame), GTK_SHADOW_IN );
   111     gtk_container_add( GTK_CONTAINER(frame), win->video );
   113     win->statusbar = gtk_statusbar_new();
   115     vbox = gtk_vbox_new(FALSE, 0);
   116     gtk_container_add( GTK_CONTAINER(win->window), vbox );
   117     gtk_box_pack_start( GTK_BOX(vbox), menubar, FALSE, FALSE, 0 );
   118     gtk_box_pack_start( GTK_BOX(vbox), toolbar, FALSE, FALSE, 0 );
   119     gtk_box_pack_start( GTK_BOX(vbox), frame, TRUE, TRUE, 0 );
   120     gtk_box_pack_start( GTK_BOX(vbox), win->statusbar, FALSE, FALSE, 0 );
   121     gtk_widget_show_all( win->window );
   122     gtk_widget_grab_focus( win->video );
   124     gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, "Stopped" );
   125     g_signal_connect( win->window, "delete_event", 
   126 		      G_CALLBACK(on_main_window_deleted), win );
   127     g_signal_connect( win->window, "window-state-event",
   128 		      G_CALLBACK(on_main_window_state_changed), win );
   129     return win;
   130 }
   132 void main_window_set_running( main_window_t win, gboolean running )
   133 {
   134     gtk_gui_enable_action( "Pause", running );
   135     gtk_gui_enable_action( "Run", !running && dreamcast_can_run() );
   136     gtk_statusbar_pop( GTK_STATUSBAR(win->statusbar), 1 );
   137     gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, running ? "Running" : "Stopped" );
   138 }
   140 void main_window_set_framerate( main_window_t win, float rate )
   141 {
   144 }
   146 void main_window_set_speed( main_window_t win, double speed )
   147 {
   148     char buf[32];
   150     snprintf( buf, 32, "Running (%2.4f%%)", speed );
   151     gtk_statusbar_pop( GTK_STATUSBAR(win->statusbar), 1 );
   152     gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, buf );
   155 }
   157 GtkWidget *main_window_get_renderarea( main_window_t win )
   158 {
   159     return win->video;
   160 }
   162 GtkWindow *main_window_get_frame( main_window_t win )
   163 {
   164     return GTK_WINDOW(win->window);
   165 }
   167 void main_window_set_fullscreen( main_window_t win, gboolean fullscreen )
   168 {
   169     if( fullscreen ) {
   170 	gtk_window_fullscreen( GTK_WINDOW(win->window) );
   171     } else {
   172 	gtk_window_unfullscreen( GTK_WINDOW(win->window) );
   173     }
   174 }
.