2024.8.23
Loading...
Searching...
No Matches
wb_resources.inc.php
Go to the documentation of this file.
1<?php
2
3/*******************************************************************************
4
5 WINBINDER - The native Windows binding for PHP for PHP
6
7 Copyright � Hypervisual - see LICENSE.TXT for details
8 Author: Rubem Pechansky (http://winbinder.org/contact.php)
9
10 RC file parser: convert Windows resource file to WinBinder commands
11
12*******************************************************************************/
13
14// so this file will not be necessary in the future
15
16//-------------------------------------------------------------------- CONSTANTS
17
18define("WB_KX_SCREEN", 1.498); // Determined through trial and error
19define("WB_KY_SCREEN", 1.625); // Determined through trial and error
20
21//------------------------------------------------- WINDOWS CONSTANTS FROM WIN.H
22
23define("WS_VISIBLE", 0x10000000);
24define("WS_DISABLED", 0x08000000);
25define("WS_GROUP", 0x00020000);
26define("WS_EX_STATICEDGE", 0x00020000);
27
28// Button styles
29
30define("BS_PUSHBUTTON", 0x00);
31define("BS_CHECKBOX", 0x02);
32define("BS_AUTOCHECKBOX", 0x03);
33define("BS_RADIOBUTTON", 0x04);
34define("BS_GROUPBOX", 0x07);
35define("BS_AUTORADIOBUTTON", 0x09);
36define("BS_ICON", 0x40);
37define("BS_BITMAP", 0x80);
38
39// Edit control styles
40
41define("ES_NUMBER", 0x2000);
42define("ES_PASSWORD", 0x20);
43define("ES_READONLY", 0x0800);
44define("ES_UPPERCASE", 0x08);
45define("ES_LEFT", 0x0);
46define("ES_CENTER", 0x01);
47define("ES_RIGHT", 0x02);
48define("ES_MULTILINE", 0x04);
49
50// Static styles
51
52define("SS_LEFT", 0x00);
53define("SS_CENTER", 0x01);
54define("SS_RIGHT", 0x02);
55define("SS_ETCHEDHORZ", 0x10);
56define("SS_ETCHEDVERT", 0x11);
57define("SS_ETCHEDFRAME", 0x12);
58define("SS_ICON", 0x03);
59define("SS_BITMAP", 0x0E);
60define("SS_LEFTNOWORDWRAP", 0x0C);
61define("SS_WORDELLIPSIS", 0xC000);
62
63// Other styles
64
65define("CBS_SORT", 0x100);
66define("CBS_DROPDOWNLIST", 3);
67
68define("LBS_SORT", 2);
69define("LVS_NOSORTHEADER", 0x00008000);
70define("LVS_GRIDLINES", 0x00800000); // Actually WS_BORDER
71define("LVS_CHECKBOXES", 0x00000800); // Actually LVS_ALIGNLEFT
72define("LVS_SINGLESEL", 0x00000004);
73define("TBS_AUTOTICKS", 1);
74
75//-------------------------------------------------------------------- FUNCTIONS
76
77/*
78
79 Returns the WinBinder code that results from the resource text $rc, usually
80 read from a RC (Windows resource) file.
81
82NOTE: Caption is not used, it's taken from the resource instead. The parameter is kept
83 here just to be compatible with wb_create_window()
84*/
85
86function parse_rc($rc, $winvar='$mainwin', $parent=null, $type="AppWindow", $caption=null,
87 $x=WBC_CENTER, $y=WBC_CENTER, $width=WBC_CENTER, $height=WBC_CENTER, $style=0, $lparam=0,
88 $respath=PATH_RES)
89{
90 global $_winclass, $_usergeom, $path_res;
91
92 // Read file
93
94 $_usergeom = array($x, $y, $width, $height);
95 $path_res = $respath;
96
97 // Remove comments and useless spaces
98
99 $rc = preg_replace("/^\s*;.*$/m", "", $rc);
100 $rc = preg_replace("/^\s*(.*)$/m", "\\1", $rc);
101
102 // Maintain #defines and discard the rest (fixed to work with newer versions of PHP -- thanks Hans)
103
104// $def = preg_replace("/(?!^\s*#define)(.*)$/m", "\\2", $rc);
105 $def = preg_replace('/^((?!#define).)*$/m', "\\2", $rc);
106
107 // Remove blank lines
108
109 $def = preg_replace("/\n+/m", "\n", $def);
110
111 // Change string C #defines to PHP format
112
113 $def = preg_replace("/#define\s+(\w+)\s+\"(.*)\"/", "if(!defined(\"\\1\")) define(\"\\1\", \"\\2\");", $def);
114
115 // Change character C #defines to PHP format
116
117 $def = preg_replace("/#define\s+(\w+)\s+'(.+)'/", "if(!defined(\"\\1\")) define(\"\\1\", \"\\2\");", $def);
118
119 // Change numeric C #defines to PHP format
120
121 $def = preg_replace("/#define\s+(\w+)\s+(\S+)/", "if(!defined(\"\\1\")) define(\"\\1\", \\2);", $def);
122 $def = "// Control identifiers\n\n" . preg_replace("/(\r\n|\r|\n)+/sm", "\n", $def);
123
124 // Return to original string and eliminates the #defines
125
126 $rc = preg_replace("/^\s*#define(.*)$/m", "", $rc);
127
128 // Create the window
129
130 $_winclass = $type;
131
132 $tok = "\s*((?:[\"'][\S \t]*[\"'])|(?:[\S^,'\"]+))\s*"; // Normal or quoted token
133 $rc = "// Create window\n\n" . preg_replace_callback("/^$tok\s+DIALOGEX$tok,$tok,$tok,$tok\s+CAPTION$tok\s+FONT$tok,$tok\s+STYLE$tok\s+EXSTYLE$tok/m", "_scale_dialog", $rc);
134
135 // Create the controls
136
137 $rc = preg_replace_callback("/^\s*CONTROL\s+$tok,$tok,$tok,$tok,$tok,$tok,$tok,$tok,$tok/m", "_scale_controls", $rc);
138
139 // Create BEGIN / END comments
140
141 $rc = preg_replace("/^\s*BEGIN/m", "\n// Insert controls\n", $rc);
142 $rc = preg_replace("/^\s*END/m", "\n// End controls", $rc);
143
144 // Replace variable names
145
146 $rc = str_replace("%WINVAR%", $winvar, $rc);
147 $rc = str_replace("%PARENT%", $parent? $parent : "NULL", $rc);
148 $rc = str_replace("%STYLE%", $style, $rc);
149 $rc = str_replace("%LPARAM%", $lparam, $rc);
150
151 return "$def\n$rc";
152}
153
154//----------------------------------------------------------- INTERNAL FUNCTIONS
155
156function _scale_dialog($c)
157{
158 global $_winclass, $_usergeom, $_tabN;
159
160 if($_winclass == "TabControl") {
161
162 $_tabN++;
163 $code = "wbtemp_create_item(%PARENT%, ". $c[6] . ");\n";
164
165 } else {
166
167 $_addx = 8; //width + 2xborder
168 $_addy = 4 + 42 + 17 + 4; //border + caption + border
169
170 switch(is_string($_winclass) ? strtolower($_winclass) : $_winclass) {
171
172 case "appwindow":
173 $_winclass = AppWindow;
174 $_addx = 8; //width + 2xborder
175 $_addy = 3 + 18 + 22 + 18 + 3; //border + caption + menu + statusbar + border
176 break;
177 case "resizablewindow":
178 $_winclass = ResizableWindow;
179 $_addx = 8; //width + 2xborder
180 $_addy = 4 + 42 + 17 + 4; //border + caption + menu + statusbar + border
181 break;
182 case "modaldialog":
183 $_winclass = ModalDialog;
184 $_addx = 8; //width + 2xborder
185 $_addy = 4 + 42 + 17 + 4; //border + caption + border
186 break;
187 case "modelessdialog":
188 $_winclass = ModelessDialog;
189 break;
190 case "tooldialog":
191 $_winclass = ToolDialog;
192 break;
193 }
194
195 if(!(($_usergeom[0] == WBC_CENTER && $_usergeom[1] == WBC_CENTER &&
196 $_usergeom[2] == WBC_CENTER && $_usergeom[3] == WBC_CENTER))) {
197
198 $code = "%WINVAR% = wb_create_window(" .
199 "%PARENT%, " . // parent
200 "$_winclass, " . // class
201 $c[6] . ", " . // caption
202 $_usergeom[0] . ", " . // left
203 $_usergeom[1] . ", " . // top
204 $_usergeom[2] . ", " . // width
205 $_usergeom[3] . ", " . // height
206 "%STYLE%, " . // style
207 "%LPARAM%);\n"; // lparam
208
209 } else {
210
211 if(is_array($_usergeom)) {
212 if(count($_usergeom) == 2) { // Width, height only
213 $_usergeom[2] = $_usergeom[0];
214 $_usergeom[3] = $_usergeom[1];
215 $_usergeom[0] = WBC_CENTER;
216 $_usergeom[1] = WBC_CENTER;
217 }
218 } elseif(is_null($_usergeom)) {
219 $_usergeom[0] = WBC_DEFAULTPOS;
220 $_usergeom[1] = WBC_DEFAULTPOS;
221 $_usergeom[2] = WBC_DEFAULTPOS;
222 $_usergeom[3] = WBC_DEFAULTPOS;
223 }
224
225 $code = "%WINVAR% = wb_create_window(" .
226 "%PARENT%, " . // parent
227 "$_winclass, " . // class
228 $c[6] . ", " . // caption
229 "WBC_CENTER, " . // left
230 "WBC_CENTER, " . // top
231// (int)($c[4] * WB_KX_SCREEN + $_addx) . ", " .
232// (int)($c[5] * WB_KY_SCREEN + $_addy) . ", " .
233 (int)($c[4] * WB_KX_SCREEN) . ", " .
234 (int)($c[5] * WB_KY_SCREEN) . ", " .
235 "%STYLE%, " . // style
236 "%LPARAM%);\n"; // lparam
237 }
238
239 $_tabN = 0;
240
241 }
242 return $code;
243}
244
245function _scale_controls($c)
246{
247 global $_tabN, $path_res;
248
249 $winclass = $c[3];
250 $winstyle = hexdec($c[4]);
251 $winexstyle = hexdec($c[9]);
252
253 if(_bit_test($winstyle, WS_VISIBLE))
254 $style = "WBC_VISIBLE";
255 else
256 $style = "WBC_INVISIBLE";
257
258 if(_bit_test($winstyle, WS_DISABLED))
259 $style .= " | WBC_DISABLED";
260 else
261 $style .= " | WBC_ENABLED";
262
263 if(_bit_test($winexstyle, WS_EX_STATICEDGE))
264 $style .= " | WBC_BORDER";
265
266 // Set attributes according to control class
267
268 switch(strtolower($winclass)) {
269
270 case '"button"':
271
272 switch($winstyle & 0x0F) {
274 case BS_RADIOBUTTON:
275 $class = "RadioButton";
276 if(_bit_test($winstyle, WS_GROUP))
277 $style .= " | WBC_GROUP";
278 break;
279 case BS_AUTOCHECKBOX:
280 case BS_CHECKBOX:
281 $class = "CheckBox";
282 break;
283 case BS_GROUPBOX:
284 $class = "Frame";
285 break;
286 case BS_PUSHBUTTON:
287 default:
288 $class = "PushButton";
289 break;
290 }
291 break;
292
293 case '"static"':
294
295 switch($winstyle & 0x1F) {
296 case SS_ICON:
297 case SS_BITMAP:
298 $style .= " | WBC_IMAGE | WBC_CENTER";
299 $class = "Frame";
300 break;
301 case SS_ETCHEDHORZ:
302 case SS_ETCHEDVERT:
303 case SS_ETCHEDFRAME:
304 $class = "Frame";
305 break;
306 case SS_CENTER:
307 if(_bit_test($winstyle, SS_WORDELLIPSIS))
308 $style .= " | WBC_ELLIPSIS";
309 $style .= " | WBC_CENTER";
310 $class = "Label";
311 break;
312 case SS_RIGHT:
313 if(_bit_test($winstyle, SS_WORDELLIPSIS))
314 $style .= " | WBC_ELLIPSIS";
315 $style .= " | WBC_RIGHT";
316 $class = "Label";
317 break;
318 case SS_LEFT:
319 default:
320 if(!_bit_test($winstyle, SS_LEFTNOWORDWRAP))
321 $style .= " | WBC_MULTILINE";
322 if(_bit_test($winstyle, SS_WORDELLIPSIS))
323 $style .= " | WBC_ELLIPSIS";
324 $class = "Label";
325 break;
326 }
327 break;
328
329 case '"edit"':
330 $class = "EditBox";
331 if(_bit_test($winstyle, ES_MULTILINE)) {
332 $style .= " | WBC_MULTILINE";
333 } else {
334 switch($winstyle & 0x03) {
335 case ES_CENTER:
336 $style .= " | WBC_CENTER";
337 break;
338 case ES_RIGHT:
339 $style .= " | WBC_RIGHT";
340 break;
341 case ES_LEFT:
342 default:
343 break;
344 }
345 }
346 if(_bit_test($winstyle, ES_READONLY))
347 $style .= " | WBC_READONLY";
348 if(_bit_test($winstyle, ES_PASSWORD))
349 $style .= " | WBC_MASKED";
350 if(_bit_test($winstyle, ES_NUMBER))
351 $style .= " | WBC_NUMBER";
352 break;
353
354 case '"richedit20a"':
355 if(_bit_test($winstyle, ES_READONLY))
356 $style .= " | WBC_READONLY";
357 $class = "RTFEditBox";
358 switch($winstyle & 0x03) {
359 case ES_CENTER:
360 $style .= " | WBC_CENTER";
361 break;
362 case ES_RIGHT:
363 $style .= " | WBC_RIGHT";
364 break;
365 case ES_LEFT:
366 default:
367 break;
368 }
369 break;
370
371 case '"combobox"':
372 $class = "ComboBox";
373 if(_bit_test($winstyle, CBS_SORT))
374 $style .= " | WBC_SORT";
375 if(_bit_test($winstyle, CBS_DROPDOWNLIST))
376 $style .= " | WBC_READONLY";
377 break;
378
379 case '"listbox"':
380 $class = "ListBox";
381 if(_bit_test($winstyle, LBS_SORT))
382 $style .= " | WBC_SORT";
383 break;
384
385 case '"scrollbar"':
386 $class = "ScrollBar";
387 break;
388
389 case '"syslistview32"':
390 $class = "ListView";
391 if(!_bit_test($winstyle, LVS_NOSORTHEADER))
392 $style .= " | WBC_SORT";
393 if(_bit_test($winstyle, LVS_GRIDLINES))
394 $style .= " | WBC_LINES";
395 if(_bit_test($winstyle, LVS_CHECKBOXES))
396 $style .= " | WBC_CHECKBOXES";
397 if(!_bit_test($winstyle, LVS_SINGLESEL))
398 $style .= " | WBC_SINGLE";
399 break;
400
401 case '"systabcontrol32"':
402 $class = "TabControl";
403 break;
404
405 case '"systreeview32"':
406 $class = "TreeView";
407 break;
408
409 case '"toolbarwindow32"':
410 $class = "ToolBar";
411 break;
412
413 case '"msctls_progress32"':
414 $class = "Gauge";
415 break;
416
417 case '"msctls_statusbar32"':
418 $class = "StatusBar";
419 break;
420
421 case '"sysmonthcal32"':
422 $class = "Calendar";
423 break;
424
425 case '"msctls_trackbar32"':
426 $class = "Slider";
427 if(_bit_test($winstyle, TBS_AUTOTICKS))
428 $style .= " | WBC_LINES";
429 break;
430
431 case '"msctls_updown32"':
432 $class = "Spinner";
433 if(_bit_test($winstyle, WS_GROUP))
434 $style .= " | WBC_GROUP";
435 break;
436 }
437
438 // Convert Windows style to WinBinder style
439
440 $str = "wb_create_control(" .
441 "%WINVAR%, " . // Parent
442 $class . ", " . // Class
443 $c[1] . ", " . // Caption
444 (int)($c[5] * WB_KX_SCREEN) . ", " . // Left
445 (int)($c[6] * WB_KY_SCREEN) . ", " . // Top
446 (int)($c[7] * WB_KX_SCREEN) . ", " . // Width
447 (int)($c[8] * WB_KY_SCREEN) . ", " . // Height
448 $c[2] . ", " . // ID
449 $style . ", " . // Style
450 "0" . // Param
451 ($_tabN ? ", " . ($_tabN - 1) . ");\n" : ");\n"); // Tab #
452
453 // Add some attributes to controls where needed
454
455 switch($class) {
456
457 case "Frame":
458
459 if(strstr($style, "WBC_IMAGE")) {
460 if(($winstyle & (SS_BITMAP | SS_ICON)) && ($c[1] !== '""')) {
461 $image = $path_res . _trim_quotes($c[1]);
462 if(preg_match("/\.(bmp|ico)$/", $image))
463 $str = "\$_tmp_ctrl_ = " . $str . "wb_set_image(\$_tmp_ctrl_, '$image', GREEN);" . " unset(\$_tmp_ctrl_);\n";
464 }
465 }
466 break;
467
468 case "PushButton":
469
470 if(($winstyle & (BS_BITMAP | BS_ICON)) && ($c[1] !== '""')) {
471 $image = $path_res . _trim_quotes($c[1]);
472 if($image)
473 if(preg_match("/\.(bmp|ico)$/", $image))
474 $str = "\$_tmp_ctrl_ = " . $str . "wb_set_image(\$_tmp_ctrl_, '$image', GREEN);" . " unset(\$_tmp_ctrl_);\n";
475 }
476 break;
477 }
478
479 return $str;
480}
481
482function _trim_quotes($str)
483{
484 return str_replace('"', '', $str);
485}
486
487function _bit_test($v, $t)
488{
489 return (($v & $t) == $t);
490}
491
492//-------------------------------------------------------------------------- EN
493
494?>
_scale_controls($c)
const WS_DISABLED
_scale_dialog($c)
const WB_KY_SCREEN
_trim_quotes($str)
const WB_KX_SCREEN
const WS_VISIBLE
const ES_LEFT
_bit_test($v, $t)
const ES_READONLY
const WS_EX_STATICEDGE
const ES_PASSWORD
const SS_ICON
parse_rc($rc, $winvar=' $mainwin', $parent=null, $type="AppWindow", $caption=null, $x=WBC_CENTER, $y=WBC_CENTER, $width=WBC_CENTER, $height=WBC_CENTER, $style=0, $lparam=0, $respath=PATH_RES)
const SS_ETCHEDHORZ
const ES_MULTILINE
const CBS_SORT
const SS_LEFTNOWORDWRAP
const LVS_CHECKBOXES
const SS_ETCHEDFRAME
const WS_GROUP
const ES_RIGHT
const BS_AUTOCHECKBOX
const CBS_DROPDOWNLIST
const BS_AUTORADIOBUTTON
const LVS_NOSORTHEADER
const BS_PUSHBUTTON
const BS_BITMAP
const LBS_SORT
const SS_ETCHEDVERT
const SS_CENTER
const SS_RIGHT
const BS_GROUPBOX
const SS_BITMAP
const LVS_GRIDLINES
const BS_CHECKBOX
const LVS_SINGLESEL
const SS_WORDELLIPSIS
const SS_LEFT
const TBS_AUTOTICKS
const BS_RADIOBUTTON
const ES_NUMBER
const BS_ICON
const ES_CENTER