source\embed\msvisual6.0\c.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2008, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * ID: gentee 18.10.06 0.0.A.
11 *
12 * Author: Alexey Krivonogov
13 *
14 * Summary:
15 *
16 ******************************************************************************/
17
18 #include "resource.h"
19 #include "windows.h"
20 #include "windowsx.h"
21 #include "commctrl.h"
22 #include "../../src/genteeapi/gentee.h"
23 #include "stdio.h"
24
25
26 #define DEF_STRING 256
27 #define MAX_STRING 1024
28
29 HINSTANCE hInst;
30 HINSTANCE gehandle = NULL; // gentee.dll handle
31 HWND hwTab;
32 HWND hwCurItem;
33 HWND hwItemTabs[2];
34 DWORD threadid;
35 HANDLE hthread;
36 char szinitdir[_MAX_PATH];
37 char szfilename[_MAX_PATH];
38 char szmessage[MAX_STRING];
39 char szprint[MAX_STRING];
40
41 DWORD p_sztextcolor;
42 DWORD p_bgcolor;
43 char p_sztext[DEF_STRING];
44 BOOL p_flg;
45
46 typedef int (__cdecl *CDECLPROC)();
47 FARPROC ge_deinit;
48 FARPROC ge_init;
49 FARPROC ge_compile;
50 FARPROC ge_load;
51 FARPROC ge_set;
52 CDECLPROC ge_call;
53 CDECLPROC ge_getid;
54
55 void shell_ge_deinit( void );
56 HINSTANCE shell_ge_init( PVOID messagef, PVOID printf, PVOID exportf );
57
58 void change_tab( DWORD n );
59
60 BOOL CALLBACK main_dlgproc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam );
61 BOOL CALLBACK tab_dlgproc ( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam );
62 LRESULT CALLBACK panel_proc ( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
63
64 //--------------------------------------------------------------------------
65
66 int WINAPI WinMain( HINSTANCE hInstance,
67 HINSTANCE hPrevInstance,
68 LPSTR lpCmdLine,
69 int nCmdShow)
70 {
71 WNDCLASS wc;
72 hInst = hInstance;
73 hwTab = 0;
74 hwCurItem = 0;
75 InitCommonControls();
76
77 wc.style = wc.cbClsExtra = wc.cbWndExtra = 0;
78 wc.lpfnWndProc = panel_proc;
79 wc.hInstance = hInst;
80 wc.hIcon = NULL;
81 wc.hCursor = NULL;
82 wc.hbrBackground = NULL;
83 wc.lpszMenuName = NULL;
84 wc.lpszClassName = "panelclass";
85 RegisterClass( &wc );
86
87 DialogBox(hInstance, MAKEINTRESOURCE( IDD_MAIN ), 0, main_dlgproc );
88
89 return 0;
90 }
91
92 //--------------------------------------------------------------------------
93 // You can call this function from the Gentee function
94 void CALLBACK c_func( DWORD textcolor, DWORD bgcolor, PCHAR text )
95 {
96 p_bgcolor = bgcolor;
97 p_sztextcolor = textcolor;
98 strcpy( p_sztext, text );
99 p_flg = TRUE;
100 InvalidateRect( GetDlgItem( hwItemTabs[1], IDC_PANEL ), NULL, FALSE );
101 }
102
103 // Hook functions
104 //--------------------------------------------------------------------------
105 // Your function to sending address of c_func to Gentee virtual mashine
106 PVOID CALLBACK exporttogentee( PCHAR str )
107 {
108 if ( !strcmp( str, "c_func" ) )
109 return &c_func;
110 return NULL;
111 }
112
113 //--------------------------------------------------------------------------
114
115 int CALLBACK message_f( pmsginfo minfo )
116 {
117 char out[DEF_STRING];
118 pubyte er;
119
120 out[0] = 0;
121 if ( minfo->flag & MSG_EXIT )
122 {
123 sprintf( out, "Error [ 0x%X %i ]: ", minfo->code, minfo->code );
124 if ( minfo->line )
125 sprintf( out + strlen( out ), "%s\r\n[ Line: %i Pos: %i ] ",
126 minfo->filename, minfo->line, minfo->pos );
127 }
128 er = out + strlen( out );
129
130 if ( minfo->flag & MSG_VALSTR )
131 sprintf( er, minfo->pattern, minfo->uintpar, minfo->uintpar, minfo->namepar );
132 else
133 if ( minfo->flag & MSG_VALVAL )
134 sprintf( er, minfo->pattern, minfo->uintpar, minfo->uintpar );
135 else
136 if ( minfo->flag & MSG_STR )
137 sprintf( er, minfo->pattern, minfo->namepar );
138 else
139 if ( minfo->flag & MSG_VALUE )
140 sprintf( er, minfo->pattern, minfo->uintpar );
141 else
142 sprintf( er, minfo->pattern );
143
144 SetWindowText( GetDlgItem( hwItemTabs[0], IDC_EDITMESSAGE), out );
145 return 0;
146 }
147
148 //--------------------------------------------------------------------------
149
150 void CALLBACK print_f( char* mes, uint len )
151 {
152 strcat( szprint, mes );
153 SetWindowText( GetDlgItem( hwItemTabs[0], IDC_EDITPRINT), szprint );
154 }
155
156 uint CALLBACK getch_f( char* mes, uint len )
157 {
158 if ( mes ) // scan
159 return 0;
160 // getch
161 MessageBox( hwTab, "Click on 'OK' button!", "getch", MB_OK | MB_ICONINFORMATION );
162 return 13;
163 }
164
165 // Thread functions
166 //--------------------------------------------------------------------------
167
168 DWORD WINAPI thread_example( PVOID param )
169 {
170 char nametext[DEF_STRING];
171 char progtext[MAX_STRING];
172 DWORD idfunc, res;
173
174 GetWindowText( GetDlgItem( hwItemTabs[1], IDC_EDITSOURCE), progtext, MAX_STRING );
175 if ( progtext[0] )
176 {
177 szmessage[0] = 0;
178 szprint[0] = 0;
179 SetWindowText( GetDlgItem( hwItemTabs[0], IDC_EDITMESSAGE), szmessage );
180 SetWindowText( GetDlgItem( hwItemTabs[0], IDC_EDITPRINT), szprint );
181 FreeConsole();
182
183 if ( shell_ge_init( NULL, NULL, &exporttogentee ))
184 {
185 compileinfo cmplinfo;
186
187 cmplinfo.flag = CMPL_SRC;// | CMPL_NORUN;
188 cmplinfo.input = progtext;
189 cmplinfo.libdirs = "";
190 cmplinfo.include = "";
191 cmplinfo.defargs = "";
192 cmplinfo.output = "";
193 ge_compile( &cmplinfo );
194
195 idfunc = ge_getid( "gentee_func", GID_ANYOBJ );
196
197 if ( idfunc )
198 {
199 GetWindowText( GetDlgItem( hwItemTabs[1], IDC_EDITNAME), nametext, DEF_STRING );
200 ge_call( idfunc, &res, nametext );
201 }
202 else
203 MessageBox( hwTab,
204 "The function 'gentee_func' has not been found", "Error", MB_OK | MB_ICONHAND );
205 /* {
206 SendMessage( hwTab, TCM_SETCURSEL, 0, 0 );
207 change_tab( 0 );
208 }*/
209 shell_ge_deinit();
210 }
211 }
212 return TRUE;
213 }
214
215 //--------------------------------------------------------------------------
216
217 DWORD WINAPI thread_execute( PVOID param )
218 {
219 char name[_MAX_PATH];
220 PVOID message = NULL;
221 PVOID print = NULL;
222
223 GetWindowText( GetDlgItem( hwItemTabs[0], IDC_EDITFILE), name, _MAX_PATH );
224 if ( name[0] != (char)0 )
225 {
226 szmessage[0] = 0;
227 szprint[0] = 0;
228 SetWindowText( GetDlgItem( hwItemTabs[0], IDC_EDITMESSAGE), szmessage );
229 SetWindowText( GetDlgItem( hwItemTabs[0], IDC_EDITPRINT), szprint );
230 if ( SendMessage( GetDlgItem( hwItemTabs[0], IDC_CHECKMESSAGE ),
231 BM_GETCHECK, 0, 0 ) == BST_CHECKED )
232 message = &message_f;
233 else
234 message = NULL;
235 if ( SendMessage( GetDlgItem( hwItemTabs[0], IDC_CHECKPRINT ),
236 BM_GETCHECK, 0, 0 ) == BST_CHECKED )
237 {
238 FreeConsole();
239 print = &print_f;
240 }
241 else
242 print = NULL;
243
244 if ( shell_ge_init( message, print, NULL ) )
245 {
246 compileinfo cmplinfo;
247
248 cmplinfo.flag = CMPL_LINE;
249 cmplinfo.input = name;
250 cmplinfo.libdirs = "";
251 if ( SendMessage( GetDlgItem( hwItemTabs[0], IDC_CHECKSTDLIB ),
252 BM_GETCHECK, 0, 0 ) == BST_CHECKED )
253 cmplinfo.include = "..\\..\\exe\\lib\\stdlib.ge\0\0";
254 else
255 cmplinfo.include = "";
256 cmplinfo.defargs = "";
257 cmplinfo.output = "";
258 ge_compile( &cmplinfo );
259 FreeConsole();
260 shell_ge_deinit();
261 }
262 }
263 return TRUE;
264 }
265
266 //Gentee shell functions
267 //--------------------------------------------------------------------------
268
269 HINSTANCE shell_ge_init( PVOID messagef,
270 PVOID printf, PVOID exportf )
271 {
272 if ( !gehandle )
273 gehandle = LoadLibrary("../../exe/gentee.dll");
274
275 if ( gehandle )
276 {
277 ge_deinit = GetProcAddress( gehandle, "gentee_deinit" );
278 ge_init = GetProcAddress( gehandle, "gentee_init" );
279 ge_load = GetProcAddress( gehandle, "gentee_load" );
280 ge_compile = GetProcAddress( gehandle, "gentee_compile" );
281 ge_set = GetProcAddress( gehandle, "gentee_set" );
282 ge_getid = ( CDECLPROC )GetProcAddress( gehandle, "gentee_getid" );
283 ge_call = ( CDECLPROC )GetProcAddress( gehandle, "gentee_call" );
284
285 ge_init( G_CHARPRN | ( exportf || printf ? G_SILENT : 0 ));
286 if ( messagef )
287 ge_set( GSET_MESSAGE, messagef );
288 if ( printf )
289 {
290 ge_set( GSET_PRINT, printf );
291 ge_set( GSET_GETCH, getch_f );
292 }
293 if ( exportf )
294 ge_set( GSET_EXPORT, exportf );
295 }
296 else
297 MessageBox( hwTab, "Cannot find or load gentee.dll!", "Error",
298 MB_OK | MB_ICONHAND );
299
300 return gehandle;
301 }
302
303 //--------------------------------------------------------------------------
304
305 void shell_ge_deinit( void )
306 {
307 ge_deinit();
308 FreeLibrary( gehandle );
309 gehandle = NULL;
310 }
311
312 //Interface functions
313 //--------------------------------------------------------------------------
314
315 void create_tabdialog( HWND hwOwner, PCHAR caption, DWORD id, DWORD num )
316 {
317 TC_ITEM tci;
318 RECT rt;
319
320 tci.mask = TCIF_TEXT;
321 tci.iImage = -1;
322 tci.pszText = caption;
323 TabCtrl_InsertItem( hwOwner, num, &tci );
324 GetClientRect( hwOwner, &rt );
325 hwItemTabs[ num ] = CreateDialog( hInst, MAKEINTRESOURCE( id ),
326 hwOwner, tab_dlgproc );
327 TabCtrl_AdjustRect( hwOwner, FALSE, &rt );
328 MoveWindow( hwItemTabs[ num ], rt.left, rt.top, rt.right-rt.left,
329 rt.bottom-rt.top, TRUE );
330 }
331
332 //--------------------------------------------------------------------------
333
334 void change_tab( DWORD n )
335 {
336 if ( hwCurItem )
337 ShowWindow( hwCurItem, SW_HIDE );
338 ShowWindow( hwItemTabs[ n ], SW_SHOW );
339 hwCurItem = hwItemTabs[ n ];
340 }
341
342 //--------------------------------------------------------------------------
343
344 void dlg_init( HWND hwnd )
345 {
346 RECT rt;
347 char text[MAX_STRING];
348
349
350 GetModuleFileName( hInst, szinitdir, _MAX_PATH );
351 szfilename[0] = 0;
352 hthread = 0;
353 p_flg = FALSE;
354 SetClassLong( hwnd, GCL_HICON, (LONG)LoadIcon( hInst, MAKEINTRESOURCE( IDI_ICON )));
355
356 GetClientRect( hwnd, &rt );
357 hwTab = CreateWindow( WC_TABCONTROL, "",
358 WS_TABSTOP | WS_CHILD | WS_VISIBLE | TCS_TOOLTIPS,
359 0, 0, rt.right, rt.bottom,
360 hwnd, NULL, hInst, NULL
361 );
362
363 SendMessage( hwTab, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE );
364 create_tabdialog( hwTab, "Running g files", IDD_DLG1, 0 );
365 create_tabdialog( hwTab, "C&&Gentee", IDD_DLG2, 1 );
366 LoadString(hInst, IDS_EXAMPLE, text, MAX_STRING );
367 SetWindowText( GetDlgItem( hwItemTabs[ 0 ], IDC_EDITFILE ), "..\\test.g" );
368 SendMessage( GetDlgItem( hwItemTabs[ 0 ], IDC_CHECKPRINT ), BM_SETCHECK, 1, 0 );
369 SendMessage( GetDlgItem( hwItemTabs[ 0 ], IDC_CHECKSTDLIB ), BM_SETCHECK, 1, 0 );
370 SetWindowText( GetDlgItem( hwItemTabs[ 1 ], IDC_EDITSOURCE), text);
371 SetWindowText( GetDlgItem( hwItemTabs[ 1 ], IDC_EDITNAME), "World");
372 SendMessage( hwTab, TCM_SETCURSEL, 1, 0 );
373 change_tab( 1 );
374 }
375
376 //--------------------------------------------------------------------------
377
378 BOOL dlg_open( HWND hDlg )
379 {
380 OPENFILENAME ofn;
381 char filter[] = "Gentee file (*.g)\0*.g\0\0";
382
383 ofn.lStructSize = sizeof (OPENFILENAME);
384 ofn.hwndOwner = hDlg;
385 ofn.hInstance = hInst;
386 ofn.lpstrFilter = filter;
387 ofn.lpstrCustomFilter = NULL;
388 ofn.nMaxCustFilter = 0;
389 ofn.nFilterIndex = 0;
390 ofn.lpstrFile = szfilename;
391 ofn.nMaxFile = _MAX_PATH;
392 ofn.lpstrFileTitle = NULL;
393 ofn.nMaxFileTitle = 0;
394 ofn.lpstrInitialDir = szinitdir;
395 ofn.lpstrTitle = NULL;
396 ofn.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT | OFN_EXPLORER
397 | OFN_OVERWRITEPROMPT;
398 ofn.nFileOffset = 0;
399 ofn.nFileExtension = 0;
400 ofn.lpstrDefExt = TEXT("g");
401 ofn.lCustData = 0L;
402 ofn.lpfnHook = NULL;
403 ofn.lpTemplateName = NULL;
404 return GetOpenFileName (&ofn);
405 }
406
407 //Proc function
408 //--------------------------------------------------------------------------
409
410 BOOL CALLBACK main_dlgproc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
411 {
412 BOOL fProcessed = TRUE;
413 DWORD exitthread;
414
415 switch ( uMsg )
416 {
417 case WM_INITDIALOG:
418 dlg_init( hDlg );
419 break;
420 case WM_COMMAND:
421 if ( !GetExitCodeThread( hthread, &exitthread ) || !(exitthread==STILL_ACTIVE) )
422 switch ( LOWORD(wParam) )
423 {
424 case IDCANCEL:
425 EndDialog( hDlg, FALSE );
426 break;
427 case IDOK:
428 EndDialog( hDlg, TRUE );
429 break;
430 }
431 break;
432 case WM_NOTIFY:
433 if ( ((LPNMHDR)lParam)->code == TCN_SELCHANGE )
434 change_tab( TabCtrl_GetCurSel((HWND)((LPNMHDR)lParam)->hwndFrom ));
435 break;
436 default:
437 fProcessed = FALSE;
438 }
439 return (fProcessed);
440 }
441
442 //--------------------------------------------------------------------------
443
444 BOOL CALLBACK tab_dlgproc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
445 {
446 BOOL fProcessed = TRUE;
447 DWORD exitthread;
448
449 switch ( uMsg )
450 {
451 case WM_COMMAND:
452 if ( !GetExitCodeThread( hthread, &exitthread ) || !(exitthread==STILL_ACTIVE) )
453 switch ( LOWORD(wParam) )
454 {
455 case IDC_BUTRUN:
456 hthread = CreateThread( NULL, 0, thread_execute, 0, 0, &threadid );
457 break;
458 case IDC_BUTEXAMPLE:
459 hthread = CreateThread( NULL, 0, thread_example, 0, 0, &threadid );
460 break;
461 case IDC_BUTOPEN:
462 if ( dlg_open( hDlg ) )
463 {
464 SetWindowText( GetDlgItem( hDlg, IDC_EDITFILE ), szfilename );
465 }
466 break;
467 }
468 return 1;
469 }
470 return 0;
471 }
472
473 //--------------------------------------------------------------------------
474
475 LRESULT CALLBACK panel_proc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
476 {
477 HDC hdc;
478 PAINTSTRUCT ps;
479 RECT rt;
480 HBRUSH hbr;
481 TEXTMETRIC tm;
482
483 if ( msg == WM_PAINT && p_flg )
484 {
485 hdc = BeginPaint( hwnd, &ps );
486 hbr = CreateSolidBrush( p_bgcolor );
487 GetClientRect( hwnd, &rt );
488 FillRect( hdc, &rt, hbr );
489 DeleteObject( hbr );
490 SetTextAlign( hdc, TA_CENTER | TA_TOP );
491 SetBkColor( hdc, p_bgcolor );
492 SetTextColor( hdc, p_sztextcolor );
493 GetTextMetrics( hdc, &tm );
494 TextOut( hdc, rt.right >> 1, (rt.bottom - tm.tmHeight )>> 1,
495 p_sztext, strlen(p_sztext));
496 EndPaint( hwnd, &ps );
497 return 0;
498 }
499 else
500 return DefWindowProc( hwnd, msg, wParam, lParam );
501 }
502
503
504