From 61ca1a679e0175b4c3a60f1af947e2e203f54573 Mon Sep 17 00:00:00 2001
From: johns <>
Date: Sun, 14 Apr 2002 18:43:20 +0000
Subject: [PATCH] Made grab mouse and leave window configurable.

---
 src/include/interface.h  |  4 +++-
 src/include/video.h      |  2 +-
 src/stratagus/script.cpp | 33 +++++++++++++++++++++++++++++----
 src/ui/interface.cpp     |  2 +-
 src/ui/mouse.cpp         |  8 ++++++--
 src/ui/script_ui.cpp     | 40 ++++++++++++++++++++++++++++++++++++++--
 src/video/X11.cpp        |  9 +++++----
 src/video/cursor.cpp     |  2 +-
 src/video/sdl.cpp        |  8 +++++---
 src/video/svgalib.cpp    |  4 +++-
 src/video/wince.cpp      |  4 +++-
 11 files changed, 95 insertions(+), 21 deletions(-)

diff --git a/src/include/interface.h b/src/include/interface.h
index 6f4fe61ec..bfeb382ff 100644
--- a/src/include/interface.h
+++ b/src/include/interface.h
@@ -218,7 +218,9 @@ extern Unit* UnitUnderCursor;
     /// button number under the cursor
 extern int ButtonUnderCursor;
     /// button 0 (Game Menu) was clicked down
-extern int GameMenuButtonClicked;
+extern char GameMenuButtonClicked;
+    /// Mouse leaves windows stops scroll
+extern char LeaveStops;
     /// current CursorOn field
 extern enum _cursor_on_ CursorOn;
 
diff --git a/src/include/video.h b/src/include/video.h
index 838b03fbf..ecef48bdb 100644
--- a/src/include/video.h
+++ b/src/include/video.h
@@ -1366,7 +1366,7 @@ extern void SetVideoSync(void);
 extern void CheckVideoInterrupts(void);
 
     /// Toggle mouse grab mode
-extern void ToggleGrabMouse(void);
+extern void ToggleGrabMouse(int mode);
 
     /// Toggle full screen mode
 extern void ToggleFullScreen(void);
diff --git a/src/stratagus/script.cpp b/src/stratagus/script.cpp
index f2f10c3b8..ef634cefa 100644
--- a/src/stratagus/script.cpp
+++ b/src/stratagus/script.cpp
@@ -63,8 +63,8 @@ global char* CclStartFile;		/// CCL start file
 global int CclInConfigFile;		/// True while config file parsing
 
 global char*	Tips[MAX_TIPS+1];	/// Array of tips
-global int	ShowTips=0;		/// Show tips at start of level
-global int	CurrentTip=0;		/// Current tip to display
+global int	ShowTips;		/// Show tips at start of level
+global int	CurrentTip;		/// Current tip to display
 
 /*----------------------------------------------------------------------------
 --	Functions
@@ -234,6 +234,7 @@ local SCM CclDecorationOnTop(void)
     return SCM_UNSPECIFIED;
 }
 
+#if 0
 /**
 **	Show tips at the start of a level.
 */
@@ -246,10 +247,34 @@ local SCM CclShowTips(void)
 
     InitTips=0;
     ShowTips=1;
-    memset(Tips,0,sizeof(Tips));
+    // JOHNS: Done by system: memset(Tips,0,sizeof(Tips));
     return SCM_UNSPECIFIED;
 }
+#endif
 
+/**
+**	Enable/disable Showing the tips at the start of a level.
+**
+**	@param flag	True = turn on, false = off.
+**	@return		The old state of tips displayed.
+*/
+local SCM CclSetShowTips(SCM flag)
+{
+    int old;
+
+    old=ShowTips;
+    ShowTips=gh_scm2bool(flag);
+
+    return gh_bool2scm(old);
+}
+
+/**
+**	Add a new tip to the list of tips.
+**
+**	@param tip	A new tip to be displayed before level.
+**
+**	@todo	FIXME:	Memory for tips is never freed.
+*/
 local SCM CclAddTip(SCM tip)
 {
     int i;
@@ -584,7 +609,7 @@ global void InitCcl(void)
     init_subr_0("show-no-full",CclShowNoFull);
     init_subr_0("decoration-on-top",CclDecorationOnTop);
 
-    init_subr_0("show-tips",CclShowTips);
+    gh_new_procedure1_0("set-show-tips!",CclSetShowTips);
     gh_new_procedure1_0("add-tip",CclAddTip);
 
     gh_new_procedure1_0("speed-mine",CclSpeedMine);
diff --git a/src/ui/interface.cpp b/src/ui/interface.cpp
index 8a7a8fe1f..0d1708664 100644
--- a/src/ui/interface.cpp
+++ b/src/ui/interface.cpp
@@ -526,7 +526,7 @@ local void UiFindIdleWorker(void)
 local void UiToggleGrabMouse(void)
 {
     DebugLevel0Fn("%x\n",KeyModifiers);
-    ToggleGrabMouse();
+    ToggleGrabMouse(0);
     SetStatusLine("Grab mouse toggled.");
 }
 
diff --git a/src/ui/mouse.cpp b/src/ui/mouse.cpp
index ec4ede2fd..b27639c66 100644
--- a/src/ui/mouse.cpp
+++ b/src/ui/mouse.cpp
@@ -59,9 +59,10 @@ global enum _mouse_buttons_ MouseButtons;/// current pressed mouse buttons
 
 global enum _key_modifiers_ KeyModifiers;/// current keyboard modifiers
 
-global int ButtonUnderCursor=-1;	/// Button under cursor
-global int GameMenuButtonClicked=0;	/// Game menu button (F10) was clicked
 global Unit* UnitUnderCursor;		/// Unit under cursor
+global int ButtonUnderCursor=-1;	/// Button under cursor
+global char GameMenuButtonClicked;	/// Game menu button (F10) was clicked
+global char LeaveStops;			/// Mouse leaves windows stops scroll
 
 global enum _cursor_on_ CursorOn=CursorOnUnknown;	/// cursor on field
 
@@ -563,6 +564,9 @@ local void HandleMouseOn(int x,int y)
 */
 global void HandleMouseExit(void)
 {
+    if( !LeaveStops ) {			// Disabled
+	return;
+    }
     //
     // Denote cursor not on anything in window (used?)
     //
diff --git a/src/ui/script_ui.cpp b/src/ui/script_ui.cpp
index 1f680f8b7..b6aa9ada7 100644
--- a/src/ui/script_ui.cpp
+++ b/src/ui/script_ui.cpp
@@ -469,7 +469,7 @@ local SCM CclDefineCursor(SCM list)
 **
 **	@param ident	Cursor identifier.
 */
-local SCM CclGameCursor(SCM ident)
+local SCM CclSetGameCursor(SCM ident)
 {
     char* str;
 
@@ -1216,6 +1216,40 @@ local SCM CclSetMouseScrollSpeed(SCM num)
     return gh_int2scm(old);
 }
 
+/**
+**	Enable/disable grabbing the mouse.
+**
+**	@param flag	True = grab on, false = grab off.
+**	@return		FIXME: not supported: The old state of grabbing.
+*/
+local SCM CclSetGrabMouse(SCM flag)
+{
+    if( gh_scm2bool(flag) ) {
+	ToggleGrabMouse(1);
+    } else {
+	ToggleGrabMouse(-1);
+    }
+
+    //return gh_bool2scm(old);
+    return SCM_UNSPECIFIED;
+}
+
+/**
+**	Enable/disable leaving the window stops scrolling.
+**
+**	@param flag	True = stop on, false = stop off.
+**	@return		The old state of stopping.
+*/
+local SCM CclSetLeaveStops(SCM flag)
+{
+    int old;
+
+    old=LeaveStops;
+    LeaveStops=gh_scm2bool(flag);
+
+    return gh_bool2scm(old);
+}
+
 /**
 **	Enable/disable scrolling with the keyboard.
 **
@@ -1696,9 +1730,11 @@ global void UserInterfaceCclRegister(void)
     gh_new_procedure1_0("set-original-resources!",CclSetOriginalResources);
 
     gh_new_procedureN("define-cursor",CclDefineCursor);
-    gh_new_procedure1_0("game-cursor",CclGameCursor);
+    gh_new_procedure1_0("set-game-cursor!",CclSetGameCursor);
     gh_new_procedureN("define-ui",CclDefineUI);
 
+    gh_new_procedure1_0("set-grab-mouse!", CclSetGrabMouse);
+    gh_new_procedure1_0("set-leave-stops!", CclSetLeaveStops);
     gh_new_procedure1_0("set-key-scroll!", CclSetKeyScroll);
     gh_new_procedure1_0("set-key-scroll-speed!", CclSetKeyScrollSpeed);
     gh_new_procedure1_0("set-mouse-scroll!", CclSetMouseScroll);
diff --git a/src/video/X11.cpp b/src/video/X11.cpp
index d40b05016..02f50304a 100644
--- a/src/video/X11.cpp
+++ b/src/video/X11.cpp
@@ -1359,21 +1359,22 @@ global void RealizeVideoMemory(void)
 
 /**
 **	Toggle grab mouse.
+**
+**	@param mode	Wanted mode, 1 grab, -1 not grab, 0 toggle.
 */
-global void ToggleGrabMouse(void)
+global void ToggleGrabMouse(int mode)
 {
     static int grabbed;
 
-    if( grabbed ) {
+    if( mode<=0 && grabbed ) {
 	XUngrabPointer(TheDisplay,CurrentTime);
 	grabbed=0;
-    } else {
+    } else if( mode>=0 && !grabbed ) {
 	if( XGrabPointer(TheDisplay,TheMainWindow,True,0
 		,GrabModeAsync,GrabModeAsync
 		,TheMainWindow, None, CurrentTime)==GrabSuccess ) {
 	    grabbed=1;
 	}
-
     }
 }
 
diff --git a/src/video/cursor.cpp b/src/video/cursor.cpp
index 7874d2e05..1408fbf13 100644
--- a/src/video/cursor.cpp
+++ b/src/video/cursor.cpp
@@ -1062,7 +1062,7 @@ global void SaveCursors(FILE* file)
     }
 
     // Not ready:
-    fprintf(file,";;(game-cursor '%s)\n",GameCursor->Ident);
+    fprintf(file,";;(set-game-cursor! '%s)\n",GameCursor->Ident);
     // FIXME: what about the other variables???
     switch( CursorState ) {
 	case CursorStatePoint:
diff --git a/src/video/sdl.cpp b/src/video/sdl.cpp
index 71d5ea725..bbdc6e5d2 100644
--- a/src/video/sdl.cpp
+++ b/src/video/sdl.cpp
@@ -864,15 +864,17 @@ global void SdlUnlockScreen(void)
 
 /**
 **	Toggle grab mouse.
+**
+**	@param mode	Wanted mode, 1 grab, -1 not grab, 0 toggle.
 */
-global void ToggleGrabMouse(void)
+global void ToggleGrabMouse(int mode)
 {
     static int grabbed;
 
-    if( grabbed ) {
+    if( mode<=0 && grabbed ) {
 	SDL_WM_GrabInput(SDL_GRAB_OFF);
 	grabbed=0;
-    } else {
+    } else if( mode>=0 && !grabbed ) {
 	if( SDL_WM_GrabInput(SDL_GRAB_ON)==SDL_GRAB_ON ) {
 	    grabbed=1;
 	}
diff --git a/src/video/svgalib.cpp b/src/video/svgalib.cpp
index daa1f1ea1..6fe435214 100644
--- a/src/video/svgalib.cpp
+++ b/src/video/svgalib.cpp
@@ -1319,8 +1319,10 @@ global void RealizeVideoMemory(void)
 
 /**
 **	Toggle grab mouse.
+**
+**	@param mode	Wanted mode, 1 grab, -1 not grab, 0 toggle.
 */
-global void ToggleGrabMouse(void)
+global void ToggleGrabMouse(int mode __attribute__((unused)))
 {
 }
 
diff --git a/src/video/wince.cpp b/src/video/wince.cpp
index a9733c3d9..a1cdb60f5 100644
--- a/src/video/wince.cpp
+++ b/src/video/wince.cpp
@@ -494,8 +494,10 @@ global void RealizeVideoMemory(void)
 
 /**
 **	Toggle grab mouse.
+**
+**	@param mode	Wanted mode, 1 grab, -1 not grab, 0 toggle.
 */
-global void ToggleGrabMouse(void)
+global void ToggleGrabMouse(int mode __attribute__((unused)))
 {
 }