Demo Code on main page showing click not found - gtk

I am trying to compile demonstration code on the main page of Vala programming language
int main (string[] args) {
var app = new Gtk.Application(
"com.example.App",
ApplicationFlags.FLAGS_NONE
);
app.activate.connect(() => {
var win = new Gtk.ApplicationWindow(app);
var btn = new Gtk.Button.with_label("Hello World");
btn.click.connect(win.close);
win.child = btn;
win.present();
});
return app.run(args);
}
I am using following command:
$ valac --pkg gtk+-3.0 valademo.vala
However, it is giving following error:
valademo.vala:13.5-13.13: error: The name `click' does not exist in the context of `Gtk.Button'
btn.click.connect(win.close);
^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)
Where is the problem and how can it be solved?

That's probably a typo, it should be clicked not click. You may want to raise an issue with the website or submit a pull request.
There are a couple of other problems. Firstly GTK+3 needs win.show_all(), but that has been removed in GTK 4.
Secondly the documentation for win.present() is advising that function shouldn't be used.
Here's a working example for GTK+3:
int main (string[] args) {
var app = new Gtk.Application(
"com.example.App",
ApplicationFlags.FLAGS_NONE
);
app.activate.connect(() => {
var win = new Gtk.ApplicationWindow(app);
var btn = new Gtk.Button.with_label("Hello World");
btn.clicked.connect(win.close);
win.child = btn;
win.show_all();
});
return app.run(args);
}

Related

typo3 Uncaught Error: Class not found when start development new extension typo3 v 10

Hello Typo3 experts I trying to build an extension in typo3 I have an HTML page that uses jquery to call a class called rpc.php when I tried to use any function from typo3 core it always give me class not found
namespace GlobalizationPartnersInternational\Gpi\Models\Tsc;
class rpc{
public $endPoint = '';
public $token = '';
public function main(){
$typo3InstallDir = \TYPO3\CMS\Core\Core\Environment::getPublicPath() . '/';
print_r('Hello World!'.$typo3InstallDir,true);
}
}
$rpc = new rpc();
$rpc->main();
JQUERY CODE
(function($) {
$(document).ready(function() {
if (window.efx && window.efx.init) {
efx.startup("http://localhost/typo3/typo3conf/ext/gpi/Classes/models/tsc/rpc.php");
efx.init();
}
$(document).on("keyup", "#tsc_SearchQuery", function() {
$("li label span, li label").css("background-color", "");
var phrase = this.value;
if (phrase.length < MIN_QUERY) {
return false;
}
//find text to highlight
$("li label span, li label").each(function(i, v) {
var block = $(v);
$(v).filter(function() {
return block.text().match(new RegExp(phrase, "gi"));
}).css("background-color", COLOR_HIGHLIGHT);
});
return false;
});
});
})(jQuery);
Fatal error: Uncaught Error: Class 'TYPO3\CMS\Core\Core\Environment' not found in C:\wamp64\www\typo3\typo3conf\ext\gpi\Classes\models\tsc\rpc.php
I appreciated any help thanks in advance
You can't just call a class like http://localhost/typo3/typo3conf/ext/gpi/Classes/models/tsc/rpc.php
If you are using TYPO3 11 I suggest to write a custom middleware or if you use an older TYPO3 version a bit simpler variant is to use but already marked as deprecated!
As written in the example you need:
Register the middleware in your extension in Configuration/RequestMiddlewares.php
Implement the middlware and return the response you need

Adding an element after clicking on a button

I was trying to learn Vala by programming a very simple application and I stumbled over a problem, that I was unable to resolve on my own.
The program shows simply a button Init and on click it should add a Button X to the Grid container. Unfortunately, the contents of the Grid container remain empty and I don't know why.
Even more confusing is, that adding the Button right in the constructor works as expected.
So what I'm doing wrong here?
using Gtk;
class MyWindow: Gtk.Window {
private Gtk.Grid mGrid;
public MyWindow() {
var init=new Gtk.Button.with_label("Init");
init.clicked.connect((t)=>{
stdout.printf("Init");
mGrid.attach(new Gtk.Button.with_label("X"),0,0,1,1);
});
var box=new Gtk.Box(VERTICAL,0);
mGrid=new Gtk.Grid();
//mGrid.attach(new Gtk.Button.with_label("X"),0,0,1,1);
box.add(init);
box.add(mGrid);
this.add(box);
this.show_all();
}
}
int main(string[] args) {
Gtk.init(ref args);
new MyWindow();
Gtk.main();
return 0;
}
With the GTK+ toolkit widgets are hidden by default. Although you have this.show_all (), the button is created afterwards and is hidden. Changing the callback from:
init.clicked.connect((t)=>{
stdout.printf("Init");
mGrid.attach(new Gtk.Button.with_label("X"),0,0,1,1);
});
to something like:
init.clicked.connect((t)=>{
stdout.printf("Init");
var my_button = new Gtk.Button.with_label("X");
my_button.show_all ();
mGrid.attach(my_button,0,0,1,1);
});
now works.

How do I connect a custom function to the clicked action of a GTK Button?

I am working my way through the Vala GTK+3 tutorial provided by Elementary OS. I understand that this code:
var button_hello = new Gtk.Button.with_label ("Click me!");
button_hello.clicked.connect (() => {
button_hello.label = "Hello World!";
button_hello.set_sensitive (false);
});
uses a Lambda function to change the button's label when it's clicked. What I want to do is call this function instead:
void clicked_button(Gtk.Button sender) {
sender.label = "Clicked. Yippee!";
sender.set_sensitive(false);
}
I've tried this:
button.clicked.connect(clicked_button(button));
But I get this error from the Vala compile when I try to compile:
hello-packaging.vala:16.25-16.46: error: invocation of void method not allowed as expression
button.clicked.connect(clicked_button(button));
^^^^^^^^^^^^^^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)
I'm new to both Vala and Linux so please be gentle but can someone point me in the right direction?
You need to pass a reference to the function, rather than the result of the function. So it should be:
button.clicked.connect (clicked_button);
When the button is clicked GTK+ will invoke the clicked_button function with the button as an argument.
The error message invocation of void method not allowed as expression is telling you you are calling (invoking) the method and it has no result (void). Adding parentheses, (), to the end of a function name invokes that function.
Managed to get it working. Here's the code in case others need it:
int main(string[] args) {
// Initialise GTK
Gtk.init(ref args);
// Configure our window
var window = new Gtk.Window();
window.set_default_size(350, 70);
window.title = "Hello Packaging App";
window.set_position(Gtk.WindowPosition.CENTER);
window.set_border_width(12);
window.destroy.connect(Gtk.main_quit);
// Create our button
var button = new Gtk.Button.with_label("Click Me!");
button.clicked.connect(clicked_button);
// Add the button to the window
window.add(button);
window.show_all();
// Start the main application loop
Gtk.main();
return 0;
}
// Handled the clicking of the button
void clicked_button(Gtk.Button sender) {
sender.label = "Clicked. Yippee!";
sender.set_sensitive(false);
}

How to load an accelerators map from a file using GTK3 in Vala?

I'm making a text editor using GTK3 in Vala. I have a Gtk.MenuBar in a Gtk.Window and I want to use accelerators to easily activate its Gtk.MenuItems. But I want the user to be able to change the key combinations, so I'm loading the accelerators specifications from a file using the method Gtk.AccelMap.load("accels"). However, after calling this method, the accelerators are not loaded: the menu items don't have AccelLabels and are not activated when I press the key combinations. Here are the two files I'm working on. The first file contains a small version of my application (to show what I'm trying to do) and the second one is the accels file from which I load the accels specifications, and they must be in the same directory.
main.vala
// Compile me with: valac main.vala -o main --pkg gtk+-3.0
public class MyWindow: Gtk.Window {
public MyWindow() {
this.set_default_size(500, 500);
var main_box = new Gtk.VBox(false, 0);
this.add(main_box);
var accel_group = new Gtk.AccelGroup();
this.add_accel_group(accel_group);
// Load the accelerators from the file
Gtk.AccelMap.load("accels");
// Create the action
var quit_action = new Gtk.Action("file-quit", "Quit", "Quit the application", null);
quit_action.activate.connect(()=>{
Gtk.main_quit();
});
quit_action.set_accel_group(accel_group);
quit_action.set_accel_path("<MyWindow>/File/Quit");
// Menubar
var menubar = new Gtk.MenuBar();
main_box.pack_start(menubar, false, false, 0);
var file = new Gtk.MenuItem.with_label("File");
menubar.add(file);
var file_menu = new Gtk.Menu();
file.set_submenu(file_menu);
var quit_mi = (Gtk.MenuItem)quit_action.create_menu_item();
file_menu.append(quit_mi);
// Label
var label = new Gtk.Label("My Window");
main_box.pack_start(label, true, true, 0);
this.destroy.connect(Gtk.main_quit);
}
}
int main(string[] args) {
Gtk.init(ref args);
var win = new MyWindow();
win.show_all();
Gtk.main();
return 0;
}
"accels" file
; main GtkAccelMap rc-file -*- scheme -*-
; this file is an automated accelerator map dump
;
; (gtk_accel_path "<MyWindow>/File/Quit" "<Control>q")
So, why is this not working? What do I have to do before or after loading the accel file?
PS: I don't want to use a Gtk.UIManager.
See : https://docs.xfce.org/faq#keyboard_related
"
This functionality has been disabled since GTK3 which means that Xfce apps that have migrated to GTK3 (such as xfce4-terminal) do not support it.
Refer to specific app's documentation to learn how to configure its shortcuts.
"

Sometimes GTK modal dialogs are not modal --- bug or feature?

When I create a custom dialog in GTK (both, GTK2 or GTK3) and set it to be modal, all input to other windows of my application is ignored. This works nearly always, but it fails under certain conditions.
When I add a ScrolledWindow containing a TreeView to my dialog, it still works as supposed. But if I fill the TreeView with entries until the ScrolledWindow starts to display its scroll bars --- the modality is suddenly lost and I can click on my other windows!
Here is the most basic example I was able to set up. It's written in Vala, but you'll get the idea:
class MyDialog: Gtk.Dialog {
public MyDialog() {
this.modal = true;
var data = new Gtk.ListStore(1, typeof(string));
// increase this number -- the dialog is not modal anymore!
for (int i=0; i<2; ++i) {
Gtk.TreeIter current;
data.append(out current);
data.set(current, 0, "Lorem Ipsum");
}
var render = new Gtk.CellRendererText();
var column = new Gtk.TreeViewColumn();
column.pack_start(render, true);
column.add_attribute(render, "text", 0);
var treeview = new Gtk.TreeView.with_model(data);
treeview.append_column(column);
treeview.show();
var scroll = new Gtk.ScrolledWindow(null, null);
scroll.set_size_request(100, 100);
scroll.add(treeview);
scroll.show();
(this.get_content_area() as Gtk.Box).add(scroll);
}
}
int main (string[] args) {
Gtk.init (ref args);
var window = new Gtk.Window();
window.set_default_size(350, 170);
window.destroy.connect(Gtk.main_quit);
var button = new Gtk.Button.with_label("Click me!");
button.clicked.connect(() => {
var dialog = new MyDialog();
dialog.set_transient_for(window);
dialog.run();
dialog.destroy();
});
window.add(button);
window.show_all();
Gtk.main();
return 0;
}
Compile it with:
valac --pkg gtk+-3.0 main.vala
Am I missing something? Is this behaviour wanted? Or is it a bug? If so, is there a workaround?
EDIT: I investigated a bit further: The problem disappears when the overlay-scrollbars from Ubuntu are uninstalled. So It's not solved yet, but I know where I have to report this...
Definitely a bug. Post a bug report and/or upgrade your GTK+ lib.