|
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="fr">
|
|
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 éditeur de texte multiligne</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Luc Rebert,</mal:name>
|
|
Packit |
1470ea |
<mal:email>traduc@rebert.name</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011</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>Alain Lojewski,</mal:name>
|
|
Packit |
1470ea |
<mal:email>allomervan@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011-2012</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>Luc Pionchon</mal:name>
|
|
Packit |
1470ea |
<mal:email>pionchon.luc@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011</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>Bruno Brouard</mal:name>
|
|
Packit |
1470ea |
<mal:email>annoa.b@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011-12</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>Luis Menina</mal:name>
|
|
Packit |
1470ea |
<mal:email>liberforce@freeside.fr</mal:email>
|
|
Packit |
1470ea |
<mal:years>2014</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 élément graphique TextView est en réalité (le plus souvent) une combinaison de trois objets.
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>En bas se trouve un tampon <link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.TextBuffer.html">TextBuffer</link> qui contient le texte lui-même. </item>
|
|
Packit |
1470ea |
<item>Au milieu, nous trouvons l'élément graphique <link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.TextView.html">TextView</link>, qui nous permet d'afficher et de modifier le texte du tampon. Il se redimensionne automatiquement en fonction de la quantité de texte qu'il contient. </item>
|
|
Packit |
1470ea |
<item>Since the automatic resizing can make a TextView unwieldy, you normally place it inside of a ScrolledWindow. Despite the name, it's not an actual window in terms of having a title bar and an X button; it's a widget you put on the application you're making, which acts like a window onto a more manageable chunk of a TextView. If the text in the buffer is too big to fit, scrollbars will appear. </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
Si vous modifiez le texte affiché dans le TextView, vous agissez sur le TextBuffer puisque c'est lui qui contient effectivement le texte. Procédez de la même manière si vous souhaitez connaître le texte saisi par quelqu'un. Cet exemple d'application vous permet de parler à un pingouin (imaginaire) et parcourt le TextBuffer pour voir si vous avez saisi le mot « poisson » quelque part.
|
|
Packit |
1470ea |
<note>Dans la vraie vie, les populations de pingouins déclinent rapidement, à cause du changement climatique qui fait fondre les glaces sur lesquels ils vivent et raréfie les stocks de poissons dont ils se nourrissent. Allez jeter un coup d'œil à <link href="http://pingus.seul.org/">Pingus.</link> si vous voulez jouer à un jeu GNOME (un peu stupide) basé sur ce constat. </note>
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="imports">
|
|
Packit |
1470ea |
<title>Bibliothèques à importer</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 |
Ce sont les bibliothèques que nous devons importer pour faire fonctionner cette application. N'oubliez pas que la ligne qui informe GNOME que nous allons utiliser Gjs doit toujours se trouver au début.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="applicationwindow">
|
|
Packit |
1470ea |
<title>Création de la fenêtre de l'application</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 |
Tout le code de cet exemple tient dans la classe TextViewExample. Le code ci-dessus crée une <link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link> pour nos éléments graphiques et la fenêtre qui les contient.
|
|
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 fonction _buildUI est l'endroit où nous mettons tout le code nécessaire à la création de l'interface utilisateur de l'application. La première étape consiste à créer une <link xref="GtkApplicationWindow.js">Gtk.ApplicationWindow</link> pour y mettre tous nos éléments graphiques.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="textview">
|
|
Packit |
1470ea |
<title>Création de l'élément graphique 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 |
Pour commencer, créons le <link xref="label.js">Label</link> (étiquette) que le pingouin va utiliser pour dialoguer avec vous. Définissons la propriété de césure du texte de l'étiquette sur wrap (césure) en lui attribuant la valeur « true », mais nous utiliserons une méthode différente sur l'élément graphique TextView lui-même pour avoir un contrôle plus fin.
|
|
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 |
La première étape consiste à créer un élément graphique TextBuffer pour y placer les mots. Ensuite, nous créons l'élément TextView et nous lui indiquons d'utiliser le TextBuffer comme tampon. Nous le paramétrons aussi pour qu'il soit modifiable (editable), car nous voulons pouvoir y saisir d'autres choses.
|
|
Packit |
1470ea |
The wrap_mode property lets you select from four different <link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.WrapMode.html">WrapModes</link>. Gtk.WrapMode.CHAR, for instance, starts wrapping around in the middle of a word if you keep typing when it gets to the edge. Most people are probably used to Gtk.WrapMode.WORD, which will automatically put the word you're typing on the next line if it gets to be too long.
|
|
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 |
Here we create a ScrolledWindow, and set it to automatically scroll if it gets to be too big horizontally or vertically. We also give it a nice-looking ETCHED_IN border. After that, we put our TextView inside, and tell the ScrolledWindow to give us a viewport onto it.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="ui">
|
|
Packit |
1470ea |
<title>Création du reste de l'interface utilisateur</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 |
Le premier <link xref="grid.js">Grid</link> (grille) que nous avons créé ne contient que l'étiquette et 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 |
Nous créons un <link xref="button.js">Button</link> (bouton) pour envoyer votre message au pingouin et un nouveau Grid (grille) qui contient l'autre grille en haut et le bouton en bas. Le bouton comporte une marge (margin) dans sa partie supérieure, de telle sorte qu'il n'est pas collé contre 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 |
Enfin, nous relions la grille principale à la fenêtre, puis indiquons à la fenêtre de s'afficher avec son contenu quand l'application est lancée.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="function">
|
|
Packit |
1470ea |
<title>Fonction prenant en charge la réponse du pingouin</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 |
Nous utilisons ici quelques fonctions JavaScript de base pour que le pingouin dise quelque chose au hasard. Les pingouins apprécient le poisson et nous allons donc le faire répondre quand on prononce le mot « fish » (poisson). Pour cela, nous appliquons la méthode JavaScript de correspondance d'objets chaîne de caractères sur le texte <file>this.buffer.text</file>, qui renvoie le contenu de notre TextBuffer.
|
|
Packit |
1470ea |
Comme nous voulons effacer le TextBuffer après chaque clic sur « Sent » (Envoyer), nous paramétrons le <file>this.buffer.text</file> pour qu'il contienne une chaîne vide après cela. Nous renvoyons ensuite le focus sur notre élément TextView, pour pouvoir continuer à y saisir du texte sans avoir besoin de cliquer dessus au préalable.
|
|
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 |
Enfin, nous créons une nouvelle instance de la classe TextViewExample que nous venons de terminer et démarrons l'application.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="complete">
|
|
Packit |
1470ea |
<title>Exemple complet de code</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>Documentation approfondie</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>
|