Skip to content

Commit 5bda565

Browse files
Refactor TopLevel class to enhance structure, improve option handling, and add additional functionality
1 parent c6488ff commit 5bda565

File tree

1 file changed

+155
-46
lines changed

1 file changed

+155
-46
lines changed

src/Widget/TopLevel.php

Lines changed: 155 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,176 @@
11
<?php
2+
23
namespace PhpGui\Widget;
34

5+
use PhpGui\ProcessTCL;
6+
47
/**
58
* Class TopLevel
69
* Represents a top-level window in the GUI.
710
*
811
* @package PhpGui\Widget
912
*/
10-
class TopLevel extends AbstractWidget {
11-
public function __construct(array $options = []) {
13+
class TopLevel extends AbstractWidget
14+
{
15+
public function __construct(array $options = [])
16+
{
1217
parent::__construct(null, $options); // null parent for top-level widget
1318
$this->create();
1419
}
15-
16-
protected function create(): void {
17-
$text = $this->options['text'] ?? 'Top Level';
18-
$this->tcl->evalTcl("toplevel .{$this->id}");
19-
$this->tcl->evalTcl("label .{$this->id}.child -text \"{$text}\"");
20-
}
21-
22-
public function chooseColor(): string {
23-
$this->tcl->evalTcl("set chosen [tk_chooseColor -initialcolor red]");
24-
return $this->tcl->getResult();
25-
}
26-
27-
public function chooseDirectory(): string {
28-
$this->tcl->evalTcl("set dir [tk_chooseDirectory -initialdir \"/tmp\"]");
29-
return $this->tcl->getResult();
30-
}
31-
32-
public function dialog(string $title, string $message, string $icon, string $option1, string $option2, string $extra): string {
33-
$this->tcl->evalTcl("set result [tk_dialog .dialog \"$title\" \"$message\" \"$icon\" \"$option1\" \"$option2\" \"$extra\"]");
34-
return $this->tcl->getResult();
35-
}
36-
37-
public function getOpenFile(): string {
38-
$this->tcl->evalTcl("set open [tk_getOpenFile -initialdir \"/tmp\"]");
39-
return $this->tcl->getResult();
40-
}
41-
42-
public function getSaveFile(): string {
43-
$this->tcl->evalTcl("set save [tk_getSaveFile -initialdir \"/tmp\"]");
44-
return $this->tcl->getResult();
45-
}
46-
47-
public function messageBox(string $message, string $type): string {
48-
$this->tcl->evalTcl("set mresult [tk_messageBox -message \"$message\" -type $type]");
49-
return $this->tcl->getResult();
50-
}
51-
52-
public function popupMenu(int $x, int $y): string {
20+
21+
protected function create(): void
22+
{
23+
$extra = $this->getOptionString();
24+
$this->tcl->evalTcl("toplevel .{$this->id} {$extra}");
25+
26+
// Set window title if provided
27+
if (isset($this->options['title'])) {
28+
$this->tcl->evalTcl("wm title .{$this->id} \"{$this->options['title']}\"");
29+
}
30+
31+
// Set window size if provided
32+
if (isset($this->options['width'], $this->options['height'])) {
33+
$this->tcl->evalTcl("wm geometry .{$this->id} {$this->options['width']}x{$this->options['height']}");
34+
}
35+
}
36+
37+
protected function getOptionString(): string
38+
{
39+
$opts = "";
40+
foreach ($this->options as $key => $value) {
41+
if (in_array($key, ['title', 'width', 'height'])) continue;
42+
$opts .= " -$key \"$value\"";
43+
}
44+
return $opts;
45+
}
46+
47+
public function setTitle(string $title): void
48+
{
49+
$this->tcl->evalTcl("wm title .{$this->id} \"{$title}\"");
50+
}
51+
52+
public function setGeometry(int $width, int $height, ?int $x = null, ?int $y = null): void
53+
{
54+
$geometry = "{$width}x{$height}";
55+
if ($x !== null && $y !== null) {
56+
$geometry .= "+{$x}+{$y}";
57+
}
58+
$this->tcl->evalTcl("wm geometry .{$this->id} {$geometry}");
59+
}
60+
61+
public function iconify(): void
62+
{
63+
$this->tcl->evalTcl("wm iconify .{$this->id}");
64+
}
65+
66+
public function deiconify(): void
67+
{
68+
$this->tcl->evalTcl("wm deiconify .{$this->id}");
69+
}
70+
71+
public function withdraw(): void
72+
{
73+
$this->tcl->evalTcl("wm withdraw .{$this->id}");
74+
}
75+
76+
public function focus(): void
77+
{
78+
$this->tcl->evalTcl("focus .{$this->id}");
79+
}
80+
81+
public function setResizable(bool $width, bool $height): void
82+
{
83+
$w = $width ? "1" : "0";
84+
$h = $height ? "1" : "0";
85+
$this->tcl->evalTcl("wm resizable .{$this->id} {$w} {$h}");
86+
}
87+
88+
public function setMinsize(int $width, int $height): void
89+
{
90+
$this->tcl->evalTcl("wm minsize .{$this->id} {$width} {$height}");
91+
}
92+
93+
public function setMaxsize(int $width, int $height): void
94+
{
95+
$this->tcl->evalTcl("wm maxsize .{$this->id} {$width} {$height}");
96+
}
97+
98+
public static function chooseColor(string $initialColor = 'red'): ?string
99+
{
100+
$tcl = ProcessTCL::getInstance();
101+
$tcl->evalTcl("set ::chosen [tk_chooseColor -parent . -initialcolor {$initialColor}]");
102+
$tcl->evalTcl("update idletasks");
103+
$result = trim($tcl->getVar("::chosen"));
104+
return ($result === "" || $result === "none") ? null : $result;
105+
}
106+
107+
public static function chooseDirectory(string $initialDir = "."): ?string
108+
{
109+
$tcl = ProcessTCL::getInstance();
110+
$tcl->evalTcl("set ::chosen [tk_chooseDirectory -parent . -initialdir \"$initialDir\"]");
111+
$tcl->evalTcl("update idletasks");
112+
$result = trim($tcl->getVar("::chosen"));
113+
return ($result === "" || $result === "none") ? null : $result;
114+
}
115+
116+
public static function getOpenFile(string $initialDir = "."): ?string
117+
{
118+
$tcl = ProcessTCL::getInstance();
119+
$tcl->evalTcl("set ::chosen [tk_getOpenFile -parent . -initialdir \"$initialDir\"]");
120+
$tcl->evalTcl("update idletasks");
121+
$result = trim($tcl->getVar("::chosen"));
122+
return ($result === "" || $result === "none") ? null : $result;
123+
}
124+
125+
public static function getSaveFile(string $initialDir = "."): ?string
126+
{
127+
$tcl = ProcessTCL::getInstance();
128+
$tcl->evalTcl("set ::chosen [tk_getSaveFile -parent . -initialdir \"$initialDir\"]");
129+
$tcl->evalTcl("update idletasks");
130+
$result = trim($tcl->getVar("::chosen"));
131+
return ($result === "" || $result === "none") ? null : $result;
132+
}
133+
134+
public static function messageBox(string $message, string $type = "ok"): string
135+
{
136+
$tcl = ProcessTCL::getInstance();
137+
$tcl->evalTcl("set ::chosen [tk_messageBox -parent . -message \"$message\" -type $type]");
138+
$tcl->evalTcl("update idletasks");
139+
$result = trim($tcl->getVar("::chosen"));
140+
return $result;
141+
}
142+
143+
public function dialog(string $title, string $message, string $icon, string $option1, string $option2 = "", string $extra = ""): string
144+
{
145+
$this->tcl->evalTcl("set ::chosen [tk_dialog .dialog \"$title\" \"$message\" \"$icon\" \"$option1\" \"$option2\" \"$extra\"]");
146+
$this->tcl->evalTcl("update idletasks");
147+
$result = trim($this->tcl->getVar("::chosen"));
148+
return $result;
149+
}
150+
151+
public function popupMenu(int $x, int $y): string
152+
{
53153
$this->tcl->evalTcl("menu .popup -tearoff 0");
54-
$this->tcl->evalTcl("set presult [tk_popup .popup $x $y]");
55-
$result = $this->tcl->getResult();
154+
$this->tcl->evalTcl("set ::chosen [tk_popup .popup $x $y]");
155+
$this->tcl->evalTcl("update idletasks");
156+
$result = trim($this->tcl->getVar("::chosen"));
56157
$this->tcl->evalTcl("destroy .popup");
57158
return $result;
58159
}
59-
60-
public function setText(string $text): void {
160+
161+
public function getText(): string
162+
{
163+
$this->tcl->evalTcl("set ::childtext [.{$this->id}.child cget -text]");
164+
return trim($this->tcl->getVar("::childtext"));
165+
}
166+
167+
public function setText(string $text): void
168+
{
61169
$this->tcl->evalTcl(".{$this->id}.child configure -text \"{$text}\"");
62170
}
63-
64-
public function destroy(): void {
171+
172+
public function destroy(): void
173+
{
65174
$this->tcl->evalTcl("destroy .{$this->id}");
66175
}
67176
}

0 commit comments

Comments
 (0)