|
1 | 1 | <?php
|
| 2 | + |
2 | 3 | namespace PhpGui\Widget;
|
3 | 4 |
|
| 5 | +use PhpGui\ProcessTCL; |
| 6 | + |
4 | 7 | /**
|
5 | 8 | * Class TopLevel
|
6 | 9 | * Represents a top-level window in the GUI.
|
7 | 10 | *
|
8 | 11 | * @package PhpGui\Widget
|
9 | 12 | */
|
10 |
| -class TopLevel extends AbstractWidget { |
11 |
| - public function __construct(array $options = []) { |
| 13 | +class TopLevel extends AbstractWidget |
| 14 | +{ |
| 15 | + public function __construct(array $options = []) |
| 16 | + { |
12 | 17 | parent::__construct(null, $options); // null parent for top-level widget
|
13 | 18 | $this->create();
|
14 | 19 | }
|
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 | + { |
53 | 153 | $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")); |
56 | 157 | $this->tcl->evalTcl("destroy .popup");
|
57 | 158 | return $result;
|
58 | 159 | }
|
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 | + { |
61 | 169 | $this->tcl->evalTcl(".{$this->id}.child configure -text \"{$text}\"");
|
62 | 170 | }
|
63 |
| - |
64 |
| - public function destroy(): void { |
| 171 | + |
| 172 | + public function destroy(): void |
| 173 | + { |
65 | 174 | $this->tcl->evalTcl("destroy .{$this->id}");
|
66 | 175 | }
|
67 | 176 | }
|
0 commit comments