2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): 2007 Blender Foundation (refactor)
23 * ***** END GPL LICENSE BLOCK *****
26 * Subwindow opengl handling.
27 * BTW: subwindows open/close in X11 are way too slow, tried it, and choose for my own system... (ton)
31 /** \file blender/windowmanager/intern/wm_subwindow.c
38 #include "MEM_guardedalloc.h"
40 #include "DNA_windowmanager_types.h"
41 #include "DNA_screen_types.h"
43 #include "BLI_blenlib.h"
45 #include "BLI_utildefines.h"
48 #include "BKE_context.h"
49 #include "BKE_global.h"
53 #include "GPU_extensions.h"
56 #include "wm_subwindow.h"
57 #include "wm_window.h"
59 /* wmSubWindow stored in wmWindow... but not exposed outside this C file */
60 /* it seems a bit redundant (area regions can store it too, but we keep it
61 * because we can store all kind of future opengl fanciness here */
63 /* we use indices and array because:
64 * - index has safety, no pointers from this C file hanging around
65 * - fast lookups of indices with array, list would give overhead
66 * - old code used it this way...
67 * - keep option open to have 2 screens using same window
70 typedef struct wmSubWindow {
71 struct wmSubWindow *next, *prev;
78 /* ******************* open, free, set, get data ******************** */
80 /* not subwindow itself */
81 static void wm_subwindow_free(wmSubWindow *UNUSED(swin))
83 /* future fancy stuff */
86 void wm_subwindows_free(wmWindow *win)
90 for (swin = win->subwindows.first; swin; swin = swin->next)
91 wm_subwindow_free(swin);
93 BLI_freelistN(&win->subwindows);
97 int wm_subwindow_get(wmWindow *win)
100 return win->curswin->swinid;
104 static wmSubWindow *swin_from_swinid(wmWindow *win, int swinid)
108 for (swin = win->subwindows.first; swin; swin = swin->next)
109 if (swin->swinid == swinid)
114 void wm_subwindow_getsize(wmWindow *win, int swinid, int *x, int *y)
116 wmSubWindow *swin = swin_from_swinid(win, swinid);
119 *x = swin->winrct.xmax - swin->winrct.xmin + 1;
120 *y = swin->winrct.ymax - swin->winrct.ymin + 1;
124 void wm_subwindow_getorigin(wmWindow *win, int swinid, int *x, int *y)
126 wmSubWindow *swin = swin_from_swinid(win, swinid);
129 *x = swin->winrct.xmin;
130 *y = swin->winrct.ymin;
134 void wm_subwindow_getmatrix(wmWindow *win, int swinid, float mat[][4])
136 wmSubWindow *swin = swin_from_swinid(win, swinid);
139 /* used by UI, should find a better way to get the matrix there */
140 if (swinid == win->screen->mainwin) {
143 wm_subwindow_getsize(win, swin->swinid, &width, &height);
144 orthographic_m4(mat, -0.375f, (float)width - 0.375f, -0.375f, (float)height - 0.375f, -100, 100);
147 glGetFloatv(GL_PROJECTION_MATRIX, (float *)mat);
151 /* always sets pixel-precise 2D window/view matrices */
152 /* coords is in whole pixels. xmin = 15, xmax= 16: means window is 2 pix big */
153 int wm_subwindow_open(wmWindow *win, rcti *winrct)
159 for (swin = win->subwindows.first; swin; swin = swin->next)
160 if (freewinid <= swin->swinid)
161 freewinid = swin->swinid + 1;
163 win->curswin = swin = MEM_callocN(sizeof(wmSubWindow), "swinopen");
164 BLI_addtail(&win->subwindows, swin);
166 if (G.debug & G_DEBUG_EVENTS) {
167 printf("%s: swin %d added\n", __func__, freewinid);
170 swin->swinid = freewinid;
171 swin->winrct = *winrct;
173 /* and we appy it all right away */
174 wmSubWindowSet(win, swin->swinid);
177 wm_subwindow_getsize(win, swin->swinid, &width, &height);
178 wmOrtho2(-0.375f, (float)width - 0.375f, -0.375f, (float)height - 0.375f);
185 void wm_subwindow_close(wmWindow *win, int swinid)
187 wmSubWindow *swin = swin_from_swinid(win, swinid);
190 if (swin == win->curswin)
192 wm_subwindow_free(swin);
193 BLI_remlink(&win->subwindows, swin);
197 printf("wm_subwindow_close: Internal error, bad winid: %d\n", swinid);
202 /* pixels go from 0-99 for a 100 pixel window */
203 void wm_subwindow_position(wmWindow *win, int swinid, rcti *winrct)
205 wmSubWindow *swin = swin_from_swinid(win, swinid);
210 swin->winrct = *winrct;
212 /* CRITICAL, this clamping ensures that
213 * the viewport never goes outside the screen
214 * edges (assuming the x, y coords aren't
215 * outside). This caused a hardware lock
216 * on Matrox cards if it happens.
218 * Really Blender should never _ever_ try
219 * to do such a thing, but just to be safe
220 * clamp it anyway (or fix the bScreen
221 * scaling routine, and be damn sure you
222 * fixed it). - zr (2001!)
225 if (swin->winrct.xmax > win->sizex)
226 swin->winrct.xmax = win->sizex;
227 if (swin->winrct.ymax > win->sizey)
228 swin->winrct.ymax = win->sizey;
231 wmSubWindowSet(win, swinid);
232 wm_subwindow_getsize(win, swinid, &width, &height);
233 wmOrtho2(-0.375f, (float)width - 0.375f, -0.375f, (float)height - 0.375f);
236 printf("wm_subwindow_position: Internal error, bad winid: %d\n", swinid);
240 /* ---------------- WM versions of OpenGL style API calls ------------------------ */
241 /* ----------------- exported in WM_api.h ------------------------------------------------------ */
243 /* internal state, no threaded opengl! XXX */
244 static wmWindow *_curwindow = NULL;
245 static wmSubWindow *_curswin = NULL;
247 void wmSubWindowScissorSet(wmWindow *win, int swinid, rcti *srct)
250 _curswin = swin_from_swinid(win, swinid);
252 if (_curswin == NULL) {
253 printf("wmSubWindowSet %d: doesn't exist\n", swinid);
257 win->curswin = _curswin;
260 width = _curswin->winrct.xmax - _curswin->winrct.xmin + 1;
261 height = _curswin->winrct.ymax - _curswin->winrct.ymin + 1;
262 glViewport(_curswin->winrct.xmin, _curswin->winrct.ymin, width, height);
265 width = srct->xmax - srct->xmin + 1;
266 height = srct->ymax - srct->ymin + 1;
267 glScissor(srct->xmin, srct->ymin, width, height);
270 glScissor(_curswin->winrct.xmin, _curswin->winrct.ymin, width, height);
272 wmOrtho2(-0.375f, (float)width - 0.375f, -0.375f, (float)height - 0.375f);
279 /* enable the WM versions of opengl calls */
280 void wmSubWindowSet(wmWindow *win, int swinid)
282 wmSubWindowScissorSet(win, swinid, NULL);
285 void wmFrustum(float x1, float x2, float y1, float y2, float n, float f)
287 glMatrixMode(GL_PROJECTION);
289 glFrustum(x1, x2, y1, y2, n, f);
290 glMatrixMode(GL_MODELVIEW);
293 void wmOrtho(float x1, float x2, float y1, float y2, float n, float f)
295 glMatrixMode(GL_PROJECTION);
298 glOrtho(x1, x2, y1, y2, n, f);
300 glMatrixMode(GL_MODELVIEW);
303 void wmOrtho2(float x1, float x2, float y1, float y2)
305 /* prevent opengl from generating errors */
306 if (x1 == x2) x2 += 1.0f;
307 if (y1 == y2) y2 += 1.0f;
309 wmOrtho(x1, x2, y1, y2, -100, 100);
312 /* *************************** Framebuffer color depth, for selection codes ********************** */
316 /* apple seems to round colors to below and up on some configs */
318 unsigned int index_to_framebuffer(int index)
320 unsigned int i = index;
322 switch (GPU_color_depth()) {
324 i = ((i & 0xF00) << 12) + ((i & 0xF0) << 8) + ((i & 0xF) << 4);
325 /* sometimes dithering subtracts! */
330 i = ((i & 0x7C00) << 9) + ((i & 0x3E0) << 6) + ((i & 0x1F) << 3);
335 default: // 18 bits...
336 i = ((i & 0x3F000) << 6) + ((i & 0xFC0) << 4) + ((i & 0x3F) << 2);
346 /* this is the old method as being in use for ages.... seems to work? colors are rounded to lower values */
348 unsigned int index_to_framebuffer(int index)
350 unsigned int i = index;
352 switch (GPU_color_depth()) {
354 i = ((i & 48) << 18) + ((i & 12) << 12) + ((i & 3) << 6);
358 i = ((i & 0xF00) << 12) + ((i & 0xF0) << 8) + ((i & 0xF) << 4);
359 /* sometimes dithering subtracts! */
364 i = ((i & 0x7C00) << 9) + ((i & 0x3E0) << 6) + ((i & 0x1F) << 3);
369 default: // 18 bits...
370 i = ((i & 0x3F000) << 6) + ((i & 0xFC0) << 4) + ((i & 0x3F) << 2);
380 void WM_set_framebuffer_index_color(int index)
382 const int col = index_to_framebuffer(index);
386 int WM_framebuffer_to_index(unsigned int col)
388 if (col == 0) return 0;
390 switch (GPU_color_depth()) {
392 return ((col & 0xC00000) >> 18) + ((col & 0xC000) >> 12) + ((col & 0xC0) >> 6);
394 return ((col & 0xF00000) >> 12) + ((col & 0xF000) >> 8) + ((col & 0xF0) >> 4);
397 return ((col & 0xF80000) >> 9) + ((col & 0xF800) >> 6) + ((col & 0xF8) >> 3);
399 return col & 0xFFFFFF;
400 default: // 18 bits...
401 return ((col & 0xFC0000) >> 6) + ((col & 0xFC00) >> 4) + ((col & 0xFC) >> 2);
406 /* ********** END MY WINDOW ************** */