Debian Codegraph @

src/af/xap/gtk/xap_UnixDlg_Encoding.cpp

raw
1 /* AbiSource Application Framework
2 * Copyright (C) 1998-2000 AbiSource, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA.
18 */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "ut_string.h"
25 #include "ut_debugmsg.h"
26 #include "xap_UnixDialogHelper.h"
27
28 #include "xap_App.h"
29 #include "xap_UnixApp.h"
30 #include "xap_Frame.h"
31
32 #include "xap_Dialog_Id.h"
33 #include "xap_UnixDlg_Encoding.h"
34
35 XAP_Dialog * XAP_UnixDialog_Encoding::static_constructor(XAP_DialogFactory * pFactory,
36 XAP_Dialog_Id id)
37 {
38 XAP_UnixDialog_Encoding * p = new XAP_UnixDialog_Encoding(pFactory,id);
39 return p;
40 }
41
42 XAP_UnixDialog_Encoding::XAP_UnixDialog_Encoding(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id)
43 : XAP_Dialog_Encoding(pDlgFactory,id)
44 {
45 }
46
47 XAP_UnixDialog_Encoding::~XAP_UnixDialog_Encoding(void)
48 {
49 }
50
51 void XAP_UnixDialog_Encoding::s_encoding_dblclicked(GtkTreeView * /*treeview*/,
52 GtkTreePath * /*arg1*/,
53 GtkTreeViewColumn * /*arg2*/,
54 XAP_UnixDialog_Encoding * me)
55 {
56 gtk_dialog_response (GTK_DIALOG(me->m_windowMain), GTK_RESPONSE_OK);
57 }
58
59 /*****************************************************************/
60
61 void XAP_UnixDialog_Encoding::runModal(XAP_Frame * pFrame)
62 {
63 // Build the window's widgets and arrange them
64 GtkWidget * mainWindow = _constructWindow();
65 UT_return_if_fail(mainWindow);
66
67 // Populate the window's data items
68 _populateWindowData();
69
70 switch ( abiRunModalDialog ( GTK_DIALOG(mainWindow), pFrame, this, GTK_RESPONSE_CANCEL, false ) )
71 {
72 case GTK_RESPONSE_OK:
73 event_Ok (); break;
74 default:
75 event_Cancel (); break;
76 }
77 abiDestroyWidget ( mainWindow ) ;
78 }
79
80 void XAP_UnixDialog_Encoding::event_Ok(void)
81 {
82 GtkTreeSelection * selection;
83 GtkTreeIter iter;
84 GtkTreeModel * model;
85
86 gint row = 0;
87
88 selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_listEncodings) );
89
90 // if there is no selection, or the selection's data (GtkListItem widget)
91 // is empty, return cancel. GTK can make this happen.
92 if ( !selection ||
93 !gtk_tree_selection_get_selected (selection, &model, &iter)
94 )
95 {
96 _setAnswer (XAP_Dialog_Encoding::a_CANCEL);
97 return;
98 }
99
100 // get the ID of the selected Type
101 gtk_tree_model_get (model, &iter, 1, &row, -1);
102
103 if (row >= 0) {
104 _setSelectionIndex(static_cast<UT_uint32>(row));
105 _setEncoding (_getAllEncodings()[row]);
106 _setAnswer (XAP_Dialog_Encoding::a_OK);
107 } else {
108 UT_ASSERT_NOT_REACHED();
109 _setAnswer (XAP_Dialog_Encoding::a_CANCEL);
110 }
111 }
112
113 void XAP_UnixDialog_Encoding::event_Cancel(void)
114 {
115 _setAnswer (XAP_Dialog_Encoding::a_CANCEL);
116 }
117
118 /*****************************************************************/
119
120 GtkWidget * XAP_UnixDialog_Encoding::_constructWindow(void)
121 {
122 const XAP_StringSet * pSS = m_pApp->getStringSet();
123 GtkCellRenderer *renderer;
124 GtkTreeViewColumn *column;
125
126 // load the dialog from the UI file
127 #if GTK_CHECK_VERSION(3,0,0)
128 GtkBuilder* builder = newDialogBuilder("xap_UnixDlg_Encoding.ui");
129 #else
130 GtkBuilder* builder = newDialogBuilder("xap_UnixDlg_Encoding-2.ui");
131 #endif
132
133 // Update our member variables with the important widgets that
134 // might need to be queried or altered later
135 m_windowMain = GTK_WIDGET(gtk_builder_get_object(builder, "xap_UnixDlg_Encoding"));
136 m_listEncodings = GTK_WIDGET(gtk_builder_get_object(builder, "encodingList"));
137
138 std::string s;
139 pSS->getValueUTF8(XAP_STRING_ID_DLG_UENC_EncTitle,s);
140 gtk_window_set_title (GTK_WINDOW(m_windowMain), s.c_str());
141 localizeLabelMarkup(GTK_WIDGET(gtk_builder_get_object(builder, "lblEncoding")), pSS, XAP_STRING_ID_DLG_UENC_EncLabel);
142
143 // add a column to our TreeViews
144
145 renderer = gtk_cell_renderer_text_new ();
146 column = gtk_tree_view_column_new_with_attributes ("Format",
147 renderer,
148 "text",
149 0,
150 NULL);
151 gtk_tree_view_append_column( GTK_TREE_VIEW(m_listEncodings), column);
152
153 // connect a dbl-clicked signal to the column
154
155 g_signal_connect_after(G_OBJECT(m_listEncodings),
156 "row-activated",
157 G_CALLBACK(s_encoding_dblclicked),
158 static_cast<gpointer>(this));
159
160 g_object_unref(G_OBJECT(builder));
161
162 return m_windowMain;
163 }
164
165 void XAP_UnixDialog_Encoding::_populateWindowData(void)
166 {
167 GtkListStore *model;
168 GtkTreeIter iter;
169
170 model = gtk_list_store_new (2,
171 G_TYPE_STRING,
172 G_TYPE_INT);
173
174 for (UT_uint32 i = 0; i < _getEncodingsCount(); i++)
175 {
176 const gchar* s = _getAllEncodings()[i];
177
178 // Add a new row to the model
179 gtk_list_store_append (model, &iter);
180
181 gtk_list_store_set (model, &iter,
182 0, s,
183 1, i,
184 -1);
185 }
186
187 gtk_tree_view_set_model(GTK_TREE_VIEW(m_listEncodings), reinterpret_cast<GtkTreeModel *>(model));
188
189 g_object_unref (model);
190
191 // now select first item in box
192 gtk_widget_grab_focus (m_listEncodings);
193 }