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

interface_region_popover.c « interface « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bf75af524b7102473279d59ff6bc2a8c6de10c46 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2008 Blender Foundation.
 * All rights reserved.
 *
 * Contributor(s): Blender Foundation
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/interface/interface_region_popover.c
 *  \ingroup edinterface
 *
 * Pop-Over Region
 *
 * \note This is very close to 'interface_region_menu_popup.c'
 *
 * We could even merge them, however menu logic is already over-loaded.
 * PopOver's have the following differences.
 *
 * - UI is not constrained to a list.
 * - Pressing a button won't close the pop-over.
 * - Different draw style (to show this is has different behavior from a menu).
 * - #PanelType are used instead of #MenuType.
 * - No menu flipping support.
 * - No moving the menu to fit the mouse cursor.
 * - No key accelerators to access menu items
 *   (if we add support they would work differently).
 * - No arrow key navigation.
 * - No menu memory.
 * - No title.
 */

#include "MEM_guardedalloc.h"

#include "DNA_userdef_types.h"

#include "BLI_listbase.h"

#include "BLI_rect.h"
#include "BLI_utildefines.h"

#include "BKE_context.h"

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


#include "UI_interface.h"

#include "interface_intern.h"
#include "interface_regions_intern.h"

/* -------------------------------------------------------------------- */
/** \name Popup Menu with Callback or String
 * \{ */

struct uiPopover {
	uiBlock *block;
	uiLayout *layout;
	uiBut *but;
	ARegion *butregion;

	int mx, my;
	bool popover, slideout;

	uiMenuCreateFunc menu_func;
	void *menu_arg;
};

static uiBlock *ui_block_func_POPOVER(bContext *C, uiPopupBlockHandle *handle, void *arg_pup)
{
	uiBlock *block;
	uiPopover *pup = arg_pup;
	int minwidth, width, height;

	if (pup->menu_func) {
		pup->block->handle = handle;
		pup->menu_func(C, pup->layout, pup->menu_arg);
		pup->block->handle = NULL;
	}

	if (pup->but) {
		/* minimum width to enforece */
		minwidth = BLI_rctf_size_x(&pup->but->rect);
	}
	else {
		minwidth = UI_MENU_WIDTH_MIN;
	}

	block = pup->block;

	/* in some cases we create the block before the region,
	 * so we set it delayed here if necessary */
	if (BLI_findindex(&handle->region->uiblocks, block) == -1)
		UI_block_region_set(block, handle->region);

	UI_block_layout_resolve(block, &width, &height);

	UI_block_flag_enable(block, UI_BLOCK_MOVEMOUSE_QUIT | UI_BLOCK_KEEP_OPEN | UI_BLOCK_POPOVER);

	UI_block_direction_set(block, UI_DIR_DOWN | UI_DIR_CENTER_X);

	const int block_margin = U.widget_unit / 2;

	if (pup->popover) {
		int offset[2] = {0, 0};  /* Dummy. */
		UI_block_flag_enable(block, UI_BLOCK_LOOP);
		UI_block_direction_set(block, block->direction);
		block->minbounds = minwidth;
		UI_block_bounds_set_popup(block, block_margin, offset[0], offset[1]);
	}
	else {
		/* for a header menu we set the direction automatic */
		block->minbounds = minwidth;
		UI_block_bounds_set_normal(block, block_margin);
	}

	/* if menu slides out of other menu, override direction */
	if (pup->slideout)
		UI_block_direction_set(block, UI_DIR_RIGHT);

	return pup->block;
}

uiPopupBlockHandle *ui_popover_panel_create(
        bContext *C, ARegion *butregion, uiBut *but,
        uiMenuCreateFunc menu_func, void *arg)
{
	wmWindow *window = CTX_wm_window(C);
	uiStyle *style = UI_style_get_dpi();
	uiPopupBlockHandle *handle;
	uiPopover *pup;

	pup = MEM_callocN(sizeof(uiPopover), __func__);
	pup->block = UI_block_begin(C, NULL, __func__, UI_EMBOSS);
	UI_block_emboss_set(pup->block, UI_EMBOSS);
	pup->layout = UI_block_layout(
	        pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0,
	        U.widget_unit * UI_POPOVER_WIDTH_UNITS, 0, MENU_PADDING, style);
	pup->slideout = false; // but ? ui_block_is_menu(but->block) : false;
	pup->but = but;
	uiLayoutSetOperatorContext(pup->layout, WM_OP_INVOKE_REGION_WIN);

	if (!but) {
		/* no button to start from, means we are a popover */
		pup->mx = window->eventstate->x;
		pup->my = window->eventstate->y;
		pup->popover = true;
		pup->block->flag |= UI_BLOCK_NO_FLIP;
	}
	/* some enums reversing is strange, currently we have no good way to
	 * reverse some enum's but not others, so reverse all so the first menu
	 * items are always close to the mouse cursor */
	else {
		if (but->context) {
			uiLayoutContextCopy(pup->layout, but->context);
		}
	}

	/* menu is created from a callback */
	pup->menu_func = menu_func;
	pup->menu_arg = arg;

	handle = ui_popup_block_create(C, butregion, but, NULL, ui_block_func_POPOVER, pup);

	if (!but) {
		handle->popup = true;

		UI_popup_handlers_add(C, &window->modalhandlers, handle, 0);
		WM_event_add_mousemove(C);
	}

	handle->can_refresh = false;
	MEM_freeN(pup);

	return handle;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Popup Menu API with begin & end
 * \{ */

/**
 * Only return handler, and set optional title.
 * \param block_name: Assigned to uiBlock.name (useful info for debugging).
 */
uiPopover *UI_popover_begin_ex(bContext *C, const char *block_name)
{
	uiStyle *style = UI_style_get_dpi();
	uiPopover *pup = MEM_callocN(sizeof(uiPopover), "popover menu");

	pup->block = UI_block_begin(C, NULL, block_name, UI_EMBOSS);
	pup->layout = UI_block_layout(
	        pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, 0, 0,
	        U.widget_unit * UI_POPOVER_WIDTH_UNITS, 0, MENU_PADDING, style);

	/* Copied from menus, change if needed. */
	uiLayoutSetOperatorContext(pup->layout, WM_OP_EXEC_REGION_WIN);

	/* create in advance so we can let buttons point to retval already */
	pup->block->handle = MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");

	return pup;
}

uiPopover *UI_popover_begin(bContext *C)
{
	return UI_popover_begin_ex(C, __func__);
}

/**
 * Setting the button makes the popover open from the button instead of the cursor.
 */
#if 0
void UI_popover_panel_but_set(uiPopover *pup, struct ARegion *butregion, uiBut *but)
{
	pup->but = but;
	pup->butregion = butregion;
}
#endif

/* set the whole structure to work */
void UI_popover_end(bContext *C, uiPopover *pup)
{
	wmWindow *window = CTX_wm_window(C);
	uiPopupBlockHandle *menu;
	uiBut *but = NULL;
	ARegion *butregion = NULL;

	pup->popover = true;
	pup->mx = window->eventstate->x;
	pup->my = window->eventstate->y;

	if (pup->but) {
		but = pup->but;
		butregion = pup->butregion;
	}

	menu = ui_popup_block_create(C, butregion, but, NULL, ui_block_func_POPOVER, pup);
	menu->popup = true;

	UI_popup_handlers_add(C, &window->modalhandlers, menu, 0);
	WM_event_add_mousemove(C);

	menu->can_refresh = false;
	MEM_freeN(pup);
}

uiLayout *UI_popover_layout(uiPopover *pup)
{
	return pup->layout;
}

/** \} */

/* We may want to support this in future */
/* Similar to UI_popup_menu_invoke */
// int UI_popover_panel_invoke(bContext *C, const char *idname, ReportList *reports);