Fix typo causing incorrect desktop sorting
[AROS.git] / workbench / system / ftmanager / ftmanager.c
blobe2085f6b6c3b67ee4ffa7b020f69c980f473c11f
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/codesets.h>
15 #include <proto/freetype2.h>
16 #include <aros/debug.h>
18 #include "fontbitmap_class.h"
19 #include "fontinfo_class.h"
20 #include "fontlist_class.h"
21 #include "fontwindow_class.h"
22 #include "globals.h"
24 #define ARG_TEMPLATE "TTFFONT/A,OUTFONT/A,CODEPAGE"
26 enum
28 ARG_TTFFONT,
29 ARG_OUTFONT,
30 ARG_CODEPAGE,
31 ARG_COUNT
34 /***********************************************************************/
36 /* Global variables */
37 BPTR destdir;
38 UWORD codepage[256];
39 struct Library *CodesetsBase;
41 /***********************************************************************/
43 static struct RDArgs *rda;
44 static Object *app;
45 static STRPTR *codesetentries;
46 static STRPTR *codesetsupported;
48 /***********************************************************************/
50 static void Cleanup(void)
52 if (rda)
53 FreeArgs(rda);
55 CleanupFontListClass();
56 CleanupFontWindowClass();
57 CleanupFontInfoClass();
58 CleanupFontBitmapClass();
60 FT_Done_Library(ftlibrary);
62 if (codesetsupported)
63 CodesetsFreeA(codesetsupported, NULL);
64 FreeVec(codesetentries);
65 CloseLibrary(CodesetsBase);
67 UnLock(destdir);
70 static int Init(BOOL gui)
72 FT_Error error;
74 CodesetsBase = OpenLibrary("codesets.library", 0);
75 if (!CodesetsBase)
76 return 0;
78 error = FT_Init_FreeType(&ftlibrary);
79 if (error != 0)
81 DEBUG_MAIN(dprintf("Init_FreeType error %d\n", error));
82 return 0;
85 if (gui)
87 if (!InitFontBitmapClass() ||
88 !InitFontInfoClass() ||
89 !InitFontWindowClass() ||
90 !InitFontListClass())
92 DEBUG_MAIN(dprintf("Can't create custom classes.\n"));
93 return 0;
97 destdir = Lock("Fonts:", ACCESS_READ);
99 return 1;
103 static void SetDefaultCodePage(void)
105 int k;
106 for (k = 0; k < 256; ++k)
107 codepage[k] = k;
110 BOOL IsDefaultCodePage(void)
112 int k;
113 for (k = 0; k < 256; ++k)
114 if (codepage[k] != k)
115 return FALSE;
116 return TRUE;
119 static BOOL LoadCodePage(int entryindex, char *codepg)
121 struct codeset *cs = NULL;
123 SetDefaultCodePage();
125 if (entryindex == 0 && codepg == NULL) // keep default code page
127 return TRUE;
130 if (codepg)
132 cs = CodesetsFind(codepg, CSA_FallbackToDefault, FALSE, TAG_DONE);
134 else if (entryindex > 0)
136 cs = CodesetsFind(codesetentries[entryindex],
137 CSA_FallbackToDefault, FALSE, TAG_DONE);
140 if (cs)
142 LONG index;
143 for (index = 0 ; index < 256 ; index++)
145 codepage[index] = (UWORD)cs->table[index].ucs4;
147 return TRUE;
150 return FALSE;
154 #define ID_SetSrc 1
155 #define ID_SetDestDir 2
156 #define ID_ShowFont 3
157 #define ID_SetCodePage 4
159 static int ftmanager_gui(void)
161 int ret = RETURN_FAIL;
162 Object *win, *src, *dest, *fontlist, *fontlv, *codepagecycle, *quit;
163 int countfrom, countto;
165 if (!Init(TRUE))
166 return RETURN_FAIL;
168 SetDefaultCodePage();
170 codesetsupported = CodesetsSupportedA(NULL); // Available codesets
171 countfrom = 0;
172 while (codesetsupported[countfrom])
174 countfrom++;
176 codesetentries = AllocVec((sizeof (STRPTR)) * (countfrom + 2), MEMF_CLEAR);
177 if (!codesetentries)
179 Cleanup();
180 return RETURN_FAIL;
182 codesetentries[0] = "----";
183 countfrom = 0;
184 countto = 1;
185 while (codesetsupported[countfrom])
187 if (strncmp(codesetsupported[countfrom], "UTF", 3))
189 codesetentries[countto] = codesetsupported[countfrom];
190 countto++;
192 countfrom++;
194 app = ApplicationObject,
195 MUIA_Application_Title, "FTManager",
196 MUIA_Application_Version, "$VER: FTManager 1.2 (4.12.2007)",
197 MUIA_Application_Copyright, "Copyright 2002-2003 by Emmanuel Lesueur",
198 MUIA_Application_Author, "Emmanuel Lesueur",
199 MUIA_Application_Description, "FreeType font manager",
200 MUIA_Application_Base, "FTMANAGER",
201 SubWindow, win = WindowObject,
202 MUIA_Window_ID, MAKE_ID('F','T','2','M'),
203 MUIA_Window_Title, "FreeType Font Manager",
204 MUIA_Window_Width, 400,
205 MUIA_Window_RootObject,VGroup,
206 Child, fontlv = ListviewObject,
207 MUIA_Listview_List, fontlist = FontListObject,
208 End,
209 End,
210 Child, ColGroup(2),
211 Child, Label2("Source"),
212 Child, PopaslObject,
213 MUIA_Popasl_Type, ASL_FileRequest,
214 MUIA_Popstring_String, src = StringObject,
215 StringFrame,
216 MUIA_String_Contents, "Fonts:TrueType",
217 MUIA_String_AdvanceOnCR, TRUE,
218 MUIA_CycleChain, TRUE,
219 End,
220 MUIA_Popstring_Button, PopButton(MUII_PopDrawer),
221 ASLFR_RejectIcons, TRUE,
222 ASLFR_DrawersOnly, TRUE,
223 End,
224 Child, Label2("Destination"),
225 Child, PopaslObject,
226 MUIA_Popasl_Type, ASL_FileRequest,
227 MUIA_Popstring_String, dest = StringObject,
228 StringFrame,
229 MUIA_String_Contents, "Fonts:",
230 MUIA_String_AdvanceOnCR, TRUE,
231 MUIA_CycleChain, TRUE,
232 End,
233 MUIA_Popstring_Button, PopButton(MUII_PopDrawer),
234 ASLFR_DoSaveMode, TRUE,
235 ASLFR_DrawersOnly, TRUE,
236 ASLFR_RejectIcons, TRUE,
237 End,
238 Child, Label2("Codepage"),
239 Child, codepagecycle = CycleObject,
240 MUIA_Cycle_Entries, codesetentries,
241 End,
242 End,
243 Child, quit = SimpleButton("_Quit"),
244 End,
245 End,
246 End;
248 if (app)
250 ULONG t;
252 ret = RETURN_OK;
254 DoMethod(src, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
255 fontlist, 2, MUIM_FontList_AddDir, MUIV_TriggerValue);
257 DoMethod(dest, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
258 app, 2, MUIM_Application_ReturnID, ID_SetDestDir);
260 DoMethod(codepagecycle, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime,
261 app, 2, MUIM_Application_ReturnID, ID_SetCodePage);
263 DoMethod(fontlv, MUIM_Notify, MUIA_Listview_DoubleClick, TRUE,
264 app, 2, MUIM_Application_ReturnID, ID_ShowFont);
266 DoMethod(win, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
267 app, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
269 DoMethod(quit, MUIM_Notify, MUIA_Pressed, FALSE,
270 app, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
272 DoMethod(fontlist, MUIM_FontList_AddDir, XGET(src, MUIA_String_Contents));
274 set(win, MUIA_Window_Open, TRUE);
275 get(win, MUIA_Window_Open, &t);
276 if (t)
278 BOOL running = TRUE;
279 ULONG sigs = 0;
280 ULONG id;
284 id = DoMethod(app, MUIM_Application_NewInput, &sigs);
285 switch (id)
287 case MUIV_Application_ReturnID_Quit:
288 running = FALSE;
289 sigs = 0;
290 break;
292 case ID_SetDestDir:
294 STRPTR name;
295 BPTR newdir;
297 get(dest, MUIA_String_Contents, &name);
298 newdir = Lock(name, ACCESS_READ);
299 if (newdir)
301 UnLock(destdir);
302 destdir = newdir;
305 break;
307 case ID_ShowFont:
309 struct MUIS_FontList_Entry *entry;
310 Object *w;
312 DoMethod(fontlist, MUIM_List_GetEntry, MUIV_List_GetEntry_Active, &entry);
314 w = FontWindowObject,
315 MUIA_FontWindow_Filename, entry->FileName,
316 MUIA_UserData, app,
317 End;
319 if (w)
321 DoMethod(w, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
322 app, 6, MUIM_Application_PushMethod, app, 3,
323 MUIM_CallHook, &CloseWinHook, w);
325 DoMethod(app, OM_ADDMEMBER, w);
326 set(w, MUIA_Window_Open, TRUE);
327 get(w, MUIA_Window_Open, &t);
328 if (!t)
330 MUI_DisposeObject(w);
335 break;
337 case ID_SetCodePage:
339 IPTR entry;
340 get(codepagecycle, MUIA_Cycle_Active, &entry);
341 LoadCodePage(entry, NULL);
343 break;
346 if (sigs)
348 sigs = Wait(sigs | SIGBREAKF_CTRL_C);
349 if (sigs & SIGBREAKF_CTRL_C)
351 running = FALSE;
355 while (running);
357 else
359 printf("Can't open window.\n");
362 MUI_DisposeObject(app);
364 else
366 printf("Can't create MUI application.\n");
369 Cleanup();
371 return ret;
374 static int ftmanager_cli(void)
376 // TODO implement me
377 return RETURN_FAIL;
380 int main(int argc, char **argv)
382 int retval = RETURN_FAIL;
383 if (argc > 1)
385 retval = ftmanager_cli();
387 else
389 // Starting from CLI without arguments opens GUI, too
390 retval = ftmanager_gui();
392 return retval;