aboutsummaryrefslogtreecommitdiff
path: root/src/windows/identity/uilib/propsheet.c
blob: 55c6cbb1b7e0f1e87adf1e1906aebff0191969d8 (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
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
/*
 * Copyright (c) 2005 Massachusetts Institute of Technology
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* $Id$ */

#include<khuidefs.h>
#include<utils.h>
#ifdef DEBUG
#include<assert.h>
#endif

CRITICAL_SECTION cs_props;

void
ps_init(void) {
    InitializeCriticalSection(&cs_props);
}

void
ps_exit(void) {
    DeleteCriticalSection(&cs_props);
}

KHMEXP khm_int32 KHMAPI 
khui_ps_create_sheet(khui_property_sheet ** sheet)
{
    khui_property_sheet * ps;

    ps = PMALLOC(sizeof(*ps));
    ZeroMemory(ps, sizeof(*ps));

    ps->header.dwSize = sizeof(ps->header);
    ps->header.dwFlags = PSH_MODELESS | PSH_PROPTITLE;
    ps->status = KHUI_PS_STATUS_NONE;

    *sheet = ps;

    return KHM_ERROR_SUCCESS;
}

KHMEXP khm_int32 KHMAPI 
khui_ps_add_page(khui_property_sheet * sheet,
                 khm_int32 credtype,
                 khm_int32 ordinal,
                 LPPROPSHEETPAGE ppage,
                 khui_property_page ** page)
{
    khui_property_page * p;

    p = PMALLOC(sizeof(*p));
    ZeroMemory(p, sizeof(*p));

    p->credtype = credtype;
    p->ordinal = ordinal;
    p->p_page = ppage;

    EnterCriticalSection(&cs_props);    
    QPUT(sheet, p);
    sheet->n_pages++;
    LeaveCriticalSection(&cs_props);

    if(page)
        *page = p;

    return KHM_ERROR_SUCCESS;
}

KHMEXP khm_int32 KHMAPI 
khui_ps_find_page(khui_property_sheet * sheet,
                  khm_int32 credtype,
                  khui_property_page ** page)
{
    khui_property_page * p;

    EnterCriticalSection(&cs_props);
    p = QTOP(sheet);

    while(p) {
        if(p->credtype == credtype)
            break;
        p = QNEXT(p);
    }
    LeaveCriticalSection(&cs_props);

    if(p) {
        *page = p;
        return KHM_ERROR_SUCCESS;
    } else {
        *page = NULL;
        return KHM_ERROR_NOT_FOUND;
    }
}

int __cdecl 
ps_order_func(const void *l, const void * r) {
    khui_property_page * lp;
    khui_property_page * rp;

    lp = *(khui_property_page **)l;
    rp = *(khui_property_page **)r;

    if (lp->ordinal == rp->ordinal)
        return lp->credtype - rp->credtype;
    else
        return lp->ordinal - rp->ordinal;
}

KHMEXP HWND KHMAPI 
khui_ps_show_sheet(HWND parent, khui_property_sheet * s)
{
    khui_property_page * p;
    HPROPSHEETPAGE phpsp[KHUI_PS_MAX_PSP];
    khui_property_page * ppgs[KHUI_PS_MAX_PSP];
    int i;
    INT_PTR prv;
    HWND hw;

    EnterCriticalSection(&cs_props);

    s->header.hwndParent = parent;
    s->header.nPages = s->n_pages;

    p = QTOP(s);
    i = 0;
    while(p) {
        p->h_page = CreatePropertySheetPage(p->p_page);
#ifdef DEBUG
        assert(p->h_page);
#endif
        ppgs[i++] = p;
        p = QNEXT(p);
    }

#ifdef DEBUG
    assert(i == s->n_pages);
#endif

    qsort(ppgs, s->n_pages, sizeof(ppgs[0]), ps_order_func);

    for (i=0; i < s->n_pages; i++) {
        phpsp[i] = ppgs[i]->h_page;
    }

    s->header.phpage = phpsp;

    prv = PropertySheet(&s->header);

    s->header.phpage = NULL;

    if(prv <= 0) {
#ifdef DEBUG
        assert(FALSE);
#endif
        /*TODO: better handling for this */
        hw = NULL;
    } else {
        s->status = KHUI_PS_STATUS_RUNNING;

        hw = (HWND) prv;
        s->hwnd = hw;
        s->hwnd_page = PropSheet_GetCurrentPageHwnd(hw);
    }
    LeaveCriticalSection(&cs_props);

    return hw;
}

KHMEXP LRESULT KHMAPI 
khui_ps_check_message(khui_property_sheet * sheet, 
                      PMSG pmsg)
{
    LRESULT lr;

    if(sheet->hwnd == NULL)
        return FALSE;

    lr = PropSheet_IsDialogMessage(sheet->hwnd, pmsg);
    if(lr) {
        sheet->hwnd_page = PropSheet_GetCurrentPageHwnd(sheet->hwnd);
        if(sheet->hwnd_page == NULL && 
           sheet->status == KHUI_PS_STATUS_RUNNING)

            sheet->status = KHUI_PS_STATUS_DONE;
    }

    return lr;
}

KHMEXP khm_int32 KHMAPI 
khui_ps_destroy_sheet(khui_property_sheet * sheet)
{
    khui_property_page * p;

    EnterCriticalSection(&cs_props);

    DestroyWindow(sheet->hwnd);
    sheet->hwnd = NULL;

    QGET(sheet, &p);
    while(p) {
        PFREE(p);
        QGET(sheet, &p);
    }
    PFREE(sheet);

    LeaveCriticalSection(&cs_props);

    return KHM_ERROR_SUCCESS;
}