|
6 | 6 | use PhpGui\Widget\Window;
|
7 | 7 | use PhpGui\Widget\Label;
|
8 | 8 | use PhpGui\Widget\Button;
|
9 |
| -use PhpGui\Widget\Input; // New Input widget |
| 9 | +use PhpGui\Widget\Input; |
| 10 | +use PhpGui\Widget\TopLevel; |
| 11 | +use PhpGui\Widget\Menu; |
| 12 | + |
10 | 13 |
|
11 | 14 | $app = new Application();
|
12 | 15 |
|
13 | 16 | // Create main window
|
14 | 17 | $window = new Window([
|
15 | 18 | 'title' => 'Hello World Example in php',
|
16 |
| - 'width' => 500, |
17 |
| - 'height' => 300 |
| 19 | + 'width' => 800, |
| 20 | + 'height' => 600 |
18 | 21 | ]);
|
19 | 22 |
|
20 | 23 | // Label Example
|
21 |
| -$label = new Label($window->getId(), [ |
| 24 | +$label = new Label($window->getId(), [ |
22 | 25 | 'text' => 'Hello, PHP GUI World!'
|
23 | 26 | ]);
|
24 | 27 | $label->pack(['pady' => 20]);
|
25 | 28 |
|
26 |
| -// Button Example |
27 |
| -$button = new Button($window->getId(), [ |
28 |
| - 'text' => 'Click Me', |
29 |
| - 'command' => function() use ($label) { |
30 |
| - echo "Button clicked!\n"; |
31 |
| - $label->setText('Button clicked!'); |
32 |
| - } |
33 |
| -]); |
34 |
| -$button->pack(['pady' => 10]); |
35 | 29 |
|
36 | 30 | // Extra styled Button example
|
37 | 31 | $styledButton = new Button($window->getId(), [
|
38 | 32 | 'text' => 'Styled Button',
|
39 |
| - 'command' => function() use ($label) { |
| 33 | + 'command' => function () use ($label) { |
40 | 34 | echo "Styled Button clicked!\n";
|
41 | 35 | $label->setText('Styled Button clicked!');
|
42 | 36 | },
|
|
56 | 50 | $input->pack(['pady' => 10]);
|
57 | 51 |
|
58 | 52 | // Register event listener for Enter key on the input widget
|
59 |
| -$input->onEnter(function() use ($input) { |
| 53 | +$input->onEnter(function () use ($input) { |
60 | 54 | echo "Input Enter Pressed: " . $input->getValue() . "\n";
|
61 | 55 | });
|
62 | 56 |
|
63 |
| -// New button to show the input box value on click |
64 |
| -$showButton = new Button($window->getId(), [ |
65 |
| - 'text' => 'Show Input', |
66 |
| - 'command' => function() use ($input) { |
67 |
| - echo "Show Input Button clicked: " . $input->getValue() . "\n"; |
| 57 | + |
| 58 | +$styledLabel = new Label($window->getId(), [ |
| 59 | + 'text' => 'This is a styled label with custom colors', |
| 60 | + 'fg' => 'white', |
| 61 | + 'bg' => '#4CAF50', |
| 62 | + 'font' => 'Arial 12', |
| 63 | + 'padx' => 10, |
| 64 | + 'pady' => 5, |
| 65 | + 'relief' => 'raised' |
| 66 | +]); |
| 67 | +$styledLabel->pack(['pady' => 5]); |
| 68 | + |
| 69 | +// Dynamic Label update example |
| 70 | +$dynamicLabel = new Label($window->getId(), [ |
| 71 | + 'text' => 'Dynamic Lable', |
| 72 | + 'font' => 'Arial 11 italic', |
| 73 | + 'fg' => '#666666' |
| 74 | +]); |
| 75 | +$dynamicLabel->pack(['pady' => 5]); |
| 76 | + |
| 77 | +// Button to demonstrate label updates |
| 78 | +$updateButton = new Button($window->getId(), [ |
| 79 | + 'text' => 'Update Labels', |
| 80 | + 'command' => function () use ($dynamicLabel, $styledLabel) { |
| 81 | + $dynamicLabel->setText('Label text updated!'); |
| 82 | + $dynamicLabel->setForeground('#009688'); |
| 83 | + $styledLabel->setBackground('#2196F3'); |
| 84 | + $styledLabel->setText('Colors and text can be changed dynamically'); |
| 85 | + } |
| 86 | +]); |
| 87 | +$updateButton->pack(['pady' => 5]); |
| 88 | + |
| 89 | + |
| 90 | +// Menu Examples |
| 91 | +$mainMenu = new Menu($window->getId(), ['type' => 'main']); |
| 92 | + |
| 93 | +// File Menu |
| 94 | +$fileMenu = $mainMenu->addSubmenu('File'); |
| 95 | +$fileMenu->addCommand('New', function () use ($dynamicLabel) { |
| 96 | + $dynamicLabel->setText('New File Selected'); |
| 97 | +}); |
| 98 | +$fileMenu->addCommand('Open', function () use ($dynamicLabel) { |
| 99 | + $dynamicLabel->setText('Open Selected'); |
| 100 | +}); |
| 101 | +$fileMenu->addSeparator(); |
| 102 | +$fileMenu->addCommand('Exit', function () { |
| 103 | + exit(); |
| 104 | +}, ['foreground' => 'red']); |
| 105 | + |
| 106 | +// Edit Menu |
| 107 | +$editMenu = $mainMenu->addSubmenu('Edit'); |
| 108 | +$editMenu->addCommand('Copy', function () use ($styledLabel) { |
| 109 | + $styledLabel->setText('Copy Selected'); |
| 110 | +}); |
| 111 | +$editMenu->addCommand('Paste', function () use ($styledLabel) { |
| 112 | + $styledLabel->setText('Paste Selected'); |
| 113 | +}); |
| 114 | + |
| 115 | +// Help Menu with Nested Submenu |
| 116 | +$helpMenu = $mainMenu->addSubmenu('Help'); |
| 117 | +$aboutMenu = $helpMenu->addSubmenu('About'); |
| 118 | +$aboutMenu->addCommand('Version', function () use ($dynamicLabel) { |
| 119 | + $dynamicLabel->setText('Version 1.0'); |
| 120 | +}); |
| 121 | + |
| 122 | +// TopLevel Examples |
| 123 | +$topLevelButton = new Button($window->getId(), [ |
| 124 | + 'text' => 'Open New Window', |
| 125 | + 'command' => function () use ($dynamicLabel) { |
| 126 | + $topLevel = new TopLevel([ |
| 127 | + 'title' => 'New Window Example', |
| 128 | + 'width' => 300, |
| 129 | + 'height' => 200 |
| 130 | + ]); |
| 131 | + |
| 132 | + // Add content to TopLevel |
| 133 | + $label = new Label($topLevel->getId(), [ |
| 134 | + 'text' => 'This is a new window', |
| 135 | + 'font' => 'Arial 14' |
| 136 | + ]); |
| 137 | + $label->pack(['pady' => 20]); |
| 138 | + |
| 139 | + $closeBtn = new Button($topLevel->getId(), [ |
| 140 | + 'text' => 'Close Window', |
| 141 | + 'command' => function () use ($topLevel, $dynamicLabel) { |
| 142 | + $dynamicLabel->setText('TopLevel window closed'); |
| 143 | + $topLevel->destroy(); |
| 144 | + } |
| 145 | + ]); |
| 146 | + $closeBtn->pack(['pady' => 10]); |
| 147 | + |
| 148 | + $minimizeBtn = new Button($topLevel->getId(), [ |
| 149 | + 'text' => 'Minimize', |
| 150 | + 'command' => function () use ($topLevel) { |
| 151 | + $topLevel->iconify(); |
| 152 | + } |
| 153 | + ]); |
| 154 | + $minimizeBtn->pack(['pady' => 10]); |
| 155 | + |
| 156 | + $dynamicLabel->setText('New window opened'); |
| 157 | + } |
| 158 | +]); |
| 159 | +$topLevelButton->pack(['pady' => 10]); |
| 160 | + |
| 161 | +// Dialog Examples |
| 162 | +$dialogsLabel = new Label($window->getId(), [ |
| 163 | + 'text' => 'Dialog Examples:', |
| 164 | + 'font' => 'Arial 12 bold' |
| 165 | +]); |
| 166 | +$dialogsLabel->pack(['pady' => 5]); |
| 167 | + |
| 168 | +// Color Picker Dialog |
| 169 | +$colorButton = new Button($window->getId(), [ |
| 170 | + 'text' => 'Choose Color', |
| 171 | + 'command' => function () use ($dynamicLabel) { |
| 172 | + try { |
| 173 | + $color = TopLevel::chooseColor(); |
| 174 | + if ($color) { |
| 175 | + echo "Selected color: $color\n"; |
| 176 | + $dynamicLabel->setText("Selected color: $color"); |
| 177 | + $dynamicLabel->setForeground($color); |
| 178 | + } |
| 179 | + } catch (\Exception $e) { |
| 180 | + echo "Error: " . $e->getMessage() . "\n"; |
| 181 | + } |
68 | 182 | }
|
69 | 183 | ]);
|
70 |
| -$showButton->pack(['pady' => 10]); |
| 184 | +$colorButton->pack(['pady' => 5]); |
| 185 | + |
| 186 | +// File Selection Dialog |
| 187 | +$fileButton = new Button($window->getId(), [ |
| 188 | + 'text' => 'Open File', |
| 189 | + 'command' => function () use ($dynamicLabel) { |
| 190 | + $file = TopLevel::getOpenFile(); |
| 191 | + if ($file) { |
| 192 | + $dynamicLabel->setText("Selected file: " . basename($file)); |
| 193 | + } |
| 194 | + } |
| 195 | +]); |
| 196 | +$fileButton->pack(['pady' => 5]); |
| 197 | + |
| 198 | +// Directory Selection Dialog |
| 199 | +$dirButton = new Button($window->getId(), [ |
| 200 | + 'text' => 'Choose Directory', |
| 201 | + 'command' => function () use ($dynamicLabel) { |
| 202 | + try { |
| 203 | + $dir = TopLevel::chooseDirectory(); |
| 204 | + if ($dir) { |
| 205 | + echo "Selected directory: $dir\n"; |
| 206 | + $dynamicLabel->setText("Selected directory: " . basename($dir)); |
| 207 | + } |
| 208 | + } catch (\Exception $e) { |
| 209 | + echo "Error: " . $e->getMessage() . "\n"; |
| 210 | + } |
| 211 | + } |
| 212 | +]); |
| 213 | +$dirButton->pack(['pady' => 5]); |
| 214 | + |
| 215 | +// Message Box Example |
| 216 | +$msgButton = new Button($window->getId(), [ |
| 217 | + 'text' => 'Show Message', |
| 218 | + 'command' => function () use ($dynamicLabel) { |
| 219 | + $result = TopLevel::messageBox("This is a test message", "okcancel"); |
| 220 | + $dynamicLabel->setText("Message result: $result"); |
| 221 | + } |
| 222 | +]); |
| 223 | +$msgButton->pack(['pady' => 5]); |
| 224 | + |
71 | 225 |
|
72 | 226 | $app->run();
|
0 commit comments