Skip to content
This repository was archived by the owner on Mar 18, 2022. It is now read-only.

Commit 2001610

Browse files
committed
add __call
1 parent 8e3cae3 commit 2001610

File tree

5 files changed

+43
-167
lines changed

5 files changed

+43
-167
lines changed

example/php/authorization/main.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function closeApp($window, $data)
1919
$window = new Window();
2020
$window->setTitle('GtkEntryBox');
2121
$window->setPosition(0);
22-
$window->setSize(200, 200);
22+
$window->setDefaultSize(200, 200);
2323

2424
$signal = new Gtk\Signal();
2525
$window->connect('destroy', 'closeApp', null);

example/php/container_widget_layout/main.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ function my_delete_event()
3131
$window->connect( 'delete_event', 'my_delete_event', null);
3232

3333
$label1 = new Label('Label 1');
34+
$label1->setText('sdf');
35+
$label1->setWidthChars(1);
3436
$label2 = new Label('Label 2');
3537
$label3 = new Label('Label 3');
3638

src/Gtk/Label.php

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
/**
88
* Class Label
99
*
10-
* @package Gtk3\Gtk
10+
* @method setWidthChars(int $char): void
11+
* @method setText(string $text): void
12+
* @method setMarkup(string $text): void
13+
*
1114
*/
1215
class Label extends Widget
1316
{
@@ -21,42 +24,15 @@ public function __construct(string $text)
2124
$this->widget = Gtk::getInstance()->gtk_label_new($text);
2225
}
2326

24-
/**
25-
* @param string $text
26-
*
27-
* @throws \Exception
28-
*/
29-
public function setText(string $text): void
27+
public function __call(string $name, array $arguments)
3028
{
31-
Gtk::getInstance()->gtk_label_set_text(
32-
Gtk::getInstance()->cast('GtkLabel*', $this->widget),
33-
$text
34-
);
35-
}
29+
$functionName = 'gtk_label_' . strtolower(preg_replace('~([A-Z])~', '_$1', $name));
30+
$cast = "GtkLabel *";
3631

37-
/**
38-
* @param string $text
39-
*
40-
* @throws \Exception
41-
*/
42-
public function setMarkup(string $text): void
43-
{
44-
Gtk::getInstance()->gtk_label_set_markup(
45-
Gtk::getInstance()->cast('GtkLabel*', $this->widget),
46-
$text
47-
);
48-
}
49-
50-
/**
51-
* @param int $char
52-
*
53-
* @throws \Exception
54-
*/
55-
public function setWidthChars(int $char): void
56-
{
57-
Gtk::getInstance()->gtk_label_set_width_chars(
58-
Gtk::getInstance()->cast('GtkLabel*', $this->widget),
59-
$char
60-
);
32+
try {
33+
return Gtk::getInstance()->$functionName(Gtk::getInstance()->cast($cast, $this->widget), ...$arguments);
34+
} catch (\Throwable) {
35+
return parent::__call($name, $arguments);
36+
}
6137
}
6238
}

src/Gtk/Widget.php

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
/**
1111
* GtkWidget is the base class all widgets in GTK+ derive from. It manages the widget lifecycle, states and style.
12+
*
13+
* @method setSizeRequest(int $width, int $height): void
14+
* @method show(): void
15+
* @method showAll(): void
1216
*/
1317
class Widget
1418
{
@@ -17,28 +21,11 @@ class Widget
1721
*/
1822
public CData $widget;
1923

20-
/**
21-
* @param $width
22-
* @param $height
23-
*/
24-
public function setSizeRequest(int $width, int $height): void
24+
public function __call(string $name, array $arguments)
2525
{
26-
Gtk::getInstance()->gtk_widget_set_size_request($this->widget, $width, $height);
27-
}
26+
$functionName = 'gtk_widget_' . strtolower(preg_replace('~([A-Z])~', '_$1', $name));
27+
$cast = "GtkWidget *";
2828

29-
/**
30-
*
31-
*/
32-
public function show(): void
33-
{
34-
Gtk::getInstance()->gtk_widget_show($this->widget);
35-
}
36-
37-
/**
38-
*
39-
*/
40-
public function showAll(): void
41-
{
42-
Gtk::getInstance()->gtk_widget_show_all($this->widget);
29+
Gtk::getInstance()->$functionName(Gtk::getInstance()->cast($cast, $this->widget), ...$arguments);
4330
}
4431
}

src/Gtk/Window.php

Lines changed: 20 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99

1010
/**
1111
* Class Window
12+
*
13+
* @method setTitle(string $title): void
14+
* @method setPosition(int $position): void
15+
* @method setDefaultSize(int $width, int $height): void
16+
* @method fullscreen(): void
17+
* @method unfullscreen(): void
18+
* @method resize(int $width, int $height): void
19+
* @method setResizable(bool $resizable = true): void
20+
* @method present(): void
21+
* @method maximize(): void
22+
* @method unmaximize(): void
23+
*
1224
*/
1325
class Window extends Widget
1426
{
@@ -31,118 +43,17 @@ public function __construct(int $type = self::GTK_WINDOW_TOPLEVEL)
3143
}
3244
}
3345

34-
/**
35-
* @param string $title
36-
*/
37-
public function setTitle(string $title): void
38-
{
39-
Gtk::getInstance()->gtk_window_set_title(Gtk::getInstance()->cast("GtkWindow *", $this->widget), $title);
40-
}
41-
42-
/**
43-
* @param string $title
44-
*/
45-
public function setSize(int $width, int $height): void
46-
{
47-
Gtk::getInstance()
48-
->gtk_window_set_default_size(
49-
Gtk::getInstance()->cast("GtkWindow *", $this->widget),
50-
$width,
51-
$height
52-
);
53-
}
54-
55-
/**
56-
* @throws \Exception
57-
*/
58-
public function fullscreen(): void
59-
{
60-
Gtk::getInstance()->gtk_window_fullscreen(
61-
Gtk::getInstance()->cast("GtkWindow *", $this->widget)
62-
);
63-
}
64-
65-
/**
66-
* @param $position
67-
*
68-
* @throws \Exception
69-
*/
70-
public function setPosition(int $position): void
46+
public function __call(string $name, array $arguments)
7147
{
72-
Gtk::getInstance()->gtk_window_set_position(
73-
Gtk::getInstance()->cast("GtkWindow *", $this->widget),
74-
$position
75-
);
76-
}
48+
$functionName = 'gtk_window_' . strtolower(preg_replace('~([A-Z])~', '_$1', $name));
49+
$cast = "GtkWindow *";
7750

78-
/**
79-
* @throws \Exception
80-
*/
81-
public function unfullscreen(): void
82-
{
83-
Gtk::getInstance()->gtk_window_unfullscreen(
84-
Gtk::getInstance()->cast("GtkWindow *", $this->widget)
85-
);
86-
}
87-
88-
/**
89-
* @param int $width
90-
* @param int $height
91-
*
92-
* @throws \Exception
93-
*/
94-
public function resize(int $width, int $height): void
95-
{
96-
Gtk::getInstance()->gtk_window_resize(
97-
Gtk::getInstance()->cast("GtkWindow*", $this->widget),
98-
$width,
99-
$height
100-
);
101-
}
102-
103-
/**
104-
* @param bool $resizable
105-
*
106-
* @throws \Exception
107-
*/
108-
public function setResizable(bool $resizable = true): void
109-
{
110-
Gtk::getInstance()->gtk_window_set_resizable(
111-
Gtk::getInstance()->cast("GtkWindow*", $this->widget),
112-
$resizable
113-
);
114-
}
115-
116-
/**
117-
* @throws \Exception
118-
*/
119-
public function present(): void
120-
{
121-
Gtk::getInstance()->gtk_window_present(
122-
Gtk::getInstance()->cast("GtkWindow*", $this->widget)
123-
);
124-
}
125-
126-
/**
127-
* @throws \Exception
128-
*/
129-
public function maximize(): void
130-
{
131-
Gtk::getInstance()->gtk_window_maximize(
132-
Gtk::getInstance()->cast("GtkWindow*", $this->widget)
133-
);
134-
}
135-
136-
/**
137-
* @throws \Exception
138-
*/
139-
public function unmaximize(): void
140-
{
141-
Gtk::getInstance()->gtk_window_unmaximize(
142-
Gtk::getInstance()->cast("GtkWindow*", $this->widget)
143-
);
51+
try {
52+
return Gtk::getInstance()->$functionName(Gtk::getInstance()->cast($cast, $this->widget), ...$arguments);
53+
} catch (\Throwable) {
54+
parent::__call($name, $arguments);
55+
}
14456
}
145-
14657
/**
14758
* @param string $detailed_signal
14859
* @param callable $c_handler

0 commit comments

Comments
 (0)