3 *******************************************************************************
5 * © 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 ****************************************************************************** *
15 * file name: gnomelayout.cpp
17 * created on: 09/04/2001
18 * created by: Eric R. Mader
23 #include FT_FREETYPE_H
25 #include "unicode/ustring.h"
26 #include "unicode/uscript.h"
28 #include "GnomeFontInstance.h"
30 #include "paragraph.h"
32 #include "GnomeGUISupport.h"
33 #include "GnomeFontMap.h"
34 #include "UnicodeReader.h"
35 #include "ScriptCompositeFontInstance.h"
37 #define ARRAY_LENGTH(array) (sizeof array / sizeof array[0])
46 static FT_Library engine;
47 static GnomeGUISupport *guiSupport;
48 static GnomeFontMap *fontMap;
49 static ScriptCompositeFontInstance *font;
51 static GSList *appList = NULL;
53 GtkWidget *newSample(const gchar *fileName);
54 void closeSample(GtkWidget *sample);
56 void showabout(GtkWidget */*widget*/, gpointer /*data*/)
59 const gchar *documentedBy[] = {NULL};
60 const gchar *writtenBy[] = {
65 aboutBox = gnome_about_new("Gnome Layout Sample",
67 "Copyright (C) 1998-2006 By International Business Machines Corporation and others. All Rights Reserved.",
68 "A simple demo of the ICU LayoutEngine.",
74 gtk_widget_show(aboutBox);
77 void notimpl(GtkObject */*object*/, gpointer /*data*/)
79 gnome_ok_dialog("Not implemented...");
82 gchar *prettyTitle(const gchar *path)
84 const gchar *name = g_basename(path);
85 gchar *title = g_strconcat("Gnome Layout Sample - ", name, NULL);
90 void openOK(GtkObject */*object*/, gpointer data)
92 GtkFileSelection *fileselection = GTK_FILE_SELECTION(data);
93 GtkWidget *app = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(fileselection), "app"));
94 Context *context = (Context *) gtk_object_get_data(GTK_OBJECT(app), "context");
95 gchar *fileName = g_strdup(gtk_file_selection_get_filename(fileselection));
98 gtk_widget_destroy(GTK_WIDGET(fileselection));
100 newPara = Paragraph::paragraphFactory(fileName, font, guiSupport);
102 if (newPara != NULL) {
103 gchar *title = prettyTitle(fileName);
104 GtkWidget *area = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(app), "area"));
106 if (context->paragraph != NULL) {
107 delete context->paragraph;
110 context->paragraph = newPara;
111 gtk_window_set_title(GTK_WINDOW(app), title);
113 gtk_widget_hide(area);
114 context->paragraph->breakLines(context->width, context->height);
115 gtk_widget_show_all(area);
123 void openfile(GtkObject */*object*/, gpointer data)
125 GtkWidget *app = GTK_WIDGET(data);
126 GtkWidget *fileselection;
128 GtkWidget *cancelButton;
131 gtk_file_selection_new("Open File");
133 gtk_object_set_data(GTK_OBJECT(fileselection), "app", app);
136 GTK_FILE_SELECTION(fileselection)->ok_button;
139 GTK_FILE_SELECTION(fileselection)->cancel_button;
141 gtk_signal_connect(GTK_OBJECT(fileselection), "destroy",
142 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
144 gtk_signal_connect(GTK_OBJECT(okButton), "clicked",
145 GTK_SIGNAL_FUNC(openOK), fileselection);
147 gtk_signal_connect_object(GTK_OBJECT(cancelButton), "clicked",
148 GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(fileselection));
150 gtk_window_set_modal(GTK_WINDOW(fileselection), TRUE);
151 gtk_widget_show(fileselection);
155 void newapp(GtkObject */*object*/, gpointer /*data*/)
157 GtkWidget *app = newSample("Sample.txt");
159 gtk_widget_show_all(app);
162 void closeapp(GtkWidget */*widget*/, gpointer data)
164 GtkWidget *app = GTK_WIDGET(data);
169 void shutdown(GtkObject */*object*/, gpointer /*data*/)
174 GnomeUIInfo fileMenu[] =
176 GNOMEUIINFO_MENU_NEW_ITEM((gchar *) "_New Sample",
177 (gchar *) "Create a new Gnome Layout Sample",
180 GNOMEUIINFO_MENU_OPEN_ITEM(openfile, NULL),
181 GNOMEUIINFO_SEPARATOR,
182 GNOMEUIINFO_MENU_CLOSE_ITEM(closeapp, NULL),
183 GNOMEUIINFO_MENU_EXIT_ITEM(shutdown, NULL),
187 GnomeUIInfo helpMenu[] =
189 // GNOMEUIINFO_HELP("gnomelayout"),
190 GNOMEUIINFO_MENU_ABOUT_ITEM(showabout, NULL),
194 GnomeUIInfo mainMenu[] =
196 GNOMEUIINFO_SUBTREE(N_((gchar *) "File"), fileMenu),
197 GNOMEUIINFO_SUBTREE(N_((gchar *) "Help"), helpMenu),
201 gint eventDelete(GtkWidget *widget, GdkEvent */*event*/, gpointer /*data*/)
205 // indicate that closeapp already destroyed the window
209 gint eventConfigure(GtkWidget */*widget*/, GdkEventConfigure *event, Context *context)
211 if (context->paragraph != NULL) {
212 context->width = event->width;
213 context->height = event->height;
215 if (context->width > 0 && context->height > 0) {
216 context->paragraph->breakLines(context->width, context->height);
223 gint eventExpose(GtkWidget *widget, GdkEvent */*event*/, Context *context)
225 if (context->paragraph != NULL) {
226 gint maxLines = context->paragraph->getLineCount() - 1;
227 gint firstLine = 0, lastLine = context->height / context->paragraph->getLineHeight();
228 GnomeSurface surface(widget);
230 context->paragraph->draw(&surface, firstLine, (maxLines < lastLine)? maxLines : lastLine);
236 GtkWidget *newSample(const gchar *fileName)
238 Context *context = new Context();
240 context->width = 600;
241 context->height = 400;
242 context->paragraph = Paragraph::paragraphFactory(fileName, font, guiSupport);
244 gchar *title = prettyTitle(fileName);
245 GtkWidget *app = gnome_app_new("gnomeLayout", title);
247 gtk_object_set_data(GTK_OBJECT(app), "context", context);
249 gtk_window_set_default_size(GTK_WINDOW(app), 600 - 24, 400);
251 gnome_app_create_menus_with_data(GNOME_APP(app), mainMenu, app);
253 gtk_signal_connect(GTK_OBJECT(app), "delete_event",
254 GTK_SIGNAL_FUNC(eventDelete), NULL);
256 GtkWidget *area = gtk_drawing_area_new();
257 gtk_object_set_data(GTK_OBJECT(app), "area", area);
259 GtkStyle *style = gtk_style_copy(gtk_widget_get_style(area));
261 for (int i = 0; i < 5; i += 1) {
262 style->fg[i] = style->white;
265 gtk_widget_set_style(area, style);
267 gnome_app_set_contents(GNOME_APP(app), area);
269 gtk_signal_connect(GTK_OBJECT(area),
271 GTK_SIGNAL_FUNC(eventExpose),
274 gtk_signal_connect(GTK_OBJECT(area),
276 GTK_SIGNAL_FUNC(eventConfigure),
279 appList = g_slist_prepend(appList, app);
286 void closeSample(GtkWidget *app)
288 Context *context = (Context *) gtk_object_get_data(GTK_OBJECT(app), "context");
290 if (context->paragraph != NULL) {
291 delete context->paragraph;
296 appList = g_slist_remove(appList, app);
298 gtk_widget_destroy(app);
300 if (appList == NULL) {
305 int main (int argc, char *argv[])
307 LEErrorCode fontStatus = LE_NO_ERROR;
311 FT_Init_FreeType(&engine);
313 gnome_init_with_popt_table("gnomelayout", "0.1", argc, argv, NULL, 0, &ptctx);
315 guiSupport = new GnomeGUISupport();
316 fontMap = new GnomeFontMap(engine, "FontMap.Gnome", 24, guiSupport, fontStatus);
317 font = new ScriptCompositeFontInstance(fontMap);
319 if (LE_FAILURE(fontStatus)) {
320 FT_Done_FreeType(engine);
324 const char *defaultArgs[] = {"Sample.txt", NULL};
325 const char **args = poptGetArgs(ptctx);
331 for (int i = 0; args[i] != NULL; i += 1) {
332 app = newSample(args[i]);
334 gtk_widget_show_all(app);
337 poptFreeContext(ptctx);
344 FT_Done_FreeType(engine);