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

Commit 49a6b66

Browse files
committed
fix
1 parent bacc3ef commit 49a6b66

18 files changed

+412
-119
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
composer.lock
22
vendor
3-
.idea
3+
.idea

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
],
99
"require": {
1010
"php": "^7.4",
11-
"ext-FFI": "^7.4"
11+
"ext-FFI": "^7.4",
12+
"serafim/ffi-loader": "^1.0"
1213
},
1314
"autoload": {
1415
"psr-4": {
15-
"Kmaestro\\Gtk3\\": "src"
16+
"Gtk3\\": "src"
1617
}
1718
}
1819
}

example/c/hello-world/hello_world

-12.8 KB
Binary file not shown.

example/c/hello-world/hello_world_old

-12.6 KB
Binary file not shown.

gtk.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

out/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

resources/gtk.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
typedef char gchar;
2+
typedef int gint;
3+
typedef unsigned long gulong;
4+
typedef void* gpointer;
5+
typedef void* (*GCallback) (void*, void*);
6+
typedef struct _GClosureNotify GClosureNotify;
7+
typedef struct _GConnectFlags GConnectFlags;
8+
typedef struct _GClosureNotifyData GClosureNotifyData;
9+
extern void gtk_init(int *, char **[]);
10+
void gtk_main();
11+
void gtk_main_quit();
12+
typedef struct _GtkWidget GtkWidget;
13+
typedef struct _GtkWindow GtkWindow;
14+
typedef struct _GtkContainer GtkContainer;
15+
typedef enum
16+
{
17+
GTK_WINDOW_TOPLEVEL,
18+
GTK_WINDOW_POPUP
19+
} GtkWindowType;
20+
21+
GtkWidget * gtk_window_new (GtkWindowType type);
22+
void gtk_window_set_default_size (GtkWindow *window,gint width,gint height);
23+
void gtk_widget_show (GtkWidget *);
24+
void gtk_window_set_title (GtkWindow *window, gchar *title);
25+
void gtk_window_get_size (GtkWindow *window, gint *width, gint *height);
26+
void gtk_container_add (GtkContainer *container, GtkWidget *widget);
27+
GtkWidget *gtk_button_new_with_label (const gchar *label);
28+
void gtk_widget_show_all (GtkWidget *);
29+
gulong g_signal_connect_data(
30+
gpointer instance,
31+
const gchar *detailed_signal,
32+
GCallback c_handler,
33+
void* data,
34+
GCallback destroy_data,
35+
int connect_flags
36+
);

src/FfiGtk.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/Gtk.php

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,93 @@
11
<?php
22

3-
namespace Kmaestro\Gtk3;
3+
declare(strict_types=1);
4+
5+
namespace Gtk3;
46

57
use FFI;
8+
use Gtk3\Library;
9+
use Gtk3\Support\ProxyTrait;
10+
use Gtk3\Support\SingletonTrait;
11+
use Serafim\FFILoader\LibraryInformation;
12+
use Serafim\FFILoader\LibraryInterface;
13+
use Serafim\FFILoader\Loader;
614

715
/**
816
* Class Gtk3
9-
* @package Kmaestro
1017
*/
1118
class Gtk
1219
{
13-
private function __clone() {}
14-
private function __wakeup() {}
15-
private function __construct() {}
20+
use ProxyTrait;
21+
use SingletonTrait;
22+
23+
public LibraryInformation $info;
24+
25+
public Loader $loader;
26+
27+
/**
28+
* Gtk constructor.
29+
*/
30+
public function __construct()
31+
{
32+
$this->loader = $this->loader();
33+
34+
$this->info = $this->loadLibrary(new Library());
35+
36+
self::setInstance($this);
37+
}
38+
39+
/**
40+
* @return Loader
41+
*/
42+
private function loader(): Loader
43+
{
44+
$loader = new Loader();
45+
46+
$pre = $loader->preprocessor();
47+
$pre->keepComments = false;
48+
$pre->minify = false;
49+
$pre->tolerant = false;
50+
51+
return $loader;
52+
}
53+
54+
/**
55+
* @param LibraryInterface $library
56+
* @return LibraryInformation
57+
*/
58+
public function loadLibrary(LibraryInterface $library): LibraryInformation
59+
{
60+
return $this->loader->load($library);
61+
}
1662

17-
public static function init()
63+
/**
64+
* @param int $argc
65+
* @param array $argv
66+
* @throws \Exception
67+
*/
68+
public function init(int $argc = 0, array $argv = [])
1869
{
19-
$argc = FFI::new('int');
20-
$argv = FFI::new('char[0]');
21-
$pargv = FFI::addr($argv);
70+
$argcPtr = $this->new('int');
71+
$argcPtr->cdata = $argc;
72+
$argvPtr = FFI::new('char**');
73+
$argcPtr->cdata = $argc;
2274

23-
FfiGtk::getFFI()->gtk_init(FFI::addr($argc), FFI::addr($pargv));
75+
$this->gtk_init(FFI::addr($argcPtr), FFI::addr($argvPtr));
2476
}
2577

2678
/**
2779
*
2880
*/
29-
public static function main(): void
81+
public function main(): void
3082
{
31-
FfiGtk::getFFI()->gtk_main();
83+
$this->gtk_main();
3284
}
3385

3486
/**
3587
*
3688
*/
37-
public static function mainQuit(): void
89+
public function mainQuit(): void
3890
{
39-
FfiGtk::getFFI()->gtk_main_quit();
91+
$this->gtk_main_quit();
4092
}
4193
}

src/Gtk/Button.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
<?php
22

3+
declare(strict_types=1);
34

4-
namespace Kmaestro\Gtk3\Gtk;
5-
5+
namespace Gtk3\Gtk;
66

7+
use Gtk3\Gtk;
78
use Kmaestro\Gtk3\FfiGtk;
89

10+
/**
11+
* Class Button
12+
*/
913
class Button extends Widget
1014
{
1115
public $instance;
1216

1317
public function __construct(string $text)
1418
{
15-
$this->instance = FfiGtk::getFFI()->gtk_button_new_with_label($text);
19+
$this->instance = Gtk::getInstance()->gtk_button_new_with_label($text);
1620
}
1721
}

src/Gtk/Container.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
<?php
22

3+
declare(strict_types=1);
34

4-
namespace Kmaestro\Gtk3\Gtk;
5+
namespace Gtk3\Gtk;
56

7+
use Gtk3\Gtk;
8+
use Gtk3\Gtk\Window;
69

7-
use Kmaestro\Gtk3\FfiGtk;
8-
10+
/**
11+
* Class Container
12+
*/
913
class Container
1014
{
11-
1215
public function add(Window $window, Widget $widget)
1316
{
14-
FfiGtk::getFFI()->gtk_container_add(FfiGtk::getFFI()->cast("GtkContainer *", $window->instance), FfiGtk::getFFI()->cast("GtkWidget *", $widget->instance));
17+
Gtk::getInstance()->gtk_container_add(
18+
Gtk::getInstance()->cast("GtkContainer *", $window->instance),
19+
Gtk::getInstance()->cast("GtkWidget *", $widget->instance)
20+
);
1521
}
1622
}

src/Gtk/Signal.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gtk3\Gtk;
6+
7+
use Gtk3\Gtk;
8+
9+
/**
10+
* Class Signal
11+
*/
12+
class Signal
13+
{
14+
public function connect($instance, $detailed_signal, $c_handler, $data = null)
15+
{
16+
return Gtk::getInstance()->g_signal_connect_data($instance, $detailed_signal, $c_handler, $data, null, null);
17+
}
18+
}

src/Gtk/Widget.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3+
declare(strict_types=1);
34

4-
namespace Kmaestro\Gtk3\Gtk;
5-
5+
namespace Gtk3\Gtk;
66

7+
use Gtk3\Gtk;
78
use Kmaestro\Gtk3\FfiGtk;
89

910
/**
1011
* Class Widget
11-
* @package Kmaestro\Gtk
1212
*/
1313
class Widget
1414
{
@@ -17,13 +17,13 @@ class Widget
1717
*/
1818
public static function show(Window $window)
1919
{
20-
FfiGtk::getFFI()->gtk_widget_show(FfiGtk::getFFI()->cast("GtkWidget *", $window->instance));
20+
Gtk::getInstance()->gtk_widget_show(Gtk::getInstance()->cast("GtkWidget *", $window->instance));
2121
}
2222
/**
2323
* @param Window $window
2424
*/
2525
public static function showAll(Window $window)
2626
{
27-
FfiGtk::getFFI()->gtk_widget_show_all(FfiGtk::getFFI()->cast("GtkWidget *", $window->instance));
27+
Gtk::getInstance()->gtk_widget_show_all(Gtk::getInstance()->cast("GtkWidget *", $window->instance));
2828
}
2929
}

src/Gtk/Window.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3+
declare(strict_types=1);
34

4-
namespace Kmaestro\Gtk3\Gtk;
5-
5+
namespace Gtk3\Gtk;
66

7+
use Gtk3\Gtk;
78
use Kmaestro\Gtk3\FfiGtk;
89

910
/**
1011
* Class Window
11-
* @package Kmaestro\Gtk
1212
*/
1313
class Window
1414
{
@@ -22,14 +22,22 @@ class Window
2222
*/
2323
public function __construct()
2424
{
25-
$this->instance = FfiGtk::getFFI()->gtk_window_new(0);
25+
$this->instance = Gtk::getInstance()->gtk_window_new(0);
26+
}
27+
28+
/**
29+
* @param string $title
30+
*/
31+
public function setTitle(string $title)
32+
{
33+
Gtk::getInstance()->gtk_window_set_title(Gtk::getInstance()->cast("GtkWindow *", $this->instance), $title);
2634
}
2735

2836
/**
2937
* @param string $title
3038
*/
31-
public function setTitle(string $title="")
39+
public function setSize(int $width, int $height)
3240
{
33-
FfiGtk::getFFI()->gtk_window_set_title(FfiGtk::getFFI()->cast("GtkWindow *", $this->instance), $title);
41+
Gtk::getInstance()->gtk_window_set_default_size(Gtk::getInstance()->cast("GtkWindow *", $this->instance), $width, $height);
3442
}
3543
}

0 commit comments

Comments
 (0)