2024.8.23
Loading...
Searching...
No Matches
wb_windows.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 Windows functions
11
12*******************************************************************************/
13
14// so this file will not be necessary in the future
15
16//-------------------------------------------------------------------- CONSTANTS
17
18// Windows constants
19
20define("BM_SETCHECK", 241);
21define("LVM_FIRST", 0x1000);
22define("LVM_DELETEALLITEMS", (LVM_FIRST+9));
23define("LVM_GETITEMCOUNT", (LVM_FIRST+4));
24define("LVM_GETITEMSTATE", (LVM_FIRST+44));
25define("LVM_GETSELECTEDCOUNT", (LVM_FIRST+50));
26define("LVIS_SELECTED", 2);
27define("TCM_GETCURSEL", 4875);
28define("CB_FINDSTRINGEXACT", 344);
29define("CB_SETCURSEL", 334);
30define("LB_FINDSTRINGEXACT", 418);
31define("LB_SETCURSEL", 390);
32define("TCM_SETCURSEL", 4876);
33define("WM_SETTEXT", 12);
34
35//------------------------------------------------------------- WINDOW FUNCTIONS
36
37/*
38
39Creates a window control, menu, toolbar, status bar or accelerator.
40
41*/
42
43function wb_create_control($parent, $class, $caption="", $xpos=0, $ypos=0, $width=0, $height=0, $id=null, $style=0, $lparam=null, $ntab=0)
44{
45 switch($class) {
46
47 case Accel:
48 return wbtemp_set_accel_table($parent, $caption);
49
50 case ToolBar:
51 return wbtemp_create_toolbar($parent, $caption, $width, $height, $lparam);
52
53 case Menu:
54 return wbtemp_create_menu($parent, $caption);
55
56 case HyperLink:
57 return wbtemp_create_control($parent, $class, $caption, $xpos, $ypos, $width, $height, $id, $style,
58 is_null($lparam) ? NOCOLOR : $lparam, $ntab);
59
60 case ComboBox:
61 case ListBox:
62 case ListView:
63 $ctrl = wbtemp_create_control($parent, $class, $caption, $xpos, $ypos, $width, $height, $id, $style, $lparam, $ntab);
64 if(is_array($caption))
65 wb_set_text($ctrl, $caption[0]);
66 return $ctrl;
67
68 case TreeView:
69 $ctrl = wbtemp_create_control($parent, $class, $caption, $xpos, $ypos, $width, $height, $id, $style, $lparam, $ntab);
70 if(is_array($caption))
71 wb_set_text($ctrl, $caption[0]);
72 return $ctrl;
73
74 case Gauge:
75 case Slider:
76 case ScrollBar:
77 $ctrl = wbtemp_create_control($parent, $class, $caption, $xpos, $ypos, $width, $height, $id, $style, $lparam, $ntab);
78 if($lparam)
79 wb_set_value($ctrl, $lparam);
80 return $ctrl;
81
82 default:
83 return wbtemp_create_control($parent, $class, $caption, $xpos, $ypos, $width, $height, $id, $style, $lparam, $ntab);
84 }
85}
86
87/*
88
89Sets the value of a control or control item
90
91*/
92
93function wb_set_value($ctrl, $value, $item = null)
94{
95 if(!$ctrl)
96 return null;
97
98 $class = wb_get_class($ctrl);
99 switch($class) {
100
101 case ListView: // Array with items to be checked
102
103 if($value === null)
104 break;
105 elseif(is_string($value) && strstr($value, ","))
106 $values = explode(",", $value);
107 elseif(!is_array($value))
108 $values = array($value);
109 else
110 $values = $value;
111 foreach($values as $index)
112 wbtemp_set_listview_item_checked($ctrl, $index, 1);
113 break;
114
115 case TreeView: // Array with items to be checked
116
117 if($item === null)
118 $item = wb_get_selected($ctrl);
119 return wbtemp_set_treeview_item_value($ctrl, $item, $value);
120
121 default:
122
123 if($value !== null) {
124 return wbtemp_set_value($ctrl, $value, $item);
125 }
126 }
127}
128
129
130/*
131
132Gets the text from a control, a control item, or a control sub-item.
133
134*/
135
136function wb_get_text($ctrl, $item=null, $subitem=null)
137{
138 if(!$ctrl)
139 return null;
140
141 if(wb_get_class($ctrl) == ListView) {
142
143 if($item !== null) { // Valid item
144
145 $line = wbtemp_get_listview_text($ctrl, $item);
146 if($subitem === null)
147 return $line;
148 else
149 return $line[$subitem];
150
151 } else { // NULL item
152
153 $sel = wb_get_selected($ctrl);
154 if($sel === null) { // Returns the entire table
155 $items = array();
156 for($i = 0;;$i++) {
157 $item = wbtemp_get_listview_text($ctrl, $i);
158 $all = '';
159 foreach($item as $col)
160 $all .= $col;
161 if($all == '')
162 break;
163 $items[] = $item;
164 }
165 return $items ? $items : null;
166 } else {
167 $items = array();
168 foreach($sel as $row)
169 $items[] = wbtemp_get_listview_text($ctrl, $row);
170 return $items ? $items : null;
171 }
172 }
173
174 } elseif(wb_get_class($ctrl) == TreeView) {
175
176 if($item) {
177 return wbtemp_get_treeview_item_text($ctrl, $item);
178 } else {
179 $sel = wb_get_selected($ctrl);
180 if($sel === null)
181 return null;
182 else {
183 return wbtemp_get_text($ctrl);
184 }
185 }
186
187 } elseif(wb_get_class($ctrl) == ComboBox) {
188
189 return wbtemp_get_text($ctrl, $item === null ? -1 : $item);
190
191 } elseif(wb_get_class($ctrl) == ListBox) {
192
193 return wbtemp_get_text($ctrl, $item === null ? -1 : $item);
194
195 } else {
196
197 return wbtemp_get_text($ctrl, $item);
198
199 }
200}
201
202/*
203
204Sets the text of a control.
205In a ListView, it creates columns: each element of the array text is a column.
206In a tab control, it renames the tabs.
207Sets the text of a control item.
208
209*/
210
211function wb_set_text($ctrl, $text, $item=null, $subitem=null)
212{
213 if(!$ctrl)
214 return null;
215
216 switch(wb_get_class($ctrl)) {
217
218 case ListView:
219
220 if($item !== null) {
221
222 if(!is_array($text) && $subitem !== null) {
223
224 // Set text of a ListView cell according to $item and $subitem
225
226 wbtemp_set_listview_item_text($ctrl, $item, $subitem, $text);
227
228 } else {
229
230 // Set text of several ListView cells, ignoring $subitem
231
232 for($sub = 0; $sub < count($text); $sub++) {
233 if($text) {
234 if(($text[$sub] !== null)) {
235 wbtemp_set_listview_item_text($ctrl, $item, $sub, (string)$text[$sub]);
236 }
237 } else {
238 wbtemp_set_listview_item_text($ctrl, $item, $sub, "");
239 }
240 }
241 }
242
243 } else {
244
245 if(!is_array($text))
246 $text = explode(",", $text);
247
248 wb_delete_items($ctrl, null);
249
250 if(!$item) {
251 wbtemp_clear_listview_columns($ctrl);
252
253 // Create column headers
254 // In the loop below, passing -1 as the 'width' argument of wbtemp_create_listview_column()
255 // makes it calculate the column width automatically
256
257 for($i = 0; $i < count($text); $i++) {
258 if(is_array($text[$i]))
259 wbtemp_create_listview_column($ctrl, $i,
260 (string)$text[$i][0],
261 isset($text[$i][1]) ? (int)$text[$i][1] : -1,
262 isset($text[$i][2]) ? (int)$text[$i][2] : WBC_LEFT
263 );
264 else
265 wbtemp_create_listview_column($ctrl, $i,
266 (string)$text[$i], -1, 0);
267 }
268 }
269 }
270 break;
271
272 case ListBox:
273
274 if(!$text) {
275 wb_delete_items($ctrl);
276 } elseif(is_string($text)) {
277 if(strchr($text, "\r") || strchr($text, "\n")) {
278 $text = preg_split("/[\r\n,]/", $text);
279 wb_delete_items($ctrl);
280 foreach($text as $str)
281 wbtemp_create_item($ctrl, (string)$str);
282 } else {
283 $index = wb_send_message($ctrl, LB_FINDSTRINGEXACT, -1, wb_get_address($text));
284 wb_send_message($ctrl, LB_SETCURSEL, $index, 0);
285 }
286 } elseif(is_array($text)) {
287 wb_delete_items($ctrl);
288 foreach($text as $str)
289 wbtemp_create_item($ctrl, (string)$str);
290 }
291 return;
292
293 case ComboBox:
294
295 if(!$text)
296 wb_delete_items($ctrl);
297 elseif(is_string($text)) {
298 if(strchr($text, "\r") || strchr($text, "\n")) {
299 $text = preg_split("/[\r\n,]/", $text);
300 wb_delete_items($ctrl);
301 foreach($text as $str)
302 wbtemp_create_item($ctrl, (string)$str);
303 } else {
304 $index = wb_send_message($ctrl, CB_FINDSTRINGEXACT, -1, wb_get_address($text));
305 wb_send_message($ctrl, CB_SETCURSEL, $index, 0);
306 if($index == -1)
307 wb_send_message($ctrl, WM_SETTEXT, 0, wb_get_address($text));
308 }
309 } elseif(is_array($text)) {
310 wb_delete_items($ctrl);
311 foreach($text as $str)
312 wbtemp_create_item($ctrl, (string)$str);
313 }
314 return;
315
316 case TreeView:
317
318 if($item)
319 return wbtemp_set_treeview_item_text($ctrl, $item, $text);
320 else
321 return wb_create_items($ctrl, $text, true);
322
323 default:
324 // The (string) cast below works well but is a temporary fix, must be
325 // removed when wbtemp_set_text() accepts numeric types correctly
326 if(is_array($text))
327 return wbtemp_set_text($ctrl, $text, $item);
328 else
329 return wbtemp_set_text($ctrl, (string)$text, $item);
330 }
331}
332
333/*
334
335Selects one or more items. Compare with wb_set_value() which checks items instead.
336
337*/
338
339function wb_set_selected($ctrl, $selitems, $selected=TRUE)
340{
341 switch(wb_get_class($ctrl)) {
342
343 case ComboBox:
344 wb_send_message($ctrl, CB_SETCURSEL, (int)$selitems, 0);
345 break;
346
347 case ListBox:
348 wb_send_message($ctrl, LB_SETCURSEL, (int)$selitems, 0);
349 break;
350
351 case ListView:
352
353 if(is_null($selitems)) {
354 return wbtemp_select_all_listview_items($ctrl, false);
355 } elseif(is_array($selitems)) {
356 foreach($selitems as $item)
357 wbtemp_select_listview_item($ctrl, $item, $selected);
358 return TRUE;
359 } else
360 return wbtemp_select_listview_item($ctrl, $selitems, $selected);
361 break;
362
363 case Menu:
364 return wbtemp_set_menu_item_checked($ctrl, $selitems, $selected);
365
366 case TabControl:
367 wbtemp_select_tab($ctrl, (int)$selitems);
368 break;
369
370 case TreeView:
371 wbtemp_set_treeview_item_selected($ctrl, $selitems);
372 break;
373
374 default:
375 return false;
376 }
377 return true;
378}
379
380/*
381
382Creates one or more items in a control.
383
384*/
385
386function wb_create_items($ctrl, $items, $clear=false, $param=null)
387{
388 switch(wb_get_class($ctrl)) {
389
390 case ListView:
391
392 if($clear)
393 wb_send_message($ctrl, LVM_DELETEALLITEMS, 0, 0);
394
395 $last = -1;
396
397 // For each row
398
399 for($i = 0; $i < count($items); $i++) {
400 if(!is_scalar($items[$i]))
401 $last = wbtemp_create_listview_item(
402 $ctrl, -1, -1, (string)$items[$i][0]);
403 else
404 $last = wbtemp_create_listview_item(
405 $ctrl, -1, -1, (string)$items[$i]);
406 wbtemp_set_listview_item_text($ctrl, -1, 0, (string)$items[$i][0]);
407
408 // For each column except the first
409
410 for($sub = 0; $sub < count($items[$i]) - 1; $sub++) {
411 if($param) {
412 $result = call_user_func($param, // Callback function
413 $items[$i][$sub + 1], // Item value
414 $i, // Row
415 $sub // Column
416 );
417 wbtemp_set_listview_item_text($ctrl, $last, $sub + 1, $result);
418 } else
419 wbtemp_set_listview_item_text($ctrl, $last, $sub + 1, (string)$items[$i][$sub + 1]);
420 }
421 }
422 return $last;
423 break;
424
425 case TreeView:
426
427 if($clear)
428 $handle = wb_delete_items($ctrl); // Empty the treeview
429
430 if(!$items)
431 break;
432 $ret = array();
433 for($i = 0; $i < count($items); $i++) {
434 $ret[] = wbtemp_create_treeview_item($ctrl,
435 (string)$items[$i][0], // Name
436 isset($items[$i][1]) ? $items[$i][1] : 0, // Value
437 isset($items[$i][2]) ? $items[$i][2] : 0, // Where
438 isset($items[$i][3]) ? $items[$i][3] : -1, // ImageIndex
439 isset($items[$i][4]) ? $items[$i][4] : -1, // SelectedImageIndex
440 isset($items[$i][5]) ? $items[$i][5] : 0 // InsertionType
441 );
442 }
443 return (count($ret) > 1 ? $ret : $ret[0]);
444 break;
445
446 case StatusBar:
447 wbtemp_create_statusbar_items($ctrl, $items, $clear, $param);
448 return true;
449
450 default:
451
452 if(is_array($items)) {
453 foreach($items as $item)
454 wbtemp_create_item($ctrl, $item);
455 return true;
456 } else
457 return wbtemp_create_item($ctrl, $items);
458 break;
459 }
460}
461
462/*
463
464Opens the standard Open dialog box.
465
466*/
467
468function wb_sys_dlg_open($parent=null, $title=null, $filter=null, $path=null, $filename=null, $flags = null)
469{
470 $filter = _make_file_filter($filter ? $filter : $filename);
471 return wbtemp_sys_dlg_open($parent, $title, $filter, $path, $flags);
472}
473
474/*
475
476Opens the standard Save As dialog box.
477
478*/
479
480function wb_sys_dlg_save($parent=null, $title=null, $filter=null, $path=null, $filename=null, $defext=null)
481{
482 $filter = _make_file_filter($filter ? $filter : $filename);
483
484 return wbtemp_sys_dlg_save($parent, $title, $filter, $path, $filename, $defext);
485}
486
487//----------------------------------------- AUXILIARY FUNCTIONS FOR INTERNAL USE
488
489/*
490
491Creates a file filter for Open/Save dialog boxes based on an array.
492
493*/
494
495function _make_file_filter($filter)
496{
497 if(!$filter)
498 return "All Files (*.*)\0*.*\0\0";
499
500 if(is_array($filter)) {
501 $result = "";
502 foreach($filter as $line)
503 $result .= "$line[0] ($line[1])\0$line[1]\0";
504 $result .= "\0";
505 return $result;
506 } else
507 return $filter;
508}
509
510//-------------------------------------------------------------------------- END
511
512?>
$result
const CB_FINDSTRINGEXACT
const LB_SETCURSEL
wb_set_text($ctrl, $text, $item=null, $subitem=null)
_make_file_filter($filter)
wb_set_value($ctrl, $value, $item=null)
const LVM_FIRST
wb_create_items($ctrl, $items, $clear=false, $param=null)
wb_create_control($parent, $class, $caption="", $xpos=0, $ypos=0, $width=0, $height=0, $id=null, $style=0, $lparam=null, $ntab=0)
const LVM_DELETEALLITEMS
const LB_FINDSTRINGEXACT
wb_sys_dlg_open($parent=null, $title=null, $filter=null, $path=null, $filename=null, $flags=null)
wb_get_text($ctrl, $item=null, $subitem=null)
const CB_SETCURSEL
wb_set_selected($ctrl, $selitems, $selected=TRUE)
wb_sys_dlg_save($parent=null, $title=null, $filter=null, $path=null, $filename=null, $defext=null)
const WM_SETTEXT