3 ****************************************************************************** *
5 * Copyright (C) 1999-2005, 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
16 #include "freetype/freetype.h"
18 #include "unicode/ustring.h"
19 #include "unicode/uscript.h"
21 #include "GnomeFontInstance.h"
23 #include "paragraph.h"
25 #include "GnomeGUISupport.h"
26 #include "GnomeFontMap.h"
27 #include "UnicodeReader.h"
28 #include "ScriptCompositeFontInstance.h"
30 #define ARRAY_LENGTH(array) (sizeof array / sizeof array[0])
39 static TT_Engine engine
;
40 static GnomeGUISupport
*guiSupport
;
41 static GnomeFontMap
*fontMap
;
42 static ScriptCompositeFontInstance
*font
;
44 static GSList
*appList
= NULL
;
46 GtkWidget
*newSample(const gchar
*fileName
);
47 void closeSample(GtkWidget
*sample
);
49 void showabout(GtkWidget */
*widget*/
, gpointer
/*data*/)
52 const gchar
*writtenBy
[] = {
57 aboutBox
= gnome_about_new("Gnome Layout Sample",
59 "Copyright (C) 1998-2002 By International Business Machines Corporation and others. All Rights Reserved.",
61 "A simple demo of the ICU LayoutEngine.",
64 gtk_widget_show(aboutBox
);
67 void notimpl(GtkObject */
*object*/
, gpointer
/*data*/)
69 gnome_ok_dialog("Not implemented...");
72 gchar
*prettyTitle(const gchar
*path
)
74 gchar
*name
= g_basename(path
);
75 gchar
*title
= g_strconcat("Gnome Layout Sample - ", name
, NULL
);
80 void openOK(GtkObject */
*object*/
, gpointer data
)
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
));
88 gtk_widget_destroy(GTK_WIDGET(fileselection
));
90 newPara
= Paragraph::paragraphFactory(fileName
, font
, guiSupport
);
92 if (newPara
!= NULL
) {
93 gchar
*title
= prettyTitle(fileName
);
94 GtkWidget
*area
= GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(app
), "area"));
96 if (context
->paragraph
!= NULL
) {
97 delete context
->paragraph
;
100 context
->paragraph
= newPara
;
101 gtk_window_set_title(GTK_WINDOW(app
), title
);
103 gtk_widget_hide(area
);
104 context
->paragraph
->breakLines(context
->width
, context
->height
);
105 gtk_widget_show_all(area
);
113 void openfile(GtkObject */
*object*/
, gpointer data
)
115 GtkWidget
*app
= GTK_WIDGET(data
);
116 GtkWidget
*fileselection
;
118 GtkWidget
*cancelButton
;
121 gtk_file_selection_new("Open File");
123 gtk_object_set_data(GTK_OBJECT(fileselection
), "app", app
);
126 GTK_FILE_SELECTION(fileselection
)->ok_button
;
129 GTK_FILE_SELECTION(fileselection
)->cancel_button
;
131 gtk_signal_connect(GTK_OBJECT(fileselection
), "destroy",
132 GTK_SIGNAL_FUNC(gtk_main_quit
), NULL
);
134 gtk_signal_connect(GTK_OBJECT(okButton
), "clicked",
135 GTK_SIGNAL_FUNC(openOK
), fileselection
);
137 gtk_signal_connect_object(GTK_OBJECT(cancelButton
), "clicked",
138 GTK_SIGNAL_FUNC(gtk_widget_destroy
), GTK_OBJECT(fileselection
));
140 gtk_window_set_modal(GTK_WINDOW(fileselection
), TRUE
);
141 gtk_widget_show(fileselection
);
145 void newapp(GtkObject */
*object*/
, gpointer
/*data*/)
147 GtkWidget
*app
= newSample("Sample.txt");
149 gtk_widget_show_all(app
);
152 void closeapp(GtkWidget */
*widget*/
, gpointer data
)
154 GtkWidget
*app
= GTK_WIDGET(data
);
159 void shutdown(GtkObject */
*object*/
, gpointer
/*data*/)
164 GnomeUIInfo fileMenu
[] =
166 GNOMEUIINFO_MENU_NEW_ITEM((gchar
*) "_New Sample",
167 (gchar
*) "Create a new Gnome Layout Sample",
170 GNOMEUIINFO_MENU_OPEN_ITEM(openfile
, NULL
),
171 GNOMEUIINFO_SEPARATOR
,
172 GNOMEUIINFO_MENU_CLOSE_ITEM(closeapp
, NULL
),
173 GNOMEUIINFO_MENU_EXIT_ITEM(shutdown
, NULL
),
177 GnomeUIInfo helpMenu
[] =
179 // GNOMEUIINFO_HELP("gnomelayout"),
180 GNOMEUIINFO_MENU_ABOUT_ITEM(showabout
, NULL
),
184 GnomeUIInfo mainMenu
[] =
186 GNOMEUIINFO_SUBTREE(N_((gchar
*) "File"), fileMenu
),
187 GNOMEUIINFO_SUBTREE(N_((gchar
*) "Help"), helpMenu
),
191 gint
eventDelete(GtkWidget
*widget
, GdkEvent */
*event*/
, gpointer
/*data*/)
195 // indicate that closeapp already destroyed the window
199 gint
eventConfigure(GtkWidget */
*widget*/
, GdkEventConfigure
*event
, Context
*context
)
201 if (context
->paragraph
!= NULL
) {
202 context
->width
= event
->width
;
203 context
->height
= event
->height
;
205 if (context
->width
> 0 && context
->height
> 0) {
206 context
->paragraph
->breakLines(context
->width
, context
->height
);
213 gint
eventExpose(GtkWidget
*widget
, GdkEvent */
*event*/
, Context
*context
)
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
);
220 context
->paragraph
->draw(&surface
, firstLine
, (maxLines
< lastLine
)? maxLines
: lastLine
);
226 GtkWidget
*newSample(const gchar
*fileName
)
228 Context
*context
= new Context();
230 context
->width
= 600;
231 context
->height
= 400;
232 context
->paragraph
= Paragraph::paragraphFactory(fileName
, font
, guiSupport
);
234 gchar
*title
= prettyTitle(fileName
);
235 GtkWidget
*app
= gnome_app_new("gnomeLayout", title
);
237 gtk_object_set_data(GTK_OBJECT(app
), "context", context
);
239 gtk_window_set_default_size(GTK_WINDOW(app
), 600 - 24, 400);
241 gnome_app_create_menus_with_data(GNOME_APP(app
), mainMenu
, app
);
243 gtk_signal_connect(GTK_OBJECT(app
), "delete_event",
244 GTK_SIGNAL_FUNC(eventDelete
), NULL
);
246 GtkWidget
*area
= gtk_drawing_area_new();
247 gtk_object_set_data(GTK_OBJECT(app
), "area", area
);
249 GtkStyle
*style
= gtk_style_copy(gtk_widget_get_style(area
));
251 for (int i
= 0; i
< 5; i
+= 1) {
252 style
->fg
[i
] = style
->white
;
255 gtk_widget_set_style(area
, style
);
257 gnome_app_set_contents(GNOME_APP(app
), area
);
259 gtk_signal_connect(GTK_OBJECT(area
),
261 GTK_SIGNAL_FUNC(eventExpose
),
264 gtk_signal_connect(GTK_OBJECT(area
),
266 GTK_SIGNAL_FUNC(eventConfigure
),
269 appList
= g_slist_prepend(appList
, app
);
276 void closeSample(GtkWidget
*app
)
278 Context
*context
= (Context
*) gtk_object_get_data(GTK_OBJECT(app
), "context");
280 if (context
->paragraph
!= NULL
) {
281 delete context
->paragraph
;
286 appList
= g_slist_remove(appList
, app
);
288 gtk_widget_destroy(app
);
290 if (appList
== NULL
) {
295 int main (int argc
, char *argv
[])
297 LEErrorCode fontStatus
= LE_NO_ERROR
;
300 TT_Init_FreeType(&engine
);
302 gnome_init("gnomelayout", "0.1", argc
, argv
);
304 guiSupport
= new GnomeGUISupport();
305 fontMap
= new GnomeFontMap(engine
, "FontMap.Gnome", 24, guiSupport
, fontStatus
);
306 font
= new ScriptCompositeFontInstance(fontMap
);
308 if (LE_FAILURE(fontStatus
)) {
309 TT_Done_FreeType(engine
);
314 app
= newSample("Sample.txt");
316 gtk_widget_show_all(app
);
318 for (int i
= 1; i
< argc
; i
+= 1) {
319 app
= newSample(argv
[i
]);
321 gtk_widget_show_all(app
);
330 TT_Done_FreeType(engine
);