From 1a3d47d06daa2004a4df629fdcf7676ca1a4e27a Mon Sep 17 00:00:00 2001
From: Camilla Berglund
Date: Fri, 30 Nov 2012 13:56:42 +0100
Subject: [PATCH] Added window position callback.
---
include/GL/glfw3.h | 18 ++++++++++++++++++
readme.html | 3 ++-
src/internal.h | 1 +
src/window.c | 24 ++++++++++++++++++++++++
tests/events.c | 10 ++++++++++
5 files changed, 55 insertions(+), 1 deletion(-)
diff --git a/include/GL/glfw3.h b/include/GL/glfw3.h
index 8759ea79..e9cea004 100644
--- a/include/GL/glfw3.h
+++ b/include/GL/glfw3.h
@@ -706,6 +706,16 @@ typedef void* GLFWwindow;
*/
typedef void (* GLFWerrorfun)(int,const char*);
+/*! @brief The function signature for window position callbacks.
+ * @param[in] window The window that the user moved.
+ * @param[in] x The new x-coordinate, in pixels, of the upper-left corner of
+ * the client area of the window.
+ * @param[in] y The new y-coordinate, in pixels, of the upper-left corner of
+ * the client area of the window.
+ * @ingroup window
+ */
+typedef void (* GLFWwindowposfun)(GLFWwindow,int,int);
+
/*! @brief The function signature for window resize callbacks.
* @param[in] window The window that the user resized.
* @param[in] width The new width, in pixels, of the window.
@@ -1274,6 +1284,14 @@ GLFWAPI void glfwSetWindowUserPointer(GLFWwindow window, void* pointer);
*/
GLFWAPI void* glfwGetWindowUserPointer(GLFWwindow window);
+/*! @brief Sets the position callback for the specified window.
+ * @param[in] window The window whose callback to set.
+ * @param[in] cbfun The new callback, or @c NULL to remove the currently set
+ * callback.
+ * @ingroup window
+ */
+GLFWAPI void glfwSetWindowPosCallback(GLFWwindow window, GLFWwindowposfun cbfun);
+
/*! @brief Sets the size callback for the specified window.
* @param[in] window The window whose callback to set.
* @param[in] cbfun The new callback, or @c NULL to remove the currently set
diff --git a/readme.html b/readme.html
index 81100df6..1e629456 100644
--- a/readme.html
+++ b/readme.html
@@ -274,7 +274,7 @@ version of GLFW.
Added glfwSetErrorCallback
function and GLFWerrorfun
type for receiving more specific and/or nested errors
Added glfwSetWindowUserPointer
and glfwGetWindowUserPointer
functions for per-window user pointers
Added glfwGetVersionString
function for determining which code paths were enabled at compile time
- Added glfwGetWindowPos
function for querying the position of the specified window
+ Added glfwSetWindowPosCallback
function and GLFWwindowposfun
type for reciving window position events
Added glfwSetWindowFocusCallback
function and GLFWwindowfocusfun
type for receiving window focus events
Added glfwSetWindowIconifyCallback
function and GLFWwindowiconifyfun
type for receiving window iconification events
Added glfwGetClipboardString
and glfwSetClipboardString
functions for interacting with the system clipboard
@@ -284,6 +284,7 @@ version of GLFW.
Added GLFW_OPENGL_REVISION
window parameter to make up for removal of glfwGetGLVersion
Added GLFW_INCLUDE_GLCOREARB
macro for including glcorearb.h
instead of gl.h
Added GLFW_VISIBLE
window hint and parameter for controlling and polling window visibility
+ Added GLFW_POSITION_X
and GLFW_POSITION_Y
window hints and parameter for controlling and polling window position
Added windows
simple multi-window test program
Added sharing
simple OpenGL object sharing test program
Added modes
video mode enumeration and setting test program
diff --git a/src/internal.h b/src/internal.h
index c5a40af3..2ef8f10c 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -199,6 +199,7 @@ struct _GLFWwindow
int glRobustness;
PFNGLGETSTRINGIPROC GetStringi;
+ GLFWwindowposfun windowPosCallback;
GLFWwindowsizefun windowSizeCallback;
GLFWwindowclosefun windowCloseCallback;
GLFWwindowrefreshfun windowRefreshCallback;
diff --git a/src/window.c b/src/window.c
index 908a2a7c..ac0fd02f 100644
--- a/src/window.c
+++ b/src/window.c
@@ -119,8 +119,14 @@ void _glfwInputWindowFocus(_GLFWwindow* window, GLboolean focused)
void _glfwInputWindowPos(_GLFWwindow* window, int x, int y)
{
+ if (window->positionX == x && window->positionY == y)
+ return;
+
window->positionX = x;
window->positionY = y;
+
+ if (window->windowPosCallback)
+ window->windowPosCallback(window, x, y);
}
@@ -764,6 +770,24 @@ GLFWAPI void* glfwGetWindowUserPointer(GLFWwindow handle)
}
+//========================================================================
+// Set callback function for window position changes
+//========================================================================
+
+GLFWAPI void glfwSetWindowPosCallback(GLFWwindow handle, GLFWwindowposfun cbfun)
+{
+ _GLFWwindow* window = (_GLFWwindow*) handle;
+
+ if (!_glfwInitialized)
+ {
+ _glfwSetError(GLFW_NOT_INITIALIZED, NULL);
+ return;
+ }
+
+ window->windowPosCallback = cbfun;
+}
+
+
//========================================================================
// Set callback function for window size changes
//========================================================================
diff --git a/tests/events.c b/tests/events.c
index 3150a82e..46bde2ab 100644
--- a/tests/events.c
+++ b/tests/events.c
@@ -218,6 +218,15 @@ static const char* get_character_string(int character)
return result;
}
+static void window_pos_callback(GLFWwindow window, int x, int y)
+{
+ printf("%08x at %0.3f: Window position: %i %i\n",
+ counter++,
+ glfwGetTime(),
+ x,
+ y);
+}
+
static void window_size_callback(GLFWwindow window, int width, int height)
{
printf("%08x at %0.3f: Window size: %i %i\n",
@@ -354,6 +363,7 @@ int main(void)
printf("Window opened\n");
+ glfwSetWindowPosCallback(window, window_pos_callback);
glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetWindowRefreshCallback(window, window_refresh_callback);