fe586c6fceff21abb116bd692b4b6fc6e48d08c5
[platform/upstream/krb5.git] / src / windows / lib / gic.c
1 /*
2  * Copyright (C) 1997 Cygnus Solutions.
3  *
4  * Author:  Michael Graff
5  */
6
7 #include <windows.h>
8 #include <windowsx.h>
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13
14 #include "krb5.h"
15
16 #include "vardlg.h"
17 #include "gic.h"
18
19 /*
20  * Steps performed:
21  *
22  *   1)  Create the dialog with all the windows we will need
23  *       later.  This is done by calling vardlg_build() from
24  *       gic_prompter().
25  *
26  *   2)  Run the dialog from within gic_prompter().  If the return
27  *       value of the dialog is -1 or IDCANCEL, return an error.
28  *       Otherwise, return success.
29  *
30  *   3)  From within the dialog initialization code, call
31  *       vardlg_config(), which will:
32  *
33  *      a)  Set all the label strings in all the entry labels and
34  *          the banner.
35  *
36  *      b)  Set the maximum input lengths on the entry fields.
37  *
38  *      c)  Calculate the size of the text used within the banner.
39  *
40  *      d)  Calculate the longest string of text used as a label.
41  *
42  *      e)  Resize each label and each entry within the dialog
43  *          to "look nice."
44  *
45  *      f)  Place the OK and perhaps the Cancel buttons at the bottom
46  *          of the dialog.
47  *
48  *   4)  When the OK button is clicked, copy all the values from the
49  *       input fields and store them in the pointers we are given.
50  *       Also, set the actual lengths to what we collected from the
51  *       entries.  Finally, call EndDialog(IDOK) to end the dialog.
52  */
53
54 /*
55  * Yes, a global.  It is a PITA to not use them in windows.
56  */
57 gic_data *gd;
58
59
60 /*
61  * initialize the dialog
62  */
63 static BOOL
64 gic_dialog_init(HWND hwnd, HWND hwndFocus, LPARAM lParam)
65 {
66         vardlg_config(hwnd, gd->width, gd->banner, gd->num_prompts,
67                 gd->prompts, (WORD)(gd->id));
68
69         return FALSE;
70 }
71
72 /*
73  * process dialog "commands"
74  */
75 static void
76 gic_dialog_command(HWND hwnd, int cid, HWND hwndCtl, UINT codeNotify)
77 {
78
79         int n;
80         WORD id;
81
82         /*
83          * We are only interested in button clicks, and then only of
84          * type IDOK or IDCANCEL.
85          */
86         if (codeNotify != BN_CLICKED)
87                 return;
88         if (cid != IDOK && cid != IDCANCEL)
89                 return;
90
91         /*
92          * If we are canceled, wipe all the fields and return IDCANCEL.
93          */
94         if (cid == IDCANCEL) {
95                 EndDialog(hwnd, IDCANCEL);
96                 return;
97         }
98
99         /*
100          * must be IDOK...
101          */
102         id = (gd->id + 2);
103         for (n = 0 ; n < gd->num_prompts ; n++) {
104                 Edit_GetText(GetDlgItem(hwnd, id), gd->prompts[n].reply->data,
105                         gd->prompts[n].reply->length);
106                 gd->prompts[n].reply->length = (unsigned)strlen(gd->prompts[n].reply->data);
107                 id += 2;
108         }
109
110         EndDialog(hwnd, IDOK);
111 }
112
113 /*
114  * The dialog callback.
115  */
116 static INT_PTR CALLBACK
117 gic_dialog(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
118 {
119         switch (message) {
120                 HANDLE_MSG(hwnd, WM_INITDIALOG, gic_dialog_init);
121
122                 HANDLE_MSG(hwnd, WM_COMMAND, gic_dialog_command);
123         }
124
125         return FALSE;
126 }
127
128
129 /*
130  * All the disgusting code to use the get_init_creds() functions in a
131  * broken environment
132  */
133 krb5_error_code KRB5_CALLCONV
134 gic_prompter(krb5_context ctx, void *data, const char *name,
135              const char *banner, int num_prompts, krb5_prompt prompts[])
136 {
137         int       rc;
138         void     *dlg;
139
140         gd = data;
141
142         gd->banner = banner;
143         gd->num_prompts = num_prompts;
144         gd->prompts = prompts;
145         if (gd->width == 0)
146                 gd->width = 450;
147
148         dlg = vardlg_build((WORD)(gd->width), name, gd->banner,
149                            (WORD)num_prompts, prompts, (WORD)(gd->id));
150
151         rc = DialogBoxIndirect(gd->hinstance, (LPDLGTEMPLATE)dlg, gd->hwnd, gic_dialog);
152
153         if (rc != IDOK)
154                 return 1;
155
156         return 0;
157 }