[Fxruby-users] Call for help :)
Status: Inactive
Brought to you by:
lyle
|
From: Hal F. <ha...@hy...> - 2004-03-04 11:13:50
|
I'm having trouble dynamically displaying child windows in a parent
window.
The (ugly pre-alpha) code is shown at the bottom of this email -- see
the last 20 lines, where comments say "this works" and "this doesn't."
The code depends on a tree of subdirs existing under exper/data. If you
want to build some random data for that, here's some code (tweaking it
will make it smaller than the approx. 6000 nodes and 4 meg I have).
Thanks for any assistance...
Hal
#################################################
# Build some fake data
root = "exper/data"
NumRange = [0, 5, 10, 15, 60, 70, 80, 100, 200, 300]
DirRange = [0, 1, 1, 1, 2, 2, 3 ]
TopRange = [5, 5, 10, 10, 20, 20, 100]
LeafProb = 0.25
LenRange = [20]*8 + [100]*6 + [300]*4 + [800]*2 + [1600]*1
Alpha = ("a".."z").to_a + ["\n"]
def add_files
many = NumRange[rand(NumRange.length)]
name = "file001"
1.upto(many) do |i|
size = LenRange[rand(LenRange.length)]
file = File.open(name,"w")
1.upto(size) do |char|
file.print(Alpha[rand(26)])
end
file.close
name.succ!
end
end
def add_dirs(depth)
name = "dir001"
range = DirRange
many = range[rand(range.length)]
dirs = []
1.upto(many) do |i|
dirs << name.dup
name.succ!
end
dirs
end
def filldir(depth,name)
Dir.mkdir(name)
Dir.chdir(name) do
add_files
return if depth > 1
dirs = add_dirs(depth)
dirs.each {|dir| filldir(depth+1,dir) }
end
end
topdirs = ("aa".."ap").to_a
Dir.chdir(root)
topdirs.each do |td|
filldir(0,td)
end
############################################################
# The app itself
require "fox"
require "yaml"
include Fox
module Fox
class FXTreeList
alias :expand :expandTree
alias :add :addItemLast
end
end
FillXY = LAYOUT_FILL_X|LAYOUT_FILL_Y
Sunken_FillXY = FRAME_SUNKEN|LAYOUT_FILL_X|LAYOUT_FILL_Y
CenterXY_FixHW = LAYOUT_CENTER_X|LAYOUT_CENTER_Y|
LAYOUT_FIX_WIDTH|LAYOUT_FIX_HEIGHT
CMD_ENABLE = MKUINT(FXWindow::ID_ENABLE, SEL_COMMAND)
CMD_DISABLE = MKUINT(FXWindow::ID_DISABLE, SEL_COMMAND)
CMD_QUIT = MKUINT(FXApp::ID_QUIT, SEL_COMMAND)
class Tree
attr_accessor :topic, :leaf, :subtrees, :notes, :dir, :title
@@nodes = 0
@@leaves = 0
def initialize(topic)
@topic = topic
@subtrees = []
@leaf = true # ??
@notes = []
@dir = topic.dir
@title = topic.title
end
def add_child(item,note=nil)
@@nodes += 1
if note.nil?
file = "#{item.dir}/index.yaml"
hash = File.open(file) {|f| ::YAML.load(f) } rescue {'title' =>
item.dir}
item.title = hash['title']
@subtrees << Tree.load(item)
@leaf = false
else
@notes << note
@leaf = true
@@leaves += 1
end
end
def Tree.load(topic)
tree = Tree.new(topic) # will return this object
Dir.chdir(topic.dir) do
entries = Dir.entries(".") - %w[. ..]
entries.each do |x|
if File.directory?(x)
tree.add_child(Topic.new(x))
else
tree.add_child(x,File.read(x))
end
end
end
tree
end
def each(&b)
#if b
b.call self
@subtrees.each {|sub| sub.each(&b) }
#end
end
def out
puts self
self.subtrees do |sub|
sub.out {|x| puts x }
end
end
def printdata
end
def to_a
temp = [self]
self.subtrees.each do |sub|
temp << sub.to_a
end
temp
end
def leaf?
@leaf
end
def nodes
@@nodes
end
def leaves
@@leaves
end
def inspect
self.topic.dir
end
def to_s
inspect
end
def to_str
inspect
end
end
class Topic
attr_accessor :dir, :title
def initialize(dir)
@dir = dir
@title = nil # ?
# @index = File.open("#{dir}/Index.yaml") {|f| ::YAML.load(f) }
rescue []
end
def to_str
@dir
end
def inspect
"#@dir:#@title"
end
end
class MyMain < FXMainWindow
attr_reader :right, :tree
def mktree(treetop,list)
ntop = nil
first = list.shift
node = @tree.add(treetop,first.to_str)
node.data = first
list.each do |item|
mktree(node, item)
end
end
def initialize(app)
# Initialize base class
super(app,"Tycho",nil,nil,DECOR_ALL)
# Main window layout
menubar = FXMenubar.new(self, LAYOUT_SIDE_TOP|LAYOUT_FILL_X)
@buttons = FXHorizontalFrame.new(self)
@rest = FXHorizontalFrame.new(self,FillXY)
@split = FXSplitter.new(@rest, Sunken_FillXY)
@left = FXVerticalFrame.new(@split,Sunken_FillXY, 0,0,0,0, 0,0,0,0)
# @right = FXVerticalFrame.new(@split,Sunken_FillXY)
@right = FXMDIClient.new(@split)
@right.height = 400
@right.width = 600
# Make MDI Menu
@mdimenu = FXMDIMenu.new(self, @right)
# MDI buttons in menu:- note the message ID's!!!!!
# Normally, MDI commands are simply sensitized or desensitized;
# Under the menubar, however, they're hidden if the MDI Client is
# not maximized. To do this, they must have different ID's.
FXMDIWindowButton.new(menubar, @right, FXMDIClient::ID_MDI_MENUWINDOW,
LAYOUT_LEFT)
FXMDIDeleteButton.new(menubar, @right, FXMDIClient::ID_MDI_MENUCLOSE,
FRAME_RAISED|LAYOUT_RIGHT)
FXMDIRestoreButton.new(menubar, @right,
FXMDIClient::ID_MDI_MENURESTORE,
FRAME_RAISED|LAYOUT_RIGHT)
FXMDIMinimizeButton.new(menubar, @right,
FXMDIClient::ID_MDI_MENUMINIMIZE, FRAME_RAISED|LAYOUT_RIGHT)
self.connect(SEL_UPDATE) {
unless @right.activeChild.nil?
self.title = @right.activeChild.title
else
self.title = "Tycho"
end
}
@tree = FXTreeList.new(@left, 0, nil, 0, (FRAME_SUNKEN|FRAME_THICK|
LAYOUT_FILL_X|LAYOUT_FILL_Y|LAYOUT_TOP|LAYOUT_RIGHT|TREELIST_SHOWS_LINES|
TREELIST_SHOWS_BOXES|TREELIST_ROOT_BOXES|TREELIST_EXTENDEDSELECT))
treetop = @tree.add(nil, "Tycho datastore")
@tree.expand(treetop)
@tree.disableItem(treetop)
cant_select = [treetop]
root = Topic.new("exper/data")
datatree = Tree.load(root)
data = datatree.subtrees.to_a
data.each do |item|
mktree(treetop, item.to_a)
end
@btnQuit = FXButton.new(@buttons,"Quit")
@btnQuit.connect(SEL_COMMAND) do
getApp().handle(@btnQuit, CMD_QUIT, nil)
end
@btnQuit.connect(SEL_KEYPRESS) do |sender, sel, event|
if event.code == KEY_Return || event.code == KEY_KP_Enter
getApp().handle(@btnQuit, CMD_QUIT, nil)
end
end
@btnQuit.setLayoutHints(LAYOUT_CENTER_X)
end
def create
super # Do base-class create first
# Resize the left pane so that it's wide enough
@left.width = 1.1*@tree.contentWidth
self.height = 480
self.width = 680
# Now show the main window
show(PLACEMENT_SCREEN)
end
end
class Tycho
attr_reader :main, :app
def initialize
super
# Now init Fox...
@app = FXApp.new("Tycho","myself")
@app.init(ARGV)
@main = MyMain.new(@app)
# @app.create
# @app.run
end
end
pim = Tycho.new
def show_notes(notes,window)
off = 30 # Magic number! bad coding style
notes.each_with_index do |note,i|
box = FXMDIChild.new(window,note,nil,nil,MDI_NORMAL,0,off*i,0,0)
FXText.new(box)
box.show
box.position(10,off*i+6,100,40)
end
window.recalc
end
strs = ["This is a note",
"This is another note",
"These notes are little pieces of information",
"Such as code snippets, phone numbers,",
"addresses, quotations, URLs, and...",
"other such random stuff.",
"Unlike this corny example, they will usually",
"not be related directly to each other.",
"This PIM is ultimately supposed to be a full-featured program",
"reminiscent of Info Select (www.miclog.com)."]
## This piece of code works
# show_notes(strs,pim.main.right)
FXButton.new(pim.main.right,"Blah")
pim.main.right.show
## This one doesn't
pim.main.tree.connect(SEL_COMMAND) do |sender,sel,ptr|
show_notes(ptr.data.notes,pim.main.right)
pim.main.right.recalc
end
pim.app.create
pim.app.run
|