File: ComboBoxEntry.chs

package info (click to toggle)
haskell-gtk 0.11.0-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,920 kB
  • ctags: 82
  • sloc: haskell: 1,929; ansic: 714; sh: 5; makefile: 3
file content (228 lines) | stat: -rw-r--r-- 7,766 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{-# LANGUAGE CPP #-}
-- -*-haskell-*-
--  GIMP Toolkit (GTK) Widget ComboBoxEntry
--
--  Author : Duncan Coutts
--
--  Created: 25 April 2004
--
--  Copyright (C) 2004-2005 Duncan Coutts
--
--  This library is free software; you can redistribute it and/or
--  modify it under the terms of the GNU Lesser General Public
--  License as published by the Free Software Foundation; either
--  version 2.1 of the License, or (at your option) any later version.
--
--  This library is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--  Lesser General Public License for more details.
--
-- |
-- Maintainer  : gtk2hs-users@lists.sourceforge.net
-- Stability   : provisional
-- Portability : portable (depends on GHC)
--
-- A text entry field with a dropdown list
--
-- * Module available since Gtk+ version 2.4
--
module Graphics.UI.Gtk.MenuComboToolbar.ComboBoxEntry (
-- * Detail
--  
-- | A 'ComboBoxEntry' is a widget that allows the user to choose from a list
-- of valid choices or enter a different value. It is very similar to a
-- 'ComboBox', but it displays the selected value in an entry to allow
-- modifying it.
--
-- In contrast to a 'ComboBox', the underlying model of a 'ComboBoxEntry' must
-- always have a text column (see 'comboBoxEntrySetTextColumn'), and the entry
-- will show the content of the text column in the selected row. To get the
-- text from the entry, use 'comboBoxEntryGetActiveText'.
--
-- The 'Graphics.UI.Gtk.MenuComboToolbar.ComboBox.changed' signal will be
-- emitted while typing into a 'ComboBoxEntry', as well as when selecting an
-- item from the 'ComboBoxEntry''s list. Use 'comboBoxGetActive' or
-- 'comboBoxGetActiveIter' to discover whether an item was actually selected
-- from the list.
--
-- Connect to the activate signal of the 'Entry' (use 'binGetChild') to detect
-- when the user actually finishes entering text.
--

-- * Class Hierarchy
-- |
-- @
-- |  'GObject'
-- |   +----'Object'
-- |         +----'Widget'
-- |               +----'Container'
-- |                     +----'Bin'
-- |                           +----'ComboBox'
-- |                                 +----ComboBoxEntry
-- @

#if GTK_CHECK_VERSION(2,4,0)
-- * Types
  ComboBoxEntry,
  ComboBoxEntryClass,
  castToComboBoxEntry, gTypeComboBoxEntry,
  toComboBoxEntry,

-- * Constructors
  comboBoxEntryNew,
  comboBoxEntryNewText,
  comboBoxEntryNewWithModel,

-- * Methods
  comboBoxEntrySetModelText,

  comboBoxEntrySetTextColumn,
  comboBoxEntryGetTextColumn,
#if GTK_CHECK_VERSION(2,6,0)
  comboBoxEntryGetActiveText,
#endif
  
-- * Attributes
  comboBoxEntryTextColumn,
#endif
  ) where

import Control.Monad	(liftM)

import System.Glib.FFI
import System.Glib.UTFString
import System.Glib.Attributes
import Graphics.UI.Gtk.Abstract.Object	(makeNewObject)
{#import Graphics.UI.Gtk.Types#} hiding ( ListStore )
import Graphics.UI.Gtk.ModelView.Types
import Graphics.UI.Gtk.MenuComboToolbar.ComboBox
import Graphics.UI.Gtk.ModelView.CellRendererText
import Graphics.UI.Gtk.ModelView.CellLayout
{#import Graphics.UI.Gtk.ModelView.CustomStore#}
{#import Graphics.UI.Gtk.ModelView.TreeModel#} 
import Graphics.UI.Gtk.ModelView.ListStore ( ListStore, listStoreNew )

{# context lib="gtk" prefix="gtk" #}

#if GTK_CHECK_VERSION(2,4,0)
--------------------
-- Constructors

-- | Creates a new 'ComboBoxEntry' which has a 'Entry' as child. After
-- construction, you should set a model using 'comboBoxSetModel' and a
-- text column using 'comboBoxEntrySetTextColumn'.
--
comboBoxEntryNew :: IO ComboBoxEntry
comboBoxEntryNew =
  makeNewObject mkComboBoxEntry $
  liftM (castPtr :: Ptr Widget -> Ptr ComboBoxEntry) $
  {# call gtk_combo_box_entry_new #}

-- | Creates a new 'ComboBoxEntry' with a store containing strings.
--   See 'comboBoxEntrySetModelText'.
--
comboBoxEntryNewText :: IO ComboBoxEntry
comboBoxEntryNewText = do
  combo <- comboBoxEntryNew
  comboBoxEntrySetModelText combo
  return combo
  
-- | Creates a new 'ComboBoxEntry' which has a 'Entry' as child and a list of
-- strings as popup. You can get the 'Entry' from a 'ComboBoxEntry' using
-- 'binGetChild'. To add and remove strings from the list, just modify @model@
-- using its data manipulation API.
--
comboBoxEntryNewWithModel :: TreeModelClass model => 
    model        -- ^ @model@ - A 'CustomStore'.
 -> IO ComboBoxEntry
comboBoxEntryNewWithModel model = do
  combo <- comboBoxEntryNew
  comboBoxSetModel combo (Just model)
  return combo

--------------------
-- Methods

-- | Set a model box that holds strings.
--
-- This function stores a 'Graphics.UI.Gtk.ModelView.ListStore' with the
-- widget and sets the model to the list store. The widget can contain only
-- strings. The model can be retrieved with 'comboBoxGetModel'. The list
-- store can be retrieved with 'comboBoxGetModelText'.
-- Any exisiting model or renderers are removed before setting the new text
-- model.
-- In order to respond to new texts that the user enters, it is necessary to
-- connect to the 'Graphics.UI.Gtk.Entry.Entry.entryActivate' signal of the
-- contained 'Graphics.UI.Gtk.Entry.Entry.Entry' an insert the text into the
-- text model which can be retrieved with
-- 'Graphics.UI.Gtk.MenuComboToolbar.ComboBox.comboBoxGetModelText'.
-- Note that the functions 'comboBoxAppendText', 'comboBoxInsertText',
-- 'comboBoxPrependText', 'comboBoxRemoveText' and 'comboBoxGetActiveText'
-- can be called on a combo box only once 'comboBoxEntrySetModelText' is
-- called.
--
comboBoxEntrySetModelText :: ComboBoxEntryClass self => self ->
                             IO (ListStore String)
comboBoxEntrySetModelText combo = do
  store <- listStoreNew ([] :: [String])
  comboBoxSetModel combo (Just store)
  let colId = makeColumnIdString 0
  customStoreSetColumn store colId id
  comboBoxEntrySetTextColumn (toComboBoxEntry combo) colId
  objectSetAttribute comboQuark (toComboBoxEntry combo) (Just store)
  return store

-- %hash c:b7d7 d:2818
-- | Sets the model column should be use to get strings from to
-- be @textColumn@.
--
comboBoxEntrySetTextColumn :: ComboBoxEntryClass self => self
 -> ColumnId row String -- ^ @textColumn@ - A column in @model@ to get the strings from.
 -> IO ()
comboBoxEntrySetTextColumn self textColumn =
  {# call gtk_combo_box_entry_set_text_column #}
    (toComboBoxEntry self)
    (fromIntegral (columnIdToNumber textColumn))

-- %hash c:a3e3 d:6441
-- | Returns the column which is used to get the strings from.
--
comboBoxEntryGetTextColumn :: ComboBoxEntryClass self => self
 -> IO (ColumnId row String) -- ^ returns A column in the data source model of @entryBox@.
comboBoxEntryGetTextColumn self =
  liftM (makeColumnIdString . fromIntegral) $
  {# call gtk_combo_box_entry_get_text_column #}
    (toComboBoxEntry self)

#if GTK_CHECK_VERSION(2,6,0)
-- | Retrieve the text currently in the entry.
--
-- * Returns @Nothing@ if no text is selected or entered.
--
-- * Availabe in Gtk 2.6 or higher.
--
comboBoxEntryGetActiveText :: ComboBoxEntryClass self => self
  -> IO (Maybe String)
comboBoxEntryGetActiveText self = do
  strPtr <- {# call gtk_combo_box_get_active_text #} (toComboBox self)
  if strPtr == nullPtr then return Nothing else liftM Just $
    peekUTFString (castPtr strPtr)
#endif

--------------------
-- Attributes

-- %hash c:84ff d:be07
-- | A column in the data source model to get the strings from.
--
-- Allowed values: >= 0
--
-- Default value: 'Graphics.UI.Gtk.ModelView.CustomStore.invalidColumnId'
--
comboBoxEntryTextColumn :: ComboBoxEntryClass self => Attr self (ColumnId row String)
comboBoxEntryTextColumn = newAttr
  comboBoxEntryGetTextColumn
  comboBoxEntrySetTextColumn

#endif