Howto:Tabbed file dialog

From FlightGear wiki
Jump to navigation Jump to search
This article is a stub. You can help the wiki by expanding it.
TheEagle's tab widget

Parse $FG_ROOT/gui/dialogs to create a tabbed dialog for each XML dialog file found:

var PUIPreviewDialog = {
	new: func {
		var m = {
			parents: [PUIPreviewDialog],
			_window: canvas.Window.new([700, 500], "dialog")
		};
		
		m._window.setBool("resize", 1);
		
		m.root = m._window.getCanvas(1)
						.set("background", canvas.style.getColor("bg_color"))
						.createGroup();
		m.vbox = canvas.VBoxLayout.new();
		m._window.setLayout(m.vbox);
		m.tabContents = {};
		m.tabWidgets = {};
		return m;
		},
		
init: func {
		var fg_root = getprop("/sim/fg-root");
		var dialogPath = fg_root ~ "/gui/dialogs/";
		print("Path: ", dialogPath);
		# https://wiki.flightgear.org/Nasal_library#directory.28.29
		var files = directory(dialogPath);
		files = subvec(files, 2); # remove . and .. entries
		
		print("files in $FG_ROOT/gui/dialogs:", size(files));
	
		var dialogFiles = [];
		foreach(var f; files) {
		if ( size(split(".xml", f))==2 ) {
			append(dialogFiles, dialogPath~f);
		}
		}
		print("Dialog files:", size(dialogFiles));

		me.tabs = canvas.gui.widgets.TabWidget.new(me.root, canvas.style, {});
		me.tabsContent = me.tabs.getContent();
		me.vbox.addItem(me.tabs);

		foreach (var i;dialogFiles[:4]) {
		me.addTab(i, io.basename(i) );
		}        
	}, # init()
	
	addTab: func(name, title) {
	print("Adding new tab:", name, " title:", title);
	me.tabContents[title] = canvas.VBoxLayout.new();	
	me.tabWidgets[title] = canvas.gui.widgets.Label.new(me.tabsContent, canvas.style, {})
                                                .setText("preview for:"~title);
                                                #.setBackground("#ffaaaa");
                                                
    me.tabContents[title].addItem(me.tabWidgets[title]);
	me.tabs.addTab(name, title, me.tabContents[title] ); 
	}, #addTab()
	
	del: func {
	} #del()
};

var p = PUIPreviewDialog.new();
p.init();