Writing a small text editor in Ruby - ruby

I want to write a small text-editor in Ruby for learning the basics. I want to show all the possible text-manipulation options in one single menu. Once the option is selected, the string typed in an Input box will be altered accordingly.
I am not following how to prepare this basic GUI.

You can have a look at FXRuby. Here is a small tutorial to make a text editor using FXRuby.http://rubylearning.com/satishtalim/fxruby.html

On Windows, your best bet is using JRuby and SWT. If you plan to switch to Mac OS X, you can think about MacRuby.

You can use visualruby, and you could write it in about an hour:
http://visualruby.net
You would build your window in the glade interface designer, then add a menu to your window with all your menu options. All of this can be done in the glade software so no code is required. Then you could give your menu options names that you reference later:
menuClear
menuIndent
menuComment
menuQuit
etc.
You could have the editor itself be a textview named textview1.
Then your code could be:
class MyEditor
def show
load_glade(__FILE__)
show_window()
end
def menuClear__clicked
#builder["textview1"].buffer.text = ""
end
def menuIndent__clicked
current_line = tab + current_line
end
def menuComment__clicked
current_line = "#" + current_line
end
def menuQuit__clicked
destroy_window
end
end
MyEditor.new.show
I think its easier than any of the other toolkits, and its more robust because its GTK.
All of the above statement are explained on the website. Good luck.

Ruby got Tk builtin, or you can use ffi-tk, which is a bit closer to tcl/tk. ver is an example. tkdocs is quite comprehensive, and it's got a nice API imo. And the Text Widget is really powerful.

If you have previous Java/Swing experience you might want to look at JRuby and Monekybars.

Related

How can I acquire text from text filed

I am new to programming and am trying to design a Fortune Teller game according to a book "Applescript studio programming for the absolute beginner".
When the player types the question in the Text Field and then clicks the "Ask" button, the program will randomly give the answer "Yes" "No" "Maybe".
I did as the book instructed. But when I press build and go in Xcode, it returns "2018-04-08 22:22:01.189 Fortune Teller[14813:245050] *** -[AppDelegate clicked:]: Can’t get every text of TextField of class "NSObject". (error -1728)"
Can anyone tell me how to fix that problem? Thank you!
The following is the interface.
The following is the code.
-- This code runs whenever the player clicks on the Ask button
on clicked_(theObject)
-- Assign the text entered by the player clicks on the Ask button
set the question to contents of text field "textbox" of window "main"
-- Display an error message if the player did not enter any text
if question = "" then
display dialogue ¬
"Sorry, but you did not ask a question. Please try again."¬
buttons {"OK"}
return
end if
-- Assign a random number between 1 and 3 to a variable named random
set randomNo to a random number from 1 to 3
if randomNo = 1 then
set answer to "Yes"
end if
if randomNo = 2 then
set answer to "No"
end if
if randomNo = 3 then
set answer to "Maybe"
end if
beep -- Play a beep sound to get player's intention
display display dialog "Question:" & question & "Answer:" & answer & buttons {"OK"}
end clicked_
Ouch. Get a new book.
Mac OS X 10.6 replaced AppleScript Studio with AppleScript-ObjC. Unlike ASS, which gave you AppleScript-style commands and classes for controlling Cocoa, ASOC is just a thin wrapper around Cocoa’s Objective-C APIs, meaning you need to learn those APIs before you can use it.
Shane Stanley used to have an e-book on GUI programming with ASOC, but I think it’s out of print unfortunately. There was an introductory chapter to GUI programming with ASOC at the end of Apress’s Learn AppleScript, 3rd edition (which I co-wrote), and the AppleScript Users mailing list/MacScripter.net forum can no doubt point you to whatever other resources are out there. But all in all, documentation and support is not great. You pretty much have to learn ObjC in order to use ASOC effectively, even if just to read the Cocoa documentation and translate it to AS syntax in your head. And it hasn’t been updated in several years so lacks support for newer ObjC features such as blocks, which means you end up writing ObjC code anyway if you want to use those APIs.
..
Frankly, if your goal is to write your own GUI Apps (rather than automate existing ones), I strongly recommend you bite the bullet and learn Apple’s Swift. While it’s a bloated mess of a language, it’s well-documented, widely-used, and 100% supported by Apple [unlike AppleScript]. With Apple recently announcing a new GUI framework for writing iOS and macOS apps, it’s clear that Swift is the future.
If you need a bit of AppleScript to talk to other apps, it is possible to mix Swift and AS in the same app; but that’s a different question for a separate post.

How do I set program title and icon in Clutter toolkit?

I have recently been learning how to program with the Clutter GUI toolkit. One thing I haven't been able to figure out is how to set the programs title and icon for the window manager.
As illustrated in the image below, Gnome Shell says that the program name is "Unknown" and that the program does not have an icon.
So, how do I do this?
you cannot do this from Clutter: the windowing system API inside Clutter only allows basic operations.
if you want proper integration in a windowing system you should use Clutter-GTK, and embed a ClutterStage into a Gtk application.
In theory, you can do that in this way:
let stage = Clutter.Stage.get_default ();
let gdkWind = ClutterGdk.get_stage_window (stage);
// The list most containt icons in different sizes.
let list = [GdkPixbuf.Pixbuf.new_from_file("test.png")];
gdkWind.set_icon_list(list);
//The next line not work
gdkWind.set_title("This title is not added");
In practice, you only will can load the icon and the windows title, but not the task bar title for the windows. The set_title won't work as Gdk.Window reference say it will (https://people.gnome.org/~gcampagna/docs/Gdk-3.0/Gdk.Window.set_title.html). Is then a Clutter issue, because is not a GDK "special case". But well is not working.

OCaml + LablGTK2: Multi-Line Text Box

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

Does Shoes have a list view control?

I was taking a look at Shoes which seems like a very nice tool for quick GUI applications. The common elements/controls, however, don't seem to include the equivalent of a list/report view (e.g., ListView in Windows, NSTableView in OS X).
Did I just miss this, or does it not (yet) exist?
If you mean output a large quantity of textual data such as a report or table then there's a nice simple app in the shoebox called the Simple Table Class which works very nicely. Before I found it i was ready to give shoes up - but this class solved my problem in minutes.
Looking for it i stumbled across laces which includes a class called The Tablie. I haven't tried it but I am suddenly intrigued....
There doesn't seem to be. Shoes Classes doesn't seem to mention anything similar.
I guess it shouldn't be too difficult to manually create something similar. The good-vjot.rb sample script is probably the closest I can think of, although it's hardly identical..
Martin DeMello's Gooey Challenge
(Using Shoes widgets to build custom Shoes controls) may be of some help
Shoes.app do
stack do
#l = list_box :items => ["YES", "NO"] do
alert #l.text
end
end
end

How do I change the icon of my Shoes App?

I was wondering if it was possible to change the my Shoes app's icon? I imagine its style-oriented, but I haven't been able to find anything on it.
Is this possible?
You can do it in Green Shoes, but you have to go under the hood a bit. Here's some very simple code I wrote for SciRuby:
class Shoes
class App
def icon filename=nil
filename.nil? ? win.icon : win.icon = filename
end
class << self
def default_icon= filename
Gtk::Window.set_default_icon(filename)
end
end
end
end
The second method is optional; you can use it to set the icon for your whole app, I think. The first method sets the icon for the current window, and you would use it like so:
DIR = Pathname.new(__FILE__).realpath.dirname.to_s
ICON_PATH = File.join(DIR, '..', 'static', 'my-icon.png')
# ...
Shoes.app :title => "My App" do
icon ICON_PATH
end
And for the default:
Shoes::App.default_icon = ICON_PATH
I use a 128x128 png file. I tried a 500x500 one and it didn't work properly. In the code above, ICON_PATH, from the root of your project directory, should be ./static/my-icon.png.
You may also need to require "gtk2" somewhere for the default_icon= method.
I don't believe this is possible from within Shoes. This is based on extensive searching, both online and in the source code.
However, there are a couple things external to Shoes that work. One is to simply change the file #{DIR}/static/shoes-icon.png, which is where the runtime pulls its icon from. The other is to change the hardcoded value in the file libshoes.so (or your OS's equivalent); you could use a hex editor, or any other editor that wouldn't mess the file up. Note that for the hardcoded editing to work without crashing, you have to replace the hardcoded string with something of the same length.
Use IcoFx. _why himself suggested this utility on the mailing list. You will find the instruction in the tutorial section of the icoFX website.
So far, I am not sure that this is actually possible. in OS X, I was able to modify the package contents to change what the plist is pointing to in terms of icons. However, when the application runs, it uses the Shoes' runtime icon.
I have no clue how you'd do this on Windows.

Resources