3 *******************************************************************************
5 * Copyright (C) 2016 and later: Unicode, Inc. and others.
6 * License & terms of use: http://www.unicode.org/copyright.html#License
8 *******************************************************************************
9 ****************************************************************************** *
11 * Copyright (C) 1999-2007, International Business Machines
12 * Corporation and others. All Rights Reserved.
14 ****************************************************************************** *
19 #include FT_FREETYPE_H
23 #include "gnomeglue.h"
34 typedef struct Context Context;
36 static FT_Library engine;
37 static gs_guiSupport *guiSupport;
38 static fm_fontMap *fontMap;
41 static GSList *appList = NULL;
43 GtkWidget *newSample(const gchar *fileName);
44 void closeSample(GtkWidget *sample);
46 static void showabout(GtkWidget *widget, gpointer data)
49 const gchar *documentedBy[] = {NULL};
50 const gchar *writtenBy[] = {
55 aboutBox = gnome_about_new("Gnome Layout Sample",
57 "Copyright (C) 1998-2006 By International Business Machines Corporation and others. All Rights Reserved.",
58 "A simple demo of the ICU LayoutEngine.",
64 gtk_widget_show(aboutBox);
68 static void notimpl(GtkObject *object, gpointer data)
70 gnome_ok_dialog("Not implemented...");
74 static gchar *prettyTitle(const gchar *path)
76 const gchar *name = g_basename(path);
77 gchar *title = g_strconcat("Gnome Layout Sample - ", name, NULL);
82 static void openOK(GtkObject *object, gpointer data)
84 GtkFileSelection *fileselection = GTK_FILE_SELECTION(data);
85 GtkWidget *app = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(fileselection), "app"));
86 Context *context = (Context *) gtk_object_get_data(GTK_OBJECT(app), "context");
87 gchar *fileName = g_strdup(gtk_file_selection_get_filename(fileselection));
90 gtk_widget_destroy(GTK_WIDGET(fileselection));
92 newPara = pf_factory(fileName, font, guiSupport);
94 if (newPara != NULL) {
95 gchar *title = prettyTitle(fileName);
96 GtkWidget *area = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(app), "area"));
98 if (context->paragraph != NULL) {
99 pf_close(context->paragraph);
102 context->paragraph = newPara;
103 gtk_window_set_title(GTK_WINDOW(app), title);
105 gtk_widget_hide(area);
106 pf_breakLines(context->paragraph, context->width, context->height);
107 gtk_widget_show_all(area);
115 static void openfile(GtkObject *object, gpointer data)
117 GtkWidget *app = GTK_WIDGET(data);
118 GtkWidget *fileselection;
120 GtkWidget *cancelButton;
123 gtk_file_selection_new("Open File");
125 gtk_object_set_data(GTK_OBJECT(fileselection), "app", app);
128 GTK_FILE_SELECTION(fileselection)->ok_button;
131 GTK_FILE_SELECTION(fileselection)->cancel_button;
133 gtk_signal_connect(GTK_OBJECT(fileselection), "destroy",
134 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
136 gtk_signal_connect(GTK_OBJECT(okButton), "clicked",
137 GTK_SIGNAL_FUNC(openOK), fileselection);
139 gtk_signal_connect_object(GTK_OBJECT(cancelButton), "clicked",
140 GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(fileselection));
142 gtk_window_set_modal(GTK_WINDOW(fileselection), TRUE);
143 gtk_widget_show(fileselection);
147 static void newapp(GtkObject *object, gpointer data)
149 GtkWidget *app = newSample("Sample.txt");
151 gtk_widget_show_all(app);
154 static void closeapp(GtkWidget *widget, gpointer data)
156 GtkWidget *app = GTK_WIDGET(data);
161 static void shutdown(GtkObject *object, gpointer data)
166 GnomeUIInfo fileMenu[] =
168 GNOMEUIINFO_MENU_NEW_ITEM((gchar *) "_New Sample",
169 (gchar *) "Create a new Gnome Layout Sample",
172 GNOMEUIINFO_MENU_OPEN_ITEM(openfile, NULL),
173 GNOMEUIINFO_SEPARATOR,
174 GNOMEUIINFO_MENU_CLOSE_ITEM(closeapp, NULL),
175 GNOMEUIINFO_MENU_EXIT_ITEM(shutdown, NULL),
179 GnomeUIInfo helpMenu[] =
181 /* GNOMEUIINFO_HELP("gnomelayout"), */
182 GNOMEUIINFO_MENU_ABOUT_ITEM(showabout, NULL),
186 GnomeUIInfo mainMenu[] =
188 GNOMEUIINFO_SUBTREE(N_((gchar *) "File"), fileMenu),
189 GNOMEUIINFO_SUBTREE(N_((gchar *) "Help"), helpMenu),
193 static gint eventDelete(GtkWidget *widget, GdkEvent *event, gpointer data)
197 /* indicate that closeapp already destroyed the window */
201 static gint eventConfigure(GtkWidget *widget, GdkEventConfigure *event, Context *context)
203 if (context->paragraph != NULL) {
204 context->width = event->width;
205 context->height = event->height;
207 if (context->width > 0 && context->height > 0) {
208 pf_breakLines(context->paragraph, context->width, context->height);
215 static gint eventExpose(GtkWidget *widget, GdkEvent *event, Context *context)
217 if (context->paragraph != NULL) {
218 gint maxLines = pf_getLineCount(context->paragraph) - 1;
219 gint firstLine = 0, lastLine = context->height / pf_getLineHeight(context->paragraph);
220 rs_surface *surface = rs_gnomeRenderingSurfaceOpen(widget);
222 pf_draw(context->paragraph, surface, firstLine, (maxLines < lastLine)? maxLines : lastLine);
224 rs_gnomeRenderingSurfaceClose(surface);
230 GtkWidget *newSample(const gchar *fileName)
232 Context *context = NEW_ARRAY(Context, 1);
239 context->width = 600;
240 context->height = 400;
241 context->paragraph = pf_factory(fileName, font, guiSupport);
243 title = prettyTitle(fileName);
244 app = gnome_app_new("gnomeLayout", title);
246 gtk_object_set_data(GTK_OBJECT(app), "context", context);
248 gtk_window_set_default_size(GTK_WINDOW(app), 600 - 24, 400);
250 gnome_app_create_menus_with_data(GNOME_APP(app), mainMenu, app);
252 gtk_signal_connect(GTK_OBJECT(app), "delete_event",
253 GTK_SIGNAL_FUNC(eventDelete), NULL);
255 area = gtk_drawing_area_new();
256 gtk_object_set_data(GTK_OBJECT(app), "area", area);
258 style = gtk_style_copy(gtk_widget_get_style(area));
260 for (i = 0; i < 5; i += 1) {
261 style->fg[i] = style->white;
264 gtk_widget_set_style(area, style);
266 gnome_app_set_contents(GNOME_APP(app), area);
268 gtk_signal_connect(GTK_OBJECT(area),
270 GTK_SIGNAL_FUNC(eventExpose),
273 gtk_signal_connect(GTK_OBJECT(area),
275 GTK_SIGNAL_FUNC(eventConfigure),
278 appList = g_slist_prepend(appList, app);
285 void closeSample(GtkWidget *app)
287 Context *context = (Context *) gtk_object_get_data(GTK_OBJECT(app), "context");
289 if (context->paragraph != NULL) {
290 pf_close(context->paragraph);
293 DELETE_ARRAY(context);
295 appList = g_slist_remove(appList, app);
297 gtk_widget_destroy(app);
299 if (appList == NULL) {
304 int main (int argc, char *argv[])
306 LEErrorCode fontStatus = LE_NO_ERROR;
309 const char *defaultArgs[] = {"Sample.txt", NULL};
313 FT_Init_FreeType(&engine);
315 gnome_init_with_popt_table("gnomelayout", "0.1", argc, argv, NULL, 0, &ptctx);
317 guiSupport = gs_gnomeGuiSupportOpen();
318 fontMap = fm_gnomeFontMapOpen(engine, "FontMap.Gnome", 24, guiSupport, &fontStatus);
319 font = le_scriptCompositeFontOpen(fontMap);
321 if (LE_FAILURE(fontStatus)) {
322 FT_Done_FreeType(engine);
326 args = poptGetArgs(ptctx);
332 for (i = 0; args[i] != NULL; i += 1) {
333 app = newSample(args[i]);
335 gtk_widget_show_all(app);
338 poptFreeContext(ptctx);
343 gs_gnomeGuiSupportClose(guiSupport);
345 FT_Done_FreeType(engine);