Imported Upstream version 3.2.2
[debian/gnuradio] / grc / gui / Dialogs.py
1 """
2 Copyright 2008, 2009 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 """
19
20 import pygtk
21 pygtk.require('2.0')
22 import gtk
23 import Preferences
24 import Utils
25
26 class TextDisplay(gtk.TextView):
27         """A non editable gtk text view."""
28
29         def __init__(self, text=''):
30                 """
31                 TextDisplay constructor.
32                 @param text the text to display (string)
33                 """
34                 text_buffer = gtk.TextBuffer()
35                 text_buffer.set_text(text)
36                 self.set_text = text_buffer.set_text
37                 self.insert = lambda line: text_buffer.insert(text_buffer.get_end_iter(), line)
38                 gtk.TextView.__init__(self, text_buffer)
39                 self.set_editable(False)
40                 self.set_cursor_visible(False)
41                 self.set_wrap_mode(gtk.WRAP_WORD_CHAR)
42
43 def MessageDialogHelper(type, buttons, title=None, markup=None):
44         """
45         Create a modal message dialog and run it.
46         @param type the type of message: gtk.MESSAGE_INFO, gtk.MESSAGE_WARNING, gtk.MESSAGE_QUESTION or gtk.MESSAGE_ERROR
47         @param buttons the predefined set of buttons to use:
48                 gtk.BUTTONS_NONE, gtk.BUTTONS_OK, gtk.BUTTONS_CLOSE, gtk.BUTTONS_CANCEL, gtk.BUTTONS_YES_NO, gtk.BUTTONS_OK_CANCEL
49         @param tittle the title of the window (string)
50         @param markup the message text with pango markup
51         @return the gtk response from run()
52         """
53         message_dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, type, buttons)
54         if title: message_dialog.set_title(title)
55         if markup: message_dialog.set_markup(markup)
56         response = message_dialog.run()
57         message_dialog.destroy()
58         return response
59
60 class AboutDialog(gtk.AboutDialog):
61         """A cute little about dialog."""
62
63         def __init__(self, platform):
64                 """AboutDialog constructor."""
65                 gtk.AboutDialog.__init__(self)
66                 self.set_name(platform.get_name())
67                 self.set_version(platform.get_version())
68                 self.set_license(platform.get_license())
69                 self.set_copyright(platform.get_license().splitlines()[0])
70                 self.set_website(platform.get_website())
71                 self.run()
72                 self.destroy()
73
74 def HelpDialog(): MessageDialogHelper(
75         type=gtk.MESSAGE_INFO,
76         buttons=gtk.BUTTONS_CLOSE,
77         title='Help',
78         markup="""\
79 <b>Usage Tips</b>
80
81 <u>Add block</u>: drag and drop or double click a block in the block selection window.
82 <u>Rotate block</u>: Select a block, press left/right on the keyboard.
83 <u>Change type</u>: Select a block, press up/down on the keyboard.
84 <u>Edit parameters</u>: double click on a block in the flow graph.
85 <u>Make connection</u>: click on the source port of one block, then click on the sink port of another block.
86 <u>Remove connection</u>: select the connection and press delete, or drag the connection.
87
88 * See the menu for other keyboard shortcuts.""")
89
90 COLORS_DIALOG_MARKUP_TMPL = """\
91 <b>Color Mapping</b>
92
93 #if $colors
94         #set $max_len = max([len(color[0]) for color in $colors]) + 10
95         #for $title, $color_spec in $colors
96 <span background="$color_spec"><tt>$($encode($title).center($max_len))</tt></span>
97         #end for
98 #end if
99 """
100
101 def ColorsDialog(platform): MessageDialogHelper(
102         type=gtk.MESSAGE_INFO,
103         buttons=gtk.BUTTONS_CLOSE,
104         title='Colors',
105         markup=Utils.parse_template(COLORS_DIALOG_MARKUP_TMPL, colors=platform.get_colors()))