summaryrefslogtreecommitdiff
path: root/src/ui/util.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-03-15 12:47:20 +0200
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-03-15 19:03:41 +0200
commiteeb9f8b6306524782e7dcc8ef90797dd5bdae1bf (patch)
treeacfde64b0304d563769f73a7288cacb0568bb179 /src/ui/util.c
parenteb8da869cf87692a5cbb38803644643cd2e192f6 (diff)
Added a page translation service
This is quite experimental. The page contents are sent to an instance of LibreTranslate (powered by Argos Translate), which may or may not successfully translate the contents without mangling the gemtext markup.
Diffstat (limited to 'src/ui/util.c')
-rw-r--r--src/ui/util.c107
1 files changed, 93 insertions, 14 deletions
diff --git a/src/ui/util.c b/src/ui/util.c
index a2aaa893..61ecf948 100644
--- a/src/ui/util.c
+++ b/src/ui/util.c
@@ -1688,8 +1688,19 @@ static void appendFramelessTabPage_(iWidget *tabs, iWidget *page, const char *ti
1688 iTrue); 1688 iTrue);
1689} 1689}
1690 1690
1691static iWidget *makeTwoColumnWidget_(iWidget **headings, iWidget **values) {
1692 iWidget *page = new_Widget();
1693 setFlags_Widget(page, arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag, iTrue);
1694 *headings = addChildFlags_Widget(
1695 page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
1696 *values = addChildFlags_Widget(
1697 page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
1698 return page;
1699}
1700
1691static iWidget *appendTwoColumnPage_(iWidget *tabs, const char *title, int shortcut, iWidget **headings, 1701static iWidget *appendTwoColumnPage_(iWidget *tabs, const char *title, int shortcut, iWidget **headings,
1692 iWidget **values) { 1702 iWidget **values) {
1703 /* TODO: Use `makeTwoColumnWidget_()`, see above. */
1693 iWidget *page = new_Widget(); 1704 iWidget *page = new_Widget();
1694 setFlags_Widget(page, arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag | 1705 setFlags_Widget(page, arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag |
1695 resizeHeightOfChildren_WidgetFlag | borderTop_WidgetFlag, iTrue); 1706 resizeHeightOfChildren_WidgetFlag | borderTop_WidgetFlag, iTrue);
@@ -1935,13 +1946,8 @@ iWidget *makeBookmarkEditor_Widget(void) {
1935 iClob(new_LabelWidget(uiHeading_ColorEscape "EDIT BOOKMARK", NULL)), 1946 iClob(new_LabelWidget(uiHeading_ColorEscape "EDIT BOOKMARK", NULL)),
1936 frameless_WidgetFlag), 1947 frameless_WidgetFlag),
1937 "bmed.heading"); 1948 "bmed.heading");
1938 iWidget *page = new_Widget(); 1949 iWidget *headings, *values;
1939 addChild_Widget(dlg, iClob(page)); 1950 addChild_Widget(dlg, iClob(makeTwoColumnWidget_(&headings, &values)));
1940 setFlags_Widget(page, arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag, iTrue);
1941 iWidget *headings = addChildFlags_Widget(
1942 page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
1943 iWidget *values = addChildFlags_Widget(
1944 page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
1945 iInputWidget *inputs[4]; 1951 iInputWidget *inputs[4];
1946 addChild_Widget(headings, iClob(makeHeading_Widget("Title:"))); 1952 addChild_Widget(headings, iClob(makeHeading_Widget("Title:")));
1947 setId_Widget(addChild_Widget(values, iClob(inputs[0] = new_InputWidget(0))), "bmed.title"); 1953 setId_Widget(addChild_Widget(values, iClob(inputs[0] = new_InputWidget(0))), "bmed.title");
@@ -2074,13 +2080,8 @@ iWidget *makeFeedSettings_Widget(uint32_t bookmarkId) {
2074 NULL)), 2080 NULL)),
2075 frameless_WidgetFlag), 2081 frameless_WidgetFlag),
2076 "feedcfg.heading"); 2082 "feedcfg.heading");
2077 iWidget *page = new_Widget(); 2083 iWidget *headings, *values;
2078 addChild_Widget(dlg, iClob(page)); 2084 addChild_Widget(dlg, iClob(makeTwoColumnWidget_(&headings, &values)));
2079 setFlags_Widget(page, arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag, iTrue);
2080 iWidget *headings = addChildFlags_Widget(
2081 page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
2082 iWidget *values = addChildFlags_Widget(
2083 page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag);
2084 addChild_Widget(headings, iClob(makeHeading_Widget("Title:"))); 2085 addChild_Widget(headings, iClob(makeHeading_Widget("Title:")));
2085 iInputWidget *input = new_InputWidget(0); 2086 iInputWidget *input = new_InputWidget(0);
2086 setId_Widget(addChild_Widget(values, iClob(input)), "feedcfg.title"); 2087 setId_Widget(addChild_Widget(values, iClob(input)), "feedcfg.title");
@@ -2184,3 +2185,81 @@ iWidget *makeIdentityCreation_Widget(void) {
2184 finalizeSheet_Widget(dlg); 2185 finalizeSheet_Widget(dlg);
2185 return dlg; 2186 return dlg;
2186} 2187}
2188
2189static const iMenuItem languages[] = {
2190 { "Arabic", 0, 0, "xlt.lang id:ar" },
2191 { "Chinese", 0, 0, "xlt.lang id:zh" },
2192 { "English", 0, 0, "xlt.lang id:en" },
2193 { "French", 0, 0, "xlt.lang id:fr" },
2194 { "German", 0, 0, "xlt.lang id:de" },
2195 { "Hindi", 0, 0, "xlt.lang id:hi" },
2196 { "Italian", 0, 0, "xlt.lang id:it" },
2197 { "Japanese", 0, 0, "xlt.lang id:ja" },
2198 { "Portuguese", 0, 0, "xlt.lang id:pt" },
2199 { "Russian", 0, 0, "xlt.lang id:ru" },
2200 { "Spanish", 0, 0, "xlt.lang id:es" },
2201};
2202
2203static iBool translationHandler_(iWidget *dlg, const char *cmd) {
2204 if (equal_Command(cmd, "xlt.lang")) {
2205 iLabelWidget *menuItem = pointer_Command(cmd);
2206 iWidget *button = parent_Widget(parent_Widget(menuItem));
2207 iAssert(isInstance_Object(button, &Class_LabelWidget));
2208 updateText_LabelWidget((iLabelWidget *) button, text_LabelWidget(menuItem));
2209 return iTrue;
2210 }
2211 return iFalse;
2212}
2213
2214const char *languageId_String(const iString *menuItemLabel) {
2215 iForIndices(i, languages) {
2216 if (!cmp_String(menuItemLabel, languages[i].label)) {
2217 return cstr_Rangecc(range_Command(languages[i].command, "id"));
2218 }
2219 }
2220 return "";
2221}
2222
2223iWidget *makeTranslation_Widget(iWidget *parent) {
2224 iWidget *dlg = makeSheet_Widget("xlt");
2225 setCommandHandler_Widget(dlg, translationHandler_);
2226 addChildFlags_Widget(dlg,
2227 iClob(new_LabelWidget(uiHeading_ColorEscape "TRANSLATE PAGE", NULL)),
2228 frameless_WidgetFlag);
2229 addChild_Widget(dlg, iClob(makePadding_Widget(lineHeight_Text(uiLabel_FontId))));
2230 iWidget *headings, *values;
2231 iWidget *page;
2232 addChild_Widget(dlg, iClob(page = makeTwoColumnWidget_(&headings, &values)));
2233 setId_Widget(page, "xlt.langs");
2234 addChild_Widget(headings, iClob(makeHeading_Widget("From:")));
2235 iLabelWidget *fromLang, *toLang;
2236 setId_Widget(addChildFlags_Widget(values,
2237 iClob(fromLang = makeMenuButton_LabelWidget(
2238 "Portuguese", languages, iElemCount(languages))),
2239 alignLeft_WidgetFlag),
2240 "xlt.from");
2241 updateTextCStr_LabelWidget(fromLang, "French"); /* TODO: Check source media type; remember last use. */
2242 setBackgroundColor_Widget(findChild_Widget(as_Widget(fromLang), "menu"),
2243 uiBackgroundMenu_ColorId);
2244 addChild_Widget(headings, iClob(makeHeading_Widget("To:")));
2245 setId_Widget(addChildFlags_Widget(values,
2246 iClob(toLang = makeMenuButton_LabelWidget(
2247 "Portuguese", languages, iElemCount(languages))),
2248 alignLeft_WidgetFlag),
2249 "xlt.to");
2250 setBackgroundColor_Widget(findChild_Widget(as_Widget(toLang), "menu"),
2251 uiBackgroundMenu_ColorId);
2252 updateTextCStr_LabelWidget(toLang, "English"); /* TODO: User preference. */
2253 addChild_Widget(dlg, iClob(makePadding_Widget(lineHeight_Text(uiLabel_FontId))));
2254 addChild_Widget(
2255 dlg,
2256 iClob(makeDialogButtons_Widget(
2257 (iMenuItem[]){
2258 { "Cancel", SDLK_ESCAPE, 0, "translation.cancel" },
2259 { uiTextAction_ColorEscape "Translate", SDLK_RETURN, 0, "translation.submit" } },
2260 2)));
2261 addChild_Widget(parent, iClob(dlg));
2262 arrange_Widget(dlg);
2263 finalizeSheet_Widget(dlg);
2264 return dlg;
2265}