summaryrefslogtreecommitdiff
path: root/src/T2OpenSelectDlg.cpp
blob: 00e6162a8c8990c056a72993f0c9bf109d55c099 (plain)
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
#include "StdAfx.h"
#include "Bitmap.h"
#include "T2DLL/GlobalFunc.h"
#include "T2.h"
#include "T2DLL/T2BitImage.h"
#include "T2DLL/T2DLL.h"
#include "T2DLL/T2ImageObj.h"
#include "T2OpenSelectDlg.h"
#include "T2TowerDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static CWnd *unknownWindow;

T2OpenSelectDlg::T2OpenSelectDlg(T2TowerDoc *inDoc) {
    mTowerDoc = inDoc;

#line 29
    mBitmap = new Bitmap;
    mBitmap->LoadFile("T2S.dat");

    mDeleteOnClose = true;
}

/*virtual*/ T2OpenSelectDlg::~T2OpenSelectDlg() {
    delete mBitmap;

    if (mImageObj)
        delete mImageObj;
    if (mImage)
        delete mImage;

    free(mPal1);
    free(mPal2);
}

void T2OpenSelectDlg::Setup() {
    CBitmap theBitmap;
    CBrush theBrush;

    HRSRC resource = FindResource(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(ID_OPEN_SELECT_PALETTE), "PALETTE");
    HGLOBAL theHandle = LoadResource(AfxGetApp()->m_hInstance, resource);
    void *resData = LockResource(theHandle);

    LOGPALETTE *theLogPalette = ConvACTPalette((ACTPALETTEENTRY *) resData);
    mPalette2.CreatePalette(theLogPalette);

    int i;

    mPal1 = (short *) malloc(sizeof(short) * 256);
    for (i = 0; i < 256; i++)
        mPal1[i] = i;

    mPal2 = (short *) malloc(sizeof(short) * 256);
    for (i = 0; i < 256; i++) {
        int r = theLogPalette->palPalEntry[i].peRed;
        int g = theLogPalette->palPalEntry[i].peGreen;
        int b = theLogPalette->palPalEntry[i].peBlue;
        int index = mPalette2.GetNearestPaletteIndex(RGB(r, g, b));

        if (theLogPalette->palPalEntry[index].peRed == 1 && theLogPalette->palPalEntry[index].peGreen == 1 && theLogPalette->palPalEntry[index].peBlue == 1)
            index = 255;

        mPal2[i] = index;
    }

    free(theLogPalette);

    theBitmap.LoadBitmap(IDB_PATTERN_BRUSH);
    theBrush.CreatePatternBrush(&theBitmap);
}

int T2OpenSelectDlg::ShowOpenSelectDialog() {
    Setup();

    CRect rect;
    GetDesktopWindow()->GetWindowRect(rect);

    HINSTANCE instance = AfxGetInstanceHandle();

    T2DLGTEMPLATE tmpl;
    tmpl.pt = rect.CenterPoint();
    tmpl.moduleHandle = instance;
    tmpl.resID = kDlgResID;

    T2TowerDoc *theDoc = NULL;

#line 137
    mImageObj = new T2ImageObj;
    mImage = new T2BitImage(instance, IDB_OPEN_SELECT, true);
    mImageObj->AddObject(instance, IDB_OPEN_SELECT, mImage);

    Realize(this, &tmpl, theDoc, mImageObj, &mPalette2, true, NULL, 0, false);

    if (gT2App->mStrE0 != "") {
        PostMessage(WM_COMMAND, kCmdLoadFromPath);
    } else {
        ShowWindow(SW_SHOW);
        SetActiveWindow();
    }

    return 0;
}

/*virtual*/ void T2OpenSelectDlg::ListenToMessage(unsigned int msg, void *data) {
}

/*virtual*/ BOOL T2OpenSelectDlg::OnT2DialogCommand(WPARAM inWParam, LPARAM inLParam) {
    BOOL result = true;
    WORD code = HIWORD(inWParam);
    WORD id = LOWORD(inWParam);

    if (id == kCmdNew && code == 0) {
        ShowWindow(SW_HIDE);
        mTowerDoc->LoadsWorldPlugin();
        DoClose(id);
    } else if (id == kCmdOpen && code == 0) {
        ShowWindow(SW_HIDE);
        T2_APP->OnCmdOpen();
        DoClose(id);
    } else if (id == kCmdExit && code == 0) {
        PostQuitMessage(0);
        DoClose(id);
    } else if (id == kCmdLoadFromPath && code == 0) {
        GetCurrentT2TowerDoc()->SetPathName(gT2App->mStrE0);
        GetCurrentT2TowerDoc()->OnOpenDocument(gT2App->mStrE0);
        gT2App->mStrE0 = "";
        DoClose(id);
    }

    return result;
}

/*virtual*/ BOOL T2OpenSelectDlg::OnT2EraseBkgnd(CDC *pDC) {
#pragma var_order(paletteH, result, dc, hdc, palette)
    unknownWindow = GetActiveWindow();

    SetFocus();
    SetActiveWindow();

    BOOL result = true;

    CPaintDC dc(this);
    HDC hdc = dc;
    HPALETTE paletteH = mBitmap->CreatePalette();
    CPalette palette;

    SelectPalette(dc, paletteH, false);
    RealizePalette(dc);

    mBitmap->Draw(hdc, 0, 0);

    SelectPalette(hdc, palette, false);
    DeleteObject(paletteH);

    return result;
}

/*virtual*/ void T2OpenSelectDlg::vfF4() {
    OnClose();
}

/*virtual*/ void T2OpenSelectDlg::vfF8(int unk1, int unk2) {
}