From 7f10d1fa8afbfd7606aac666855c7bb320cab54c Mon Sep 17 00:00:00 2001 From: brawer Date: Fri, 19 Aug 2016 22:55:20 +0200 Subject: [PATCH] Add wxSYS_CARET_{ON,OFF,TIMEOUT}_MSEC system settings Provide a way to retrieve the caret blink times from wxSystemSe and implement it for wxOSX and wxGTK. Closes #17629. --- docs/changes.txt | 1 + include/wx/settings.h | 5 +++- interface/wx/settings.h | 36 ++++++++++++++++++++++++++- src/gtk/settings.cpp | 52 +++++++++++++++++++++++++++++++++++++++ src/osx/cocoa/settings.mm | 46 ++++++++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 2 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index f08b98f8d5..abb9a61a6b 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -96,6 +96,7 @@ All (GUI): - Fix rescaling of wxImage. - Fix displaying edited value of wxUIntProperty (wxPropertyGrid). - Fix displaying validation errors for numeric wxPropertyGrid properties. +- Add wxSYS_CARET_{ON,OFF,TIMEOUT}_MSEC system settings (brawer). wxGTK: diff --git a/include/wx/settings.h b/include/wx/settings.h index f90ae071ba..cf5602ccd8 100644 --- a/include/wx/settings.h +++ b/include/wx/settings.h @@ -137,7 +137,10 @@ enum wxSystemMetric wxSYS_PENWINDOWS_PRESENT, wxSYS_SHOW_SOUNDS, wxSYS_SWAP_BUTTONS, - wxSYS_DCLICK_MSEC + wxSYS_DCLICK_MSEC, + wxSYS_CARET_ON_MSEC, + wxSYS_CARET_OFF_MSEC, + wxSYS_CARET_TIMEOUT_MSEC }; // possible values for wxSystemSettings::HasFeature() parameter diff --git a/interface/wx/settings.h b/interface/wx/settings.h index 04cc77d74e..ff4176bb37 100644 --- a/interface/wx/settings.h +++ b/interface/wx/settings.h @@ -191,7 +191,41 @@ enum wxSystemMetric //!< visually in situations where it would otherwise present the information //!< only in audible form; zero otherwise. wxSYS_SWAP_BUTTONS, //!< Non-zero if the meanings of the left and right mouse buttons are swapped; zero otherwise. - wxSYS_DCLICK_MSEC //!< Maximal time, in milliseconds, which may pass between subsequent clicks for a double click to be generated. + wxSYS_DCLICK_MSEC, //!< Maximal time, in milliseconds, which may pass between subsequent clicks for a double click to be generated. + + /** + Time, in milliseconds, for how long a blinking caret should + stay visible during a single blink cycle before it disappears. + If this value is negative, the platform does not support the + user setting. Implemented only on GTK+ and MacOS X. + + @since 3.1.1 + */ + wxSYS_CARET_ON_MSEC, + + /** + Time, in milliseconds, for how long a blinking caret should + stay invisible during a single blink cycle before it reappears. + If this value is zero, carets should be visible all the time + instead of blinking. If the value is negative, the platform + does not support the user setting. Implemented only on GTK+ + and MacOS X. + + @since 3.1.1 + */ + wxSYS_CARET_OFF_MSEC, + + /** + Time, in milliseconds, for how long a caret should blink after + a user interaction. After this timeout has expired, the caret + should stay continuously visible until the user interacts with + the caret again (for example by entering, deleting or cutting + text). If this value is negative, carets should blink forever; + if it is zero, carets should not blink at all. + + @since 3.1.1 + */ + wxSYS_CARET_TIMEOUT_MSEC }; /** diff --git a/src/gtk/settings.cpp b/src/gtk/settings.cpp index 8343c6ed09..0dd99095c4 100644 --- a/src/gtk/settings.cpp +++ b/src/gtk/settings.cpp @@ -19,6 +19,8 @@ #include "wx/fontutil.h" #include "wx/fontenum.h" +#include + #include #include "wx/gtk/private/win_gtk.h" #include "wx/gtk/private/gtk2-compat.h" @@ -574,6 +576,56 @@ int wxSystemSettingsNative::GetMetric( wxSystemMetric index, wxWindow* win ) "gtk-double-click-time", &dclick, NULL); return dclick; + case wxSYS_CARET_ON_MSEC: + { + gint blink_time = -1; + g_object_get(GetSettingsForWindowScreen(window), + "gtk-cursor-blink-time", &blink_time, NULL); + if (blink_time > 0) + return blink_time / 2; + + return -1; + } + + case wxSYS_CARET_OFF_MSEC: + { + gboolean should_blink = true; + gint blink_time = -1; + g_object_get(GetSettingsForWindowScreen(window), + "gtk-cursor-blink", &should_blink, + "gtk-cursor-blink-time", &blink_time, + NULL); + if (!should_blink) + return 0; + + if (blink_time > 0) + return blink_time / 2; + + return -1; + } + + case wxSYS_CARET_TIMEOUT_MSEC: + { + gboolean should_blink = true; + gint timeout = 0; + g_object_get(GetSettingsForWindowScreen(window), + "gtk-cursor-blink", &should_blink, + "gtk-cursor-blink-timeout", &timeout, + NULL); + if (!should_blink) + return 0; + + // GTK+ returns this value in seconds, not milliseconds, + // Special value of 2147483647 means that the cursor never + // blinks and we handle any value that would overflow int after + // multiplication in the same manner as it looks quite + // unnecessary to support cursor blinking once a month. + if (timeout > 0 && timeout < INT32_MAX / 1000) + return timeout * 1000; + + return -1; // no timeout, blink forever + } + case wxSYS_DRAG_X: case wxSYS_DRAG_Y: gint drag_threshold; diff --git a/src/osx/cocoa/settings.mm b/src/osx/cocoa/settings.mm index 6ccaf26490..101ded7eac 100644 --- a/src/osx/cocoa/settings.mm +++ b/src/osx/cocoa/settings.mm @@ -21,6 +21,26 @@ #include "wx/osx/cocoa/private.h" #import +#import + + +static int wxOSXGetUserDefault(NSString* key, int defaultValue) +{ + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + if (!defaults) + { + return defaultValue; + } + + id setting = [defaults objectForKey: key]; + if (!setting) + { + return defaultValue; + } + + return [setting intValue]; +} + // ---------------------------------------------------------------------------- // wxSystemSettingsNative @@ -231,6 +251,32 @@ int wxSystemSettingsNative::GetMetric(wxSystemMetric index, wxWindow *WXUNUSED(w // but rather rely on the 'click-count' by the system delivered in a mouse event return 500; + case wxSYS_CARET_ON_MSEC: + value = wxOSXGetUserDefault(@"NSTextInsertionPointBlinkPeriodOn", -1); + if (value > 0) + return value; + + value = wxOSXGetUserDefault(@"NSTextInsertionPointBlinkPeriod", -1); + if (value > 0) + return value / 2; + + return -1; + + case wxSYS_CARET_OFF_MSEC: + value = wxOSXGetUserDefault(@"NSTextInsertionPointBlinkPeriodOff", -1); + if (value > 0) + return value; + + value = wxOSXGetUserDefault(@"NSTextInsertionPointBlinkPeriod", -1); + if (value > 0) + return value / 2; + + return -1; + + case wxSYS_CARET_TIMEOUT_MSEC: + // On MacOS X, carets don't stop blinking after user interactions. + return -1; + default: return -1; // unsupported metric }