OCaml + LablGTK2: Multi-Line Text Box - user-interface

I am trying to figure out how to instantiate a multi-line text box inside a graphical widget. LablGTK2 appears to be quite limited in terms of documentation and the API is scarce for the things that start to look like what I want.
I have started to cross-reference the original GTK2 documentation, https://developer.gnome.org/gtk3/stable/gtkobjects.html, against the Lablgtk2 documentation, http://wwwfun.kurims.kyoto-u.ac.jp/soft/lsl/lablgtk/html/GText.html.
However, the best tutorial or simple/clear example I have found makes use of the very limiting single line text entry box:
http://plus.kaist.ac.kr/~shoh/ocaml/lablgtk2/lablgtk2-tutorial/x1155.html
I have found that some clear simple, derived examples are great for learning the basics. Does anyone have sample vignette that shows how to set up a multi-line text using OCaml & Lablgtk/lablgtk2? Or better recommendations for tutorials that will show to develop a multi-line text box (which is a pretty important feature in any GUI-based program)? Ideally, I want to connect the text input into this multi-line text to an OCaml module I have written that will process that text and then the GUI will display that processing results back on the GUI. Any help would be greatly appreciated.

You can use GtkTextView widget for multi-line text:
let _ =
(* prepare main window *)
let window = GWindow.window () in
window#connect#destroy ~callback:GMain.Main.quit;
(* add text view with scroll bars *)
let scroll = GBin.scrolled_window
~hpolicy:`AUTOMATIC ~vpolicy:`AUTOMATIC
~packing:window#add () in
let textview = GText.view ~packing:scroll#add_with_viewport () in
(* set text *)
textview#buffer#set_text "multi-\nline\ntext";
(* show everything and enter main loop *)
window#show ();
GMain.Main.main ()

You're right that the docs are very sparse. Therefore we must learn by copying each other. Like monkeys...
I would wager that the ocamleditor should contain an example of how to do this: https://forge.ocamlcore.org/projects/ocamleditor/
Also OCP is creating a simple OCaml editor which should also be helpful: https://github.com/OCamlPro/ocp-edit-simple

The lablgtk2 source code provides basic code snippets that are easy to understand for the beginning (at least easier than real world code)
If you use godi, they are installed under $GODI_PRFIX/doc/godi-lablgtk2/examples .
You can see them in action from the command line with the script lablgtk2, e.g
lablgtk2 /opt/wodi32/doc/godi-lablgtk2/examples/editor.ml
lablgtk2 /opt/wodi32/doc/godi-lablgtk2/examples/text/text-demo.ml

Related

How to insert text into an Ace Editor instance as a comment

I am building a website where I want to allow people to code in almost any language that the Ace editor supports. Right now I have it redirect if they try to load a language that either isn't supported by Ace, or that I didn't list in my supported languages. I only have a few languages that I allow to run (because I haven't got others to work or don't know how to), and for all those other languages that aren't run-supported, I want to load the Ace editor with a note saying that the language they loaded isn't supported for running, but I want it to be a comment in that language.
I tried inserting the text using editor.setValue([message], -1). Then set the focus on the editor using editor.focus(). Then I select all the text with editor.selectAll(). And then use the editor.toggleCommentLines() to make it a comment. For some reason this doesn't work, and I am not sure why.
I know that I could just look up how to write comment in each of the languages that I am allowing, and then convert the message into a comment before inserting it into the editor, but I want an easier way if possible.
Is there an easier way to do it? Or should I just do what I said that I could?
Since I answered my own question, I want to ask another to see if anyone has the answer for this:
When in the Ace editor you can press CTRL+/ to toggle line comments. But if you press CTRL+SHIFT+/ it will toggle multiline comments. I don't see a function for this in the Ace editor documentation but because it works with a keyboard shortcut, there must be a way to do it programmatically, right?
If someone could figure this out, that would be great!
a user found it! See his comment to see the answer to this part.
I was trying a few more things, and one of them was doing a setTimeout on the editor.toggleCommentLines(), and that worked. The timeout worked best if I used 150ms or higher (I started with 2000 just to see if it worked at all, and then slowly moved down).
So the following code works to automatically insert a message and comment it out:
const editor = ace.edit([element]);
editor.setValue([message], 0); // You can also leave the second parameter blank, 0 is the default which automatically selects all.
setTimeout(() => {
editor.toggleCommentLines();
}, 150); // 150ms or more works best
You may also notice that this method clears out 2 methods that I was previously using. There are two reasons for this:
When using 0 instead of -1 for the second parameter of editor.setValue() it selects all the text.
Because we are using the editor variable, the editor doesn't need to be in "focus".

I want the user to enter text in a text box, how can i do this?

I want the user to enter text in a text box (such as a login text box on some site).
I tried to search in text() bibliography and other similar functions.
This guy seems to have figured it out: http://www.sojamo.de/libraries/controlP5/. Look for the textArea control.
If you don't like this, I read that the g4p library had something like an inputbox. Otherwise you will have to draw it yourself (which would be a nice exercise) using keyboard inputs and the like.
What you're describing is called a graphical user interface, or GUI. There are many ways to approach this problem:
Code it yourself. Use something like the rect() function to draw a text box, and the keyPressed() function to detect key presses.
Use a library. The Processing libraries page lists several GUI libraries. Try each of them out and see which one you like best.
Use a more general GUI library. If you're deploying as Java, then you might use Swing or JavaFX. If you're deploying as JavaScript, then you might use HTML elements.
Any of the above might work, and which approach you choose depends more on you, your context, and your preferences. We can't tell you which is best, because it's all up to what you prefer.

In Visual Studio Code, is it possible to Edit the MiniMap View with settings

I'm updating some legacy code, because of my unfamiliarity to it, Its very difficult to find structures, especially if I step away for a week for other projects.
To alleviate this problem I've gone back to my old-school roots and started adding ascii text-art as comments above sections of code, using this tool:
http://patorjk.com/software/taag/#p=display&h=3&v=1&f=Big%20Money-ne&t=Reveal%0ACheck
This allows me to see via my MiniMap the titles of functions or sections of code I might need to come back to
The Thought then occured.. well surely someone else has this problem, and since VSCode seems to be written by the community, maybe someone has already written a plugin that would search the code for function titles (like Javadocs?) and display the title in a readable size?
If not, would it be easily coded? i.e. is the minimap just a very shrunk down copy(not easy) or is it structured and can be parsed and tweaked?
As long as your language plugin supports it, you can use cmd + shift + o to go to function definition.
All functions should also be listed in the Side Bar under "Outline"

How to create tabbed Mathematica notebooks

Is there a way to create and edit notebooks (sequences of cells) in a tabbed interface? Could such an interface be made with TabView or some other tool? I assume this would be possible if I made a front-end from scratch, but is there a way within the standard Wolfram front-end?
Two things motivate me to ask this. First, I would like to create a replacement for Microsoft Office OneNote with Mathematica notebooks. Second, when I'm working in Mathematica I find myself wondering whether a tabbed interface would work better than having numerous separate windows open.
While Mathematica doesn't support tabbed notebook windows directly, it is possible to reproduce something of the effect using DockedCells. The Virtual Book/Function Navigator interface (from the help menu) does this...it's essentially a slide show with two slides, one holding the VB and the other containing the FN, with a DockedCells navigation interface driven by NotebookFind that looks a bit like tabs.
Here's the gist of how you might go about making such a notebook on your own. Sorry, there are some kind of advanced concepts here...if there's any parts of this solution which you want to learn more about, maybe you can spin off more questions.
(* make a single page of the notebook *)
page[tag_String] :=
Cell#CellGroupData[{Cell["", "SlideShowNavigationBar",
CellTags -> {tag}], Cell[tag, "Title"]}];
(* make a single tab-like button which selects the page *)
button[tag_String] :=
Button[Dynamic[
Setter[Dynamic[
CurrentValue[EvaluationNotebook[], {TaggingRules, "page"},
tag]], tag]],
CurrentValue[EvaluationNotebook[], {TaggingRules, "page"}] = tag;
NotebookLocate[tag],
Appearance -> None];
(* make a notebook based upon a list of strings which are names of tabs *)
makeTabbedNotebook[nameList_List] :=
NotebookPut#Notebook[page /# nameList,
DockedCells ->
ToBoxes[ExpressionCell[Row[button /# nameList],
"DockedCell"]][[1]],
ScreenStyleEnvironment -> "SlideShow"];
makeTabbedNotebook[{"First", "Second", "Third"}]
Edit: changed NotebookFind[ButtonNotebook[],tag,All,CellTags], which appears to not always scroll the slideshow correctly, to NotebookLocate[tag]. See discussion in comments. The two bits of code should, in theory, be equivalent, but a bug in Mathematica 8 appears to make them behave differently sometimes.

Matlab GUI's managebuttons

I have to maintain a large MATLAB GUI, containing quite a few callback functions, each of which gets called at many points. At some point, I decided to rename one of the callbacks; in order not to have to go through GUIDE to manually modify each of the UI that calls it, I used the excellent gencode program to "unwrap" the .fig file, s/old_name/new_name/g, and recreate the .fig.
This seems to work well, except for one thing: when clicking on a button in a button group, I got a message telling me that the function manageButtons is unknown. After a bit of googling, I find that manageButtons is a nested function in childAddedCbk, which, I assume, is called by GUIDE as the GUI is created. Now, trying to move just manageButtons outside to its own, in-the-path file doesn't work either: the hgroup passed to it is an invalid handle object.
So... does anyone has a workaround for that? Any of the following will be appreciated:
changing all the callbacks in a GUI in a semi-automatic way, or
making the manageButtons get a valid handle, or
having gencode not break all that.
Thanks,
Antony
You can export the FIG + M file to single .m file. You will lose the ability to edit this with guide, but you will be able to do a simple regular expression replace on the resulting .m file.
In order to do so - use on the menu of GUIDE -> File -> Export.

Resources