3 ****************************************************************************** *
5 * Copyright (C) 1999-2007, International Business Machines
6 * Corporation and others. All Rights Reserved.
8 ****************************************************************************** *
9 * file name: gnomelayout.cpp
11 * created on: 09/04/2001
12 * created by: Eric R. Mader
17 #include FT_FREETYPE_H
19 #include "unicode/ustring.h"
20 #include "unicode/uscript.h"
22 #include "GnomeFontInstance.h"
24 #include "paragraph.h"
26 #include "GnomeGUISupport.h"
27 #include "GnomeFontMap.h"
28 #include "UnicodeReader.h"
29 #include "ScriptCompositeFontInstance.h"
31 #define ARRAY_LENGTH(array) (sizeof array / sizeof array[0])
40 static FT_Library engine;
41 static GnomeGUISupport *guiSupport;
42 static GnomeFontMap *fontMap;
43 static ScriptCompositeFontInstance *font;
45 static GSList *appList = NULL;
47 GtkWidget *newSample(const gchar *fileName);
48 void closeSample(GtkWidget *sample);
50 void showabout(GtkWidget */*widget*/, gpointer /*data*/)
53 const gchar *documentedBy[] = {NULL};
54 const gchar *writtenBy[] = {
59 aboutBox = gnome_about_new("Gnome Layout Sample",
61 "Copyright (C) 1998-2006 By International Business Machines Corporation and others. All Rights Reserved.",
62 "A simple demo of the ICU LayoutEngine.",
68 gtk_widget_show(aboutBox);
71 void notimpl(GtkObject */*object*/, gpointer /*data*/)
73 gnome_ok_dialog("Not implemented...");
76 gchar *prettyTitle(const gchar *path)
78 const gchar *name = g_basename(path);
79 gchar *title = g_strconcat("Gnome Layout Sample - ", name, NULL);
84 void openOK(GtkObject */*object*/, gpointer data)
86 GtkFileSelection *fileselection = GTK_FILE_SELECTION(data);
87 GtkWidget *app = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(fileselection), "app"));
88 Context *context = (Context *) gtk_object_get_data(GTK_OBJECT(app), "context");
89 gchar *fileName = g_strdup(gtk_file_selection_get_filename(fileselection));
92 gtk_widget_destroy(GTK_WIDGET(fileselection));
94 newPara = Paragraph::paragraphFactory(fileName, font, guiSupport);
96 if (newPara != NULL) {
97 gchar *title = prettyTitle(fileName);
98 GtkWidget *area = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(app), "area"));
100 if (context->paragraph != NULL) {
101 delete context->paragraph;
104 context->paragraph = newPara;
105 gtk_window_set_title(GTK_WINDOW(app), title);
107 gtk_widget_hide(area);
108 context->paragraph->breakLines(context->width, context->height);
109 gtk_widget_show_all(area);
117 void openfile(GtkObject */*object*/, gpointer data)
119 GtkWidget *app = GTK_WIDGET(data);
120 GtkWidget *fileselection;
122 GtkWidget *cancelButton;
125 gtk_file_selection_new("Open File");
127 gtk_object_set_data(GTK_OBJECT(fileselection), "app", app);
130 GTK_FILE_SELECTION(fileselection)->ok_button;
133 GTK_FILE_SELECTION(fileselection)->cancel_button;
135 gtk_signal_connect(GTK_OBJECT(fileselection), "destroy",
136 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
138 gtk_signal_connect(GTK_OBJECT(okButton), "clicked",
139 GTK_SIGNAL_FUNC(openOK), fileselection);
141 gtk_signal_connect_object(GTK_OBJECT(cancelButton), "clicked",
142 GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(fileselection));
144 gtk_window_set_modal(GTK_WINDOW(fileselection), TRUE);
145 gtk_widget_show(fileselection);
149 void newapp(GtkObject */*object*/, gpointer /*data*/)
151 GtkWidget *app = newSample("Sample.txt");
153 gtk_widget_show_all(app);
156 void closeapp(GtkWidget */*widget*/, gpointer data)
158 GtkWidget *app = GTK_WIDGET(data);
163 void shutdown(GtkObject */*object*/, gpointer /*data*/)
168 GnomeUIInfo fileMenu[] =
170 GNOMEUIINFO_MENU_NEW_ITEM((gchar *) "_New Sample",
171 (gchar *) "Create a new Gnome Layout Sample",
174 GNOMEUIINFO_MENU_OPEN_ITEM(openfile, NULL),
175 GNOMEUIINFO_SEPARATOR,
176 GNOMEUIINFO_MENU_CLOSE_ITEM(closeapp, NULL),
177 GNOMEUIINFO_MENU_EXIT_ITEM(shutdown, NULL),
181 GnomeUIInfo helpMenu[] =
183 // GNOMEUIINFO_HELP("gnomelayout"),
184 GNOMEUIINFO_MENU_ABOUT_ITEM(showabout, NULL),
188 GnomeUIInfo mainMenu[] =
190 GNOMEUIINFO_SUBTREE(N_((gchar *) "File"), fileMenu),
191 GNOMEUIINFO_SUBTREE(N_((gchar *) "Help"), helpMenu),
195 gint eventDelete(GtkWidget *widget, GdkEvent */*event*/, gpointer /*data*/)
199 // indicate that closeapp already destroyed the window
203 gint eventConfigure(GtkWidget */*widget*/, GdkEventConfigure *event, Context *context)
205 if (context->paragraph != NULL) {
206 context->width = event->width;
207 context->height = event->height;
209 if (context->width > 0 && context->height > 0) {
210 context->paragraph->breakLines(context->width, context->height);
217 gint eventExpose(GtkWidget *widget, GdkEvent */*event*/, Context *context)
219 if (context->paragraph != NULL) {
220 gint maxLines = context->paragraph->getLineCount() - 1;
221 gint firstLine = 0, lastLine = context->height / context->paragraph->getLineHeight();
222 GnomeSurface surface(widget);
224 context->paragraph->draw(&surface, firstLine, (maxLines < lastLine)? maxLines : lastLine);
230 GtkWidget *newSample(const gchar *fileName)
232 Context *context = new Context();
234 context->width = 600;
235 context->height = 400;
236 context->paragraph = Paragraph::paragraphFactory(fileName, font, guiSupport);
238 gchar *title = prettyTitle(fileName);
239 GtkWidget *app = gnome_app_new("gnomeLayout", title);
241 gtk_object_set_data(GTK_OBJECT(app), "context", context);
243 gtk_window_set_default_size(GTK_WINDOW(app), 600 - 24, 400);
245 gnome_app_create_menus_with_data(GNOME_APP(app), mainMenu, app);
247 gtk_signal_connect(GTK_OBJECT(app), "delete_event",
248 GTK_SIGNAL_FUNC(eventDelete), NULL);
250 GtkWidget *area = gtk_drawing_area_new();
251 gtk_object_set_data(GTK_OBJECT(app), "area", area);
253 GtkStyle *style = gtk_style_copy(gtk_widget_get_style(area));
255 for (int i = 0; i < 5; i += 1) {
256 style->fg[i] = style->white;
259 gtk_widget_set_style(area, style);
261 gnome_app_set_contents(GNOME_APP(app), area);
263 gtk_signal_connect(GTK_OBJECT(area),
265 GTK_SIGNAL_FUNC(eventExpose),
268 gtk_signal_connect(GTK_OBJECT(area),
270 GTK_SIGNAL_FUNC(eventConfigure),
273 appList = g_slist_prepend(appList, app);
280 void closeSample(GtkWidget *app)
282 Context *context = (Context *) gtk_object_get_data(GTK_OBJECT(app), "context");
284 if (context->paragraph != NULL) {
285 delete context->paragraph;
290 appList = g_slist_remove(appList, app);
292 gtk_widget_destroy(app);
294 if (appList == NULL) {
299 int main (int argc, char *argv[])
301 LEErrorCode fontStatus = LE_NO_ERROR;
305 FT_Init_FreeType(&engine);
307 gnome_init_with_popt_table("gnomelayout", "0.1", argc, argv, NULL, 0, &ptctx);
309 guiSupport = new GnomeGUISupport();
310 fontMap = new GnomeFontMap(engine, "FontMap.Gnome", 24, guiSupport, fontStatus);
311 font = new ScriptCompositeFontInstance(fontMap);
313 if (LE_FAILURE(fontStatus)) {
314 FT_Done_FreeType(engine);
318 const char *defaultArgs[] = {"Sample.txt", NULL};
319 const char **args = poptGetArgs(ptctx);
325 for (int i = 0; args[i] != NULL; i += 1) {
326 app = newSample(args[i]);
328 gtk_widget_show_all(app);
331 poptFreeContext(ptctx);
338 FT_Done_FreeType(engine);