~itmages/itmages/itmages-windows-extension

« back to all changes in this revision

Viewing changes to itmagesgui.c

  • Committer: Dmitry
  • Date: 2012-03-11 07:56:24 UTC
  • Revision ID: git-v1:d5a30bf1fe74b46641b08d0151d39dba15b67f0e
Migrate from launchpad

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    ITmages  upload client for Windows
 
3
    Copyright (C) 2011 Dmitriy Simbiriatin <slpiv@itmages.ru>
 
4
 
 
5
    This program is free software; you can redistribute it and/or
 
6
    modify it under the terms of the GNU General Public License
 
7
    as published by the Free Software Foundation; either version 2
 
8
    of the License, or (at your option) any later version.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program; if not, write to the Free Software
 
17
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 */
 
19
 
 
20
#define _WIN32_WINNT 0x0501
 
21
#define _WIN32_IE 0x0501
 
22
#define WINVER 0x0501
 
23
 
 
24
#include <windows.h>
 
25
#include <wchar.h>
 
26
#include <shlobj.h>
 
27
#include <uxtheme.h>
 
28
#include <process.h>
 
29
#include <libintl.h>
 
30
#include <locale.h>
 
31
#include "itmagesgui.h"
 
32
#include "itmagescmd.h"
 
33
#include "imagefunc.h"
 
34
#include "errorshandler.h"
 
35
#include "enconv.h"
 
36
 
 
37
// Window classes --------------------------------------------------------------
 
38
const char g_LoginWindowClass[] = "ITmagesLogin";
 
39
const char g_AboutWindowClass[] = "ITmagesAbout";
 
40
const char g_ReadyWindowClass[] = "ITmagesReady";
 
41
// -----------------------------------------------------------------------------
 
42
 
 
43
// Main windows ----------------------------------------------------------------
 
44
HWND login_wnd = NULL;
 
45
HWND ready_wnd = NULL;
 
46
HWND about_wnd = NULL;
 
47
// -----------------------------------------------------------------------------
 
48
 
 
49
HINSTANCE g_ItmagesInst = NULL;
 
50
 
 
51
// Default system's font.
 
52
HFONT g_SystemFont = NULL;
 
53
 
 
54
// Application's icon.
 
55
HICON g_AppIcon = NULL;
 
56
 
 
57
// Static control's background brush.
 
58
HBRUSH g_StaticBrush = NULL;
 
59
 
 
60
// Image's preview.
 
61
HBITMAP previewImage = NULL;
 
62
 
 
63
// Indicates if the new visual style is enabled or not.
 
64
short g_ThemeActive = 0;
 
65
 
 
66
ItmagesImageInfo imageInfo;
 
67
ItmagesImageLinks imageLinks;
 
68
ItmagesImageData imageData;
 
69
ItmagesUserInfo userInfo;
 
70
ItmagesConfig configFiles;
 
71
 
 
72
INT APIENTRY WinMain(
 
73
        HINSTANCE inst,
 
74
        HINSTANCE prev_inst,
 
75
        LPSTR cmd_line,
 
76
        int cmd_show)
 
77
{
 
78
    g_ItmagesInst = inst;
 
79
 
 
80
    // Loading application's icon.
 
81
    g_AppIcon = LoadIcon(g_ItmagesInst, MAKEINTRESOURCE(ID_APPICON));
 
82
 
 
83
    // Loading default system's font.
 
84
    g_SystemFont = (HFONT) GetStockObject(DEFAULT_GUI_FONT);
 
85
 
 
86
    // Getting directory with the localization files and choose an appropriate one.
 
87
    char *locale_dir = GetLocaleDir();
 
88
    if (locale_dir != NULL) {
 
89
        setlocale(LC_ALL, "");
 
90
        bindtextdomain("itmages", locale_dir);
 
91
        textdomain("itmages");
 
92
    }
 
93
    free(locale_dir);
 
94
 
 
95
    LPWSTR *arg_list;
 
96
    int arg_count;
 
97
 
 
98
    // Retrieving a path to the image from the command line.
 
99
    arg_list = CommandLineToArgvW(GetCommandLineW(), &arg_count);
 
100
    if (arg_list == NULL) {
 
101
        ErrorsHandler(ERROR_GETIMGPATH);
 
102
        exit(EXIT_FAILURE);
 
103
    }
 
104
 
 
105
    if (arg_count == 1) {
 
106
        // If the path was not provided, loading the open file dialog.
 
107
        char *image_path = CreateOpenImageDialog();
 
108
        imageData.path = AcpToWide(image_path);
 
109
        free(image_path);
 
110
    } else {
 
111
        size_t path_len = wcslen(arg_list[1]);
 
112
        imageData.path = (wchar_t*) malloc((path_len + 1) * sizeof (wchar_t));
 
113
        if (imageData.path == NULL) {
 
114
            ErrorsHandler(ERROR_MEMALLOC);
 
115
            exit(EXIT_FAILURE);
 
116
        }
 
117
        wmemcpy(imageData.path, arg_list[1], path_len + 1);
 
118
    }
 
119
    LocalFree(arg_list);
 
120
 
 
121
    imageData.basename = (wchar_t*) calloc(_MAX_FNAME + 1, sizeof (wchar_t));
 
122
    if (imageData.basename == NULL) {
 
123
        ErrorsHandler(ERROR_MEMALLOC);
 
124
        exit(EXIT_FAILURE);
 
125
    }
 
126
    imageData.ext = (wchar_t*) calloc(_MAX_EXT + 1, sizeof (wchar_t));
 
127
    if (imageData.ext == NULL) {
 
128
        ErrorsHandler(ERROR_MEMALLOC);
 
129
        exit(EXIT_FAILURE);
 
130
    }
 
131
    // Retrieving the image's basename and extension from the image's path.
 
132
    _wsplitpath(imageData.path, NULL, NULL, imageData.basename, imageData.ext);
 
133
 
 
134
    // Retrieving an information about the image(e.g name, width, height, size etc.)
 
135
    if (GetImageInfo(imageData.path, imageData.basename,
 
136
                                imageData.ext, &imageInfo) == -1) {
 
137
        ErrorsHandler(ERROR_GETIMGINFO);
 
138
        exit(EXIT_FAILURE);
 
139
    }
 
140
 
 
141
    // Setting the image's extension to the lower case
 
142
    SetExtToLower(imageData.ext);
 
143
 
 
144
    /* Checking the image's extension. ITmages supports only 
 
145
     *.jpg, *.jpeg, *.png, *.gif images. */
 
146
    if (CheckImageExtension(imageData.ext) == -1) {
 
147
        ErrorsHandler(ERROR_SUPPORTEDIMG);
 
148
        exit(EXIT_FAILURE);
 
149
    }
 
150
 
 
151
    // Checking the image's size. The size of the image has to be less than 6 Mb.
 
152
    if (imageInfo.size == -1) {
 
153
        ErrorsHandler(ERROR_MAXIMGSIZE);
 
154
        exit(EXIT_FAILURE);
 
155
    } else if (imageInfo.size == 0) {
 
156
        ErrorsHandler(ERROR_BADIMGSIZE);
 
157
        exit(EXIT_FAILURE);
 
158
    }
 
159
 
 
160
    // Retrieving the system's default path for the config and cookies files.
 
161
    GetConfigFiles(&configFiles);
 
162
 
 
163
    // Registering window classes.
 
164
    if (
 
165
            WinLogin() == -1 ||
 
166
            WinAbout() == -1 ||
 
167
            WinReady() == -1
 
168
            ) {
 
169
        ErrorsHandler(ERROR_REGISTERWNDCLS);
 
170
    }
 
171
 
 
172
    CreateLoginWnd();
 
173
 
 
174
    MSG msg;
 
175
 
 
176
    while (GetMessage(&msg, NULL, 0, 0)) {
 
177
        if (
 
178
                !IsDialogMessage(login_wnd, &msg) &&
 
179
                !IsDialogMessage(about_wnd, &msg) &&
 
180
                !IsDialogMessage(ready_wnd, &msg)
 
181
                ) {
 
182
            TranslateMessage(&msg);
 
183
            DispatchMessage(&msg);
 
184
        }
 
185
    }
 
186
    return (DWORD) msg.wParam;
 
187
}
 
188
 
 
189
INT APIENTRY WinLogin()
 
190
{
 
191
    WNDCLASSEX itmagesLogin;
 
192
    memset(&itmagesLogin, 0, sizeof (itmagesLogin));
 
193
 
 
194
    itmagesLogin.cbSize = sizeof (itmagesLogin);
 
195
    itmagesLogin.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
 
196
    itmagesLogin.lpfnWndProc = WinMainProc;
 
197
    itmagesLogin.cbClsExtra = 0;
 
198
    itmagesLogin.cbWndExtra = 0;
 
199
    itmagesLogin.hInstance = g_ItmagesInst;
 
200
    itmagesLogin.hIcon = g_AppIcon;
 
201
    itmagesLogin.hCursor = LoadCursor(NULL, IDC_ARROW);
 
202
    itmagesLogin.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
 
203
    itmagesLogin.lpszMenuName = NULL;
 
204
    itmagesLogin.lpszClassName = g_LoginWindowClass;
 
205
    itmagesLogin.hIconSm = NULL;
 
206
 
 
207
    if (RegisterClassEx(&itmagesLogin) == 0) {
 
208
        return -1;
 
209
    }
 
210
    return 0;
 
211
}
 
212
 
 
213
void CreateLoginWnd()
 
214
{
 
215
    size_t desk_width = GetSystemMetrics(SM_CXSCREEN);
 
216
    size_t desk_height = GetSystemMetrics(SM_CYSCREEN);
 
217
 
 
218
    login_wnd = CreateWindowEx(
 
219
            0,
 
220
            g_LoginWindowClass,
 
221
            gettext("ITmages - Login"),
 
222
            WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_CLIPCHILDREN,
 
223
            desk_width / 2 - LOGIN_WND_WIDTH / 2,
 
224
            desk_height / 2 - LOGIN_WND_HEIGHT / 2,
 
225
            LOGIN_WND_WIDTH,
 
226
            LOGIN_WND_HEIGHT,
 
227
            NULL,
 
228
            NULL,
 
229
            g_ItmagesInst,
 
230
            NULL);
 
231
 
 
232
    // Disabling maximize button.
 
233
    long wnd_style = GetWindowLong(login_wnd, GWL_STYLE);
 
234
    SetWindowLong(login_wnd, GWL_STYLE, wnd_style & ~WS_MAXIMIZEBOX);
 
235
 
 
236
    // Creating Tab control inside the Login window.
 
237
    CreateLoginTabControl();
 
238
 
 
239
    ShowWindow(login_wnd, SW_SHOW);
 
240
    UpdateWindow(login_wnd);
 
241
}
 
242
 
 
243
void CreateLoginTabControl()
 
244
{
 
245
    HWND login_tab = CreateWindowEx(
 
246
            0,
 
247
            WC_TABCONTROL,
 
248
            "",
 
249
            WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPCHILDREN,
 
250
            LOGIN_TABCONTROL_X,
 
251
            LOGIN_TABCONTROL_Y,
 
252
            LOGIN_TABCONTROL_WIDTH,
 
253
            LOGIN_TABCONTROL_HEIGHT,
 
254
            login_wnd,
 
255
            (HMENU) ID_LOGIN_TABCONTROL,
 
256
            g_ItmagesInst,
 
257
            NULL
 
258
            );
 
259
 
 
260
    SendMessage(login_tab, WM_SETFONT, (WPARAM) g_SystemFont, 0);
 
261
 
 
262
    TCITEM tie;
 
263
    memset(&tie, 0, sizeof (tie));
 
264
 
 
265
    tie.mask = TCIF_TEXT;
 
266
    tie.pszText = gettext("Account");
 
267
    TabCtrl_InsertItem(login_tab, 0, &tie);
 
268
    tie.pszText = gettext("Image settings");
 
269
    TabCtrl_InsertItem(login_tab, 1, &tie);
 
270
    tie.pszText = gettext("Proxy");
 
271
    TabCtrl_InsertItem(login_tab, 2, &tie);
 
272
 
 
273
    UpdateBackgroundBrush(login_tab);
 
274
}
 
275
 
 
276
LRESULT CALLBACK WinMainProc(
 
277
        HWND login_wnd,
 
278
        UINT message,
 
279
        WPARAM wParam,
 
280
        LPARAM lParam)
 
281
{
 
282
    HWND login_tab = GetDlgItem(login_wnd, ID_LOGIN_TABCONTROL);
 
283
    static HWND login, login_txt,
 
284
            passwd, passwd_txt,
 
285
            without_login, save_data,
 
286
            resize_img, resize_size,
 
287
            resize_updown, resize_filter,
 
288
            rotate_img, rotate_angle,
 
289
            use_proxy, proxy_addrss,
 
290
            proxy_addrss_txt, proxy_port, proxy_port_txt,
 
291
            about, upload, upload_progress,
 
292
            login_tool, passwd_tool;
 
293
 
 
294
    static HWND withoutlogin_tool,
 
295
            savedata_tool,
 
296
            resizeimg_tool,
 
297
            size_tool,
 
298
            filter_tool,
 
299
            addrss_tool,
 
300
            port_tool;
 
301
    switch (message) {
 
302
    case WM_CREATE:
 
303
    {
 
304
        login = CreateWindowEx(
 
305
                WS_EX_CLIENTEDGE,
 
306
                "EDIT",
 
307
                "",
 
308
                WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | WS_TABSTOP | WS_CLIPSIBLINGS,
 
309
                LOGIN_EDIT_X,
 
310
                LOGIN_EDIT_Y,
 
311
                LOGIN_EDIT_WIDTH,
 
312
                LOGIN_EDIT_HEIGHT,
 
313
                login_wnd,
 
314
                (HMENU) ID_LOGIN,
 
315
                g_ItmagesInst,
 
316
                NULL);
 
317
 
 
318
        login_txt = CreateWindowEx(
 
319
                0,
 
320
                "STATIC",
 
321
                gettext("Login:"),
 
322
                WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
 
323
                LOGIN_TXT_X,
 
324
                LOGIN_TXT_Y,
 
325
                LOGIN_TXT_WIDTH,
 
326
                LOGIN_TXT_HEIGHT,
 
327
                login_wnd,
 
328
                (HMENU) ID_LOGIN_TXT,
 
329
                g_ItmagesInst,
 
330
                NULL);
 
331
 
 
332
        passwd = CreateWindowEx(
 
333
                WS_EX_CLIENTEDGE,
 
334
                "EDIT",
 
335
                "",
 
336
                WS_CHILD | WS_VISIBLE | ES_PASSWORD | ES_AUTOHSCROLL | WS_TABSTOP | WS_CLIPSIBLINGS,
 
337
                PASSWD_EDIT_X,
 
338
                PASSWD_EDIT_Y,
 
339
                PASSWD_EDIT_WIDTH,
 
340
                PASSWD_EDIT_HEIGHT,
 
341
                login_wnd,
 
342
                (HMENU) ID_PASSWD,
 
343
                g_ItmagesInst,
 
344
                NULL);
 
345
        passwd_txt = CreateWindowEx(
 
346
                0,
 
347
                "STATIC",
 
348
                gettext("Password:"),
 
349
                WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
 
350
                PASSWD_TXT_X,
 
351
                PASSWD_TXT_Y,
 
352
                PASSWD_TXT_WIDTH,
 
353
                PASSWD_TXT_HEIGHT,
 
354
                login_wnd,
 
355
                (HMENU) ID_PASSWD_TXT,
 
356
                g_ItmagesInst,
 
357
                NULL);
 
358
 
 
359
        without_login = CreateWindowEx(
 
360
                0,
 
361
                "BUTTON",
 
362
                gettext("Without account"),
 
363
                WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | WS_TABSTOP | WS_CLIPSIBLINGS,
 
364
                CHECK_WITHOUTLOGIN_X,
 
365
                CHECK_WITHOUTLOGIN_Y,
 
366
                CHECK_WITHOUTLOGIN_WIDTH,
 
367
                CHECK_WITHOUTLOGIN_HEIGHT,
 
368
                login_wnd,
 
369
                (HMENU) ID_CHECK_WITHOUTLOGIN,
 
370
                g_ItmagesInst,
 
371
                NULL);
 
372
        save_data = CreateWindowEx(
 
373
                0,
 
374
                "BUTTON",
 
375
                gettext("Save account settings"),
 
376
                WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | WS_TABSTOP | WS_CLIPSIBLINGS,
 
377
                CHECK_SAVEDATA_X,
 
378
                CHECK_SAVEDATA_Y,
 
379
                CHECK_SAVEDATA_WIDTH,
 
380
                CHECK_SAVEDATA_HEIGHT,
 
381
                login_wnd,
 
382
                (HMENU) ID_CHECK_SAVEDATA,
 
383
                g_ItmagesInst,
 
384
                NULL);
 
385
        resize_img = CreateWindowEx(
 
386
                0,
 
387
                "BUTTON",
 
388
                gettext("Reduce to:"),
 
389
                WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | WS_TABSTOP | WS_CLIPSIBLINGS,
 
390
                CHECK_RESIZEIMG_X,
 
391
                CHECK_RESIZEIMG_Y,
 
392
                CHECK_RESIZEIMG_WIDTH,
 
393
                CHECK_RESIZEIMG_HEIGHT,
 
394
                login_wnd,
 
395
                (HMENU) ID_CHECK_RESIZEIMG,
 
396
                g_ItmagesInst,
 
397
                NULL);
 
398
        resize_size = CreateWindowEx(
 
399
                WS_EX_CLIENTEDGE,
 
400
                "EDIT",
 
401
                "",
 
402
                WS_CHILD | WS_VISIBLE | ES_LEFT | ES_NUMBER | ES_AUTOHSCROLL | WS_TABSTOP | WS_CLIPSIBLINGS,
 
403
                RESIZEIMG_SIZE_X,
 
404
                RESIZEIMG_SIZE_Y,
 
405
                RESIZEIMG_SIZE_WIDTH,
 
406
                RESIZEIMG_SIZE_HEIGHT,
 
407
                login_wnd,
 
408
                (HMENU) ID_RESIZEIMG_SIZE,
 
409
                g_ItmagesInst,
 
410
                NULL);
 
411
        resize_updown = CreateWindow(
 
412
                UPDOWN_CLASS,
 
413
                "",
 
414
                WS_CHILD | WS_VISIBLE | UDS_ARROWKEYS | UDS_ALIGNRIGHT | UDS_SETBUDDYINT | UDS_NOTHOUSANDS,
 
415
                0,
 
416
                0,
 
417
                0,
 
418
                0,
 
419
                login_wnd,
 
420
                (HMENU) ID_RESIZEIMGSIZE_UPDOWN,
 
421
                g_ItmagesInst,
 
422
                NULL);
 
423
        resize_filter = CreateWindowEx(
 
424
                0,
 
425
                "COMBOBOX",
 
426
                NULL,
 
427
                WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_TABSTOP | WS_CLIPSIBLINGS,
 
428
                RESIZE_FILTER_X,
 
429
                RESIZE_FILTER_Y,
 
430
                RESIZE_FILTER_WIDTH,
 
431
                RESIZE_FILTER_HEIGHT,
 
432
                login_wnd,
 
433
                (HMENU) ID_RESIZE_FILTER,
 
434
                g_ItmagesInst,
 
435
                NULL);
 
436
        rotate_img = CreateWindowEx(
 
437
                0,
 
438
                "BUTTON",
 
439
                gettext("Rotate:"),
 
440
                WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | WS_TABSTOP | WS_CLIPSIBLINGS,
 
441
                CHECK_ROTATEIMG_X,
 
442
                CHECK_ROTATEIMG_Y,
 
443
                CHECK_ROTATEIMG_WIDTH,
 
444
                CHECK_ROTATEIMG_HEIGHT,
 
445
                login_wnd,
 
446
                (HMENU) ID_CHECK_ROTATEIMG,
 
447
                g_ItmagesInst,
 
448
                NULL);
 
449
        rotate_angle = CreateWindowEx(
 
450
                0,
 
451
                "COMBOBOX",
 
452
                NULL,
 
453
                WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_TABSTOP | WS_CLIPSIBLINGS,
 
454
                ROTATEIMG_ANGLE_X,
 
455
                ROTATEIMG_ANGLE_Y,
 
456
                ROTATEIMG_ANGLE_WIDTH,
 
457
                ROTATEIMG_ANGLE_HEIGHT,
 
458
                login_wnd,
 
459
                (HMENU) ID_ROTATEIMG_ANGLE,
 
460
                g_ItmagesInst,
 
461
                NULL);
 
462
        use_proxy = CreateWindowEx(
 
463
                0,
 
464
                "BUTTON",
 
465
                gettext("Use proxy server"),
 
466
                WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | WS_TABSTOP | WS_CLIPSIBLINGS,
 
467
                CHECK_USEPROXY_X,
 
468
                CHECK_USEPROXY_Y,
 
469
                CHECK_USEPROXY_WIDTH,
 
470
                CHECK_USEPROXY_HEIGHT,
 
471
                login_wnd,
 
472
                (HMENU) ID_CHECK_USEPROXY,
 
473
                g_ItmagesInst,
 
474
                NULL);
 
475
        proxy_addrss = CreateWindowEx(
 
476
                WS_EX_CLIENTEDGE,
 
477
                "EDIT",
 
478
                "",
 
479
                WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | WS_TABSTOP | WS_CLIPSIBLINGS,
 
480
                PROXY_ADDRESS_X,
 
481
                PROXY_ADDRESS_Y,
 
482
                PROXY_ADDRESS_WIDTH,
 
483
                PROXY_ADDRESS_HEIGHT,
 
484
                login_wnd,
 
485
                (HMENU) ID_PROXY_ADDRESS,
 
486
                g_ItmagesInst,
 
487
                NULL);
 
488
        proxy_addrss_txt = CreateWindowEx(
 
489
                0,
 
490
                "STATIC",
 
491
                gettext("Address:"),
 
492
                WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
 
493
                PROXYADDRESS_TXT_X,
 
494
                PROXYADDRESS_TXT_Y,
 
495
                PROXYADDRESS_TXT_WIDTH,
 
496
                PROXYADDRESS_TXT_HEIGHT,
 
497
                login_wnd,
 
498
                (HMENU) ID_PROXYADDRESS_TXT,
 
499
                g_ItmagesInst,
 
500
                NULL);
 
501
        proxy_port = CreateWindowEx(
 
502
                WS_EX_CLIENTEDGE,
 
503
                "EDIT",
 
504
                "",
 
505
                WS_CHILD | WS_VISIBLE | ES_NUMBER | ES_AUTOHSCROLL | WS_TABSTOP | WS_CLIPSIBLINGS,
 
506
                PROXY_PORT_X,
 
507
                PROXY_PORT_Y,
 
508
                PROXY_PORT_WIDTH,
 
509
                PROXY_PORT_HEIGHT,
 
510
                login_wnd,
 
511
                (HMENU) ID_PROXY_PORT,
 
512
                g_ItmagesInst,
 
513
                NULL);
 
514
        proxy_port_txt = CreateWindowEx(
 
515
                0,
 
516
                "STATIC",
 
517
                gettext("Port:"),
 
518
                WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
 
519
                PROXYPORT_TXT_X,
 
520
                PROXYPORT_TXT_Y,
 
521
                PROXYPORT_TXT_WIDTH,
 
522
                PROXYPORT_TXT_HEIGHT,
 
523
                login_wnd,
 
524
                (HMENU) ID_PROXYPORT_TXT,
 
525
                g_ItmagesInst,
 
526
                NULL);
 
527
        upload_progress = CreateWindowEx(
 
528
                0,
 
529
                PROGRESS_CLASS,
 
530
                NULL,
 
531
                WS_CHILD | WS_VISIBLE | PBS_SMOOTH | WS_CLIPSIBLINGS,
 
532
                UPLOAD_PROGRESSBAR_X,
 
533
                UPLOAD_PROGRESSBAR_Y,
 
534
                UPLOAD_PROGRESSBAR_WIDTH,
 
535
                UPLOAD_PROGRESSBAR_HEIGHT,
 
536
                login_wnd,
 
537
                (HMENU) ID_UPLOAD_PROGRESSBAR,
 
538
                g_ItmagesInst,
 
539
                NULL);
 
540
 
 
541
        about = CreateWindowEx(
 
542
                0,
 
543
                "BUTTON",
 
544
                gettext("About"),
 
545
                WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPSIBLINGS,
 
546
                ABOUT_BUTTON_X,
 
547
                ABOUT_BUTTON_Y,
 
548
                ABOUT_BUTTON_WIDTH,
 
549
                ABOUT_BUTTON_HEIGHT,
 
550
                login_wnd,
 
551
                (HMENU) ID_ABOUT_BUTTON,
 
552
                g_ItmagesInst,
 
553
                NULL);
 
554
        upload = CreateWindowEx(
 
555
                0,
 
556
                "BUTTON",
 
557
                gettext("Upload"),
 
558
                WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPSIBLINGS,
 
559
                UPLOAD_BUTTON_X,
 
560
                UPLOAD_BUTTON_Y,
 
561
                UPLOAD_BUTTON_WIDTH,
 
562
                UPLOAD_BUTTON_HEIGHT,
 
563
                login_wnd,
 
564
                (HMENU) ID_UPLOAD_BUTTON,
 
565
                g_ItmagesInst,
 
566
                NULL);
 
567
 
 
568
        CreateTooltip(
 
569
                login,
 
570
                login_tool,
 
571
                gettext("Enter your login here"));
 
572
        CreateTooltip(
 
573
                passwd,
 
574
                passwd_tool,
 
575
                gettext("Enter your password here"));
 
576
        CreateTooltip(
 
577
                without_login,
 
578
                withoutlogin_tool,
 
579
                gettext("Upload the image without authentification"));
 
580
        CreateTooltip(
 
581
                save_data,
 
582
                savedata_tool,
 
583
                gettext("Store the login and password in the configuration file"));
 
584
        CreateTooltip(
 
585
                resize_img,
 
586
                resizeimg_tool,
 
587
                gettext("Respectively rescales the image to the selected width"));
 
588
        CreateTooltip(
 
589
                resize_size,
 
590
                size_tool,
 
591
                gettext("Enter the new image's width here"));
 
592
        CreateTooltip(
 
593
                resize_filter,
 
594
                filter_tool,
 
595
                gettext("Select the type of the rescale filter"));
 
596
        CreateTooltip(
 
597
                proxy_addrss,
 
598
                addrss_tool,
 
599
                gettext("Enter the address of the proxy server here"));
 
600
        CreateTooltip(
 
601
                proxy_port,
 
602
                port_tool,
 
603
                gettext("Enter the port of the proxy server here"));
 
604
 
 
605
        // User's login and password have to be less than 32 characters.
 
606
        SendMessage(login, EM_SETLIMITTEXT, (WPARAM) 32, 0);
 
607
        SendMessage(passwd, EM_SETLIMITTEXT, (WPARAM) 32, 0);
 
608
 
 
609
        // Limiting width value of the image to 4 digits.
 
610
        SendMessage(resize_size, EM_SETLIMITTEXT, (WPARAM) 4, 0);
 
611
 
 
612
        /* Limiting possible lenght of the proxy addres and port to
 
613
        15 and 4 chacters respectively. */
 
614
        SendMessage(proxy_addrss, EM_SETLIMITTEXT, (WPARAM) 15, 0);
 
615
        SendMessage(proxy_port, EM_SETLIMITTEXT, (WPARAM) 4, 0);
 
616
 
 
617
        // Configuring Up-Down control.
 
618
        SendMessage(resize_updown, UDM_SETBUDDY, (LONG) resize_size, 0L);
 
619
        SendMessage(resize_updown, UDM_SETRANGE, 0, MAKELONG(9999, 0));
 
620
        SendMessage(resize_updown, UDM_SETPOS, 0, MAKELONG(0, 0));
 
621
 
 
622
        // Resize and rotate image checkboxes are disabled by default.
 
623
        SendMessage(resize_img, BM_SETCHECK, (WPARAM) BST_UNCHECKED, 0);
 
624
        SendMessage(rotate_img, BM_SETCHECK, (WPARAM) BST_UNCHECKED, 0);
 
625
 
 
626
        // Setting possible rescale filters.
 
627
        SendMessage(resize_filter, CB_ADDSTRING, 0, (LPARAM) gettext("Lanczos"));
 
628
        SendMessage(resize_filter, CB_ADDSTRING, 0, (LPARAM) gettext("Box"));
 
629
        SendMessage(resize_filter, CB_ADDSTRING, 0, (LPARAM) gettext("Bilinear"));
 
630
        SendMessage(resize_filter, CB_ADDSTRING, 0, (LPARAM) gettext("B-Spline"));
 
631
 
 
632
        // Setting possible rotate angles.
 
633
        SendMessage(rotate_angle, CB_ADDSTRING, 0, (LPARAM) gettext("Do not rotate"));
 
634
        SendMessage(rotate_angle, CB_ADDSTRING, 0, (LPARAM) gettext("Left"));
 
635
        SendMessage(rotate_angle, CB_ADDSTRING, 0, (LPARAM) gettext("Right"));
 
636
        SendMessage(rotate_angle, CB_ADDSTRING, 0, (LPARAM) gettext("Flip"));
 
637
 
 
638
        // Setting the default values for the resize filter and rotate angle.
 
639
        SendMessage(resize_filter, CB_SETCURSEL, 0, 0);
 
640
        SendMessage(rotate_angle, CB_SETCURSEL, 0, 0);
 
641
 
 
642
        EnableWindow(resize_size, 0);
 
643
        EnableWindow(resize_updown, 0);
 
644
        EnableWindow(resize_filter, 0);
 
645
        EnableWindow(rotate_angle, 0);
 
646
 
 
647
        // Seting default system font for the child windows.
 
648
        SetSystemFont(login_wnd, ID_LOGIN, ID_UPLOAD_BUTTON);
 
649
 
 
650
        // Hidding child windows placed on the 2nd and 3rd tab.
 
651
        HideWindows(login_wnd, ID_CHECK_RESIZEIMG, ID_PROXYPORT_TXT);
 
652
 
 
653
        // Seting focus on Upload button by default.
 
654
        SetFocus(upload);
 
655
 
 
656
        char *config_path = WideToAcp(configFiles.config);
 
657
 
 
658
        char state[MAX_STATE];
 
659
        memset(state, 0, MAX_STATE);
 
660
 
 
661
        if (GetPrivateProfileString("Settings", "savedata", NULL, state, MAX_STATE + 1, config_path) == 0) {
 
662
            memcpy(state, "false", 6);
 
663
        }
 
664
 
 
665
        // If the "Save account setting" checkbox was checked.
 
666
        if (memcmp(state, "true", 5) == 0) {
 
667
            userInfo.login = (char*) calloc(MAX_LOGIN + 1, sizeof (char));
 
668
            if (userInfo.login == NULL) {
 
669
                ErrorsHandler(ERROR_MEMALLOC);
 
670
                exit(EXIT_FAILURE);
 
671
            }
 
672
 
 
673
            userInfo.passwd = (char*) calloc(MAX_PASSWD + 1, sizeof (char));
 
674
            if (userInfo.passwd == NULL) {
 
675
                ErrorsHandler(ERROR_MEMALLOC);
 
676
                exit(EXIT_FAILURE);
 
677
            }
 
678
 
 
679
            // Retrieving the user's login and password from the configuration file and putting them into the Edit controls.
 
680
            if (GetPrivateProfileString("Settings", "login", NULL, userInfo.login, MAX_LOGIN + 1, config_path) == 0 ||
 
681
                    GetPrivateProfileString("Settings", "passwd", NULL, userInfo.passwd, MAX_PASSWD + 1, config_path) == 0) {
 
682
                ErrorsHandler(ERROR_READCONFFILE);
 
683
            } else {
 
684
                SetWindowText(login, userInfo.login);
 
685
                SetWindowText(passwd, userInfo.passwd);
 
686
            }
 
687
            SendMessage(save_data, BM_SETCHECK, (WPARAM) TRUE, 0);
 
688
        } else {
 
689
            EnableWindow(login, 0);
 
690
            EnableWindow(passwd, 0);
 
691
            SendMessage(without_login, BM_SETCHECK, (WPARAM) TRUE, 0);
 
692
        }
 
693
 
 
694
        memset(state, 0, MAX_STATE);
 
695
 
 
696
        if (GetPrivateProfileString("Proxy", "useproxy", NULL, state, MAX_STATE + 1, config_path) == 0) {
 
697
            memcpy(state, "false", 6);
 
698
        }
 
699
 
 
700
        // If "Use proxy server" checkbox was checked.
 
701
        if (memcmp(state, "true", 5) == 0) {
 
702
            userInfo.address = (char*) calloc(MAX_ADDRSS + 1, sizeof (char));
 
703
            if (userInfo.address == NULL) {
 
704
                ErrorsHandler(ERROR_MEMALLOC);
 
705
                exit(EXIT_FAILURE);
 
706
            }
 
707
 
 
708
            userInfo.port = (char*) calloc(MAX_PORT + 1, sizeof (char));
 
709
            if (userInfo.port == NULL) {
 
710
                ErrorsHandler(ERROR_MEMALLOC);
 
711
                exit(EXIT_FAILURE);
 
712
            }
 
713
 
 
714
            // Retrieving proxy address and port from the configuration file and putting them into the Edit controls.
 
715
            if (GetPrivateProfileString("Proxy", "address", NULL, userInfo.address, MAX_ADDRSS + 1, config_path) == 0 ||
 
716
                    GetPrivateProfileString("Proxy", "port", NULL, userInfo.port, MAX_PORT + 1, config_path) == 0) {
 
717
                ErrorsHandler(ERROR_READCONFFILE);
 
718
            } else {
 
719
                SetWindowText(proxy_addrss, userInfo.address);
 
720
                SetWindowText(proxy_port, userInfo.port);
 
721
            }
 
722
            SendMessage(use_proxy, BM_SETCHECK, (WPARAM) TRUE, 0);
 
723
        } else {
 
724
            EnableWindow(proxy_addrss, 0);
 
725
            EnableWindow(proxy_port, 0);
 
726
            SendMessage(use_proxy, BM_SETCHECK, (WPARAM) FALSE, 0);
 
727
        }
 
728
        free(config_path);
 
729
    }
 
730
        break;
 
731
    case WM_COMMAND:
 
732
        if (LOWORD(wParam) == ID_CHECK_WITHOUTLOGIN) {
 
733
            LRESULT result = SendMessage(without_login, BM_GETCHECK, 0, 0);
 
734
            if (result == BST_CHECKED) {
 
735
                EnableWindow(login, 0);
 
736
                EnableWindow(passwd, 0);
 
737
                SendMessage(login, EM_SETSEL, 0, -1);
 
738
                SendMessage(login, WM_CLEAR, 0, 0);
 
739
                SendMessage(passwd, EM_SETSEL, 0, -1);
 
740
                SendMessage(passwd, WM_CLEAR, 0, 0);
 
741
                SendMessage(save_data, BM_SETCHECK, (WPARAM) FALSE, 0);
 
742
            } else {
 
743
                EnableWindow(login, 1);
 
744
                EnableWindow(passwd, 1);
 
745
                SendMessage(without_login, BM_SETCHECK, (WPARAM) FALSE, 0);
 
746
            }
 
747
        }
 
748
 
 
749
        if (LOWORD(wParam) == ID_CHECK_SAVEDATA) {
 
750
            LRESULT result = SendMessage(without_login, BM_GETCHECK, 0, 0);
 
751
            if (result == BST_CHECKED) {
 
752
                EnableWindow(login, 1);
 
753
                EnableWindow(passwd, 1);
 
754
                SendMessage(without_login, BM_SETCHECK, (WPARAM) FALSE, 0);
 
755
            }
 
756
        }
 
757
 
 
758
        if (LOWORD(wParam) == ID_LOGIN && HIWORD(wParam) == EN_CHANGE) {
 
759
            if (userInfo.login == NULL) {
 
760
                userInfo.login = (char*) calloc(MAX_LOGIN + 1, sizeof (char));
 
761
                if (userInfo.login == NULL) {
 
762
                    ErrorsHandler(ERROR_MEMALLOC);
 
763
                    exit(EXIT_FAILURE);
 
764
                }
 
765
            } else {
 
766
                memset(userInfo.login, 0, MAX_LOGIN + 1);
 
767
            }
 
768
            GetWindowText(login, userInfo.login, MAX_LOGIN + 1);
 
769
        }
 
770
 
 
771
        if (LOWORD(wParam) == ID_PASSWD && HIWORD(wParam) == EN_CHANGE) {
 
772
            if (userInfo.passwd == NULL) {
 
773
                userInfo.passwd = (char*) calloc(MAX_PASSWD + 1, sizeof (char));
 
774
                if (userInfo.passwd == NULL) {
 
775
                    ErrorsHandler(ERROR_MEMALLOC);
 
776
                    exit(EXIT_FAILURE);
 
777
                }
 
778
            } else {
 
779
                memset(userInfo.passwd, 0, MAX_PASSWD + 1);
 
780
            }
 
781
            GetWindowText(passwd, userInfo.passwd, MAX_PASSWD + 1);
 
782
        }
 
783
 
 
784
        if (LOWORD(wParam) == ID_CHECK_RESIZEIMG) {
 
785
            LRESULT result = SendMessage(resize_img, BM_GETCHECK, 0, 0);
 
786
            if (result == BST_CHECKED) {
 
787
                EnableWindow(resize_size, 1);
 
788
                EnableWindow(resize_updown, 1);
 
789
                EnableWindow(resize_filter, 1);
 
790
            } else {
 
791
                EnableWindow(resize_size, 0);
 
792
                EnableWindow(resize_updown, 0);
 
793
                EnableWindow(resize_filter, 0);
 
794
            }
 
795
        }
 
796
 
 
797
        if (LOWORD(wParam) == ID_CHECK_ROTATEIMG) {
 
798
            LRESULT result = SendMessage(rotate_img, BM_GETCHECK, 0, 0);
 
799
            if (result == BST_CHECKED) {
 
800
                EnableWindow(rotate_angle, 1);
 
801
            } else {
 
802
                EnableWindow(rotate_angle, 0);
 
803
            }
 
804
        }
 
805
 
 
806
        if (LOWORD(wParam) == ID_RESIZEIMG_SIZE && HIWORD(wParam) == EN_CHANGE) {
 
807
            if (imageInfo.width == 0) {
 
808
                ErrorsHandler(ERROR_GETIMGWIDTH);
 
809
            } else {
 
810
                char width[MAX_DIM_CHARS];
 
811
                memset(width, 0, MAX_DIM_CHARS);
 
812
 
 
813
                GetWindowText(resize_size, width, MAX_DIM_CHARS + 1);
 
814
                size_t new_width = atoi(width);
 
815
 
 
816
                // Reduce value have to be less than actual width of the image.
 
817
                if (new_width > imageInfo.width) {
 
818
                    ErrorsHandler(ERROR_BIGWIDTH);
 
819
                    SendMessage(resize_size, EM_SETSEL, 0, -1);
 
820
                    SendMessage(resize_size, WM_CLEAR, 0, 0);
 
821
                }
 
822
            }
 
823
        }
 
824
 
 
825
        if (LOWORD(wParam) == ID_CHECK_USEPROXY) {
 
826
            LRESULT result = SendMessage(use_proxy, BM_GETCHECK, 0, 0);
 
827
            if (result == BST_CHECKED) {
 
828
                EnableWindow(proxy_addrss, 1);
 
829
                EnableWindow(proxy_port, 1);
 
830
            } else {
 
831
                EnableWindow(proxy_addrss, 0);
 
832
                EnableWindow(proxy_port, 0);
 
833
                SendMessage(proxy_addrss, EM_SETSEL, 0, -1);
 
834
                SendMessage(proxy_addrss, WM_CLEAR, 0, 0);
 
835
                SendMessage(proxy_port, EM_SETSEL, 0, -1);
 
836
                SendMessage(proxy_port, WM_CLEAR, 0, 0);
 
837
            }
 
838
        }
 
839
 
 
840
        if (LOWORD(wParam) == ID_PROXY_ADDRESS && HIWORD(wParam) == EN_CHANGE) {
 
841
            GetWindowText(proxy_addrss, userInfo.address, 16);
 
842
        }
 
843
 
 
844
        if (LOWORD(wParam) == ID_PROXY_PORT && HIWORD(wParam) == EN_CHANGE) {
 
845
            GetWindowText(proxy_port, userInfo.port, 5);
 
846
        }
 
847
 
 
848
        // If the "About" button was pressed.
 
849
        if (LOWORD(wParam) == ID_ABOUT_BUTTON) {
 
850
            CreateAboutWnd();
 
851
            EnableWindow(login_wnd, 0);
 
852
        }
 
853
 
 
854
        // If the "Upload" button was pressed.
 
855
        if (LOWORD(wParam) == ID_UPLOAD_BUTTON) {
 
856
            EnableWindow(upload, 0);
 
857
 
 
858
            char *config_path = WideToAcp(configFiles.config);
 
859
 
 
860
            LRESULT resize_result = SendMessage(resize_img, BM_GETCHECK, 0, 0);
 
861
            LRESULT rotate_result = SendMessage(rotate_img, BM_GETCHECK, 0, 0);
 
862
 
 
863
            if (resize_result == BST_CHECKED || rotate_result == BST_CHECKED) {
 
864
                size_t choise = MessageBox(
 
865
                        login_wnd,
 
866
                        gettext("The current image was modified.\nApply the changes?"),
 
867
                        gettext("ITmages - Image"),
 
868
                        MB_YESNO | MB_ICONQUESTION);
 
869
 
 
870
                if (choise == IDYES) {
 
871
                    if (resize_result == BST_CHECKED) {
 
872
                        char width[MAX_DIM_CHARS];
 
873
                        memset(width, 0, MAX_DIM_CHARS);
 
874
 
 
875
                        GetWindowText(resize_size, width, MAX_DIM_CHARS + 1);
 
876
 
 
877
                        size_t new_width = atoi(width);
 
878
                        size_t filter_index = SendMessage(resize_filter, CB_GETCURSEL, 0, 0);
 
879
 
 
880
                        ResizeImageTo(imageData.path, new_width, filter_index);
 
881
                        imageInfo.size = GetImageSize(imageData.path);
 
882
                    }
 
883
 
 
884
                    if (rotate_result == BST_CHECKED) {
 
885
                        int angle;
 
886
 
 
887
                        size_t selected_index = SendMessage(rotate_angle, CB_GETCURSEL, 0, 0);
 
888
                        switch (selected_index) {
 
889
                        case 0: angle = 0; // Do not rotate
 
890
                            break;
 
891
                        case 1: angle = 90; // Left
 
892
                            break;
 
893
                        case 2: angle = -90; // Rigth
 
894
                            break;
 
895
                        case 3: angle = 180; // Flip
 
896
                            break;
 
897
                        }
 
898
                        RotateImageTo(imageData.path, angle);
 
899
                        imageInfo.size = GetImageSize(imageData.path);
 
900
                    }
 
901
 
 
902
                                        /* Retrieving the information about the image again, coz the
 
903
                    image's properties was changed. */
 
904
                    FreeImageInfo(&imageInfo);
 
905
                    if (GetImageInfo(imageData.path, imageData.basename,
 
906
                            imageData.ext, &imageInfo) == -1) {
 
907
                        ErrorsHandler(ERROR_GETIMGINFO);
 
908
                        exit(EXIT_FAILURE);
 
909
                    }
 
910
                }
 
911
            }
 
912
 
 
913
            /* If the "Save account settings" checkbox was checked - saving login
 
914
            and password to the configuration file. */
 
915
            LRESULT result_savedata = SendMessage(save_data, BM_GETCHECK, 0, 0);
 
916
            if (result_savedata == BST_CHECKED) {
 
917
                if (WritePrivateProfileString("Settings", "login", userInfo.login, config_path) == 0 ||
 
918
                        WritePrivateProfileString("Settings", "passwd", userInfo.passwd, config_path) == 0 ||
 
919
                        WritePrivateProfileString("Settings", "savedata", "true", config_path) == 0) {
 
920
                    ErrorsHandler(ERROR_WRITECONFFILE);
 
921
                }
 
922
            } else {
 
923
                if (WritePrivateProfileString("Settings", "savedata", "false", config_path) == 0) {
 
924
                    ErrorsHandler(ERROR_WRITECONFFILE);
 
925
                }
 
926
            }
 
927
 
 
928
            /* If the "Use proxy server" checkbox was checked - saving proxy
 
929
            address and port to the configuration file. */
 
930
            LRESULT result_useproxy = SendMessage(use_proxy, BM_GETCHECK, 0, 0);
 
931
            if (result_useproxy == BST_CHECKED) {
 
932
                if (WritePrivateProfileString("Proxy", "address", userInfo.address, config_path) == 0 ||
 
933
                        WritePrivateProfileString("Proxy", "port", userInfo.port, config_path) == 0 ||
 
934
                        WritePrivateProfileString("Proxy", "useproxy", "true", config_path) == 0) {
 
935
                    ErrorsHandler(ERROR_WRITECONFFILE);
 
936
                }
 
937
            } else {
 
938
                if (WritePrivateProfileString("Proxy", "useproxy", "false", config_path) == 0) {
 
939
                    ErrorsHandler(ERROR_WRITECONFFILE);
 
940
                }
 
941
            }
 
942
 
 
943
            /* Checking the image's size again to be sure that the image still
 
944
            has an appropriate size. */
 
945
            if (imageInfo.size == -1) {
 
946
                ErrorsHandler(ERROR_MAXIMGSIZE);
 
947
                exit(EXIT_FAILURE);
 
948
            } else if (imageInfo.size == 0) {
 
949
                ErrorsHandler(ERROR_BADIMGSIZE);
 
950
                exit(EXIT_FAILURE);
 
951
            } else {
 
952
                // Starting the upload threads.
 
953
                LRESULT result_withoutlogin = SendMessage(without_login, BM_GETCHECK, 0, 0);
 
954
                if (result_withoutlogin == BST_CHECKED) {
 
955
                    HANDLE withoutlogin_thread = (HANDLE) _beginthreadex(NULL, 0, (void*) UploadWithoutLoginThread, login_wnd, 0, NULL);
 
956
                    if (withoutlogin_thread == NULL) {
 
957
                        ErrorsHandler(ERROR_CREATETHREAD);
 
958
                    }
 
959
                    CloseHandle(withoutlogin_thread);
 
960
                } else {
 
961
                    HANDLE withlogin_thread = (HANDLE) _beginthreadex(NULL, 0, (void*) UploadWithLoginThread, login_wnd, 0, NULL);
 
962
                    if (withlogin_thread == NULL) {
 
963
                        ErrorsHandler(ERROR_CREATETHREAD);
 
964
                    }
 
965
                    CloseHandle(withlogin_thread);
 
966
                }
 
967
            }
 
968
            free(config_path);
 
969
        }
 
970
        break;
 
971
    case WM_NOTIFY:
 
972
    {
 
973
        LPNMHDR lpnmhdr = (LPNMHDR) lParam;
 
974
        if (lpnmhdr->code == TCN_SELCHANGE) {
 
975
            size_t nTab = TabCtrl_GetCurSel(login_tab);
 
976
            switch (nTab) {
 
977
            case 0:
 
978
                ShowTabWindows(
 
979
                        login_wnd,
 
980
                        ID_LOGIN,
 
981
                        ID_PROXYPORT_TXT,
 
982
                        ID_LOGIN,
 
983
                        ID_CHECK_SAVEDATA);
 
984
                break;
 
985
            case 1:
 
986
                ShowTabWindows(
 
987
                        login_wnd,
 
988
                        ID_LOGIN,
 
989
                        ID_PROXYPORT_TXT,
 
990
                        ID_CHECK_RESIZEIMG,
 
991
                        ID_ROTATEIMG_ANGLE);
 
992
                break;
 
993
            case 2:
 
994
                ShowTabWindows(
 
995
                        login_wnd,
 
996
                        ID_LOGIN,
 
997
                        ID_PROXYPORT_TXT,
 
998
                        ID_CHECK_USEPROXY,
 
999
                        ID_PROXYPORT_TXT);
 
1000
                break;
 
1001
            }
 
1002
        }
 
1003
    }
 
1004
        break;
 
1005
    case WM_FINISHTHREAD:
 
1006
        /* If image upload was successful - creating the Ready window and minimizing
 
1007
        the Login window into the taskbar. */
 
1008
        CreateReadyWnd();
 
1009
        EnableWindow(login_wnd, 0);
 
1010
        break;
 
1011
    case WM_SYSCOLORCHANGE:
 
1012
        /* Updating the statics' and checkboxes' background if the system's
 
1013
        visual style was changed. */
 
1014
        UpdateBackgroundBrush(login_tab);
 
1015
        break;
 
1016
    case WM_CTLCOLORSTATIC:
 
1017
        /* If the visual style is enabled - drawing an appropriate background
 
1018
        for the static controls and checkboxes. */
 
1019
        if (g_ThemeActive == 1) {
 
1020
            SetThemedBackground(login_tab, (HWND) lParam, (HDC) wParam);
 
1021
            return (LRESULT) (g_StaticBrush);
 
1022
        } else {
 
1023
            SetBkColor((HDC) wParam, (COLORREF) GetSysColor(COLOR_BTNFACE));
 
1024
            return (LRESULT) GetSysColorBrush(COLOR_BTNFACE);
 
1025
        }
 
1026
        break;
 
1027
    case WM_DESTROY:
 
1028
        FreeImageInfo(&imageInfo);
 
1029
        FreeImageLinks(&imageLinks);
 
1030
        FreeImageData(&imageData);
 
1031
        FreeUserInfo(&userInfo);
 
1032
        FreeConfig(&configFiles);
 
1033
 
 
1034
        PostQuitMessage(0);
 
1035
        break;
 
1036
    default:
 
1037
        return DefWindowProc(login_wnd, message, wParam, lParam);
 
1038
    }
 
1039
    return 0;
 
1040
}
 
1041
 
 
1042
INT APIENTRY WinAbout()
 
1043
{
 
1044
    WNDCLASSEX itmagesAbout;
 
1045
    memset(&itmagesAbout, 0, sizeof (itmagesAbout));
 
1046
 
 
1047
    itmagesAbout.cbSize = sizeof (itmagesAbout);
 
1048
    itmagesAbout.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
 
1049
    itmagesAbout.lpfnWndProc = WinAboutProc;
 
1050
    itmagesAbout.cbClsExtra = 0;
 
1051
    itmagesAbout.cbWndExtra = 0;
 
1052
    itmagesAbout.hInstance = g_ItmagesInst;
 
1053
    itmagesAbout.hIcon = g_AppIcon;
 
1054
    itmagesAbout.hCursor = LoadCursor(NULL, IDC_ARROW);
 
1055
    itmagesAbout.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
 
1056
    itmagesAbout.lpszMenuName = NULL;
 
1057
    itmagesAbout.lpszClassName = g_AboutWindowClass;
 
1058
    itmagesAbout.hIconSm = NULL;
 
1059
 
 
1060
    if (RegisterClassEx(&itmagesAbout) == 0) {
 
1061
        return -1;
 
1062
    }
 
1063
    return 0;
 
1064
}
 
1065
 
 
1066
void CreateAboutWnd()
 
1067
{
 
1068
    size_t frame_width = GetSystemMetrics(SM_CXSIZEFRAME);
 
1069
    size_t frame_height = GetSystemMetrics(SM_CYSIZEFRAME);
 
1070
 
 
1071
    RECT parent_rect;
 
1072
    GetWindowRect(login_wnd, &parent_rect);
 
1073
 
 
1074
    about_wnd = CreateWindowEx(
 
1075
            WS_EX_DLGMODALFRAME,
 
1076
            g_AboutWindowClass,
 
1077
            gettext("ITmages - About"),
 
1078
            WS_POPUP | WS_VISIBLE | WS_SYSMENU | WS_CAPTION | WS_THICKFRAME | WS_CLIPCHILDREN,
 
1079
            (parent_rect.left + LOGIN_WND_WIDTH / 2) - ABOUT_WND_WIDTH / 2,
 
1080
            (parent_rect.top + LOGIN_WND_HEIGHT / 2) - ABOUT_WND_HEIGHT / 2,
 
1081
            ABOUT_WND_WIDTH + frame_width,
 
1082
            ABOUT_WND_HEIGHT + frame_height,
 
1083
            login_wnd,
 
1084
            NULL,
 
1085
            g_ItmagesInst,
 
1086
            NULL);
 
1087
 
 
1088
    // Creating the Tab conrol for the About window
 
1089
    CreateAboutTabControl();
 
1090
 
 
1091
    ShowWindow(about_wnd, SW_SHOW);
 
1092
    UpdateWindow(about_wnd);
 
1093
}
 
1094
 
 
1095
void CreateAboutTabControl()
 
1096
{
 
1097
    HWND about_tab = CreateWindowEx(
 
1098
            0,
 
1099
            WC_TABCONTROL,
 
1100
            "",
 
1101
            WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPCHILDREN,
 
1102
            ABOUT_TABCONTROL_X,
 
1103
            ABOUT_TABCONTROL_Y,
 
1104
            ABOUT_TABCONTROL_WIDTH,
 
1105
            ABOUT_TABCONTROL_HEIGHT,
 
1106
            about_wnd,
 
1107
            (HMENU) ID_ABOUT_TABCONTROL,
 
1108
            g_ItmagesInst,
 
1109
            NULL);
 
1110
 
 
1111
    SendMessage(about_tab, WM_SETFONT, (WPARAM) g_SystemFont, 0);
 
1112
 
 
1113
    TCITEM tie;
 
1114
    memset(&tie, 0, sizeof (tie));
 
1115
 
 
1116
    tie.mask = TCIF_TEXT;
 
1117
    tie.pszText = gettext("About");
 
1118
    TabCtrl_InsertItem(about_tab, 0, &tie);
 
1119
    tie.pszText = gettext("License");
 
1120
    TabCtrl_InsertItem(about_tab, 1, &tie);
 
1121
    tie.pszText = gettext("Credits");
 
1122
    TabCtrl_InsertItem(about_tab, 2, &tie);
 
1123
 
 
1124
    UpdateBackgroundBrush(about_tab);
 
1125
}
 
1126
 
 
1127
LRESULT CALLBACK WinAboutProc(
 
1128
        HWND about_wnd,
 
1129
        UINT message,
 
1130
        WPARAM wParam,
 
1131
        LPARAM lParam)
 
1132
{
 
1133
    HWND about_tab = GetDlgItem(about_wnd, ID_ABOUT_TABCONTROL);
 
1134
    static HWND itmages_img,
 
1135
            about_info,
 
1136
            license_info,
 
1137
            credits_info,
 
1138
            close;
 
1139
 
 
1140
    size_t frame_width = GetSystemMetrics(SM_CXSIZEFRAME);
 
1141
    size_t frame_height = GetSystemMetrics(SM_CYSIZEFRAME);
 
1142
 
 
1143
    switch (message) {
 
1144
    case WM_CREATE:
 
1145
        itmages_img = CreateWindowEx(
 
1146
                0,
 
1147
                "STATIC",
 
1148
                "",
 
1149
                WS_CHILD | WS_VISIBLE | SS_ICON | WS_CLIPSIBLINGS,
 
1150
                ITMAGES_IMG_X,
 
1151
                ITMAGES_IMG_Y,
 
1152
                CW_USEDEFAULT,
 
1153
                CW_USEDEFAULT,
 
1154
                about_wnd,
 
1155
                (HMENU) ID_ITMAGES_IMG,
 
1156
                g_ItmagesInst,
 
1157
                NULL);
 
1158
        about_info = CreateWindowEx(
 
1159
                0,
 
1160
                "STATIC",
 
1161
                gettext("ITmages upload client\n"
 
1162
                "Version : 0.3\n"
 
1163
                "Author : Dmitriy Simbiriatin\n"
 
1164
                "E - mail : slpiv@itmages.ru\n"
 
1165
                "Copyright(c) 2011 Dmitriy Simbiriatin\n"
 
1166
                "License : GPL version 2"),
 
1167
                WS_CHILD | WS_VISIBLE | ES_LEFT | WS_CLIPSIBLINGS,
 
1168
                ABOUT_INFO_X,
 
1169
                ABOUT_INFO_Y,
 
1170
                ABOUT_INFO_WIDTH,
 
1171
                ABOUT_INFO_HEIGHT,
 
1172
                about_wnd,
 
1173
                (HMENU) ID_ABOUT_INFO,
 
1174
                g_ItmagesInst,
 
1175
                NULL);
 
1176
        license_info = CreateWindowEx(
 
1177
                0,
 
1178
                "EDIT",
 
1179
                "",
 
1180
                WS_CHILD | WS_VISIBLE | ES_LEFT | ES_MULTILINE |
 
1181
                WS_VSCROLL | ES_READONLY | ES_WANTRETURN |
 
1182
                ES_AUTOVSCROLL | WS_CLIPSIBLINGS,
 
1183
                LICENSE_INFO_X,
 
1184
                LICENSE_INFO_Y,
 
1185
                LICENSE_INFO_WIDTH,
 
1186
                LICENSE_INFO_HEIGHT,
 
1187
                about_wnd,
 
1188
                (HMENU) ID_LICENSE_INFO,
 
1189
                g_ItmagesInst,
 
1190
                NULL);
 
1191
        credits_info = CreateWindowEx(
 
1192
                0,
 
1193
                "EDIT",
 
1194
                "",
 
1195
                WS_CHILD | WS_VISIBLE | ES_LEFT | ES_MULTILINE |
 
1196
                WS_VSCROLL | ES_READONLY | ES_WANTRETURN |
 
1197
                ES_AUTOVSCROLL | WS_CLIPSIBLINGS,
 
1198
                CREDITS_INFO_X,
 
1199
                CREDITS_INFO_Y,
 
1200
                CREDITS_INFO_WIDTH,
 
1201
                CREDITS_INFO_HEIGHT,
 
1202
                about_wnd,
 
1203
                (HMENU) ID_CREDITS_INFO,
 
1204
                g_ItmagesInst,
 
1205
                NULL);
 
1206
        close = CreateWindowEx(
 
1207
                0,
 
1208
                "BUTTON",
 
1209
                gettext("Close"),
 
1210
                WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPSIBLINGS,
 
1211
                CLOSE_BUTTON_X,
 
1212
                CLOSE_BUTTON_Y,
 
1213
                CLOSE_BUTTON_WIDTH,
 
1214
                CLOSE_BUTTON_HEIGHT,
 
1215
                about_wnd,
 
1216
                (HMENU) ID_CLOSE_BUTTON,
 
1217
                g_ItmagesInst,
 
1218
                NULL);
 
1219
 
 
1220
        // Putting ITmages logo into the static control.
 
1221
        SendMessage(itmages_img, STM_SETIMAGE, IMAGE_ICON, (LPARAM) g_AppIcon);
 
1222
 
 
1223
        // Hiding the windows that are placed on the 2nd and the 3rd tabs.
 
1224
        HideWindows(about_wnd, ID_LICENSE_INFO, ID_CREDITS_INFO);
 
1225
 
 
1226
        // Setting the system's default font for the child windows.
 
1227
        SetSystemFont(about_wnd, ID_ABOUT_INFO, ID_CLOSE_BUTTON);
 
1228
 
 
1229
        // Loading the documentation files.
 
1230
        if (LoadDocFile(license_info, DOCS_DIR, "COPYING.txt") == -1) {
 
1231
                        SetWindowText(license_info, gettext("Failed to open the file."));
 
1232
                }
 
1233
        if (LoadDocFile(credits_info, DOCS_DIR, "CREDITS.txt") == -1) {
 
1234
                        SetWindowText(credits_info, gettext("Failed to open the file."));
 
1235
                }
 
1236
        break;
 
1237
    case WM_COMMAND:
 
1238
        if (LOWORD(wParam) == ID_CLOSE_BUTTON) {
 
1239
            DestroyWindow(about_wnd);
 
1240
        }
 
1241
        break;
 
1242
    case WM_NOTIFY:
 
1243
    {
 
1244
        LPNMHDR lpnmhdr = (LPNMHDR) lParam;
 
1245
        if (lpnmhdr->code == TCN_SELCHANGE) {
 
1246
            size_t nTab = TabCtrl_GetCurSel(about_tab);
 
1247
            switch (nTab) {
 
1248
            case 0:
 
1249
                ShowTabWindows(
 
1250
                        about_wnd,
 
1251
                        ID_ITMAGES_IMG,
 
1252
                        ID_CREDITS_INFO,
 
1253
                        ID_ITMAGES_IMG,
 
1254
                        ID_ABOUT_INFO);
 
1255
                break;
 
1256
            case 1:
 
1257
                ShowTabWindows(
 
1258
                        about_wnd,
 
1259
                        ID_ITMAGES_IMG,
 
1260
                        ID_CREDITS_INFO,
 
1261
                        ID_LICENSE_INFO,
 
1262
                        ID_LICENSE_INFO);
 
1263
                break;
 
1264
            case 2:
 
1265
                ShowTabWindows(
 
1266
                        about_wnd,
 
1267
                        ID_ITMAGES_IMG,
 
1268
                        ID_CREDITS_INFO,
 
1269
                        ID_CREDITS_INFO,
 
1270
                        ID_CREDITS_INFO);
 
1271
                break;
 
1272
            }
 
1273
        }
 
1274
    }
 
1275
        break;
 
1276
    case WM_SYSCOLORCHANGE:
 
1277
        /* Updating the statics' and checkboxes' background if the system's
 
1278
        visual style was changed. */
 
1279
        UpdateBackgroundBrush(about_tab);
 
1280
        break;
 
1281
    case WM_CTLCOLORSTATIC:
 
1282
        /* If the visual style is enabled - drawing an appropriate background
 
1283
        for the static controls and checkboxes. */
 
1284
        if (g_ThemeActive == 1) {
 
1285
            SetThemedBackground(about_tab, (HWND) lParam, (HDC) wParam);
 
1286
            return (LRESULT) g_StaticBrush;
 
1287
        } else {
 
1288
            SetBkColor((HDC) wParam, (COLORREF) GetSysColor(COLOR_BTNFACE));
 
1289
            return (LRESULT) GetSysColorBrush(COLOR_BTNFACE);
 
1290
        }
 
1291
        break;
 
1292
    case WM_SIZING:
 
1293
    {
 
1294
        UpdateBackgroundBrush(about_tab);
 
1295
 
 
1296
        RECT about_rect;
 
1297
        GetWindowRect(about_wnd, &about_rect);
 
1298
 
 
1299
        // Difference between the initial size of the window and the new one.
 
1300
        int delta_width = ((about_rect.right - about_rect.left) - ABOUT_WND_WIDTH);
 
1301
        int delta_height = ((about_rect.bottom - about_rect.top) - ABOUT_WND_HEIGHT);
 
1302
 
 
1303
        MoveWindow(about_tab,
 
1304
                ABOUT_TABCONTROL_X,
 
1305
                ABOUT_TABCONTROL_Y,
 
1306
                ABOUT_TABCONTROL_WIDTH + delta_width - frame_width,
 
1307
                ABOUT_TABCONTROL_HEIGHT + delta_height - frame_height,
 
1308
                TRUE);
 
1309
        MoveWindow(about_info,
 
1310
                ABOUT_INFO_X,
 
1311
                ABOUT_INFO_Y,
 
1312
                ABOUT_INFO_WIDTH + delta_width - frame_width,
 
1313
                ABOUT_INFO_HEIGHT + delta_height - frame_height,
 
1314
                TRUE);
 
1315
        MoveWindow(license_info,
 
1316
                LICENSE_INFO_X,
 
1317
                LICENSE_INFO_Y,
 
1318
                LICENSE_INFO_WIDTH + delta_width - frame_width,
 
1319
                LICENSE_INFO_HEIGHT + delta_height - frame_height,
 
1320
                TRUE);
 
1321
        MoveWindow(credits_info,
 
1322
                CREDITS_INFO_X,
 
1323
                CREDITS_INFO_Y,
 
1324
                CREDITS_INFO_WIDTH + delta_width - frame_width,
 
1325
                CREDITS_INFO_HEIGHT + delta_height - frame_height,
 
1326
                TRUE);
 
1327
        MoveWindow(close,
 
1328
                CLOSE_BUTTON_X + delta_width - frame_width,
 
1329
                CLOSE_BUTTON_Y + delta_height - frame_height,
 
1330
                CLOSE_BUTTON_WIDTH,
 
1331
                CLOSE_BUTTON_HEIGHT,
 
1332
                TRUE);
 
1333
    }
 
1334
        break;
 
1335
    case WM_GETMINMAXINFO:
 
1336
    {
 
1337
        // Setting the minimal width and height of the About window.
 
1338
        ((MINMAXINFO*) lParam)->ptMinTrackSize.x = ABOUT_WND_WIDTH + frame_width;
 
1339
        ((MINMAXINFO*) lParam)->ptMinTrackSize.y = ABOUT_WND_HEIGHT + frame_height;
 
1340
    }
 
1341
        break;
 
1342
    case WM_DESTROY:
 
1343
        EnableWindow(login_wnd, 1);
 
1344
        SetFocus(login_wnd);
 
1345
        DestroyWindow(about_wnd);
 
1346
        break;
 
1347
    default:
 
1348
        return DefWindowProc(about_wnd, message, wParam, lParam);
 
1349
    }
 
1350
    return 0;
 
1351
}
 
1352
 
 
1353
INT APIENTRY WinReady()
 
1354
{
 
1355
    WNDCLASSEX itmagesReady;
 
1356
    memset(&itmagesReady, 0, sizeof (itmagesReady));
 
1357
 
 
1358
    itmagesReady.cbSize = sizeof (itmagesReady);
 
1359
    itmagesReady.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
 
1360
    itmagesReady.lpfnWndProc = WinReadyProc;
 
1361
    itmagesReady.cbClsExtra = 0;
 
1362
    itmagesReady.cbWndExtra = 0;
 
1363
    itmagesReady.hInstance = g_ItmagesInst;
 
1364
    itmagesReady.hIcon = g_AppIcon;
 
1365
    itmagesReady.hCursor = LoadCursor(NULL, IDC_ARROW);
 
1366
    itmagesReady.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
 
1367
    itmagesReady.lpszMenuName = NULL;
 
1368
    itmagesReady.lpszClassName = g_ReadyWindowClass;
 
1369
    itmagesReady.hIconSm = NULL;
 
1370
 
 
1371
    if (RegisterClassEx(&itmagesReady) == 0) {
 
1372
        return -1;
 
1373
    }
 
1374
    return 0;
 
1375
}
 
1376
 
 
1377
void CreateReadyWnd()
 
1378
{
 
1379
    RECT parent_rect;
 
1380
    GetWindowRect(login_wnd, &parent_rect);
 
1381
 
 
1382
    ready_wnd = CreateWindowEx(
 
1383
            WS_EX_DLGMODALFRAME,
 
1384
            g_ReadyWindowClass,
 
1385
            gettext("ITmages - Ready!"),
 
1386
            WS_POPUP | WS_VISIBLE | WS_SYSMENU | WS_CAPTION | WS_CLIPCHILDREN,
 
1387
            (parent_rect.left + LOGIN_WND_WIDTH / 2) - READY_WND_WIDTH / 2,
 
1388
            (parent_rect.top + LOGIN_WND_HEIGHT / 2) - READY_WND_HEIGHT / 2,
 
1389
            READY_WND_WIDTH,
 
1390
            READY_WND_HEIGHT,
 
1391
            login_wnd,
 
1392
            NULL,
 
1393
            g_ItmagesInst,
 
1394
            NULL);
 
1395
 
 
1396
    // Creating a preview for the uploaded image.
 
1397
    HWND preview = GetDlgItem(ready_wnd, ID_IMAGE_PREVIEW);
 
1398
    previewImage = (HBITMAP) HBITMAPFromFile(
 
1399
            preview,
 
1400
            imageData.path,
 
1401
            IMAGE_PREVIEW_WIDTH,
 
1402
            IMAGE_PREVIEW_HEIGHT);
 
1403
 
 
1404
    if (previewImage != NULL) {
 
1405
        SendMessage(preview, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM) previewImage);
 
1406
    }
 
1407
 
 
1408
    ShowWindow(ready_wnd, SW_SHOW);
 
1409
    UpdateWindow(ready_wnd);
 
1410
}
 
1411
 
 
1412
LRESULT CALLBACK WinReadyProc(
 
1413
        HWND ready_wnd,
 
1414
        UINT message,
 
1415
        WPARAM wParam,
 
1416
        LPARAM lParam)
 
1417
{
 
1418
    static HWND preview,
 
1419
            links_list,
 
1420
            link_preview,
 
1421
            copy_to_clip,
 
1422
            open_in_bsr;
 
1423
 
 
1424
    static HWND links_list_tool,
 
1425
            preview_tool,
 
1426
            clip_tool,
 
1427
            bsr_tool;
 
1428
 
 
1429
    static char image_info[MAX_INFO];
 
1430
    static char preview_buffer[MAX_PREVIEW];
 
1431
 
 
1432
    switch (message) {
 
1433
    case WM_PAINT:
 
1434
    {
 
1435
        DefWindowProc(ready_wnd, message, wParam, lParam);
 
1436
 
 
1437
        HDC ready_wnd_dc = GetDC(ready_wnd);
 
1438
 
 
1439
        RECT text_rect;
 
1440
        RECT edge_rect;
 
1441
 
 
1442
        SetRect(&text_rect, 18, 8, 165, 25);
 
1443
        SetRect(&edge_rect, 10, 15, 170, 140);
 
1444
 
 
1445
        SetBkColor(ready_wnd_dc, GetSysColor(COLOR_BTNFACE));
 
1446
        SelectObject(ready_wnd_dc, g_SystemFont);
 
1447
 
 
1448
        // Drawing the groupbox around the preview image.
 
1449
        DrawEdge(ready_wnd_dc, &edge_rect, BDR_SUNKENINNER | BDR_RAISEDOUTER, BF_RECT | BF_FLAT);
 
1450
        DrawText(ready_wnd_dc, gettext("Preview"), -1, &text_rect, DT_LEFT | DT_VCENTER);
 
1451
 
 
1452
        DeleteDC(ready_wnd_dc);
 
1453
        ReleaseDC(ready_wnd, ready_wnd_dc);
 
1454
    }
 
1455
        break;
 
1456
    case WM_CREATE:
 
1457
        preview = CreateWindowEx(
 
1458
                0,
 
1459
                "STATIC",
 
1460
                "",
 
1461
                WS_CHILD | WS_VISIBLE | SS_BITMAP | SS_NOTIFY | WS_BORDER | WS_CLIPSIBLINGS,
 
1462
                IMAGE_PREVIEW_X,
 
1463
                IMAGE_PREVIEW_Y,
 
1464
                IMAGE_PREVIEW_WIDTH,
 
1465
                IMAGE_PREVIEW_HEIGHT,
 
1466
                ready_wnd,
 
1467
                (HMENU) ID_IMAGE_PREVIEW,
 
1468
                g_ItmagesInst,
 
1469
                NULL);
 
1470
        links_list = CreateWindowEx(
 
1471
                0,
 
1472
                "COMBOBOX",
 
1473
                NULL,
 
1474
                WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_TABSTOP | WS_CLIPSIBLINGS,
 
1475
                LINK_LIST_X,
 
1476
                LINK_LIST_Y,
 
1477
                LINK_LIST_WIDTH,
 
1478
                LINK_LIST_HEIGHT,
 
1479
                ready_wnd,
 
1480
                (HMENU) ID_LINKS_LIST,
 
1481
                g_ItmagesInst,
 
1482
                NULL);
 
1483
        link_preview = CreateWindowEx(
 
1484
                WS_EX_CLIENTEDGE,
 
1485
                "EDIT",
 
1486
                NULL,
 
1487
                WS_CHILD | WS_VISIBLE | ES_READONLY | ES_AUTOHSCROLL | WS_TABSTOP | WS_CLIPSIBLINGS,
 
1488
                LINK_PREVIEW_X,
 
1489
                LINK_PREVIEW_Y,
 
1490
                LINK_PREVIEW_WIDTH,
 
1491
                LINK_PREVIEW_HEIGHT,
 
1492
                ready_wnd,
 
1493
                (HMENU) ID_LINK_PREVIEW,
 
1494
                g_ItmagesInst,
 
1495
                NULL);
 
1496
        copy_to_clip = CreateWindowEx(
 
1497
                0,
 
1498
                "BUTTON",
 
1499
                gettext("Copy to Clipboard"),
 
1500
                WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPSIBLINGS,
 
1501
                COPYTOCLIPBOARD_X,
 
1502
                COPYTOCLIPBOARD_Y,
 
1503
                COPYTOCLIPBOARD_WIDTH,
 
1504
                COPYTOCLIPBOARD_HEIGHT,
 
1505
                ready_wnd,
 
1506
                (HMENU) ID_COPYTOCLIPBOARD,
 
1507
                g_ItmagesInst,
 
1508
                NULL);
 
1509
        open_in_bsr = CreateWindowEx(
 
1510
                0,
 
1511
                "BUTTON",
 
1512
                gettext("Open in Browser"),
 
1513
                WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPSIBLINGS,
 
1514
                OPENINBROWSER_X,
 
1515
                OPENINBROWSER_Y,
 
1516
                OPENINBROWSER_WIDTH,
 
1517
                OPENINBROWSER_HEIGHT,
 
1518
                ready_wnd,
 
1519
                (HMENU) ID_OPENINBROWSER,
 
1520
                g_ItmagesInst,
 
1521
                NULL);
 
1522
 
 
1523
        CreateTooltip(
 
1524
                links_list,
 
1525
                links_list_tool,
 
1526
                gettext("Choose the link type"));
 
1527
        CreateTooltip(
 
1528
                copy_to_clip,
 
1529
                clip_tool,
 
1530
                gettext("Copy the selected link into clipboard"));
 
1531
        CreateTooltip(
 
1532
                open_in_bsr,
 
1533
                bsr_tool,
 
1534
                gettext("Open the selected link in the default web browser"));
 
1535
 
 
1536
        // Putting information about the image inside the preview's tooltip.
 
1537
        wchar_t *fullname_wide = Utf8ToWide(imageInfo.fullname);
 
1538
        char *fullname_mb = WideToAcp(fullname_wide);
 
1539
        free(fullname_wide);
 
1540
 
 
1541
        sprintf(
 
1542
                image_info,
 
1543
                gettext("Name: %s\nWidth: %d\nHeight: %d\nSize: %d Kb"),
 
1544
                fullname_mb,
 
1545
                imageInfo.width,
 
1546
                imageInfo.height,
 
1547
                (imageInfo.size / 1024));
 
1548
        free(fullname_mb);
 
1549
 
 
1550
        CreateTooltip(
 
1551
                preview,
 
1552
                preview_tool,
 
1553
                image_info);
 
1554
 
 
1555
        // Setting the system's default font for the child windows.
 
1556
        SetSystemFont(ready_wnd, ID_IMAGE_PREVIEW, ID_OPENINBROWSER);
 
1557
 
 
1558
        // Adding the supported link types to the list.
 
1559
        SendMessage(links_list, CB_ADDSTRING, 0, (LPARAM) gettext("Link"));
 
1560
        SendMessage(links_list, CB_ADDSTRING, 0, (LPARAM) gettext("Direct Link"));
 
1561
        SendMessage(links_list, CB_ADDSTRING, 0, (LPARAM) gettext("BB code thumbnail"));
 
1562
        SendMessage(links_list, CB_ADDSTRING, 0, (LPARAM) gettext("HTML Code thumbnail"));
 
1563
 
 
1564
        char *config_path = WideToAcp(configFiles.config);
 
1565
 
 
1566
        char index_buffer[MAX_INDEX];
 
1567
        memset(index_buffer, 0, MAX_INDEX);
 
1568
 
 
1569
        // Retrieving an index of the previously chosen link type from the configuration file.
 
1570
        if (GetPrivateProfileString("Settings", "linkindex", NULL, index_buffer, MAX_INDEX + 1, config_path) == 0) {
 
1571
            SendMessage(links_list, CB_SETCURSEL, 0, 0);
 
1572
            ShowSelectedLink(link_preview, open_in_bsr, preview_buffer, 0);
 
1573
        } else {
 
1574
            size_t selected_index = atoi(index_buffer);
 
1575
            SendMessage(links_list, CB_SETCURSEL, selected_index, 0);
 
1576
            ShowSelectedLink(link_preview, open_in_bsr, preview_buffer, selected_index);
 
1577
        }
 
1578
        free(config_path);
 
1579
        break;
 
1580
    case WM_COMMAND:
 
1581
        if (LOWORD(wParam) == ID_LINKS_LIST && HIWORD(wParam) == CBN_SELCHANGE) {
 
1582
            size_t selected_index = SendMessage(links_list, CB_GETCURSEL, 0, 0);
 
1583
            ShowSelectedLink(link_preview, open_in_bsr, preview_buffer, selected_index);
 
1584
        }
 
1585
        if (LOWORD(wParam) == ID_COPYTOCLIPBOARD) {
 
1586
            if (OpenClipboard(ready_wnd) == 0) {
 
1587
                ErrorsHandler(ERROR_OPENCLIPBOARD);
 
1588
            } else {
 
1589
                HGLOBAL clip_buffer;
 
1590
                char *tmp_buffer;
 
1591
 
 
1592
                EmptyClipboard();
 
1593
                clip_buffer = GlobalAlloc(GMEM_DDESHARE, sizeof (preview_buffer));
 
1594
                tmp_buffer = (char*) GlobalLock(clip_buffer);
 
1595
                memcpy(tmp_buffer, preview_buffer, strlen(preview_buffer) + 1);
 
1596
                GlobalUnlock(clip_buffer);
 
1597
                SetClipboardData(CF_TEXT, clip_buffer);
 
1598
                CloseClipboard();
 
1599
            }
 
1600
        }
 
1601
        if (LOWORD(wParam) == ID_OPENINBROWSER) {
 
1602
            size_t selected_index = SendMessage(links_list, CB_GETCURSEL, 0, 0);
 
1603
            if (selected_index == 0 || selected_index == 1) {
 
1604
                ShellExecute(NULL, "open", preview_buffer, NULL, NULL, SW_SHOWNORMAL);
 
1605
            }
 
1606
        }
 
1607
        break;
 
1608
    case WM_DESTROY:
 
1609
    {
 
1610
        char index_buffer[MAX_INDEX];
 
1611
        memset(index_buffer, 0, MAX_INDEX);
 
1612
 
 
1613
        size_t selected_index = SendMessage(links_list, CB_GETCURSEL, 0, 0);
 
1614
        itoa(selected_index, index_buffer, 10);
 
1615
 
 
1616
        char *config_path = WideToAcp(configFiles.config);
 
1617
 
 
1618
        // Saving the last chosen link type index to the configuration file.
 
1619
        if (WritePrivateProfileString("Settings", "linkindex", index_buffer, config_path) == 0) {
 
1620
            ErrorsHandler(ERROR_WRITECONFFILE);
 
1621
        }
 
1622
        free(config_path);
 
1623
 
 
1624
        DeleteObject(previewImage);
 
1625
 
 
1626
        FreeImageInfo(&imageInfo);
 
1627
        FreeImageLinks(&imageLinks);
 
1628
        FreeImageData(&imageData);
 
1629
        FreeUserInfo(&userInfo);
 
1630
        FreeConfig(&configFiles);
 
1631
 
 
1632
        EnableWindow(login_wnd, 1);
 
1633
        SetFocus(login_wnd);
 
1634
        
 
1635
        PostQuitMessage(0);
 
1636
    }
 
1637
        break;
 
1638
    default:
 
1639
        return DefWindowProc(ready_wnd, message, wParam, lParam);
 
1640
    }
 
1641
    return 0;
 
1642
}
 
1643
 
 
1644
UINT APIENTRY UploadWithLoginThread(HWND login_wnd)
 
1645
{
 
1646
    short thread_success = 1;
 
1647
    short result = 0;
 
1648
 
 
1649
    char *cookies_path = WideToAcp(configFiles.cookies);
 
1650
 
 
1651
    result = ItmagesCheckConnection(cookies_path, userInfo.address, userInfo.port);
 
1652
    if (result == 1) {
 
1653
        ErrorsHandler(ERROR_CONNECTION);
 
1654
        thread_success = 0;
 
1655
    } else if (result == -1) {
 
1656
        thread_success = 0;
 
1657
    } else {
 
1658
        result = ItmagesLogin(userInfo.login, userInfo.passwd, cookies_path,
 
1659
                userInfo.address, userInfo.port);
 
1660
        if (result == 1) {
 
1661
            ErrorsHandler(ERROR_LOGIN);
 
1662
            thread_success = 0;
 
1663
        } else if (result == -1) {
 
1664
            thread_success = 0;
 
1665
        } else {
 
1666
            FreeImageLinks(&imageLinks);
 
1667
            HWND upload_progress = GetDlgItem(login_wnd, ID_UPLOAD_PROGRESSBAR);
 
1668
            result = ItmagesUpload(upload_progress, imageData.path, imageInfo.fullname, imageInfo.mime,
 
1669
                    cookies_path, userInfo.address, userInfo.port, imageInfo.size, &imageLinks);
 
1670
            if (result != 0) {
 
1671
                thread_success = 0;
 
1672
            }
 
1673
            result = ItmagesLogout(cookies_path, userInfo.address, userInfo.port);
 
1674
            if (result == 1) {
 
1675
                ErrorsHandler(ERROR_LOGOUT);
 
1676
            }
 
1677
        }
 
1678
    }
 
1679
    free(cookies_path);
 
1680
 
 
1681
    if (thread_success == 1) {
 
1682
        SendMessage(login_wnd, WM_FINISHTHREAD, 0, 0);
 
1683
    }
 
1684
    HWND upload = GetDlgItem(login_wnd, ID_UPLOAD_BUTTON);
 
1685
    EnableWindow(upload, 1);
 
1686
    return (DWORD) 0;
 
1687
}
 
1688
 
 
1689
UINT APIENTRY UploadWithoutLoginThread(HWND login_wnd)
 
1690
{
 
1691
    short thread_success = 1;
 
1692
    short result = 0;
 
1693
 
 
1694
    char *cookies_path = WideToAcp(configFiles.cookies);
 
1695
 
 
1696
    result = ItmagesCheckConnection(cookies_path, userInfo.address, userInfo.port);
 
1697
    if (result == 1) {
 
1698
        ErrorsHandler(ERROR_CONNECTION);
 
1699
        thread_success = 0;
 
1700
    } else if (result == -1) {
 
1701
        thread_success = 0;
 
1702
    } else {
 
1703
        FreeImageLinks(&imageLinks);
 
1704
        HWND upload_progress = GetDlgItem(login_wnd, ID_UPLOAD_PROGRESSBAR);
 
1705
        result = ItmagesUpload(upload_progress, imageData.path, imageInfo.fullname, imageInfo.mime,
 
1706
                cookies_path, userInfo.address, userInfo.port, imageInfo.size, &imageLinks);
 
1707
        if (result != 0) {
 
1708
            thread_success = 0;
 
1709
        }
 
1710
    }
 
1711
    free(cookies_path);
 
1712
 
 
1713
    if (thread_success == 1) {
 
1714
        SendMessage(login_wnd, WM_FINISHTHREAD, 0, 0);
 
1715
    }
 
1716
    HWND upload = GetDlgItem(login_wnd, ID_UPLOAD_BUTTON);
 
1717
    EnableWindow(upload, 1);
 
1718
    return (DWORD) 0;
 
1719
}
 
1720
 
 
1721
void CreateTooltip(
 
1722
        HWND parent_wnd,
 
1723
        HWND tooltip_wnd,
 
1724
        char *tooltip_txt)
 
1725
{
 
1726
    tooltip_wnd = CreateWindowEx(
 
1727
            WS_EX_TOPMOST,
 
1728
            TOOLTIPS_CLASS,
 
1729
            NULL,
 
1730
            TTS_NOPREFIX | TTS_ALWAYSTIP,
 
1731
            CW_USEDEFAULT,
 
1732
            CW_USEDEFAULT,
 
1733
            CW_USEDEFAULT,
 
1734
            CW_USEDEFAULT,
 
1735
            parent_wnd,
 
1736
            NULL,
 
1737
            g_ItmagesInst,
 
1738
            NULL);
 
1739
 
 
1740
    SetWindowPos(
 
1741
            tooltip_wnd,
 
1742
            HWND_TOPMOST,
 
1743
            0, 0, 0, 0,
 
1744
            SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
 
1745
 
 
1746
    TOOLINFO ti;
 
1747
    memset(&ti, 0, sizeof (ti));
 
1748
 
 
1749
    ti.cbSize = sizeof (TOOLINFO);
 
1750
    ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS | TTF_TRANSPARENT;
 
1751
    ti.hwnd = parent_wnd;
 
1752
    ti.uId = (UINT_PTR) parent_wnd;
 
1753
    ti.hinst = g_ItmagesInst;
 
1754
    ti.lpszText = tooltip_txt;
 
1755
 
 
1756
    GetClientRect(parent_wnd, &ti.rect);
 
1757
 
 
1758
    SendMessage(tooltip_wnd, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) & ti);
 
1759
    SendMessage(tooltip_wnd, TTM_ACTIVATE, TRUE, 0);
 
1760
    SendMessage(tooltip_wnd, TTM_SETMAXTIPWIDTH, 0, (WPARAM) 180);
 
1761
}
 
1762
 
 
1763
char *CreateOpenImageDialog()
 
1764
{
 
1765
    OPENFILENAME ofn;
 
1766
    memset(&ofn, 0, sizeof (OPENFILENAME));
 
1767
 
 
1768
    char *image_path = (char*) calloc(_MAX_PATH + 1, sizeof (char));
 
1769
    if (image_path == NULL) {
 
1770
        ErrorsHandler(ERROR_MEMALLOC);
 
1771
        exit(EXIT_FAILURE);
 
1772
    }
 
1773
 
 
1774
    ofn.lStructSize = sizeof (OPENFILENAME);
 
1775
    ofn.lpstrFile = image_path;
 
1776
    ofn.nMaxFile = _MAX_PATH;
 
1777
    ofn.lpstrFilter = "*.png,*.jpeg,*.jpg,*.gif\0*.jpg;*.jpeg;*.png;*.gif\0";
 
1778
    ofn.nFilterIndex = 1;
 
1779
    ofn.lpstrTitle = gettext("Choose the image...");
 
1780
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
 
1781
    if (GetOpenFileName(&ofn) == FALSE) {
 
1782
        exit(EXIT_SUCCESS);
 
1783
    }
 
1784
    return image_path;
 
1785
}
 
1786
 
 
1787
void ShowSelectedLink(
 
1788
        HWND link_preview,
 
1789
        HWND bsr_button,
 
1790
        char *preview_buffer,
 
1791
        size_t index)
 
1792
{
 
1793
    switch (index) {
 
1794
    case 0:
 
1795
        sprintf(
 
1796
                preview_buffer,
 
1797
                "http://itmages.ru/image/view/%s/%s",
 
1798
                imageLinks.id,
 
1799
                imageLinks.key);
 
1800
        SetWindowText(link_preview, preview_buffer);
 
1801
        EnableWindow(bsr_button, 1);
 
1802
        break;
 
1803
    case 1:
 
1804
        sprintf(
 
1805
                preview_buffer,
 
1806
                "http://%s.static.itmages.ru/%s",
 
1807
                imageLinks.server,
 
1808
                imageLinks.full);
 
1809
        SetWindowText(link_preview, preview_buffer);
 
1810
        EnableWindow(bsr_button, 1);
 
1811
        break;
 
1812
    case 2:
 
1813
        sprintf(
 
1814
                preview_buffer,
 
1815
                "[url=http://itmages.ru/image/view/%s/%s][img]http://%s.static.itmages.ru/%s[/img][/url]",
 
1816
                imageLinks.id,
 
1817
                imageLinks.key,
 
1818
                imageLinks.server,
 
1819
                imageLinks.thumb);
 
1820
        SetWindowText(link_preview, preview_buffer);
 
1821
        EnableWindow(bsr_button, 0);
 
1822
        break;
 
1823
    case 3:
 
1824
        sprintf(
 
1825
                preview_buffer,
 
1826
                "<a target=\"_blank\"; href=\"http://itmages.ru/image/view/%s/%s\"><img src=\"http://%s.static.itmages.ru/%s\" /></a>",
 
1827
                imageLinks.id,
 
1828
                imageLinks.key,
 
1829
                imageLinks.server,
 
1830
                imageLinks.thumb);
 
1831
        SetWindowText(link_preview, preview_buffer);
 
1832
        EnableWindow(bsr_button, 0);
 
1833
        break;
 
1834
    }
 
1835
}
 
1836
 
 
1837
void UpdateBackgroundBrush(HWND wnd)
 
1838
{
 
1839
    if (IsAppThemed() == TRUE) {
 
1840
        g_ThemeActive = 1;
 
1841
    } else {
 
1842
        g_ThemeActive = 0;
 
1843
    }
 
1844
 
 
1845
    if (g_StaticBrush != NULL) {
 
1846
        DeleteObject(g_StaticBrush);
 
1847
    }
 
1848
 
 
1849
    g_StaticBrush = NULL;
 
1850
 
 
1851
    if (g_ThemeActive == 1) {
 
1852
        RECT wnd_rect;
 
1853
 
 
1854
        GetWindowRect(wnd, &wnd_rect);
 
1855
 
 
1856
        HDC wnd_dc = GetDC(wnd);
 
1857
        HDC wnd_dc_mem = CreateCompatibleDC(wnd_dc);
 
1858
 
 
1859
        HBITMAP bitmap = CreateCompatibleBitmap(wnd_dc,
 
1860
                wnd_rect.right - wnd_rect.left, wnd_rect.bottom - wnd_rect.top);
 
1861
        HBITMAP bitmap_old = (HBITMAP) SelectObject(wnd_dc_mem, bitmap);
 
1862
 
 
1863
        SendMessage(wnd, WM_PRINTCLIENT, (WPARAM) (wnd_dc_mem),
 
1864
                (LPARAM) (PRF_ERASEBKGND | PRF_CLIENT | PRF_NONCLIENT));
 
1865
 
 
1866
        g_StaticBrush = CreatePatternBrush(bitmap);
 
1867
 
 
1868
        SelectObject(wnd_dc_mem, bitmap_old);
 
1869
 
 
1870
        DeleteObject(bitmap);
 
1871
        DeleteDC(wnd_dc_mem);
 
1872
        ReleaseDC(wnd, wnd_dc);
 
1873
    }
 
1874
}
 
1875
 
 
1876
void SetThemedBackground(
 
1877
        HWND wnd,
 
1878
        HWND static_wnd,
 
1879
        HDC static_hdc)
 
1880
{
 
1881
    RECT wnd_rect;
 
1882
 
 
1883
    GetWindowRect(static_wnd, &wnd_rect);
 
1884
    SetBkMode(static_hdc, TRANSPARENT);
 
1885
    MapWindowPoints(NULL, wnd, (LPPOINT) (&wnd_rect), 2);
 
1886
    SetBrushOrgEx(static_hdc, -wnd_rect.left, -wnd_rect.top, NULL);
 
1887
}
 
1888
 
 
1889
void SetSystemFont(
 
1890
        HWND parent_wnd,
 
1891
        ItmagesWindowIds first_wnd,
 
1892
        ItmagesWindowIds last_wnd)
 
1893
{
 
1894
    size_t i;
 
1895
 
 
1896
    for (i = first_wnd; i <= last_wnd; ++i) {
 
1897
        SendDlgItemMessage(parent_wnd, i, WM_SETFONT, (WPARAM) g_SystemFont, 0);
 
1898
    }
 
1899
}
 
1900
 
 
1901
void HideWindows(
 
1902
        HWND parent_wnd,
 
1903
        ItmagesWindowIds first_wnd,
 
1904
        ItmagesWindowIds last_wnd)
 
1905
{
 
1906
    size_t i;
 
1907
    HWND hide_wnd;
 
1908
 
 
1909
    for (i = first_wnd; i <= last_wnd; ++i) {
 
1910
        hide_wnd = GetDlgItem(parent_wnd, i);
 
1911
        ShowWindow(hide_wnd, SW_HIDE);
 
1912
    }
 
1913
}
 
1914
 
 
1915
void ShowTabWindows(
 
1916
        HWND parent_wnd,
 
1917
        ItmagesWindowIds first_wnd,
 
1918
        ItmagesWindowIds last_wnd,
 
1919
        ItmagesWindowIds first_show,
 
1920
        ItmagesWindowIds last_show)
 
1921
{
 
1922
    size_t i;
 
1923
    HWND tab_wnds;
 
1924
 
 
1925
    for (i = first_wnd; i <= last_wnd; ++i) {
 
1926
        tab_wnds = GetDlgItem(parent_wnd, i);
 
1927
        if (i > first_show - 1 && i < last_show + 1) {
 
1928
            ShowWindow(tab_wnds, SW_SHOW);
 
1929
        } else {
 
1930
            ShowWindow(tab_wnds, SW_HIDE);
 
1931
        }
 
1932
    }
 
1933
}
 
1934
 
 
1935
void GetConfigFiles(ItmagesConfig *configFiles)
 
1936
{
 
1937
    size_t folder_len = wcslen(ITMAGES_DIR);
 
1938
    size_t config_len = wcslen(CONFIG_FILE);
 
1939
    size_t cookies_len = wcslen(COOKIES_FILE);
 
1940
 
 
1941
    configFiles->config = (wchar_t*) calloc(_MAX_PATH + 1, sizeof (wchar_t));
 
1942
    if (configFiles->config == NULL) {
 
1943
        ErrorsHandler(ERROR_MEMALLOC);
 
1944
        exit(EXIT_FAILURE);
 
1945
    }
 
1946
    if (SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, configFiles->config) != S_OK) {
 
1947
        ErrorsHandler(ERROR_GETSETTINGSDIR);
 
1948
    } else {
 
1949
        size_t path_len = wcslen(configFiles->config);
 
1950
        wmemcpy(configFiles->config + path_len, ITMAGES_DIR, folder_len + 1);
 
1951
        CreateDirectoryW(configFiles->config, NULL);
 
1952
        wmemcpy(configFiles->config + path_len + folder_len, CONFIG_FILE, config_len + 1);
 
1953
    }
 
1954
    configFiles->cookies = (wchar_t*) calloc(_MAX_PATH + 1, sizeof (wchar_t));
 
1955
    if (configFiles->cookies == NULL) {
 
1956
        ErrorsHandler(ERROR_MEMALLOC);
 
1957
        exit(EXIT_FAILURE);
 
1958
    }
 
1959
    if (SHGetFolderPathW(NULL, CSIDL_COOKIES | CSIDL_FLAG_CREATE, NULL, 0, configFiles->cookies) != S_OK) {
 
1960
        ErrorsHandler(ERROR_GETCOOKIESDIR);
 
1961
    } else {
 
1962
        size_t path_len = wcslen(configFiles->cookies);
 
1963
        wmemcpy(configFiles->cookies + path_len, ITMAGES_DIR, folder_len + 1);
 
1964
        CreateDirectoryW(configFiles->cookies, NULL);
 
1965
        wmemcpy(configFiles->cookies + path_len + folder_len, COOKIES_FILE, cookies_len + 1);
 
1966
    }
 
1967
}
 
1968
 
 
1969
char *GetInstallDir()
 
1970
{
 
1971
    HKEY key;
 
1972
    DWORD type = REG_SZ;
 
1973
    DWORD size = _MAX_PATH;
 
1974
 
 
1975
    char *install_dir = (char*) calloc(_MAX_PATH + 1, sizeof (char));
 
1976
    if (install_dir == NULL) {
 
1977
        ErrorsHandler(ERROR_MEMALLOC);
 
1978
        exit(EXIT_FAILURE);
 
1979
    }
 
1980
    if (RegOpenKeyEx(HKEY_CURRENT_USER, ITMAGES_REG_KEY, 0L, KEY_ALL_ACCESS, &key) == ERROR_SUCCESS) {
 
1981
        if (RegQueryValueEx(key, "", NULL, &type, (LPBYTE) install_dir, &size) != ERROR_SUCCESS) {
 
1982
            return NULL;
 
1983
        }
 
1984
    } else {
 
1985
        return NULL;
 
1986
    }
 
1987
    RegCloseKey(key);
 
1988
    return install_dir;
 
1989
}
 
1990
 
 
1991
char *GetLocaleDir()
 
1992
{
 
1993
    char *locale_dir = GetInstallDir();
 
1994
    if (locale_dir != NULL) {
 
1995
        memcpy(locale_dir + strlen(locale_dir), LOCALE_DIR, strlen(LOCALE_DIR) + 1);
 
1996
    }
 
1997
    return locale_dir;
 
1998
}
 
1999
 
 
2000
int LoadDocFile(
 
2001
        HWND load_wnd,
 
2002
        char *docs_dir,
 
2003
        char *load_file)
 
2004
{
 
2005
    char *doc_file = GetInstallDir();
 
2006
    if (doc_file == NULL) {
 
2007
                return -1;
 
2008
        }
 
2009
        
 
2010
    size_t docs_len = strlen(DOCS_DIR);
 
2011
    size_t load_len = strlen(load_file);
 
2012
 
 
2013
    char *buffer = (char*) malloc((docs_len + load_len + 1) * sizeof (char));
 
2014
    if (buffer == NULL) {
 
2015
        ErrorsHandler(ERROR_MEMALLOC);
 
2016
        exit(EXIT_FAILURE);
 
2017
    }
 
2018
    memcpy(buffer, docs_dir, docs_len + 1);
 
2019
    memcpy(buffer + strlen(buffer), load_file, load_len + 1);
 
2020
    memcpy(doc_file + strlen(doc_file), buffer, docs_len + load_len + 1);
 
2021
    free(buffer);
 
2022
 
 
2023
    wchar_t * doc_file_wide = AcpToWide(doc_file);
 
2024
    free(doc_file);
 
2025
 
 
2026
    FILE *file;
 
2027
    long file_len;
 
2028
 
 
2029
    file = _wfopen(doc_file_wide, L"rb");
 
2030
    if (file == NULL) {
 
2031
                free(doc_file_wide);
 
2032
                return -1;
 
2033
        }
 
2034
    
 
2035
        fseek(file, 0, SEEK_END);
 
2036
    file_len = ftell(file);
 
2037
    rewind(file);
 
2038
 
 
2039
    char *text = (char*) malloc((file_len + 1) * sizeof (char));
 
2040
    if (text == NULL) {
 
2041
        ErrorsHandler(ERROR_MEMALLOC);
 
2042
        exit(EXIT_FAILURE);
 
2043
    }
 
2044
    fread(text, 1, file_len, file);
 
2045
    free(doc_file_wide);
 
2046
    fclose(file);
 
2047
 
 
2048
    SetWindowText(load_wnd, text);
 
2049
    free(text);
 
2050
        return 0;
 
2051
}
 
2052
 
 
2053
void FreeUserInfo(ItmagesUserInfo *userInfo)
 
2054
{
 
2055
    if (userInfo->login != NULL) {
 
2056
        free(userInfo->login);
 
2057
        userInfo->login = NULL;
 
2058
    }
 
2059
    if (userInfo->passwd != NULL) {
 
2060
        free(userInfo->passwd);
 
2061
        userInfo->passwd = NULL;
 
2062
    }
 
2063
    if (userInfo->address != NULL) {
 
2064
        free(userInfo->address);
 
2065
        userInfo->address = NULL;
 
2066
    }
 
2067
    if (userInfo->port != NULL) {
 
2068
        free(userInfo->port);
 
2069
        userInfo->port = NULL;
 
2070
    }
 
2071
}
 
2072
 
 
2073
void FreeConfig(ItmagesConfig *configFiles)
 
2074
{
 
2075
    if (configFiles->config != NULL) {
 
2076
        free(configFiles->config);
 
2077
        configFiles->config = NULL;
 
2078
    }
 
2079
    if (configFiles->cookies != NULL) {
 
2080
        free(configFiles->cookies);
 
2081
        configFiles->cookies = NULL;
 
2082
    }
 
2083
}
 
2084
 
 
2085
void FreeImageData(ItmagesImageData *imageData)
 
2086
{
 
2087
    if (imageData->path != NULL) {
 
2088
        free(imageData->path);
 
2089
        imageData->path = NULL;
 
2090
    }
 
2091
    if (imageData->basename != NULL) {
 
2092
        free(imageData->basename);
 
2093
        imageData->basename = NULL;
 
2094
    }
 
2095
    if (imageData->ext != NULL) {
 
2096
        free(imageData->ext);
 
2097
        imageData->ext = NULL;
 
2098
    }
 
2099
}
 
2100
 
 
2101
 
 
2102
 
 
2103
 
 
2104
 
 
2105
 
 
2106
 
 
2107
 
 
2108
 
 
2109
 
 
2110
 
 
2111
 
 
2112
 
 
2113
 
 
2114
 
 
2115
 
 
2116
 
 
2117
 
 
2118
 
 
2119
 
 
2120
 
 
2121
 
 
2122
 
 
2123
 
 
2124
 
 
2125
 
 
2126
 
 
2127
 
 
2128
 
 
2129