tests: Port to GdkMonitor

This commit is contained in:
Benjamin Otte 2016-09-04 21:13:41 +02:00
parent 76d95c312d
commit 29c570a660
2 changed files with 83 additions and 80 deletions

View File

@ -168,7 +168,8 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
GError *error = NULL; GError *error = NULL;
GdkScreen *screen; GdkDisplay *display;
GdkMonitor *monitor;
GdkRectangle monitor_bounds; GdkRectangle monitor_bounds;
GOptionContext *context = g_option_context_new (NULL); GOptionContext *context = g_option_context_new (NULL);
@ -204,10 +205,9 @@ main(int argc, char **argv)
G_CALLBACK (on_map_event), NULL); G_CALLBACK (on_map_event), NULL);
on_frame (0.); on_frame (0.);
screen = gtk_widget_get_screen (window); display = gtk_widget_get_display (window);
gdk_screen_get_monitor_geometry (screen, monitor = gdk_display_get_primary_monitor (display);
gdk_screen_get_primary_monitor (screen), gdk_monitor_get_geometry (monitor, &monitor_bounds);
&monitor_bounds);
gtk_window_move (GTK_WINDOW (window), gtk_window_move (GTK_WINDOW (window),
monitor_bounds.x + (monitor_bounds.width - window_width) / 2, monitor_bounds.x + (monitor_bounds.width - window_width) / 2,

View File

@ -20,107 +20,110 @@
#include <stdlib.h> #include <stdlib.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
static gint num_monitors; static void
static gint primary_monitor; label_set_text_for_monitor (GtkLabel *label,
GdkMonitor *monitor)
{
GdkRectangle monitor_geom;
gchar *str;
gdk_monitor_get_geometry (monitor, &monitor_geom);
str = g_strdup_printf ("<big><span foreground='white' background='black'>"
"%s %s</span></big>\n"
"<i>Width - Height </i>: (%d,%d)\n"
"<i>Top left coordinate </i>: (%d,%d)\n"
"<i>Primary monitor</i>: %s",
gdk_monitor_get_manufacturer (monitor),
gdk_monitor_get_model (monitor),
monitor_geom.width, monitor_geom.height,
monitor_geom.x, monitor_geom.y,
gdk_monitor_is_primary (monitor) ? "YES" : "no");
gtk_label_set_markup (label, str);
g_free (str);
}
static void static void
request (GtkWidget *widget, request (GtkWidget *widget,
gpointer user_data) gpointer user_data)
{ {
gchar *str; GdkDisplay *display = gtk_widget_get_display (widget);
GdkScreen *screen = gtk_widget_get_screen (widget); GdkMonitor *monitor = gdk_display_get_monitor_at_window (display,
gint i = gdk_screen_get_monitor_at_window (screen, gtk_widget_get_window (widget));
gtk_widget_get_window (widget));
if (i < 0) if (monitor == NULL)
str = g_strdup ("<big><span foreground='white' background='black'>Not on a monitor </span></big>"); gtk_label_set_markup (GTK_LABEL (user_data),
"<big><span foreground='white' background='black'>Not on a monitor </span></big>");
else else
{ label_set_text_for_monitor (GTK_LABEL (user_data), monitor);
GdkRectangle monitor;
gdk_screen_get_monitor_geometry (screen,
i, &monitor);
primary_monitor = gdk_screen_get_primary_monitor (screen);
str = g_strdup_printf ("<big><span foreground='white' background='black'>"
"Monitor %d of %d</span></big>\n"
"<i>Width - Height </i>: (%d,%d)\n"
"<i>Top left coordinate </i>: (%d,%d)\n"
"<i>Primary monitor: %d</i>",
i + 1, num_monitors,
monitor.width, monitor.height,
monitor.x, monitor.y,
primary_monitor);
}
gtk_label_set_markup (GTK_LABEL (user_data), str);
g_free (str);
} }
static void static void
monitors_changed_cb (GdkScreen *screen, monitor_changed_cb (GdkMonitor *monitor,
gpointer data) GParamSpec *pspec,
GtkWidget *label)
{ {
GtkWidget *label = (GtkWidget *)data;
request (label, label); request (label, label);
} }
static void
monitor_added (GdkDisplay *display,
GdkMonitor *monitor,
gpointer unused)
{
GtkWidget *window, *label, *vbox, *button;
GdkRectangle monitor_geom;
gdk_monitor_get_geometry (monitor, &monitor_geom);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_window_move (GTK_WINDOW (window), (monitor_geom.width - 200) / 2 + monitor_geom.x,
(monitor_geom.height - 200) / 2 + monitor_geom.y);
label = gtk_label_new (NULL);
label_set_text_for_monitor (GTK_LABEL (label), monitor);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 1);
gtk_box_set_homogeneous (GTK_BOX (vbox), TRUE);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_container_add (GTK_CONTAINER (vbox), label);
button = gtk_button_new_with_label ("Query current monitor");
g_signal_connect (button, "clicked", G_CALLBACK (request), label);
gtk_container_add (GTK_CONTAINER (vbox), button);
button = gtk_button_new_with_label ("Close");
g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_add (GTK_CONTAINER (vbox), button);
gtk_widget_show_all (window);
g_signal_connect (monitor, "notify",
G_CALLBACK (monitor_changed_cb), label);
g_signal_connect_swapped (monitor, "invalidate",
G_CALLBACK (gtk_widget_destroy), window);
}
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
GtkWidget *window, *label, *vbox, *button; GdkDisplay *display;
GdkScreen *screen; gint i, num_monitors;
gint i;
gtk_init (&argc, &argv); gtk_init (&argc, &argv);
screen = gdk_screen_get_default (); display = gdk_display_get_default ();
num_monitors = gdk_screen_get_n_monitors (screen); g_signal_connect (display, "monitor-added", G_CALLBACK (monitor_added), NULL);
num_monitors = gdk_display_get_n_monitors (display);
if (num_monitors == 1) if (num_monitors == 1)
g_warning ("The default screen of the current display only has one monitor."); g_warning ("The current display only has one monitor.");
primary_monitor = gdk_screen_get_primary_monitor (screen);
for (i = 0; i < num_monitors; i++) for (i = 0; i < num_monitors; i++)
{ {
GdkRectangle monitor; GdkMonitor *monitor;
gchar *str;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL); monitor = gdk_display_get_monitor (display, i);
monitor_added (display, monitor, NULL);
gdk_screen_get_monitor_geometry (screen, i, &monitor);
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_window_move (GTK_WINDOW (window), (monitor.width - 200) / 2 + monitor.x,
(monitor.height - 200) / 2 + monitor.y);
label = gtk_label_new (NULL);
str = g_strdup_printf ("<big><span foreground='white' background='black'>"
"Monitor %d of %d</span></big>\n"
"<i>Width - Height </i>: (%d,%d)\n"
"<i>Top left coordinate </i>: (%d,%d)\n"
"<i>Primary monitor: %d</i>",
i + 1, num_monitors,
monitor.width, monitor.height,
monitor.x, monitor.y,
primary_monitor);
gtk_label_set_markup (GTK_LABEL (label), str);
g_free (str);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 1);
gtk_box_set_homogeneous (GTK_BOX (vbox), TRUE);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_container_add (GTK_CONTAINER (vbox), label);
button = gtk_button_new_with_label ("Query current monitor");
g_signal_connect (button, "clicked", G_CALLBACK (request), label);
gtk_container_add (GTK_CONTAINER (vbox), button);
button = gtk_button_new_with_label ("Close");
g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_add (GTK_CONTAINER (vbox), button);
gtk_widget_show_all (window);
g_signal_connect (screen, "monitors-changed",
G_CALLBACK (monitors_changed_cb), label);
} }
gtk_main (); gtk_main ();