Changeset 825


Ignore:
Timestamp:
Aug 13, 2011, 10:05:53 PM (8 years ago)
Author:
sam
Message:

gtk: refactor the GTK mess once again.

Location:
trunk/deushax/gtk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/deushax/gtk/editor.cpp

    r824 r825  
    2222
    2323#include "glmapview.h"
    24 
    25 /* Closing the window does not quit directly, we must first make sure the
    26  * GL widget is destroyed cleanly. */
    27 static gboolean close_window(GtkWidget *w, GdkEvent *e, void *data)
    28 {
    29     (void)w;
    30     (void)e;
    31     GlMapView *glmapview = (GlMapView *)data;
    32     glmapview->Shutdown();
    33     gtk_main_quit();
    34     return FALSE;
    35 }
    3624
    3725int main(int argc, char **argv)
     
    6048
    6149    /* Add our custom GL map viewer */
    62     GlMapView *glmapview = new GlMapView(builder);
     50    GlMapView *glmapview = new GlMapView(
     51           GTK_CONTAINER(gtk_builder_get_object(builder, "gl_container")),
     52           GTK_ADJUSTMENT(gtk_builder_get_object(builder, "gl_hadj")),
     53           GTK_ADJUSTMENT(gtk_builder_get_object(builder, "gl_vadj")));
    6354
    6455    /* Show window. We're good to go! */
    6556    GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
    6657    gtk_widget_show_all(window);
    67     gtk_signal_connect(GTK_OBJECT(window), "delete_event",
    68                        GTK_SIGNAL_FUNC(close_window), glmapview);
     58    g_signal_connect(GTK_OBJECT(window), "delete_event",
     59                     GTK_SIGNAL_FUNC(gtk_main_quit), glmapview);
     60    g_signal_connect(GTK_OBJECT(window), "delete_event",
     61                     GTK_SIGNAL_FUNC(gtk_main_quit), glmapview2);
    6962    g_object_unref(G_OBJECT(builder));
    7063
  • trunk/deushax/gtk/glmapview.cpp

    r824 r825  
    2020static float const FPS = 30.0f;
    2121
    22 GlMapView::GlMapView(GtkBuilder *builder)
    23   : hadj(GTK_ADJUSTMENT(gtk_builder_get_object(builder, "gl_hadj"))),
    24     vadj(GTK_ADJUSTMENT(gtk_builder_get_object(builder, "gl_vadj"))),
     22GlMapView::GlMapView(GtkContainer *in_container,
     23                     GtkAdjustment *in_hadj,
     24                     GtkAdjustment *in_vadj)
     25  : hadj(in_hadj), vadj(in_vadj),
    2526    ticking(FALSE), panning(FALSE), destroyed(FALSE),
    2627    mapviewer(0),
     
    4748    gtk_widget_set_can_focus(glarea, TRUE);
    4849
    49     GtkContainer *cont = GTK_CONTAINER(gtk_builder_get_object(builder,
    50                                                               "gl_container"));
    51     gtk_container_add(cont, glarea);
     50    gtk_container_add(in_container, glarea);
    5251
    5352    /* We tick from the idle function instead of a timeout to avoid
     
    5655    g_idle_add((GSourceFunc)IdleTickSignal, this);
    5756
    58     /* We must divert gtk_main_quit() to release our resources when the
    59      * GL widget is still realised. We'll call gtk_main_quit() when we
    60      * are sure that everything has been cleaned. */
    61     gtk_quit_add(0, (GtkFunction)ShutdownSignal, this);
    62 
    63     gtk_signal_connect(GTK_OBJECT(glarea), "realize",
    64                        GTK_SIGNAL_FUNC(SetupSignal), this);
    65     gtk_signal_connect(GTK_OBJECT(glarea), "expose_event",
    66                        GTK_SIGNAL_FUNC(DrawSignal), this);
    67     gtk_signal_connect(GTK_OBJECT(glarea), "configure_event",
    68                        GTK_SIGNAL_FUNC(ReshapeSignal), this);
    69 
    70     gtk_signal_connect(GTK_OBJECT(glarea), "button_press_event",
    71                        GTK_SIGNAL_FUNC(MouseButtonSignal), this);
    72     gtk_signal_connect(GTK_OBJECT(glarea), "button_release_event",
    73                        GTK_SIGNAL_FUNC(MouseButtonSignal), this);
    74     gtk_signal_connect(GTK_OBJECT(glarea), "motion_notify_event",
    75                        GTK_SIGNAL_FUNC(MouseMotionSignal), this);
    76 
    77     gtk_signal_connect(GTK_OBJECT(glarea), "key_press_event",
    78                        GTK_SIGNAL_FUNC(KeyPressSignal), this);
     57    /* FIXME: for completeness, the "hierarchy_changed" signal should be
     58     * intercepted in case we ever get reparented. */
     59    g_signal_connect(GTK_OBJECT(glarea), "realize",
     60                     GTK_SIGNAL_FUNC(SetupSignal), this);
     61    g_signal_connect(GTK_OBJECT(glarea), "expose_event",
     62                     GTK_SIGNAL_FUNC(DrawSignal), this);
     63    g_signal_connect(GTK_OBJECT(glarea), "configure_event",
     64                     GTK_SIGNAL_FUNC(ReshapeSignal), this);
     65
     66    g_signal_connect(GTK_OBJECT(glarea), "button_press_event",
     67                     GTK_SIGNAL_FUNC(MouseButtonSignal), this);
     68    g_signal_connect(GTK_OBJECT(glarea), "button_release_event",
     69                     GTK_SIGNAL_FUNC(MouseButtonSignal), this);
     70    g_signal_connect(GTK_OBJECT(glarea), "motion_notify_event",
     71                     GTK_SIGNAL_FUNC(MouseMotionSignal), this);
     72
     73    g_signal_connect(GTK_OBJECT(glarea), "key_press_event",
     74                     GTK_SIGNAL_FUNC(KeyPressSignal), this);
     75}
     76
     77GlMapView::~GlMapView()
     78{
     79    Shutdown();
    7980}
    8081
     
    106107    }
    107108
    108     ticking = TRUE;
    109 
    110     if (mapviewer)
    111         mapviewer->SetPOV(gtk_adjustment_get_value(hadj),
    112                           mapviewer->GetHeight() - glarea->allocation.height
    113                            - gtk_adjustment_get_value(vadj));
    114 
    115     /* Tick the game */
    116     Ticker::TickGame();
     109    if (!ticking)
     110    {
     111        ticking = TRUE;
     112
     113        if (mapviewer)
     114            mapviewer->SetPOV(gtk_adjustment_get_value(hadj),
     115                              mapviewer->GetHeight() - glarea->allocation.height
     116                               - gtk_adjustment_get_value(vadj));
     117
     118        /* Tick the game */
     119        Ticker::TickGame();
     120    }
    117121
    118122    gtk_widget_draw(GTK_WIDGET(glarea), NULL);
     
    123127gboolean GlMapView::Setup()
    124128{
     129    /* Hook to the toplevel's unmap signal */
     130    GtkWidget *toplevel = gtk_widget_get_toplevel(glarea);
     131    if (g_signal_handler_find(toplevel, G_SIGNAL_MATCH_DATA,
     132                              0, 0, NULL, NULL, this) == 0)
     133        g_signal_connect(GTK_OBJECT(toplevel), "delete_event",
     134                         GTK_SIGNAL_FUNC(DeleteSignal), this);
     135
    125136    /* Set up display */
    126137    gtk_widget_grab_focus(glarea);
     
    137148}
    138149
     150/* This method is called when gtk_main_quit() is called, or when the toplevel
     151 * widget is destroyed, or both. */
    139152gboolean GlMapView::Shutdown()
    140153{
     154    /* Make sure we don't initiate shutdown twice. It could happen if we
     155     * got the delete signal first, then the gtk_main_quit() signal. In
     156     * that case, ignore the latter. */
    141157    if (!destroyed)
    142158    {
     
    145161        Ticker::Shutdown();
    146162        gtk_widget_set_sensitive(gtk_widget_get_toplevel(glarea), FALSE);
    147         /* Hijack the exit sequence by adding another level of gtk_main */
     163        /* Hijack the exit sequence by adding another level of gtk_main,
     164         * because returning too soon may leave us no time to clean up. */
    148165        gtk_main();
    149166    }
     
    167184            ;
    168185
    169         // FIXME: do some GTK stuff in here
     186        /* FIXME: do some GTK stuff instead of waiting for so long */
    170187        Ticker::ClampFps();
    171188    }
     
    272289}
    273290
    274 gboolean GlMapView::ShutdownSignal(GlMapView *that)
    275 {
     291gboolean GlMapView::DeleteSignal(GtkWidget *w, GdkEvent *e,
     292                                 GlMapView *that)
     293{
     294    /* XXX: w is not glarea, it's the top level window */
     295    (void)w;
     296    (void)e;
    276297    return that->Shutdown();
    277298}
  • trunk/deushax/gtk/glmapview.h

    r824 r825  
    1414{
    1515public:
    16     GlMapView(GtkBuilder *builder);
     16    GlMapView(GtkContainer *in_container,
     17              GtkAdjustment *in_hadj, GtkAdjustment *in_vadj);
     18    ~GlMapView();
    1719    void LoadMap(char const *path);
    1820    void CloseMap();
     
    3335    static gboolean IdleTickSignal(GlMapView *that);
    3436    static gboolean SetupSignal(GtkWidget *w, GlMapView *that);
    35     static gboolean ShutdownSignal(GlMapView *that);
     37    static gboolean DeleteSignal(GtkWidget *w, GdkEvent *e, GlMapView *that);
    3638    static gboolean DrawSignal(GtkWidget *w, GdkEventExpose *e,
    3739                               GlMapView *that);
Note: See TracChangeset for help on using the changeset viewer.