Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / system / ftmanager / ftmanager.c
blob8d64558cc30ad0e3d3e1947994a8c29b6fd1be5d
1 /*
2 * Based on source from ftmanager from MorphOS for their ft2.library
3 */
4 #define NO_INLINE_STDARG
6 #include <libraries/mui.h>
7 #include <libraries/asl.h>
9 #include <proto/exec.h>
10 #include <proto/dos.h>
11 #include <proto/intuition.h>
12 #include <proto/muimaster.h>
13 #undef NO_INLINE_STDARG
14 #include <proto/alib.h>
15 #include <proto/codesets.h>
16 #include <proto/freetype2.h>
17 #include <aros/debug.h>
19 #include "fontbitmap_class.h"
20 #include "fontinfo_class.h"
21 #include "fontlist_class.h"
22 #include "fontwindow_class.h"
23 #include "globals.h"
24 #include "locale.h"
26 #define ARG_TEMPLATE "TTFFONT/A,OUTFONT/A,CODEPAGE"
28 #define VERSION "$VER: FTManager 1.4 (22.5.2015) ©2011-2015 The AROS Development Team"
30 enum
32 ARG_TTFFONT,
33 ARG_OUTFONT,
34 ARG_CODEPAGE,
35 ARG_COUNT
38 /***********************************************************************/
40 /* Global variables */
41 BPTR destdir;
42 UWORD codepage[256];
43 struct Library *CodesetsBase;
45 /***********************************************************************/
47 static struct RDArgs *rda;
48 static Object *app;
49 static STRPTR *codesetentries;
50 static STRPTR *codesetsupported;
52 /***********************************************************************/
54 static void Cleanup(void)
56 if (rda)
57 FreeArgs(rda);
59 CleanupFontListClass();
60 CleanupFontWindowClass();
61 CleanupFontInfoClass();
62 CleanupFontBitmapClass();
64 FT_Done_Library(ftlibrary);
66 if (codesetsupported)
67 CodesetsFreeA(codesetsupported, NULL);
68 FreeVec(codesetentries);
69 CloseLibrary(CodesetsBase);
71 UnLock(destdir);
74 static int Init(BOOL gui)
76 FT_Error error;
78 CodesetsBase = OpenLibrary("codesets.library", 0);
79 if (!CodesetsBase)
80 return 0;
82 error = FT_Init_FreeType(&ftlibrary);
83 if (error != 0)
85 DEBUG_MAIN(dprintf("Init_FreeType error %d\n", error));
86 return 0;
89 if (gui)
91 if (!InitFontBitmapClass() ||
92 !InitFontInfoClass() ||
93 !InitFontWindowClass() ||
94 !InitFontListClass())
96 DEBUG_MAIN(dprintf("Can't create custom classes.\n"));
97 return 0;
101 destdir = Lock("Fonts:", ACCESS_READ);
103 return 1;
107 static void SetDefaultCodePage(void)
109 int k;
110 for (k = 0; k < 256; ++k)
111 codepage[k] = k;
114 BOOL IsDefaultCodePage(void)
116 int k;
117 for (k = 0; k < 256; ++k)
118 if (codepage[k] != k)
119 return FALSE;
120 return TRUE;
123 static BOOL LoadCodePage(int entryindex, char *codepg)
125 struct codeset *cs = NULL;
127 SetDefaultCodePage();
129 if (entryindex == 0 && codepg == NULL) // keep default code page
131 return TRUE;
134 if (codepg)
136 cs = CodesetsFind(codepg, CSA_FallbackToDefault, FALSE, TAG_DONE);
138 else if (entryindex > 0)
140 cs = CodesetsFind(codesetentries[entryindex],
141 CSA_FallbackToDefault, FALSE, TAG_DONE);
144 if (cs)
146 LONG index;
147 for (index = 0 ; index < 256 ; index++)
149 codepage[index] = (UWORD)cs->table[index].ucs4;
151 return TRUE;
154 return FALSE;
158 #define ID_SetSrc 1
159 #define ID_SetDestDir 2
160 #define ID_ShowFont 3
161 #define ID_SetCodePage 4
163 static int ftmanager_gui(void)
165 int ret = RETURN_FAIL;
166 Object *win, *src, *dest, *fontlist, *fontlv, *codepagecycle, *quit;
167 int countfrom, countto;
169 if (!Init(TRUE))
170 return RETURN_FAIL;
172 SetDefaultCodePage();
174 codesetsupported = CodesetsSupportedA(NULL); // Available codesets
175 countfrom = 0;
176 while (codesetsupported[countfrom])
178 countfrom++;
180 codesetentries = AllocVec((sizeof (STRPTR)) * (countfrom + 2), MEMF_CLEAR);
181 if (!codesetentries)
183 Cleanup();
184 return RETURN_FAIL;
186 codesetentries[0] = "----";
187 countfrom = 0;
188 countto = 1;
189 while (codesetsupported[countfrom])
191 if (strncmp(codesetsupported[countfrom], "UTF", 3))
193 codesetentries[countto] = codesetsupported[countfrom];
194 countto++;
196 countfrom++;
198 app = ApplicationObject,
199 MUIA_Application_Title, __(MSG_APP_NAME),
200 MUIA_Application_Version,(IPTR) VERSION,
201 MUIA_Application_Copyright, "Copyright 2002-2003 by Emmanuel Lesueur",
202 MUIA_Application_Author, "Emmanuel Lesueur",
203 MUIA_Application_Description, __(MSG_APP_TITLE),
204 MUIA_Application_Base, "FTMANAGER",
205 SubWindow, win = WindowObject,
206 MUIA_Window_ID, MAKE_ID('F','T','2','M'),
207 MUIA_Window_Title, __(MSG_WINDOW_TITLE),
208 MUIA_Window_Width, 400,
209 MUIA_Window_RootObject,VGroup,
210 Child, fontlv = ListviewObject,
211 MUIA_Listview_List, fontlist = FontListObject,
212 End,
213 End,
214 Child, ColGroup(2),
215 Child, Label2(_(MSG_LABEL_SOURCE)),
216 Child, PopaslObject,
217 MUIA_Popasl_Type, ASL_FileRequest,
218 MUIA_Popstring_String, src = StringObject,
219 StringFrame,
220 MUIA_String_Contents, __(MSG_ASL_FONTS_TRUETYPE),
221 MUIA_String_AdvanceOnCR, TRUE,
222 MUIA_CycleChain, TRUE,
223 End,
224 MUIA_Popstring_Button, PopButton(MUII_PopDrawer),
225 ASLFR_RejectIcons, TRUE,
226 ASLFR_DrawersOnly, TRUE,
227 End,
228 Child, Label2(_(MSG_LABEL_DESTINATION)),
229 Child, PopaslObject,
230 MUIA_Popasl_Type, ASL_FileRequest,
231 MUIA_Popstring_String, dest = StringObject,
232 StringFrame,
233 MUIA_String_Contents, __(MSG_ASL_FONTS),
234 MUIA_String_AdvanceOnCR, TRUE,
235 MUIA_CycleChain, TRUE,
236 End,
237 MUIA_Popstring_Button, PopButton(MUII_PopDrawer),
238 ASLFR_DoSaveMode, TRUE,
239 ASLFR_DrawersOnly, TRUE,
240 ASLFR_RejectIcons, TRUE,
241 End,
242 Child, Label2(_(MSG_LABEL_CODEPAGE)),
243 Child, codepagecycle = CycleObject,
244 MUIA_Cycle_Entries, codesetentries,
245 End,
246 End,
247 Child, quit = SimpleButton(_(MSG_QUIT)),
248 End,
249 End,
250 End;
252 if (app)
254 IPTR t;
256 ret = RETURN_OK;
258 DoMethod(src, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
259 fontlist, 2, MUIM_FontList_AddDir, MUIV_TriggerValue);
261 DoMethod(dest, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
262 app, 2, MUIM_Application_ReturnID, ID_SetDestDir);
264 DoMethod(codepagecycle, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime,
265 app, 2, MUIM_Application_ReturnID, ID_SetCodePage);
267 DoMethod(fontlv, MUIM_Notify, MUIA_Listview_DoubleClick, TRUE,
268 app, 2, MUIM_Application_ReturnID, ID_ShowFont);
270 DoMethod(win, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
271 app, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
273 DoMethod(quit, MUIM_Notify, MUIA_Pressed, FALSE,
274 app, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
276 DoMethod(fontlist, MUIM_FontList_AddDir, XGET(src, MUIA_String_Contents));
278 set(win, MUIA_Window_Open, TRUE);
279 t = 0;
280 get(win, MUIA_Window_Open, &t);
281 if (t)
283 BOOL running = TRUE;
284 ULONG sigs = 0;
285 ULONG id;
289 id = DoMethod(app, MUIM_Application_NewInput, &sigs);
290 switch (id)
292 case MUIV_Application_ReturnID_Quit:
293 running = FALSE;
294 sigs = 0;
295 break;
297 case ID_SetDestDir:
299 CONST_STRPTR name = NULL;
300 BPTR newdir;
302 get(dest, MUIA_String_Contents, &name);
303 if (!name)
304 break;
306 newdir = Lock(name, ACCESS_READ);
307 if (newdir)
309 UnLock(destdir);
310 destdir = newdir;
313 break;
315 case ID_ShowFont:
317 struct MUIS_FontList_Entry *entry;
318 Object *w;
320 DoMethod(fontlist, MUIM_List_GetEntry, MUIV_List_GetEntry_Active, &entry);
322 w = FontWindowObject,
323 MUIA_FontWindow_Filename, entry->FileName,
324 MUIA_UserData, app,
325 End;
327 if (w)
329 DoMethod(w, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
330 app, 6, MUIM_Application_PushMethod, app, 3,
331 MUIM_CallHook, &CloseWinHook, w);
333 DoMethod(app, OM_ADDMEMBER, w);
334 set(w, MUIA_Window_Open, TRUE);
335 get(w, MUIA_Window_Open, &t);
336 if (!t)
338 MUI_DisposeObject(w);
343 break;
345 case ID_SetCodePage:
347 IPTR entry = 0;
348 get(codepagecycle, MUIA_Cycle_Active, &entry);
349 LoadCodePage(entry, NULL);
351 break;
354 if (sigs)
356 sigs = Wait(sigs | SIGBREAKF_CTRL_C);
357 if (sigs & SIGBREAKF_CTRL_C)
359 running = FALSE;
363 while (running);
365 else
367 printf("Can't open window.\n");
370 MUI_DisposeObject(app);
372 else
374 printf("Can't create MUI application.\n");
377 Cleanup();
379 return ret;
382 static int ftmanager_cli(void)
384 // TODO implement me
385 return RETURN_FAIL;
388 int main(int argc, char **argv)
390 int retval = RETURN_FAIL;
392 Locale_Initialize();
394 if (argc > 1)
396 retval = ftmanager_cli();
398 else
400 // Starting from CLI without arguments opens GUI, too
401 retval = ftmanager_gui();
404 Locale_Deinitialize();
406 return retval;