|
Packit |
1470ea |
|
|
Packit |
1470ea |
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="radiobutton.js" xml:lang="es">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">RadioButton (JavaScript)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.js#buttons"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-06-15" status="draft"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Taryn Fox</name>
|
|
Packit |
1470ea |
<email its:translate="no">jewelfox@fursona.net</email>
|
|
Packit |
1470ea |
<years>2012</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<desc>Sólo se puede seleccionar uno a la vez</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Daniel Mustieles</mal:name>
|
|
Packit |
1470ea |
<mal:email>daniel.mustieles@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011 - 2017</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Nicolás Satragno</mal:name>
|
|
Packit |
1470ea |
<mal:email>nsatragno@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012 - 2013</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Jorge González</mal:name>
|
|
Packit |
1470ea |
<mal:email>jorgegonz@svn.gnome.org</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>RadioButton</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/radiobuttontravel.png"/>
|
|
Packit |
1470ea |
Los botones de radio se llaman así por las radios de los coches viejos, que tenían botones para alternar entre canales programados. Dado que la radio sólo podía sintonizar una estación a la vez, sólo se podía apretar un botón por vez; si presionaba uno nuevo, el que ya estaba presionado saltaba. Así es como estos botones funcionan.
|
|
Packit |
1470ea |
Cada botón de radio necesita una etiqueta de texto y un grupo. Sólo se puede seleccionar un botón de cada grupo por vez. No necesita nombrar cada grupo; sólo configure varios botones de radio para que sean parte del mismo grupo que otro existente. Si crea uno nuevo fuera de un grupo, automáticamente crea un grupo nuevo para que forme parte.
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="imports">
|
|
Packit |
1470ea |
<title>Bibliotecas que importar</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
#!/usr/bin/gjs
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
const Gio = imports.gi.Gio;
|
|
Packit |
1470ea |
const Gtk = imports.gi.Gtk;
|
|
Packit |
1470ea |
const Lang = imports.lang;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Estas son las bibliotecas que necesita importar para que esta aplicación se ejecute. Recuerde que la línea que le dice a GNOME que está usando Gjs siempre tiene que ir al principio.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="applicationwindow">
|
|
Packit |
1470ea |
<title>Crear la ventana de la aplicación</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
const RadioButtonExample = new Lang.Class({
|
|
Packit |
1470ea |
Name: 'RadioButton Example',
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the application itself
|
|
Packit |
1470ea |
_init: function() {
|
|
Packit |
1470ea |
this.application = new Gtk.Application({
|
|
Packit |
1470ea |
application_id: 'org.example.jsradiobutton',
|
|
Packit |
1470ea |
flags: Gio.ApplicationFlags.FLAGS_NONE
|
|
Packit |
1470ea |
});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect 'activate' and 'startup' signals to the callback functions
|
|
Packit |
1470ea |
this.application.connect('activate', Lang.bind(this, this._onActivate));
|
|
Packit |
1470ea |
this.application.connect('startup', Lang.bind(this, this._onStartup));
|
|
Packit |
1470ea |
},
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Callback function for 'activate' signal presents window when active
|
|
Packit |
1470ea |
_onActivate: function() {
|
|
Packit |
1470ea |
this._window.present();
|
|
Packit |
1470ea |
},
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Callback function for 'startup' signal builds the UI
|
|
Packit |
1470ea |
_onStartup: function() {
|
|
Packit |
1470ea |
this._buildUI ();
|
|
Packit |
1470ea |
},
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Todo el código de este ejemplo va en la clase RadioButtonExample. El código anterior crea una <link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link> para que vayan los widgets y la ventana.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Build the application's UI
|
|
Packit |
1470ea |
_buildUI: function() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the application window
|
|
Packit |
1470ea |
this._window = new Gtk.ApplicationWindow({
|
|
Packit |
1470ea |
application: this.application,
|
|
Packit |
1470ea |
window_position: Gtk.WindowPosition.CENTER,
|
|
Packit |
1470ea |
border_width: 20,
|
|
Packit |
1470ea |
title: "Travel Planning"});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
La función _buildUI es donde se pone todo el código que crea la interfaz de usuario de la aplicación. El primer paso es crear una <link xref="GtkApplicationWindow.js">Gtk.ApplicationWindow</link> nueva para poner dentro todos los widgets.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="button">
|
|
Packit |
1470ea |
<title>Crear los botones de radio</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a label for the first group of buttons
|
|
Packit |
1470ea |
this._placeLabel = new Gtk.Label ({label: "Where would you like to travel to?"});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Se usa una <link xref="label.js">Gtk.Label</link> para diferenciar cada grupo de botones de radio. Nada le impedirá poner botones de radio de grupos diferentes donde quiera, así que si quiere que la gente sepa cuáles van juntos, necesita ordenar las cosas adecuadamente.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create three radio buttons three different ways
|
|
Packit |
1470ea |
this._place1 = new Gtk.RadioButton ({label: "The Beach"});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this._place2 = Gtk.RadioButton.new_from_widget (this._place1);
|
|
Packit |
1470ea |
this._place2.set_label ("The Moon");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this._place3 = Gtk.RadioButton.new_with_label_from_widget (this._place1, "Antarctica");
|
|
Packit |
1470ea |
// this._place3.set_active (true);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Aquí hay tres maneras diferentes de crear botones de radio. La primera es la manera común, donde se crea un «Gtk.RadioButton» nuevo y se le asignan sus propiedades al mismo tiempo. La segunda y la tercera usan funciones que manejan algunas de sus propiedades automáticamente; «new_from_widget» toma un solo argumento, un botón de radio del mismo grupo en el que quiere poner el nuevo. «new_with_label_from_widget», además, toma eso y la etiqueta del botón de radio al mismo tiempo.
|
|
Packit |
1470ea |
El primer botón de radio en un grupo es el que está seleccionado de manera predeterminada. Pruebe a descomentar la última línea en este código de ejemplo para ver cómo puede configurar uno diferente para que sea la selección predeterminada.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a label for the second group of buttons
|
|
Packit |
1470ea |
this._thingLabel = new Gtk.Label ({label: "And what would you like to bring?" });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create three more radio buttons
|
|
Packit |
1470ea |
this._thing1 = new Gtk.RadioButton ({label: "Penguins" });
|
|
Packit |
1470ea |
this._thing2 = new Gtk.RadioButton ({label: "Sunscreen", group: this._thing1 });
|
|
Packit |
1470ea |
this._thing3 = new Gtk.RadioButton ({label: "A spacesuit", group: this._thing1 });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Aquí se crea la etiqueta para el segundo grupo de botones, y después se crean todos de la misma manera.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="ui">
|
|
Packit |
1470ea |
<title>Crear el resto de la interfaz de usuario</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a stock OK button
|
|
Packit |
1470ea |
this._okButton = new Gtk.Button ({
|
|
Packit |
1470ea |
label: 'gtk-ok',
|
|
Packit |
1470ea |
use_stock: 'true',
|
|
Packit |
1470ea |
halign: Gtk.Align.END });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect the button to the function which handles clicking it
|
|
Packit |
1470ea |
this._okButton.connect ('clicked', Lang.bind (this, this._okClicked));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Este código crea un <link xref="button.js">Gtk.Button</link> y lo enlaza a una función que mostrará un mensaje tonto cuando pulse «OK», dependiendo de qué botones de radio se seleccionaron.
|
|
Packit |
1470ea |
Para asegurarse de que la etiqueta «OK» del botón se muestra correctamente en cada idioma al que se traduce GNOME, recuerde usar uno de los <link href="https://developer.gnome.org/gtk3/3.4/gtk3-Stock-Items.html">tipos de botón del almacén</link> de GTK+.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a grid to put the "place" items in
|
|
Packit |
1470ea |
this._places = new Gtk.Grid ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Attach the "place" items to the grid
|
|
Packit |
1470ea |
this._places.attach (this._placeLabel, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._places.attach (this._place1, 0, 1, 1, 1);
|
|
Packit |
1470ea |
this._places.attach (this._place2, 0, 2, 1, 1);
|
|
Packit |
1470ea |
this._places.attach (this._place3, 0, 3, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a grid to put the "thing" items in
|
|
Packit |
1470ea |
this._things = new Gtk.Grid ({ margin_top: 50 });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Attach the "thing" items to the grid
|
|
Packit |
1470ea |
this._things.attach (this._thingLabel, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._things.attach (this._thing1, 0, 1, 1, 1);
|
|
Packit |
1470ea |
this._things.attach (this._thing2, 0, 2, 1, 1);
|
|
Packit |
1470ea |
this._things.attach (this._thing3, 0, 3, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a grid to put everything in
|
|
Packit |
1470ea |
this._grid = new Gtk.Grid ({
|
|
Packit |
1470ea |
halign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
valign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
margin_left: 40,
|
|
Packit |
1470ea |
margin_right: 50 });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Attach everything to the grid
|
|
Packit |
1470ea |
this._grid.attach (this._places, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._grid.attach (this._things, 0, 1, 1, 1);
|
|
Packit |
1470ea |
this._grid.attach (this._okButton, 0, 2, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Add the grid to the window
|
|
Packit |
1470ea |
this._window.add (this._grid);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Se usa una <link xref="grid.js">Gtk.Grid</link> separada para organizar cada grupo de botones de radio. De esta manera se puede cambiar la distribución sin liarse más adelante. La segunda rejilla tiene un margen en la parte superior, para separar visualmente los dos conjuntos de opciones.
|
|
Packit |
1470ea |
Después de haberlos organizado, se ponen en una tercera rejilla maestra, junto con el botón «OK». Luego se adjuntan a la ventana.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Show the window and all child widgets
|
|
Packit |
1470ea |
this._window.show_all();
|
|
Packit |
1470ea |
},
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Finalmente, se le dice a la ventana y a todo dentro que se vuelva visible cuando se ejecute la aplicación.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="function">
|
|
Packit |
1470ea |
<title>Función que maneja su selección</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_okClicked: function () {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a popup that shows a silly message
|
|
Packit |
1470ea |
this._travel = new Gtk.MessageDialog ({
|
|
Packit |
1470ea |
transient_for: this._window,
|
|
Packit |
1470ea |
modal: true,
|
|
Packit |
1470ea |
message_type: Gtk.MessageType.OTHER,
|
|
Packit |
1470ea |
buttons: Gtk.ButtonsType.OK,
|
|
Packit |
1470ea |
text: this._messageText() });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Show the popup
|
|
Packit |
1470ea |
this._travel.show();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Bind the OK button to the function that closes the popup
|
|
Packit |
1470ea |
this._travel.connect ("response", Lang.bind (this, this._clearTravelPopUp));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
},
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Cuando pulsa «OK», aparece un <link xref="messagedialog.js">Gtk.MessageDialog</link>. Esta función crea y muestra la ventana emergente, después enlaza su botón «OK» a una función que la cierra. Qué texto aparece en el mensaje emergente depende de la función «_messageText()», que devuelve un valor diferente dependiendo de qué conjunto de opciones elija.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_messageText: function() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a silly message for the popup depending on what you selected
|
|
Packit |
1470ea |
var stringMessage = "";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (this._place1.get_active()) {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (this._thing1.get_active())
|
|
Packit |
1470ea |
stringMessage = "Penguins love the beach, too!";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this._thing2.get_active())
|
|
Packit |
1470ea |
stringMessage = "Make sure to put on that sunscreen!";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else stringMessage = "Are you going to the beach in space?";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this._place2.get_active()) {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (this._thing1.get_active())
|
|
Packit |
1470ea |
stringMessage = "The penguins will take over the moon!";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this._thing2.get_active())
|
|
Packit |
1470ea |
stringMessage = "A lack of sunscreen will be the least of your problems!";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else stringMessage = "You'll probably want a spaceship, too!";
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this._place3.get_active()) {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (this._thing1.get_active())
|
|
Packit |
1470ea |
stringMessage = "The penguins will be happy to be back home!";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this._thing2.get_active())
|
|
Packit |
1470ea |
stringMessage = "Antarctic sunbathing may be hazardous to your health!";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else stringMessage = "Try bringing a parka instead!";
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
return stringMessage;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
},
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
El método «get_active()» indica qué botón de radio está presionado. Esta función devuelve un mensaje tonto diferente dependiendo de qué conjunto de botones se presionó. Su valor de retorno se usa como propiedad «text» del «MessageDialog».
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_clearTravelPopUp: function () {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this._travel.destroy();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Esta función se llama cuando se presiona el botón «OK» del «MessageDialog». Simplemente hace desaparecer el diálogo emergente.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Run the application
|
|
Packit |
1470ea |
let app = new RadioButtonExample ();
|
|
Packit |
1470ea |
app.application.run (ARGV);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Finalmente, se crea una instancia nueva de la clase RadioButtonExample terminada, y se ejecuta la aplicación.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="complete">
|
|
Packit |
1470ea |
<title>Código de ejemplo completo</title>
|
|
Packit |
1470ea |
#!/usr/bin/gjs
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
imports.gi.versions.Gtk = '3.0';
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
const Gio = imports.gi.Gio;
|
|
Packit |
1470ea |
const Gtk = imports.gi.Gtk;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class RadioButtonExample {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the application itself
|
|
Packit |
1470ea |
constructor() {
|
|
Packit |
1470ea |
this.application = new Gtk.Application({
|
|
Packit |
1470ea |
application_id: 'org.example.jsradiobutton',
|
|
Packit |
1470ea |
flags: Gio.ApplicationFlags.FLAGS_NONE
|
|
Packit |
1470ea |
});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect 'activate' and 'startup' signals to the callback functions
|
|
Packit |
1470ea |
this.application.connect('activate', this._onActivate.bind(this));
|
|
Packit |
1470ea |
this.application.connect('startup', this._onStartup.bind(this));
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Callback function for 'activate' signal presents window when active
|
|
Packit |
1470ea |
_onActivate() {
|
|
Packit |
1470ea |
this._window.present();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Callback function for 'startup' signal builds the UI
|
|
Packit |
1470ea |
_onStartup() {
|
|
Packit |
1470ea |
this._buildUI();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Build the application's UI
|
|
Packit |
1470ea |
_buildUI() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the application window
|
|
Packit |
1470ea |
this._window = new Gtk.ApplicationWindow({
|
|
Packit |
1470ea |
application: this.application,
|
|
Packit |
1470ea |
window_position: Gtk.WindowPosition.CENTER,
|
|
Packit |
1470ea |
border_width: 20,
|
|
Packit |
1470ea |
title: "Travel Planning"});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a label for the first group of buttons
|
|
Packit |
1470ea |
this._placeLabel = new Gtk.Label ({label: "Where would you like to travel to?"});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create three radio buttons three different ways
|
|
Packit |
1470ea |
this._place1 = new Gtk.RadioButton ({label: "The Beach"});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this._place2 = Gtk.RadioButton.new_from_widget (this._place1);
|
|
Packit |
1470ea |
this._place2.set_label ("The Moon");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this._place3 = Gtk.RadioButton.new_with_label_from_widget (this._place1, "Antarctica");
|
|
Packit |
1470ea |
// this._place3.set_active (true);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a label for the second group of buttons
|
|
Packit |
1470ea |
this._thingLabel = new Gtk.Label ({label: "And what would you like to bring?" });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create three more radio buttons
|
|
Packit |
1470ea |
this._thing1 = new Gtk.RadioButton ({label: "Penguins" });
|
|
Packit |
1470ea |
this._thing2 = new Gtk.RadioButton ({label: "Sunscreen", group: this._thing1 });
|
|
Packit |
1470ea |
this._thing3 = new Gtk.RadioButton ({label: "A spacesuit", group: this._thing1 });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a stock OK button
|
|
Packit |
1470ea |
this._okButton = new Gtk.Button ({
|
|
Packit |
1470ea |
label: 'gtk-ok',
|
|
Packit |
1470ea |
use_stock: 'true',
|
|
Packit |
1470ea |
halign: Gtk.Align.END });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect the button to the function which handles clicking it
|
|
Packit |
1470ea |
this._okButton.connect ('clicked', this._okClicked.bind(this));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a grid to put the "place" items in
|
|
Packit |
1470ea |
this._places = new Gtk.Grid ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Attach the "place" items to the grid
|
|
Packit |
1470ea |
this._places.attach (this._placeLabel, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._places.attach (this._place1, 0, 1, 1, 1);
|
|
Packit |
1470ea |
this._places.attach (this._place2, 0, 2, 1, 1);
|
|
Packit |
1470ea |
this._places.attach (this._place3, 0, 3, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a grid to put the "thing" items in
|
|
Packit |
1470ea |
this._things = new Gtk.Grid ({ margin_top: 50 });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Attach the "thing" items to the grid
|
|
Packit |
1470ea |
this._things.attach (this._thingLabel, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._things.attach (this._thing1, 0, 1, 1, 1);
|
|
Packit |
1470ea |
this._things.attach (this._thing2, 0, 2, 1, 1);
|
|
Packit |
1470ea |
this._things.attach (this._thing3, 0, 3, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a grid to put everything in
|
|
Packit |
1470ea |
this._grid = new Gtk.Grid ({
|
|
Packit |
1470ea |
halign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
valign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
margin_left: 40,
|
|
Packit |
1470ea |
margin_right: 50 });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Attach everything to the grid
|
|
Packit |
1470ea |
this._grid.attach (this._places, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._grid.attach (this._things, 0, 1, 1, 1);
|
|
Packit |
1470ea |
this._grid.attach (this._okButton, 0, 2, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Add the grid to the window
|
|
Packit |
1470ea |
this._window.add (this._grid);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Show the window and all child widgets
|
|
Packit |
1470ea |
this._window.show_all();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_okClicked() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a popup that shows a silly message
|
|
Packit |
1470ea |
this._travel = new Gtk.MessageDialog ({
|
|
Packit |
1470ea |
transient_for: this._window,
|
|
Packit |
1470ea |
modal: true,
|
|
Packit |
1470ea |
message_type: Gtk.MessageType.OTHER,
|
|
Packit |
1470ea |
buttons: Gtk.ButtonsType.OK,
|
|
Packit |
1470ea |
text: this._messageText() });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Show the popup
|
|
Packit |
1470ea |
this._travel.show();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Bind the OK button to the function that closes the popup
|
|
Packit |
1470ea |
this._travel.connect ("response", this._clearTravelPopUp.bind(this));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_messageText() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a silly message for the popup depending on what you selected
|
|
Packit |
1470ea |
var stringMessage = "";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (this._place1.get_active()) {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (this._thing1.get_active())
|
|
Packit |
1470ea |
stringMessage = "Penguins love the beach, too!";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this._thing2.get_active())
|
|
Packit |
1470ea |
stringMessage = "Make sure to put on that sunscreen!";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else stringMessage = "Are you going to the beach in space?";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this._place2.get_active()) {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (this._thing1.get_active())
|
|
Packit |
1470ea |
stringMessage = "The penguins will take over the moon!";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this._thing2.get_active())
|
|
Packit |
1470ea |
stringMessage = "A lack of sunscreen will be the least of your problems!";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else stringMessage = "You'll probably want a spaceship, too!";
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this._place3.get_active()) {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (this._thing1.get_active())
|
|
Packit |
1470ea |
stringMessage = "The penguins will be happy to be back home!";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this._thing2.get_active())
|
|
Packit |
1470ea |
stringMessage = "Antarctic sunbathing may be hazardous to your health!";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else stringMessage = "Try bringing a parka instead!";
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
return stringMessage;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_clearTravelPopUp() {
|
|
Packit |
1470ea |
this._travel.destroy();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
};
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Run the application
|
|
Packit |
1470ea |
let app = new RadioButtonExample ();
|
|
Packit |
1470ea |
app.application.run (ARGV);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="in-depth">
|
|
Packit |
1470ea |
<title>Documentación en profundidad</title>
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkApplicationWindow.html">Gtk.ApplicationWindow</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Button.html">Gtk.Button</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Grid.html">Gtk.Grid</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Label.html">Gtk.Label</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.RadioButton.html">Gtk.RadioButton</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|