Welcome to mirror list, hosted at ThFree Co, Russian Federation.

interface_template_search_operator.cc « interface « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58f44de5278a04f64c6fba26901d02c0e8bf8eca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup edinterface
 *
 * Search available operators by scanning all and checking their poll function.
 * accessed via the #WM_OT_search_operator operator.
 */

#include <cstring>

#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "DNA_texture_types.h"

#include "BLI_array.hh"
#include "BLI_ghash.h"
#include "BLI_math_vec_types.hh"
#include "BLI_string.h"
#include "BLI_utildefines.h"

#include "BLT_translation.h"

#include "BKE_context.h"
#include "BKE_global.h"

#include "WM_api.h"
#include "WM_types.h"

#include "UI_interface.h"
#include "interface_intern.h"

/* -------------------------------------------------------------------- */
/** \name Operator Search Template Implementation
 * \{ */

static void operator_search_exec_fn(bContext *C, void * /*arg1*/, void *arg2)
{
  wmOperatorType *ot = static_cast<wmOperatorType *>(arg2);

  if (ot) {
    WM_operator_name_call_ptr(C, ot, WM_OP_INVOKE_DEFAULT, nullptr, nullptr);
  }
}

static void operator_search_update_fn(const bContext *C,
                                      void * /*arg*/,
                                      const char *str,
                                      uiSearchItems *items,
                                      const bool /*is_first*/)
{
  GHashIterator iter;

  /* Prepare BLI_string_all_words_matched. */
  const size_t str_len = strlen(str);
  const int words_max = BLI_string_max_possible_word_count(str_len);
  blender::Array<blender::int2> words(words_max);
  const int words_len = BLI_string_find_split_words(
      str, str_len, ' ', (int(*)[2])words.data(), words_max);

  for (WM_operatortype_iter(&iter); !BLI_ghashIterator_done(&iter);
       BLI_ghashIterator_step(&iter)) {
    wmOperatorType *ot = static_cast<wmOperatorType *>(BLI_ghashIterator_getValue(&iter));
    const char *ot_ui_name = CTX_IFACE_(ot->translation_context, ot->name);

    if ((ot->flag & OPTYPE_INTERNAL) && (G.debug & G_DEBUG_WM) == 0) {
      continue;
    }

    if (BLI_string_all_words_matched(ot_ui_name, str, (int(*)[2])words.data(), words_len)) {
      if (WM_operator_poll((bContext *)C, ot)) {
        char name[256];
        const int len = strlen(ot_ui_name);

        /* display name for menu, can hold hotkey */
        BLI_strncpy(name, ot_ui_name, sizeof(name));

        /* check for hotkey */
        if (len < sizeof(name) - 6) {
          if (WM_key_event_operator_string(C,
                                           ot->idname,
                                           WM_OP_EXEC_DEFAULT,
                                           nullptr,
                                           true,
                                           &name[len + 1],
                                           sizeof(name) - len - 1)) {
            name[len] = UI_SEP_CHAR;
          }
        }

        if (!UI_search_item_add(items, name, ot, ICON_NONE, 0, 0)) {
          break;
        }
      }
    }
  }
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Operator Search Template API
 * \{ */

void UI_but_func_operator_search(uiBut *but)
{
  UI_but_func_search_set(but,
                         ui_searchbox_create_operator,
                         operator_search_update_fn,
                         nullptr,
                         false,
                         nullptr,
                         operator_search_exec_fn,
                         nullptr);
}

void uiTemplateOperatorSearch(uiLayout *layout)
{
  uiBlock *block;
  uiBut *but;
  static char search[256] = "";

  block = uiLayoutGetBlock(layout);
  UI_block_layout_set_current(block, layout);

  but = uiDefSearchBut(
      block, search, 0, ICON_VIEWZOOM, sizeof(search), 0, 0, UI_UNIT_X * 6, UI_UNIT_Y, 0, 0, "");
  UI_but_func_operator_search(but);
}

/** \} */