It is currently Thu Oct 18, 2018 6:06 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Show / hide cursor
PostPosted: Mon Aug 06, 2018 7:23 am 
Offline
User
User

Joined: Wed Sep 12, 2012 5:09 pm
Posts: 15
Hi, does anyone know if their's a way (like showcurosr() in windows) to show or hide the cursor on a window ?
I'm using the windowmouseX/Y to display my mouse sprite in a 2D screen to avoid high DPI mouse "bug" so my "game" is (for now) windows only just because of this API request.
(If anyone know the same tric on macOS, i'm intrested too.)

Thx.


Top
 Profile  
Reply with quote  
 Post subject: Re: Show / hide cursor
PostPosted: Mon Aug 06, 2018 9:19 am 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1560
Location: Germany
In order to hide the cursor in Linux you have to change the cursor to #GDK_BLANK_CURSOR (tested successfully on Linux Mint 19 x86 'Tara' with Cinnamon using PB 5.46 x86 in ASCII and Unicode mode with GTK2 and GTK3):
Code:
EnableExplicit

#GDK_BLANK_CURSOR = -2

ImportC ""
  gtk_widget_get_window(*Widget.GtkWidget)
EndImport

Procedure HideCursor(HideCursor.I = #True)
  Protected Cursor.I

  If HideCursor
    Cursor = gdk_cursor_new_(#GDK_BLANK_CURSOR)
  Else
    Cursor = gdk_cursor_new_(#GDK_ARROW)
  EndIf

  gdk_window_set_cursor_(gtk_widget_get_window(WindowID(0)), Cursor)
EndProcedure

ProcedureC KeyPressCallback(*Widget.GtkWidget, *Event.GdkEventKey, *UserData)
  Protected Key.L

  Key = *Event\keyval & $FF

  If Key = #ESC
    HideCursor(#False)
  EndIf
EndProcedure   

OpenWindow(0, 200, 100, 200, 100, "Hide cursor demo")
ButtonGadget(0, 30, 20, 140, 25, "Hide cursor")

CreateStatusBar(0, WindowID(0))
AddStatusBarField(#PB_Ignore)
StatusBarText(0, 0, "Press <Esc> to show cursor", #PB_StatusBar_Center)
g_signal_connect_(WindowID(0), "key_press_event", @KeyPressCallback(), 0)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0 And EventType() = #PB_EventType_LeftClick
        HideCursor(#True)
      EndIf
  EndSelect
ForEver


Top
 Profile  
Reply with quote  
 Post subject: Re: Show / hide cursor
PostPosted: Mon Aug 06, 2018 11:18 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 808
Location: right here
Thanks Shardik. Works here too with PB570b1 x64, Ubuntu 16.04, xfce, gtk3/gtk2.


Top
 Profile  
Reply with quote  
 Post subject: Re: Show / hide cursor
PostPosted: Mon Aug 06, 2018 12:59 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1560
Location: Germany
#NULL wrote:
Thanks Shardik. Works here too with PB570b1 x64, Ubuntu 16.04, xfce, gtk3/gtk2.
Thank you for your testing and feedback!


Top
 Profile  
Reply with quote  
 Post subject: Re: Show / hide cursor
PostPosted: Mon Aug 06, 2018 3:21 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1560
Location: Germany
This is a cross-platform example to hide and show the cursor. I successfully tested it on these operating systems:
- Fedora 27 x86 with Gnome 3 and PB 5.46 x86 in ASCII and Unicode mode with GTK3 (for GTK2 take a look into Update 2 below)
- Kubuntu 18.04 x86 with KDE and PB 5.46 x86 in ASCII and Unicode mode with GTK2 and GTK3
- Linux Mint 18.3 x64 'Sylvia' with Cinnamon and PB 5.46 x64 in ASCII and Unicode mode
- MacOS 10.6.8 'Snow Leopard' with PB 5.46 x86 in ASCII and Unicode mode
- Windows 7 x64 with PB 5.46 x86 and x64 in ASCII and Unicode mode
Code:
EnableExplicit

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  #GDK_BLANK_CURSOR = -2
 
  ImportC ""
    gtk_widget_get_window(*Widget.GtkWidget)
  EndImport
CompilerEndIf
 
Procedure HideCursor(HideCursor.I = #True)
  Protected Cursor.I
 
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      If HideCursor
        Cursor = gdk_cursor_new_(#GDK_BLANK_CURSOR)
      Else
        Cursor = 0
      EndIf
     
      gdk_window_set_cursor_(gtk_widget_get_window(WindowID(0)), Cursor)
    CompilerCase #PB_OS_MacOS
      If HideCursor
        CocoaMessage(0, 0, "NSCursor hide")
      Else
        CocoaMessage(0, 0, "NSCursor unhide")
      EndIf
    CompilerCase #PB_OS_Windows
      If HideCursor
        ShowCursor_(#False)
      Else
        ShowCursor_(#True)
      EndIf
  CompilerEndSelect
EndProcedure

OpenWindow(0, 200, 100, 200, 100, "Hide cursor demo")
ButtonGadget(0, 30, 20, 140, 25, "Hide cursor")

CreateStatusBar(0, WindowID(0))
AddStatusBarField(#PB_Ignore)
StatusBarText(0, 0, "Press <Esc> to show cursor", #PB_StatusBar_Center)

AddKeyboardShortcut(0, #PB_Shortcut_Escape, 0)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0 And EventType() = #PB_EventType_LeftClick
        HideCursor(#True)
        DisableGadget(0, #True)
      EndIf
    Case #PB_Event_Menu
      If EventMenu() = 0
        HideCursor(#False)
        DisableGadget(0, #False)
      EndIf
  EndSelect
ForEver

Update 1: For Linux I have changed
Code:
Cursor = gdk_cursor_new_(#GDK_ARROW)
to
Code:
Cursor = 0
in order to restore the default cursor after having pressed the <Esc> key.

Update 2: In order to run the example on the Linux distribution Fedora 27 x86 with Gnome 3 and GTK2 you have to add the following declarations to the ImportC..EndImport block:
Code:
    gdk_cursor_new(CursorType.L)
    gdk_window_set_cursor(*GdkWindow, *Cursor.GdkCursor)
and you have delete the trailing underscores when calling these functions.


Last edited by Shardik on Wed Aug 08, 2018 3:49 pm, edited 3 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Show / hide cursor
PostPosted: Mon Aug 06, 2018 5:34 pm 
Offline
User
User

Joined: Wed Sep 12, 2012 5:09 pm
Posts: 15
Can't test macOS yet but for linux it work ! Thank you a lot Shardik !!


Top
 Profile  
Reply with quote  
 Post subject: Re: Show / hide cursor
PostPosted: Mon Aug 06, 2018 9:16 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 25, 2014 5:25 pm
Posts: 262
Location: Germany
On my Manjaro Linux (gtk3) the normal mouse cursor is not restored correctly:

Image

Edit:
If I replace "#GDK_ARROW" with "#GDK_LEFT_PTR", it works correctly.
But there is also "#GDK_RIGHT_PTR" so it is best to save the current mouse cursor so that it can be restored correctly.

_________________
Image
Why OpenSource should have a license
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download -- Any help is welcome!
Manjaro Xfce x64 (Main system) :: WindowsXP/Xubuntu x86 (VirtualBox) :: PureBasic (Linux: x86/x64, Windows: x86) :: All are up to date


Last edited by Sicro on Mon Aug 06, 2018 11:15 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Show / hide cursor
PostPosted: Mon Aug 06, 2018 9:17 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1566
@Shardik: the Windows version, ShowCursor_(#False), only works for the current window. As soon as you move the mouse away from it, it becomes visible again. Just in case you weren't aware.


Top
 Profile  
Reply with quote  
 Post subject: Re: Show / hide cursor
PostPosted: Tue Aug 07, 2018 9:53 am 
Offline
User
User

Joined: Wed Sep 12, 2012 5:09 pm
Posts: 15
Quote:
@Shardik: the Windows version, ShowCursor_(#False), only works for the current window. As soon as you move the mouse away from it, it becomes visible again. Just in case you weren't aware.


That's what i asked for, but thank for the warn.

Quote:
so it is best to save the current mouse cursor so that it can be restored correctly.


Does something like gdk_window_get_cursor_() work for this ? (I'm realy new with linux... ^^")


Top
 Profile  
Reply with quote  
 Post subject: Re: Show / hide cursor
PostPosted: Tue Aug 07, 2018 10:44 am 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 1560
Location: Germany
Sicro wrote:
On my Manjaro Linux (gtk3) the normal mouse cursor is not restored correctly

Thank you for your bug report. I expected this problem already to appear. I hope to have found a solution by changing
Code:
Cursor = gdk_cursor_new_(#GDK_ARROW)
to
Code:
Cursor = 0
This should set the default cursor. I have changed this also in my cross-platform example above.


Top
 Profile  
Reply with quote  
 Post subject: Re: Show / hide cursor
PostPosted: Tue Aug 07, 2018 12:32 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1566
Papala wrote:
That's what i asked for

Oops, I didn't realize. My bad. :oops:


Top
 Profile  
Reply with quote  
 Post subject: Re: Show / hide cursor
PostPosted: Tue Aug 07, 2018 4:45 pm 
Offline
User
User

Joined: Wed Sep 12, 2012 5:09 pm
Posts: 15
Shardik wrote:
This should set the default cursor.


Work fine with Debian 8.


Top
 Profile  
Reply with quote  
 Post subject: Re: Show / hide cursor
PostPosted: Tue Aug 07, 2018 5:05 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 25, 2014 5:25 pm
Posts: 262
Location: Germany
Thank you, Shardik, now it works :)

_________________
Image
Why OpenSource should have a license
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download -- Any help is welcome!
Manjaro Xfce x64 (Main system) :: WindowsXP/Xubuntu x86 (VirtualBox) :: PureBasic (Linux: x86/x64, Windows: x86) :: All are up to date


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye