]> git.saurik.com Git - apple/icu.git/blob - icuSources/samples/layout/gnomelayout.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / samples / layout / gnomelayout.cpp
1
2 /*
3 ****************************************************************************** *
4 *
5 * Copyright (C) 1999-2005, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *
8 ****************************************************************************** *
9 * file name: gnomelayout.cpp
10 *
11 * created on: 09/04/2001
12 * created by: Eric R. Mader
13 */
14
15 #include <gnome.h>
16 #include "freetype/freetype.h"
17
18 #include "unicode/ustring.h"
19 #include "unicode/uscript.h"
20
21 #include "GnomeFontInstance.h"
22
23 #include "paragraph.h"
24
25 #include "GnomeGUISupport.h"
26 #include "GnomeFontMap.h"
27 #include "UnicodeReader.h"
28 #include "ScriptCompositeFontInstance.h"
29
30 #define ARRAY_LENGTH(array) (sizeof array / sizeof array[0])
31
32 struct Context
33 {
34 long width;
35 long height;
36 Paragraph *paragraph;
37 };
38
39 static TT_Engine engine;
40 static GnomeGUISupport *guiSupport;
41 static GnomeFontMap *fontMap;
42 static ScriptCompositeFontInstance *font;
43
44 static GSList *appList = NULL;
45
46 GtkWidget *newSample(const gchar *fileName);
47 void closeSample(GtkWidget *sample);
48
49 void showabout(GtkWidget */*widget*/, gpointer /*data*/)
50 {
51 GtkWidget *aboutBox;
52 const gchar *writtenBy[] = {
53 "Eric Mader",
54 NULL
55 };
56
57 aboutBox = gnome_about_new("Gnome Layout Sample",
58 "0.1",
59 "Copyright (C) 1998-2002 By International Business Machines Corporation and others. All Rights Reserved.",
60 writtenBy,
61 "A simple demo of the ICU LayoutEngine.",
62 NULL);
63
64 gtk_widget_show(aboutBox);
65 }
66
67 void notimpl(GtkObject */*object*/, gpointer /*data*/)
68 {
69 gnome_ok_dialog("Not implemented...");
70 }
71
72 gchar *prettyTitle(const gchar *path)
73 {
74 gchar *name = g_basename(path);
75 gchar *title = g_strconcat("Gnome Layout Sample - ", name, NULL);
76
77 return title;
78 }
79
80 void openOK(GtkObject */*object*/, gpointer data)
81 {
82 GtkFileSelection *fileselection = GTK_FILE_SELECTION(data);
83 GtkWidget *app = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(fileselection), "app"));
84 Context *context = (Context *) gtk_object_get_data(GTK_OBJECT(app), "context");
85 gchar *fileName = g_strdup(gtk_file_selection_get_filename(fileselection));
86 Paragraph *newPara;
87
88 gtk_widget_destroy(GTK_WIDGET(fileselection));
89
90 newPara = Paragraph::paragraphFactory(fileName, font, guiSupport);
91
92 if (newPara != NULL) {
93 gchar *title = prettyTitle(fileName);
94 GtkWidget *area = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(app), "area"));
95
96 if (context->paragraph != NULL) {
97 delete context->paragraph;
98 }
99
100 context->paragraph = newPara;
101 gtk_window_set_title(GTK_WINDOW(app), title);
102
103 gtk_widget_hide(area);
104 context->paragraph->breakLines(context->width, context->height);
105 gtk_widget_show_all(area);
106
107 g_free(title);
108 }
109
110 g_free(fileName);
111 }
112
113 void openfile(GtkObject */*object*/, gpointer data)
114 {
115 GtkWidget *app = GTK_WIDGET(data);
116 GtkWidget *fileselection;
117 GtkWidget *okButton;
118 GtkWidget *cancelButton;
119
120 fileselection =
121 gtk_file_selection_new("Open File");
122
123 gtk_object_set_data(GTK_OBJECT(fileselection), "app", app);
124
125 okButton =
126 GTK_FILE_SELECTION(fileselection)->ok_button;
127
128 cancelButton =
129 GTK_FILE_SELECTION(fileselection)->cancel_button;
130
131 gtk_signal_connect(GTK_OBJECT(fileselection), "destroy",
132 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
133
134 gtk_signal_connect(GTK_OBJECT(okButton), "clicked",
135 GTK_SIGNAL_FUNC(openOK), fileselection);
136
137 gtk_signal_connect_object(GTK_OBJECT(cancelButton), "clicked",
138 GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(fileselection));
139
140 gtk_window_set_modal(GTK_WINDOW(fileselection), TRUE);
141 gtk_widget_show(fileselection);
142 gtk_main();
143 }
144
145 void newapp(GtkObject */*object*/, gpointer /*data*/)
146 {
147 GtkWidget *app = newSample("Sample.txt");
148
149 gtk_widget_show_all(app);
150 }
151
152 void closeapp(GtkWidget */*widget*/, gpointer data)
153 {
154 GtkWidget *app = GTK_WIDGET(data);
155
156 closeSample(app);
157 }
158
159 void shutdown(GtkObject */*object*/, gpointer /*data*/)
160 {
161 gtk_main_quit();
162 }
163
164 GnomeUIInfo fileMenu[] =
165 {
166 GNOMEUIINFO_MENU_NEW_ITEM((gchar *) "_New Sample",
167 (gchar *) "Create a new Gnome Layout Sample",
168 newapp, NULL),
169
170 GNOMEUIINFO_MENU_OPEN_ITEM(openfile, NULL),
171 GNOMEUIINFO_SEPARATOR,
172 GNOMEUIINFO_MENU_CLOSE_ITEM(closeapp, NULL),
173 GNOMEUIINFO_MENU_EXIT_ITEM(shutdown, NULL),
174 GNOMEUIINFO_END
175 };
176
177 GnomeUIInfo helpMenu[] =
178 {
179 // GNOMEUIINFO_HELP("gnomelayout"),
180 GNOMEUIINFO_MENU_ABOUT_ITEM(showabout, NULL),
181 GNOMEUIINFO_END
182 };
183
184 GnomeUIInfo mainMenu[] =
185 {
186 GNOMEUIINFO_SUBTREE(N_((gchar *) "File"), fileMenu),
187 GNOMEUIINFO_SUBTREE(N_((gchar *) "Help"), helpMenu),
188 GNOMEUIINFO_END
189 };
190
191 gint eventDelete(GtkWidget *widget, GdkEvent */*event*/, gpointer /*data*/)
192 {
193 closeSample(widget);
194
195 // indicate that closeapp already destroyed the window
196 return TRUE;
197 }
198
199 gint eventConfigure(GtkWidget */*widget*/, GdkEventConfigure *event, Context *context)
200 {
201 if (context->paragraph != NULL) {
202 context->width = event->width;
203 context->height = event->height;
204
205 if (context->width > 0 && context->height > 0) {
206 context->paragraph->breakLines(context->width, context->height);
207 }
208 }
209
210 return TRUE;
211 }
212
213 gint eventExpose(GtkWidget *widget, GdkEvent */*event*/, Context *context)
214 {
215 if (context->paragraph != NULL) {
216 gint maxLines = context->paragraph->getLineCount() - 1;
217 gint firstLine = 0, lastLine = context->height / context->paragraph->getLineHeight();
218 GnomeSurface surface(widget);
219
220 context->paragraph->draw(&surface, firstLine, (maxLines < lastLine)? maxLines : lastLine);
221 }
222
223 return TRUE;
224 }
225
226 GtkWidget *newSample(const gchar *fileName)
227 {
228 Context *context = new Context();
229
230 context->width = 600;
231 context->height = 400;
232 context->paragraph = Paragraph::paragraphFactory(fileName, font, guiSupport);
233
234 gchar *title = prettyTitle(fileName);
235 GtkWidget *app = gnome_app_new("gnomeLayout", title);
236
237 gtk_object_set_data(GTK_OBJECT(app), "context", context);
238
239 gtk_window_set_default_size(GTK_WINDOW(app), 600 - 24, 400);
240
241 gnome_app_create_menus_with_data(GNOME_APP(app), mainMenu, app);
242
243 gtk_signal_connect(GTK_OBJECT(app), "delete_event",
244 GTK_SIGNAL_FUNC(eventDelete), NULL);
245
246 GtkWidget *area = gtk_drawing_area_new();
247 gtk_object_set_data(GTK_OBJECT(app), "area", area);
248
249 GtkStyle *style = gtk_style_copy(gtk_widget_get_style(area));
250
251 for (int i = 0; i < 5; i += 1) {
252 style->fg[i] = style->white;
253 }
254
255 gtk_widget_set_style(area, style);
256
257 gnome_app_set_contents(GNOME_APP(app), area);
258
259 gtk_signal_connect(GTK_OBJECT(area),
260 "expose_event",
261 GTK_SIGNAL_FUNC(eventExpose),
262 context);
263
264 gtk_signal_connect(GTK_OBJECT(area),
265 "configure_event",
266 GTK_SIGNAL_FUNC(eventConfigure),
267 context);
268
269 appList = g_slist_prepend(appList, app);
270
271 g_free(title);
272
273 return app;
274 }
275
276 void closeSample(GtkWidget *app)
277 {
278 Context *context = (Context *) gtk_object_get_data(GTK_OBJECT(app), "context");
279
280 if (context->paragraph != NULL) {
281 delete context->paragraph;
282 }
283
284 delete context;
285
286 appList = g_slist_remove(appList, app);
287
288 gtk_widget_destroy(app);
289
290 if (appList == NULL) {
291 gtk_main_quit();
292 }
293 }
294
295 int main (int argc, char *argv[])
296 {
297 LEErrorCode fontStatus = LE_NO_ERROR;
298 GtkWidget *app;
299
300 TT_Init_FreeType(&engine);
301
302 gnome_init("gnomelayout", "0.1", argc, argv);
303
304 guiSupport = new GnomeGUISupport();
305 fontMap = new GnomeFontMap(engine, "FontMap.Gnome", 24, guiSupport, fontStatus);
306 font = new ScriptCompositeFontInstance(fontMap);
307
308 if (LE_FAILURE(fontStatus)) {
309 TT_Done_FreeType(engine);
310 return 1;
311 }
312
313 if (argc <= 1) {
314 app = newSample("Sample.txt");
315
316 gtk_widget_show_all(app);
317 } else {
318 for (int i = 1; i < argc; i += 1) {
319 app = newSample(argv[i]);
320
321 gtk_widget_show_all(app);
322 }
323 }
324
325 gtk_main();
326
327 delete font;
328 delete guiSupport;
329
330 TT_Done_FreeType(engine);
331
332 exit(0);
333 }