Going Fullscreen in Gtk+

athlon32 0 Tallied Votes 3K Views Share

This is just a little example of howto make a Gtk+ application go fullscreen. This is really useful in game programming. Please rate/comment/report errors :icon_mrgreen:

#include <gtk/gtk.h>

int main (int argc, char *argv[])
  {
    /*** The Widgets we'll be using ***/ 
    GtkWidget *win = NULL;
    GtkWidget *close = NULL;

    /*** Initialize GTK+ ***/
    g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
    gtk_init (&argc, &argv);
    g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);

    /*** Make that Window!!! ***/
    win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_container_set_border_width (GTK_CONTAINER (win), 10);
    gtk_widget_realize (win);
    gtk_window_fullscreen(win);

    /*** this is a button that'll help us close the window ***/
    close = gtk_button_new_with_label("Close Window");
    gtk_container_add(win, close);

    /*** Callbacks ***/
    g_signal_connect (close, "clicked", gtk_main_quit, NULL);
    g_signal_connect (win, "destroy", gtk_main_quit, NULL);

    /*** Enter the main loop ***/
    gtk_widget_show_all (win);
    gtk_main ();

    return 0;
}

Dani AI

Generated

Nice mini-example, . One gotcha that ran into: gtk_window_fullscreen() expects a GtkWindow*. If you keep the variable as GtkWidget*, cast with GTK_WINDOW(win) (preferred over a C-style cast). Also, you do not need gtk_widget_realize() for this; you can request fullscreen before showing the window and GTK will apply it once the window is mapped. Remember the request is asynchronous and may be refused by the window manager, so do not assume success. You can observe the state via the window-state-event signal. See the GTK 3 docs for details. GTK 3: gtk_window_fullscreen.

For a game-like UX, bind F11 to enter fullscreen and Esc to leave, and log transitions:

#include <gtk/gtk.h>

static gboolean on_key(GtkWidget *w, GdkEventKey *e, gpointer u) {
    if (e->keyval == GDK_KEY_F11) { gtk_window_fullscreen(GTK_WINDOW(w)); return TRUE; }
    if (e->keyval == GDK_KEY_Escape) { gtk_window_unfullscreen(GTK_WINDOW(w)); return TRUE; }
    return FALSE;
}

static gboolean on_state(GtkWidget *w, GdkEventWindowState *ev, gpointer u) {
    if (ev->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) g_print("Now fullscreen\n");
    else g_print("Exited fullscreen\n");
    return FALSE;
}

/* ... after creating your window ... */
g_signal_connect(win, "key-press-event", G_CALLBACK(on_key), NULL);
g_signal_connect(win, "window-state-event", G_CALLBACK(on_state), NULL);

To exit, call gtk_window_unfullscreen(). GTK 3: gtk_window_unfullscreen.

Multi-monitor tip: if you need a specific display, GTK 3.18+ adds gtk_window_fullscreen_on_monitor(). GTK 3: gtk_window_fullscreen_on_monitor. On GTK 4, the same operations exist and you can also watch the GtkWindow:fullscreened property for completion. GTK 4: gtk_window_fullscreen, GTK 4: GtkWindow:fullscreened.

Stefan_4 0 Newbie Poster

Soory but your code dosn't work for me. you have missed some casts I think.

#include <gtk/gtk.h>
int main (int argc, char *argv[])
{
  /*** The Widgets we'll be using ***/
  GtkWidget *win = NULL;
  GtkWidget *close = NULL;
  /*** Initialize GTK+ ***/
  g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
  gtk_init (&argc, &argv);
  g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);
  /*** Make that Window!!! ***/
  win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width (GTK_CONTAINER (win), 10);
  gtk_widget_realize (win);
  gtk_window_fullscreen((GtkWindow*)win);
  /*** this is a button that'll help us close the window ***/
  close = gtk_button_new_with_label("Close Window");
  gtk_container_add(GTK_CONTAINER(win), close);
  /*** Callbacks ***/
  g_signal_connect (close, "clicked", gtk_main_quit, NULL);
  g_signal_connect (win, "destroy", gtk_main_quit, NULL);
  /*** Enter the main loop ***/
  gtk_widget_show_all (win);
  gtk_main ();
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.