1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2009 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup RNA
22  */
23 
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 
28 #include "BLI_utildefines.h"
29 
30 #include "RNA_define.h"
31 #include "RNA_enum_types.h"
32 
33 #include "DNA_screen_types.h"
34 #include "DNA_space_types.h"
35 #include "DNA_windowmanager_types.h"
36 
37 #include "UI_interface.h"
38 
39 #include "wm_cursors.h"
40 #include "wm_event_types.h"
41 
42 #include "rna_internal.h" /* own include */
43 
44 /* confusing 2 enums mixed up here */
45 const EnumPropertyItem rna_enum_window_cursor_items[] = {
46     {WM_CURSOR_DEFAULT, "DEFAULT", 0, "Default", ""},
47     {WM_CURSOR_NONE, "NONE", 0, "None", ""},
48     {WM_CURSOR_WAIT, "WAIT", 0, "Wait", ""},
49     {WM_CURSOR_EDIT, "CROSSHAIR", 0, "Crosshair", ""},
50     {WM_CURSOR_X_MOVE, "MOVE_X", 0, "Move-X", ""},
51     {WM_CURSOR_Y_MOVE, "MOVE_Y", 0, "Move-Y", ""},
52 
53     /* new */
54     {WM_CURSOR_KNIFE, "KNIFE", 0, "Knife", ""},
55     {WM_CURSOR_TEXT_EDIT, "TEXT", 0, "Text", ""},
56     {WM_CURSOR_PAINT_BRUSH, "PAINT_BRUSH", 0, "Paint Brush", ""},
57     {WM_CURSOR_PAINT, "PAINT_CROSS", 0, "Paint Cross", ""},
58     {WM_CURSOR_DOT, "DOT", 0, "Dot Cursor", ""},
59     {WM_CURSOR_ERASER, "ERASER", 0, "Eraser", ""},
60     {WM_CURSOR_HAND, "HAND", 0, "Hand", ""},
61     {WM_CURSOR_EW_SCROLL, "SCROLL_X", 0, "Scroll-X", ""},
62     {WM_CURSOR_NS_SCROLL, "SCROLL_Y", 0, "Scroll-Y", ""},
63     {WM_CURSOR_NSEW_SCROLL, "SCROLL_XY", 0, "Scroll-XY", ""},
64     {WM_CURSOR_EYEDROPPER, "EYEDROPPER", 0, "Eyedropper", ""},
65     {0, NULL, 0, NULL, NULL},
66 };
67 
68 #ifdef RNA_RUNTIME
69 
70 #  include "BKE_context.h"
71 #  include "BKE_undo_system.h"
72 
73 #  include "WM_types.h"
74 
rna_KeyMapItem_to_string(wmKeyMapItem * kmi,bool compact,char * result)75 static void rna_KeyMapItem_to_string(wmKeyMapItem *kmi, bool compact, char *result)
76 {
77   WM_keymap_item_to_string(kmi, compact, result, UI_MAX_SHORTCUT_STR);
78 }
79 
rna_keymap_active(wmKeyMap * km,bContext * C)80 static wmKeyMap *rna_keymap_active(wmKeyMap *km, bContext *C)
81 {
82   wmWindowManager *wm = CTX_wm_manager(C);
83   return WM_keymap_active(wm, km);
84 }
85 
rna_keymap_restore_to_default(wmKeyMap * km,bContext * C)86 static void rna_keymap_restore_to_default(wmKeyMap *km, bContext *C)
87 {
88   WM_keymap_restore_to_default(km, CTX_wm_manager(C));
89 }
90 
rna_keymap_restore_item_to_default(wmKeyMap * km,bContext * C,wmKeyMapItem * kmi)91 static void rna_keymap_restore_item_to_default(wmKeyMap *km, bContext *C, wmKeyMapItem *kmi)
92 {
93   WM_keymap_item_restore_to_default(CTX_wm_manager(C), km, kmi);
94 }
95 
rna_Operator_report(wmOperator * op,int type,const char * msg)96 static void rna_Operator_report(wmOperator *op, int type, const char *msg)
97 {
98   BKE_report(op->reports, type, msg);
99 }
100 
rna_Operator_is_repeat(wmOperator * op,bContext * C)101 static bool rna_Operator_is_repeat(wmOperator *op, bContext *C)
102 {
103   return WM_operator_is_repeat(C, op);
104 }
105 
106 /* since event isn't needed... */
rna_Operator_enum_search_invoke(bContext * C,wmOperator * op)107 static void rna_Operator_enum_search_invoke(bContext *C, wmOperator *op)
108 {
109   WM_enum_search_invoke(C, op, NULL);
110 }
111 
rna_event_modal_handler_add(struct bContext * C,struct wmOperator * operator)112 static bool rna_event_modal_handler_add(struct bContext *C, struct wmOperator *operator)
113 {
114   return WM_event_add_modal_handler(C, operator) != NULL;
115 }
116 
117 /* XXX, need a way for python to know event types, 0x0110 is hard coded */
rna_event_timer_add(struct wmWindowManager * wm,float time_step,wmWindow * win)118 static wmTimer *rna_event_timer_add(struct wmWindowManager *wm, float time_step, wmWindow *win)
119 {
120   return WM_event_add_timer(wm, win, 0x0110, time_step);
121 }
122 
rna_event_timer_remove(struct wmWindowManager * wm,wmTimer * timer)123 static void rna_event_timer_remove(struct wmWindowManager *wm, wmTimer *timer)
124 {
125   WM_event_remove_timer(wm, timer->win, timer);
126 }
127 
wm_gizmogrouptype_find_for_add_remove(ReportList * reports,const char * idname)128 static wmGizmoGroupType *wm_gizmogrouptype_find_for_add_remove(ReportList *reports,
129                                                                const char *idname)
130 {
131   wmGizmoGroupType *gzgt = WM_gizmogrouptype_find(idname, true);
132   if (gzgt == NULL) {
133     BKE_reportf(reports, RPT_ERROR, "Gizmo group type '%s' not found!", idname);
134     return NULL;
135   }
136   if (gzgt->flag & WM_GIZMOGROUPTYPE_PERSISTENT) {
137     BKE_reportf(reports, RPT_ERROR, "Gizmo group '%s' has 'PERSISTENT' option set!", idname);
138     return NULL;
139   }
140   return gzgt;
141 }
142 
rna_gizmo_group_type_ensure(ReportList * reports,const char * idname)143 static void rna_gizmo_group_type_ensure(ReportList *reports, const char *idname)
144 {
145   wmGizmoGroupType *gzgt = wm_gizmogrouptype_find_for_add_remove(reports, idname);
146   if (gzgt != NULL) {
147     WM_gizmo_group_type_ensure_ptr(gzgt);
148   }
149 }
150 
rna_gizmo_group_type_unlink_delayed(ReportList * reports,const char * idname)151 static void rna_gizmo_group_type_unlink_delayed(ReportList *reports, const char *idname)
152 {
153   wmGizmoGroupType *gzgt = wm_gizmogrouptype_find_for_add_remove(reports, idname);
154   if (gzgt != NULL) {
155     WM_gizmo_group_type_unlink_delayed_ptr(gzgt);
156   }
157 }
158 
159 /* placeholder data for final implementation of a true progressbar */
160 static struct wmStaticProgress {
161   float min;
162   float max;
163   bool is_valid;
164 } wm_progress_state = {0, 0, false};
165 
rna_progress_begin(struct wmWindowManager * UNUSED (wm),float min,float max)166 static void rna_progress_begin(struct wmWindowManager *UNUSED(wm), float min, float max)
167 {
168   float range = max - min;
169   if (range != 0) {
170     wm_progress_state.min = min;
171     wm_progress_state.max = max;
172     wm_progress_state.is_valid = true;
173   }
174   else {
175     wm_progress_state.is_valid = false;
176   }
177 }
178 
rna_progress_update(struct wmWindowManager * wm,float value)179 static void rna_progress_update(struct wmWindowManager *wm, float value)
180 {
181   if (wm_progress_state.is_valid) {
182     /* Map to cursor_time range [0,9999] */
183     wmWindow *win = wm->winactive;
184     if (win) {
185       int val = (int)(10000 * (value - wm_progress_state.min) /
186                       (wm_progress_state.max - wm_progress_state.min));
187       WM_cursor_time(win, val);
188     }
189   }
190 }
191 
rna_progress_end(struct wmWindowManager * wm)192 static void rna_progress_end(struct wmWindowManager *wm)
193 {
194   if (wm_progress_state.is_valid) {
195     wmWindow *win = wm->winactive;
196     if (win) {
197       WM_cursor_modal_restore(win);
198       wm_progress_state.is_valid = false;
199     }
200   }
201 }
202 
203 /* wrap these because of 'const wmEvent *' */
rna_Operator_confirm(bContext * C,wmOperator * op,wmEvent * event)204 static int rna_Operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
205 {
206   return WM_operator_confirm(C, op, event);
207 }
rna_Operator_props_popup(bContext * C,wmOperator * op,wmEvent * event)208 static int rna_Operator_props_popup(bContext *C, wmOperator *op, wmEvent *event)
209 {
210   return WM_operator_props_popup(C, op, event);
211 }
212 
rna_KeyMap_item_new(wmKeyMap * km,ReportList * reports,const char * idname,int type,int value,bool any,bool shift,bool ctrl,bool alt,bool oskey,int keymodifier,bool repeat,bool head)213 static wmKeyMapItem *rna_KeyMap_item_new(wmKeyMap *km,
214                                          ReportList *reports,
215                                          const char *idname,
216                                          int type,
217                                          int value,
218                                          bool any,
219                                          bool shift,
220                                          bool ctrl,
221                                          bool alt,
222                                          bool oskey,
223                                          int keymodifier,
224                                          bool repeat,
225                                          bool head)
226 {
227   /*  wmWindowManager *wm = CTX_wm_manager(C); */
228   wmKeyMapItem *kmi = NULL;
229   char idname_bl[OP_MAX_TYPENAME];
230   int modifier = 0;
231 
232   /* only on non-modal maps */
233   if (km->flag & KEYMAP_MODAL) {
234     BKE_report(reports, RPT_ERROR, "Not a non-modal keymap");
235     return NULL;
236   }
237 
238   WM_operator_bl_idname(idname_bl, idname);
239 
240   if (shift) {
241     modifier |= KM_SHIFT;
242   }
243   if (ctrl) {
244     modifier |= KM_CTRL;
245   }
246   if (alt) {
247     modifier |= KM_ALT;
248   }
249   if (oskey) {
250     modifier |= KM_OSKEY;
251   }
252 
253   if (any) {
254     modifier = KM_ANY;
255   }
256 
257   /* create keymap item */
258   kmi = WM_keymap_add_item(km, idname_bl, type, value, modifier, keymodifier);
259 
260   if (!repeat) {
261     kmi->flag |= KMI_REPEAT_IGNORE;
262   }
263 
264   /* T32437 allow scripts to define hotkeys that get added to start of keymap
265    *          so that they stand a chance against catch-all defines later on
266    */
267   if (head) {
268     BLI_remlink(&km->items, kmi);
269     BLI_addhead(&km->items, kmi);
270   }
271 
272   return kmi;
273 }
274 
rna_KeyMap_item_new_from_item(wmKeyMap * km,ReportList * reports,wmKeyMapItem * kmi_src,bool head)275 static wmKeyMapItem *rna_KeyMap_item_new_from_item(wmKeyMap *km,
276                                                    ReportList *reports,
277                                                    wmKeyMapItem *kmi_src,
278                                                    bool head)
279 {
280   /*  wmWindowManager *wm = CTX_wm_manager(C); */
281 
282   if ((km->flag & KEYMAP_MODAL) == (kmi_src->idname[0] != '\0')) {
283     BKE_report(reports, RPT_ERROR, "Can not mix modal/non-modal items");
284     return NULL;
285   }
286 
287   /* create keymap item */
288   wmKeyMapItem *kmi = WM_keymap_add_item_copy(km, kmi_src);
289   if (head) {
290     BLI_remlink(&km->items, kmi);
291     BLI_addhead(&km->items, kmi);
292   }
293   return kmi;
294 }
295 
rna_KeyMap_item_new_modal(wmKeyMap * km,ReportList * reports,const char * propvalue_str,int type,int value,bool any,bool shift,bool ctrl,bool alt,bool oskey,int keymodifier,bool repeat)296 static wmKeyMapItem *rna_KeyMap_item_new_modal(wmKeyMap *km,
297                                                ReportList *reports,
298                                                const char *propvalue_str,
299                                                int type,
300                                                int value,
301                                                bool any,
302                                                bool shift,
303                                                bool ctrl,
304                                                bool alt,
305                                                bool oskey,
306                                                int keymodifier,
307                                                bool repeat)
308 {
309   wmKeyMapItem *kmi = NULL;
310   int modifier = 0;
311   int propvalue = 0;
312 
313   /* only modal maps */
314   if ((km->flag & KEYMAP_MODAL) == 0) {
315     BKE_report(reports, RPT_ERROR, "Not a modal keymap");
316     return NULL;
317   }
318 
319   if (shift) {
320     modifier |= KM_SHIFT;
321   }
322   if (ctrl) {
323     modifier |= KM_CTRL;
324   }
325   if (alt) {
326     modifier |= KM_ALT;
327   }
328   if (oskey) {
329     modifier |= KM_OSKEY;
330   }
331 
332   if (any) {
333     modifier = KM_ANY;
334   }
335 
336   /* not initialized yet, do delayed lookup */
337   if (!km->modal_items) {
338     kmi = WM_modalkeymap_add_item_str(km, type, value, modifier, keymodifier, propvalue_str);
339   }
340   else {
341     if (RNA_enum_value_from_id(km->modal_items, propvalue_str, &propvalue) == 0) {
342       BKE_report(reports, RPT_WARNING, "Property value not in enumeration");
343     }
344     kmi = WM_modalkeymap_add_item(km, type, value, modifier, keymodifier, propvalue);
345   }
346 
347   if (!repeat) {
348     kmi->flag |= KMI_REPEAT_IGNORE;
349   }
350 
351   return kmi;
352 }
353 
rna_KeyMap_item_remove(wmKeyMap * km,ReportList * reports,PointerRNA * kmi_ptr)354 static void rna_KeyMap_item_remove(wmKeyMap *km, ReportList *reports, PointerRNA *kmi_ptr)
355 {
356   wmKeyMapItem *kmi = kmi_ptr->data;
357 
358   if (WM_keymap_remove_item(km, kmi) == false) {
359     BKE_reportf(reports,
360                 RPT_ERROR,
361                 "KeyMapItem '%s' cannot be removed from '%s'",
362                 kmi->idname,
363                 km->idname);
364     return;
365   }
366 
367   RNA_POINTER_INVALIDATE(kmi_ptr);
368 }
369 
rna_KeyMap_item_find_from_operator(ID * id,wmKeyMap * km,const char * idname,PointerRNA * properties,int include_mask,int exclude_mask)370 static PointerRNA rna_KeyMap_item_find_from_operator(ID *id,
371                                                      wmKeyMap *km,
372                                                      const char *idname,
373                                                      PointerRNA *properties,
374                                                      int include_mask,
375                                                      int exclude_mask)
376 {
377   char idname_bl[OP_MAX_TYPENAME];
378   WM_operator_bl_idname(idname_bl, idname);
379 
380   wmKeyMapItem *kmi = WM_key_event_operator_from_keymap(
381       km, idname_bl, properties->data, include_mask, exclude_mask);
382   PointerRNA kmi_ptr;
383   RNA_pointer_create(id, &RNA_KeyMapItem, kmi, &kmi_ptr);
384   return kmi_ptr;
385 }
386 
rna_KeyMap_item_match_event(ID * id,wmKeyMap * km,bContext * C,wmEvent * event)387 static PointerRNA rna_KeyMap_item_match_event(ID *id, wmKeyMap *km, bContext *C, wmEvent *event)
388 {
389   wmKeyMapItem *kmi = WM_event_match_keymap_item(C, km, event);
390   PointerRNA kmi_ptr;
391   RNA_pointer_create(id, &RNA_KeyMapItem, kmi, &kmi_ptr);
392   return kmi_ptr;
393 }
394 
rna_keymap_new(wmKeyConfig * keyconf,ReportList * reports,const char * idname,int spaceid,int regionid,bool modal,bool tool)395 static wmKeyMap *rna_keymap_new(wmKeyConfig *keyconf,
396                                 ReportList *reports,
397                                 const char *idname,
398                                 int spaceid,
399                                 int regionid,
400                                 bool modal,
401                                 bool tool)
402 {
403   if (modal) {
404     /* Sanity check: Don't allow add-ons to override internal modal key-maps
405      * because this isn't supported, the restriction can be removed when
406      * add-ons can define modal key-maps.
407      * Currently this is only useful for add-ons to override built-in modal keymaps
408      * which is not the intended use for add-on keymaps. */
409     wmWindowManager *wm = G_MAIN->wm.first;
410     if (keyconf == wm->addonconf) {
411       BKE_reportf(reports, RPT_ERROR, "Modal key-maps not supported for add-on key-config");
412       return NULL;
413     }
414   }
415 
416   wmKeyMap *keymap;
417 
418   if (modal == 0) {
419     keymap = WM_keymap_ensure(keyconf, idname, spaceid, regionid);
420   }
421   else {
422     keymap = WM_modalkeymap_ensure(keyconf, idname, NULL); /* items will be lazy init */
423   }
424 
425   if (keymap && tool) {
426     keymap->flag |= KEYMAP_TOOL;
427   }
428 
429   return keymap;
430 }
431 
rna_keymap_find(wmKeyConfig * keyconf,const char * idname,int spaceid,int regionid)432 static wmKeyMap *rna_keymap_find(wmKeyConfig *keyconf,
433                                  const char *idname,
434                                  int spaceid,
435                                  int regionid)
436 {
437   return WM_keymap_list_find(&keyconf->keymaps, idname, spaceid, regionid);
438 }
439 
rna_keymap_find_modal(wmKeyConfig * UNUSED (keyconf),const char * idname)440 static wmKeyMap *rna_keymap_find_modal(wmKeyConfig *UNUSED(keyconf), const char *idname)
441 {
442   wmOperatorType *ot = WM_operatortype_find(idname, 0);
443 
444   if (!ot) {
445     return NULL;
446   }
447   else {
448     return ot->modalkeymap;
449   }
450 }
451 
rna_KeyMap_remove(wmKeyConfig * keyconfig,ReportList * reports,PointerRNA * keymap_ptr)452 static void rna_KeyMap_remove(wmKeyConfig *keyconfig, ReportList *reports, PointerRNA *keymap_ptr)
453 {
454   wmKeyMap *keymap = keymap_ptr->data;
455 
456   if (WM_keymap_remove(keyconfig, keymap) == false) {
457     BKE_reportf(reports, RPT_ERROR, "KeyConfig '%s' cannot be removed", keymap->idname);
458     return;
459   }
460 
461   RNA_POINTER_INVALIDATE(keymap_ptr);
462 }
463 
rna_KeyConfig_remove(wmWindowManager * wm,ReportList * reports,PointerRNA * keyconf_ptr)464 static void rna_KeyConfig_remove(wmWindowManager *wm, ReportList *reports, PointerRNA *keyconf_ptr)
465 {
466   wmKeyConfig *keyconf = keyconf_ptr->data;
467 
468   if (WM_keyconfig_remove(wm, keyconf) == false) {
469     BKE_reportf(reports, RPT_ERROR, "KeyConfig '%s' cannot be removed", keyconf->idname);
470     return;
471   }
472 
473   RNA_POINTER_INVALIDATE(keyconf_ptr);
474 }
475 
rna_KeyConfig_find_item_from_operator(wmWindowManager * wm,bContext * C,const char * idname,int opcontext,PointerRNA * properties,int include_mask,int exclude_mask,PointerRNA * km_ptr)476 static PointerRNA rna_KeyConfig_find_item_from_operator(wmWindowManager *wm,
477                                                         bContext *C,
478                                                         const char *idname,
479                                                         int opcontext,
480                                                         PointerRNA *properties,
481                                                         int include_mask,
482                                                         int exclude_mask,
483                                                         PointerRNA *km_ptr)
484 {
485   char idname_bl[OP_MAX_TYPENAME];
486   WM_operator_bl_idname(idname_bl, idname);
487 
488   wmKeyMap *km = NULL;
489   wmKeyMapItem *kmi = WM_key_event_operator(
490       C, idname_bl, opcontext, properties->data, include_mask, exclude_mask, &km);
491   PointerRNA kmi_ptr;
492   RNA_pointer_create(&wm->id, &RNA_KeyMap, km, km_ptr);
493   RNA_pointer_create(&wm->id, &RNA_KeyMapItem, kmi, &kmi_ptr);
494   return kmi_ptr;
495 }
496 
rna_KeyConfig_update(wmWindowManager * wm)497 static void rna_KeyConfig_update(wmWindowManager *wm)
498 {
499   WM_keyconfig_update(wm);
500 }
501 
502 /* popup menu wrapper */
rna_PopMenuBegin(bContext * C,const char * title,int icon)503 static PointerRNA rna_PopMenuBegin(bContext *C, const char *title, int icon)
504 {
505   PointerRNA r_ptr;
506   void *data;
507 
508   data = (void *)UI_popup_menu_begin(C, title, icon);
509 
510   RNA_pointer_create(NULL, &RNA_UIPopupMenu, data, &r_ptr);
511 
512   return r_ptr;
513 }
514 
rna_PopMenuEnd(bContext * C,PointerRNA * handle)515 static void rna_PopMenuEnd(bContext *C, PointerRNA *handle)
516 {
517   UI_popup_menu_end(C, handle->data);
518 }
519 
520 /* popover wrapper */
rna_PopoverBegin(bContext * C,int ui_units_x,bool from_active_button)521 static PointerRNA rna_PopoverBegin(bContext *C, int ui_units_x, bool from_active_button)
522 {
523   PointerRNA r_ptr;
524   void *data;
525 
526   data = (void *)UI_popover_begin(C, U.widget_unit * ui_units_x, from_active_button);
527 
528   RNA_pointer_create(NULL, &RNA_UIPopover, data, &r_ptr);
529 
530   return r_ptr;
531 }
532 
rna_PopoverEnd(bContext * C,PointerRNA * handle,wmKeyMap * keymap)533 static void rna_PopoverEnd(bContext *C, PointerRNA *handle, wmKeyMap *keymap)
534 {
535   UI_popover_end(C, handle->data, keymap);
536 }
537 
538 /* pie menu wrapper */
rna_PieMenuBegin(bContext * C,const char * title,int icon,PointerRNA * event)539 static PointerRNA rna_PieMenuBegin(bContext *C, const char *title, int icon, PointerRNA *event)
540 {
541   PointerRNA r_ptr;
542   void *data;
543 
544   data = (void *)UI_pie_menu_begin(C, title, icon, event->data);
545 
546   RNA_pointer_create(NULL, &RNA_UIPieMenu, data, &r_ptr);
547 
548   return r_ptr;
549 }
550 
rna_PieMenuEnd(bContext * C,PointerRNA * handle)551 static void rna_PieMenuEnd(bContext *C, PointerRNA *handle)
552 {
553   UI_pie_menu_end(C, handle->data);
554 }
555 
rna_WindowManager_print_undo_steps(wmWindowManager * wm)556 static void rna_WindowManager_print_undo_steps(wmWindowManager *wm)
557 {
558   BKE_undosys_print(wm->undo_stack);
559 }
560 
rna_WindowManager_tag_script_reload(void)561 static void rna_WindowManager_tag_script_reload(void)
562 {
563   WM_script_tag_reload();
564   WM_main_add_notifier(NC_WINDOW, NULL);
565 }
566 
rna_WindoManager_operator_properties_last(const char * idname)567 static PointerRNA rna_WindoManager_operator_properties_last(const char *idname)
568 {
569   wmOperatorType *ot = WM_operatortype_find(idname, true);
570 
571   if (ot != NULL) {
572     PointerRNA ptr;
573     WM_operator_last_properties_ensure(ot, &ptr);
574     return ptr;
575   }
576   return PointerRNA_NULL;
577 }
578 
rna_Window_event_add_simulate(wmWindow * win,ReportList * reports,int type,int value,const char * unicode,int x,int y,bool shift,bool ctrl,bool alt,bool oskey)579 static wmEvent *rna_Window_event_add_simulate(wmWindow *win,
580                                               ReportList *reports,
581                                               int type,
582                                               int value,
583                                               const char *unicode,
584                                               int x,
585                                               int y,
586                                               bool shift,
587                                               bool ctrl,
588                                               bool alt,
589                                               bool oskey)
590 {
591   if ((G.f & G_FLAG_EVENT_SIMULATE) == 0) {
592     BKE_report(reports, RPT_ERROR, "Not running with '--enable-event-simulate' enabled");
593     return NULL;
594   }
595 
596   if (!ELEM(value, KM_PRESS, KM_RELEASE, KM_NOTHING)) {
597     BKE_report(reports, RPT_ERROR, "Value: only 'PRESS/RELEASE/NOTHING' are supported");
598     return NULL;
599   }
600   if (ISKEYBOARD(type) || ISMOUSE_BUTTON(type)) {
601     if (!ELEM(value, KM_PRESS, KM_RELEASE)) {
602       BKE_report(reports, RPT_ERROR, "Value: must be 'PRESS/RELEASE' for keyboard/buttons");
603       return NULL;
604     }
605   }
606   if (ELEM(type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
607     if (value != KM_NOTHING) {
608       BKE_report(reports, RPT_ERROR, "Value: must be 'NOTHING' for motion");
609       return NULL;
610     }
611   }
612   if (unicode != NULL) {
613     if (value != KM_PRESS) {
614       BKE_report(reports, RPT_ERROR, "Value: must be 'PRESS' when unicode is set");
615       return NULL;
616     }
617   }
618   /* TODO: validate NDOF. */
619 
620   char ascii = 0;
621   if (unicode != NULL) {
622     int len = BLI_str_utf8_size(unicode);
623     if (len == -1 || unicode[len] != '\0') {
624       BKE_report(reports, RPT_ERROR, "Only a single character supported");
625       return NULL;
626     }
627     if (len == 1 && isascii(unicode[0])) {
628       ascii = unicode[0];
629     }
630   }
631 
632   wmEvent e = *win->eventstate;
633   e.type = type;
634   e.val = value;
635   e.is_repeat = false;
636   e.x = x;
637   e.y = y;
638   /* Note: KM_MOD_FIRST, KM_MOD_SECOND aren't used anywhere, set as bools */
639   e.shift = shift;
640   e.ctrl = ctrl;
641   e.alt = alt;
642   e.oskey = oskey;
643 
644   e.prevx = win->eventstate->x;
645   e.prevy = win->eventstate->y;
646   e.prevval = win->eventstate->val;
647   e.prevtype = win->eventstate->type;
648 
649   e.ascii = '\0';
650   e.utf8_buf[0] = '\0';
651   if (unicode != NULL) {
652     e.ascii = ascii;
653     STRNCPY(e.utf8_buf, unicode);
654   }
655 
656   /* Until we expose setting tablet values here. */
657   WM_event_tablet_data_default_set(&e.tablet);
658 
659   return WM_event_add_simulate(win, &e);
660 }
661 
662 #else
663 
664 #  define WM_GEN_INVOKE_EVENT (1 << 0)
665 #  define WM_GEN_INVOKE_SIZE (1 << 1)
666 #  define WM_GEN_INVOKE_RETURN (1 << 2)
667 
rna_generic_op_invoke(FunctionRNA * func,int flag)668 static void rna_generic_op_invoke(FunctionRNA *func, int flag)
669 {
670   PropertyRNA *parm;
671 
672   RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_CONTEXT);
673   parm = RNA_def_pointer(func, "operator", "Operator", "", "Operator to call");
674   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
675 
676   if (flag & WM_GEN_INVOKE_EVENT) {
677     parm = RNA_def_pointer(func, "event", "Event", "", "Event");
678     RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
679   }
680 
681   if (flag & WM_GEN_INVOKE_SIZE) {
682     RNA_def_int(func, "width", 300, 0, INT_MAX, "", "Width of the popup", 0, INT_MAX);
683   }
684 
685   if (flag & WM_GEN_INVOKE_RETURN) {
686     parm = RNA_def_enum_flag(
687         func, "result", rna_enum_operator_return_items, OPERATOR_FINISHED, "result", "");
688     RNA_def_function_return(func, parm);
689   }
690 }
691 
RNA_api_window(StructRNA * srna)692 void RNA_api_window(StructRNA *srna)
693 {
694   FunctionRNA *func;
695   PropertyRNA *parm;
696 
697   func = RNA_def_function(srna, "cursor_warp", "WM_cursor_warp");
698   parm = RNA_def_int(func, "x", 0, INT_MIN, INT_MAX, "", "", INT_MIN, INT_MAX);
699   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
700   parm = RNA_def_int(func, "y", 0, INT_MIN, INT_MAX, "", "", INT_MIN, INT_MAX);
701   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
702   RNA_def_function_ui_description(func, "Set the cursor position");
703 
704   func = RNA_def_function(srna, "cursor_set", "WM_cursor_set");
705   parm = RNA_def_property(func, "cursor", PROP_ENUM, PROP_NONE);
706   RNA_def_property_enum_items(parm, rna_enum_window_cursor_items);
707   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
708   RNA_def_function_ui_description(func, "Set the cursor");
709 
710   func = RNA_def_function(srna, "cursor_modal_set", "WM_cursor_modal_set");
711   parm = RNA_def_property(func, "cursor", PROP_ENUM, PROP_NONE);
712   RNA_def_property_enum_items(parm, rna_enum_window_cursor_items);
713   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
714   RNA_def_function_ui_description(func, "Set the cursor, so the previous cursor can be restored");
715 
716   RNA_def_function(srna, "cursor_modal_restore", "WM_cursor_modal_restore");
717   RNA_def_function_ui_description(
718       func, "Restore the previous cursor after calling ``cursor_modal_set``");
719 
720   /* Arguments match 'rna_KeyMap_item_new'. */
721   func = RNA_def_function(srna, "event_simulate", "rna_Window_event_add_simulate");
722   RNA_def_function_flag(func, FUNC_USE_REPORTS);
723   parm = RNA_def_enum(func, "type", rna_enum_event_type_items, 0, "Type", "");
724   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
725   parm = RNA_def_enum(func, "value", rna_enum_event_value_items, 0, "Value", "");
726   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
727   parm = RNA_def_string(func, "unicode", NULL, 0, "", "");
728   RNA_def_parameter_clear_flags(parm, PROP_NEVER_NULL, 0);
729 
730   RNA_def_int(func, "x", 0, INT_MIN, INT_MAX, "", "", INT_MIN, INT_MAX);
731   RNA_def_int(func, "y", 0, INT_MIN, INT_MAX, "", "", INT_MIN, INT_MAX);
732 
733   RNA_def_boolean(func, "shift", 0, "Shift", "");
734   RNA_def_boolean(func, "ctrl", 0, "Ctrl", "");
735   RNA_def_boolean(func, "alt", 0, "Alt", "");
736   RNA_def_boolean(func, "oskey", 0, "OS Key", "");
737   parm = RNA_def_pointer(func, "event", "Event", "Item", "Added key map item");
738   RNA_def_function_return(func, parm);
739 }
740 
RNA_api_wm(StructRNA * srna)741 void RNA_api_wm(StructRNA *srna)
742 {
743   FunctionRNA *func;
744   PropertyRNA *parm;
745 
746   func = RNA_def_function(srna, "fileselect_add", "WM_event_add_fileselect");
747   RNA_def_function_ui_description(
748       func,
749       "Opens a file selector with an operator. "
750       "The string properties 'filepath', 'filename', 'directory' and a 'files' "
751       "collection are assigned when present in the operator");
752   rna_generic_op_invoke(func, 0);
753 
754   func = RNA_def_function(srna, "modal_handler_add", "rna_event_modal_handler_add");
755   RNA_def_function_ui_description(
756       func,
757       "Add a modal handler to the window manager, for the given modal operator "
758       "(called by invoke() with self, just before returning {'RUNNING_MODAL'})");
759   RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_CONTEXT);
760   parm = RNA_def_pointer(func, "operator", "Operator", "", "Operator to call");
761   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
762   RNA_def_function_return(
763       func, RNA_def_boolean(func, "handle", 1, "", "Whether adding the handler was successful"));
764 
765   func = RNA_def_function(srna, "event_timer_add", "rna_event_timer_add");
766   RNA_def_function_ui_description(
767       func, "Add a timer to the given window, to generate periodic 'TIMER' events");
768   parm = RNA_def_property(func, "time_step", PROP_FLOAT, PROP_NONE);
769   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
770   RNA_def_property_range(parm, 0.0, FLT_MAX);
771   RNA_def_property_ui_text(parm, "Time Step", "Interval in seconds between timer events");
772   RNA_def_pointer(func, "window", "Window", "", "Window to attach the timer to, or None");
773   parm = RNA_def_pointer(func, "result", "Timer", "", "");
774   RNA_def_function_return(func, parm);
775 
776   func = RNA_def_function(srna, "event_timer_remove", "rna_event_timer_remove");
777   parm = RNA_def_pointer(func, "timer", "Timer", "", "");
778   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
779 
780   func = RNA_def_function(srna, "gizmo_group_type_ensure", "rna_gizmo_group_type_ensure");
781   RNA_def_function_ui_description(
782       func, "Activate an existing widget group (when the persistent option isn't set)");
783   RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_REPORTS);
784   parm = RNA_def_string(func, "identifier", NULL, 0, "", "Gizmo group type name");
785   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
786 
787   func = RNA_def_function(
788       srna, "gizmo_group_type_unlink_delayed", "rna_gizmo_group_type_unlink_delayed");
789   RNA_def_function_ui_description(func,
790                                   "Unlink a widget group (when the persistent option is set)");
791   RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_REPORTS);
792   parm = RNA_def_string(func, "identifier", NULL, 0, "", "Gizmo group type name");
793   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
794 
795   /* Progress bar interface */
796   func = RNA_def_function(srna, "progress_begin", "rna_progress_begin");
797   RNA_def_function_ui_description(func, "Start progress report");
798   parm = RNA_def_property(func, "min", PROP_FLOAT, PROP_NONE);
799   RNA_def_property_ui_text(parm, "min", "any value in range [0,9999]");
800   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
801   parm = RNA_def_property(func, "max", PROP_FLOAT, PROP_NONE);
802   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
803   RNA_def_property_ui_text(parm, "max", "any value in range [min+1,9998]");
804 
805   func = RNA_def_function(srna, "progress_update", "rna_progress_update");
806   RNA_def_function_ui_description(func, "Update the progress feedback");
807   parm = RNA_def_property(func, "value", PROP_FLOAT, PROP_NONE);
808   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
809   RNA_def_property_ui_text(
810       parm, "value", "Any value between min and max as set in progress_begin()");
811 
812   func = RNA_def_function(srna, "progress_end", "rna_progress_end");
813   RNA_def_function_ui_description(func, "Terminate progress report");
814 
815   /* invoke functions, for use with python */
816   func = RNA_def_function(srna, "invoke_props_popup", "rna_Operator_props_popup");
817   RNA_def_function_ui_description(
818       func,
819       "Operator popup invoke "
820       "(show operator properties and execute it automatically on changes)");
821   rna_generic_op_invoke(func, WM_GEN_INVOKE_EVENT | WM_GEN_INVOKE_RETURN);
822 
823   /* invoked dialog opens popup with OK button, does not auto-exec operator. */
824   func = RNA_def_function(srna, "invoke_props_dialog", "WM_operator_props_dialog_popup");
825   RNA_def_function_ui_description(
826       func,
827       "Operator dialog (non-autoexec popup) invoke "
828       "(show operator properties and only execute it on click on OK button)");
829   rna_generic_op_invoke(func, WM_GEN_INVOKE_SIZE | WM_GEN_INVOKE_RETURN);
830 
831   /* invoke enum */
832   func = RNA_def_function(srna, "invoke_search_popup", "rna_Operator_enum_search_invoke");
833   RNA_def_function_ui_description(
834       func,
835       "Operator search popup invoke which "
836       "searches values of the operator's :class:`bpy.types.Operator.bl_property` "
837       "(which must be an EnumProperty), executing it on confirmation");
838   rna_generic_op_invoke(func, 0);
839 
840   /* invoke functions, for use with python */
841   func = RNA_def_function(srna, "invoke_popup", "WM_operator_ui_popup");
842   RNA_def_function_ui_description(func,
843                                   "Operator popup invoke "
844                                   "(only shows operator's properties, without executing it)");
845   rna_generic_op_invoke(func, WM_GEN_INVOKE_SIZE | WM_GEN_INVOKE_RETURN);
846 
847   func = RNA_def_function(srna, "invoke_confirm", "rna_Operator_confirm");
848   RNA_def_function_ui_description(
849       func,
850       "Operator confirmation popup "
851       "(only to let user confirm the execution, no operator properties shown)");
852   rna_generic_op_invoke(func, WM_GEN_INVOKE_EVENT | WM_GEN_INVOKE_RETURN);
853 
854   /* wrap UI_popup_menu_begin */
855   func = RNA_def_function(srna, "popmenu_begin__internal", "rna_PopMenuBegin");
856   RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_CONTEXT);
857   parm = RNA_def_string(func, "title", NULL, 0, "", "");
858   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
859   parm = RNA_def_property(func, "icon", PROP_ENUM, PROP_NONE);
860   RNA_def_property_enum_items(parm, rna_enum_icon_items);
861   /* return */
862   parm = RNA_def_pointer(func, "menu", "UIPopupMenu", "", "");
863   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_RNAPTR);
864   RNA_def_function_return(func, parm);
865 
866   /* wrap UI_popup_menu_end */
867   func = RNA_def_function(srna, "popmenu_end__internal", "rna_PopMenuEnd");
868   RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_CONTEXT);
869   parm = RNA_def_pointer(func, "menu", "UIPopupMenu", "", "");
870   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_RNAPTR | PARM_REQUIRED);
871 
872   /* wrap UI_popover_begin */
873   func = RNA_def_function(srna, "popover_begin__internal", "rna_PopoverBegin");
874   RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_CONTEXT);
875   RNA_def_property(func, "ui_units_x", PROP_INT, PROP_UNSIGNED);
876   /* return */
877   parm = RNA_def_pointer(func, "menu", "UIPopover", "", "");
878   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_RNAPTR);
879   RNA_def_function_return(func, parm);
880   RNA_def_boolean(
881       func, "from_active_button", 0, "Use Button", "Use the active button for positioning");
882 
883   /* wrap UI_popover_end */
884   func = RNA_def_function(srna, "popover_end__internal", "rna_PopoverEnd");
885   RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_CONTEXT);
886   parm = RNA_def_pointer(func, "menu", "UIPopover", "", "");
887   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_RNAPTR | PARM_REQUIRED);
888   RNA_def_pointer(func, "keymap", "KeyMap", "Key Map", "Active key map");
889 
890   /* wrap uiPieMenuBegin */
891   func = RNA_def_function(srna, "piemenu_begin__internal", "rna_PieMenuBegin");
892   RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_CONTEXT);
893   parm = RNA_def_string(func, "title", NULL, 0, "", "");
894   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
895   parm = RNA_def_property(func, "icon", PROP_ENUM, PROP_NONE);
896   RNA_def_property_enum_items(parm, rna_enum_icon_items);
897   parm = RNA_def_pointer(func, "event", "Event", "", "");
898   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_RNAPTR);
899   /* return */
900   parm = RNA_def_pointer(func, "menu_pie", "UIPieMenu", "", "");
901   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_RNAPTR);
902   RNA_def_function_return(func, parm);
903 
904   /* wrap uiPieMenuEnd */
905   func = RNA_def_function(srna, "piemenu_end__internal", "rna_PieMenuEnd");
906   RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_CONTEXT);
907   parm = RNA_def_pointer(func, "menu", "UIPieMenu", "", "");
908   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_RNAPTR | PARM_REQUIRED);
909 
910   /* access last operator options (optionally create). */
911   func = RNA_def_function(
912       srna, "operator_properties_last", "rna_WindoManager_operator_properties_last");
913   RNA_def_function_flag(func, FUNC_NO_SELF);
914   parm = RNA_def_string(func, "operator", NULL, 0, "", "");
915   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
916   /* return */
917   parm = RNA_def_pointer(func, "result", "OperatorProperties", "", "");
918   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_RNAPTR);
919   RNA_def_function_return(func, parm);
920 
921   RNA_def_function(srna, "print_undo_steps", "rna_WindowManager_print_undo_steps");
922 
923   /* Used by (#SCRIPT_OT_reload). */
924   func = RNA_def_function(srna, "tag_script_reload", "rna_WindowManager_tag_script_reload");
925   RNA_def_function_ui_description(
926       func, "Tag for refreshing the interface after scripts have been reloaded");
927   RNA_def_function_flag(func, FUNC_NO_SELF);
928 
929   parm = RNA_def_property(srna, "is_interface_locked", PROP_BOOLEAN, PROP_NONE);
930   RNA_def_property_ui_text(
931       parm,
932       "Is Interface Locked",
933       "If true, the interface is currently locked by a running job and data shouldn't be modified "
934       "from application timers. Otherwise, the running job might conflict with the handler "
935       "causing unexpected results or even crashes");
936   RNA_def_property_clear_flag(parm, PROP_EDITABLE);
937 }
938 
RNA_api_operator(StructRNA * srna)939 void RNA_api_operator(StructRNA *srna)
940 {
941   FunctionRNA *func;
942   PropertyRNA *parm;
943 
944   /* utility, not for registering */
945   func = RNA_def_function(srna, "report", "rna_Operator_report");
946   parm = RNA_def_enum_flag(func, "type", rna_enum_wm_report_items, 0, "Type", "");
947   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
948   parm = RNA_def_string(func, "message", NULL, 0, "Report Message", "");
949   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
950 
951   /* utility, not for registering */
952   func = RNA_def_function(srna, "is_repeat", "rna_Operator_is_repeat");
953   RNA_def_function_flag(func, FUNC_USE_CONTEXT);
954   /* return */
955   parm = RNA_def_boolean(func, "result", 0, "result", "");
956   RNA_def_function_return(func, parm);
957 
958   /* Registration */
959 
960   /* poll */
961   func = RNA_def_function(srna, "poll", NULL);
962   RNA_def_function_ui_description(func, "Test if the operator can be called or not");
963   RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_REGISTER_OPTIONAL);
964   RNA_def_function_return(func, RNA_def_boolean(func, "visible", 1, "", ""));
965   parm = RNA_def_pointer(func, "context", "Context", "", "");
966   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
967 
968   /* exec */
969   func = RNA_def_function(srna, "execute", NULL);
970   RNA_def_function_ui_description(func, "Execute the operator");
971   RNA_def_function_flag(func, FUNC_REGISTER_OPTIONAL | FUNC_ALLOW_WRITE);
972   parm = RNA_def_pointer(func, "context", "Context", "", "");
973   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
974 
975   /* better name? */
976   parm = RNA_def_enum_flag(
977       func, "result", rna_enum_operator_return_items, OPERATOR_FINISHED, "result", "");
978   RNA_def_function_return(func, parm);
979 
980   /* check */
981   func = RNA_def_function(srna, "check", NULL);
982   RNA_def_function_ui_description(
983       func, "Check the operator settings, return True to signal a change to redraw");
984   RNA_def_function_flag(func, FUNC_REGISTER_OPTIONAL | FUNC_ALLOW_WRITE);
985   parm = RNA_def_pointer(func, "context", "Context", "", "");
986   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
987 
988   parm = RNA_def_boolean(func, "result", 0, "result", ""); /* better name? */
989   RNA_def_function_return(func, parm);
990 
991   /* invoke */
992   func = RNA_def_function(srna, "invoke", NULL);
993   RNA_def_function_ui_description(func, "Invoke the operator");
994   RNA_def_function_flag(func, FUNC_REGISTER_OPTIONAL | FUNC_ALLOW_WRITE);
995   parm = RNA_def_pointer(func, "context", "Context", "", "");
996   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
997   parm = RNA_def_pointer(func, "event", "Event", "", "");
998   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
999 
1000   /* better name? */
1001   parm = RNA_def_enum_flag(
1002       func, "result", rna_enum_operator_return_items, OPERATOR_FINISHED, "result", "");
1003   RNA_def_function_return(func, parm);
1004 
1005   func = RNA_def_function(srna, "modal", NULL); /* same as invoke */
1006   RNA_def_function_ui_description(func, "Modal operator function");
1007   RNA_def_function_flag(func, FUNC_REGISTER_OPTIONAL | FUNC_ALLOW_WRITE);
1008   parm = RNA_def_pointer(func, "context", "Context", "", "");
1009   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
1010   parm = RNA_def_pointer(func, "event", "Event", "", "");
1011   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
1012 
1013   /* better name? */
1014   parm = RNA_def_enum_flag(
1015       func, "result", rna_enum_operator_return_items, OPERATOR_FINISHED, "result", "");
1016   RNA_def_function_return(func, parm);
1017 
1018   /* draw */
1019   func = RNA_def_function(srna, "draw", NULL);
1020   RNA_def_function_ui_description(func, "Draw function for the operator");
1021   RNA_def_function_flag(func, FUNC_REGISTER_OPTIONAL);
1022   parm = RNA_def_pointer(func, "context", "Context", "", "");
1023   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
1024 
1025   /* cancel */
1026   func = RNA_def_function(srna, "cancel", NULL);
1027   RNA_def_function_ui_description(func, "Called when the operator is canceled");
1028   RNA_def_function_flag(func, FUNC_REGISTER_OPTIONAL | FUNC_ALLOW_WRITE);
1029   parm = RNA_def_pointer(func, "context", "Context", "", "");
1030   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
1031 
1032   /* description */
1033   func = RNA_def_function(srna, "description", NULL);
1034   RNA_def_function_ui_description(func, "Compute a description string that depends on parameters");
1035   RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_REGISTER_OPTIONAL);
1036   parm = RNA_def_string(func, "result", NULL, 4096, "result", "");
1037   RNA_def_parameter_clear_flags(parm, PROP_NEVER_NULL, 0);
1038   RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0);
1039   RNA_def_function_output(func, parm);
1040   parm = RNA_def_pointer(func, "context", "Context", "", "");
1041   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
1042   parm = RNA_def_pointer(func, "properties", "OperatorProperties", "", "");
1043   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED | PARM_RNAPTR);
1044 }
1045 
RNA_api_macro(StructRNA * srna)1046 void RNA_api_macro(StructRNA *srna)
1047 {
1048   FunctionRNA *func;
1049   PropertyRNA *parm;
1050 
1051   /* utility, not for registering */
1052   func = RNA_def_function(srna, "report", "rna_Operator_report");
1053   parm = RNA_def_enum_flag(func, "type", rna_enum_wm_report_items, 0, "Type", "");
1054   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1055   parm = RNA_def_string(func, "message", NULL, 0, "Report Message", "");
1056   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1057 
1058   /* Registration */
1059 
1060   /* poll */
1061   func = RNA_def_function(srna, "poll", NULL);
1062   RNA_def_function_ui_description(func, "Test if the operator can be called or not");
1063   RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_REGISTER_OPTIONAL);
1064   RNA_def_function_return(func, RNA_def_boolean(func, "visible", 1, "", ""));
1065   parm = RNA_def_pointer(func, "context", "Context", "", "");
1066   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
1067 
1068   /* draw */
1069   func = RNA_def_function(srna, "draw", NULL);
1070   RNA_def_function_ui_description(func, "Draw function for the operator");
1071   RNA_def_function_flag(func, FUNC_REGISTER_OPTIONAL);
1072   parm = RNA_def_pointer(func, "context", "Context", "", "");
1073   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
1074 }
1075 
RNA_api_keyconfig(StructRNA * UNUSED (srna))1076 void RNA_api_keyconfig(StructRNA *UNUSED(srna))
1077 {
1078   /* FunctionRNA *func; */
1079   /* PropertyRNA *parm; */
1080 }
1081 
RNA_api_keymap(StructRNA * srna)1082 void RNA_api_keymap(StructRNA *srna)
1083 {
1084   FunctionRNA *func;
1085   PropertyRNA *parm;
1086 
1087   func = RNA_def_function(srna, "active", "rna_keymap_active");
1088   RNA_def_function_flag(func, FUNC_USE_CONTEXT);
1089   parm = RNA_def_pointer(func, "keymap", "KeyMap", "Key Map", "Active key map");
1090   RNA_def_function_return(func, parm);
1091 
1092   func = RNA_def_function(srna, "restore_to_default", "rna_keymap_restore_to_default");
1093   RNA_def_function_flag(func, FUNC_USE_CONTEXT);
1094 
1095   func = RNA_def_function(srna, "restore_item_to_default", "rna_keymap_restore_item_to_default");
1096   RNA_def_function_flag(func, FUNC_USE_CONTEXT);
1097   parm = RNA_def_pointer(func, "item", "KeyMapItem", "Item", "");
1098   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
1099 }
1100 
RNA_api_keymapitem(StructRNA * srna)1101 void RNA_api_keymapitem(StructRNA *srna)
1102 {
1103   FunctionRNA *func;
1104   PropertyRNA *parm;
1105 
1106   func = RNA_def_function(srna, "compare", "WM_keymap_item_compare");
1107   parm = RNA_def_pointer(func, "item", "KeyMapItem", "Item", "");
1108   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1109   parm = RNA_def_boolean(func, "result", 0, "Comparison result", "");
1110   RNA_def_function_return(func, parm);
1111 
1112   func = RNA_def_function(srna, "to_string", "rna_KeyMapItem_to_string");
1113   RNA_def_boolean(func, "compact", false, "Compact", "");
1114   parm = RNA_def_string(func, "result", NULL, UI_MAX_SHORTCUT_STR, "result", "");
1115   RNA_def_parameter_flags(parm, PROP_THICK_WRAP, 0);
1116   RNA_def_function_output(func, parm);
1117 }
1118 
RNA_api_keymapitems(StructRNA * srna)1119 void RNA_api_keymapitems(StructRNA *srna)
1120 {
1121   FunctionRNA *func;
1122   PropertyRNA *parm;
1123 
1124   func = RNA_def_function(srna, "new", "rna_KeyMap_item_new");
1125   RNA_def_function_flag(func, FUNC_USE_REPORTS);
1126   parm = RNA_def_string(func, "idname", NULL, 0, "Operator Identifier", "");
1127   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1128   parm = RNA_def_enum(func, "type", rna_enum_event_type_items, 0, "Type", "");
1129   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1130   parm = RNA_def_enum(func, "value", rna_enum_event_value_items, 0, "Value", "");
1131   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1132   RNA_def_boolean(func, "any", 0, "Any", "");
1133   RNA_def_boolean(func, "shift", 0, "Shift", "");
1134   RNA_def_boolean(func, "ctrl", 0, "Ctrl", "");
1135   RNA_def_boolean(func, "alt", 0, "Alt", "");
1136   RNA_def_boolean(func, "oskey", 0, "OS Key", "");
1137   RNA_def_enum(func, "key_modifier", rna_enum_event_type_items, 0, "Key Modifier", "");
1138   RNA_def_boolean(func, "repeat", true, "Repeat", "When set, accept key-repeat events");
1139   RNA_def_boolean(func,
1140                   "head",
1141                   0,
1142                   "At Head",
1143                   "Force item to be added at start (not end) of key map so that "
1144                   "it doesn't get blocked by an existing key map item");
1145   parm = RNA_def_pointer(func, "item", "KeyMapItem", "Item", "Added key map item");
1146   RNA_def_function_return(func, parm);
1147 
1148   func = RNA_def_function(srna, "new_modal", "rna_KeyMap_item_new_modal");
1149   RNA_def_function_flag(func, FUNC_USE_REPORTS);
1150   parm = RNA_def_string(func, "propvalue", NULL, 0, "Property Value", "");
1151   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1152   parm = RNA_def_enum(func, "type", rna_enum_event_type_items, 0, "Type", "");
1153   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1154   parm = RNA_def_enum(func, "value", rna_enum_event_value_items, 0, "Value", "");
1155   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1156   RNA_def_boolean(func, "any", 0, "Any", "");
1157   RNA_def_boolean(func, "shift", 0, "Shift", "");
1158   RNA_def_boolean(func, "ctrl", 0, "Ctrl", "");
1159   RNA_def_boolean(func, "alt", 0, "Alt", "");
1160   RNA_def_boolean(func, "oskey", 0, "OS Key", "");
1161   RNA_def_enum(func, "key_modifier", rna_enum_event_type_items, 0, "Key Modifier", "");
1162   RNA_def_boolean(func, "repeat", true, "Repeat", "When set, accept key-repeat events");
1163   parm = RNA_def_pointer(func, "item", "KeyMapItem", "Item", "Added key map item");
1164   RNA_def_function_return(func, parm);
1165 
1166   func = RNA_def_function(srna, "new_from_item", "rna_KeyMap_item_new_from_item");
1167   RNA_def_function_flag(func, FUNC_USE_REPORTS);
1168   parm = RNA_def_pointer(func, "item", "KeyMapItem", "Item", "Item to use as a reference");
1169   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
1170   RNA_def_boolean(func, "head", 0, "At Head", "");
1171   parm = RNA_def_pointer(func, "result", "KeyMapItem", "Item", "Added key map item");
1172   RNA_def_function_return(func, parm);
1173 
1174   func = RNA_def_function(srna, "remove", "rna_KeyMap_item_remove");
1175   RNA_def_function_flag(func, FUNC_USE_REPORTS);
1176   parm = RNA_def_pointer(func, "item", "KeyMapItem", "Item", "");
1177   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED | PARM_RNAPTR);
1178   RNA_def_parameter_clear_flags(parm, PROP_THICK_WRAP, 0);
1179 
1180   func = RNA_def_function(srna, "from_id", "WM_keymap_item_find_id");
1181   parm = RNA_def_property(func, "id", PROP_INT, PROP_NONE);
1182   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1183   RNA_def_property_ui_text(parm, "id", "ID of the item");
1184   parm = RNA_def_pointer(func, "item", "KeyMapItem", "Item", "");
1185   RNA_def_function_return(func, parm);
1186 
1187   /* Keymap introspection
1188    * Args follow: KeyConfigs.find_item_from_operator */
1189   func = RNA_def_function(srna, "find_from_operator", "rna_KeyMap_item_find_from_operator");
1190   RNA_def_function_flag(func, FUNC_USE_SELF_ID);
1191   parm = RNA_def_string(func, "idname", NULL, 0, "Operator Identifier", "");
1192   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1193   parm = RNA_def_pointer(func, "properties", "OperatorProperties", "", "");
1194   RNA_def_parameter_flags(parm, 0, PARM_RNAPTR);
1195   RNA_def_enum_flag(
1196       func, "include", rna_enum_event_type_mask_items, EVT_TYPE_MASK_ALL, "Include", "");
1197   RNA_def_enum_flag(func, "exclude", rna_enum_event_type_mask_items, 0, "Exclude", "");
1198   parm = RNA_def_pointer(func, "item", "KeyMapItem", "", "");
1199   RNA_def_parameter_flags(parm, 0, PARM_RNAPTR);
1200   RNA_def_function_return(func, parm);
1201 
1202   func = RNA_def_function(srna, "match_event", "rna_KeyMap_item_match_event");
1203   RNA_def_function_flag(func, FUNC_USE_SELF_ID | FUNC_USE_CONTEXT);
1204   parm = RNA_def_pointer(func, "event", "Event", "", "");
1205   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1206   parm = RNA_def_pointer(func, "item", "KeyMapItem", "", "");
1207   RNA_def_parameter_flags(parm, 0, PARM_RNAPTR);
1208   RNA_def_function_return(func, parm);
1209 }
1210 
RNA_api_keymaps(StructRNA * srna)1211 void RNA_api_keymaps(StructRNA *srna)
1212 {
1213   FunctionRNA *func;
1214   PropertyRNA *parm;
1215 
1216   func = RNA_def_function(srna, "new", "rna_keymap_new"); /* add_keymap */
1217   RNA_def_function_flag(func, FUNC_USE_REPORTS);
1218   parm = RNA_def_string(func, "name", NULL, 0, "Name", "");
1219   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1220   RNA_def_enum(func, "space_type", rna_enum_space_type_items, SPACE_EMPTY, "Space Type", "");
1221   RNA_def_enum(
1222       func, "region_type", rna_enum_region_type_items, RGN_TYPE_WINDOW, "Region Type", "");
1223   RNA_def_boolean(func, "modal", 0, "Modal", "Keymap for modal operators");
1224   RNA_def_boolean(func, "tool", 0, "Tool", "Keymap for active tools");
1225   parm = RNA_def_pointer(func, "keymap", "KeyMap", "Key Map", "Added key map");
1226   RNA_def_function_return(func, parm);
1227 
1228   func = RNA_def_function(srna, "remove", "rna_KeyMap_remove"); /* remove_keymap */
1229   RNA_def_function_flag(func, FUNC_USE_REPORTS);
1230   parm = RNA_def_pointer(func, "keymap", "KeyMap", "Key Map", "Removed key map");
1231   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED | PARM_RNAPTR);
1232   RNA_def_parameter_clear_flags(parm, PROP_THICK_WRAP, 0);
1233 
1234   func = RNA_def_function(srna, "find", "rna_keymap_find"); /* find_keymap */
1235   parm = RNA_def_string(func, "name", NULL, 0, "Name", "");
1236   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1237   RNA_def_enum(func, "space_type", rna_enum_space_type_items, SPACE_EMPTY, "Space Type", "");
1238   RNA_def_enum(
1239       func, "region_type", rna_enum_region_type_items, RGN_TYPE_WINDOW, "Region Type", "");
1240   parm = RNA_def_pointer(func, "keymap", "KeyMap", "Key Map", "Corresponding key map");
1241   RNA_def_function_return(func, parm);
1242 
1243   func = RNA_def_function(srna, "find_modal", "rna_keymap_find_modal"); /* find_keymap_modal */
1244   parm = RNA_def_string(func, "name", NULL, 0, "Operator Name", "");
1245   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1246   parm = RNA_def_pointer(func, "keymap", "KeyMap", "Key Map", "Corresponding key map");
1247   RNA_def_function_return(func, parm);
1248 }
1249 
RNA_api_keyconfigs(StructRNA * srna)1250 void RNA_api_keyconfigs(StructRNA *srna)
1251 {
1252   FunctionRNA *func;
1253   PropertyRNA *parm;
1254 
1255   func = RNA_def_function(srna, "new", "WM_keyconfig_new_user"); /* add_keyconfig */
1256   parm = RNA_def_string(func, "name", NULL, 0, "Name", "");
1257   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1258   parm = RNA_def_pointer(
1259       func, "keyconfig", "KeyConfig", "Key Configuration", "Added key configuration");
1260   RNA_def_function_return(func, parm);
1261 
1262   func = RNA_def_function(srna, "remove", "rna_KeyConfig_remove"); /* remove_keyconfig */
1263   RNA_def_function_flag(func, FUNC_USE_REPORTS);
1264   parm = RNA_def_pointer(
1265       func, "keyconfig", "KeyConfig", "Key Configuration", "Removed key configuration");
1266   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED | PARM_RNAPTR);
1267   RNA_def_parameter_clear_flags(parm, PROP_THICK_WRAP, 0);
1268 
1269   /* Helper functions */
1270 
1271   /* Keymap introspection */
1272   func = RNA_def_function(
1273       srna, "find_item_from_operator", "rna_KeyConfig_find_item_from_operator");
1274   RNA_def_function_flag(func, FUNC_USE_CONTEXT);
1275   parm = RNA_def_string(func, "idname", NULL, 0, "Operator Identifier", "");
1276   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1277   parm = RNA_def_property(func, "context", PROP_ENUM, PROP_NONE);
1278   RNA_def_property_enum_items(parm, rna_enum_operator_context_items);
1279   parm = RNA_def_pointer(func, "properties", "OperatorProperties", "", "");
1280   RNA_def_parameter_flags(parm, 0, PARM_RNAPTR);
1281   RNA_def_enum_flag(
1282       func, "include", rna_enum_event_type_mask_items, EVT_TYPE_MASK_ALL, "Include", "");
1283   RNA_def_enum_flag(func, "exclude", rna_enum_event_type_mask_items, 0, "Exclude", "");
1284   parm = RNA_def_pointer(func, "keymap", "KeyMap", "", "");
1285   RNA_def_parameter_flags(parm, 0, PARM_RNAPTR | PARM_OUTPUT);
1286   parm = RNA_def_pointer(func, "item", "KeyMapItem", "", "");
1287   RNA_def_parameter_flags(parm, 0, PARM_RNAPTR);
1288   RNA_def_function_return(func, parm);
1289 
1290   RNA_def_function(srna, "update", "rna_KeyConfig_update"); /* WM_keyconfig_update */
1291 }
1292 
1293 #endif
1294