testmultidisplay.c 4.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* testmultidisplay.c
 * Copyright (C) 2001 Sun Microsystems Inc.
 * Author: Erwann Chenede <erwann.chenede@sun.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

21
#include <config.h>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <gtk/gtk.h>
#include <gtk/gtkstock.h>
#include <gdk/gdk.h>

gchar *screen2_name = NULL;

typedef struct
{
  GtkEntry *e1;
  GtkEntry *e2;
}
MyDoubleGtkEntry;

void
get_screen_response (GtkDialog *dialog,
		     gint       response_id,
		     GtkEntry  *entry)
{
  if (response_id == GTK_RESPONSE_DELETE_EVENT)
    return;
42
  g_free (screen2_name);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  screen2_name = g_strdup (gtk_entry_get_text (entry));
}

void
entry_dialog_response (GtkDialog        *dialog,
		       gint              response_id,
		       MyDoubleGtkEntry *de)
{
  if (response_id == GTK_RESPONSE_APPLY)
    gtk_entry_set_text (de->e2, gtk_entry_get_text (de->e1));
  else
    gtk_main_quit ();
}

void
make_selection_dialog (GdkScreen * screen,
		       GtkWidget * entry,
		       GtkWidget * other_entry)
{
  GtkWidget *window, *vbox;
  MyDoubleGtkEntry *double_entry = g_new (MyDoubleGtkEntry, 1);
  double_entry->e1 = GTK_ENTRY (entry);
  double_entry->e2 = GTK_ENTRY (other_entry);

  if (!screen)
Owen Taylor's avatar
Owen Taylor committed
68
    screen = gdk_screen_get_default ();
69 70 71 72 73 74 75

  window = gtk_widget_new (GTK_TYPE_DIALOG,
			   "screen", screen,
			   "user_data", NULL,
			   "type", GTK_WINDOW_TOPLEVEL,
			   "title", "MultiDisplay Cut & Paste",
			   "border_width", 10, NULL);
Manish Singh's avatar
Manish Singh committed
76
  g_signal_connect (window, "destroy",
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
		    G_CALLBACK (gtk_main_quit), NULL);


  vbox = gtk_widget_new (GTK_TYPE_VBOX,
			 "border_width", 5,
			 NULL);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, FALSE, FALSE, 0);

  gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
  gtk_widget_grab_focus (entry);

  gtk_dialog_add_buttons (GTK_DIALOG (window),
			  GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
			  GTK_STOCK_QUIT, GTK_RESPONSE_DELETE_EVENT,
			  NULL);
  gtk_dialog_set_default_response (GTK_DIALOG (window), GTK_RESPONSE_APPLY);

Manish Singh's avatar
Manish Singh committed
94
  g_signal_connect (window, "response",
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		    G_CALLBACK (entry_dialog_response), double_entry);

  gtk_widget_show_all (window);
}

int
main (int argc, char *argv[])
{
  GtkWidget *dialog, *display_entry, *dialog_label;
  GtkWidget *entry, *entry2;
  GdkDisplay *dpy2;
  GdkScreen *scr2 = NULL;	/* Quiet GCC */
  gboolean correct_second_display = FALSE;

  gtk_init (&argc, &argv);

  if (argc == 2)
    screen2_name = g_strdup (argv[1]);
  
  /* Get the second display information */

  dialog = gtk_dialog_new_with_buttons ("Second Display Selection",
					NULL,
					GTK_DIALOG_MODAL,
					GTK_STOCK_OK,
					GTK_RESPONSE_OK, NULL);

  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
  display_entry = gtk_entry_new ();
  gtk_entry_set_activates_default (GTK_ENTRY (display_entry), TRUE);
  dialog_label =
    gtk_label_new ("Please enter the name of\nthe second display\n");

  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), dialog_label);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
		     display_entry);
Manish Singh's avatar
Manish Singh committed
131 132
  g_signal_connect (dialog, "response",
		    G_CALLBACK (get_screen_response), display_entry);
133 134 135 136 137 138 139 140

  gtk_widget_grab_focus (display_entry);
  gtk_widget_show_all (GTK_BIN (dialog)->child);
  
  while (!correct_second_display)
    {
      if (screen2_name)
	{
Manish Singh's avatar
Manish Singh committed
141
	  if (!g_ascii_strcasecmp (screen2_name, ""))
142 143
	    g_printerr ("No display name, reverting to default display\n");
	  
Owen Taylor's avatar
Owen Taylor committed
144
	  dpy2 = gdk_display_open (screen2_name);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	  if (dpy2)
	    {
	      scr2 = gdk_display_get_default_screen (dpy2);
	      correct_second_display = TRUE;
	    }
	  else
	    {
	      char *error_msg =
		g_strdup_printf  ("Can't open display :\n\t%s\nplease try another one\n",
				  screen2_name);
	      gtk_label_set_text (GTK_LABEL (dialog_label), error_msg);
	      g_free (error_msg);
	    }
       }

      if (!correct_second_display)
	gtk_dialog_run (GTK_DIALOG (dialog));
    }
  
  gtk_widget_destroy (dialog);

  entry = gtk_widget_new (GTK_TYPE_ENTRY,
			  "activates_default", TRUE,
			  "visible", TRUE,
			  NULL);
  entry2 = gtk_widget_new (GTK_TYPE_ENTRY,
			   "activates_default", TRUE,
			   "visible", TRUE,
			   NULL);

  /* for default display */
  make_selection_dialog (NULL, entry2, entry);
  /* for selected display */
  make_selection_dialog (scr2, entry, entry2);
  gtk_main ();

  return 0;
}