Beispiel #1
0
static int ui_button_press_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
  const bool skip_depressed = RNA_boolean_get(op->ptr, "skip_depressed");
  ARegion *ar_prev = CTX_wm_region(C);
  ARegion *ar = region_event_inside_for_screen(C, &event->x);

  if (ar == NULL) {
    ar = ar_prev;
  }

  CTX_wm_region_set(C, ar);
  uiBut *but = UI_context_active_but_get(C);
  CTX_wm_region_set(C, ar_prev);

  if (but == NULL) {
    return OPERATOR_PASS_THROUGH;
  }
  if (skip_depressed && (but->flag & (UI_SELECT | UI_SELECT_DRAW))) {
    return OPERATOR_PASS_THROUGH;
  }

  /* Weak, this is a workaround for 'UI_but_is_tool', which checks the operator type,
   * having this avoids a minor drawing glitch. */
  void *but_optype = but->optype;

  UI_but_execute(C, but);

  but->optype = but_optype;

  WM_event_add_mousemove(C);

  return OPERATOR_FINISHED;
}
Beispiel #2
0
static int editsource_exec(bContext *C, wmOperator *op)
{
  uiBut *but = UI_context_active_but_get(C);

  if (but) {
    GHashIterator ghi;
    struct uiEditSourceButStore *but_store = NULL;

    ARegion *ar = CTX_wm_region(C);
    int ret;

    /* needed else the active button does not get tested */
    UI_screen_free_active_but(C, CTX_wm_screen(C));

    // printf("%s: begin\n", __func__);

    /* take care not to return before calling ui_editsource_active_but_clear */
    ui_editsource_active_but_set(but);

    /* redraw and get active button python info */
    ED_region_do_layout(C, ar);
    ED_region_do_draw(C, ar);
    ar->do_draw = false;

    for (BLI_ghashIterator_init(&ghi, ui_editsource_info->hash);
         BLI_ghashIterator_done(&ghi) == false;
         BLI_ghashIterator_step(&ghi)) {
      uiBut *but_key = BLI_ghashIterator_getKey(&ghi);
      if (but_key && ui_editsource_uibut_match(&ui_editsource_info->but_orig, but_key)) {
        but_store = BLI_ghashIterator_getValue(&ghi);
        break;
      }
    }

    if (but_store) {
      if (but_store->py_dbg_ln != -1) {
        ret = editsource_text_edit(C, op, but_store->py_dbg_fn, but_store->py_dbg_ln);
      }
      else {
        BKE_report(
            op->reports, RPT_ERROR, "Active button is not from a script, cannot edit source");
        ret = OPERATOR_CANCELLED;
      }
    }
    else {
      BKE_report(op->reports, RPT_ERROR, "Active button match cannot be found");
      ret = OPERATOR_CANCELLED;
    }

    ui_editsource_active_but_clear();

    // printf("%s: end\n", __func__);

    return ret;
  }
  else {
    BKE_report(op->reports, RPT_ERROR, "Active button not found");
    return OPERATOR_CANCELLED;
  }
}
static bool eyedropper_colorband_init(bContext *C, wmOperator *op)
{
	ColorBand *band = NULL;
	EyedropperColorband *eye;

	uiBut *but = UI_context_active_but_get(C);

	if (but == NULL) {
		/* pass */
	}
	else if (but->type == UI_BTYPE_COLORBAND) {
		/* When invoked with a hotkey, we can find the band in 'but->poin'. */
		band = (ColorBand *)but->poin;
	}
	else {
		/* When invoked from a button it's in custom_data field. */
		band = (ColorBand *)but->custom_data;
	}

	if (!band)
		return false;

	op->customdata = eye = MEM_callocN(sizeof(EyedropperColorband), __func__);
	eye->color_buffer_alloc = 16;
	eye->color_buffer = MEM_mallocN(sizeof(*eye->color_buffer) * eye->color_buffer_alloc, __func__);
	eye->color_buffer_len = 0;
	eye->color_band = band;
	eye->init_color_band = *eye->color_band;
	eye->ptr = ((Colorband_RNAUpdateCb *)but->func_argN)->ptr;
	eye->prop  = ((Colorband_RNAUpdateCb *)but->func_argN)->prop;

	return true;
}
Beispiel #4
0
static bool copy_python_command_button_poll(bContext *C)
{
  uiBut *but = UI_context_active_but_get(C);

  if (but && (but->optype != NULL)) {
    return 1;
  }

  return 0;
}
static bool eyedropper_colorband_poll(bContext *C)
{
  uiBut *but = UI_context_active_but_get(C);
  if (but && but->type == UI_BTYPE_COLORBAND) {
    return true;
  }
  PointerRNA ptr = CTX_data_pointer_get_type(C, "color_ramp", &RNA_ColorRamp);
  if (ptr.data != NULL) {
    return true;
  }
  return false;
}
Beispiel #6
0
/**
 * Jump to the object or bone referred to by the current UI field value.
 *
 * \note quite heavy for a poll callback, but the operator is only
 * used as a right click menu item for certain UI field types, and
 * this will fail quickly if the context is completely unsuitable.
 */
static bool jump_to_target_button(bContext *C, bool poll)
{
  PointerRNA ptr, target_ptr;
  PropertyRNA *prop;
  int index;

  UI_context_active_but_prop_get(C, &ptr, &prop, &index);

  /* If there is a valid property... */
  if (ptr.data && prop) {
    const PropertyType type = RNA_property_type(prop);

    /* For pointer properties, use their value directly. */
    if (type == PROP_POINTER) {
      target_ptr = RNA_property_pointer_get(&ptr, prop);

      return jump_to_target_ptr(C, target_ptr, poll);
    }
    /* For string properties with prop_search, look up the search collection item. */
    else if (type == PROP_STRING) {
      const uiBut *but = UI_context_active_but_get(C);

      if (but->type == UI_BTYPE_SEARCH_MENU && but->search_func == ui_rna_collection_search_cb) {
        uiRNACollectionSearch *coll_search = but->search_arg;

        char str_buf[MAXBONENAME];
        char *str_ptr = RNA_property_string_get_alloc(&ptr, prop, str_buf, sizeof(str_buf), NULL);

        int found = RNA_property_collection_lookup_string(
            &coll_search->search_ptr, coll_search->search_prop, str_ptr, &target_ptr);

        if (str_ptr != str_buf) {
          MEM_freeN(str_ptr);
        }

        if (found) {
          return jump_to_target_ptr(C, target_ptr, poll);
        }
      }
    }
  }

  return false;
}
static bool eyedropper_colorband_init(bContext *C, wmOperator *op)
{
  ColorBand *band = NULL;

  uiBut *but = UI_context_active_but_get(C);

  if (but == NULL) {
    /* pass */
  }
  else if (but->type == UI_BTYPE_COLORBAND) {
    /* When invoked with a hotkey, we can find the band in 'but->poin'. */
    band = (ColorBand *)but->poin;
  }
  else {
    /* When invoked from a button it's in custom_data field. */
    band = (ColorBand *)but->custom_data;
  }

  if (!band) {
    PointerRNA ptr = CTX_data_pointer_get_type(C, "color_ramp", &RNA_ColorRamp);
    if (ptr.data != NULL) {
      band = ptr.data;
    }
  }

  if (!band) {
    return false;
  }

  EyedropperColorband *eye = MEM_callocN(sizeof(EyedropperColorband), __func__);
  eye->color_buffer_alloc = 16;
  eye->color_buffer = MEM_mallocN(sizeof(*eye->color_buffer) * eye->color_buffer_alloc, __func__);
  eye->color_buffer_len = 0;
  eye->color_band = band;
  eye->init_color_band = *eye->color_band;
  eye->ptr = ((Colorband_RNAUpdateCb *)but->func_argN)->ptr;
  eye->prop = ((Colorband_RNAUpdateCb *)but->func_argN)->prop;
  eye->is_undo = UI_but_flag_is_set(but, UI_BUT_UNDO);

  op->customdata = eye;

  return true;
}
Beispiel #8
0
static int copy_python_command_button_exec(bContext *C, wmOperator *UNUSED(op))
{
  uiBut *but = UI_context_active_but_get(C);

  if (but && (but->optype != NULL)) {
    PointerRNA *opptr;
    char *str;
    opptr = UI_but_operator_ptr_get(but); /* allocated when needed, the button owns it */

    str = WM_operator_pystring_ex(C, NULL, false, true, but->optype, opptr);

    WM_clipboard_text_set(str, 0);

    MEM_freeN(str);

    return OPERATOR_FINISHED;
  }

  return OPERATOR_CANCELLED;
}
Beispiel #9
0
static int edittranslation_exec(bContext *C, wmOperator *op)
{
  uiBut *but = UI_context_active_but_get(C);
  int ret = OPERATOR_CANCELLED;

  if (but) {
    wmOperatorType *ot;
    PointerRNA ptr;
    char popath[FILE_MAX];
    const char *root = U.i18ndir;
    const char *uilng = BLT_lang_get();

    uiStringInfo but_label = {BUT_GET_LABEL, NULL};
    uiStringInfo rna_label = {BUT_GET_RNA_LABEL, NULL};
    uiStringInfo enum_label = {BUT_GET_RNAENUM_LABEL, NULL};
    uiStringInfo but_tip = {BUT_GET_TIP, NULL};
    uiStringInfo rna_tip = {BUT_GET_RNA_TIP, NULL};
    uiStringInfo enum_tip = {BUT_GET_RNAENUM_TIP, NULL};
    uiStringInfo rna_struct = {BUT_GET_RNASTRUCT_IDENTIFIER, NULL};
    uiStringInfo rna_prop = {BUT_GET_RNAPROP_IDENTIFIER, NULL};
    uiStringInfo rna_enum = {BUT_GET_RNAENUM_IDENTIFIER, NULL};
    uiStringInfo rna_ctxt = {BUT_GET_RNA_LABEL_CONTEXT, NULL};

    if (!BLI_is_dir(root)) {
      BKE_report(op->reports,
                 RPT_ERROR,
                 "Please set your Preferences' 'Translation Branches "
                 "Directory' path to a valid directory");
      return OPERATOR_CANCELLED;
    }
    ot = WM_operatortype_find(EDTSRC_I18N_OP_NAME, 0);
    if (ot == NULL) {
      BKE_reportf(op->reports,
                  RPT_ERROR,
                  "Could not find operator '%s'! Please enable ui_translate add-on "
                  "in the User Preferences",
                  EDTSRC_I18N_OP_NAME);
      return OPERATOR_CANCELLED;
    }
    /* Try to find a valid po file for current language... */
    edittranslation_find_po_file(root, uilng, popath, FILE_MAX);
    /* printf("po path: %s\n", popath); */
    if (popath[0] == '\0') {
      BKE_reportf(
          op->reports, RPT_ERROR, "No valid po found for language '%s' under %s", uilng, root);
      return OPERATOR_CANCELLED;
    }

    UI_but_string_info_get(C,
                           but,
                           &but_label,
                           &rna_label,
                           &enum_label,
                           &but_tip,
                           &rna_tip,
                           &enum_tip,
                           &rna_struct,
                           &rna_prop,
                           &rna_enum,
                           &rna_ctxt,
                           NULL);

    WM_operator_properties_create_ptr(&ptr, ot);
    RNA_string_set(&ptr, "lang", uilng);
    RNA_string_set(&ptr, "po_file", popath);
    RNA_string_set(&ptr, "but_label", but_label.strinfo);
    RNA_string_set(&ptr, "rna_label", rna_label.strinfo);
    RNA_string_set(&ptr, "enum_label", enum_label.strinfo);
    RNA_string_set(&ptr, "but_tip", but_tip.strinfo);
    RNA_string_set(&ptr, "rna_tip", rna_tip.strinfo);
    RNA_string_set(&ptr, "enum_tip", enum_tip.strinfo);
    RNA_string_set(&ptr, "rna_struct", rna_struct.strinfo);
    RNA_string_set(&ptr, "rna_prop", rna_prop.strinfo);
    RNA_string_set(&ptr, "rna_enum", rna_enum.strinfo);
    RNA_string_set(&ptr, "rna_ctxt", rna_ctxt.strinfo);
    ret = WM_operator_name_call_ptr(C, ot, WM_OP_INVOKE_DEFAULT, &ptr);

    /* Clean up */
    if (but_label.strinfo) {
      MEM_freeN(but_label.strinfo);
    }
    if (rna_label.strinfo) {
      MEM_freeN(rna_label.strinfo);
    }
    if (enum_label.strinfo) {
      MEM_freeN(enum_label.strinfo);
    }
    if (but_tip.strinfo) {
      MEM_freeN(but_tip.strinfo);
    }
    if (rna_tip.strinfo) {
      MEM_freeN(rna_tip.strinfo);
    }
    if (enum_tip.strinfo) {
      MEM_freeN(enum_tip.strinfo);
    }
    if (rna_struct.strinfo) {
      MEM_freeN(rna_struct.strinfo);
    }
    if (rna_prop.strinfo) {
      MEM_freeN(rna_prop.strinfo);
    }
    if (rna_enum.strinfo) {
      MEM_freeN(rna_enum.strinfo);
    }
    if (rna_ctxt.strinfo) {
      MEM_freeN(rna_ctxt.strinfo);
    }

    return ret;
  }
  else {
    BKE_report(op->reports, RPT_ERROR, "Active button not found");
    return OPERATOR_CANCELLED;
  }
}
uiPopupBlockHandle *ui_popup_block_create(bContext *C,
                                          ARegion *butregion,
                                          uiBut *but,
                                          uiBlockCreateFunc create_func,
                                          uiBlockHandleCreateFunc handle_create_func,
                                          void *arg,
                                          void (*arg_free)(void *arg))
{
  wmWindow *window = CTX_wm_window(C);
  uiBut *activebut = UI_context_active_but_get(C);
  static ARegionType type;
  ARegion *ar;
  uiBlock *block;
  uiPopupBlockHandle *handle;

  /* disable tooltips from buttons below */
  if (activebut) {
    UI_but_tooltip_timer_remove(C, activebut);
  }
  /* standard cursor by default */
  WM_cursor_set(window, CURSOR_STD);

  /* create handle */
  handle = MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");

  /* store context for operator */
  handle->ctx_area = CTX_wm_area(C);
  handle->ctx_region = CTX_wm_region(C);

  /* store vars to refresh popup (RGN_DRAW_REFRESH_UI) */
  handle->popup_create_vars.create_func = create_func;
  handle->popup_create_vars.handle_create_func = handle_create_func;
  handle->popup_create_vars.arg = arg;
  handle->popup_create_vars.arg_free = arg_free;
  handle->popup_create_vars.but = but;
  handle->popup_create_vars.butregion = but ? butregion : NULL;
  copy_v2_v2_int(handle->popup_create_vars.event_xy, &window->eventstate->x);

  /* don't allow by default, only if popup type explicitly supports it */
  handle->can_refresh = false;

  /* create area region */
  ar = ui_region_temp_add(CTX_wm_screen(C));
  handle->region = ar;

  memset(&type, 0, sizeof(ARegionType));
  type.draw = ui_block_region_draw;
  type.layout = ui_block_region_refresh;
  type.regionid = RGN_TYPE_TEMPORARY;
  ar->type = &type;

  UI_region_handlers_add(&ar->handlers);

  block = ui_popup_block_refresh(C, handle, butregion, but);
  handle = block->handle;

  /* keep centered on window resizing */
  if (block->bounds_type == UI_BLOCK_BOUNDS_POPUP_CENTER) {
    type.listener = ui_block_region_popup_window_listener;
  }

  return handle;
}
static int eyedropper_colorband_poll(bContext *C)
{
	uiBut *but = UI_context_active_but_get(C);
	return (but && but->type == UI_BTYPE_COLORBAND);
}