forked from jps1973/Internet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternetApp.cpp
More file actions
658 lines (476 loc) · 16 KB
/
InternetApp.cpp
File metadata and controls
658 lines (476 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
// InternetApp.cpp
#include "InternetApp.h"
int TagFunction( LPCTSTR lpszTag )
{
int nResult = -1;
// See if this is an image tag
if( HtmlFileIsTagName( lpszTag, HTML_FILE_IMAGE_TAG_NAME ) )
{
// This is an image tag
// Add tag to list box window
nResult = ListBoxWindowAddStringEx( lpszTag );
} // End of this is an image tag
return nResult;
} // End of function TagFunction
BOOL EditWindowChangeFunction( DWORD dwTextLength )
{
BOOL bResult = FALSE;
// See if edit window contains text
if( dwTextLength )
{
// Edit window contains text
// Enable button window
ButtonWindowEnable( TRUE );
} // End of edit window contains text
else
{
// Edit window is empty
// Disable button window
ButtonWindowEnable( FALSE );
} // End of edit window is empty
return bResult;
} // End of function EditWindowChangeFunction
BOOL ListBoxWindowDoubleClickFunction( LPCTSTR lpszItemText )
{
BOOL bResult = FALSE;
// Show tag
MessageBox( 0, lpszItemText, INFORMATION_MESSAGE_CAPTION, ( MB_OK | MB_ICONINFORMATION ) );
return bResult;
} // End of function ListBoxWindowDoubleClickFunction
int ShowAboutMessage( HWND hWndParent )
{
int nResult = 0;
MSGBOXPARAMS mbp;
// Clear message box parameter structure
ZeroMemory( &mbp, sizeof( mbp ) );
// Initialise message box parameter structure
mbp.cbSize = sizeof( MSGBOXPARAMS );
mbp.hwndOwner = hWndParent;
mbp.hInstance = GetModuleHandle( NULL );
mbp.lpszText = ABOUT_MESSAGE_TEXT;
mbp.lpszCaption = ABOUT_MESSAGE_CAPTION;
mbp.dwStyle = ( MB_OK | MB_USERICON );
mbp.lpszIcon = MAIN_WINDOW_CLASS_ICON_NAME;
// Show message box
nResult = MessageBoxIndirect( &mbp );
return nResult;
} // End of function ShowAboutMessage
LRESULT CALLBACK MainWindowProcedure( HWND hWndMain, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
LRESULT lr = 0;
// Select message
switch( uMsg )
{
case WM_CREATE:
{
// A create message
HINSTANCE hInstance;
HFONT hFont;
// Get instance
hInstance = ( ( LPCREATESTRUCT )lParam )->hInstance;
// Get font
hFont = ( HFONT )GetStockObject( DEFAULT_GUI_FONT );
// Create edit window
if( EditWindowCreate( hWndMain, hInstance, DEFAULT_URL ) )
{
// Successfully created edit window
// Set edit window font
EditWindowSetFont( hFont );
// Create button window
if( ButtonWindowCreate( hWndMain, hInstance ) )
{
// Successfully created button window
// Set button window font
ButtonWindowSetFont( hFont );
// Create list box window
if( ListBoxWindowCreate( hWndMain, hInstance ) )
{
// Successfully created list box window
// Set list box window font
ListBoxWindowSetFont( hFont );
// Create status bar window
if( StatusBarWindowCreate( hWndMain, hInstance ) )
{
// Successfully created status bar window
// Set status bar window font
StatusBarWindowSetFont( hFont );
// Select edit window text
EditWindowSelect();
} // End of successfully created status bar window
} // End of successfully created list box window
} // End of successfully created button window
} // End of successfully created edit window
// Break out of switch
break;
} // End of a create message
case WM_SIZE:
{
// A size message
int nClientWidth;
int nClientHeight;
RECT rcStatus;
int nStatusWindowHeight;
int nListBoxWindowHeight;
int nListBoxWindowTop;
int nEditWindowWidth;
int nButtonWindowLeft;
// Store client width and height
nClientWidth = ( int )LOWORD( lParam );
nClientHeight = ( int )HIWORD( lParam );
// Size status bar window
StatusBarWindowSize();
// Get status window size
StatusBarWindowGetRect( &rcStatus );
// Calculate window sizes
nStatusWindowHeight = ( rcStatus.bottom - rcStatus.top );
nListBoxWindowHeight = ( nClientHeight - ( nStatusWindowHeight + BUTTON_WINDOW_HEIGHT ) + WINDOW_BORDER_HEIGHT );
nEditWindowWidth = ( ( nClientWidth - BUTTON_WINDOW_WIDTH ) + WINDOW_BORDER_WIDTH );
// Calculate window positions
nListBoxWindowTop = ( BUTTON_WINDOW_HEIGHT - WINDOW_BORDER_HEIGHT );
nButtonWindowLeft = ( nEditWindowWidth - WINDOW_BORDER_WIDTH );
// Move control windows
ListBoxWindowMove( 0, nListBoxWindowTop, nClientWidth, nListBoxWindowHeight, TRUE );
EditWindowMove( 0, 0, nEditWindowWidth, BUTTON_WINDOW_HEIGHT, TRUE );
ButtonWindowMove( nButtonWindowLeft, 0, BUTTON_WINDOW_WIDTH, BUTTON_WINDOW_HEIGHT, TRUE );
// Break out of switch
break;
} // End of a size message
case WM_ACTIVATE:
{
// An activate message
// Focus on edit window
EditWindowSetFocus();
// Break out of switch
break;
} // End of an activate message
case WM_GETMINMAXINFO:
{
// A get min max info message
MINMAXINFO FAR *lpMinMaxInfo;
// Get min max info structure
lpMinMaxInfo = ( MINMAXINFO FAR * )lParam;
// Update min max info structure
lpMinMaxInfo->ptMinTrackSize.x = MAIN_WINDOW_MINIMUM_WIDTH;
lpMinMaxInfo->ptMinTrackSize.y = MAIN_WINDOW_MINIMUM_HEIGHT;
// Break out of switch
break;
} // End of a get min max info message
case WM_DROPFILES:
{
// A drop files message
UINT uFileCount;
HDROP hDrop;
UINT uWhichFile;
UINT uFileSize;
// Allocate string memory
LPTSTR lpszFilePath = new char[ STRING_LENGTH + sizeof( char ) ];
// Get drop handle
hDrop = ( HDROP )wParam;
// Count dropped files
uFileCount = DragQueryFile( hDrop, ( UINT )-1, NULL, 0 );
// Loop through dropped files
for( uWhichFile = 0; uWhichFile < uFileCount; uWhichFile ++ )
{
// Get size of dropped file
uFileSize = DragQueryFile( hDrop, uWhichFile, NULL, 0 );
// Ensure that file size is valid
if( uFileSize != 0 )
{
// File size is valid
// Get file path
if( DragQueryFile( hDrop, uWhichFile, lpszFilePath, ( uFileSize + sizeof( char ) ) ) )
{
// Successfully got file path
// Add file path to list box window
ListBoxWindowAddString( lpszFilePath );
} // End of successfully got file path
} // End of file size is valid
}; // End of loop through dropped files
// Free string memory
delete [] lpszFilePath;
// Break out of switch
break;
} // End of a drop files message
case WM_COMMAND:
{
// A command message
// Select command
switch( LOWORD( wParam ) )
{
case BUTTON_WINDOW_ID:
{
// A button window command
// Allocate string memory
LPTSTR lpszUrl = new char[ STRING_LENGTH + sizeof( char ) ];
// Get url from edit window
if( EditWindowGetText( lpszUrl, STRING_LENGTH ) )
{
// Successfully got url from edit window
// Allocate string memory
LPTSTR lpszLocalFilePath = new char[ STRING_LENGTH + sizeof( char ) ];
// Download file
if( InternetDownloadFile( lpszUrl, lpszLocalFilePath, &StatusBarWindowSetText ) )
{
// Successfully downloaded file
// Load html file
if( HtmlFileLoad( lpszLocalFilePath ) )
{
// Successfully loaded html file
int nTagCount;
// Allocate string memory
LPTSTR lpszStatusMessage = new char[ STRING_LENGTH + sizeof( char ) ];
// Process tags
nTagCount = HtmlFileProcessTags( &TagFunction );
// Format status message
wsprintf( lpszStatusMessage, HTML_FILE_PROCESS_TAGS_STATUS_MESSAGE_FORMAT_STRING, lpszLocalFilePath, nTagCount );
// Show status message on status bar window
StatusBarWindowSetText( lpszStatusMessage );
// Free memory
HtmlFileFreeMemory();
// Free string memory
delete [] lpszStatusMessage;
} // End of successfully loaded html file
} // End of successfully downloaded file
// Free string memory
delete [] lpszLocalFilePath;
} // End of successfully got url from edit window
// Free string memory
delete [] lpszUrl;
// Break out of switch
break;
} // End of a button window command
case IDM_FILE_EXIT:
{
// A file exit command
// Destroy main window
DestroyWindow( hWndMain );
// Break out of switch
break;
} // End of a file exit command
case IDM_HELP_ABOUT:
{
// A help about command
// Show about message
ShowAboutMessage( hWndMain );
// Break out of switch
break;
} // End of a help about command
default:
{
// Default command
// See if command message is from edit window
if( IsEditWindow( ( HWND )lParam ) )
{
// Command message is from edit window
// Handle command message from edit window
if( !( EditWindowHandleCommandMessage( wParam, lParam, &EditWindowChangeFunction ) ) )
{
// Command message was not handled from edit window
// Call default procedure
lr = DefWindowProc( hWndMain, uMsg, wParam, lParam );
} // End of command message was not handled from edit window
} // End of command message is from edit window
if( IsListBoxWindow( ( HWND )lParam ) )
{
// Command message is from list box window
// Handle command message from list box window
if( !( ListBoxWindowHandleCommandMessage( wParam, lParam, &ListBoxWindowDoubleClickFunction, &StatusBarWindowSetText ) ) )
{
// Command message was not handled from list box window
// Call default procedure
lr = DefWindowProc( hWndMain, uMsg, wParam, lParam );
} // End of command message was not handled from list box window
} // End of command message is from list box window
else
{
// Command message is not from list box window
// Call default procedure
lr = DefWindowProc( hWndMain, uMsg, wParam, lParam );
} // End of command message is not from list box window
// Break out of switch
break;
} // End of default command
}; // End of selection for command
// Break out of switch
break;
} // End of a command message
case WM_SYSCOMMAND:
{
// A system command message
// Select system command
switch( LOWORD( wParam ) )
{
case IDM_HELP_ABOUT:
{
// A help about system command
// Show about message
ShowAboutMessage( hWndMain );
// Break out of switch
break;
} // End of a help about system command
default:
{
// Default system command
// Call default procedure
lr = DefWindowProc( hWndMain, uMsg, wParam, lParam );
// Break out of switch
break;
} // End of default system command
}; // End of selection for system command
// Break out of switch
break;
} // End of a system command message
case WM_NOTIFY:
{
// A notify message
// Call default procedure
lr = DefWindowProc( hWndMain, uMsg, wParam, lParam );
// Break out of switch
break;
} // End of a notify message
case WM_CONTEXTMENU:
{
// A context menu message
HMENU hMenu;
// Load context menu
hMenu = LoadMenu( NULL, MAKEINTRESOURCE( IDR_CONTEXT_MENU ) );
// Show context menu
TrackPopupMenu( GetSubMenu( hMenu, 0 ), ( TPM_LEFTALIGN | TPM_LEFTBUTTON ), LOWORD( lParam ), HIWORD( lParam ), 0, hWndMain, NULL );
// Break out of switch
break;
} // End of a context menu message
case WM_CLOSE:
{
// A close message
// Destroy main window
DestroyWindow( hWndMain );
// Break out of switch
break;
} // End of a close message
case WM_DESTROY:
{
// A destroy message
// Terminate thread
PostQuitMessage( 0 );
// Break out of switch
break;
} // End of a destroy message
default:
{
// Default message
// Call default window procedure
lr = DefWindowProc( hWndMain, uMsg, wParam, lParam );
// Break out of switch
break;
} // End of default message
}; // End of selection for message
return lr;
} // End of function MainWindowProcedure
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow )
{
MSG msg;
// Clear message structure
ZeroMemory( &msg, sizeof( msg ) );
// Connect to internet
if( InternetConnect() )
{
// Successfully connected to internet
WNDCLASSEX wcMain;
// Clear window class structure
ZeroMemory( &wcMain, sizeof( wcMain ) );
// Initialise window class structure
wcMain.cbSize = sizeof( WNDCLASSEX );
wcMain.lpfnWndProc = MainWindowProcedure;
wcMain.hInstance = hInstance;
wcMain.lpszClassName = MAIN_WINDOW_CLASS_NAME;
wcMain.style = MAIN_WINDOW_CLASS_STYLE;
wcMain.hIcon = MAIN_WINDOW_CLASS_ICON;
wcMain.hCursor = MAIN_WINDOW_CLASS_CURSOR;
wcMain.hbrBackground = MAIN_WINDOW_CLASS_BACKGROUND;
wcMain.lpszMenuName = MAIN_WINDOW_CLASS_MENU_NAME;
wcMain.hIconSm = MAIN_WINDOW_CLASS_ICON_SMALL;
// Register main window class
if( RegisterClassEx( &wcMain ) )
{
// Successfully registered main window class
HWND hWndMain;
// Create main window
hWndMain = CreateWindowEx( MAIN_WINDOW_EXTENDED_STYLE, MAIN_WINDOW_CLASS_NAME, MAIN_WINDOW_TEXT, MAIN_WINDOW_STYLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
// Ensure that main window was created
if( hWndMain )
{
// Successfully created main window
HMENU hMenuSystem;
LPWSTR *lpszArgumentList;
int nArgumentCount;
// Get system menu
hMenuSystem = GetSystemMenu( hWndMain, FALSE );
// Add separator item to system menu
InsertMenu( hMenuSystem, SYSTEM_MENU_SEPARATOR_ITEM_POSITION, ( MF_BYPOSITION | MF_SEPARATOR ), 0, NULL );
// Add about item to system menu
InsertMenu( hMenuSystem, SYSTEM_MENU_ABOUT_ITEM_POSITION, MF_BYPOSITION, IDM_HELP_ABOUT, SYSTEM_MENU_ABOUT_ITEM_TEXT );
// Get argument list
lpszArgumentList = CommandLineToArgvW( GetCommandLineW(), &nArgumentCount );
// Ensure that argument list was got
if( lpszArgumentList )
{
// Successfully got argument list
int nWhichArgument;
int nSizeNeeded;
int nWideArgumentLength;
// Allocate string memory
LPTSTR lpszArgument = new char[ STRING_LENGTH + sizeof( char ) ];
// Loop through arguments
for( nWhichArgument = 1; nWhichArgument < nArgumentCount; nWhichArgument ++ )
{
// Get wide argument length
nWideArgumentLength = lstrlenW( lpszArgumentList[ nWhichArgument ] );
// Get size required for argument
nSizeNeeded = WideCharToMultiByte( CP_UTF8, 0, lpszArgumentList[ nWhichArgument ], nWideArgumentLength, NULL, 0, NULL, NULL );
// Convert argument to ansi
WideCharToMultiByte( CP_UTF8, 0, lpszArgumentList[ nWhichArgument ], nWideArgumentLength, lpszArgument, nSizeNeeded, NULL, NULL );
// Terminate argument
lpszArgument[ nSizeNeeded ] = ( char )NULL;
// Add argument to list box window
ListBoxWindowAddString( lpszArgument );
}; // End of loop through arguments
// Free string memory
delete [] lpszArgument;
} // End of successfully got argument list
// Show main window
ShowWindow( hWndMain, nCmdShow );
// Update main window
UpdateWindow( hWndMain );
// Message loop
while( GetMessage( &msg, NULL, 0, 0 ) > 0 )
{
// Translate message
TranslateMessage( &msg );
// Dispatch message
DispatchMessage( &msg );
}; // End of message loop
} // End of successfully main created window
else
{
// Unable to create main window
// Display error message
MessageBox( NULL, UNABLE_TO_CREATE_MAIN_WINDOW_ERROR_MESSAGE, ERROR_MESSAGE_CAPTION, ( MB_OK | MB_ICONERROR ) );
} // End of unable to create main window
} // End of successfully registered main window class
else
{
// Unable to register main window class
// Display error message
MessageBox( NULL, UNABLE_TO_REGISTER_MAIN_WINDOW_CLASS_ERROR_MESSAGE, ERROR_MESSAGE_CAPTION, ( MB_OK | MB_ICONERROR ) );
} // End of unable to register main window class
// Close internet
InternetClose();
} // End of successfully connected to internet
else
{
// Unable to connect to internet
// Display error message
MessageBox( NULL, INTERNET_UNABLE_TO_CONNECT_TO_INTERNET_ERROR_MESSAGE, ERROR_MESSAGE_CAPTION, ( MB_OK | MB_ICONERROR ) );
} // End of unable to connect to internet
return msg.wParam;
} // End of function WinMain