]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/layout/cgnomelayout.c
3 ****************************************************************************** *
5 * Copyright (C) 1999-2007, International Business Machines
6 * Corporation and others. All Rights Reserved.
8 ****************************************************************************** *
13 #include FT_FREETYPE_H
17 #include "gnomeglue.h"
28 typedef struct Context Context
;
30 static FT_Library engine
;
31 static gs_guiSupport
*guiSupport
;
32 static fm_fontMap
*fontMap
;
35 static GSList
*appList
= NULL
;
37 GtkWidget
*newSample(const gchar
*fileName
);
38 void closeSample(GtkWidget
*sample
);
40 static void showabout(GtkWidget
*widget
, gpointer data
)
43 const gchar
*documentedBy
[] = {NULL
};
44 const gchar
*writtenBy
[] = {
49 aboutBox
= gnome_about_new("Gnome Layout Sample",
51 "Copyright (C) 1998-2006 By International Business Machines Corporation and others. All Rights Reserved.",
52 "A simple demo of the ICU LayoutEngine.",
58 gtk_widget_show(aboutBox
);
62 static void notimpl(GtkObject
*object
, gpointer data
)
64 gnome_ok_dialog("Not implemented...");
68 static gchar
*prettyTitle(const gchar
*path
)
70 const gchar
*name
= g_basename(path
);
71 gchar
*title
= g_strconcat("Gnome Layout Sample - ", name
, NULL
);
76 static void openOK(GtkObject
*object
, gpointer data
)
78 GtkFileSelection
*fileselection
= GTK_FILE_SELECTION(data
);
79 GtkWidget
*app
= GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(fileselection
), "app"));
80 Context
*context
= (Context
*) gtk_object_get_data(GTK_OBJECT(app
), "context");
81 gchar
*fileName
= g_strdup(gtk_file_selection_get_filename(fileselection
));
84 gtk_widget_destroy(GTK_WIDGET(fileselection
));
86 newPara
= pf_factory(fileName
, font
, guiSupport
);
88 if (newPara
!= NULL
) {
89 gchar
*title
= prettyTitle(fileName
);
90 GtkWidget
*area
= GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(app
), "area"));
92 if (context
->paragraph
!= NULL
) {
93 pf_close(context
->paragraph
);
96 context
->paragraph
= newPara
;
97 gtk_window_set_title(GTK_WINDOW(app
), title
);
99 gtk_widget_hide(area
);
100 pf_breakLines(context
->paragraph
, context
->width
, context
->height
);
101 gtk_widget_show_all(area
);
109 static void openfile(GtkObject
*object
, gpointer data
)
111 GtkWidget
*app
= GTK_WIDGET(data
);
112 GtkWidget
*fileselection
;
114 GtkWidget
*cancelButton
;
117 gtk_file_selection_new("Open File");
119 gtk_object_set_data(GTK_OBJECT(fileselection
), "app", app
);
122 GTK_FILE_SELECTION(fileselection
)->ok_button
;
125 GTK_FILE_SELECTION(fileselection
)->cancel_button
;
127 gtk_signal_connect(GTK_OBJECT(fileselection
), "destroy",
128 GTK_SIGNAL_FUNC(gtk_main_quit
), NULL
);
130 gtk_signal_connect(GTK_OBJECT(okButton
), "clicked",
131 GTK_SIGNAL_FUNC(openOK
), fileselection
);
133 gtk_signal_connect_object(GTK_OBJECT(cancelButton
), "clicked",
134 GTK_SIGNAL_FUNC(gtk_widget_destroy
), GTK_OBJECT(fileselection
));
136 gtk_window_set_modal(GTK_WINDOW(fileselection
), TRUE
);
137 gtk_widget_show(fileselection
);
141 static void newapp(GtkObject
*object
, gpointer data
)
143 GtkWidget
*app
= newSample("Sample.txt");
145 gtk_widget_show_all(app
);
148 static void closeapp(GtkWidget
*widget
, gpointer data
)
150 GtkWidget
*app
= GTK_WIDGET(data
);
155 static void shutdown(GtkObject
*object
, gpointer data
)
160 GnomeUIInfo fileMenu
[] =
162 GNOMEUIINFO_MENU_NEW_ITEM((gchar
*) "_New Sample",
163 (gchar
*) "Create a new Gnome Layout Sample",
166 GNOMEUIINFO_MENU_OPEN_ITEM(openfile
, NULL
),
167 GNOMEUIINFO_SEPARATOR
,
168 GNOMEUIINFO_MENU_CLOSE_ITEM(closeapp
, NULL
),
169 GNOMEUIINFO_MENU_EXIT_ITEM(shutdown
, NULL
),
173 GnomeUIInfo helpMenu
[] =
175 /* GNOMEUIINFO_HELP("gnomelayout"), */
176 GNOMEUIINFO_MENU_ABOUT_ITEM(showabout
, NULL
),
180 GnomeUIInfo mainMenu
[] =
182 GNOMEUIINFO_SUBTREE(N_((gchar
*) "File"), fileMenu
),
183 GNOMEUIINFO_SUBTREE(N_((gchar
*) "Help"), helpMenu
),
187 static gint
eventDelete(GtkWidget
*widget
, GdkEvent
*event
, gpointer data
)
191 /* indicate that closeapp already destroyed the window */
195 static gint
eventConfigure(GtkWidget
*widget
, GdkEventConfigure
*event
, Context
*context
)
197 if (context
->paragraph
!= NULL
) {
198 context
->width
= event
->width
;
199 context
->height
= event
->height
;
201 if (context
->width
> 0 && context
->height
> 0) {
202 pf_breakLines(context
->paragraph
, context
->width
, context
->height
);
209 static gint
eventExpose(GtkWidget
*widget
, GdkEvent
*event
, Context
*context
)
211 if (context
->paragraph
!= NULL
) {
212 gint maxLines
= pf_getLineCount(context
->paragraph
) - 1;
213 gint firstLine
= 0, lastLine
= context
->height
/ pf_getLineHeight(context
->paragraph
);
214 rs_surface
*surface
= rs_gnomeRenderingSurfaceOpen(widget
);
216 pf_draw(context
->paragraph
, surface
, firstLine
, (maxLines
< lastLine
)? maxLines
: lastLine
);
218 rs_gnomeRenderingSurfaceClose(surface
);
224 GtkWidget
*newSample(const gchar
*fileName
)
226 Context
*context
= NEW_ARRAY(Context
, 1);
233 context
->width
= 600;
234 context
->height
= 400;
235 context
->paragraph
= pf_factory(fileName
, font
, guiSupport
);
237 title
= prettyTitle(fileName
);
238 app
= gnome_app_new("gnomeLayout", title
);
240 gtk_object_set_data(GTK_OBJECT(app
), "context", context
);
242 gtk_window_set_default_size(GTK_WINDOW(app
), 600 - 24, 400);
244 gnome_app_create_menus_with_data(GNOME_APP(app
), mainMenu
, app
);
246 gtk_signal_connect(GTK_OBJECT(app
), "delete_event",
247 GTK_SIGNAL_FUNC(eventDelete
), NULL
);
249 area
= gtk_drawing_area_new();
250 gtk_object_set_data(GTK_OBJECT(app
), "area", area
);
252 style
= gtk_style_copy(gtk_widget_get_style(area
));
254 for (i
= 0; i
< 5; i
+= 1) {
255 style
->fg
[i
] = style
->white
;
258 gtk_widget_set_style(area
, style
);
260 gnome_app_set_contents(GNOME_APP(app
), area
);
262 gtk_signal_connect(GTK_OBJECT(area
),
264 GTK_SIGNAL_FUNC(eventExpose
),
267 gtk_signal_connect(GTK_OBJECT(area
),
269 GTK_SIGNAL_FUNC(eventConfigure
),
272 appList
= g_slist_prepend(appList
, app
);
279 void closeSample(GtkWidget
*app
)
281 Context
*context
= (Context
*) gtk_object_get_data(GTK_OBJECT(app
), "context");
283 if (context
->paragraph
!= NULL
) {
284 pf_close(context
->paragraph
);
287 DELETE_ARRAY(context
);
289 appList
= g_slist_remove(appList
, app
);
291 gtk_widget_destroy(app
);
293 if (appList
== NULL
) {
298 int main (int argc
, char *argv
[])
300 LEErrorCode fontStatus
= LE_NO_ERROR
;
303 const char *defaultArgs
[] = {"Sample.txt", NULL
};
307 FT_Init_FreeType(&engine
);
309 gnome_init_with_popt_table("gnomelayout", "0.1", argc
, argv
, NULL
, 0, &ptctx
);
311 guiSupport
= gs_gnomeGuiSupportOpen();
312 fontMap
= fm_gnomeFontMapOpen(engine
, "FontMap.Gnome", 24, guiSupport
, &fontStatus
);
313 font
= le_scriptCompositeFontOpen(fontMap
);
315 if (LE_FAILURE(fontStatus
)) {
316 FT_Done_FreeType(engine
);
320 args
= poptGetArgs(ptctx
);
326 for (i
= 0; args
[i
] != NULL
; i
+= 1) {
327 app
= newSample(args
[i
]);
329 gtk_widget_show_all(app
);
332 poptFreeContext(ptctx
);
337 gs_gnomeGuiSupportClose(guiSupport
);
339 FT_Done_FreeType(engine
);