Debian Codegraph @

fontforgeexe/pythonui.c

raw symbols SCIP
1 /* Copyright (C) 2007-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /* Python Interface to FontForge */
28
29 #include <fontforge-config.h>
30
31 #ifndef _NO_PYTHON
32
33 #include "cvundoes.h"
34 #include "ffglib.h"
35 #include "ffpython.h"
36 #include "fontforgeui.h"
37 #include "hotkeys.h"
38 #include "scriptfuncs.h"
39 #include "scripting.h"
40 #include "splinestroke.h"
41 #include "splineutil.h"
42 #include "ttf.h"
43 #include "ustring.h"
44
45 #include <dirent.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <math.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <sys/types.h>
52 #include <unistd.h>
53
54 /* The (messy) menu name situation:
55 *
56 * The updated interface encourages the caller to pass three names for each
57 * menu: a localized name, an untranslated name, and an identifier string.
58 * (With luck) the latter is for future use when menus are configurable, so
59 * that a user can choose to put a registered menu item anywhere in the
60 * hierarchy without worrying about the "registration name".
61 *
62 * The complexity with the other two names relates to mnemonics. The names
63 * might come in any of the following forms in any combination:
64 *
65 * Cancel Annuler
66 * _Cancel _Annuler
67 * Cancel (_1) キャンセル(_C)
68 *
69 * (A parenthetical in the English form is unlikely, but possible if a
70 * developer has run out of other letters or hopes to claim a less likely
71 * mnemonic in a Tools menu.)
72 *
73 * We want the following:
74 *
75 * 1. Below the Tools menus a "_" specified mnemonic is honored.
76 * 2. On the Tools menu the specified mnemonic is treated as a suggestion.
77 * 3. A user can assign a hotkey to a menu item, similar to other menus.
78 *
79 * So at the level of the tools menu we would ideally register two strings, one
80 * to display in the menu with its assigned mnemonic and one to match assigned
81 * hotkeys, which are (normally) specified in English without mnemonics.
82 *
83 * Take the second line above. If "A" is available we want these assignments
84 * (ignoring casting):
85 *
86 * mmn[j].ti.text = "Annuler";
87 * mmn[j].ti.text_untranslated = "Cancel";
88 * mmn[j].ti.mnemonic = 'A';
89 *
90 * If "A" is not available but "u" is, we could do the same but switch the
91 * mnemonic to 'u'. If none of the French letters are available we might
92 * wind up with:
93 *
94 * mmn[j].ti.text = "Annuler (4)";
95 * mmn[j].ti.text_untranslated = "Cancel";
96 * mmn[j].ti.mnemonic = '4';
97 *
98 * If we only have one set of strings we don't know whether it is localized,
99 * so we need to do:
100 *
101 * mmn[j].ti.text = "Annuler (4)";
102 * mmn[j].ti.text_untranslated = "Annuler";
103 * mmn[j].ti.mnemonic = '4';
104 *
105 * And, of course, if we *start* with "Annuler (_C)" or "キャンセル(_C)" and
106 * "C" is not available we don't want to accidentally do
107 *
108 * mmn[j].ti.text = "Annuler (C) (4)";
109 * mmn[j].ti.text_untranslated = "Annuler (C)";
110 * mmn[j].ti.mnemonic = '4';
111 *
112 * so we need to strip off the parenthetical when reassigning (and hope that's
113 * the convention the translator is using).
114 *
115 * ti.text is a (unichar_t *) but can be assigned a utf8 string when setting
116 * ti.text_is_1byte to true. ti.text_untranslated is a (char *) that we just
117 * have to hope can handle non-ASCII UTF-8 content, because that may be all we
118 * have.
119 */
120
121 static const char mn_order[] = NU_("}{:[]&=~%^;?+/,#$-@*!.7869054321QXZVJWFYGKUBHPCDMLTNSIROEA");
122
123 struct py_menu_item {
124 PyObject *func;
125 PyObject *check; /* May be None (which I change to NULL) */
126 PyObject *data; /* May be None (left as None) */
127 };
128
129 typedef void (*ff_menu_callback)(GWindow gw, struct gmenuitem *mi, GEvent *e);
130
131 enum py_menu_type { pmt_font=0, pmt_char=1, pmt_size=2 };
132 enum py_menu_flag { pmf_font=1, pmf_char=2 };
133
134 static struct py_menu_data {
135 struct py_menu_item *items;
136 int cnt, max;
137 GMenuItem2 *menu;
138 ff_menu_callback moveto, invoke;
139 void (*setmenu)(GMenuItem2 *menu);
140 uint16_t mn_offset;
141 unichar_t *mn_string;
142 GHashTable *mn_avail;
143 char *hotkey_prefix;
144 } *py_menus;
145
146 static unichar_t AllocateNextMnemonic(struct py_menu_data *pmd) {
147 unichar_t uc = 0;
148 while ( (uc=pmd->mn_string[pmd->mn_offset])!=0 &&
149 !g_hash_table_contains(pmd->mn_avail, GUINT_TO_POINTER(uc)) )
150 ++pmd->mn_offset;
151
152 if ( uc!=0 )
153 g_hash_table_remove(pmd->mn_avail, GUINT_TO_POINTER(uc));
154
155 return (unichar_t) uc;
156 }
157
158 static unichar_t FindMnemonic(unichar_t *menu_string, unichar_t alt,
159 struct py_menu_data *pmd) {
160 unichar_t *cp;
161
162 if ( g_hash_table_contains(pmd->mn_avail, GUINT_TO_POINTER(alt)) ) {
163 g_hash_table_remove(pmd->mn_avail, GUINT_TO_POINTER(alt));
164 return alt;
165 }
166 for ( cp = menu_string; *cp!=0; ++cp ) {
167 alt = toupper(*cp);
168 if ( g_hash_table_contains(pmd->mn_avail, GUINT_TO_POINTER(alt)) ) {
169 g_hash_table_remove(pmd->mn_avail, GUINT_TO_POINTER(alt));
170 return alt;
171 }
172 }
173 return 0;
174 }
175
176 // Make sure there is no suffix when c==0 and exactly one suffix " (c)" when c!=0
177 static unichar_t *SetMnemonicSuffix(const unichar_t *menu_string, unichar_t e, unichar_t c) {
178 int i;
179 unichar_t *r;
180 i = u_strlen(menu_string);
181 r = malloc((i+5) * sizeof(unichar_t));
182 memcpy(r, menu_string, sizeof(unichar_t)*(i+1));
183
184 i--; // Make index of last character
185
186 // Remove trailing spaces (if any)
187 while ( i>=0 && r[i]==' ' )
188 --i;
189 r[i+1] = 0;
190 // The following is intended to remove a parenthetical mnemonic
191 // if one is already present in the string, or do nothing otherwise.
192 // If the input is something less "standard" we just append the
193 // new mnemonic character and hope for the best.
194 if ( i>=0 && r[i]==')' ) {
195 --i;
196 if ( i>=0 && r[i]==e ) {
197 --i;
198 if ( i>=0 && r[i]=='(' ) {
199 --i;
200 while ( i>=0 && r[i]==' ' )
201 --i;
202 r[i+1] = 0;
203 }
204 }
205 }
206
207 // The calloc above ensures there is room for these characters
208 if ( c!=0 ) {
209 r[i+1] = ' ';
210 r[i+2] = '(';
211 r[i+3] = c;
212 r[i+4] = ')';
213 r[i+5] = 0;
214 }
215 return r;
216 }
217
218 static void py_tllistcheck(struct gmenuitem *mi, PyObject *owner, struct py_menu_data *pmd) {
219 PyObject *arglist, *result;
220
221 if ( mi == NULL )
222 return;
223
224 for ( mi = mi->sub; mi!=NULL && (mi->ti.text!=NULL || mi->ti.line); ++mi ) {
225 if ( mi->mid==-1 ) /* Submenu */
226 continue;
227 if ( mi->mid<0 || mi->mid>=pmd->cnt ) {
228 fprintf( stderr, "Bad Menu ID in python menu %d\n", mi->mid );
229 mi->ti.disabled = true;
230 continue;
231 }
232 if ( pmd->items[mi->mid].check==NULL ) {
233 mi->ti.disabled = false;
234 continue;
235 }
236 arglist = PyTuple_New(2);
237 Py_XINCREF(pmd->items[mi->mid].data);
238 Py_XINCREF(owner);
239 PyTuple_SetItem(arglist,0,pmd->items[mi->mid].data);
240 PyTuple_SetItem(arglist,1,owner);
241 result = PyObject_CallObject(pmd->items[mi->mid].check, arglist);
242 Py_DECREF(arglist);
243 if ( result==NULL )
244 /* Oh. An error. How fun. See below */;
245 else if ( !PyLong_Check(result)) {
246 char *menu_item_name = u2utf8_copy(mi->ti.text);
247 LogError(_("Return from enabling function for menu item %s must be boolean"), menu_item_name );
248 free( menu_item_name );
249 mi->ti.disabled = true;
250 } else
251 mi->ti.disabled = PyLong_AsLong(result)==0;
252 Py_XDECREF(result);
253 if ( PyErr_Occurred()!=NULL )
254 PyErr_Print();
255 }
256 }
257
258 static void py_menuactivate(struct gmenuitem *mi, PyObject *owner, struct py_menu_data *pmd) {
259 PyObject *arglist, *result;
260
261 if ( mi->mid==-1 ) /* Submenu */
262 return;
263
264 assert( py_menus!=NULL );
265
266 if ( mi->mid<0 || mi->mid>=pmd->cnt ) {
267 fprintf( stderr, "Bad Menu ID in python menu %d\n", mi->mid );
268 return;
269 }
270 if ( pmd->items[mi->mid].func==NULL ) {
271 return;
272 }
273 arglist = PyTuple_New(2);
274 Py_XINCREF(pmd->items[mi->mid].data);
275 Py_XINCREF(owner);
276 PyTuple_SetItem(arglist,0,pmd->items[mi->mid].data);
277 PyTuple_SetItem(arglist,1,owner);
278 result = PyObject_CallObject(pmd->items[mi->mid].func, arglist);
279 Py_DECREF(arglist);
280 Py_XDECREF(result);
281 if ( PyErr_Occurred()!=NULL )
282 PyErr_Print();
283 }
284
285 void cvpy_tllistcheck(GWindow gw,struct gmenuitem *mi,GEvent *e) {
286 CharView *cv = (CharView *) GDrawGetUserData(gw);
287 PyObject *pysc = PySC_From_SC(cv->b.sc);
288
289 sc_active_in_ui = cv->b.sc;
290 layer_active_in_ui = CVLayer((CharViewBase *) cv);
291 PyFF_Glyph_Set_Layer(sc_active_in_ui,layer_active_in_ui);
292 py_tllistcheck(mi,pysc,py_menus + pmt_char);
293 sc_active_in_ui = NULL;
294 layer_active_in_ui = ly_fore;
295 }
296
297 static void cvpy_menuactivate(GWindow gw,struct gmenuitem *mi,GEvent *e) {
298 CharView *cv = (CharView *) GDrawGetUserData(gw);
299 PyObject *pysc = PySC_From_SC(cv->b.sc);
300
301 sc_active_in_ui = cv->b.sc;
302 layer_active_in_ui = CVLayer((CharViewBase *) cv);
303 PyFF_Glyph_Set_Layer(sc_active_in_ui,layer_active_in_ui);
304 py_menuactivate(mi,pysc,py_menus + pmt_char);
305 sc_active_in_ui = NULL;
306 layer_active_in_ui = ly_fore;
307 }
308
309 void fvpy_tllistcheck(GWindow gw,struct gmenuitem *mi,GEvent *e) {
310 FontViewBase *fv = (FontViewBase *) GDrawGetUserData(gw);
311 PyObject *pyfv = PyFF_FontForFV(fv);
312
313 fv_active_in_ui = fv;
314 layer_active_in_ui = fv->active_layer;
315 py_tllistcheck(mi,pyfv,py_menus + pmt_font);
316 fv_active_in_ui = NULL;
317 }
318
319 static void fvpy_menuactivate(GWindow gw,struct gmenuitem *mi,GEvent *e) {
320 FontViewBase *fv = (FontViewBase *) GDrawGetUserData(gw);
321 PyObject *pyfv = PyFF_FontForFV(fv);
322
323 fv_active_in_ui = fv;
324 layer_active_in_ui = fv->active_layer;
325 py_menuactivate(mi,pyfv,py_menus + pmt_font);
326 fv_active_in_ui = NULL;
327 }
328
329 static void PyMenuInit() {
330 int t;
331 unichar_t *mn_string, *cp;
332 if ( py_menus!=NULL )
333 return;
334
335 py_menus = calloc(pmt_size, sizeof(struct py_menu_data));
336 mn_string = utf82u_copy(U_(mn_order));
337
338 for ( t=0; t<pmt_size; ++t ) {
339 py_menus[t].mn_string = mn_string;
340 py_menus[t].mn_avail = g_hash_table_new(g_direct_hash, g_direct_equal);
341 for ( cp = mn_string; *cp!=0; ++cp )
342 g_hash_table_add(py_menus[t].mn_avail, GUINT_TO_POINTER(*cp));
343 }
344
345 py_menus[pmt_font].hotkey_prefix = "FontView.Menu.Tools.";
346 py_menus[pmt_font].moveto = fvpy_tllistcheck;
347 py_menus[pmt_font].invoke = fvpy_menuactivate;
348 py_menus[pmt_font].setmenu = FVSetToolsSubmenu;
349
350 py_menus[pmt_char].hotkey_prefix = "CharView.Menu.Tools.";
351 py_menus[pmt_char].moveto = cvpy_tllistcheck;
352 py_menus[pmt_char].invoke = cvpy_menuactivate;
353 py_menus[pmt_char].setmenu = CVSetToolsSubmenu;
354 }
355
356 static struct flaglist menuviews[] = {
357 { "Font", pmf_font },
358 { "Glyph", pmf_char },
359 { "Char", pmf_char },
360 FLAGLIST_EMPTY
361 };
362
363 struct py_menu_text {
364 const char *localized;
365 const char *untranslated;
366 const char *identifier;
367 };
368
369 struct py_menu_spec {
370 int depth, divider;
371 struct py_menu_text *levels;
372 const char *shortcut_str;
373 PyObject *func, *check, *data;
374 };
375
376 static int MenuDataAdd(struct py_menu_spec *spec, struct py_menu_data *pmd) {
377 if ( pmd->cnt >= pmd->max )
378 pmd->items = realloc(pmd->items,(pmd->max+=10)*sizeof(struct py_menu_item));
379
380 pmd->items[pmd->cnt].func = spec->func;
381 Py_XINCREF(spec->func);
382
383 pmd->items[pmd->cnt].check= spec->check;
384 Py_XINCREF(spec->check);
385
386 pmd->items[pmd->cnt].data = spec->data;
387 Py_XINCREF(spec->data);
388
389 return pmd->cnt++;
390 }
391
392 static unichar_t *DoMnemonic(unichar_t *trans, unichar_t *alt,
393 struct py_menu_data *pmd) {
394 unichar_t tmp_alt, *tmp_uni;
395
396 tmp_alt = FindMnemonic(trans, *alt, pmd);
397 if ( tmp_alt!=0 && tmp_alt!=*alt ) {
398 // The original string might have had a suffix but we only need
399 // to remove it if the alt code is replaced.
400 tmp_uni = SetMnemonicSuffix(trans, *alt, 0);
401 free(trans);
402 trans = tmp_uni;
403 *alt = tmp_alt;
404 } else if ( tmp_alt==0 ) {
405 *alt = AllocateNextMnemonic(pmd);
406 tmp_uni = SetMnemonicSuffix(trans, 0, *alt);
407 free(trans);
408 trans = tmp_uni;
409 }
410 return trans;
411 }
412
413 static void InsertSubMenus(struct py_menu_spec *spec, struct py_menu_data *pmd) {
414 int i, j;
415 GMenuItem2 *mmn, *orig_menu, **mn;
416 unichar_t alt;
417 char *untrans, *action, *tmp_str;
418 unichar_t *trans, *tmp_uni;
419
420 mn = &pmd->menu;
421 orig_menu = pmd->menu;
422
423 for ( i=0; i<spec->depth; ++i ) {
424 if ( i==spec->depth-1 && spec->divider ) {
425 untrans = "_____UNMATCH___ABLE______";
426 trans = NULL;
427 action = NULL;
428 alt = 0;
429 } else {
430 // Lots of churn just to strip out a potentially unicode
431 // character from the untrans suffix, but oh well.
432 tmp_uni = utf82u_mncopy(spec->levels[i].untranslated, &alt);
433 untrans = u2utf8_copy(tmp_uni);
434 free(tmp_uni);
435 trans = utf82u_mncopy(spec->levels[i].localized, &alt);
436 if ( i==0 ) {
437 action = strconcat(pmd->hotkey_prefix, untrans);
438 } else {
439 tmp_str = strconcat3(action, ".", untrans);
440 free(action);
441 action = tmp_str;
442 }
443 }
444 j = 0;
445 if ( *mn != NULL ) {
446 for ( j=0; (*mn)[j].ti.text!=NULL || (*mn)[j].ti.line; ++j ) {
447 if ( (*mn)[j].ti.text==NULL )
448 continue;
449 if ( strcmp((const char *) (*mn)[j].ti.text_untranslated,untrans)==0 )
450 break;
451 }
452 }
453 if ( *mn==NULL || (*mn)[j].ti.text==NULL ) {
454 *mn = realloc(*mn,(j+2)*sizeof(GMenuItem2));
455 memset(*mn+j,0,2*sizeof(GMenuItem2));
456 }
457 mmn = *mn;
458 if ( mmn[j].ti.text==NULL ) {
459 mmn[j].ti.fg = mmn[j].ti.bg = COLOR_DEFAULT;
460 if ( i==0 && !spec->divider )
461 trans = DoMnemonic(trans, &alt, pmd);
462 if ( i!=spec->depth-1 ) {
463 mmn[j].ti.text = trans;
464 mmn[j].ti.text_untranslated = untrans;
465 mmn[j].ti.text_is_1byte = false;
466 mmn[j].ti.mnemonic = alt;
467 mmn[j].mid = -1;
468 mmn[j].moveto = pmd->moveto;
469 mn = &mmn[j].sub;
470 } else if ( spec->divider ) {
471 mmn[j].ti.line = true;
472 } else {
473 mmn[j].ti.text = trans;
474 mmn[j].ti.text_untranslated = untrans;
475 mmn[j].ti.text_is_1byte = false;
476 mmn[j].ti.mnemonic = alt;
477 mmn[j].invoke = pmd->invoke;
478 mmn[j].mid = MenuDataAdd(spec,pmd);
479 }
480 } else {
481 if ( mmn[j].sub != NULL ) {
482 if ( i==0 && !spec->divider ) {
483 if ( mmn[j].ti.mnemonic != 0 )
484 g_hash_table_add(pmd->mn_avail, GUINT_TO_POINTER(mmn[j].ti.mnemonic));
485 trans = DoMnemonic(trans, &alt, pmd);
486 }
487 free(mmn[j].ti.text);
488 mmn[j].ti.text = trans;
489 mmn[j].ti.mnemonic = alt;
490 }
491 if ( i!=spec->depth-1 )
492 mn = &mmn[j].sub;
493 else {
494 mmn[j].invoke = pmd->invoke;
495 mmn[j].mid = MenuDataAdd(spec,pmd);
496 fprintf( stderr, "Redefining menu item %s\n", untrans );
497 }
498 free(untrans);
499 }
500 }
501 if ( spec->shortcut_str!=NULL )
502 hotkeySetFull(action, spec->shortcut_str, false, hk_python);
503 if ( orig_menu!=pmd->menu )
504 (*pmd->setmenu)(pmd->menu);
505 }
506
507 static int DecodeMenuLevel(struct py_menu_text *text, PyObject *args) {
508 if ( PyUnicode_Check(args) ) {
509 text->localized = text->untranslated = PyUnicode_AsUTF8(args);
510 } else if ( PyTuple_Check(args) ) {
511 for ( int i=0; i<PyTuple_Size(args); ++i ) {
512 if ( !PyUnicode_Check(PyTuple_GetItem(args, i)) ) {
513 PyErr_Format(PyExc_ValueError, "All elements of a `name` or `submenu` tuple must be strings" );
514 return false;
515 }
516 }
517 if ( PyTuple_Size(args)==2 ) {
518 text->localized = text->untranslated = PyUnicode_AsUTF8(PyTuple_GetItem(args, 0));
519 text->identifier = PyUnicode_AsUTF8(PyTuple_GetItem(args, 1));
520 } else if ( PyTuple_Size(args)==3 ) {
521 text->localized = PyUnicode_AsUTF8(PyTuple_GetItem(args, 0));
522 text->untranslated = PyUnicode_AsUTF8(PyTuple_GetItem(args, 1));
523 text->identifier = PyUnicode_AsUTF8(PyTuple_GetItem(args, 2));
524 } else {
525 PyErr_Format(PyExc_ValueError, "A `name` or `submenu` tuple must have either 2 or 3 elements" );
526 return false;
527 }
528 } else {
529 PyErr_Format(PyExc_ValueError, "A `name` or `submenu` object must be either string or tuple" );
530 return false;
531 }
532 return true;
533 }
534
535 static const char *rmi_keywords[] = { "callback", "enable", "data", "context", "hotkey", "name", "submenu", "keyword_only", NULL };
536 static const char *rmdiv_keywords[] = { "context", "divider", "submenu", NULL };
537
538 static PyObject *PyFF_registerMenuItem(PyObject *self, PyObject *args, PyObject *kwargs) {
539 int i, flags, by_keyword = false, keyword_only = false;
540 struct py_menu_spec spec;
541 PyObject *context = Py_None, *name = Py_None, *submenu = Py_None;
542
543 memset(&spec, 0, sizeof(spec));
544
545 spec.func = spec.check = spec.data = Py_None;
546
547 if ( no_windowing_ui ) // Just ignore non-ui calls
548 Py_RETURN_NONE;
549
550 if ( PyArg_ParseTupleAndKeywords(args, kwargs, "Op|$O", (char**) rmdiv_keywords,
551 &context, &spec.divider, &submenu) ) {
552 if ( !spec.divider ) {
553 PyErr_Format(PyExc_ValueError, "'divider' must be True when using this combination of keywords." );
554 return NULL;
555 }
556 by_keyword = true;
557 } else {
558 PyErr_Clear();
559 if ( PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOOsO$Op", (char**) rmi_keywords,
560 &spec.func, &spec.check, &spec.data, &context,
561 &spec.shortcut_str, &name, &submenu, &keyword_only) ) {
562 by_keyword = true;
563 } // Leave error for keyword_only
564 }
565
566 if ( by_keyword ) {
567 if ( PyList_Check(submenu) ) {
568 spec.depth = PyList_Size(submenu)+1;
569 spec.levels = calloc(spec.depth, sizeof(struct py_menu_text));
570 for (i=0; i<spec.depth-1; ++i) {
571 if ( !DecodeMenuLevel(spec.levels+i, PyList_GetItem(submenu, i)) ) {
572 free(spec.levels);
573 return NULL;
574 }
575 }
576 } else if ( PyTuple_Check(submenu) || PyUnicode_Check(submenu) ) {
577 spec.depth = 2;
578 spec.levels = calloc(spec.depth, sizeof(struct py_menu_text));
579 if ( !DecodeMenuLevel(spec.levels, submenu) ) {
580 free(spec.levels);
581 return NULL;
582 }
583 } else if ( submenu == Py_None ) {
584 spec.depth = 1;
585 spec.levels = calloc(spec.depth, sizeof(struct py_menu_text));
586 } else {
587 PyErr_Format(PyExc_ValueError, "Unrecognized type for `submenu` argument" );
588 return NULL;
589 }
590
591 if ( PyTuple_Check(name) || PyUnicode_Check(name) ) {
592 if ( !DecodeMenuLevel(spec.levels+spec.depth-1, name) ) {
593 free(spec.levels);
594 return NULL;
595 }
596 } else if ( !spec.divider ) {
597 PyErr_Format(PyExc_ValueError, "Unrecognized type for `name` argument" );
598 return NULL;
599 }
600 } else {
601 if ( kwargs!=NULL && PyObject_IsTrue(PyDict_GetItemString(kwargs, "keyword_only"))==1 ) {
602 return NULL; // Return error set by ParseTupleAndKeywords
603 }
604
605 PyErr_Clear();
606
607 // Legacy interface
608 spec.depth = PyTuple_Size(args)-5;
609 if ( spec.depth<1 ) {
610 PyErr_Format(PyExc_TypeError, "Positional interface: Too few arguments");
611 return NULL;
612 }
613
614 spec.func = PyTuple_GetItem(args, 0);
615 spec.check = PyTuple_GetItem(args, 1);
616 spec.data = PyTuple_GetItem(args, 2);
617 context = PyTuple_GetItem(args, 3);
618
619 if ( PyTuple_GetItem(args,4)!=Py_None ) {
620 spec.shortcut_str = PyUnicode_AsUTF8(PyTuple_GetItem(args, 4));
621 if (spec.shortcut_str == NULL) {
622 PyErr_Format(PyExc_ValueError, "Positional interface: Shortcut argument is not string" );
623 return NULL;
624 }
625 }
626
627 spec.levels = calloc(spec.depth, sizeof(struct py_menu_text));
628 for ( i=0; i<spec.depth; ++i ) {
629 if ( !DecodeMenuLevel(spec.levels+i, PyTuple_GetItem(args, i+5)) ) {
630 free(spec.levels);
631 return NULL;
632 }
633 }
634 }
635
636 if ( !spec.divider && !PyCallable_Check(spec.func) ) {
637 PyErr_Format(PyExc_TypeError, "First ('callback') argument is not callable" );
638 free(spec.levels);
639 return NULL;
640 }
641
642 if (spec.check!=Py_None && !PyCallable_Check(spec.check)) {
643 PyErr_Format(PyExc_TypeError, "Second ('enable') argument is not callable" );
644 free(spec.levels);
645 return NULL;
646 } else if (spec.check==Py_None) {
647 spec.check = NULL;
648 }
649
650 flags = FlagsFromTuple(context, menuviews, "menu window" );
651 if ( flags==-1 ) {
652 PyErr_Format(PyExc_ValueError, "Unknown context for menu" );
653 free(spec.levels);
654 return NULL;
655 }
656
657 if (spec.shortcut_str!=NULL && !HotkeyParse(NULL, spec.shortcut_str)) {
658 PyErr_Format(PyExc_ValueError, "Cannot parse shortcut string" );
659 free(spec.levels);
660 return NULL;
661 }
662
663 PyMenuInit();
664
665 if ( flags&pmf_font )
666 InsertSubMenus(&spec, py_menus + pmt_font);
667 if ( flags&pmf_char )
668 InsertSubMenus(&spec, py_menus + pmt_char);
669
670 Py_RETURN_NONE;
671 }
672
673 ////////////////////////////////////////////////////////////////////////////////
674 ////////////////////////////////////////////////////////////////////////////////
675 ////////////////////////////////////////////////////////////////////////////////
676
677 // This was used to add pycontrib/gdraw related interfaces to the fontforge
678 // module. It is left as a hook for possible future extension.
679 PyMethodDef module_fontforge_ui_methods[] = {
680
681 // { "removeGtkWindowToMainEventLoopByFD", (PyCFunction) PyFFFont_removeGtkWindowToMainEventLoopByFD, METH_VARARGS, "fixme." },
682
683 PYMETHODDEF_EMPTY /* Sentinel */
684 };
685
686
687 static PyMethodDef*
688 copyUIMethodsToBaseTable( PyMethodDef* ui, PyMethodDef* md )
689 {
690 TRACE("copyUIMethodsToBaseTable()\n");
691 // move md to the first target slot
692 for( ; md->ml_name; )
693 {
694 md++;
695 }
696 for( ; ui->ml_name; ui++, md++ )
697 {
698 memcpy( md, ui, sizeof(PyMethodDef));
699 }
700 return md;
701 }
702
703 void PythonUI_Init(void) {
704 TRACE("PythonUI_Init()\n");
705 FfPy_Replace_MenuItemStub((PyCFunction)PyFF_registerMenuItem);
706
707 copyUIMethodsToBaseTable( module_fontforge_ui_methods, module_fontforge_methods );
708 }
709 #endif
710