|
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="textview.js" xml:lang="es">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">TextView (JavaScript)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.js#multiline"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="button.js"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="grid.js"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="GtkApplicationWindow.js"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="label.js"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-06-28" 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>Un editor de texto multilínea</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>TextView</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/textviewpenguinchat.png"/>
|
|
Packit |
1470ea |
Un «TextView» es realmente (o al menos generalmente) un conjunto anidado de tres objetos.
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>En la parte inferior hay un <link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.TextBuffer.html">Búfer de texto</link>. Este almacena el texto. </item>
|
|
Packit |
1470ea |
<item>En el medio está el <link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.TextView.html">TextView</link>, que es un widget que le permite ver y editar el texto en el búfer. Cambia su tamaño automáticamente dependiendo de cuánto texto hay. </item>
|
|
Packit |
1470ea |
<item>Dado que el cambio de tamaño automático puede hacer un «TextView» difícil de manejar, normalmente se ubica dentro de una «ScrolledWindow». A pesar de su nombre, no es una ventana real en términos de tener una barra de título y un botón «X»; es un widget en el que pone la aplicación que está haciendo, que actúa como una ventana sobre un pedazo más manejable de un «TextView». Si el texto en el búfer es demasiado grande para entrar, aparecerán barras de desplazamiento. </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
Si quiere cambiar el texto que se muestra en el «TextView», actúe en el búfer de texto, dado que es lo que realmente contiene al texto. Es lo mismo si quiere ver lo que se ha introducido en él. Esta aplicación de ejemplo le permite hablar con un pingüino de fantasía, y verifica el búfer de texto para ver si ha introducido la palabra «fish» en él.
|
|
Packit |
1470ea |
<note>Las poblaciones de pingüinos reales están decayendo rápidamente, porque el cambio climático está derritiendo el hielo en el que viven y matando a los peces que comen. Si le interesa jugar a un juego de GNOME algo tonto basado en esta premisa, échele un vistazo a <link href="http://pingus.seul.org/">Pingus</link>. </note>
|
|
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 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 TextViewExample = new Lang.Class ({
|
|
Packit |
1470ea |
Name: 'TextView 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.jstextview' });
|
|
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 windows 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 «TextViewExample». 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 poner 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 |
title: "Talk to a Penguin",
|
|
Packit |
1470ea |
default_height: 400,
|
|
Packit |
1470ea |
default_width: 440,
|
|
Packit |
1470ea |
border_width: 20 });
|
|
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="textview">
|
|
Packit |
1470ea |
<title>Crear el «TextView»</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a label for the penguin to talk to you
|
|
Packit |
1470ea |
this._penguin = new Gtk.Label ({
|
|
Packit |
1470ea |
height_request: 180,
|
|
Packit |
1470ea |
width_request: 400,
|
|
Packit |
1470ea |
label: "Squaaaak?",
|
|
Packit |
1470ea |
wrap: true });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
El primer paso en este ejemplo es crear la <link xref="label.js">Label</link> que el pingüino usará para hablarle. Se configura para que tenga ajuste de línea estableciendo su propiedad «wrap» a «true», pero usaremos un método distinto en el «TextView» propio que permite un control más preciso.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a textview for you to talk to the penguin
|
|
Packit |
1470ea |
this.buffer = new Gtk.TextBuffer();
|
|
Packit |
1470ea |
this._textView = new Gtk.TextView ({
|
|
Packit |
1470ea |
buffer: this.buffer,
|
|
Packit |
1470ea |
editable: true,
|
|
Packit |
1470ea |
wrap_mode: Gtk.WrapMode.WORD });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
El primer paso es crear un «TextBuffer» para poner dentro las palabras. Después de eso se crea el «TextView», y se le dice que use el «TextBuffer» que se creó como su búfer. También se configura para que sea editable, dado que se quiere poder introducir cosas nuevas dentro.
|
|
Packit |
1470ea |
La propiedad «wrap_mode» le permite elegir entre cuatro <link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.WrapMode.html">Modos de ajuste de línea</link> diferentes. «Gtk.WrapMode.CHAR», por ejemplo, salta de línea en el medio de una palabra si sigue escribiendo cuando llega al borde. La mayoría de la gente está probablemente acostumbrada a «Gtk.WrapMode.WORD», que pondrá la palabra que está escribiendo en la línea siguiente automáticamente si es demasiado larga.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a "scrolled window" to put your textview in so it will scroll
|
|
Packit |
1470ea |
this._scrolled = new Gtk.ScrolledWindow ({
|
|
Packit |
1470ea |
hscrollbar_policy: Gtk.PolicyType.AUTOMATIC,
|
|
Packit |
1470ea |
vscrollbar_policy: Gtk.PolicyType.AUTOMATIC,
|
|
Packit |
1470ea |
shadow_type: Gtk.ShadowType.ETCHED_IN,
|
|
Packit |
1470ea |
height_request: 180,
|
|
Packit |
1470ea |
width_request: 400, });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Put the textview into the scrolled window
|
|
Packit |
1470ea |
this._scrolled.add_with_viewport (this._textView);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Aquí se crea una «ScrolledWindow», y se configura para que se desplace automáticamente si se vuelve demasiado grande horizontal o verticalmente. También se le da un borde bonito ETCHED_IN. Después de eso, se pone el «TextView» dentro, y se hace que la «ScrolledWindow» dé un punto de vista sobre él.
|
|
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 grid to organize them in
|
|
Packit |
1470ea |
this._grid = new Gtk.Grid ({
|
|
Packit |
1470ea |
halign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
valign: Gtk.Align.CENTER });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Put the label and textview in the grid one on top of the other
|
|
Packit |
1470ea |
this._grid.attach (this._penguin, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._grid.attach (this._scrolled, 0, 1, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
La primera <link xref="grid.js">Rejilla</link> que se crea sólo tiene dentro la etiqueta y la «ScrolledWindow».
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a button to send your message to the penguin
|
|
Packit |
1470ea |
this._send = new Gtk.Button ({
|
|
Packit |
1470ea |
halign: Gtk.Align.END,
|
|
Packit |
1470ea |
margin_top: 20,
|
|
Packit |
1470ea |
label: "Send" });
|
|
Packit |
1470ea |
this._send.connect ('clicked', Lang.bind (this, this._chat));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a grid that will have the other grid on top and the button on bottom
|
|
Packit |
1470ea |
this._mainGrid = new Gtk.Grid ({
|
|
Packit |
1470ea |
halign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
valign: Gtk.Align.CENTER });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Add the other grid and the button to the main grid
|
|
Packit |
1470ea |
this._mainGrid.attach (this._grid, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._mainGrid.attach (this._send, 0, 1, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Se crea un <link xref="button.js">Botón</link> para enviar su mensaje al pingüino, y una rejilla nueva que tiene la otra en su parte superior y el botón en la parte inferior. El botón tiene un margen en su parte superior, para que no se aplaste contra la «ScrolledWindow».
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Attach the main grid to the window
|
|
Packit |
1470ea |
this._window.add (this._mainGrid);
|
|
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 adjunta la rejilla principal a la ventana, después se le dice a la ventana y a todo lo que tiene dentro de ella 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 la respuesta del pingüino</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_chat: function () {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a random number to determine what the penguin says
|
|
Packit |
1470ea |
this.number = Math.floor ((Math.random() * 3) + 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Did you actually say anything?
|
|
Packit |
1470ea |
if (this.buffer.text) {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Did you mention fish?
|
|
Packit |
1470ea |
if (this.buffer.text.match (/fish/gi)) {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Have the penguin squaak about fish
|
|
Packit |
1470ea |
if (this.number == 1)
|
|
Packit |
1470ea |
this._penguin.set_label ("FISH!");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this.number == 2)
|
|
Packit |
1470ea |
this._penguin.set_label ("Fish fish fish fish. Fish!");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else
|
|
Packit |
1470ea |
this._penguin.set_label ("Fish? Fish fish fish. Fish fish. FISH!");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// I guess you didn't mention fish
|
|
Packit |
1470ea |
else {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Have the penguin talk about penguinny stuff
|
|
Packit |
1470ea |
if (this.number == 1)
|
|
Packit |
1470ea |
this._penguin.set_label ("SQUAAK!");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this.number == 2)
|
|
Packit |
1470ea |
this._penguin.set_label ("Ork ork ork ork squaak. Squaak squaak! *waves flippers*");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else
|
|
Packit |
1470ea |
this._penguin.set_label ("Ork ork ork ork ork?");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Clear the buffer
|
|
Packit |
1470ea |
this.buffer.text = "";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Give focus back to the textview so you don't have to click it again
|
|
Packit |
1470ea |
this._textView.has_focus = true;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Aquí se usan algunas funciones básicas de JavaScript para que el pingüino diga algo aleatorio. Sin embargo, a los pingüinos les gusta el pescado, por lo que se quiere que si se menciona «fish», el pingüino responda. Para hacer eso, se usa el método «match» del objeto de cadena de JavaScript sobre <file>this.buffer.text</file>, que devuelve el contenido del búfer de texto.
|
|
Packit |
1470ea |
Dado que se quiere limpiar el búfer de texto después de cada vez que pulsa «Send», se configura <file>this.buffer.text</file> para que contenga una cadena vacía justo entonces. Después, se devuelve el foco al «TextView», para poder seguir escribiendo sin tener que pulsar en él antes.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Run the application
|
|
Packit |
1470ea |
let app = new TextViewExample ();
|
|
Packit |
1470ea |
app.application.run (ARGV);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Finalmente, se crea una instancia nueva de la clase «TextViewExample» 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 |
const Gtk = imports.gi.Gtk;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class TextViewExample {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the application itself
|
|
Packit |
1470ea |
constructor() {
|
|
Packit |
1470ea |
this.application = new Gtk.Application({
|
|
Packit |
1470ea |
application_id: 'org.example.jstextview'
|
|
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 windows 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 |
title: "Talk to a Penguin",
|
|
Packit |
1470ea |
default_height: 400,
|
|
Packit |
1470ea |
default_width: 440,
|
|
Packit |
1470ea |
border_width: 20 });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a label for the penguin to talk to you
|
|
Packit |
1470ea |
this._penguin = new Gtk.Label ({
|
|
Packit |
1470ea |
height_request: 180,
|
|
Packit |
1470ea |
width_request: 400,
|
|
Packit |
1470ea |
label: "Squaaaak?",
|
|
Packit |
1470ea |
wrap: true });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a textview for you to talk to the penguin
|
|
Packit |
1470ea |
this.buffer = new Gtk.TextBuffer();
|
|
Packit |
1470ea |
this._textView = new Gtk.TextView ({
|
|
Packit |
1470ea |
buffer: this.buffer,
|
|
Packit |
1470ea |
editable: true,
|
|
Packit |
1470ea |
wrap_mode: Gtk.WrapMode.WORD });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a "scrolled window" to put your textview in so it will scroll
|
|
Packit |
1470ea |
this._scrolled = new Gtk.ScrolledWindow ({
|
|
Packit |
1470ea |
hscrollbar_policy: Gtk.PolicyType.AUTOMATIC,
|
|
Packit |
1470ea |
vscrollbar_policy: Gtk.PolicyType.AUTOMATIC,
|
|
Packit |
1470ea |
shadow_type: Gtk.ShadowType.ETCHED_IN,
|
|
Packit |
1470ea |
height_request: 180,
|
|
Packit |
1470ea |
width_request: 400, });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Put the textview into the scrolled window
|
|
Packit |
1470ea |
this._scrolled.add_with_viewport (this._textView);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a grid to organize them in
|
|
Packit |
1470ea |
this._grid = new Gtk.Grid ({
|
|
Packit |
1470ea |
halign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
valign: Gtk.Align.CENTER });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Put the label and textview in the grid one on top of the other
|
|
Packit |
1470ea |
this._grid.attach (this._penguin, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._grid.attach (this._scrolled, 0, 1, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a button to send your message to the penguin
|
|
Packit |
1470ea |
this._send = new Gtk.Button ({
|
|
Packit |
1470ea |
halign: Gtk.Align.END,
|
|
Packit |
1470ea |
margin_top: 20,
|
|
Packit |
1470ea |
label: "Send" });
|
|
Packit |
1470ea |
this._send.connect ('clicked', this._chat.bind(this));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a grid that will have the other grid on top and the button on bottom
|
|
Packit |
1470ea |
this._mainGrid = new Gtk.Grid ({
|
|
Packit |
1470ea |
halign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
valign: Gtk.Align.CENTER });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Add the other grid and the button to the main grid
|
|
Packit |
1470ea |
this._mainGrid.attach (this._grid, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._mainGrid.attach (this._send, 0, 1, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Attach the main grid to the window
|
|
Packit |
1470ea |
this._window.add (this._mainGrid);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Show the window and all child widgets
|
|
Packit |
1470ea |
this._window.show_all();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_chat() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a random number to determine what the penguin says
|
|
Packit |
1470ea |
this.number = Math.floor ((Math.random() * 3) + 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Did you actually say anything?
|
|
Packit |
1470ea |
if (this.buffer.text) {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Did you mention fish?
|
|
Packit |
1470ea |
if (this.buffer.text.match (/fish/gi)) {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Have the penguin squaak about fish
|
|
Packit |
1470ea |
if (this.number == 1)
|
|
Packit |
1470ea |
this._penguin.set_label ("FISH!");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this.number == 2)
|
|
Packit |
1470ea |
this._penguin.set_label ("Fish fish fish fish. Fish!");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else
|
|
Packit |
1470ea |
this._penguin.set_label ("Fish? Fish fish fish. Fish fish. FISH!");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// I guess you didn't mention fish
|
|
Packit |
1470ea |
else {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Have the penguin talk about penguinny stuff
|
|
Packit |
1470ea |
if (this.number == 1)
|
|
Packit |
1470ea |
this._penguin.set_label ("SQUAAK!");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else if (this.number == 2)
|
|
Packit |
1470ea |
this._penguin.set_label ("Ork ork ork ork squaak. Squaak squaak! *waves flippers*");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else
|
|
Packit |
1470ea |
this._penguin.set_label ("Ork ork ork ork ork?");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Clear the buffer
|
|
Packit |
1470ea |
this.buffer.text = "";
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Give focus back to the textview so you don't have to click it again
|
|
Packit |
1470ea |
this._textView.has_focus = true;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
};
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Run the application
|
|
Packit |
1470ea |
let app = new TextViewExample ();
|
|
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 |
<item><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.ScrolledWindow.html">Gtk.ScrolledWindow</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.TextBuffer.html">Gtk.TextBuffer</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.TextView.html">Gtk.TextView</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|