Search
lxdream.org :: lxdream/src/gtkui/gtk_win.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gtk_win.c
changeset 763:b3ce4448f200
prev736:a02d1475ccfd
next837:4eae2ddccf9c
author nkeynes
date Wed Jul 30 02:24:06 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Update translations again (minor line number changes)
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/gtkui/gtk_win.c Wed Jul 30 02:24:06 2008 +0000
1.3 @@ -0,0 +1,381 @@
1.4 +/**
1.5 + * $Id$
1.6 + *
1.7 + * Define the main (emu) GTK window, along with its menubars,
1.8 + * toolbars, etc.
1.9 + *
1.10 + * Copyright (c) 2005 Nathan Keynes.
1.11 + *
1.12 + * This program is free software; you can redistribute it and/or modify
1.13 + * it under the terms of the GNU General Public License as published by
1.14 + * the Free Software Foundation; either version 2 of the License, or
1.15 + * (at your option) any later version.
1.16 + *
1.17 + * This program is distributed in the hope that it will be useful,
1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.20 + * GNU General Public License for more details.
1.21 + */
1.22 +
1.23 +#include <assert.h>
1.24 +#include <sys/types.h>
1.25 +#include <sys/stat.h>
1.26 +#include <unistd.h>
1.27 +#include <string.h>
1.28 +#include <stdio.h>
1.29 +#include <stdlib.h>
1.30 +
1.31 +#include <gtk/gtk.h>
1.32 +#include <gdk/gdk.h>
1.33 +#include <gdk/gdkkeysyms.h>
1.34 +#include <X11/Xutil.h>
1.35 +
1.36 +#include "lxdream.h"
1.37 +#include "dreamcast.h"
1.38 +#include "display.h"
1.39 +#include "gtkui/gtkui.h"
1.40 +
1.41 +
1.42 +struct main_window_info {
1.43 + GtkWidget *window;
1.44 + GtkWidget *video;
1.45 + GtkWidget *menubar;
1.46 + GtkWidget *toolbar;
1.47 + GtkWidget *statusbar;
1.48 + GtkActionGroup *actions;
1.49 + gboolean use_grab;
1.50 + gboolean is_grabbed;
1.51 + int32_t mouse_x, mouse_y;
1.52 +};
1.53 +
1.54 +
1.55 +/******************** Video window **************************/
1.56 +
1.57 +/**
1.58 + * Adjust the mouse pointer so that it appears in the center of the video
1.59 + * window. Mainly used for when we have the mouse grab
1.60 + */
1.61 +void video_window_center_pointer( main_window_t win )
1.62 +{
1.63 + GdkDisplay *display = gtk_widget_get_display(win->video);
1.64 + GdkScreen *screen = gtk_widget_get_screen(win->video);
1.65 + int x,y;
1.66 + int width, height;
1.67 +
1.68 + gdk_window_get_origin(win->video->window, &x, &y);
1.69 + gdk_drawable_get_size(GDK_DRAWABLE(win->video->window), &width, &height);
1.70 + x += width / 2;
1.71 + y += height / 2;
1.72 +
1.73 + gdk_display_warp_pointer( display, screen, x, y );
1.74 + win->mouse_x = width/2;
1.75 + win->mouse_y = height/2;
1.76 +}
1.77 +
1.78 +/**
1.79 + * Grab the keyboard and mouse for the display. The mouse cursor is hidden and
1.80 + * moved to the centre of the window.
1.81 + *
1.82 + * @param win The window receiving the grab
1.83 + * @return TRUE if the grab was successful, FALSE on failure.
1.84 + */
1.85 +gboolean video_window_grab_display( main_window_t win )
1.86 +{
1.87 + GdkWindow *gdkwin = win->video->window;
1.88 + GdkColor color = { 0,0,0,0 };
1.89 + char bytes[32]; /* 16 * 16 / 8 */
1.90 + memset(bytes, 0, 32);
1.91 + GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bytes, 16, 16);
1.92 + GdkCursor *cursor = gdk_cursor_new_from_pixmap(pixmap, pixmap, &color, &color, 16, 16);
1.93 + gdk_pixmap_unref(pixmap);
1.94 +
1.95 + gboolean success =
1.96 + gdk_pointer_grab( gdkwin, FALSE,
1.97 + GDK_POINTER_MOTION_MASK|GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK,
1.98 + gdkwin, cursor, GDK_CURRENT_TIME ) == GDK_GRAB_SUCCESS;
1.99 + gdk_cursor_unref(cursor);
1.100 + if( success ) {
1.101 + success = gdk_keyboard_grab( gdkwin, FALSE, GDK_CURRENT_TIME ) == GDK_GRAB_SUCCESS;
1.102 + if( !success ) {
1.103 + gdk_pointer_ungrab(GDK_CURRENT_TIME);
1.104 + }
1.105 + }
1.106 + win->is_grabbed = success;
1.107 + main_window_set_running(win, dreamcast_is_running());
1.108 + return success;
1.109 +}
1.110 +
1.111 +/**
1.112 + * Release the display grab.
1.113 + */
1.114 +void video_window_ungrab_display( main_window_t win )
1.115 +{
1.116 + gdk_pointer_ungrab(GDK_CURRENT_TIME);
1.117 + gdk_keyboard_ungrab(GDK_CURRENT_TIME);
1.118 + win->is_grabbed = FALSE;
1.119 + main_window_set_running(win, dreamcast_is_running());
1.120 +}
1.121 +
1.122 +static gboolean on_video_window_mouse_motion( GtkWidget *widget, GdkEventMotion *event,
1.123 + gpointer user_data )
1.124 +{
1.125 + main_window_t win = (main_window_t)user_data;
1.126 + int32_t x = (int32_t)event->x;
1.127 + int32_t y = (int32_t)event->y;
1.128 + if( win->is_grabbed &&
1.129 + (x != win->mouse_x || y != win->mouse_y) ) {
1.130 + uint32_t buttons = (event->state >> 8)&0x1F;
1.131 + input_event_mouse( buttons, x - win->mouse_x, y - win->mouse_y );
1.132 + video_window_center_pointer(win);
1.133 + }
1.134 + return TRUE;
1.135 +}
1.136 +
1.137 +static gboolean on_video_window_mouse_pressed( GtkWidget *widget, GdkEventButton *event,
1.138 + gpointer user_data )
1.139 +{
1.140 + main_window_t win = (main_window_t)user_data;
1.141 + if( win->is_grabbed ) {
1.142 + // Get the buttons from the event state, and remove the released button
1.143 + uint32_t buttons = ((event->state >> 8) & 0x1F) | (1<<(event->button-1));
1.144 + input_event_mouse( buttons, 0, 0 );
1.145 + }
1.146 + return TRUE;
1.147 +}
1.148 +
1.149 +static gboolean on_video_window_mouse_released( GtkWidget *widget, GdkEventButton *event,
1.150 + gpointer user_data )
1.151 +{
1.152 + main_window_t win = (main_window_t)user_data;
1.153 + if( win->is_grabbed ) {
1.154 + // Get the buttons from the event state, and remove the released button
1.155 + uint32_t buttons = ((event->state >> 8) & 0x1F) & (~(1<<(event->button-1)));
1.156 + input_event_mouse( buttons, 0, 0 );
1.157 + } else if( win->use_grab) {
1.158 + video_window_grab_display(win);
1.159 + }
1.160 + return TRUE;
1.161 +}
1.162 +
1.163 +static gboolean on_video_window_key_pressed( GtkWidget *widget, GdkEventKey *event,
1.164 + gpointer user_data )
1.165 +{
1.166 + main_window_t win = (main_window_t)user_data;
1.167 + if( win->is_grabbed ) {
1.168 +#ifdef HAVE_GTK_OSX
1.169 + /* On OSX, use the command key rather than ctrl-alt. Mainly because GTK/OSX
1.170 + * doesn't seem to be able to get ctrl-alt reliably
1.171 + **/
1.172 + if( event->keyval == GDK_Meta_L || event->keyval == GDK_Meta_R ) {
1.173 + video_window_ungrab_display(win);
1.174 + return TRUE;
1.175 + }
1.176 +#else
1.177 + /* Check for ungrab key combo (ctrl-alt). Unfortunately GDK sends it as
1.178 + * a singly-modified keypress rather than a double-modified 'null' press,
1.179 + * so we have to do a little more work.
1.180 + * Only check Ctrl/Shift/Alt for state - don't want to check numlock/capslock/
1.181 + * mouse buttons/etc
1.182 + */
1.183 + int mod = gdk_keycode_to_modifier(gtk_widget_get_display(widget), event->hardware_keycode);
1.184 + int state = event->state & gtk_accelerator_get_default_mod_mask();
1.185 + if( (state == GDK_CONTROL_MASK && mod == GDK_MOD1_MASK) ||
1.186 + (state == GDK_MOD1_MASK && mod == GDK_CONTROL_MASK) ) {
1.187 + video_window_ungrab_display(win);
1.188 + // Consume the keypress, DC doesn't get it.
1.189 + return TRUE;
1.190 + }
1.191 +#endif
1.192 + }
1.193 + input_event_keydown( NULL, gtk_get_unmodified_keyval(event), 1 );
1.194 + return TRUE;
1.195 +}
1.196 +
1.197 +static gboolean on_video_window_key_released( GtkWidget *widget, GdkEventKey *event,
1.198 + gpointer user_data )
1.199 +{
1.200 + input_event_keyup( NULL, gtk_get_unmodified_keyval(event), 0 );
1.201 + return TRUE;
1.202 +}
1.203 +
1.204 +static gboolean on_video_window_focus_changed( GtkWidget *widget, GdkEventFocus *event,
1.205 + gpointer user_data )
1.206 +{
1.207 + display_set_focused(event->in);
1.208 + return TRUE;
1.209 +}
1.210 +
1.211 +/*************************** Main window (frame) ******************************/
1.212 +
1.213 +static gboolean on_main_window_deleted( GtkWidget *widget, GdkEvent event, gpointer user_data )
1.214 +{
1.215 + dreamcast_shutdown();
1.216 + exit(0);
1.217 +}
1.218 +
1.219 +static void on_main_window_state_changed( GtkWidget *widget, GdkEventWindowState *state,
1.220 + gpointer userdata )
1.221 +{
1.222 + main_window_t win = (main_window_t)userdata;
1.223 + if( state->changed_mask & GDK_WINDOW_STATE_FULLSCREEN ) {
1.224 + gboolean fs = (state->new_window_state & GDK_WINDOW_STATE_FULLSCREEN);
1.225 + GtkWidget *frame = gtk_widget_get_parent(win->video);
1.226 + if( frame->style == NULL ) {
1.227 + gtk_widget_set_style( frame, gtk_style_new() );
1.228 + }
1.229 + if( fs ) {
1.230 + gtk_widget_hide( win->menubar );
1.231 + gtk_widget_hide( win->toolbar );
1.232 + gtk_widget_hide( win->statusbar );
1.233 +
1.234 + frame->style->xthickness = 0;
1.235 + frame->style->ythickness = 0;
1.236 + } else {
1.237 + frame->style->xthickness = 2;
1.238 + frame->style->ythickness = 2;
1.239 + gtk_widget_show( win->menubar );
1.240 + gtk_widget_show( win->toolbar );
1.241 + gtk_widget_show( win->statusbar );
1.242 + }
1.243 + gtk_widget_queue_draw( win->window );
1.244 + }
1.245 +}
1.246 +
1.247 +main_window_t main_window_new( const gchar *title, GtkWidget *menubar, GtkWidget *toolbar,
1.248 + GtkAccelGroup *accel_group )
1.249 +{
1.250 + GtkWidget *vbox;
1.251 + GtkWidget *frame;
1.252 + main_window_t win = g_malloc0( sizeof(struct main_window_info) );
1.253 +
1.254 + win->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1.255 + win->menubar = menubar;
1.256 + win->toolbar = toolbar;
1.257 + win->use_grab = FALSE;
1.258 + win->is_grabbed = FALSE;
1.259 + gtk_window_set_title( GTK_WINDOW(win->window), title );
1.260 + gtk_window_add_accel_group (GTK_WINDOW (win->window), accel_group);
1.261 +
1.262 + gtk_toolbar_set_style( GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS );
1.263 +
1.264 + win->video = video_gtk_create_drawable();
1.265 + gtk_widget_set_size_request( win->video, 640, 480 );
1.266 + gtk_widget_set_double_buffered( win->video, FALSE );
1.267 + frame = gtk_frame_new(NULL);
1.268 + gtk_frame_set_shadow_type( GTK_FRAME(frame), GTK_SHADOW_IN );
1.269 + gtk_container_add( GTK_CONTAINER(frame), win->video );
1.270 +
1.271 + win->statusbar = gtk_statusbar_new();
1.272 +
1.273 + vbox = gtk_vbox_new(FALSE, 0);
1.274 + gtk_container_add( GTK_CONTAINER(win->window), vbox );
1.275 + gtk_box_pack_start( GTK_BOX(vbox), menubar, FALSE, FALSE, 0 );
1.276 + gtk_box_pack_start( GTK_BOX(vbox), toolbar, FALSE, FALSE, 0 );
1.277 + gtk_box_pack_start( GTK_BOX(vbox), frame, TRUE, TRUE, 0 );
1.278 + gtk_box_pack_start( GTK_BOX(vbox), win->statusbar, FALSE, FALSE, 0 );
1.279 + gtk_widget_show_all( win->window );
1.280 + gtk_widget_grab_focus( win->video );
1.281 +
1.282 + gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, "Stopped" );
1.283 +
1.284 + g_signal_connect( win->window, "delete_event",
1.285 + G_CALLBACK(on_main_window_deleted), win );
1.286 + g_signal_connect( win->window, "window-state-event",
1.287 + G_CALLBACK(on_main_window_state_changed), win );
1.288 +
1.289 + g_signal_connect( win->video, "key-press-event",
1.290 + G_CALLBACK(on_video_window_key_pressed), win );
1.291 + g_signal_connect( win->video, "key-release-event",
1.292 + G_CALLBACK(on_video_window_key_released), win );
1.293 + g_signal_connect( win->video, "motion-notify-event",
1.294 + G_CALLBACK(on_video_window_mouse_motion), win );
1.295 + g_signal_connect( win->video, "button-press-event",
1.296 + G_CALLBACK(on_video_window_mouse_pressed), win );
1.297 + g_signal_connect( win->video, "button-release-event",
1.298 + G_CALLBACK(on_video_window_mouse_released), win );
1.299 + g_signal_connect( win->video, "focus-in-event",
1.300 + G_CALLBACK(on_video_window_focus_changed), win);
1.301 + g_signal_connect( win->video, "focus-out-event",
1.302 + G_CALLBACK(on_video_window_focus_changed), win);
1.303 +
1.304 + gtk_widget_add_events( win->video,
1.305 + GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
1.306 + GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1.307 + GDK_POINTER_MOTION_MASK | GDK_FOCUS_CHANGE_MASK );
1.308 +
1.309 + return win;
1.310 +}
1.311 +
1.312 +void main_window_set_status_text( main_window_t win, char *text )
1.313 +{
1.314 + gtk_statusbar_pop( GTK_STATUSBAR(win->statusbar), 1 );
1.315 + if( win->is_grabbed ) {
1.316 + char buf[128];
1.317 +#ifdef HAVE_GTK_OSX
1.318 + snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <command> to release grab)") );
1.319 +#else
1.320 + snprintf( buf, sizeof(buf), "%s %s", text, _("(Press <ctrl><alt> to release grab)") );
1.321 +#endif
1.322 + gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, buf );
1.323 + } else {
1.324 + gtk_statusbar_push( GTK_STATUSBAR(win->statusbar), 1, text );
1.325 + }
1.326 +}
1.327 +
1.328 +void main_window_set_running( main_window_t win, gboolean running )
1.329 +{
1.330 + char *text = running ? _("Running") : _("Stopped");
1.331 + gtk_gui_enable_action( "Pause", running );
1.332 + gtk_gui_enable_action( "Run", !running && dreamcast_can_run() );
1.333 + main_window_set_status_text( win, text );
1.334 +}
1.335 +
1.336 +void main_window_set_framerate( main_window_t win, float rate )
1.337 +{
1.338 +
1.339 +
1.340 +}
1.341 +
1.342 +void main_window_set_speed( main_window_t win, double speed )
1.343 +{
1.344 + char buf[32];
1.345 +
1.346 + snprintf( buf, 32, "Running (%2.4f%%)", speed );
1.347 + main_window_set_status_text( win, buf );
1.348 +}
1.349 +
1.350 +GtkWidget *main_window_get_renderarea( main_window_t win )
1.351 +{
1.352 + return win->video;
1.353 +}
1.354 +
1.355 +GtkWindow *main_window_get_frame( main_window_t win )
1.356 +{
1.357 + return GTK_WINDOW(win->window);
1.358 +}
1.359 +
1.360 +void main_window_set_fullscreen( main_window_t win, gboolean fullscreen )
1.361 +{
1.362 + if( fullscreen ) {
1.363 + gtk_window_fullscreen( GTK_WINDOW(win->window) );
1.364 + } else {
1.365 + gtk_window_unfullscreen( GTK_WINDOW(win->window) );
1.366 + }
1.367 +}
1.368 +
1.369 +void main_window_set_use_grab( main_window_t win, gboolean use_grab )
1.370 +{
1.371 + if( use_grab != win->use_grab ) {
1.372 + if( use_grab ) {
1.373 + GdkCursor *cursor = gdk_cursor_new( GDK_HAND2 );
1.374 + gdk_window_set_cursor( win->video->window, cursor );
1.375 + gdk_cursor_unref( cursor );
1.376 + } else {
1.377 + gdk_window_set_cursor( win->video->window, NULL );
1.378 + if( gdk_pointer_is_grabbed() ) {
1.379 + video_window_ungrab_display(win);
1.380 + }
1.381 + }
1.382 + win->use_grab = use_grab;
1.383 + }
1.384 +}
.