fix a typo reported by David Gilmore.
[AROS.git] / workbench / system / ftmanager / ftmanager.c
blobb3188400e45af41ead1ab74aafe5b5d11b2f08f9
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/utility.h>
12 #include <proto/intuition.h>
13 #include <proto/muimaster.h>
14 #undef NO_INLINE_STDARG
15 #include <proto/alib.h>
16 #include <proto/codesets.h>
17 #include <proto/freetype2.h>
18 #include <aros/debug.h>
20 #include "fontbitmap_class.h"
21 #include "fontinfo_class.h"
22 #include "fontlist_class.h"
23 #include "fontwindow_class.h"
24 #include "globals.h"
25 #include "locale.h"
27 #define VERSION "$VER: FTManager 1.5 (23.1.2018) (C) 2011-2018 The AROS Development Team"
29 /***********************************************************************/
31 /* Global variables */
32 BPTR destdir;
33 UWORD codepage[256];
34 struct Library *CodesetsBase;
36 /***********************************************************************/
38 static struct RDArgs *rda;
39 static Object *app;
40 static STRPTR *codesetentries;
41 static STRPTR *codesetsupported;
43 /***********************************************************************/
45 int ftmanager_cli(void);
47 /***********************************************************************/
49 static void Cleanup(void)
51 if (rda)
52 FreeArgs(rda);
54 CleanupFontListClass();
55 CleanupFontWindowClass();
56 CleanupFontInfoClass();
57 CleanupFontBitmapClass();
59 FT_Done_Library(ftlibrary);
61 if (codesetsupported)
62 CodesetsFreeA(codesetsupported, NULL);
63 FreeVec(codesetentries);
64 CloseLibrary(CodesetsBase);
66 UnLock(destdir);
69 static int Init(BOOL gui)
71 FT_Error error;
73 CodesetsBase = OpenLibrary("codesets.library", 0);
74 if (!CodesetsBase)
75 return 0;
77 error = FT_Init_FreeType(&ftlibrary);
78 if (error != 0)
80 DEBUG_MAIN(dprintf("Init_FreeType error %d\n", error));
81 return 0;
84 if (gui)
86 if (!InitFontBitmapClass() ||
87 !InitFontInfoClass() ||
88 !InitFontWindowClass() ||
89 !InitFontListClass())
91 DEBUG_MAIN(dprintf("Can't create custom classes.\n"));
92 return 0;
96 destdir = Lock("Fonts:", ACCESS_READ);
98 return 1;
102 static void SetDefaultCodePage(void)
104 int k;
105 for (k = 0; k < 256; ++k)
106 codepage[k] = k;
109 BOOL IsDefaultCodePage(void)
111 int k;
112 for (k = 0; k < 256; ++k)
113 if (codepage[k] != k)
114 return FALSE;
115 return TRUE;
118 static BOOL LoadCodePage(int entryindex, char *codepg)
120 struct codeset *cs = NULL;
122 SetDefaultCodePage();
124 if (entryindex == 0 && codepg == NULL) // keep default code page
126 return TRUE;
129 if (codepg)
131 cs = CodesetsFind(codepg, CSA_FallbackToDefault, FALSE, TAG_DONE);
133 else if (entryindex > 0)
135 cs = CodesetsFind(codesetentries[entryindex],
136 CSA_FallbackToDefault, FALSE, TAG_DONE);
139 if (cs)
141 LONG index;
142 for (index = 0 ; index < 256 ; index++)
144 codepage[index] = (UWORD)cs->table[index].ucs4;
146 return TRUE;
149 return FALSE;
153 #define ID_SetSrc 1
154 #define ID_SetDestDir 2
155 #define ID_ShowFont 3
156 #define ID_SetCodePage 4
158 static int ftmanager_gui(void)
160 int ret = RETURN_FAIL;
161 Object *win, *src, *dest, *fontlist, *fontlv, *codepagecycle, *quit;
162 int countfrom, countto;
164 if (!Init(TRUE))
165 return RETURN_FAIL;
168 codesetsupported = CodesetsSupportedA(NULL); // Available codesets
169 countfrom = 0;
170 while (codesetsupported[countfrom])
172 countfrom++;
174 codesetentries = AllocVec((sizeof (STRPTR)) * (countfrom + 2), MEMF_CLEAR);
175 if (!codesetentries)
177 Cleanup();
178 return RETURN_FAIL;
180 codesetentries[0] = "----";
181 countfrom = 0;
182 countto = 1;
183 while (codesetsupported[countfrom])
185 if (strncmp(codesetsupported[countfrom], "UTF", 3))
187 codesetentries[countto] = codesetsupported[countfrom];
188 countto++;
190 countfrom++;
192 app = ApplicationObject,
193 MUIA_Application_Title, __(MSG_APP_NAME),
194 MUIA_Application_Version,(IPTR) VERSION,
195 MUIA_Application_Copyright, "Copyright 2002-2003 by Emmanuel Lesueur",
196 MUIA_Application_Author, "Emmanuel Lesueur",
197 MUIA_Application_Description, __(MSG_APP_TITLE),
198 MUIA_Application_Base, "FTMANAGER",
199 SubWindow, win = WindowObject,
200 MUIA_Window_ID, MAKE_ID('F','T','2','M'),
201 MUIA_Window_Title, __(MSG_WINDOW_TITLE),
202 MUIA_Window_Width, 400,
203 MUIA_Window_RootObject,VGroup,
204 Child, fontlv = ListviewObject,
205 MUIA_Listview_List, fontlist = FontListObject,
206 End,
207 End,
208 Child, ColGroup(2),
209 Child, Label2(_(MSG_LABEL_SOURCE)),
210 Child, PopaslObject,
211 MUIA_Popasl_Type, ASL_FileRequest,
212 MUIA_Popstring_String, src = StringObject,
213 StringFrame,
214 MUIA_String_Contents, __(MSG_ASL_FONTS_TRUETYPE),
215 MUIA_String_AdvanceOnCR, TRUE,
216 MUIA_CycleChain, TRUE,
217 End,
218 MUIA_Popstring_Button, PopButton(MUII_PopDrawer),
219 ASLFR_RejectIcons, TRUE,
220 ASLFR_DrawersOnly, TRUE,
221 End,
222 Child, Label2(_(MSG_LABEL_DESTINATION)),
223 Child, PopaslObject,
224 MUIA_Popasl_Type, ASL_FileRequest,
225 MUIA_Popstring_String, dest = StringObject,
226 StringFrame,
227 MUIA_String_Contents, __(MSG_ASL_FONTS),
228 MUIA_String_AdvanceOnCR, TRUE,
229 MUIA_CycleChain, TRUE,
230 End,
231 MUIA_Popstring_Button, PopButton(MUII_PopDrawer),
232 ASLFR_DoSaveMode, TRUE,
233 ASLFR_DrawersOnly, TRUE,
234 ASLFR_RejectIcons, TRUE,
235 End,
236 Child, Label2(_(MSG_LABEL_CODEPAGE)),
237 Child, codepagecycle = CycleObject,
238 MUIA_Cycle_Entries, codesetentries,
239 End,
240 End,
241 Child, quit = SimpleButton(_(MSG_QUIT)),
242 End,
243 End,
244 End;
246 if (app)
248 IPTR t;
250 ret = RETURN_OK;
252 DoMethod(src, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
253 fontlist, 2, MUIM_FontList_AddDir, MUIV_TriggerValue);
255 DoMethod(dest, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
256 app, 2, MUIM_Application_ReturnID, ID_SetDestDir);
258 DoMethod(codepagecycle, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime,
259 app, 2, MUIM_Application_ReturnID, ID_SetCodePage);
261 DoMethod(fontlv, MUIM_Notify, MUIA_Listview_DoubleClick, TRUE,
262 app, 2, MUIM_Application_ReturnID, ID_ShowFont);
264 DoMethod(win, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
265 app, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
267 DoMethod(quit, MUIM_Notify, MUIA_Pressed, FALSE,
268 app, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
270 DoMethod(fontlist, MUIM_FontList_AddDir, XGET(src, MUIA_String_Contents));
272 set(win, MUIA_Window_Open, TRUE);
273 t = 0;
274 get(win, MUIA_Window_Open, &t);
275 if (t)
277 BOOL running = TRUE;
278 ULONG sigs = 0;
279 ULONG id;
283 id = DoMethod(app, MUIM_Application_NewInput, &sigs);
284 switch (id)
286 case MUIV_Application_ReturnID_Quit:
287 running = FALSE;
288 sigs = 0;
289 break;
291 case ID_SetDestDir:
293 CONST_STRPTR name = NULL;
294 BPTR newdir;
296 get(dest, MUIA_String_Contents, &name);
297 if (!name)
298 break;
300 newdir = Lock(name, ACCESS_READ);
301 if (newdir)
303 UnLock(destdir);
304 destdir = newdir;
307 break;
309 case ID_ShowFont:
311 struct MUIS_FontList_Entry *entry;
312 Object *w;
314 DoMethod(fontlist, MUIM_List_GetEntry, MUIV_List_GetEntry_Active, &entry);
316 w = FontWindowObject,
317 MUIA_FontWindow_Filename, entry->FileName,
318 MUIA_UserData, app,
319 End;
321 if (w)
323 DoMethod(w, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
324 app, 6, MUIM_Application_PushMethod, app, 3,
325 MUIM_CallHook, &CloseWinHook, w);
327 DoMethod(app, OM_ADDMEMBER, w);
328 set(w, MUIA_Window_Open, TRUE);
329 get(w, MUIA_Window_Open, &t);
330 if (!t)
332 MUI_DisposeObject(w);
337 break;
339 case ID_SetCodePage:
341 IPTR entry = 0;
342 get(codepagecycle, MUIA_Cycle_Active, &entry);
343 LoadCodePage(entry, NULL);
345 break;
348 if (sigs)
350 sigs = Wait(sigs | SIGBREAKF_CTRL_C);
351 if (sigs & SIGBREAKF_CTRL_C)
353 running = FALSE;
357 while (running);
359 else
361 printf("Can't open window.\n");
364 MUI_DisposeObject(app);
366 else
368 printf("Can't create MUI application.\n");
371 Cleanup();
373 return ret;
377 int main(int argc, char **argv)
379 int retval = RETURN_FAIL;
381 Locale_Initialize();
383 SetDefaultCodePage();
385 if (argc > 1)
387 retval = ftmanager_cli();
389 else
391 // Starting from CLI without arguments opens GUI, too
392 retval = ftmanager_gui();
395 Locale_Deinitialize();
397 return retval;