Skip to content

Commit

Permalink
Add CBool GParamSpec (haskell-gi#409)
Browse files Browse the repository at this point in the history
* Add CBool GParamSpec

In order to avoid "unable to set property 'xx' of type 'HaskellGIStablePtr' from value of type 'gboolean'" error
Native boolean GParamSpec is needed. This patch implements just that.

* Change CBool to gboolean

As correctly pointed out gboolean is not a CBool, but rather a Int.
(using #{type gboolean})
  • Loading branch information
psiska authored Jun 9, 2023
1 parent 53eaa7f commit aea594d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
12 changes: 11 additions & 1 deletion base/Data/GI/Base/GObject.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module Data.GI.Base.GObject
, gobjectInstallProperty
, gobjectInstallCIntProperty
, gobjectInstallCStringProperty
, gobjectInstallGBooleanProperty
) where

import Data.Maybe (catMaybes)
Expand Down Expand Up @@ -69,7 +70,8 @@ import Data.GI.Base.CallStack (HasCallStack, prettyCallStack)
import Data.GI.Base.GParamSpec (PropertyInfo(..),
gParamSpecValue,
CIntPropertyInfo(..), CStringPropertyInfo(..),
gParamSpecCInt, gParamSpecCString,
GBooleanPropertyInfo(..),
gParamSpecCInt, gParamSpecCString, gParamSpecGBoolean,
getGParamSpecGetterSetter,
PropGetSetter(..))
import Data.GI.Base.GQuark (GQuark(..), gQuarkFromString)
Expand Down Expand Up @@ -411,3 +413,11 @@ gobjectInstallCStringProperty klass propInfo = do
pspec <- gParamSpecCString propInfo
withManagedPtr pspec $ \pspecPtr ->
g_object_class_install_property klass 1 pspecPtr

-- | Add a `${type gboolean}`-valued property to the given object class.
gobjectInstallGBooleanProperty :: DerivedGObject o =>
GObjectClass -> GBooleanPropertyInfo o -> IO ()
gobjectInstallGBooleanProperty klass propInfo = do
pspec <- gParamSpecGBoolean propInfo
withManagedPtr pspec $ \pspecPtr ->
g_object_class_install_property klass 1 pspecPtr
45 changes: 45 additions & 0 deletions base/Data/GI/Base/GParamSpec.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module Data.GI.Base.GParamSpec
, gParamSpecCString
, CIntPropertyInfo(..)
, gParamSpecCInt
, GBooleanPropertyInfo(..)
, gParamSpecGBoolean
, GParamFlag(..)

-- * Get\/Set
Expand All @@ -33,6 +35,7 @@ import Foreign.StablePtr (newStablePtr, deRefStablePtr,
castStablePtrToPtr, castPtrToStablePtr)
import Control.Monad (void)
import Data.Coerce (coerce)
import Data.Int
import Data.Maybe (fromMaybe)
import Data.Text (Text)

Expand Down Expand Up @@ -320,6 +323,48 @@ gParamSpecCString (CStringPropertyInfo {..}) =
gParamSpecSetQData pspecPtr quark (wrapGetSet getter setter gvalueSet_)
wrapGParamSpecPtr pspecPtr

-- | Information on a property of type `type gboolean` to be registered. A
-- property name consists of segments consisting of ASCII letters and
-- digits, separated by either the \'-\' or \'_\' character. The first
-- character of a property name must be a letter. Names which violate
-- these rules lead to undefined behaviour.
--
-- When creating and looking up a property, either separator can be
-- used, but they cannot be mixed. Using \'-\' is considerably more
-- efficient and in fact required when using property names as detail
-- strings for signals.
--
-- Beyond the name, properties have two more descriptive strings
-- associated with them, the @nick@, which should be suitable for use
-- as a label for the property in a property editor, and the @blurb@,
-- which should be a somewhat longer description, suitable for e.g. a
-- tooltip. The @nick@ and @blurb@ should ideally be localized.
data GBooleanPropertyInfo o = GBooleanPropertyInfo
{ name :: Text
, nick :: Text
, blurb :: Text
, defaultValue :: Bool
, flags :: Maybe [GParamFlag]
, setter :: o -> Bool -> IO ()
, getter :: o -> IO (Bool)
}

foreign import ccall g_param_spec_boolean ::
CString -> CString -> CString -> #{type gboolean} -> CInt -> IO (Ptr GParamSpec)

-- | Create a `GParamSpec` for a bool param.
gParamSpecGBoolean :: GObject o => GBooleanPropertyInfo o -> IO GParamSpec
gParamSpecGBoolean (GBooleanPropertyInfo {..}) =
withTextCString name $ \cname ->
withTextCString nick $ \cnick ->
withTextCString blurb $ \cblurb -> do
pspecPtr <- g_param_spec_boolean cname cnick cblurb
((fromIntegral . fromEnum) defaultValue)
(maybe defaultFlags gflagsToWord flags)
quark <- pspecQuark
gParamSpecSetQData pspecPtr quark (wrapGetSet getter setter gvalueSet_)
wrapGParamSpecPtr pspecPtr

foreign import ccall g_param_spec_set_qdata_full ::
Ptr GParamSpec -> GQuark a -> Ptr b -> FunPtr (Ptr c -> IO ()) -> IO ()

Expand Down

0 comments on commit aea594d

Please sign in to comment.