File: appwindow.rb

package info (click to toggle)
ruby-gnome2 0.15.0-1.1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 7,692 kB
  • ctags: 8,558
  • sloc: ansic: 69,912; ruby: 19,511; makefile: 97; xml: 35; sql: 13
file content (233 lines) | stat: -rw-r--r-- 7,477 bytes parent folder | download | duplicates (7)
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
229
230
231
232
233
# Copyright (c) 2003-2005 Ruby-GNOME2 Project Team
# This program is licenced under the same licence as Ruby-GNOME2.
#
# $Id: appwindow.rb,v 1.7 2005/03/05 15:01:16 mutoh Exp $
=begin
=  Application main window

Demonstrates a typical application window, with menubar, toolbar, statusbar.
=end

require 'common'

module Demo
  class AppWindow < BasicWindow
    def initialize
      register_stock_icons

      ## Create toplevel window
      super('Application Window')

      table = Gtk::Table.new(1, 4, false)
      add(table)

      ## Create the menubar
      accel_group = Gtk::AccelGroup.new
      add_accel_group(accel_group)

      item_factory = Gtk::ItemFactory.new(Gtk::ItemFactory::TYPE_MENU_BAR,
					  '<main>', accel_group)
      # create menu items
      menuitem_cb = proc do |data, widget|
	# TODO: Show item-factory's path to menu item
	dialog = Gtk::MessageDialog.new(self,
					Gtk::Dialog::DESTROY_WITH_PARENT,
					Gtk::MessageDialog::INFO,
					Gtk::MessageDialog::BUTTONS_CLOSE,
					"You selected or toggled the menu item \"#{Gtk::ItemFactory.path_from_widget(widget)}\"")
	
	# Close dialog on user response
	dialog.signal_connect('response') do |widget, data|
	  widget.destroy
	end
	
	dialog.show
      end

      menu_items = [
	["/_File"],
	["/_File/_New",
	  "<StockItem>", "<control>N", Gtk::Stock::NEW,  menuitem_cb],
	["/_File/_Open",
	  "<StockItem>", "<control>O", Gtk::Stock::OPEN, menuitem_cb],
	["/File/_Save",
	  "<StockItem>", "<control>S", Gtk::Stock::SAVE, menuitem_cb],
	["/File/Save _As...",
	  "<StockItem>", nil,          Gtk::Stock::SAVE, menuitem_cb],
	["/File/sep1",
	  "<Separator>", nil,          nil,              menuitem_cb],
	["/File/Quit",
	  "<StockItem>", "<control>Q", Gtk::Stock::QUIT, menuitem_cb],

	["/_Preferences"],
	["/_Preferences/_Color"],
	["/_Preferences/Color/_Red",
	  "<RadioItem>",            nil, nil, menuitem_cb],
	["/_Preferences/Color/_Green",
	  "/Preferences/Color/Red", nil, nil, menuitem_cb],
	["/_Preferences/Color/_Blue",
	  "/Preferences/Color/Red", nil, nil, menuitem_cb],
	
	["/Preferences/_Shape"],
	["/Preferences/Shape/_Square",
	  "<RadioItem>",                 nil, nil, menuitem_cb],
	["/Preferences/Shape/_Rectangle",
	  "/Preferences/Shape/Square",   nil, nil, menuitem_cb],
	["/Preferences/Shape/_Oval",
	  "/Preferences/Shape/Square",   nil, nil, menuitem_cb],

	# If you wanted this to be right justified you would use
	# "<LastBranch>", not "<Branch>".  Right justified help menu
	# items are generally considered a bad idea now days.
	["/_Help"],
	["/Help/_About", "<Item>", nil, nil, Proc.new{about_cb}],
      ]
      item_factory.create_items(menu_items)

      table.attach(item_factory.get_widget('<main>'),
		   # X direction            # Y direction
		   0, 1,                    0, 1,
		   Gtk::EXPAND | Gtk::FILL, 0,
		   0,                       0)

      ## Create the toolbar
      toolbar = Gtk::Toolbar.new
      # toolbar.set_toolbar_style(Gtk::Toolbar::BOTH)
      toolbar.append(Gtk::Stock::OPEN,
		     "This is a demo button with an 'open' icon"
		     ) do toolbar_cb end
      toolbar.append(Gtk::Stock::QUIT,
		     "This is a demo button with an 'quit' icon"
		     ) do toolbar_cb end
      toolbar.append_space
      toolbar.append(:demo_gtk_logo,
		     "This is a demo button with an 'gtk' icon"
		     ) do toolbar_cb end
      table.attach(toolbar,
		   # X direction            # Y direction
		   0, 1,                    1, 2,
		   Gtk::EXPAND | Gtk::FILL, 0,
		   0,                       0)

      ## Create document
      sw = Gtk::ScrolledWindow.new
      sw.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
      sw.shadow_type = Gtk::SHADOW_IN
      table.attach(sw,
		   # X direction            # Y direction
		   0, 1,                    2, 3,
		   Gtk::EXPAND | Gtk::FILL, Gtk::EXPAND | Gtk::FILL,
		   0,                       0)

      set_default_size(200, 200)

      contents = Gtk::TextView.new
      sw.add(contents)

      ## Create statusbar
      statusbar = Gtk::Statusbar.new
      table.attach(statusbar,
		   # X direction            # Y direction
		   0, 1,                    3, 4,
		   Gtk::EXPAND | Gtk::FILL, 0,
		   0,                       0)

      # Show text widget info in the statusbar
      buffer = contents.buffer
      buffer.signal_connect('changed') do |buffer|
	update_statusbar(buffer, statusbar)
      end
      # cursor moved
      buffer.signal_connect('mark_set') do |buffer, iter, mark|
	update_statusbar(buffer, statusbar)
      end
    end

    def toolbar_cb(*args)
      dialog = Gtk::MessageDialog.new(self,
				      Gtk::Dialog::DESTROY_WITH_PARENT,
				      Gtk::MessageDialog::INFO,
				      Gtk::MessageDialog::BUTTONS_CLOSE,
				      "You selected a toolbar button")

      # Close dialog on user response
      dialog.signal_connect('response') do |widget, data|
	widget.destroy
      end

      dialog.show
    end

    def about_cb(*args)
      unless Gtk.check_version?(2, 6, 0)
        puts "This sample requires GTK+ 2.6.0 or later"
        return
      end

      authors = ["Peter Mattis", "Spencer Kimball", "Josh MacDonald", "and many more..."]
      documentors = ["Owen Taylor", "Tony Gale", "Mattias Clasen <mclasen@redhat.com>",  
	"and many more..."]
      license = %Q[
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB.If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
]

      transparent = Gdk::Pixbuf.new(Demo.find_file("gtk-logo-rgb.gif")).add_alpha(true, 0xff, 0xff, 0xff)
      
      Gtk::AboutDialog.set_email_hook {|about, link|
	puts "send mail to #{link}"
      }
      Gtk::AboutDialog.set_url_hook {|about, link|
	puts "show url #{link}"
      }
      Gtk::AboutDialog.show(self,
			    :name => "GTK+ Code Demos",
			    :version => Gtk::VERSION.join("."),
			    :copyright => "(C) 1997-2004 The GTK+ Team",
			    :license => license,
			    :website => "http://www.gtk.org",
			    :comments => "Program to demonstrate GTK+ functions.",
			    :authors => authors,
			    :documenters => documentors,
			    :logo => transparent)
    end

    def update_statusbar(buffer, statusbar)
      statusbar.pop(0)

      iter = buffer.get_iter_at_mark(buffer.get_mark('insert'))

      statusbar.push(0,
		     "Cursor at row #{iter.line} column #{iter.line_offset} - #{buffer.char_count} chars in document")
    end

    def register_stock_icons
      # Register our stock items
      Gtk::Stock.add(:demo_gtk_logo, '_GTK!')

      # Add out custom icon factory to the list of defaults
      factory = Gtk::IconFactory.new
      factory.add_default

      filename = Demo.find_file('gtk-logo-rgb.gif')
      pixbuf = Gdk::Pixbuf.new(filename)

      transparent = pixbuf.add_alpha(true, 0xff, 0xff, 0xff)

      icon_set = Gtk::IconSet.new(transparent)
      factory.add('demo_gtk_logo', icon_set)
    end
  end
end