|
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="comboboxtext.js" xml:lang="ko">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">ComboBoxText(JavaScript)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.js#menu-combo-toolbar"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="GtkApplicationWindow.js"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="messagedialog.js"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-07-06" 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>텍스트 전용 드롭 다운 메뉴</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>조성호</mal:name>
|
|
Packit |
1470ea |
<mal:email>shcho@gnome.org</mal:email>
|
|
Packit |
1470ea |
<mal:years>2017</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>ComboBoxText</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/combobox.png"/>
|
|
Packit |
1470ea |
콤보박스는 드롭 다운 메뉴입니다. <link xref="combobox.js">ComboBox</link>와 ComboBoxText의 차이점이라면 ComboBoxText는 텍스트만 있는 위젯이고, 완전한 ComboBox는 분기 옵션이나, 각 선택 항목의 그림을 보여주는 ListStore나 TreeStore(기본적으로 스프레드 시트 같음)를 사용하는 위젯입니다.
|
|
Packit |
1470ea |
<note>완전한 기능을 갖춘 ComboBox가 필요하거나, ListStore와 TreeStore를 사용하지 않은 이상 가능하다면 ComboBoxText를 사용하는게 훨씬 간단합니다. </note>
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="imports">
|
|
Packit |
1470ea |
<title>가져올 라이브러리</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 |
이 프로그램을 실행할 때 가져올 라이브러리입니다. 시작 부분에 항상 gjs가 필요함을 알리는 줄을 작성해야 함을 기억하십시오.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="applicationwindow">
|
|
Packit |
1470ea |
<title>프로그램 창 만들기</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
const ComboBoxTextExample = new Lang.Class ({
|
|
Packit |
1470ea |
Name: 'ComboBoxText 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.jscomboboxtext'});
|
|
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 |
이 예제의 모든 코드는 MessageDialogExample 클래스에 넣었습니다. 위 코드에서는 위젯과 창을 담아둘 <link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link>을 만들었습니다.
|
|
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: "Welcome to GNOME",
|
|
Packit |
1470ea |
default_width: 200,
|
|
Packit |
1470ea |
border_width: 10 });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_buildUI 함수는 프로그램 사용자 인터페이스를 만드는 모든 코드를 넣는 곳입니다. 첫 단계에서는 모든 위젯을 우겨넣을 새 <link xref="GtkApplicationWindow.js">Gtk.ApplicationWindow</link>를 만듭니다.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="comboboxtext">
|
|
Packit |
1470ea |
<title>ComboBoxText 만들기</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the combobox
|
|
Packit |
1470ea |
this._comboBoxText = new Gtk.ComboBoxText();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Populate the combobox
|
|
Packit |
1470ea |
let distros = ["Select distribution", "Fedora", "Mint", "Suse"];
|
|
Packit |
1470ea |
for (let i = 0; i < distros.length; i++)
|
|
Packit |
1470ea |
this._comboBoxText.append_text (distros[i]);
|
|
Packit |
1470ea |
this._comboBoxText.set_active (0);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect the combobox's 'changed' signal to our callback function
|
|
Packit |
1470ea |
this._comboBoxText.connect ('changed', Lang.bind (this, this._onComboChanged));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
ComboBoxText를 만들고 나서 <file>append_text</file> 메서드를 사용하여 텍스트 스트링을 0번부터 시작하는 숫자 ID를 가진 배열에 추가하겠습니다. 단순하게 갖출 목적으로 ComboBoxText 항목을 실제로 배열로 만든 다음에 순서대로 넣고, 넣은 방식대로 for 반복문으로 붙여넣겠습니다.
|
|
Packit |
1470ea |
ComboBoxText를 갖추고 나서 첫번째 항목을 표시하여 ComboBoxText를 누르기 전에 "Select distribution" 줄이 나타나게 하겠습니다. 그 다음 <file>changed</file> 시그널을 _onComboChanged 함수에 연결하여 드롭 다운 메뉴에서 새 항목을 선택할 때마다 호출하도록 하겠습니다.
|
|
Packit |
1470ea |
<note>ComboBoxText에 항목을 추가하려면 <file>insert_text</file> 메서드를 사용할 수 있습니다. 각 항목의 ID를 숫자만이 아닌 텍스트 문자열을 사용하려한다면 <file>append</file> 메서드와 <file>insert</file> 메서드를 사용할 수 있습니다. 활용 방안에 대한 자세한 지침서 내용은 하단 링크를 살펴보시지요. </note>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Add the combobox to the window
|
|
Packit |
1470ea |
this._window.add (this._comboBoxText);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Show the window and all child widgets
|
|
Packit |
1470ea |
this._window.show_all();
|
|
Packit |
1470ea |
},
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
마지막으로 ComboBoxText를 창에 추가하고, 창 자신과 위젯을 나타내게 하겠습니다.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="function">
|
|
Packit |
1470ea |
<title>선택을 처리하는 함수</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_onComboChanged: function () {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// The responses we'll use for our messagedialog
|
|
Packit |
1470ea |
let responses = ["",
|
|
Packit |
1470ea |
"Fedora is a community distro sponsored by Red Hat.",
|
|
Packit |
1470ea |
"Mint is a popular distro based on Ubuntu.",
|
|
Packit |
1470ea |
"SUSE is a name shared by two separate distros."];
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
배포판 선택 기반으로 메시지를 보여주는 <link xref="messagedialog.js">MessageDialog</link> 팝업을 만들려고 합니다. 우선, 사용할 응답 배열을 만들겠습니다. ComboBoxText의 첫 문자열은 그냥 "Select distribution" 메시지니까, 배열의 첫번째 문자열은 비워두겠습니다.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Which combobox item is active?
|
|
Packit |
1470ea |
let activeItem = this._comboBoxText.get_active();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// No messagedialog if you chose "Select distribution"
|
|
Packit |
1470ea |
if (activeItem != 0) {
|
|
Packit |
1470ea |
this._popUp = new Gtk.MessageDialog ({
|
|
Packit |
1470ea |
transient_for: this._window,
|
|
Packit |
1470ea |
modal: true,
|
|
Packit |
1470ea |
buttons: Gtk.ButtonsType.OK,
|
|
Packit |
1470ea |
message_type: Gtk.MessageType.INFO,
|
|
Packit |
1470ea |
text: responses[activeItem]});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect the OK button to a handler function
|
|
Packit |
1470ea |
this._popUp.connect ('response', Lang.bind (this, this._onDialogResponse));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Show the messagedialog
|
|
Packit |
1470ea |
this._popUp.show();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
},
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
MessageDialog를 나타내기 전, "Select distribution" 메시지를 선택하지 않았는지 우선 확인해야 합니다. 확인하고 나면 ComboBoxText의 선택 항목과 관련하여 배열의 항목 텍스트를 설정합니다. 선택 항목의 숫자 ID를 반환하는 <file>get_active</file> 메서드를 사용하겠습니다.
|
|
Packit |
1470ea |
<note><file>append</file>로 할당한 텍스트 ID를 반환하는 <file>get_active_id</file> 메서드 말고도, 선택한 항목의 완전한 텍스트를 반환하는 <file>get_active_text</file> 메서드가 있습니다. </note>
|
|
Packit |
1470ea |
MessageDialog를 만들고 나면, _onDialogResponse 함수에 반응 시그널을 연결하고 MessageDialog를 나타내게 하겠습니다.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_onDialogResponse: function () {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this._popUp.destroy ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
MessageDialog에는 확인 단추만 있기 때문에 response_id에서 어떤 단추를 눌렀는지 굳이 확인할 필요가 없습니다. 그냥 띄운 창을 닫기만 하면 됩니다.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Run the application
|
|
Packit |
1470ea |
let app = new ComboBoxTextExample ();
|
|
Packit |
1470ea |
app.application.run (ARGV);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
마지막으로, 코드 작성을 마무리한 ComboBoxTextExample 클래스의 새 인스턴스를 만들고, 프로그램 실행을 구성하겠습니다.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="complete">
|
|
Packit |
1470ea |
<title>완전한 코드 예제</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 ComboBoxTextExample {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the application itself
|
|
Packit |
1470ea |
constructor() {
|
|
Packit |
1470ea |
this.application = new Gtk.Application ({
|
|
Packit |
1470ea |
application_id: 'org.example.jscomboboxtext'});
|
|
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: "Welcome to GNOME",
|
|
Packit |
1470ea |
default_width: 200,
|
|
Packit |
1470ea |
border_width: 10 });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the combobox
|
|
Packit |
1470ea |
this._comboBoxText = new Gtk.ComboBoxText();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Populate the combobox
|
|
Packit |
1470ea |
let distros = ["Select distribution", "Fedora", "Mint", "Suse"];
|
|
Packit |
1470ea |
for (let i = 0; i < distros.length; i++)
|
|
Packit |
1470ea |
this._comboBoxText.append_text (distros[i]);
|
|
Packit |
1470ea |
this._comboBoxText.set_active (0);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect the combobox's 'changed' signal to our callback function
|
|
Packit |
1470ea |
this._comboBoxText.connect ('changed', this._onComboChanged.bind(this));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Add the combobox to the window
|
|
Packit |
1470ea |
this._window.add (this._comboBoxText);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Show the window and all child widgets
|
|
Packit |
1470ea |
this._window.show_all();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_onComboChanged() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// The responses we'll use for our messagedialog
|
|
Packit |
1470ea |
let responses = ["",
|
|
Packit |
1470ea |
"Fedora is a community distro sponsored by Red Hat.",
|
|
Packit |
1470ea |
"Mint is a popular distro based on Ubuntu.",
|
|
Packit |
1470ea |
"SUSE is a name shared by two separate distros."];
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Which combobox item is active?
|
|
Packit |
1470ea |
let activeItem = this._comboBoxText.get_active();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// No messagedialog if you chose "Select distribution"
|
|
Packit |
1470ea |
if (activeItem != 0) {
|
|
Packit |
1470ea |
this._popUp = new Gtk.MessageDialog ({
|
|
Packit |
1470ea |
transient_for: this._window,
|
|
Packit |
1470ea |
modal: true,
|
|
Packit |
1470ea |
buttons: Gtk.ButtonsType.OK,
|
|
Packit |
1470ea |
message_type: Gtk.MessageType.INFO,
|
|
Packit |
1470ea |
text: responses[activeItem]});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect the OK button to a handler function
|
|
Packit |
1470ea |
this._popUp.connect ('response', this._onDialogResponse.bind(this));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Show the messagedialog
|
|
Packit |
1470ea |
this._popUp.show();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_onDialogResponse() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this._popUp.destroy ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
};
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Run the application
|
|
Packit |
1470ea |
let app = new ComboBoxTextExample ();
|
|
Packit |
1470ea |
app.application.run (ARGV);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="in-depth">
|
|
Packit |
1470ea |
<title>자세한 문서</title>
|
|
Packit |
1470ea |
이 예제는 다음 참고자료가 필요합니다:
|
|
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.ComboBoxText.html">Gtk.ComboBoxText</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.MessageDialog.html">Gtk.MessageDialog</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|