Changeset View
Changeset View
Standalone View
Standalone View
source/blender/editors/space_text/text_ops.c
| Show First 20 Lines • Show All 264 Lines • ▼ Show 20 Lines | if (prop) { | ||||
| RNA_id_pointer_create(&text->id, &idptr); | RNA_id_pointer_create(&text->id, &idptr); | ||||
| RNA_property_pointer_set(&ptr, prop, idptr, NULL); | RNA_property_pointer_set(&ptr, prop, idptr, NULL); | ||||
| RNA_property_update(C, &ptr, prop); | RNA_property_update(C, &ptr, prop); | ||||
| } | } | ||||
| else if (st) { | else if (st) { | ||||
| st->text = text; | st->text = text; | ||||
| st->left = 0; | st->left = 0; | ||||
| st->top = 0; | st->top = 0; | ||||
| st->scroll_accum[0] = 0.0f; | st->scroll_ofs_px[0] = 0; | ||||
| st->scroll_accum[1] = 0.0f; | st->scroll_ofs_px[1] = 0; | ||||
| text_drawcache_tag_update(st, 1); | text_drawcache_tag_update(st, 1); | ||||
| } | } | ||||
| WM_event_add_notifier(C, NC_TEXT | NA_ADDED, text); | WM_event_add_notifier(C, NC_TEXT | NA_ADDED, text); | ||||
| return OPERATOR_FINISHED; | return OPERATOR_FINISHED; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | if (pprop->prop) { | ||||
| RNA_id_pointer_create(&text->id, &idptr); | RNA_id_pointer_create(&text->id, &idptr); | ||||
| RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr, NULL); | RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr, NULL); | ||||
| RNA_property_update(C, &pprop->ptr, pprop->prop); | RNA_property_update(C, &pprop->ptr, pprop->prop); | ||||
| } | } | ||||
| else if (st) { | else if (st) { | ||||
| st->text = text; | st->text = text; | ||||
| st->left = 0; | st->left = 0; | ||||
| st->top = 0; | st->top = 0; | ||||
| st->scroll_accum[0] = 0.0f; | st->scroll_ofs_px[0] = 0; | ||||
| st->scroll_accum[1] = 0.0f; | st->scroll_ofs_px[1] = 0; | ||||
| } | } | ||||
| text_drawcache_tag_update(st, 1); | text_drawcache_tag_update(st, 1); | ||||
| WM_event_add_notifier(C, NC_TEXT | NA_ADDED, text); | WM_event_add_notifier(C, NC_TEXT | NA_ADDED, text); | ||||
| MEM_freeN(op->customdata); | MEM_freeN(op->customdata); | ||||
| return OPERATOR_FINISHED; | return OPERATOR_FINISHED; | ||||
| ▲ Show 20 Lines • Show All 698 Lines • ▼ Show 20 Lines | void TEXT_OT_cut(wmOperatorType *ot) | ||||
| /* flags */ | /* flags */ | ||||
| ot->flag = OPTYPE_UNDO; | ot->flag = OPTYPE_UNDO; | ||||
| } | } | ||||
| /** \} */ | /** \} */ | ||||
| /* -------------------------------------------------------------------- */ | /* -------------------------------------------------------------------- */ | ||||
| /** \name Indent or Autocomplete Operator | |||||
| * \{ */ | |||||
| static int text_indent_or_autocomplete_exec(bContext *C, wmOperator *UNUSED(op)) | |||||
| { | |||||
| Text *text = CTX_data_edit_text(C); | |||||
| TextLine *line = text->curl; | |||||
| bool text_before_cursor = text->curc != 0 && !ELEM(line->line[text->curc - 1], ' ', '\t'); | |||||
| if (text_before_cursor && (txt_has_sel(text) == false)) { | |||||
| WM_operator_name_call(C, "TEXT_OT_autocomplete", WM_OP_INVOKE_DEFAULT, NULL); | |||||
| } | |||||
| else { | |||||
| WM_operator_name_call(C, "TEXT_OT_indent", WM_OP_EXEC_DEFAULT, NULL); | |||||
| } | |||||
| return OPERATOR_FINISHED; | |||||
| } | |||||
| void TEXT_OT_indent_or_autocomplete(wmOperatorType *ot) | |||||
| { | |||||
| /* identifiers */ | |||||
| ot->name = "Indent or Autocomplete"; | |||||
| ot->idname = "TEXT_OT_indent_or_autocomplete"; | |||||
| ot->description = "Indent selected text or autocomplete"; | |||||
| /* api callbacks */ | |||||
| ot->exec = text_indent_or_autocomplete_exec; | |||||
| ot->poll = text_edit_poll; | |||||
| /* flags */ | |||||
| ot->flag = 0; | |||||
| } | |||||
| /** \} */ | |||||
| /* -------------------------------------------------------------------- */ | |||||
| /** \name Indent Operator | /** \name Indent Operator | ||||
| * \{ */ | * \{ */ | ||||
| static int text_indent_exec(bContext *C, wmOperator *UNUSED(op)) | static int text_indent_exec(bContext *C, wmOperator *UNUSED(op)) | ||||
| { | { | ||||
| Text *text = CTX_data_edit_text(C); | Text *text = CTX_data_edit_text(C); | ||||
| text_drawcache_tag_update(CTX_wm_space_text(C), 0); | text_drawcache_tag_update(CTX_wm_space_text(C), 0); | ||||
| ▲ Show 20 Lines • Show All 1,392 Lines • ▼ Show 20 Lines | |||||
| /* Moves the view vertically by the specified number of lines */ | /* Moves the view vertically by the specified number of lines */ | ||||
| static void txt_screen_skip(SpaceText *st, ARegion *ar, int lines) | static void txt_screen_skip(SpaceText *st, ARegion *ar, int lines) | ||||
| { | { | ||||
| st->top += lines; | st->top += lines; | ||||
| txt_screen_clamp(st, ar); | txt_screen_clamp(st, ar); | ||||
| } | } | ||||
| /* quick enum for tsc->zone (scroller handles) */ | /* quick enum for tsc->zone (scroller handles) */ | ||||
| enum { | enum eScrollZone { | ||||
| SCROLLHANDLE_INVALID_OUTSIDE = -1, | |||||
| SCROLLHANDLE_BAR, | SCROLLHANDLE_BAR, | ||||
| SCROLLHANDLE_MIN_OUTSIDE, | SCROLLHANDLE_MIN_OUTSIDE, | ||||
| SCROLLHANDLE_MAX_OUTSIDE, | SCROLLHANDLE_MAX_OUTSIDE, | ||||
| }; | }; | ||||
| typedef struct TextScroll { | typedef struct TextScroll { | ||||
| int old[2]; | int mval_prev[2]; | ||||
| int delta[2]; | int mval_delta[2]; | ||||
| int first; | bool is_first; | ||||
| int scrollbar; | bool is_scrollbar; | ||||
| int zone; | enum eScrollZone zone; | ||||
| /* Store the state of the display, cache some constant vars. */ | |||||
| struct { | |||||
| int ofs_init[2]; | |||||
| int ofs_max[2]; | |||||
| int size_px[2]; | |||||
| } state; | |||||
| int ofs_delta[2]; | |||||
| int ofs_delta_px[2]; | |||||
| } TextScroll; | } TextScroll; | ||||
| static void text_scroll_state_init(TextScroll *tsc, SpaceText *st, ARegion *ar) | |||||
| { | |||||
| tsc->state.ofs_init[0] = st->left; | |||||
| tsc->state.ofs_init[1] = st->top; | |||||
| tsc->state.ofs_max[0] = INT_MAX; | |||||
| tsc->state.ofs_max[1] = text_get_total_lines(st, ar) - (st->viewlines / 2); | |||||
| tsc->state.size_px[0] = st->cwidth; | |||||
| tsc->state.size_px[1] = TXT_LINE_HEIGHT(st); | |||||
| } | |||||
| static bool text_scroll_poll(bContext *C) | static bool text_scroll_poll(bContext *C) | ||||
| { | { | ||||
| /* it should be possible to still scroll linked texts to read them, | /* it should be possible to still scroll linked texts to read them, | ||||
| * even if they can't be edited... */ | * even if they can't be edited... */ | ||||
| return CTX_data_edit_text(C) != NULL; | return CTX_data_edit_text(C) != NULL; | ||||
| } | } | ||||
| static int text_scroll_exec(bContext *C, wmOperator *op) | static int text_scroll_exec(bContext *C, wmOperator *op) | ||||
| Show All 12 Lines | static int text_scroll_exec(bContext *C, wmOperator *op) | ||||
| ED_area_tag_redraw(CTX_wm_area(C)); | ED_area_tag_redraw(CTX_wm_area(C)); | ||||
| return OPERATOR_FINISHED; | return OPERATOR_FINISHED; | ||||
| } | } | ||||
| static void text_scroll_apply(bContext *C, wmOperator *op, const wmEvent *event) | static void text_scroll_apply(bContext *C, wmOperator *op, const wmEvent *event) | ||||
| { | { | ||||
| SpaceText *st = CTX_wm_space_text(C); | SpaceText *st = CTX_wm_space_text(C); | ||||
| ARegion *ar = CTX_wm_region(C); | |||||
| TextScroll *tsc = op->customdata; | TextScroll *tsc = op->customdata; | ||||
| int mval[2] = {event->x, event->y}; | int mval[2] = {event->x, event->y}; | ||||
| int scroll_steps[2] = {0, 0}; | |||||
| text_update_character_width(st); | text_update_character_width(st); | ||||
| /* compute mouse move distance */ | /* compute mouse move distance */ | ||||
| if (tsc->first) { | if (tsc->is_first) { | ||||
| tsc->old[0] = mval[0]; | tsc->mval_prev[0] = mval[0]; | ||||
| tsc->old[1] = mval[1]; | tsc->mval_prev[1] = mval[1]; | ||||
| tsc->first = 0; | tsc->is_first = false; | ||||
| } | } | ||||
| if (event->type != MOUSEPAN) { | if (event->type != MOUSEPAN) { | ||||
| tsc->delta[0] = mval[0] - tsc->old[0]; | tsc->mval_delta[0] = mval[0] - tsc->mval_prev[0]; | ||||
| tsc->delta[1] = mval[1] - tsc->old[1]; | tsc->mval_delta[1] = mval[1] - tsc->mval_prev[1]; | ||||
| } | } | ||||
| /* accumulate scroll, in float values for events that give less than one | /* accumulate scroll, in float values for events that give less than one | ||||
| * line offset but taken together should still scroll */ | * line offset but taken together should still scroll */ | ||||
| if (!tsc->scrollbar) { | if (!tsc->is_scrollbar) { | ||||
| st->scroll_accum[0] += -tsc->delta[0] / (float)st->cwidth; | tsc->ofs_delta_px[0] -= tsc->mval_delta[0]; | ||||
| st->scroll_accum[1] += tsc->delta[1] / (float)(TXT_LINE_HEIGHT(st)); | tsc->ofs_delta_px[1] += tsc->mval_delta[1]; | ||||
| } | } | ||||
| else { | else { | ||||
| st->scroll_accum[1] += -tsc->delta[1] * st->pix_per_line; | tsc->ofs_delta_px[1] -= (tsc->mval_delta[1] * st->pix_per_line) * tsc->state.size_px[1]; | ||||
| } | } | ||||
| /* round to number of lines to scroll */ | for (int i = 0; i < 2; i += 1) { | ||||
| scroll_steps[0] = (int)st->scroll_accum[0]; | int lines_from_pixels = tsc->ofs_delta_px[i] / tsc->state.size_px[i]; | ||||
| scroll_steps[1] = (int)st->scroll_accum[1]; | tsc->ofs_delta[i] += lines_from_pixels; | ||||
| tsc->ofs_delta_px[i] -= lines_from_pixels * tsc->state.size_px[i]; | |||||
| } | |||||
| st->scroll_accum[0] -= scroll_steps[0]; | /* The final values need to be calculated from the inputs, | ||||
| st->scroll_accum[1] -= scroll_steps[1]; | * so clamping and ensuring an unsigned pixel offset doesn't conflict with | ||||
| * updating the cursor mval_delta. */ | |||||
| int scroll_ofs_new[2] = { | |||||
| tsc->state.ofs_init[0] + tsc->ofs_delta[0], | |||||
| tsc->state.ofs_init[1] + tsc->ofs_delta[1], | |||||
| }; | |||||
| int scroll_ofs_px_new[2] = { | |||||
| tsc->ofs_delta_px[0], | |||||
| tsc->ofs_delta_px[1], | |||||
| }; | |||||
| /* perform vertical and/or horizontal scroll */ | for (int i = 0; i < 2; i += 1) { | ||||
| if (scroll_steps[0] || scroll_steps[1]) { | /* Ensure always unsigned (adjusting line/column accordingly). */ | ||||
| txt_screen_skip(st, ar, scroll_steps[1]); | while (scroll_ofs_px_new[i] < 0) { | ||||
| scroll_ofs_px_new[i] += tsc->state.size_px[i]; | |||||
| scroll_ofs_new[i] -= 1; | |||||
| } | |||||
| if (st->wordwrap) { | /* Clamp within usable region. */ | ||||
| st->left = 0; | if (scroll_ofs_new[i] < 0) { | ||||
| scroll_ofs_new[i] = 0; | |||||
| scroll_ofs_px_new[i] = 0; | |||||
| } | |||||
| else if (scroll_ofs_new[i] >= tsc->state.ofs_max[i]) { | |||||
| scroll_ofs_new[i] = tsc->state.ofs_max[i]; | |||||
| scroll_ofs_px_new[i] = 0; | |||||
| } | } | ||||
| else { | |||||
| st->left += scroll_steps[0]; | |||||
| if (st->left < 0) { | |||||
| st->left = 0; | |||||
| } | } | ||||
| /* Override for word-wrap. */ | |||||
| if (st->wordwrap) { | |||||
| scroll_ofs_new[0] = 0; | |||||
| scroll_ofs_px_new[0] = 0; | |||||
| } | } | ||||
| /* Apply to the screen. */ | |||||
| if (scroll_ofs_new[0] != st->left || scroll_ofs_new[1] != st->top || | |||||
| /* Horizontal sub-pixel offset currently isn't used. */ | |||||
| /* scroll_ofs_px_new[0] != st->scroll_ofs_px[0] || */ | |||||
| scroll_ofs_px_new[1] != st->scroll_ofs_px[1]) { | |||||
| st->left = scroll_ofs_new[0]; | |||||
| st->top = scroll_ofs_new[1]; | |||||
| st->scroll_ofs_px[0] = scroll_ofs_px_new[0]; | |||||
| st->scroll_ofs_px[1] = scroll_ofs_px_new[1]; | |||||
| ED_area_tag_redraw(CTX_wm_area(C)); | ED_area_tag_redraw(CTX_wm_area(C)); | ||||
| } | } | ||||
| tsc->old[0] = mval[0]; | tsc->mval_prev[0] = mval[0]; | ||||
| tsc->old[1] = mval[1]; | tsc->mval_prev[1] = mval[1]; | ||||
| } | } | ||||
| static void scroll_exit(bContext *C, wmOperator *op) | static void scroll_exit(bContext *C, wmOperator *op) | ||||
| { | { | ||||
| SpaceText *st = CTX_wm_space_text(C); | SpaceText *st = CTX_wm_space_text(C); | ||||
| TextScroll *tsc = op->customdata; | |||||
| st->flags &= ~ST_SCROLL_SELECT; | st->flags &= ~ST_SCROLL_SELECT; | ||||
| if (st->scroll_ofs_px[1] > tsc->state.size_px[1] / 2) { | |||||
| st->top += 1; | |||||
| } | |||||
| st->scroll_ofs_px[0] = 0; | |||||
| st->scroll_ofs_px[1] = 0; | |||||
| ED_area_tag_redraw(CTX_wm_area(C)); | |||||
| MEM_freeN(op->customdata); | MEM_freeN(op->customdata); | ||||
| } | } | ||||
| static int text_scroll_modal(bContext *C, wmOperator *op, const wmEvent *event) | static int text_scroll_modal(bContext *C, wmOperator *op, const wmEvent *event) | ||||
| { | { | ||||
| TextScroll *tsc = op->customdata; | TextScroll *tsc = op->customdata; | ||||
| SpaceText *st = CTX_wm_space_text(C); | SpaceText *st = CTX_wm_space_text(C); | ||||
| ARegion *ar = CTX_wm_region(C); | ARegion *ar = CTX_wm_region(C); | ||||
| Show All 25 Lines | |||||
| static void text_scroll_cancel(bContext *C, wmOperator *op) | static void text_scroll_cancel(bContext *C, wmOperator *op) | ||||
| { | { | ||||
| scroll_exit(C, op); | scroll_exit(C, op); | ||||
| } | } | ||||
| static int text_scroll_invoke(bContext *C, wmOperator *op, const wmEvent *event) | static int text_scroll_invoke(bContext *C, wmOperator *op, const wmEvent *event) | ||||
| { | { | ||||
| SpaceText *st = CTX_wm_space_text(C); | SpaceText *st = CTX_wm_space_text(C); | ||||
| ARegion *ar = CTX_wm_region(C); | |||||
| TextScroll *tsc; | TextScroll *tsc; | ||||
| if (RNA_struct_property_is_set(op->ptr, "lines")) { | if (RNA_struct_property_is_set(op->ptr, "lines")) { | ||||
| return text_scroll_exec(C, op); | return text_scroll_exec(C, op); | ||||
| } | } | ||||
| tsc = MEM_callocN(sizeof(TextScroll), "TextScroll"); | tsc = MEM_callocN(sizeof(TextScroll), "TextScroll"); | ||||
| tsc->first = 1; | tsc->is_first = true; | ||||
| tsc->zone = SCROLLHANDLE_BAR; | tsc->zone = SCROLLHANDLE_BAR; | ||||
| text_scroll_state_init(tsc, st, ar); | |||||
| op->customdata = tsc; | op->customdata = tsc; | ||||
| st->flags |= ST_SCROLL_SELECT; | st->flags |= ST_SCROLL_SELECT; | ||||
| if (event->type == MOUSEPAN) { | if (event->type == MOUSEPAN) { | ||||
| text_update_character_width(st); | text_update_character_width(st); | ||||
| tsc->old[0] = event->x; | tsc->mval_prev[0] = event->x; | ||||
| tsc->old[1] = event->y; | tsc->mval_prev[1] = event->y; | ||||
| /* Sensitivity of scroll set to 4pix per line/char */ | /* Sensitivity of scroll set to 4pix per line/char */ | ||||
| tsc->delta[0] = (event->x - event->prevx) * st->cwidth / 4; | tsc->mval_delta[0] = (event->x - event->prevx) * st->cwidth / 4; | ||||
| tsc->delta[1] = (event->y - event->prevy) * st->lheight_dpi / 4; | tsc->mval_delta[1] = (event->y - event->prevy) * st->lheight_dpi / 4; | ||||
| tsc->first = 0; | tsc->is_first = false; | ||||
| tsc->scrollbar = 0; | tsc->is_scrollbar = false; | ||||
| text_scroll_apply(C, op, event); | text_scroll_apply(C, op, event); | ||||
| scroll_exit(C, op); | scroll_exit(C, op); | ||||
| return OPERATOR_FINISHED; | return OPERATOR_FINISHED; | ||||
| } | } | ||||
| WM_event_add_modal_handler(C, op); | WM_event_add_modal_handler(C, op); | ||||
| return OPERATOR_RUNNING_MODAL; | return OPERATOR_RUNNING_MODAL; | ||||
| ▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| static int text_scroll_bar_invoke(bContext *C, wmOperator *op, const wmEvent *event) | static int text_scroll_bar_invoke(bContext *C, wmOperator *op, const wmEvent *event) | ||||
| { | { | ||||
| SpaceText *st = CTX_wm_space_text(C); | SpaceText *st = CTX_wm_space_text(C); | ||||
| ARegion *ar = CTX_wm_region(C); | ARegion *ar = CTX_wm_region(C); | ||||
| TextScroll *tsc; | TextScroll *tsc; | ||||
| const int *mval = event->mval; | const int *mval = event->mval; | ||||
| int zone = -1; | enum eScrollZone zone = SCROLLHANDLE_INVALID_OUTSIDE; | ||||
| if (RNA_struct_property_is_set(op->ptr, "lines")) { | if (RNA_struct_property_is_set(op->ptr, "lines")) { | ||||
| return text_scroll_exec(C, op); | return text_scroll_exec(C, op); | ||||
| } | } | ||||
| /* verify we are in the right zone */ | /* verify we are in the right zone */ | ||||
| if (mval[0] > st->txtbar.xmin && mval[0] < st->txtbar.xmax) { | if (mval[0] > st->txtbar.xmin && mval[0] < st->txtbar.xmax) { | ||||
| if (mval[1] >= st->txtbar.ymin && mval[1] <= st->txtbar.ymax) { | if (mval[1] >= st->txtbar.ymin && mval[1] <= st->txtbar.ymax) { | ||||
| /* mouse inside scroll handle */ | /* mouse inside scroll handle */ | ||||
| zone = SCROLLHANDLE_BAR; | zone = SCROLLHANDLE_BAR; | ||||
| } | } | ||||
| else if (mval[1] > TXT_SCROLL_SPACE && mval[1] < ar->winy - TXT_SCROLL_SPACE) { | else if (mval[1] > TXT_SCROLL_SPACE && mval[1] < ar->winy - TXT_SCROLL_SPACE) { | ||||
| if (mval[1] < st->txtbar.ymin) { | if (mval[1] < st->txtbar.ymin) { | ||||
| zone = SCROLLHANDLE_MIN_OUTSIDE; | zone = SCROLLHANDLE_MIN_OUTSIDE; | ||||
| } | } | ||||
| else { | else { | ||||
| zone = SCROLLHANDLE_MAX_OUTSIDE; | zone = SCROLLHANDLE_MAX_OUTSIDE; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| if (zone == -1) { | if (zone == SCROLLHANDLE_INVALID_OUTSIDE) { | ||||
| /* we are outside slider - nothing to do */ | /* we are outside slider - nothing to do */ | ||||
| return OPERATOR_PASS_THROUGH; | return OPERATOR_PASS_THROUGH; | ||||
| } | } | ||||
| tsc = MEM_callocN(sizeof(TextScroll), "TextScroll"); | tsc = MEM_callocN(sizeof(TextScroll), "TextScroll"); | ||||
| tsc->first = 1; | tsc->is_first = true; | ||||
| tsc->scrollbar = 1; | tsc->is_scrollbar = true; | ||||
| tsc->zone = zone; | tsc->zone = zone; | ||||
| op->customdata = tsc; | op->customdata = tsc; | ||||
| st->flags |= ST_SCROLL_SELECT; | st->flags |= ST_SCROLL_SELECT; | ||||
| text_scroll_state_init(tsc, st, ar); | |||||
| /* jump scroll, works in v2d but needs to be added here too :S */ | /* jump scroll, works in v2d but needs to be added here too :S */ | ||||
| if (event->type == MIDDLEMOUSE) { | if (event->type == MIDDLEMOUSE) { | ||||
| tsc->old[0] = ar->winrct.xmin + BLI_rcti_cent_x(&st->txtbar); | tsc->mval_prev[0] = ar->winrct.xmin + BLI_rcti_cent_x(&st->txtbar); | ||||
| tsc->old[1] = ar->winrct.ymin + BLI_rcti_cent_y(&st->txtbar); | tsc->mval_prev[1] = ar->winrct.ymin + BLI_rcti_cent_y(&st->txtbar); | ||||
| tsc->first = 0; | tsc->is_first = false; | ||||
| tsc->zone = SCROLLHANDLE_BAR; | tsc->zone = SCROLLHANDLE_BAR; | ||||
| text_scroll_apply(C, op, event); | text_scroll_apply(C, op, event); | ||||
| } | } | ||||
| WM_event_add_modal_handler(C, op); | WM_event_add_modal_handler(C, op); | ||||
| return OPERATOR_RUNNING_MODAL; | return OPERATOR_RUNNING_MODAL; | ||||
| } | } | ||||
| Show All 16 Lines | void TEXT_OT_scroll_bar(wmOperatorType *ot) | ||||
| /* flags */ | /* flags */ | ||||
| ot->flag = OPTYPE_BLOCKING | OPTYPE_INTERNAL; | ot->flag = OPTYPE_BLOCKING | OPTYPE_INTERNAL; | ||||
| /* properties */ | /* properties */ | ||||
| RNA_def_int( | RNA_def_int( | ||||
| ot->srna, "lines", 1, INT_MIN, INT_MAX, "Lines", "Number of lines to scroll", -100, 100); | ot->srna, "lines", 1, INT_MIN, INT_MAX, "Lines", "Number of lines to scroll", -100, 100); | ||||
| } | } | ||||
| /******************* set selection operator **********************/ | /** \} */ | ||||
| /* -------------------------------------------------------------------- */ | |||||
| /** \name Set Selection Operator | |||||
| * \{ */ | |||||
| typedef struct SetSelection { | typedef struct SetSelection { | ||||
| int selecting; | int selecting; | ||||
| int selc, sell; | int selc, sell; | ||||
| short old[2]; | short mval_prev[2]; | ||||
| wmTimer *timer; /* needed for scrolling when mouse at region bounds */ | wmTimer *timer; /* needed for scrolling when mouse at region bounds */ | ||||
| } SetSelection; | } SetSelection; | ||||
| static int flatten_width(SpaceText *st, const char *str) | static int flatten_width(SpaceText *st, const char *str) | ||||
| { | { | ||||
| int i, total = 0; | int i, total = 0; | ||||
| for (i = 0; str[i]; i += BLI_str_utf8_size_safe(str + i)) { | for (i = 0; str[i]; i += BLI_str_utf8_size_safe(str + i)) { | ||||
| ▲ Show 20 Lines • Show All 285 Lines • ▼ Show 20 Lines | static void text_cursor_set_apply(bContext *C, wmOperator *op, const wmEvent *event) | ||||
| else { | else { | ||||
| text_cursor_timer_remove(C, ssel); | text_cursor_timer_remove(C, ssel); | ||||
| if (event->type != TIMER) { | if (event->type != TIMER) { | ||||
| text_cursor_set_to_pos(st, ar, event->mval[0], event->mval[1], 1); | text_cursor_set_to_pos(st, ar, event->mval[0], event->mval[1], 1); | ||||
| text_scroll_to_cursor(st, ar, false); | text_scroll_to_cursor(st, ar, false); | ||||
| WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); | WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); | ||||
| ssel->old[0] = event->mval[0]; | ssel->mval_prev[0] = event->mval[0]; | ||||
| ssel->old[1] = event->mval[1]; | ssel->mval_prev[1] = event->mval[1]; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| static void text_cursor_set_exit(bContext *C, wmOperator *op) | static void text_cursor_set_exit(bContext *C, wmOperator *op) | ||||
| { | { | ||||
| SpaceText *st = CTX_wm_space_text(C); | SpaceText *st = CTX_wm_space_text(C); | ||||
| Text *text = st->text; | Text *text = st->text; | ||||
| SetSelection *ssel = op->customdata; | SetSelection *ssel = op->customdata; | ||||
| char *buffer; | char *buffer; | ||||
| if (txt_has_sel(text)) { | if (txt_has_sel(text)) { | ||||
| buffer = txt_sel_to_buf(text, NULL); | buffer = txt_sel_to_buf(text, NULL); | ||||
| WM_clipboard_text_set(buffer, 1); | WM_clipboard_text_set(buffer, 1); | ||||
| MEM_freeN(buffer); | MEM_freeN(buffer); | ||||
| } | } | ||||
| text_update_cursor_moved(C); | text_update_cursor_moved(C); | ||||
| WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); | WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, st->text); | ||||
| text_cursor_timer_remove(C, ssel); | text_cursor_timer_remove(C, ssel); | ||||
| MEM_freeN(ssel); | MEM_freeN(ssel); | ||||
| } | } | ||||
| static int text_set_selection_invoke(bContext *C, wmOperator *op, const wmEvent *event) | static int text_selection_set_invoke(bContext *C, wmOperator *op, const wmEvent *event) | ||||
| { | { | ||||
| SpaceText *st = CTX_wm_space_text(C); | SpaceText *st = CTX_wm_space_text(C); | ||||
| SetSelection *ssel; | SetSelection *ssel; | ||||
| if (event->mval[0] >= st->txtbar.xmin) { | if (event->mval[0] >= st->txtbar.xmin) { | ||||
| return OPERATOR_PASS_THROUGH; | return OPERATOR_PASS_THROUGH; | ||||
| } | } | ||||
| op->customdata = MEM_callocN(sizeof(SetSelection), "SetCursor"); | op->customdata = MEM_callocN(sizeof(SetSelection), "SetCursor"); | ||||
| ssel = op->customdata; | ssel = op->customdata; | ||||
| ssel->selecting = RNA_boolean_get(op->ptr, "select"); | |||||
| ssel->old[0] = event->mval[0]; | ssel->mval_prev[0] = event->mval[0]; | ||||
| ssel->old[1] = event->mval[1]; | ssel->mval_prev[1] = event->mval[1]; | ||||
| ssel->sell = txt_get_span(st->text->lines.first, st->text->sell); | ssel->sell = txt_get_span(st->text->lines.first, st->text->sell); | ||||
| ssel->selc = st->text->selc; | ssel->selc = st->text->selc; | ||||
| WM_event_add_modal_handler(C, op); | WM_event_add_modal_handler(C, op); | ||||
| text_cursor_set_apply(C, op, event); | text_cursor_set_apply(C, op, event); | ||||
| return OPERATOR_RUNNING_MODAL; | return OPERATOR_RUNNING_MODAL; | ||||
| } | } | ||||
| static int text_set_selection_modal(bContext *C, wmOperator *op, const wmEvent *event) | static int text_selection_set_modal(bContext *C, wmOperator *op, const wmEvent *event) | ||||
| { | { | ||||
| switch (event->type) { | switch (event->type) { | ||||
| case LEFTMOUSE: | case LEFTMOUSE: | ||||
| case MIDDLEMOUSE: | case MIDDLEMOUSE: | ||||
| case RIGHTMOUSE: | case RIGHTMOUSE: | ||||
| text_cursor_set_exit(C, op); | text_cursor_set_exit(C, op); | ||||
| return OPERATOR_FINISHED; | return OPERATOR_FINISHED; | ||||
| case TIMER: | case TIMER: | ||||
| case MOUSEMOVE: | case MOUSEMOVE: | ||||
| text_cursor_set_apply(C, op, event); | text_cursor_set_apply(C, op, event); | ||||
| break; | break; | ||||
| } | } | ||||
| return OPERATOR_RUNNING_MODAL; | return OPERATOR_RUNNING_MODAL; | ||||
| } | } | ||||
| static void text_set_selection_cancel(bContext *C, wmOperator *op) | static void text_selection_set_cancel(bContext *C, wmOperator *op) | ||||
| { | { | ||||
| text_cursor_set_exit(C, op); | text_cursor_set_exit(C, op); | ||||
| } | } | ||||
| void TEXT_OT_selection_set(wmOperatorType *ot) | void TEXT_OT_selection_set(wmOperatorType *ot) | ||||
| { | { | ||||
| /* identifiers */ | /* identifiers */ | ||||
| ot->name = "Set Selection"; | ot->name = "Set Selection"; | ||||
| ot->idname = "TEXT_OT_selection_set"; | ot->idname = "TEXT_OT_selection_set"; | ||||
| ot->description = "Set cursor selection"; | ot->description = "Set cursor selection"; | ||||
| /* api callbacks */ | /* api callbacks */ | ||||
| ot->invoke = text_set_selection_invoke; | ot->invoke = text_selection_set_invoke; | ||||
| ot->modal = text_set_selection_modal; | ot->modal = text_selection_set_modal; | ||||
| ot->cancel = text_set_selection_cancel; | ot->cancel = text_selection_set_cancel; | ||||
| ot->poll = text_region_edit_poll; | ot->poll = text_region_edit_poll; | ||||
| /* properties */ | |||||
| RNA_def_boolean(ot->srna, "select", 0, "Select", "Set selection end rather than cursor"); | |||||
| } | } | ||||
| /** \} */ | /** \} */ | ||||
| /* -------------------------------------------------------------------- */ | /* -------------------------------------------------------------------- */ | ||||
| /** \name Set Cursor Operator | /** \name Set Cursor Operator | ||||
| * \{ */ | * \{ */ | ||||
| ▲ Show 20 Lines • Show All 563 Lines • Show Last 20 Lines | |||||