~ubuntu-branches/ubuntu/trusty/rgtk2/trusty

« back to all changes in this revision

Viewing changes to demo/searchentry.R

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2010-11-03 11:35:46 UTC
  • mfrom: (1.3.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20101103113546-a7fi7jdxdebp0tw1
Tags: 2.20.1-1
* New upstream release

* debian/control: Set (Build-)Depends: to current R version
* debian/control: Set Standards-Version: to current version 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## icon and progress in a GtkEntry
 
2
 
 
3
search_progress_id <- 0
 
4
finish_search_id <- 0
 
5
 
 
6
show_find_button <- function()
 
7
{
 
8
  notebook$setCurrentPage(0)
 
9
}
 
10
 
 
11
show_cancel_button <- function()
 
12
{
 
13
  notebook$setCurrentPage(1)
 
14
}
 
15
 
 
16
search_progress <- function(data)
 
17
{
 
18
  data$progressPulse()
 
19
  return(TRUE)
 
20
}
 
21
 
 
22
search_progress_done <- function(entry)
 
23
{
 
24
  entry$setProgressFraction(0.0)
 
25
}
 
26
 
 
27
finish_search <- function(entry)
 
28
{
 
29
  show_find_button()
 
30
  
 
31
  gSourceRemove(search_progress_id)
 
32
  search_progress_id <<- 0
 
33
  search_progress_done(entry)
 
34
  
 
35
  return(FALSE)
 
36
}
 
37
 
 
38
start_search_feedback <- function(data)
 
39
{
 
40
  search_progress_id <<- gTimeoutAdd(100, search_progress, data)
 
41
  return(FALSE)
 
42
}
 
43
 
 
44
start_search <- function(button, entry)
 
45
{
 
46
  show_cancel_button()
 
47
  search_progress_id <<- gTimeoutAdd(1000, start_search_feedback, entry)
 
48
  finish_search_id <<- gTimeoutAdd(15000, finish_search, entry)
 
49
}
 
50
 
 
51
stop_search <- function(entry, data) {
 
52
  gSourceRemove(finish_search_id)
 
53
  finish_search(entry)
 
54
}
 
55
 
 
56
clear_entry <- function(entry)
 
57
{
 
58
  entry$setText("")
 
59
}
 
60
 
 
61
search_by_name <-function(item, entry)
 
62
{
 
63
  entry$setIconFromStock(GtkEntryIconPosition["primary"], GTK_STOCK_FIND)
 
64
  entry$setIconTooltipText(GtkEntryIconPosition["primary"],
 
65
                           "Search by name\nClick here to change the search type")
 
66
}
 
67
 
 
68
search_by_description <- function(item, entry)
 
69
{
 
70
  entry$setIconFromStock(GtkEntryIconPosition["primary"], GTK_STOCK_EDIT)
 
71
  entry$setIconTooltipText(GtkEntryIconPosition["primary"],
 
72
                           "Search by description\nClick here to change the search type")
 
73
}
 
74
 
 
75
search_by_file <- function(item, entry)
 
76
{
 
77
 
 
78
  entry$setIconFromStock(GtkEntryIconPosition["primary"], GTK_STOCK_OPEN)
 
79
  entry$setIconTooltipText(GtkEntryIconPosition["primary"],
 
80
                           "Search by file name\nClick here to change the search type")
 
81
}
 
82
 
 
83
create_search_menu <- function(entry)
 
84
{
 
85
  menu <- gtkMenu()
 
86
 
 
87
  item <- gtkImageMenuItemNewWithMnemonic("Search by _name")
 
88
  image <- gtkImage(stock = GTK_STOCK_FIND, size = GtkIconSize["menu"])
 
89
  item$setImage(image)
 
90
  item$setAlwaysShowImage(TRUE)
 
91
  gSignalConnect(item, "activate", search_by_name, entry)
 
92
  menu$append(item)
 
93
 
 
94
  item <- gtkImageMenuItemNewWithMnemonic("Search by _description")
 
95
  image <- gtkImage(stock = GTK_STOCK_EDIT, size = GtkIconSize["menu"])
 
96
  item$setImage(image)
 
97
  item$setAlwaysShowImage(TRUE)
 
98
  gSignalConnect(item, "activate", search_by_description, entry)
 
99
  menu$append(item)
 
100
 
 
101
  item <- gtkImageMenuItemNewWithMnemonic("Search by _file name")
 
102
  image <- gtkImage(stock = GTK_STOCK_OPEN, size = GtkIconSize["menu"])
 
103
  item$setImage(image)
 
104
  item$setAlwaysShowImage(TRUE)
 
105
  gSignalConnect(item, "activate", search_by_file, entry)
 
106
  menu$append(item)
 
107
 
 
108
  menu$showAll()
 
109
  return(menu)
 
110
}
 
111
 
 
112
icon_press_cb <- function(entry, position, event, data)
 
113
{
 
114
  if (position == GtkEntryIconPosition["primary"])
 
115
    menu$popup(NULL, NULL, NULL, NULL, event[["button"]], event[["time"]])
 
116
  else clear_entry(entry)
 
117
}
 
118
 
 
119
text_changed_cb <- function(entry, pspec, button)
 
120
{
 
121
  has_text <- entry$getTextLength() > 0
 
122
  entry$setIconSensitive(GtkEntryIconPosition["secondary"], has_text)
 
123
  button["sensitive"] <- has_text
 
124
}
 
125
 
 
126
activate_cb <- function(entry, button)
 
127
{
 
128
  if (search_progress_id != 0)
 
129
    return()
 
130
  start_search(button, entry)
 
131
}
 
132
 
 
133
search_entry_destroyed <- function(widget)
 
134
{
 
135
  if (finish_search_id != 0)
 
136
    gSourceRemove(finish_search_id)
 
137
 
 
138
  if (search_progress_id != 0)
 
139
    gSourceRemove(search_progress_id)
 
140
 
 
141
  window <- NULL
 
142
}
 
143
 
 
144
entry_populate_popup <- function(entry, menu, user_data)
 
145
{
 
146
  has_text <- entry$getTextLength() > 0
 
147
 
 
148
  item <- gtkSeparatorMenuItem()
 
149
  menu$append(item)
 
150
 
 
151
  item <- gtkMenuItemNewWithMnemonic("C_lear")
 
152
  gSignalConnect(item, "activate", clear_entry, entry, user.data.first=TRUE)
 
153
  menu$append(item)
 
154
  item["sensitive"] <- has_text
 
155
 
 
156
  search_menu <- create_search_menu(entry)
 
157
  item <- gtkMenuItem("Search by")
 
158
  item$setSubmenu(search_menu)
 
159
  menu$append(item)
 
160
}
 
161
 
 
162
window <- gtkDialog("Search Entry", NULL, 0, GTK_STOCK_CLOSE,
 
163
                    GtkResponseType["none"])
 
164
window["resizable"] <- FALSE
 
165
 
 
166
gSignalConnect(window, "response", gtkWidgetDestroy)
 
167
 
 
168
content_area <- window$getContentArea()
 
169
 
 
170
vbox <- gtkVBox(FALSE, 5)
 
171
content_area$packStart(vbox)
 
172
vbox$setBorderWidth(5)
 
173
 
 
174
label <- gtkLabel()
 
175
label$setMarkup("Search entry demo")
 
176
vbox$packStart(label, FALSE, FALSE)
 
177
 
 
178
hbox <- gtkHBox(FALSE, 10)
 
179
vbox$packStart(hbox)
 
180
vbox$setBorderWidth(0)
 
181
 
 
182
## Create our entry
 
183
entry <- gtkEntry()
 
184
hbox$packStart(entry, FALSE, FALSE)
 
185
 
 
186
## Create the find and cancel buttons
 
187
notebook <- gtkNotebook()
 
188
notebook$setShowTabs(FALSE)
 
189
notebook$setShowBorder(FALSE)
 
190
hbox$packStart(notebook, FALSE, FALSE)
 
191
 
 
192
find_button <- gtkButton("Find")
 
193
gSignalConnect(find_button, "clicked", start_search, entry)
 
194
notebook$appendPage(find_button)
 
195
 
 
196
cancel_button <- gtkButton("Cancel")
 
197
gSignalConnect(cancel_button, "clicked", stop_search, entry,
 
198
               user.data.first=TRUE)
 
199
notebook$appendPage(cancel_button)
 
200
 
 
201
## Set up the search icon
 
202
search_by_name(NULL, entry)
 
203
 
 
204
## Set up the clear icon
 
205
entry$setIconFromStock(GtkEntryIconPosition["secondary"], GTK_STOCK_CLEAR)
 
206
text_changed_cb (entry, NULL, find_button)
 
207
 
 
208
gSignalConnect(entry, "icon-press", icon_press_cb)
 
209
gSignalConnect(entry, "notify::text", text_changed_cb, find_button)
 
210
gSignalConnect(entry, "activate", activate_cb)
 
211
 
 
212
## Create the menu 
 
213
menu <- create_search_menu(entry)
 
214
menu$attachToWidget(entry)
 
215
 
 
216
## add accessible alternatives for icon functionality
 
217
gSignalConnect(entry, "populate-popup", entry_populate_popup)