paulb@46 | 1 | #!/usr/bin/env python |
paulb@46 | 2 | |
paulb@46 | 3 | """ |
paulb@46 | 4 | Simple desktop dialogue box support for Python. |
paulb@46 | 5 | |
paul@68 | 6 | Copyright (C) 2007, 2009 Paul Boddie <paul@boddie.org.uk> |
paulb@46 | 7 | |
paul@68 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@68 | 9 | the terms of the GNU Lesser General Public License as published by the Free |
paul@68 | 10 | Software Foundation; either version 3 of the License, or (at your option) any |
paul@68 | 11 | later version. |
paulb@46 | 12 | |
paul@68 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@68 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@68 | 15 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paul@68 | 16 | details. |
paulb@46 | 17 | |
paul@68 | 18 | You should have received a copy of the GNU Lesser General Public License along |
paul@68 | 19 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@46 | 20 | |
paulb@46 | 21 | -------- |
paulb@46 | 22 | |
paulb@46 | 23 | Opening Dialogue Boxes (Dialogs) |
paulb@46 | 24 | -------------------------------- |
paulb@46 | 25 | |
paulb@46 | 26 | To open a dialogue box (dialog) in the current desktop environment, relying on |
paulb@46 | 27 | the automatic detection of that environment, use the appropriate dialogue box |
paulb@46 | 28 | class: |
paulb@46 | 29 | |
paulb@46 | 30 | question = desktop.dialog.Question("Are you sure?") |
paulb@57 | 31 | result = question.open() |
paulb@46 | 32 | |
paulb@46 | 33 | To override the detected desktop, specify the desktop parameter to the open |
paulb@46 | 34 | function as follows: |
paulb@46 | 35 | |
paulb@46 | 36 | question.open("KDE") # Insists on KDE |
paulb@46 | 37 | question.open("GNOME") # Insists on GNOME |
paulb@46 | 38 | |
paulb@46 | 39 | The dialogue box options are documented in each class's docstring. |
paulb@51 | 40 | |
paulb@51 | 41 | Available dialogue box classes are listed in the desktop.dialog.available |
paulb@51 | 42 | attribute. |
paulb@57 | 43 | |
paulb@57 | 44 | Supported desktop environments are listed in the desktop.dialog.supported |
paulb@57 | 45 | attribute. |
paulb@46 | 46 | """ |
paulb@46 | 47 | |
paulb@46 | 48 | from desktop import use_desktop, _run, _readfrom, _status |
paulb@46 | 49 | |
paulb@49 | 50 | class _wrapper: |
paulb@49 | 51 | def __init__(self, handler): |
paulb@49 | 52 | self.handler = handler |
paulb@49 | 53 | |
paulb@49 | 54 | class _readvalue(_wrapper): |
paulb@49 | 55 | def __call__(self, cmd, shell): |
paulb@49 | 56 | return self.handler(cmd, shell).strip() |
paulb@49 | 57 | |
paulb@49 | 58 | class _readinput(_wrapper): |
paulb@49 | 59 | def __call__(self, cmd, shell): |
paulb@49 | 60 | return self.handler(cmd, shell)[:-1] |
paulb@49 | 61 | |
paulb@49 | 62 | class _readvalues_kdialog(_wrapper): |
paulb@49 | 63 | def __call__(self, cmd, shell): |
paulb@49 | 64 | result = self.handler(cmd, shell).strip().strip('"') |
paulb@49 | 65 | if result: |
paulb@49 | 66 | return result.split('" "') |
paulb@49 | 67 | else: |
paulb@49 | 68 | return [] |
paulb@49 | 69 | |
paulb@49 | 70 | class _readvalues_zenity(_wrapper): |
paulb@49 | 71 | def __call__(self, cmd, shell): |
paulb@49 | 72 | result = self.handler(cmd, shell).strip() |
paulb@49 | 73 | if result: |
paulb@49 | 74 | return result.split("|") |
paulb@49 | 75 | else: |
paulb@49 | 76 | return [] |
paulb@49 | 77 | |
paulb@49 | 78 | class _readvalues_Xdialog(_wrapper): |
paulb@49 | 79 | def __call__(self, cmd, shell): |
paulb@49 | 80 | result = self.handler(cmd, shell).strip() |
paulb@49 | 81 | if result: |
paulb@49 | 82 | return result.split("/") |
paulb@49 | 83 | else: |
paulb@49 | 84 | return [] |
paulb@49 | 85 | |
paulb@46 | 86 | # Dialogue parameter classes. |
paulb@46 | 87 | |
paulb@46 | 88 | class String: |
paulb@46 | 89 | |
paulb@46 | 90 | "A generic parameter." |
paulb@46 | 91 | |
paulb@46 | 92 | def __init__(self, name): |
paulb@46 | 93 | self.name = name |
paulb@46 | 94 | |
paulb@46 | 95 | def convert(self, value, program): |
paulb@46 | 96 | return [value or ""] |
paulb@46 | 97 | |
paulb@46 | 98 | class Strings(String): |
paulb@46 | 99 | |
paulb@46 | 100 | "Multiple string parameters." |
paulb@46 | 101 | |
paulb@46 | 102 | def convert(self, value, program): |
paulb@46 | 103 | return value or [] |
paulb@46 | 104 | |
paulb@49 | 105 | class StringPairs(String): |
paulb@49 | 106 | |
paulb@49 | 107 | "Multiple string parameters duplicated to make identifiers." |
paulb@49 | 108 | |
paulb@49 | 109 | def convert(self, value, program): |
paulb@49 | 110 | l = [] |
paulb@49 | 111 | for v in value: |
paulb@49 | 112 | l.append(v) |
paulb@49 | 113 | l.append(v) |
paulb@49 | 114 | return l |
paulb@49 | 115 | |
paulb@46 | 116 | class StringKeyword: |
paulb@46 | 117 | |
paulb@46 | 118 | "A keyword parameter." |
paulb@46 | 119 | |
paulb@46 | 120 | def __init__(self, keyword, name): |
paulb@46 | 121 | self.keyword = keyword |
paulb@46 | 122 | self.name = name |
paulb@46 | 123 | |
paulb@46 | 124 | def convert(self, value, program): |
paulb@46 | 125 | return [self.keyword + "=" + (value or "")] |
paulb@46 | 126 | |
paulb@46 | 127 | class StringKeywords: |
paulb@46 | 128 | |
paulb@46 | 129 | "Multiple keyword parameters." |
paulb@46 | 130 | |
paulb@46 | 131 | def __init__(self, keyword, name): |
paulb@46 | 132 | self.keyword = keyword |
paulb@46 | 133 | self.name = name |
paulb@46 | 134 | |
paulb@46 | 135 | def convert(self, value, program): |
paulb@46 | 136 | l = [] |
paulb@46 | 137 | for v in value or []: |
paulb@46 | 138 | l.append(self.keyword + "=" + v) |
paulb@46 | 139 | return l |
paulb@46 | 140 | |
paulb@46 | 141 | class Integer(String): |
paulb@46 | 142 | |
paulb@46 | 143 | "An integer parameter." |
paulb@46 | 144 | |
paulb@46 | 145 | defaults = { |
paulb@46 | 146 | "width" : 40, |
paulb@46 | 147 | "height" : 15, |
paulb@46 | 148 | "list_height" : 10 |
paulb@46 | 149 | } |
paulb@49 | 150 | scale = 8 |
paulb@49 | 151 | |
paulb@49 | 152 | def __init__(self, name, pixels=0): |
paulb@49 | 153 | String.__init__(self, name) |
paulb@49 | 154 | if pixels: |
paulb@49 | 155 | self.factor = self.scale |
paulb@49 | 156 | else: |
paulb@49 | 157 | self.factor = 1 |
paulb@46 | 158 | |
paulb@46 | 159 | def convert(self, value, program): |
paulb@46 | 160 | if value is None: |
paulb@46 | 161 | value = self.defaults[self.name] |
paulb@49 | 162 | return [str(int(value) * self.factor)] |
paulb@46 | 163 | |
paulb@46 | 164 | class IntegerKeyword(Integer): |
paulb@46 | 165 | |
paulb@46 | 166 | "An integer keyword parameter." |
paulb@46 | 167 | |
paulb@49 | 168 | def __init__(self, keyword, name, pixels=0): |
paulb@49 | 169 | Integer.__init__(self, name, pixels) |
paulb@46 | 170 | self.keyword = keyword |
paulb@46 | 171 | |
paulb@46 | 172 | def convert(self, value, program): |
paulb@46 | 173 | if value is None: |
paulb@46 | 174 | value = self.defaults[self.name] |
paulb@49 | 175 | return [self.keyword + "=" + str(int(value) * self.factor)] |
paulb@46 | 176 | |
paulb@46 | 177 | class Boolean(String): |
paulb@46 | 178 | |
paulb@46 | 179 | "A boolean parameter." |
paulb@46 | 180 | |
paulb@46 | 181 | values = { |
paulb@46 | 182 | "kdialog" : ["off", "on"], |
paulb@46 | 183 | "zenity" : ["FALSE", "TRUE"], |
paulb@46 | 184 | "Xdialog" : ["off", "on"] |
paulb@46 | 185 | } |
paulb@46 | 186 | |
paulb@46 | 187 | def convert(self, value, program): |
paulb@46 | 188 | values = self.values[program] |
paulb@46 | 189 | if value: |
paulb@46 | 190 | return [values[1]] |
paulb@46 | 191 | else: |
paulb@46 | 192 | return [values[0]] |
paulb@46 | 193 | |
paulb@46 | 194 | class MenuItemList(String): |
paulb@46 | 195 | |
paulb@46 | 196 | "A menu item list parameter." |
paulb@46 | 197 | |
paulb@46 | 198 | def convert(self, value, program): |
paulb@46 | 199 | l = [] |
paulb@46 | 200 | for v in value: |
paulb@46 | 201 | l.append(v.value) |
paulb@46 | 202 | l.append(v.text) |
paulb@46 | 203 | return l |
paulb@46 | 204 | |
paulb@46 | 205 | class ListItemList(String): |
paulb@46 | 206 | |
paulb@47 | 207 | "A radiolist/checklist item list parameter." |
paulb@47 | 208 | |
paulb@47 | 209 | def __init__(self, name, status_first=0): |
paulb@47 | 210 | String.__init__(self, name) |
paulb@47 | 211 | self.status_first = status_first |
paulb@46 | 212 | |
paulb@46 | 213 | def convert(self, value, program): |
paulb@46 | 214 | l = [] |
paulb@46 | 215 | for v in value: |
paulb@47 | 216 | boolean = Boolean(None) |
paulb@47 | 217 | status = boolean.convert(v.status, program) |
paulb@47 | 218 | if self.status_first: |
paulb@47 | 219 | l += status |
paulb@46 | 220 | l.append(v.value) |
paulb@46 | 221 | l.append(v.text) |
paulb@47 | 222 | if not self.status_first: |
paulb@47 | 223 | l += status |
paulb@46 | 224 | return l |
paulb@46 | 225 | |
paulb@46 | 226 | # Dialogue argument values. |
paulb@46 | 227 | |
paulb@46 | 228 | class MenuItem: |
paulb@47 | 229 | |
paulb@47 | 230 | "A menu item which can also be used with radiolists and checklists." |
paulb@47 | 231 | |
paulb@47 | 232 | def __init__(self, value, text, status=0): |
paulb@46 | 233 | self.value = value |
paulb@46 | 234 | self.text = text |
paulb@46 | 235 | self.status = status |
paulb@46 | 236 | |
paulb@46 | 237 | # Dialogue classes. |
paulb@46 | 238 | |
paulb@46 | 239 | class Dialogue: |
paulb@46 | 240 | |
paulb@46 | 241 | commands = { |
paulb@46 | 242 | "KDE" : "kdialog", |
paulb@46 | 243 | "GNOME" : "zenity", |
paulb@57 | 244 | "XFCE" : "zenity", # NOTE: Based on observations with Xubuntu. |
paulb@46 | 245 | "X11" : "Xdialog" |
paulb@46 | 246 | } |
paulb@46 | 247 | |
paulb@46 | 248 | def open(self, desktop=None): |
paulb@46 | 249 | |
paulb@46 | 250 | """ |
paulb@46 | 251 | Open a dialogue box (dialog) using a program appropriate to the desktop |
paulb@46 | 252 | environment in use. |
paulb@46 | 253 | |
paulb@57 | 254 | If the optional 'desktop' parameter is specified then attempt to use |
paulb@57 | 255 | that particular desktop environment's mechanisms to open the dialog |
paulb@57 | 256 | instead of guessing or detecting which environment is being used. |
paulb@46 | 257 | |
paulb@57 | 258 | Suggested values for 'desktop' are "standard", "KDE", "GNOME", |
paulb@57 | 259 | "Mac OS X", "Windows". |
paulb@46 | 260 | |
paulb@46 | 261 | The result of the dialogue interaction may be a string indicating user |
paulb@57 | 262 | input (for Input, Password, Menu, Pulldown), a list of strings |
paulb@57 | 263 | indicating selections of one or more items (for RadioList, CheckList), |
paulb@57 | 264 | or a value indicating true or false (for Question, Warning, Message, |
paulb@57 | 265 | Error). |
paulb@58 | 266 | |
paulb@58 | 267 | Where a string value may be expected but no choice is made, an empty |
paulb@58 | 268 | string may be returned. Similarly, where a list of values is expected |
paulb@58 | 269 | but no choice is made, an empty list may be returned. |
paulb@46 | 270 | """ |
paulb@46 | 271 | |
paulb@46 | 272 | # Decide on the desktop environment in use. |
paulb@46 | 273 | |
paulb@46 | 274 | desktop_in_use = use_desktop(desktop) |
paulb@46 | 275 | |
paulb@46 | 276 | # Get the program. |
paulb@46 | 277 | |
paulb@46 | 278 | try: |
paulb@46 | 279 | program = self.commands[desktop_in_use] |
paulb@46 | 280 | except KeyError: |
paulb@46 | 281 | raise OSError, "Desktop '%s' not supported (no known dialogue box command could be suggested)" % desktop_in_use |
paulb@46 | 282 | |
paulb@57 | 283 | # The handler is one of the functions communicating with the subprocess. |
paulb@57 | 284 | # Some handlers return boolean values, others strings. |
paulb@57 | 285 | |
paulb@46 | 286 | handler, options = self.info[program] |
paulb@46 | 287 | |
paulb@46 | 288 | cmd = [program] |
paulb@46 | 289 | for option in options: |
paulb@46 | 290 | if isinstance(option, str): |
paulb@46 | 291 | cmd.append(option) |
paulb@46 | 292 | else: |
paulb@46 | 293 | value = getattr(self, option.name, None) |
paulb@46 | 294 | cmd += option.convert(value, program) |
paulb@46 | 295 | |
paulb@46 | 296 | return handler(cmd, 0) |
paulb@46 | 297 | |
paulb@46 | 298 | class Simple(Dialogue): |
paulb@46 | 299 | def __init__(self, text, width=None, height=None): |
paulb@46 | 300 | self.text = text |
paulb@46 | 301 | self.width = width |
paulb@46 | 302 | self.height = height |
paulb@46 | 303 | |
paulb@46 | 304 | class Question(Simple): |
paulb@46 | 305 | |
paulb@46 | 306 | """ |
paulb@46 | 307 | A dialogue asking a question and showing response buttons. |
paulb@46 | 308 | Options: text, width (in characters), height (in characters) |
paulb@57 | 309 | Response: a boolean value indicating an affirmative response (true) or a |
paulb@57 | 310 | negative response |
paulb@46 | 311 | """ |
paulb@46 | 312 | |
paulb@46 | 313 | name = "question" |
paulb@46 | 314 | info = { |
paulb@46 | 315 | "kdialog" : (_status, ["--yesno", String("text")]), |
paulb@46 | 316 | "zenity" : (_status, ["--question", StringKeyword("--text", "text")]), |
paulb@46 | 317 | "Xdialog" : (_status, ["--stdout", "--yesno", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 318 | } |
paulb@46 | 319 | |
paulb@46 | 320 | class Warning(Simple): |
paulb@46 | 321 | |
paulb@46 | 322 | """ |
paulb@46 | 323 | A dialogue asking a question and showing response buttons. |
paulb@46 | 324 | Options: text, width (in characters), height (in characters) |
paulb@57 | 325 | Response: a boolean value indicating an affirmative response (true) or a |
paulb@57 | 326 | negative response |
paulb@46 | 327 | """ |
paulb@46 | 328 | |
paulb@46 | 329 | name = "warning" |
paulb@46 | 330 | info = { |
paulb@46 | 331 | "kdialog" : (_status, ["--warningyesno", String("text")]), |
paulb@46 | 332 | "zenity" : (_status, ["--warning", StringKeyword("--text", "text")]), |
paulb@46 | 333 | "Xdialog" : (_status, ["--stdout", "--yesno", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 334 | } |
paulb@46 | 335 | |
paulb@46 | 336 | class Message(Simple): |
paulb@46 | 337 | |
paulb@46 | 338 | """ |
paulb@46 | 339 | A message dialogue. |
paulb@46 | 340 | Options: text, width (in characters), height (in characters) |
paulb@57 | 341 | Response: a boolean value indicating an affirmative response (true) or a |
paulb@57 | 342 | negative response |
paulb@46 | 343 | """ |
paulb@46 | 344 | |
paulb@46 | 345 | name = "message" |
paulb@46 | 346 | info = { |
paulb@46 | 347 | "kdialog" : (_status, ["--msgbox", String("text")]), |
paulb@46 | 348 | "zenity" : (_status, ["--info", StringKeyword("--text", "text")]), |
paulb@46 | 349 | "Xdialog" : (_status, ["--stdout", "--msgbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 350 | } |
paulb@46 | 351 | |
paulb@46 | 352 | class Error(Simple): |
paulb@46 | 353 | |
paulb@46 | 354 | """ |
paulb@46 | 355 | An error dialogue. |
paulb@46 | 356 | Options: text, width (in characters), height (in characters) |
paulb@57 | 357 | Response: a boolean value indicating an affirmative response (true) or a |
paulb@57 | 358 | negative response |
paulb@46 | 359 | """ |
paulb@46 | 360 | |
paulb@46 | 361 | name = "error" |
paulb@46 | 362 | info = { |
paulb@46 | 363 | "kdialog" : (_status, ["--error", String("text")]), |
paulb@46 | 364 | "zenity" : (_status, ["--error", StringKeyword("--text", "text")]), |
paulb@46 | 365 | "Xdialog" : (_status, ["--stdout", "--msgbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 366 | } |
paulb@46 | 367 | |
paulb@46 | 368 | class Menu(Simple): |
paulb@46 | 369 | |
paulb@46 | 370 | """ |
paulb@46 | 371 | A menu of options, one of which being selectable. |
paulb@46 | 372 | Options: text, width (in characters), height (in characters), |
paulb@57 | 373 | list_height (in items), items (MenuItem objects) |
paulb@57 | 374 | Response: a value corresponding to the chosen item |
paulb@46 | 375 | """ |
paulb@46 | 376 | |
paulb@46 | 377 | name = "menu" |
paulb@46 | 378 | info = { |
paulb@49 | 379 | "kdialog" : (_readvalue(_readfrom), ["--menu", String("text"), MenuItemList("items")]), |
paulb@49 | 380 | "zenity" : (_readvalue(_readfrom), ["--list", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@46 | 381 | MenuItemList("items")] |
paulb@46 | 382 | ), |
paulb@49 | 383 | "Xdialog" : (_readvalue(_readfrom), ["--stdout", "--menubox", |
paulb@46 | 384 | String("text"), Integer("height"), Integer("width"), Integer("list_height"), MenuItemList("items")] |
paulb@46 | 385 | ), |
paulb@46 | 386 | } |
paulb@47 | 387 | item = MenuItem |
paulb@49 | 388 | number_of_titles = 2 |
paulb@46 | 389 | |
paulb@47 | 390 | def __init__(self, text, titles, items=None, width=None, height=None, list_height=None): |
paulb@47 | 391 | |
paulb@47 | 392 | """ |
paulb@47 | 393 | Initialise a menu with the given heading 'text', column 'titles', and |
paulb@47 | 394 | optional 'items' (which may be added later), 'width' (in characters), |
paulb@47 | 395 | 'height' (in characters) and 'list_height' (in items). |
paulb@47 | 396 | """ |
paulb@47 | 397 | |
paulb@46 | 398 | Simple.__init__(self, text, width, height) |
paulb@49 | 399 | self.titles = ([""] * self.number_of_titles + titles)[-self.number_of_titles:] |
paulb@47 | 400 | self.items = items or [] |
paulb@46 | 401 | self.list_height = list_height |
paulb@46 | 402 | |
paulb@47 | 403 | def add(self, *args, **kw): |
paulb@47 | 404 | |
paulb@47 | 405 | """ |
paulb@47 | 406 | Add an item, passing the given arguments to the appropriate item class. |
paulb@47 | 407 | """ |
paulb@47 | 408 | |
paulb@47 | 409 | self.items.append(self.item(*args, **kw)) |
paulb@47 | 410 | |
paulb@46 | 411 | class RadioList(Menu): |
paulb@46 | 412 | |
paulb@46 | 413 | """ |
paulb@46 | 414 | A list of radio buttons, one of which being selectable. |
paulb@46 | 415 | Options: text, width (in characters), height (in characters), |
paulb@57 | 416 | list_height (in items), items (MenuItem objects), titles |
paulb@57 | 417 | Response: a list of values corresponding to chosen items (since some |
paulb@57 | 418 | programs, eg. zenity, appear to support multiple default |
paulb@57 | 419 | selections) |
paulb@46 | 420 | """ |
paulb@46 | 421 | |
paulb@46 | 422 | name = "radiolist" |
paulb@46 | 423 | info = { |
paulb@49 | 424 | "kdialog" : (_readvalues_kdialog(_readfrom), ["--radiolist", String("text"), ListItemList("items")]), |
paulb@49 | 425 | "zenity" : (_readvalues_zenity(_readfrom), |
paulb@46 | 426 | ["--list", "--radiolist", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@47 | 427 | ListItemList("items", 1)] |
paulb@46 | 428 | ), |
paulb@49 | 429 | "Xdialog" : (_readvalues_Xdialog(_readfrom), ["--stdout", "--radiolist", |
paulb@46 | 430 | String("text"), Integer("height"), Integer("width"), Integer("list_height"), ListItemList("items")] |
paulb@46 | 431 | ), |
paulb@46 | 432 | } |
paulb@49 | 433 | number_of_titles = 3 |
paulb@46 | 434 | |
paulb@46 | 435 | class CheckList(Menu): |
paulb@46 | 436 | |
paulb@46 | 437 | """ |
paulb@46 | 438 | A list of checkboxes, many being selectable. |
paulb@46 | 439 | Options: text, width (in characters), height (in characters), |
paulb@57 | 440 | list_height (in items), items (MenuItem objects), titles |
paulb@57 | 441 | Response: a list of values corresponding to chosen items |
paulb@46 | 442 | """ |
paulb@46 | 443 | |
paulb@46 | 444 | name = "checklist" |
paulb@46 | 445 | info = { |
paulb@49 | 446 | "kdialog" : (_readvalues_kdialog(_readfrom), ["--checklist", String("text"), ListItemList("items")]), |
paulb@49 | 447 | "zenity" : (_readvalues_zenity(_readfrom), |
paulb@46 | 448 | ["--list", "--checklist", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@47 | 449 | ListItemList("items", 1)] |
paulb@46 | 450 | ), |
paulb@49 | 451 | "Xdialog" : (_readvalues_Xdialog(_readfrom), ["--stdout", "--checklist", |
paulb@46 | 452 | String("text"), Integer("height"), Integer("width"), Integer("list_height"), ListItemList("items")] |
paulb@46 | 453 | ), |
paulb@46 | 454 | } |
paulb@49 | 455 | number_of_titles = 3 |
paulb@46 | 456 | |
paulb@46 | 457 | class Pulldown(Menu): |
paulb@46 | 458 | |
paulb@46 | 459 | """ |
paulb@46 | 460 | A pull-down menu of options, one of which being selectable. |
paulb@46 | 461 | Options: text, width (in characters), height (in characters), |
paulb@57 | 462 | items (list of values) |
paulb@57 | 463 | Response: a value corresponding to the chosen item |
paulb@46 | 464 | """ |
paulb@46 | 465 | |
paulb@46 | 466 | name = "pulldown" |
paulb@46 | 467 | info = { |
paulb@49 | 468 | "kdialog" : (_readvalue(_readfrom), ["--combobox", String("text"), Strings("items")]), |
paulb@49 | 469 | "zenity" : (_readvalue(_readfrom), |
paulb@49 | 470 | ["--list", "--radiolist", StringKeyword("--text", "text"), StringKeywords("--column", "titles"), |
paulb@49 | 471 | StringPairs("items")] |
paulb@46 | 472 | ), |
paulb@49 | 473 | "Xdialog" : (_readvalue(_readfrom), |
paulb@49 | 474 | ["--stdout", "--combobox", String("text"), Integer("height"), Integer("width"), Strings("items")]), |
paulb@46 | 475 | } |
paulb@49 | 476 | item = unicode |
paulb@49 | 477 | number_of_titles = 2 |
paulb@46 | 478 | |
paulb@46 | 479 | class Input(Simple): |
paulb@46 | 480 | |
paulb@46 | 481 | """ |
paulb@46 | 482 | An input dialogue, consisting of an input field. |
paulb@49 | 483 | Options: text, input, width (in characters), height (in characters) |
paulb@57 | 484 | Response: the text entered into the dialogue by the user |
paulb@46 | 485 | """ |
paulb@46 | 486 | |
paulb@46 | 487 | name = "input" |
paulb@46 | 488 | info = { |
paulb@49 | 489 | "kdialog" : (_readinput(_readfrom), |
paulb@49 | 490 | ["--inputbox", String("text"), String("data")]), |
paulb@49 | 491 | "zenity" : (_readinput(_readfrom), |
paulb@49 | 492 | ["--entry", StringKeyword("--text", "text"), StringKeyword("--entry-text", "data")]), |
paulb@49 | 493 | "Xdialog" : (_readinput(_readfrom), |
paulb@49 | 494 | ["--stdout", "--inputbox", String("text"), Integer("height"), Integer("width"), String("data")]), |
paulb@46 | 495 | } |
paulb@46 | 496 | |
paulb@49 | 497 | def __init__(self, text, data="", width=None, height=None): |
paulb@46 | 498 | Simple.__init__(self, text, width, height) |
paulb@46 | 499 | self.data = data |
paulb@46 | 500 | |
paulb@46 | 501 | class Password(Input): |
paulb@46 | 502 | |
paulb@46 | 503 | """ |
paulb@46 | 504 | A password dialogue, consisting of a password entry field. |
paulb@49 | 505 | Options: text, width (in characters), height (in characters) |
paulb@57 | 506 | Response: the text entered into the dialogue by the user |
paulb@46 | 507 | """ |
paulb@46 | 508 | |
paulb@46 | 509 | name = "password" |
paulb@46 | 510 | info = { |
paulb@49 | 511 | "kdialog" : (_readinput(_readfrom), |
paulb@49 | 512 | ["--password", String("text")]), |
paulb@49 | 513 | "zenity" : (_readinput(_readfrom), |
paulb@49 | 514 | ["--entry", StringKeyword("--text", "text"), "--hide-text"]), |
paulb@49 | 515 | "Xdialog" : (_readinput(_readfrom), |
paulb@49 | 516 | ["--stdout", "--password", "--inputbox", String("text"), Integer("height"), Integer("width")]), |
paulb@46 | 517 | } |
paulb@46 | 518 | |
paulb@46 | 519 | class TextFile(Simple): |
paulb@46 | 520 | |
paulb@46 | 521 | """ |
paulb@46 | 522 | A text file input box. |
paulb@49 | 523 | Options: filename, text, width (in characters), height (in characters) |
paulb@58 | 524 | Response: any text returned by the dialogue program (typically an empty |
paulb@58 | 525 | string) |
paulb@46 | 526 | """ |
paulb@46 | 527 | |
paulb@46 | 528 | name = "textfile" |
paulb@46 | 529 | info = { |
paulb@49 | 530 | "kdialog" : (_readfrom, ["--textbox", String("filename"), Integer("width", pixels=1), Integer("height", pixels=1)]), |
paulb@49 | 531 | "zenity" : (_readfrom, ["--text-info", StringKeyword("--filename", "filename"), IntegerKeyword("--width", "width", pixels=1), |
paulb@49 | 532 | IntegerKeyword("--height", "height", pixels=1)] |
paulb@46 | 533 | ), |
paulb@49 | 534 | "Xdialog" : (_readfrom, ["--stdout", "--textbox", String("filename"), Integer("height"), Integer("width")]), |
paulb@46 | 535 | } |
paulb@46 | 536 | |
paulb@49 | 537 | def __init__(self, filename, text="", width=None, height=None): |
paulb@46 | 538 | Simple.__init__(self, text, width, height) |
paulb@46 | 539 | self.filename = filename |
paulb@46 | 540 | |
paulb@47 | 541 | # Available dialogues. |
paulb@47 | 542 | |
paulb@47 | 543 | available = [Question, Warning, Message, Error, Menu, CheckList, RadioList, Input, Password, Pulldown, TextFile] |
paulb@47 | 544 | |
paulb@57 | 545 | # Supported desktop environments. |
paulb@57 | 546 | |
paulb@57 | 547 | supported = Dialogue.commands.keys() |
paulb@57 | 548 | |
paulb@46 | 549 | # vim: tabstop=4 expandtab shiftwidth=4 |