summaryrefslogblamecommitdiffstats
path: root/private/sdktools/imagedit/file.c
blob: 2c51d0b01c4595583b1b2dd6ba9788f65aa1046a (plain) (tree)
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834

































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                              
/****************************************************************************/
/*                                                                          */
/*                         Microsoft Confidential                           */
/*                                                                          */
/*                 Copyright (c) Microsoft Corp.  1987, 1991                */
/*                           All Rights Reserved                            */
/*                                                                          */
/****************************************************************************/
/****************************** Module Header *******************************
* Module Name: file.c
*
* Contains routines for handling files.
*
* History:
*
****************************************************************************/

#include "imagedit.h"
#include "dialogs.h"

#include <string.h>

#include <commdlg.h>


#ifdef WIN16
typedef BOOL (APIENTRY *LPOFNHOOKPROC) (HWND, UINT, WPARAM, LONG);
#endif


STATICFN VOID NEAR AddFilterString(PSTR pszBuf, PSTR pszType, PSTR pszExt,
    BOOL fFirst);
STATICFN PCSTR NEAR DefExtFromFilter(INT index, PCSTR pszFilter);
STATICFN BOOL NEAR LoadFile(PSTR pszFullFileName);
STATICFN INT NEAR GetTypeFromExt(PSTR pszFileName);
STATICFN VOID NEAR FileCat(PSTR pchName, PSTR pchCat);


static OPENFILENAME ofn;



/************************************************************************
* SetFileName
*
* Updates the globals that contain the file name of the currently
* loaded file.  This routine will also cause the title bar to
* be udpated with the new name.
*
* Arguments:
*
* History:
*
************************************************************************/

VOID SetFileName(
    PSTR pszFullFileName)
{
    CHAR szTitle[CCHMAXPATH];
    WIN32_FIND_DATA ffbuf;
    CHAR *pch;

    if (pszFullFileName) {
        HANDLE hfind;
        strcpy(gszFullFileName, pszFullFileName);
        gpszFileName = FileInPath(gszFullFileName);

        if((hfind = FindFirstFile( pszFullFileName, &ffbuf)) !=
                INVALID_HANDLE_VALUE) {

            strcpy(gpszFileName, ffbuf.cFileName);
            FindClose(hfind);
        }

    }
    else {
        *gszFullFileName = '\0';
        gpszFileName = NULL;
    }

    strcpy(szTitle, ids(IDS_PGMTITLE));
    strcat(szTitle, " - ");
    pch = gpszFileName ? gpszFileName : ids(IDS_UNTITLED);
    strncat(szTitle, pch, sizeof(szTitle) - strlen(szTitle));
    szTitle[CCHMAXPATH-1] = '\0';
    SetWindowText(ghwndMain, szTitle);
}



/************************************************************************
* FileInPath
*
* This function takes a path and returns a pointer to the file name
* portion of it.  For instance, it will return a pointer to
* "abc.res" if it is given the following path: "c:\windows\abc.res".
*
* Arguments:
*   PSTR pstrPath - Path to look through.
*
* History:
*
************************************************************************/

PSTR FileInPath(
    PSTR pstrPath)
{
    PSTR pstr;

    pstr = pstrPath + strlen(pstrPath);
    while (pstr > pstrPath) {
        pstr = FAR2NEAR(AnsiPrev(pstrPath, pstr));
        if (*pstr == '\\' || *pstr == ':' || *pstr == '/') {
            pstr = FAR2NEAR(AnsiNext(pstr));
            break;
        }
    }

    return pstr;
}



/************************************************************************
* ClearResource
*
* Resets the editor back to a neutral state before editing any image.
* This function can be called before starting to edit a new file
* (but not just a new image).  Files should be saved before calling
* this routine, because the entire image list is destroyed.
*
* History:
*
************************************************************************/

VOID ClearResource(VOID)
{
    ImageLinkFreeList();

    SetFileName(NULL);

    gnImages = 0;
    fImageDirty = FALSE;
    fFileDirty = FALSE;
    gpImageCur = NULL;

    /*
     * Hide the workspace and view windows.
     */
    ShowWindow(ghwndWork, SW_HIDE);
    ViewShow(FALSE);

    /*
     * Destroy the image DC's.
     */
    ImageDCDelete();

    /*
     * Update the properties bar.
     */
    PropBarClearPos();
    PropBarClearSize();
    PropBarUpdate();
}



/************************************************************************
* OpenDlg
*
*
*
* Arguments:
*
* History:
*
************************************************************************/

BOOL OpenDlg(
    PSTR pszFileName,
    INT iType)
{
    BOOL fGotName;
    INT idDlg;
    INT idPrevDlg;
    CHAR szFilter[CCHTEXTMAX];

    pszFileName[0] = '\0';

    switch (iType) {
        case FT_BITMAP:
        case FT_ICON:
        case FT_CURSOR:
            AddFilterString(szFilter, ids(IDS_BMPFILTER),
                    ids(IDS_BMPFILTEREXT), TRUE);
            AddFilterString(szFilter, ids(IDS_ICOFILTER),
                    ids(IDS_ICOFILTEREXT), FALSE);
            AddFilterString(szFilter, ids(IDS_CURFILTER),
                    ids(IDS_CURFILTEREXT), FALSE);
            AddFilterString(szFilter, ids(IDS_ALLFILTER),
                    ids(IDS_ALLFILTEREXT), FALSE);

            ofn.nFilterIndex = iType + 1;
            idDlg = DID_COMMONFILEOPEN;

            break;

        case FT_PALETTE:
            AddFilterString(szFilter, ids(IDS_PALFILTER),
                    ids(IDS_PALFILTEREXT), TRUE);
            AddFilterString(szFilter, ids(IDS_ALLFILTER),
                    ids(IDS_ALLFILTEREXT), FALSE);

            ofn.nFilterIndex = 1;
            idDlg = DID_COMMONFILEOPENPAL;

            break;
    }

    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = ghwndMain;
    ofn.hInstance = NULL;
    ofn.lpstrFilter = szFilter;
    ofn.lpstrCustomFilter = NULL;
    ofn.nMaxCustFilter = 0;
    ofn.lpstrFile = pszFileName;
    ofn.nMaxFile = CCHMAXPATH;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.lpstrTitle = NULL;
    ofn.Flags = OFN_HIDEREADONLY | OFN_SHOWHELP | OFN_FILEMUSTEXIST |
            OFN_ENABLEHOOK;
    ofn.nFileOffset = 0;
    ofn.nFileExtension = 0;
    ofn.lpstrDefExt = (LPCSTR)DefExtFromFilter((INT)ofn.nFilterIndex - 1,
            FAR2NEAR(ofn.lpstrFilter));
    ofn.lCustData = 0;
    ofn.lpfnHook = (LPOFNHOOKPROC)MakeProcInstance(
            (FARPROC)GetOpenFileNameHook, ghInst);
    ofn.lpTemplateName = NULL;

    EnteringDialog(idDlg, &idPrevDlg, TRUE);
    fGotName = GetOpenFileName(&ofn);
    EnteringDialog(idPrevDlg, NULL, FALSE);

    FreeProcInstance((FARPROC)ofn.lpfnHook);

    return fGotName;
}



/************************************************************************
* SaveAsDlg
*
*
*
* Arguments:
*
* History:
*
************************************************************************/

BOOL SaveAsDlg(
    PSTR pszFileName,
    INT iType)
{
    INT idDlg;
    BOOL fGotName;
    INT idPrevDlg;
    CHAR szFilter[CCHTEXTMAX];

    switch (iType) {
        case FT_BITMAP:
            AddFilterString(szFilter, ids(IDS_BMPFILTER),
                    ids(IDS_BMPFILTEREXT), TRUE);

            ofn.lpstrDefExt = ids(IDS_DEFEXTBMP);
            idDlg = DID_COMMONFILESAVE;

            break;

        case FT_ICON:
            AddFilterString(szFilter, ids(IDS_ICOFILTER),
                    ids(IDS_ICOFILTEREXT), TRUE);

            ofn.lpstrDefExt = ids(IDS_DEFEXTICO);
            idDlg = DID_COMMONFILESAVE;

            break;

        case FT_CURSOR:
            AddFilterString(szFilter, ids(IDS_CURFILTER),
                    ids(IDS_CURFILTEREXT), TRUE);

            ofn.lpstrDefExt = ids(IDS_DEFEXTCUR);
            idDlg = DID_COMMONFILESAVE;

            break;

        case FT_PALETTE:
            AddFilterString(szFilter, ids(IDS_PALFILTER),
                    ids(IDS_PALFILTEREXT), TRUE);

            ofn.lpstrDefExt = ids(IDS_DEFEXTPAL);
            idDlg = DID_COMMONFILESAVEPAL;

            break;
    }

    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = ghwndMain;
    ofn.hInstance = NULL;
    ofn.lpstrFilter = szFilter;
    ofn.lpstrCustomFilter = NULL;
    ofn.nMaxCustFilter = 0;
    ofn.nFilterIndex = 1;
    ofn.lpstrFile = pszFileName;
    ofn.nMaxFile = CCHMAXPATH;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.lpstrTitle = NULL;
    ofn.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_SHOWHELP;
    ofn.nFileOffset = 0;
    ofn.nFileExtension = 0;
    ofn.lCustData = 0;
    ofn.lpfnHook = NULL;
    ofn.lpTemplateName = NULL;

    EnteringDialog(idDlg, &idPrevDlg, TRUE);
    fGotName = GetSaveFileName(&ofn);
    EnteringDialog(idPrevDlg, NULL, FALSE);

    return fGotName;
}



/************************************************************************
* GetOpenFileNameHook
*
* This function is the hook function for the Common Dialogs
* GetOpenFileName funtion.  It is used to be sure the default
* extension that is used when the function exits is the same
* as the image file type that the user specifies they want
* opened.
*
* Arguments:
*
* History:
*
************************************************************************/

DIALOGPROC GetOpenFileNameHook(
    HWND hwnd,
    UINT msg,
    WPARAM wParam,
    LONG lParam)
{
    switch (msg) {
        case WM_INITDIALOG:
            /*
             * Tell Windows to set the focus for me.
             */
            return TRUE;

        case WM_COMMAND:
            /*
             * Did they change the type of file from the File Type
             * combo box?
             */
            if (GET_WM_COMMAND_ID(wParam, lParam) == DID_COMMDLG_TYPECOMBO &&
                    GET_WM_COMMAND_CMD(wParam, lParam) == CBN_SELCHANGE) {
                INT iSelect;

                /*
                 * Get the selected file type, then change the default
                 * extension field of the ofn structure to match it.
                 * This ensures that the proper default extension
                 * gets added to the end of the file name if the user
                 * does not specify an extension explicitly.
                 */
                if ((iSelect = (INT)SendDlgItemMessage(hwnd,
                        DID_COMMDLG_TYPECOMBO, CB_GETCURSEL, 0, 0L))
                        != CB_ERR) {
                    ofn.lpstrDefExt = (LPCSTR)DefExtFromFilter(
                            iSelect, FAR2NEAR(ofn.lpstrFilter));
                }
            }

            break;
    }

    /*
     * Process the message normally.
     */
    return FALSE;
}



/************************************************************************
* AddFilterString
*
* This function adds a filter string pair to a filter string for
* use by the common dialog open/save file functions.  The string
* pair will be added to the end of the given filter string, unless
* fFirst is TRUE, in which case it will be written out to the
* start of the buffer.  A double null will always be written out
* at the end of the filter string.
*
* Arguments:
*   PSTR pszBuf  - Buffer to write to.
*   PSTR pszType - Type string.  Something like "Icon files (*.ico)".
*   PSTR pszExt  - Extension string.  Something like "*.ico".
*   BOOL fFirst  - TRUE if this is the first filter string in the
*                  buffer.  If FALSE, the new filter string pair will
*                  be added to the end of pszBuf.
*
* History:
*
************************************************************************/

STATICFN VOID NEAR AddFilterString(
    PSTR pszBuf,
    PSTR pszType,
    PSTR pszExt,
    BOOL fFirst)
{
    PSTR psz;

    psz = pszBuf;

    /*
     * If this is not the first filter string pair, skip to the
     * terminating double null sequence.
     */
    if (!fFirst) {
        while (*psz || *(psz + 1))
            psz++;

        psz++;
    }

    strcpy(psz, pszType);
    psz += strlen(pszType) + 1;
    strcpy(psz, pszExt);
    psz += strlen(pszExt) + 1;
    *psz = '\0';
}



/************************************************************************
* DefExtFromFilter
*
* This function returns the default extension for the given index
* from the specified filter string chain.  The filter string chain
* is in the format expected by the GetSaveFileName function.
*
* It will return NULL if the filter extension found is "*.*".
*
* Arguments:
*   INT index      - Zero based index to the filter string.
*   PSTR pszFilter - Pointer to the start of the filter chain.
*
* History:
*
************************************************************************/

STATICFN PCSTR NEAR DefExtFromFilter(
    INT index,
    PCSTR pszFilter)
{
    if (!pszFilter)
        return NULL;

    /*
     * Skip to the specified filter string pair.
     */
    while (index--) {
        pszFilter += strlen(pszFilter) + 1;
        pszFilter += strlen(pszFilter) + 1;
    }

    /*
     * Skip the first string, then skip the '*' and the '.'.
     */
    pszFilter += strlen(pszFilter) + 1 + 1 + 1;

    /*
     * If the string found was "*.*", return NULL for the default
     * extension.
     */
    if (*pszFilter == '*')
        return NULL;

    /*
     * Return a pointer to the default extension.  This will be
     * something like "bmp" or "ico".
     */
    return pszFilter;
}



/************************************************************************
* VerifySaveFile
*
* Prompts the user if they want to save the current file to disk.
* If Yes, calls the appropriate save routine.
*
* Returns:
*   Returns TRUE if either the file was not dirty (no save was done)
*   or if it was and the user did not want to save it or the file
*   was dirty and the user wanted to save it and the save was
*   successful.
*
*   Returns FALSE if the user cancelled the operation, or an error
*   occured with the save.
*
* History:
*
************************************************************************/

BOOL VerifySaveFile(VOID)
{
    if (fImageDirty || fFileDirty) {
        switch (Message(MSG_SAVEFILE,
                gpszFileName ? gpszFileName : ids(IDS_UNTITLED))) {
            case IDYES:
                return SaveFile(FALSE);

            case IDNO:
                fImageDirty = FALSE;
                break;

            case IDCANCEL:
                return FALSE;
        }
    }

    return TRUE;
}



/************************************************************************
* SaveFile
*
* Does a save of the current file.  If the file is untitled, it
* will ask the user for a file name.
*
* Arguments:
*   BOOL fSaveAs - TRUE to force a Save As operation (always prompts
*                  for the file name).
*
* Returns:
*   Returns TRUE if the save was successful.
*
*   Returns FALSE if the user cancelled the operation, or an error
*   occured with the save.
*
* History:
*
************************************************************************/

BOOL SaveFile(
    BOOL fSaveAs)
{
    CHAR szFileName[CCHMAXPATH];

    if (gnImages == 0) {
        Message(MSG_NOIMAGES);
        return FALSE;
    }

    if (gpszFileName)
        strcpy(szFileName, gszFullFileName);
    else
        *szFileName = '\0';

    if (fSaveAs || !gpszFileName) {
        if (!SaveAsDlg(szFileName, giType))
            return FALSE;
    }

    switch (giType) {
        case FT_BITMAP:
            return SaveBitmapFile(szFileName);

        case FT_ICON:
        case FT_CURSOR:
            return SaveIconCursorFile(szFileName, giType);
    }

    return FALSE;
}



/************************************************************************
* OpenAFile
*
* Prompts for a file name to open and then does the loading of it.
*
* History:
*
************************************************************************/

BOOL OpenAFile(VOID)
{
    CHAR szFileName[CCHMAXPATH];

    if (OpenDlg(szFileName, giType)) {
        /*
         * Clear out the current resource.
         */
        ClearResource();

        LoadFile(szFileName);

        return TRUE;
    }
    else {
        return FALSE;
    }
}



/************************************************************************
* LoadFile
*
* Loads the specified file for editing.
*
* Arguments:
*   PSTR pszFullFileName - Full path name to the file to load.
*
* History:
*
************************************************************************/

STATICFN BOOL NEAR LoadFile(
    PSTR pszFullFileName)
{
    switch (GetTypeFromExt(pszFullFileName)) {
        case FT_BITMAP:
            return LoadBitmapFile(pszFullFileName);

        case FT_ICON:
            return LoadIconCursorFile(pszFullFileName, TRUE);

        case FT_CURSOR:
            return LoadIconCursorFile(pszFullFileName, FALSE);
    }

    return FALSE;
}



/************************************************************************
* GetTypeFromExt
*
* Returns the type of file based on it's file name extension.
*
* Arguments:
*   PSTR pszFileName - File name to check.
*
* History:
*
************************************************************************/

STATICFN INT NEAR GetTypeFromExt(
    PSTR pszFileName)
{
    PSTR pszExt;

    pszExt = pszFileName + strlen(pszFileName) - 3;

    if (_strcmpi(pszExt, ids(IDS_DEFEXTICO)) == 0)
        return FT_ICON;
    else if (_strcmpi(pszExt, ids(IDS_DEFEXTCUR)) == 0)
        return FT_CURSOR;
    else
        return FT_BITMAP;
}



/************************************************************************
* OpenCmdLineFile
*
* Handles opening of the file specified on the command line.
*
* History:
* Nov 7, 1989   Byron Dazey - Created
*
************************************************************************/

VOID OpenCmdLineFile(
    PSTR pstrFileName)
{
    CHAR szFullPath[CCHMAXPATH];
    OFSTRUCT OfStruct;

    strcpy(szFullPath, pstrFileName);

    /*
     * If the file name does not already have an extension,
     * assume it is a bitmap file and add a .BMP extension
     * to it.
     */
    FileCat(szFullPath, ids(IDS_DOTBMP));

    if (MOpenFile(szFullPath, &OfStruct, OF_EXIST) == (HFILE)-1) {
        Message(MSG_CANTOPEN, pstrFileName);
    }
    else {
        LoadFile(OfStruct.szPathName);
    }
}



/************************************************************************
* FileCat
*
* This function checks for an extension on the give file name.
* If an extension is not found, the extension specified by
* pchCat is added to the file name.
*
* Arguments:
*     PSTR pch          = The file spec to "cat" the extension to.
*     PSTR pchCat       = The extension to "cat" on to pch,
*                         including the '.'
*
* History:
*
************************************************************************/

STATICFN VOID NEAR FileCat(
    PSTR pchName,
    PSTR pchCat)
{
    PSTR pch;

    pch = pchName + strlen(pchName);
    pch = FAR2NEAR(AnsiPrev(pchName, pch));

    /* back up to '.' or '\\' */
    while (*pch != '.') {
        if (*pch == '\\' || pch <= pchName) {
            /* no extension, add one */
            strcat(pchName, pchCat);
            return;
        }

        pch = FAR2NEAR(AnsiPrev(pchName, pch));
    }
}



/************************************************************************
* MyFileRead
*
*
*
* Arguments:
*
* History:
*
************************************************************************/

BOOL MyFileRead(
    HFILE hf,
    LPSTR lpBuffer,
    UINT nBytes,
    PSTR pszFileName,
    INT iType)
{
    register UINT cb;

    cb = M_lread(hf, lpBuffer, nBytes);

    if (cb == -1) {
        Message(MSG_READERROR, pszFileName);
        return FALSE;
    }
    else if (cb != nBytes) {
        Message((iType == FT_BITMAP) ? MSG_BADBMPFILE : MSG_BADICOCURFILE,
            pszFileName);
        return FALSE;
    }
    else {
        return TRUE;
    }
}



/************************************************************************
* MyFileWrite
*
*
*
* Arguments:
*
* History:
*
************************************************************************/

BOOL MyFileWrite(
    HFILE hf,
    LPSTR lpBuffer,
    UINT nBytes,
    PSTR pszFileName)
{
    register UINT cb;

    cb = M_lwrite(hf, lpBuffer, nBytes);

    if (cb == -1 || cb != nBytes) {
        Message(MSG_WRITEERROR, pszFileName);
        return FALSE;
    }
    else {
        return TRUE;
    }
}