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