GTK# window not rendered completely - c#

Right now, I am trying to develop a program using Mono and GTK# on a Debian (Raspbian) system.
The issue I'm facing is, that, completely randomly, the GUI (generated by the Stetic designer or its dynamic elements) isn't completely drawn, missing either a few characters from a Label-element or whole widgets, mostly those that were dynamically created. This is how it looks on a dialog window: http://imgur.com/oEZRg7c (text is cut off)
As soon as one window shows this issue, every other window has the same issues, sometimes missing whole widgets, even if those were created afterwards. The solution is usually to quit the program and reopen it, as it only randomly occurs.
This is how the constructor of most of my windows looks like (the part after Build() varies):
public partial class ErrorSolutionDialog : Gtk.Dialog
{
public ErrorSolutionDialog (string errorMessage, string solutionHint)
{
this.WidthRequest = this.Screen.Width;
this.HeightRequest = this.Screen.Height;
this.Maximize ();
this.Fullscreen ();
this.KeepAbove = true;
this.DestroyWithParent = false;
Build ();
this.ErrorMessage.Markup = "<b><span size='xx-large'>" + errorMessage + "</span></b>";
this.SolutionHint.Text = solutionHint;
}
}

I wouldn't say that the use of the Stetic designer inside Xamarin Studio/Monodevelop is bad, but as any piece of software it certainly has some issues.
Also, the use of any designer in any software environment will tie you to that development platform forever. Finally, the created source code will be hardly maintainable, apart from completely foreign for you.
That's why I always recommend to get rid of the designer. You can follow a Gtk# tutorial such as this one, and you'll find it is easy and rewarding. And you'll have whole and thorough control of your code.
The basics about Gtk# is creating a layout with VBoxes and HBoxes. For example, the following code creates a layout in which you'll have a TreeView and a TextView in a Dialog.
var swWin1 = new Gtk.ScrollWindow();
var swWin2 = new Gtk.ScrollWindow();
// TextView
this.txtView = new Gtk.TextView();
swWin1.AddWithViewport( this.txtView );
// TreeView
this.tvView = new Gtk.TreeView();
swWin2.AddWithViewport( this.tvView );
// Layout
var hBox = new HBox( false, 2 );
hBox.PackStart( swWin1, true, true, 5 );
hBox.PackStart( swWin2, true, true, 5 );
this.VBox.PackStart( hBox, true, true, 5 );
PackStart() is the method doing the magic in order to add a widget to a layout. The booleans tell Gtk to expand the widget. A ScrollWindow adds scrollbars to any widget.
Finally, my advice is for any action, use Gtk.Action, and call its methods CreateMenuItem() and CreateToolItem() in order to create menu entries and toobar buttons, instead of repeating the same code again and again.
Hope this helps.

Related

Why can't I get the RootVisual Of External App?

I am working on an automated testing framework in WPF. I am finding that while it is possible to automate most things using the basic WPF automation framework, it is very difficult to get down to the fine grained details of what is happening at the UI level. I need to be able to see things like the properties of the DataContext, properties of controls, and so on. I know this is possible because Snoop can do it. Snoop allows you to traverse the entire Visual Tree of any WPF app. I need this functionality. So, I wrote this code:
public async Visual GetAppRootVisual()
{
var allProcesses = Process.GetProcesses();
var filteredProcess = allProcesses.Where(p => p.ProcessName.Contains(ProcessSearchText)).First();
var windowHandle = filteredProcess.MainWindowHandle;
var hwndSource = HwndSource.FromHwnd(windowHandle);
return hwndSource.RootVisual;
}
The code works up until the second last line. The second last line returns null, but I can't figure out why. The windowHandle is returned, but the HwndSource is not returned. What is going wrong here?

How can I display a FolderBrowserDialog in LINQPad?

I want to browse for a folder in a LINQPad script so I tried using FolderBrowserDialog. It didn't work.
Here is a snippet showing what I'm doing?
string path = "";
var browser = new FolderBrowserDialog { ShowNewFolderButton = false };
if (browser.ShowDialog() == DialogResult.OK)
{
path = browser.SelectedPath;
}
This runs and hangs on the line with ShowDialog() with the yellow execution pointer against that line, but the folder browser dialog isn't visible.
I know that there is an overload for ShowDialog() that takes an IWin32Window owner argument and thought that might be the solution, but haven't yet figured out how to get an IWin32Window for the main LINQPad window. I hoped the Util class might provide a way but unless I'm missing it, it doesn't seem to.
Anyone have advice on getting around this problem?
Not solution, but an alternative, the FilePicker controle (part of new LinqPad Input Controls).
you can write:
new FilePicker().Dump().TextInput += (x, e) => ((FilePicker)x).Text.Dump();
or:
var picker = new FilePicker();
picker.TextInput += (x, e) => {
var fileName = picker.Text;
//action with the file...
};
full example:
void Main()
{
new FilePicker().Dump().TextInput += (x, e) => procces(((FilePicker)x).Text);
}
void procces(string file)
{
file.Dump("chosen file...");
//...
}
Setting the Run each query in its own process option to true is the cause of the problem. Setting that option back to the default false allows the code described above to run as expected.
However, making this change disables the built-in debugging. Furthermore the behaviour is still slightly problematic.
On first running the script the dialog is displayed and the script runs to completion after Ok or Cancel is selected. However, on running the script a second time it hangs as described in the question. After cancelling the execution and running it again the dialog displays but on the time after that it hangs again, and so on.
It was pointed out that setting the Always use fresh application domains option may resolve this and it does, allowing the dialog to display on every execution of the script.
I just came across this problem with LINQPad 5. I needed a folder picker similar to the file picker. Your solution worked without me having to modify LINQPad5 settings. The problem was the dialog was staying in the background. So here's how I got your snippet to work with that dialog in focus. Instead of using the FolderBrowseDialog.ShowDialog() I used the overload that passes in a windows form. I created a new form with description and window position then passed that to ShowDialog. That allowed me to set the description and window positioning.
string path = "";
using ( var browser = new System.Windows.Forms.FolderBrowserDialog { ShowNewFolderButton = false })
{
browser.Description = "Select Folder For Foo Processing";
var form = new System.Windows.Forms.Form(){TopMost = true, TopLevel = true};
var result = browser.ShowDialog(form);
if (result == System.Windows.Forms.DialogResult.OK)
{
path = browser.SelectedPath;
}
}
path.Dump();
I tried to initialize the form in ShowDialog with the settings, but had problems so I opted to declare it before show dialog. Hope this helps anyone with this problem.

how to insert the text in the editor in the TextAdornment template in Visual Studio?

I follow the steps in this page http://blogs.msdn.com/b/visualstudio/archive/2009/12/09/building-and-publishing-an-extension-for-visual-studio-2010.aspx
I create a TextAdornment project and a search box. I wan to do some different in this page. I want to query a link , get a list in the WPF user control and then write the info into the editor back. so the question is I do not know how to write the text back into the editor in seachbox(WPF user control)?
I searched a lot, and get a way to use the code look like this:
IVsTextManager txtMgr = (IVsTextManager)GetService(typeof(SVsTextManager));
IVsTextView vTextView = null;
int mustHaveFocus = 1;
txtMgr.GetActiveView(mustHaveFocus, null, out vTextView);
IVsUserData userData = vTextView as IVsUserData;
if (userData == null)
{
return null;
}
else
{
IWpfTextViewHost viewHost;
object holder;
Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
userData.GetData(ref guidViewHost, out holder);
viewHost = (IWpfTextViewHost)holder;
return viewHost;
}
However, the method "GetService" also said not found. I think the reason is this method is for VSPackage. and it is not suitable for Adornment project.
Please help to point how the write the text back into the editor from WPF user control. Thanks!
======================================================================================
Solution:
when creating the SearchBox(WPF User Control), pass through the IWpfTextView to WPF control.and then,it is possible to use this in SearchBox.xaml.cs. Also need to be aware to use the Dispatcher function to keep the UI thread is the active one.
Dispatcher.Invoke(new Action(() =>
{
ITextEdit edit = _view.TextBuffer.CreateEdit();
ITextSnapshot snapshot = edit.Snapshot;
int position = snapshot.GetText().IndexOf("gist:");
edit.Delete(position, 5);
edit.Insert(position, "billmo");
edit.Apply();
}));
The code you have there is if you're in a package and you are trying to figure out what view is currently active...it's overkill for what you're trying to do.
Assuming you started from the TextAdornment template, the adornment object is given an IWpfTextView in the constructor. (If not, you probably have an implementation of IWpfTextCreationListener.TextViewCreated which got it and you need to thread it through.) The IWpfTextView derives ITextView which has a property ITextBuffer. From here, you can call CreateEdit() and edit text from there.

Can't place two buttons in NavigationItem.RightBarButtonItems in MonoTouch

In my project, I need to add two UIBarButtonItems to a NavigationItem of a view controller. I solved this with this code:
UIBarButtonItem saveButton = new UIBarButtonItem("Save", UIBarButtonItemStyle.Bordered, (sender, e) => {
//some saving code...
});
UIBarButtonItem delButton = new UIBarButtonItem("Delete", UIBarButtonItemStyle.Done, (sender, e) => {
//some deletion code...
});
UIBarButtonItem[] items = new UIBarButtonItem[]
{
saveButton,
delButton
};
this.NavigationItem.RightBarButtonItems = items;
It worked in previous versions of MonoTouch (on iOS 5, both device and simulator), but (I think so) after I upgraded to MonoTouch 6, only first button from the array is displayed.
I try to use NavigationItem.SetRightBarButtonItems(items, false) method, but without any effect.
Is it a common problem or I am doing something wrong?
Is it a common problem or I am doing something wrong?
Touch.Unit, MonoTouch's unit test runner, use RightBarButtonItems and works correctly (showing both buttons) with MonoTouch 6.0.x.
OTOH I see nothing wrong in your code snippet (but it's a short one). Your best bet is to create a small, self contained, application that shows your issue and attach it to a bug report. We'll be able to review it and see what's wrong (or you might find the issue yourself, if it's inside your own code, when doing the test case).
OK, here is the case. The situation described in my question happens when you already have a back button and a some custom view (i.e. UISegmentedControl instance) in a navigationItem.TitleView. In this case, when you set navigationItem.RightBarButtonItems with an array of UIBarButtonItems and the space is not enough to display a navigationItem.TitleView and both right buttons, iOS renders only the first button of the array and discard the other.
I verified this in a native ObjC application and got the same behavior.

How to get selected text from an active application, without using a clipboard

What am I doing:
My main intent is to enable user friendly text to speech for personal use on Win 7. Approach should work in Google Chrome, VS and Eclipse.
Code example:
Following code creates global keyboard hook for ctrl + alt + space, called hookEvent. If event fires, it starts/stops speaking clipboard contents ( that can be updated with ctrl + c ).
/// <summary>
/// KeyboardHook from: http://www.liensberger.it/web/blog/?p=207
/// </summary>
private readonly KeyboardHook hook = new KeyboardHook();
private readonly SpeechSynthesizer speaker = //
new SpeechSynthesizer { Rate = 3, Volume = 100 };
private void doSpeaking(string text)
{
// starts / stops speaking, while not blocking UI
if (speaker.State != SynthesizerState.Speaking)
speaker.SpeakAsync(text);
else
speaker.SpeakAsyncCancelAll();
}
private void hookEvent(object sender, KeyPressedEventArgs e)
{
this.doSpeaking(Convert.ToString(Clipboard.GetText()));
}
public Form1()
{
InitializeComponent();
hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hookEvent);
hook.RegisterHotKey(ModifierKeysx.Control|ModifierKeysx.Alt, Keys.Space);
}
Question:
I would prefer not using the clipboard. Or at least, restoring the value after, something like:
[MethodImpl(MethodImplOptions.Synchronized)]
private string getSelectedTextHACK()
{
object restorePoint = Clipboard.GetData(DataFormats.UnicodeText);
SendKeys.SendWait("^c");
string result = Convert.ToString(Clipboard.GetText());
Clipboard.SetData(DataFormats.UnicodeText, restorePoint);
return result;
}
What are my options?
Edit:
To my surprise, I found that my clipboard reader is the best way to go. I created a notification area app, that responds to left click (speaking clipboard) and right click (menu opens up). In menu the user can chance speed, speak or create a audio file.
MS provide accessibility tools that do cover what you're trying to do. If you take a look at documents about screen scraping. In short, every component is accessible in some manner, if you use some of the windows debugging tools you can get to see the component names/structures within. You can then use that, however, its complicated as most times you would need to be very specific for each application you intend to scrape from.
If you manage to scrape you dont need to use the clipboard, as you can access the text property of the apps direct. Its not something I've had to do, hence, Ive no code to offer off the top of my head, but the term "screen scraping" should point you in the right direction.
If to expand a little on what Bugfinder said, Microsoft provider a UI Automation Framework to solve problems like the one you mentioned:
In particular you can use the TextSelectionChangedEvent of TextPattern:
The problem with this solution is that it only works on supported operating systems and applications - and not all support this.
Your clipboard solution is acceptable for applications that do not provide a good automation interface.
But for many applications the UI Automation Framework will work well and will provide you with a far better solution.

Categories