win-dialogs.c

Go to the documentation of this file.
00001 #include <windows.h>
00002 #include <commctrl.h>
00003 #include <vfw.h>
00004 #include <math.h>
00005 #include <stdlib.h>
00006 #include <string.h> 
00007 #include <stdio.h>
00008 #include <sys/types.h>
00009 #include <sys/stat.h>
00010 #include <malloc.h>
00011 #include "win-dialogs.h"
00012 #include "sic_comm.h"
00013 #include "cfc.h"
00014 #include "getcar.h"
00015 #include "xinput.h"
00016 
00017 #define NMENUCOMMAND 50
00018 #define LDIM 25
00019 #define BDIM 22
00020 #define IDIM 18
00021 #define vertpad 0
00022 #define horzpad 5
00023 #define FIRSTCHAINID       100 // debut des indices des chaines
00024 #define FIRSTSLIDERID      200 // debut des indices des sliders
00025 #define FIRSTBUTTONID      300 // debut des indices des boutons 'more', 'logic' et 'file'
00026 #define FIRSTCOMMANDID     400 // debut des indices des boutons de commande dans la fenetre principale
00027 #define FIRSTHELPID        500 // debut des indices des boutons helps
00028 #define FIRSTOPTIONOK      600 // debut des indices des boutons OK dans les fenetres secondaires
00029 #define BUTTONFLAGS WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHLIKE
00030 #define LOGICFLAGS WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | BS_CENTER
00031 #define LABELFLAGS WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER | SS_NOTIFY
00032 #define CHAINFLAGS WS_CHILD | WS_VISIBLE | WS_BORDER | ES_CENTER
00033 #define SLIDERFLAGS WS_CHILD | WS_VISIBLE | TBS_NOTICKS | TBS_ENABLESELRANGE
00034 #define COMBOBOXFLAGS WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_AUTOHSCROLL | CBS_DROPDOWN | CBS_HASSTRINGS
00035 
00036 int fontheight;
00037 
00038 int mainwidth, mainheight, viewportheight, commandheight;
00039 int labelwidth=200, textwidth=300, sliderchainwidth=50, sliderwidth=250; 
00040 int filechainwidth=250, buttonwidth=100, morewidth=150, helpwidth=50;
00041 int itemheight=LDIM,buttonheight=BDIM, chainheight=IDIM, labelheight=IDIM;
00042 int choiceheight=LDIM, logicheight=IDIM, sliderheight=IDIM;
00043 HWND labelw, editw, logicw, sliderw, buttonw, okw, cancelw, helpw;
00044 HWND morewindows[MAX_NB_DIALOG], editwin[NSTRUCT], sliderwin[NSTRUCT], logicwin[NSTRUCT];
00045 HFONT dialogfont=NULL;
00046 char *dialoghelpname[MAX_NB_DIALOG];
00047 int nb_widgets, WIN0=0, nmore=0, NEL[MAX_NB_DIALOG];
00048 
00049 int index[MAX_NB_DIALOG][NSTRUCT], IDWIN[NSTRUCT], INDEXCOM[MAX_NB_DIALOG];
00050 int screenheight, screenwidth, curheight[MAX_NB_DIALOG];
00051 int num_actions[NSTRUCT];
00052 static char *command[NMENUCOMMAND];
00053 static char *menucommand[NMENUCOMMAND];
00054 
00055 static char return_command[COMMANDLENGTH]; /* Passed to get_user_input */
00056 
00057 HGDIOBJ DlgFont, VarFont;
00058 
00059 char clog[7] = "No\0Yes\0";
00060 int SCB;
00061 
00062 int ERR_CODE;
00063 
00064 void MyErrorHandler(char *str1, char *str2)
00065 {
00066    fprintf(stderr, "error %d at %s in %s\n", GetLastError(), str1, str2);
00067 }
00068 
00069 HINSTANCE hInst; // current instance
00070 HWND   FrameWnd, hWnd, root_win;
00071 HANDLE hThread=NULL; 
00072 WORD *pdlgtemplate, *save_cur_ptr;
00073 
00074 
00075 LRESULT APIENTRY MainWndProc(HWND, UINT, UINT, LONG);
00076 
00077 static void save_context(HWND hwnd)
00078 {
00079   int i, j, k, ind;
00080   int len;
00081   char buffer[256];
00082 
00083   ind = GetWindowLong(hwnd, GWL_USERDATA);
00084   
00085   for (i = 0; i < NEL[ind]; i++) {
00086         j = index[ind][i];
00087     switch(widgets[j].generic.type) {
00088         case CHAIN:
00089       len = GetDlgItemText(hwnd, FIRSTCHAINID+i, (LPTSTR) buffer, 256);
00090           // len = GetDlgItemText(hwnd, + FIRSTCHAINID+j, (LPTSTR) buffer, 256);
00091           strncpy( widgets[j].chain.userchain, buffer, CHAINLENGTH);
00092           /*printf("%s\n", widgets[j].chain.userchain);
00093           strncpy(widgets[j].chain.chain,
00094                 widgets[j].chain.userchain,
00095                 widgets[j].chain.length);*/
00096           break;
00097         case CHOICE:
00098       len = GetDlgItemText(hwnd, FIRSTCHAINID+i, (LPTSTR) buffer, 256);
00099           // len = GetDlgItemText(hwnd, + FIRSTCHAINID+j, (LPTSTR) buffer, 256);
00100           
00101           if (widgets[j].choice.mode == 1) {
00102             for (k = 0; k<widgets[j].choice.nchoices; k++) {
00103                         if (len == strlen(widgets[j].choice.choices[k]) ) {  // SG compare on full length
00104                                 if (strncmp(buffer,
00105                                         widgets[j].choice.choices[k],
00106                                         strlen(widgets[j].choice.choices[k])) == 0)
00107                                 break;
00108                         }
00109             }
00110             sprintf(buffer, "%d", (k+1));
00111       }
00112       strncpy(widgets[j].choice.userchoice, buffer, CHAINLENGTH);
00113 /*
00114           printf("%s\n", widgets[j].choice.userchoice);
00115           strncpy(widgets[j].choice.choice,
00116                 widgets[j].choice.userchoice,
00117                 widgets[j].choice.length);  */
00118           break;
00119 
00120         case LOGIC:
00121       len = GetDlgItemText(hwnd, FIRSTBUTTONID+i, (LPTSTR) buffer, 256);
00122           
00123           widgets[j].logic.userlogic = strncmp(buffer, "Yes", 3) ? 0 : 1;
00124           //printf("logic %s %d\n", buffer, widgets[j].logic.userlogic);
00125           //*widgets[j].logic.logic = widgets[j].logic.userlogic;
00126 
00127           break;
00128         case SLIDER:
00129       len = GetDlgItemText(hwnd, FIRSTCHAINID+i, (LPTSTR) buffer, 256);
00130       *widgets[j].slider.value = widgets[j].slider.uservalue = (float) atof(buffer);
00131           //printf("slider %g\n", widgets[j].slider.uservalue);
00132       //*widgets[j].slider.value = widgets[j].slider.uservalue;
00133           break;
00134         case BROWSER:
00135           len = GetDlgItemText(hwnd, FIRSTCHAINID+i, (LPTSTR) buffer, 256);
00136       strncpy(widgets[j].file.userchain, buffer, CHAINLENGTH);
00137           //printf("file %s\n", buffer, widgets[j].file.userchain);
00138           //strncpy(widgets[j].file.chain, widgets[j].file.userchain, widgets[j].file.length);
00139           break;
00140         }
00141   }
00142 }
00143 
00144 static void close_dialog(HWND hwnd, WORD id)
00145 {
00146     int i;
00147 
00148     save_context(hWnd);
00149     for (i = 0; i<nmore; i++)
00150         save_context(morewindows[i]);
00151 
00152     if (on_close_dialog( return_command, ERR_CODE) == -1)
00153         MessageBeep( MB_ICONHAND);
00154 }
00155 
00156 int ncreate=0;
00157 char dumstr[80];
00158 
00159 void SetWidgetFont(HWND hwnd, HFONT hfont)
00160 {
00161   SendMessage(hwnd, WM_SETFONT,
00162               (WPARAM) hfont,                  
00163               (LPARAM) MAKELPARAM(TRUE, 0));
00164 }
00165 
00166 
00167 WNDPROC wpOrigEditProc, wpOrigChoiceProc;  
00168 POINT caretpos;
00169 
00170 
00171 
00172 int yoffset=0;
00173 SCROLLINFO si;
00174 
00175 LRESULT APIENTRY ChoiceSubclassProc(hwnd, uMsg, wParam, lParam) 
00176 HWND hwnd; 
00177 UINT uMsg; 
00178 WPARAM wParam; 
00179 LPARAM lParam; 
00180 { 
00181   int i, ind;
00182   LRESULT index;
00183   
00184   switch (uMsg) {
00185   
00186   case WM_COMMAND:
00187         //printf("COMMAND\n");
00188         ind = GetWindowLong(hwnd, GWL_USERDATA);
00189                 
00190     for (i=0; i<nb_widgets; i++) {
00191           if ((i != ind) && (strncmp(widgets[i].generic.variable, 
00192                   widgets[ind].generic.variable, 15) == 0)) {
00193                 index = SendMessage(hwnd, CB_GETCURSEL, (WPARAM)0, (LPARAM) 0);
00194                 SendMessage(editwin[i], CB_SETCURSEL, (WPARAM)index, (LPARAM)0);
00195           }
00196         }
00197         break; 
00198   }
00199   return CallWindowProc(wpOrigChoiceProc, hwnd, uMsg, wParam, lParam);
00200 }
00201 
00202 LRESULT APIENTRY EditSubclassProc(hwnd, uMsg, wParam, lParam) 
00203 HWND hwnd; 
00204 UINT uMsg; 
00205 WPARAM wParam; 
00206 LPARAM lParam; 
00207 { 
00208   HWND parent;
00209   int i, j, ind, prev, next;
00210   char buffer[256];
00211   DWORD dwPos;
00212 
00213  
00214   switch (uMsg) {
00215  
00216   
00217   case WM_KEYDOWN:
00218         if ((wParam == VK_UP) || (wParam == VK_DOWN)) {
00219           parent = GetParent(hwnd);
00220           GetCaretPos(&caretpos);
00221 
00222           MapWindowPoints(hwnd, parent, &caretpos, 1);
00223           
00224           prev = caretpos.y/itemheight;
00225 
00226           ind = GetWindowLong(parent, GWL_USERDATA);
00227           //printf("%d %d\n", ind, prev);
00228         
00229           if (prev < NEL[ind]) {
00230                 next = prev;
00231                 if (wParam == VK_UP) {
00232                   while (next > 0) {
00233                         next--;
00234                         
00235                         j = index[ind][next];
00236                         if (((widgets[j].generic.type == CHAIN) && (widgets[j].chain.editable)) ||
00237                                 (widgets[j].generic.type == SLIDER) || 
00238                                 (widgets[j].generic.type == BROWSER)) {
00239                           SetFocus(editwin[j]);
00240                           GetCaretPos(&caretpos);
00241                           MapWindowPoints(editwin[j], parent, &caretpos, 1);
00242                           if ((!ind) && (caretpos.y < -yoffset)) {
00243                             //printf("caret %d offset %d\n", caretpos.y, -yoffset);
00244                                 si.nPos-=((-yoffset-caretpos.y+itemheight)/itemheight)*itemheight;
00245                                 yoffset+=((-yoffset-caretpos.y+itemheight)/itemheight)*itemheight;
00246                         
00247                                         
00248                                 yoffset=min(0, yoffset);
00249                                 si.nPos = max(0, si.nPos); 
00250                                 
00251                                 MoveWindow(hWnd, 0, yoffset, mainwidth, mainheight, 1);
00252             
00253                                 si.fMask  = SIF_POS;
00254                                 SetScrollInfo(FrameWnd, SB_VERT, &si, TRUE); 
00255                           }
00256                           break;
00257                         }
00258                   }
00259                   
00260                 }
00261                 else if (wParam == VK_DOWN) {
00262                   while (next < NEL[ind] - 1) {
00263                         next++;
00264                         j = index[ind][next];
00265                         if (((widgets[j].generic.type == CHAIN) && (widgets[j].chain.editable)) ||
00266                         (widgets[j].generic.type == SLIDER) || 
00267                         (widgets[j].generic.type == BROWSER)) {
00268                           SetFocus(editwin[j]);
00269                           GetCaretPos(&caretpos);
00270                           MapWindowPoints(editwin[j], parent, &caretpos, 1);
00271                           
00272                           if ((!ind) && (caretpos.y + yoffset > viewportheight)) {
00273                                 //printf("caret %d offset %d\n", caretpos.y, yoffset);
00274                                 si.nPos += ((caretpos.y-viewportheight+itemheight)/itemheight)*itemheight;
00275                                 yoffset -= ((caretpos.y-viewportheight+itemheight)/itemheight)*itemheight;
00276                                 //yoffset -= itemheight;
00277                         //si.nPos += itemheight;
00278                                 yoffset = max(viewportheight - mainheight, yoffset);
00279                                 si.nPos = min(si.nPos, mainheight-viewportheight);
00280                 
00281                     MoveWindow(hWnd, 0, yoffset, mainwidth, mainheight, 1);     
00282                                 si.fMask  = SIF_POS;
00283                                 SetScrollInfo(FrameWnd, SB_VERT, &si, TRUE);  
00284                           }
00285                           break; 
00286                         }
00287                   }
00288                 }
00289         
00290           }
00291           return 0;
00292         }
00293         break;
00294 
00295   case WM_KEYUP:
00296         ind = GetWindowLong(hwnd, GWL_USERDATA);
00297                 
00298     for (i=0; i<nb_widgets; i++) {
00299         
00300           if (strncmp(widgets[i].generic.variable, widgets[ind].generic.variable, 15) == 0) {
00301         if ((i != ind) || (widgets[i].generic.type == SLIDER))
00302                         SendMessage(hwnd, WM_GETTEXT, 256, (LPARAM) buffer);
00303                 
00304                 if (i != ind) SendMessage(editwin[i], WM_SETTEXT, 0, (LPARAM) buffer);
00305                 
00306                 if (widgets[i].generic.type == SLIDER) {
00307                   dwPos = (DWORD) (100*(atof(buffer)-widgets[ind].slider.min)/
00308                           widgets[ind].slider.width);
00309                   SendMessage(sliderwin[i], TBM_SETPOS, (WPARAM) TRUE, (LPARAM) dwPos);
00310                 }
00311           }
00312         }
00313         break;
00314   }
00315   
00316   return CallWindowProc(wpOrigEditProc, hwnd, uMsg, wParam, lParam);
00317  
00318 }
00319 
00320 HWND dialog_help_win[MAX_NB_DIALOG], dialog_help_edit[MAX_NB_DIALOG]; // Fenetres de help des panneaux de controle
00321 char *dialoghelptext;
00322 
00323 void dialog_help(int idfilename, char *varname)
00324 {
00325         char *text;
00326         struct _stat statb;
00327         FILE *fd;
00328         int j, ind;
00329         char fname[256];
00330         char tampon[256];
00331         char variablename[16], taskname[16];    
00332 
00333         sprintf(fname, "%s", dialoghelpname[idfilename]);
00334         if (_stat(fname, &statb) == -1) {
00335                 text = (char *) malloc(256*sizeof(char));
00336     
00337                 if (varname == 0)
00338                         strcpy(text, "Reference not found ...");
00339                 else {
00340                         sprintf(tampon, "Variable %s\n", varname);
00341                         strcpy(text, tampon);
00342                         strcat(text, "---------------------------------------------\n");
00343                 }
00344         }
00345         else {
00346                 //Allocate much more memory to allow chain substitutions and separator lines
00347                 text = (char *) malloc((unsigned)(2*statb.st_size));
00348                 
00349                 
00350                 fd = fopen(fname, "r");
00351         /*
00352                   numread = fread(text, sizeof(char), statb.st_size+1, fd);
00353                 printf( "Number of bytes read = %d\n", numread );
00354 
00355         text[statb.st_size] = 0;
00356         */
00357                 if (varname == 0) {
00358                 // On a clique sur un bouton help
00359                         strcpy(text, "  ");
00360                         while (fgets(tampon, 255, fd) != NULL) {
00361                                 if (strncmp(tampon, "1 END", 5) == 0) break;
00362 
00363                                 if (tampon[0] == '1') {
00364                                         sscanf(tampon, "%d%s", &ind, taskname);
00365                                         strcpy(text, "---------------------------------------------\n");
00366                                         sprintf(tampon, "%s%s\n", "TASK : ", taskname);
00367                                         strcat(text, tampon);
00368                                         strcat(text, "---------------------------------------------\n\n");
00369                                 }       
00370                                 else if (tampon[0] == '2') {
00371                                         sscanf(tampon, "%d%s", &ind, variablename);
00372                                         sprintf(tampon, "\n Variable %s\n", variablename);
00373                                         strcat(text, tampon);
00374                                         strcat(text, "---------------------------------------------\n\n");
00375                                 }
00376                                 else strcat(text, tampon);
00377                         }
00378                 
00379                 }
00380                 else {
00381                         j = 0;
00382                         while(varname[j] != ' ') j++;
00383                         while (fgets(tampon, 255, fd) != NULL) {
00384                                 sscanf(tampon, "%d%s", &ind, variablename);
00385                                 if (strncmp(variablename, varname, j) == 0)
00386                                         break;
00387                         }
00388                         if (strncmp(variablename, varname, j) != 0) {
00389                                 sprintf(tampon, "Variable %s\n", varname);
00390                                 strcpy(text, tampon);
00391                                 strcat(text, "---------------------------------------------\n");
00392                         }
00393                         else {
00394                                 sscanf(tampon, "%d%s", &ind, variablename);
00395                                 sprintf(tampon, "Variable %s\n", variablename);
00396                                 strcpy(text, tampon);
00397                                 strcat(text, "---------------------------------------------\n");
00398 
00399                                 while (fgets(tampon, 255, fd) != NULL) {
00400                                         if ((tampon[0] == '1') || (tampon[0] == '2')) break;
00401                                         else strcat(text, tampon);
00402                                 }
00403                         }
00404                 }
00405                 fclose(fd);
00406         }
00407         
00408         //printf("IDFILENAME %d\n", idfilename);
00409         if (dialog_help_win[idfilename] == NULL) {
00410         
00411                 dialog_help_win[idfilename] = CreateWindowEx(WS_EX_TOOLWINDOW, TEXT("helpdialog"),
00412                                                                         (LPCTSTR) fname,  
00413                                                             WS_OVERLAPPEDWINDOW,
00414                                                                         CW_USEDEFAULT, CW_USEDEFAULT,
00415                                                                         600, 300,
00416                                                                         (HWND) NULL, (HMENU) NULL, hInst, (LPSTR) NULL);
00417 
00418             ShowWindow(dialog_help_win[idfilename], SW_SHOWNORMAL);
00419         }
00420         else ShowWindow(dialog_help_win[idfilename], SW_SHOW);
00421     
00422         SetForegroundWindow(dialog_help_win[idfilename]);
00423 //SendMessage(dialog_help_edit, WM_SETTEXT, 0, (LPARAM) dialoghelptext);
00424 //free(dialoghelptext);
00425         SendMessage(dialog_help_edit[idfilename], WM_SETTEXT, 0, (LPARAM) text);
00426         free(text);
00427 
00428 }
00429 
00430 int nhelpwcreate=0;
00431 
00432 LRESULT APIENTRY HelpWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
00433 {
00434   HWND wnd;
00435   int i;
00436 
00437   switch (message) {
00438   case WM_CREATE:
00439           dialog_help_edit[nhelpwcreate] = CreateWindow("RICHEDIT", NULL,       
00440                                                            WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER |
00441                                                            ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY, 
00442                                                            0, 0, 0, 0, /* set size in WM_SIZE message  */ 
00443                                                        hwnd, (HMENU) NULL, hInst, (LPSTR) NULL);
00444       nhelpwcreate++;
00445           /*
00446           SendMessage(dialog_help_edit, WM_SETTEXT, 0, (LPARAM) dialoghelptext);
00447           free(dialoghelptext);
00448    */
00449           break;
00450           
00451   case WM_SIZE:
00452         for (i = 0; i<nhelpwcreate; i++) {
00453            //printf("%s %d\n", dialoghelpname[i], i); 
00454            wnd = FindWindow("helpdialog", dialoghelpname[i]);
00455            //printf("%d %s %d %d %d\n", nhelpwcreate, dialoghelpname[i], i, hwnd, wnd);
00456            if (hwnd == wnd) break;
00457         }
00458 
00459         MoveWindow(dialog_help_edit[i], 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
00460         break;
00461   case WM_CLOSE:
00462         ShowWindow(hwnd, SW_HIDE);
00463         break;
00464   default:
00465         return (DefWindowProc(hwnd, message, wParam, lParam));
00466         break;
00467   }
00468   return 0;
00469 }
00470                         
00471 // Fonction de reception des messages en provenance des Panneaux de controle
00472 
00473 int firstpass=1;
00474 int NewPos, PrevPos;
00475 
00476 LRESULT APIENTRY MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
00477 {
00478   int i, j, k, nh, ind, cursel;
00479   size_t l;
00480   POINT pt;
00481   WORD idcom;
00482   DWORD dwPos;
00483   float value;
00484   HWND hwndCtl;
00485   char buffer[256];
00486   
00487 
00488   switch (message) {
00489  
00490   case WM_CREATE:
00491 
00492     
00493         if (dialogfont == NULL) {
00494 
00495           dialogfont = CreateFont(14, 0, 0, 0, 800, FALSE, FALSE, FALSE,
00496                                         ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
00497                                         DEFAULT_QUALITY, DEFAULT_PITCH, "Arial");
00498           
00499           //printf("CREATE mainheight %d viewportheight %d\n", mainheight, viewportheight);
00500         }
00501                 
00502         if (SCB && firstpass) {
00503                 
00504                 si.cbSize = sizeof(si); 
00505         si.fMask  = SIF_RANGE | SIF_PAGE | SIF_POS; 
00506         si.nMin   = 0; 
00507         si.nMax   = mainheight - viewportheight;  // SG: subtract 100
00508         si.nPage  = 1; 
00509         si.nPos   = 0; 
00510         SetScrollInfo(hwnd, SB_VERT, &si, TRUE); 
00511                 firstpass = 0; 
00512                 //printf("Scroll range %d\n", si.nMax);
00513                 return 0;//(DefWindowProc(hwnd, message, wParam, lParam));
00514         }
00515           
00516                 
00517     SetWindowLong(hwnd, GWL_USERDATA, ncreate);
00518         ind = ncreate;
00519         ncreate++;
00520 
00521         //printf("Create win %d\n", ind);
00522 
00523 
00524      
00525         for (i = 0; i < NEL[ind]; i++) {
00526           j = index[ind][i];
00527           if (j == -1) return 0;
00528 
00529           labelw = CreateWindow("STATIC", widgets[j].generic.label,  LABELFLAGS,
00530                                            0, curheight[ind], labelwidth, labelheight,
00531                                        hwnd, (HMENU) NULL, hInst, (LPSTR) NULL);
00532       SetWidgetFont(labelw, dialogfont);
00533 
00534           switch(widgets[j].generic.type) {
00535           case SHOW:
00536                 if (strlen(widgets[j].generic.label) != 0)
00537                         labelw = CreateWindow("STATIC", widgets[j].show.text,  LABELFLAGS,
00538                                                 labelwidth+horzpad, curheight[ind], textwidth, labelheight,
00539                                                 (HWND) hwnd, (HMENU) NULL, hInst, (LPSTR) NULL);
00540                 else
00541                         labelw = CreateWindow("STATIC", widgets[j].show.text,  LABELFLAGS,
00542                                                 0, curheight[ind], labelwidth+horzpad+textwidth, labelheight,
00543                                                 (HWND) hwnd, (HMENU) NULL, hInst, (LPSTR) NULL);
00544                 
00545                 SetWidgetFont(labelw, dialogfont);
00546                 curheight[ind]+=itemheight;
00547                 break;
00548           case CHAIN:
00549                 
00550             /* End user chain with 0  for display */
00551         for (l = widgets[j].chain.length-1; l >=0; l--)
00552           if (widgets[j].chain.userchain[l] != ' ')   break;
00553         widgets[j].chain.userchain[l+1] = 0;
00554 
00555                 if (widgets[j].chain.editable) {
00556                         editw = CreateWindow("EDIT", widgets[j].chain.userchain,  CHAINFLAGS,
00557                                            labelwidth+horzpad, curheight[ind], textwidth, chainheight,
00558                                        (HWND) hwnd, (HMENU)(intptr_t)(FIRSTCHAINID+i), hInst, (LPSTR) NULL);
00559                     
00560                         
00561                 }
00562                 else
00563                         editw = CreateWindow("EDIT", widgets[j].chain.userchain,  CHAINFLAGS | ES_READONLY,
00564                                            labelwidth+horzpad, curheight[ind], textwidth, chainheight,
00565                                        (HWND) hwnd, (HMENU)(intptr_t)(FIRSTCHAINID+i), hInst, (LPSTR) NULL);
00566                 
00567                 
00568                 
00569                 SetWindowLong(editw, GWL_USERDATA, j);
00570                 SetWidgetFont(editw, dialogfont);
00571                 curheight[ind]+=itemheight;
00572 
00573 
00574                 editwin[j] = editw;
00575         wpOrigEditProc = (WNDPROC)(intptr_t) SetWindowLong(editw, GWL_WNDPROC, (LONG)(intptr_t)EditSubclassProc);
00576                         //printf("PROC %d\n", wpOrigEditProc);
00577                 break;
00578           case BROWSER:
00579             
00580                 /* End user chain with 0  for display */
00581         for (l = widgets[j].file.length-1; l >=0; l--)
00582           if (widgets[j].file.userchain[l] != ' ')   break;
00583         widgets[j].file.userchain[l+1] = 0;
00584                 editw = CreateWindow("EDIT", widgets[j].file.userchain,  CHAINFLAGS,
00585                                            labelwidth+horzpad, curheight[ind], 
00586                                            filechainwidth-horzpad, chainheight,
00587                                        (HWND) hwnd, (HMENU)(intptr_t)(FIRSTCHAINID+i), hInst, (LPSTR) NULL);
00588 
00589                 SetWidgetFont(editw, dialogfont);
00590                 
00591                 buttonw = CreateWindow("BUTTON", "File", BUTTONFLAGS,
00592                        labelwidth+filechainwidth+horzpad, curheight[ind]-vertpad, 
00593                                            helpwidth, buttonheight,
00594                                        hwnd, (HMENU)(intptr_t)(FIRSTBUTTONID+i), hInst, (LPSTR) NULL);
00595                 SetWindowLong(editw, GWL_USERDATA, j);
00596                 SetWidgetFont(buttonw, dialogfont);
00597                 curheight[ind]+=itemheight;
00598                 editwin[j] = editw;
00599         wpOrigEditProc = (WNDPROC)(intptr_t)SetWindowLong(editw, GWL_WNDPROC, (LONG)(intptr_t)EditSubclassProc);
00600                 break;
00601           
00602           case CHOICE:
00603             
00604                 if (widgets[j].choice.mode == 0) {
00605              
00606                   /* End user chain with 0  for display */
00607                   for (l = widgets[j].choice.length-1; l >=0; l--)
00608                 if (widgets[j].choice.userchoice[l] != ' ')   break;
00609           widgets[j].choice.userchoice[l+1] = 0;
00610                 
00611                   editw = CreateWindow("COMBOBOX", NULL, COMBOBOXFLAGS,
00612                                                    labelwidth+horzpad, curheight[ind]-vertpad, textwidth,
00613                                                    widgets[j].choice.nchoices*choiceheight,
00614                                                (HWND) hwnd, (HMENU)(intptr_t)(FIRSTCHAINID+i), hInst, (LPSTR) NULL);
00615                 }
00616                 else
00617                   editw = CreateWindow("COMBOBOX", NULL, COMBOBOXFLAGS | CBS_DROPDOWNLIST,
00618                                                    labelwidth+horzpad, curheight[ind]-vertpad, textwidth,
00619                                                    widgets[j].choice.nchoices*choiceheight,
00620                                                (HWND) hwnd, (HMENU)(intptr_t)(FIRSTCHAINID+i), hInst, (LPSTR) NULL);
00621                         
00622                 
00623                 editwin[j] = editw;
00624         wpOrigChoiceProc = (WNDPROC)(intptr_t)SetWindowLong(editw, GWL_WNDPROC, (LONG)(intptr_t)ChoiceSubclassProc);
00625                 
00626                 for (k = 0; k <widgets[j].choice.nchoices; k++)
00627           SendMessage(editw, CB_ADDSTRING,
00628                       (WPARAM) 0,                  
00629                       (LPARAM) (LPCTSTR) widgets[j].choice.choices[k]);
00630                 
00631                 cursel = (int)SendMessage(editw, CB_FINDSTRINGEXACT,
00632                              (WPARAM) -1,                  
00633                              (LPARAM) (LPCTSTR) widgets[j].choice.userchoice);
00634         if (cursel != CB_ERR)
00635                   SendMessage(editw, CB_SETCURSEL,
00636                       (WPARAM) cursel,                  
00637                       (LPARAM) 0);
00638                 SetWindowLong(editw, GWL_USERDATA, j);
00639                 SetWidgetFont(editw, dialogfont);
00640                 curheight[ind]+=itemheight;
00641                 break;
00642           case LOGIC:
00643                 
00644                   logicw = CreateWindow("BUTTON", (LPCTSTR) &clog[3*widgets[j].logic.userlogic], LOGICFLAGS,
00645                        labelwidth+horzpad, curheight[ind], textwidth, logicheight,
00646                                        hwnd, (HMENU)(intptr_t)(FIRSTBUTTONID+i), hInst, (LPSTR) NULL);
00647                   if (widgets[j].logic.userlogic) CheckDlgButton(hwnd, (FIRSTBUTTONID+i), BST_CHECKED);
00648                   SetWidgetFont(logicw, dialogfont);
00649                   curheight[ind]+=itemheight;
00650                   logicwin[j] = logicw;
00651                   SetWindowLong(logicw, GWL_USERDATA, j);
00652                   break;
00653           case SLIDER:
00654         
00655                   sprintf(dumstr, "%g", widgets[j].slider.uservalue);
00656                   editw = CreateWindow("EDIT", dumstr,  CHAINFLAGS,
00657                                            labelwidth+horzpad, curheight[ind], sliderchainwidth, chainheight,
00658                                        (HWND) hwnd, (HMENU)(intptr_t)(FIRSTCHAINID+i), hInst, (LPSTR) NULL);
00659                   
00660                   
00661                   editwin[j] = editw;
00662                   wpOrigEditProc = (WNDPROC)(intptr_t)SetWindowLong(editw, GWL_WNDPROC, (LONG)(intptr_t)EditSubclassProc);
00663                   
00664                   SetWidgetFont(editw, dialogfont);
00665                   sliderw = CreateWindow(TRACKBAR_CLASS, (LPCTSTR) "Trackbar Control", SLIDERFLAGS,
00666                        labelwidth+horzpad+sliderchainwidth, curheight[ind], sliderwidth, sliderheight,
00667                                            hwnd, (HMENU)(intptr_t)(FIRSTSLIDERID+i), hInst, (LPSTR) NULL);
00668                   sliderwin[j] = sliderw;
00669                   SendMessage(sliderw, TBM_SETRANGE, 
00670                       (WPARAM) TRUE,                   // redraw flag 
00671                       (LPARAM) MAKELONG(0, 100));      // min. & max. positions 
00672           SendMessage(sliderw, TBM_SETPOS, 
00673                       (WPARAM) TRUE,                   // redraw flag 
00674                       (LPARAM) (100*(widgets[j].slider.uservalue-widgets[j].slider.min)/
00675                                    widgets[j].slider.width));
00676                   SetWindowLong(editw, GWL_USERDATA, j);
00677                   SetWindowLong(sliderw, GWL_USERDATA, j);
00678                   curheight[ind]+=itemheight;
00679                   break;
00680           case BUTTON:
00681                   
00682                   
00683                   buttonw = CreateWindow("BUTTON", widgets[j].button.title, BUTTONFLAGS,
00684                        labelwidth+horzpad, curheight[ind]-vertpad, buttonwidth, buttonheight,
00685                                        hwnd, (HMENU)(intptr_t)(FIRSTCOMMANDID+i), hInst, (LPSTR) NULL);
00686                   
00687                   SetWidgetFont(buttonw, dialogfont);
00688                   buttonw = CreateWindow("BUTTON", widgets[j].button.moretxt, BUTTONFLAGS,
00689                        labelwidth+horzpad+buttonwidth, curheight[ind]-vertpad, morewidth, buttonheight,
00690                                        hwnd, (HMENU)(intptr_t)(FIRSTBUTTONID+i), hInst, (LPSTR) NULL);
00691                   SetWidgetFont(buttonw, dialogfont);
00692                   buttonw = CreateWindow("BUTTON", "Help", BUTTONFLAGS,
00693                        labelwidth+horzpad+buttonwidth+morewidth, curheight[ind]-vertpad, helpwidth, buttonheight,
00694                                        hwnd, (HMENU)(intptr_t)(FIRSTHELPID+i), hInst, (LPSTR) NULL);
00695                   SetWidgetFont(buttonw, dialogfont);
00696                   curheight[ind]+=itemheight;
00697                   break;
00698                 default:
00699                   printf("type %d unknown in win %d\n", widgets[j].generic.type, ind);
00700                   break;        
00701 
00702           }
00703         }
00704         
00705         if (ind) {
00706           okw = CreateWindow("BUTTON", "Go",  BUTTONFLAGS,
00707                                          0, curheight[ind], mainwidth/3, itemheight,
00708                                          (HWND) hwnd, (HMENU)(intptr_t)(FIRSTOPTIONOK+ind-1), hInst, (LPSTR) NULL);
00709           SetWidgetFont(okw, dialogfont);
00710           cancelw = CreateWindow("BUTTON", "Dismiss",  BUTTONFLAGS,
00711                                              mainwidth/3, curheight[ind], mainwidth/3, itemheight,
00712                                              (HWND) hwnd, (HMENU)(intptr_t)IDCANCEL, hInst, (LPSTR) NULL);
00713         
00714           SetWidgetFont(cancelw, dialogfont);
00715           helpw = CreateWindow("BUTTON", "Help",  BUTTONFLAGS,
00716                                                2*mainwidth/3, curheight[ind], mainwidth/3+1, itemheight,
00717                                            (HWND) hwnd, (HMENU)(intptr_t)(FIRSTHELPID+ind), hInst, (LPSTR) NULL);
00718           SetWidgetFont(helpw, dialogfont);
00719           // create helpfen
00720         }
00721         
00722         return 0;
00723         break;
00724   
00725   break;
00726   case WM_HSCROLL:
00727          hwndCtl = (HWND) lParam;
00728          idcom = GetDlgCtrlID(hwndCtl);
00729          ind = GetWindowLong(hwnd, GWL_USERDATA);
00730 
00731      i = idcom - FIRSTSLIDERID;
00732          if ((i>=0) && (i<NEL[ind])) {
00733            j = index[ind][i];
00734            dwPos = (DWORD)SendMessage(hwndCtl, TBM_GETPOS, 0, 0); 
00735 
00736            value = widgets[j].slider.min + (widgets[j].slider.width/100)*dwPos;
00737 
00738        sprintf(dumstr, "%g", value);
00739            SetDlgItemText(hwnd, i+FIRSTCHAINID, (LPCTSTR) dumstr);
00740 
00741            for (i=0; i<nb_widgets; i++) {
00742              if ((i != j) && (strncmp(widgets[i].generic.variable, 
00743                     widgets[j].generic.variable, 15) == 0)) {
00744                   SendMessage(sliderwin[i], TBM_SETPOS, (WPARAM) TRUE, (LPARAM) dwPos);
00745                   SendMessage(editwin[i], WM_SETTEXT, 0, (LPARAM) dumstr);
00746              }
00747            }
00748          }
00749      break;
00750          // Gestion de la scrollbar dans le cas d'une fenetre plus grande que l'ecran
00751   case WM_VSCROLL:
00752         switch(LOWORD(wParam)) {
00753          case SB_PAGEUP: 
00754         yoffset+=itemheight;
00755                 si.nPos-=itemheight;
00756                 
00757                 yoffset=min(0, yoffset);
00758             si.nPos = max(0, si.nPos); 
00759         //printf("Page Up %d\n", si.nPos);      
00760                 
00761                 MoveWindow(hWnd, 0, yoffset, mainwidth, mainheight, 1);
00762             
00763                 si.fMask  = SIF_POS;
00764                 SetScrollInfo(hwnd, SB_VERT, &si, TRUE); 
00765             break;
00766     case SB_PAGEDOWN: 
00767                 yoffset -= itemheight;
00768                 si.nPos += itemheight;
00769         
00770                 yoffset = max(viewportheight - mainheight, yoffset);
00771                 
00772                 si.nPos = min(si.nPos, mainheight-viewportheight);
00773                 //printf("Page Down %d\n", si.nPos);
00774         
00775                 MoveWindow(hWnd, 0, yoffset, mainwidth, mainheight, 1); 
00776                 
00777                 si.fMask  = SIF_POS;
00778                 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);             
00779                 break;
00780         case SB_LINEUP: 
00781                 yoffset+=itemheight;
00782                 si.nPos-=itemheight;
00783                 
00784                 yoffset=min(0, yoffset);
00785             si.nPos = max(0, si.nPos); 
00786         //printf("Line Up %d\n", si.nPos);      
00787                 
00788                 MoveWindow(hWnd, 0, yoffset, mainwidth, mainheight, 1);
00789             
00790                 si.fMask  = SIF_POS;
00791                 SetScrollInfo(hwnd, SB_VERT, &si, TRUE); 
00792                 break;
00793         case SB_LINEDOWN: 
00794                 yoffset -= itemheight;
00795                 si.nPos += itemheight;
00796         
00797                 yoffset = max(viewportheight - mainheight, yoffset);
00798                 
00799                 si.nPos = min(si.nPos, mainheight-viewportheight);
00800                 //printf("Line Down %d\n", si.nPos);
00801         
00802                 MoveWindow(hWnd, 0, yoffset, mainwidth, mainheight, 1); 
00803                 
00804                 si.fMask  = SIF_POS;
00805                 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);  
00806                 break;
00807         case SB_THUMBPOSITION: 
00808         NewPos = HIWORD(wParam);
00809                 yoffset = -(NewPos/itemheight)*itemheight;
00810                 //printf("Thumb %d NewPos %d\n", NewPos, yoffset);
00811         
00812                 MoveWindow(hWnd, 0, yoffset, mainwidth, mainheight, 1); 
00813                 
00814                 si.nPos = NewPos;
00815                 si.fMask  = SIF_POS;
00816                 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
00817                 break;
00818  
00819     case SB_THUMBTRACK: 
00820                 
00821                 NewPos = HIWORD(wParam);
00822 
00823                 
00824                 if ((NewPos/itemheight)*itemheight != (PrevPos/itemheight)*itemheight) {
00825                   yoffset = -(NewPos/itemheight)*itemheight;
00826                   PrevPos = NewPos;
00827               //printf("Default %d\n", yoffset);
00828                   MoveWindow(hWnd, 0, yoffset, mainwidth, mainheight, 1);       
00829                 
00830                   si.nPos = NewPos;
00831                   si.fMask  = SIF_POS;
00832                   SetScrollInfo(hwnd, SB_VERT, &si, TRUE);  
00833                 }
00834         
00835            break;
00836         default:
00837                 break;
00838         }
00839         break;
00840 
00841         
00842   case WM_HELP:
00843         GetCursorPos(&pt);
00844     ScreenToClient(hwnd, &pt);
00845         i = pt.y/itemheight;
00846         ind = GetWindowLong(hwnd, GWL_USERDATA);
00847 
00848         //printf("Help ...\n");
00849 
00850     if (i < NEL[ind]) {
00851           j = index[ind][i];
00852           if ((widgets[j].generic.type != BUTTON) && (widgets[j].generic.type != SHOW)) {
00853             //printf("win %d %s\n", ind, widgets[j].generic.variable);
00854                 //dialoghelptext = (char*) malloc(80*sizeof(char));
00855                 //strcpy(dialoghelptext, widgets[j].generic.variable);
00856                 dialog_help(ind, widgets[j].generic.variable);
00857           }
00858         }
00859         break;
00860   case WM_COMMAND:
00861           
00862           idcom = LOWORD(wParam);
00863           
00864           if (idcom >= 1000) {
00865                 
00866                 i = idcom - 1000;
00867                 if (i == 0) strcpy( return_command, main_command); // main go
00868                 else if (i == 1) {
00869                   ERR_CODE = -3;
00870                 }
00871                 else if (i == 2) { 
00872                         dialog_help(0, 0); 
00873                         return 0; 
00874                 }
00875                 else {
00876                   j = i-3;
00877                   sprintf(return_command, "%s", command[j]);
00878             }
00879                 close_dialog(hwnd, idcom);
00880           }       
00881           else if (idcom == IDCANCEL) {
00882                 ind = GetWindowLong(hwnd, GWL_USERDATA);
00883                 ShowWindow(hwnd, SW_HIDE);
00884           }
00885           
00886           
00887           // par ordre de plage d'indice decroissants
00888           else if (idcom >= FIRSTOPTIONOK) { // id du bouton OK d'une fenetre optionnelle
00889                   i = idcom - FIRSTOPTIONOK; 
00890                   j = INDEXCOM[i];
00891               sprintf(return_command, "%s", widgets[j].button.command);
00892                   close_dialog(hwnd, idcom);
00893           }
00894           else if (idcom >= FIRSTHELPID) { // id d'un bouton help d'une fenetre optionnelle ou principale
00895                   i = idcom - FIRSTHELPID;  // index de l'element dans la fenetre principale
00896                                                                         // ou indice absolu dans le tableau de chaines dialoghelpname
00897                   ind = GetWindowLong(hwnd, GWL_USERDATA);
00898 
00899                   if (ind == 0) {// fenetre prinicipale
00900                         for (nh = 1; nh < MAX_NB_DIALOG; nh++)
00901                           if (strcmp(widgets[index[0][i]].button.helptxt, dialoghelpname[nh]) == 0) break;
00902                         dialog_help(nh, 0);
00903                   }
00904                   else dialog_help(i, 0);
00905                   return 0;
00906           }
00907           else if (idcom >= FIRSTCOMMANDID) { 
00908                 i = idcom - FIRSTCOMMANDID; // i index de l'element de commande dans la fenetre principale
00909                 if (i<NEL[0]) {
00910                   j = index[0][i]; // j index dans la liste des widgets
00911               sprintf(return_command, "%s", widgets[j].button.command);
00912                   close_dialog(hwnd, idcom);
00913                 }
00914           }
00915           else if (idcom >= FIRSTBUTTONID) { // boutons file, logic et more
00916         i = idcom - FIRSTBUTTONID;  // index de l'element dans la fenetre principale
00917                 ind = GetWindowLong(hwnd, GWL_USERDATA);
00918                 
00919                 //printf("WIN %d\n", ind);
00920 
00921                 if (i<NEL[ind]) {
00922                   j = index[ind][i]; // index de l'element dans la liste des widgets
00923       
00924                   if (widgets[j].generic.type == BROWSER) {
00925                         OPENFILENAME ofn;       // common dialog box structure
00926             char filter[MAX_PATH];
00927             char *all_files = "All files";
00928             size_t l;
00929                 
00930                         
00931                         GetDlgItemText(hwnd, FIRSTCHAINID+i, (LPTSTR) buffer, 256);
00932                         
00933                         // Initialize OPENFILENAME
00934                         ZeroMemory(&ofn, sizeof(OPENFILENAME));
00935                         ofn.lStructSize = sizeof(OPENFILENAME);
00936                         ofn.hwndOwner = hwnd;
00937                         ofn.lpstrFile = buffer;
00938                         ofn.nMaxFile = sizeof(buffer);
00939             l = strlen( widgets[j].file.filter) + 1;
00940             strcpy( filter, widgets[j].file.filter);
00941             strcpy( filter + l, widgets[j].file.filter);
00942             strcpy( filter + 2 * l, all_files);
00943             strncpy( filter + 2 * l + strlen(all_files) + 1, "*.*", 5); /* 5 for 2 null terminated character */
00944                         ofn.lpstrFilter = filter;
00945                                 //"All\0*.*\0Text\0*.TXT\0";
00946                         ofn.lpstrTitle = widgets[j].file.label;
00947                         ofn.nFilterIndex = 1;
00948                         ofn.lpstrFileTitle = NULL;
00949                         ofn.nMaxFileTitle = 0;
00950                         ofn.lpstrInitialDir = NULL;
00951                         ofn.Flags = OFN_PATHMUSTEXIST;
00952                    
00953                         // Display the Open dialog box. 
00954 
00955                         if (GetOpenFileNamePreview(&ofn)==TRUE) {
00956                           if ((strstr(ofn.lpstrFile, ".") == 0) && (strlen(ofn.lpstrFilter) != 0))
00957                                 sprintf(buffer, "%s.%s", ofn.lpstrFile, ofn.lpstrFilter);
00958                           else sprintf(buffer, ofn.lpstrFile);
00959 
00960                           SetDlgItemText(hwnd, i+FIRSTCHAINID, (LPCTSTR) buffer);
00961 
00962                           for (i=0; i<nb_widgets; i++) {
00963                     if ((i != j) && (strncmp(widgets[i].generic.variable, 
00964                                                         widgets[j].generic.variable, 15) == 0))
00965                                   SendMessage(editwin[i], WM_SETTEXT, (WPARAM) 0, (LPARAM) buffer);
00966 
00967                           }
00968                         }
00969                   }
00970                   if (widgets[j].generic.type == LOGIC) {
00971                     hwndCtl = (HWND) lParam;
00972                     GetWindowText(hwndCtl, (LPTSTR) dumstr, 128);
00973                 if (strncmp(dumstr, "Yes", 3) == 0) sprintf(dumstr, "%s", "No"); 
00974                         else sprintf(dumstr, "%s", "Yes"); 
00975                     SetWindowText(hwndCtl, (LPCTSTR) dumstr);
00976                         
00977                         
00978                         for (i=0; i<nb_widgets; i++) {
00979                   if ((i != j) && (strncmp(widgets[i].generic.variable, 
00980                                                         widgets[j].generic.variable, 15) == 0)) {
00981                                 SetWindowText(logicwin[i], (LPCTSTR) dumstr);
00982                                 if (strncmp(dumstr, "Yes", 3) == 0)
00983                                   SendMessage(logicwin[i], BM_SETCHECK, (WPARAM) BST_CHECKED, (LPARAM) 0);
00984                                 else
00985                                   SendMessage(logicwin[i], BM_SETCHECK, (WPARAM) BST_UNCHECKED, (LPARAM) 0);
00986                           }
00987                         }
00988                          
00989                   }
00990                   if (widgets[j].generic.type == BUTTON) {
00991                     // More button pressed
00992                           //printf("Show window %d\n", IDWIN[j]);
00993                           ShowWindow(morewindows[IDWIN[j]], SW_SHOWNORMAL);
00994                   }
00995                 }
00996           }
00997           /*
00998           else if (idcom >= FIRSTCHAINID) {
00999                 i = idcom - FIRSTCHAINID;
01000                 ind = GetWindowLong(hwnd, GWL_USERDATA);
01001                 
01002                 if (i<NEL[ind]) {
01003                   j = index[ind][i];
01004                   
01005                   hwndCtl = (HWND) lParam;
01006                   ind = GetWindowLong(hwndCtl, GWL_USERDATA);
01007                   printf("win %d %d ind %d %d\n", editwin[j], hwndCtl, ind, j);
01008                   
01009                 }
01010           }*/
01011 
01012           else {
01013                 return DefWindowProc(hwnd, message, wParam, lParam);
01014           }
01015         break;
01016   
01017   case WM_SYSCOMMAND: 
01018         if (wParam == SC_CLOSE) {
01019           ind = GetWindowLong(hwnd, GWL_USERDATA);
01020           if (ind) {
01021                 ShowWindow(hwnd, SW_HIDE);
01022                 return 0;
01023           }
01024         }
01025         return (DefWindowProc(hwnd, message, wParam, lParam));
01026         break;
01027     
01028   default:
01029         return (DefWindowProc(hwnd, message, wParam, lParam));
01030         break;
01031   }
01032   return 0;
01033 }
01034 
01035 
01036 DWORD DialogFunc(LPDWORD lpdwParam) 
01037 {
01038     int i;
01039     MSG    msg;
01040     RECT rect;
01041     HMENU hMenu;
01042     int n_action = 0;
01043 
01044     InitCommonControls(); /* Necessary for TRACKBAR class */
01045 
01046     for (i = 0; i < nb_widgets; i++) {
01047         if ((widgets[i].generic.type == BUTTON) &&
01048             (widgets[i].button.showlength == 0)) {
01049                 // Elements de la barre de menu
01050                 num_actions[n_action] = i;
01051                 sprintf(command[n_action], "%s", widgets[i].button.command);
01052                 n_action++;
01053                 index[widgets[i].generic.window_id][NEL[widgets[i].generic.window_id]] = -1;
01054 
01055         }
01056         else {
01057             // Boutons associes a d'autres fenetres
01058             if (widgets[i].generic.type == BUTTON) {
01059                 IDWIN[i] = nmore; // Indice dans le tableau morewindows a partir du rang initial de l'element
01060                 INDEXCOM[nmore] = i; // Rang initial de l'element a partir de l'indice de fenetre
01061                 nmore++;
01062 
01063                 sprintf(dialoghelpname[nmore], "%s", widgets[i].button.helptxt);
01064             }
01065             // Rang du widget(bouton ou non dans la fenetre window_id
01066             index[widgets[i].generic.window_id][NEL[widgets[i].generic.window_id]]=i;
01067         }
01068         NEL[widgets[i].generic.window_id]++;
01069     }
01070 
01071     mainwidth =  labelwidth+horzpad+textwidth; 
01072     mainheight = itemheight*(NEL[0]-n_action);
01073 
01074     hMenu = LoadMenu(hInst, (LPCTSTR) "dialog");
01075     for (i = 0; i < n_action; i++) {
01076         if (i == 0)
01077             AppendMenu(hMenu, MFT_STRING | MFT_MENUBARBREAK, 1003+i,
01078             widgets[num_actions[i]].button.title);
01079         else
01080             AppendMenu(hMenu, MFT_STRING, 1003+i,
01081             widgets[num_actions[i]].button.title);
01082     }
01083 
01084     commandheight = (n_action) ? GetSystemMetrics(SM_CYMENU) - 1: 0;
01085 
01086     i = 0;
01087 
01088     // Tant que la fenetre principale est trop grande, on reinitialise la structure rect,
01089     // on diminue la taille de la viewport et on reajuste rect aux nouvelles dimensions
01090     // de la viewport
01091     do {
01092         rect.left = 0; rect.right = mainwidth;
01093         rect.top = 0; rect.bottom = itemheight*(NEL[0]-n_action-i)+commandheight /* 2nd line menu */;
01094         i++;
01095         AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, 1 /* Menu present*/);
01096     } while (rect.bottom - rect.top > screenheight);
01097 
01098     viewportheight = itemheight*(NEL[0]-n_action-i+1);
01099 
01100 
01101     SCB = (i > 1) ? 1 : 0; // Test si la scrollbar est necessaire 
01102 
01103 
01104     if (SCB) {
01105         FrameWnd = CreateWindowEx(
01106             WS_EX_CONTEXTHELP,
01107             TEXT("dialog"),
01108             TEXT(window_title[0]),
01109             WS_SYSMENU | WS_VSCROLL,
01110             CW_USEDEFAULT,
01111             CW_USEDEFAULT,
01112             rect.right - rect.left + 18,
01113             rect.bottom - rect.top - 2, /* 2nd line menu */
01114             NULL,
01115             hMenu,
01116             hInst,
01117             NULL);
01118 
01119 
01120         GetClientRect(FrameWnd, &rect);
01121         viewportheight = rect.bottom;
01122         //printf("Client height %d\n", viewportheight);  
01123 
01124         hWnd = CreateWindowEx(
01125             0,
01126             TEXT("dialog"),
01127             NULL,
01128             WS_CHILD | WS_VISIBLE,
01129             0,
01130             0,
01131             mainwidth,
01132             mainheight,
01133             (HWND) FrameWnd,
01134             NULL,
01135             hInst,
01136             NULL);
01137 
01138     }
01139     else {
01140 
01141         hWnd = CreateWindowEx(
01142             WS_EX_CONTEXTHELP,
01143             TEXT("dialog"),
01144             TEXT(window_title[0]),
01145             WS_SYSMENU,
01146             CW_USEDEFAULT,
01147             CW_USEDEFAULT,
01148             rect.right - rect.left,
01149             rect.bottom - rect.top - 2,
01150             NULL,
01151             hMenu,
01152             hInst,
01153             NULL);
01154     }
01155 
01156 
01157 
01158     /* printf("wind %d\n", hWnd);*/
01159     if (!hWnd) {
01160         printf("create window failed\n");
01161         return (FALSE);
01162     }
01163 
01164 
01165     for (i = 0; i<nmore; i++) {
01166         rect.left = 0; rect.right = mainwidth;
01167         rect.top = 0; rect.bottom = itemheight*(NEL[i+1]+1);
01168         AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, 0/* Mo Menu */);   
01169         morewindows[i] = CreateWindowEx(WS_EX_CONTEXTHELP,
01170             TEXT("dialog"),
01171             TEXT(widgets[INDEXCOM[i]].button.moretxt), WS_SYSMENU,
01172             CW_USEDEFAULT, CW_USEDEFAULT, rect.right-rect.left,rect.bottom-rect.top,
01173             NULL, NULL, hInst, NULL);
01174 
01175 
01176         dialog_help_win[i] = CreateWindowEx(WS_EX_TOOLWINDOW, TEXT("helpdialog"),
01177             (LPCTSTR) dialoghelpname[i],  
01178             WS_OVERLAPPEDWINDOW,
01179             CW_USEDEFAULT, CW_USEDEFAULT,
01180             600, 300,
01181             (HWND) NULL, (HMENU) NULL, hInst, (LPSTR) NULL);
01182     }
01183 
01184     dialog_help_win[nmore] = CreateWindowEx(WS_EX_TOOLWINDOW, TEXT("helpdialog"),
01185         (LPCTSTR) dialoghelpname[nmore],  
01186         WS_OVERLAPPEDWINDOW,
01187         CW_USEDEFAULT, CW_USEDEFAULT,
01188         600, 300,
01189         (HWND) NULL, (HMENU) NULL, hInst, (LPSTR) NULL);
01190 
01191 
01192 
01193     if (SCB) {
01194         ShowWindow(FrameWnd, SW_SHOWNORMAL);
01195         /*UpdateWindow(hWnd);*/
01196         SetForegroundWindow(FrameWnd);
01197     }
01198     else {
01199         ShowWindow(hWnd, SW_SHOWNORMAL);
01200         /*UpdateWindow(hWnd);*/
01201         SetForegroundWindow(hWnd);
01202     }
01203 
01204     sic_post_widget_created( );
01205 
01206     /* Loop getting messages and dispatching them. */
01207     while (GetMessage(&msg,NULL, 0, 0)) {
01208         TranslateMessage(&msg);
01209         DispatchMessage(&msg);
01210     }
01211     return 0;
01212 }
01213 
01214 
01215 void on_end_dialog( void)
01216 {
01217   int i;
01218 
01219   for (i=0; i<NSTRUCT; i++) {
01220         IDWIN[i] = 0;
01221   }
01222 
01223   for (i=0; i<MAX_NB_DIALOG; i++) {
01224         NEL[i] = 0; 
01225         curheight[i] = 0;
01226   }
01227 
01228   nb_widgets = 0; nmore=0;
01229   ncreate=0; nhelpwcreate=0;
01230   firstpass = 1;
01231   ERR_CODE = 0;
01232 }
01233 
01234 FILE *TempFile;
01235 char TempName[256];
01236 char detach_help[256];
01237 
01238 int lastid;
01239 
01240 HWND win_help = NULL, helpedit; // fenetre de help de la barre de menu
01241 HANDLE hMenuThrd;
01242 char *helptext;
01243 char dum[256];
01244 
01245 LRESULT APIENTRY MenuWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
01246 {
01247   UINT i;
01248   
01249   switch (message) {
01250   case WM_CREATE:
01251          if (strlen((LPSTR) lParam) != 0) {
01252           helpedit = CreateWindow("RICHEDIT", NULL,       
01253                                                            WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER |
01254                                                            ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY, 
01255                                                            0, 0, 0, 0, /* set size in WM_SIZE message  */ 
01256                                                        hwnd, (HMENU) NULL, hInst, (LPSTR) NULL);
01257       
01258           
01259           SendMessage(helpedit, WM_SETTEXT, 0, (LPARAM) helptext);
01260           free(helptext);
01261     }
01262         else {
01263           for (i = 0; i<NWINDOWS; i++) {
01264           if (detach_win[i].task == hMenuThrd)
01265                   detach_win[i].hWnd = hwnd;
01266               break;
01267       }
01268         }
01269         break;
01270   case WM_SIZE:
01271         if (hwnd == win_help)
01272           MoveWindow(helpedit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE /* Repaint */);
01273         break;
01274   case WM_CLOSE:
01275         ShowWindow(hwnd, SW_HIDE);
01276         return 0;
01277   case WM_COMMAND:{
01278         WORD idcom = LOWORD(wParam) - 100;
01279         struct _stat statb;
01280         FILE *fd;
01281         size_t numread;
01282 
01283     if ((idcom>=0) && (idcom < lastid)) {
01284                 sic_post_command_text(menucommand[idcom]);
01285         }
01286         if (idcom == lastid) {
01287 
01288         for (i = 0; i<NWINDOWS; i++) {
01289             if (detach_win[i].hWnd == hwnd)
01290                 break;
01291         }
01292         if (i == NWINDOWS)
01293             break;
01294 
01295                 //printf("%d %s\n", i, detach_win[i].name);
01296                 if (_stat(detach_win[i].name, &statb) == -1) {
01297           helptext = (char *) malloc(80*sizeof(char));
01298           sprintf(helptext, "%s", "Not found ..."); 
01299         }
01300         else {
01301           fd = fopen(detach_win[i].name, "r");
01302           helptext = (char *) malloc((unsigned)(statb.st_size+1));
01303           numread = fread(helptext, sizeof(char), statb.st_size+1, fd);
01304                   //printf( "Number of bytes read = %d\n", numread );
01305 
01306           helptext[statb.st_size] = 0;
01307           fclose(fd);
01308         }
01309         if (win_help == NULL) {
01310                   win_help = CreateWindowEx(WS_EX_TOOLWINDOW, TEXT("sicmenu"), detach_win[i].name,  
01311                                                             WS_OVERLAPPEDWINDOW,
01312                                                                         CW_USEDEFAULT, CW_USEDEFAULT,
01313                                                                         600, 300,
01314                                                                         (HWND) NULL, (HMENU) NULL, hInst, (LPSTR) "helptest");
01315               ShowWindow(win_help, SW_SHOWNORMAL);
01316                   SetForegroundWindow(win_help);
01317             }
01318                 else {
01319                   ShowWindow(win_help, SW_SHOW);
01320           SetForegroundWindow(win_help);
01321                 }
01322 
01323         
01324         }
01325         
01326         
01327 
01328         }
01329         break;
01330     
01331   default:
01332         return (DefWindowProc(hwnd, message, wParam, lParam));
01333         break;
01334   }
01335   return 0;
01336 
01337 }
01338 
01339 
01340 void update_variable( sic_widget_def_t *widget)
01341 {
01342         
01343         int i, j;
01344         char dum[80];
01345 //      FILE *fsg; // SG
01346 //    fsg = fopen("dialog.txt","w");  // SG
01347 //      fprintf(fsg,"UPDATE %s %d\n", widget->generic.variable, strlen(widget->generic.variable)); //SG
01348 
01349         for (i = 0; i < NSTRUCT; i++) {
01350                 
01351                 if (widgets[i].generic.type == 0) break;
01352                 //printf("%s %d\n", 
01353                 //      widgets[i].generic.variable, strlen(widgets[i].generic.variable));
01354                 if (strncmp(widget->generic.variable, widgets[i].generic.variable, 15) == 0) {
01355                         switch(widgets[i].generic.type) {
01356                         case SLIDER:
01357                                 widgets[i].slider.uservalue = 
01358                                         min(max(widgets[i].slider.min, widget->slider.uservalue), 
01359                                                 widgets[i].slider.min + widgets[i].slider.width);
01360                                 SendMessage(sliderwin[i], TBM_SETPOS, (WPARAM) TRUE,
01361                                 (LPARAM) (100.*(widgets[i].slider.uservalue-widgets[i].slider.min)/widgets[i].slider.width));
01362                                 sprintf(dum, "%g", widget->slider.uservalue);
01363                                 SendMessage(editwin[i], WM_SETTEXT, 0, (LPARAM) dum);
01364                                 break;
01365                         case LOGIC:
01366                                 widgets[i].logic.userlogic = widget->logic.userlogic;
01367                                 
01368                                 if (widget->logic.userlogic) {
01369                                   SetWindowText(logicwin[i], (LPCTSTR) "Yes");
01370                                   SendMessage(logicwin[i], BM_SETCHECK, (WPARAM) BST_CHECKED, (LPARAM) 0);
01371                                 }
01372                                 else {
01373                                   SetWindowText(logicwin[i], (LPCTSTR) "No");
01374                                   SendMessage(logicwin[i], BM_SETCHECK, (WPARAM) BST_UNCHECKED, (LPARAM) 0);
01375                                 }
01376                                 break;
01377                         case CHAIN:
01378                                 
01379                                 strcpy(widgets[i].chain.userchain, widget->chain.userchain);
01380                                 SendMessage(editwin[i], WM_SETTEXT, 0, (LPARAM) widget->chain.userchain);
01381                                 break;
01382                         case CHOICE:
01383                                 
01384                                 // fprintf(fsg,"SendMessage %d %d %s\n", i, widgets[i].choice.mode, widget->choice.userchoice); //SG
01385                                 // Si les choix sont non editables, on verifie si la mise a jour est valable
01386 
01387                                 if (widgets[i].choice.mode == -1) {
01388 //                                      fprintf(fsg,"CHOICE %d\n", strlen(widget->choice.userchoice)); // SG
01389                                         for (j = 0; j<widgets[i].choice.nchoices; j++) { 
01390                                                 if (strncmp(widget->choice.userchoice,
01391                                                                         widgets[i].choice.choices[j],
01392                                                                         strlen(widgets[i].choice.choices[j])) == 0) {
01393                                                         SendMessage(editwin[i], CB_SETCURSEL, (WPARAM)j, (LPARAM)0);
01394                                                         strcpy(widgets[i].choice.userchoice, widget->choice.userchoice);
01395                                                         break;
01396                                                 }       
01397                                         }
01398                                 }       
01399 
01400                                 if (widgets[i].choice.mode == 1) { // Choix non editable indexe
01401                                         if ((atoi(widget->choice.userchoice) >= 1) && (atoi(widget->choice.userchoice) <= widgets[i].choice.nchoices)) {
01402 //                                              fprintf(fsg,"INDEXE %d\n", strlen(widget->choice.userchoice)); // SG
01403                                                 SendMessage(editwin[i], CB_SETCURSEL, (WPARAM) (atoi(widget->choice.userchoice) - 1), (LPARAM)0);
01404                                                 strcpy(widgets[i].choice.userchoice,
01405                                                            widgets[i].choice.choices[atoi(widget->choice.userchoice)-1]);
01406                                         }
01407                                 }
01408                                 
01409                                 if (widgets[i].choice.mode == 0)  { // Choix editable
01410                                         SendMessage(editwin[i], WM_SETTEXT, 0, (LPARAM) widget->choice.userchoice);
01411                                         strcpy(widgets[i].choice.userchoice, widget->choice.userchoice);
01412                                 }
01413                                 break;
01414                         case BROWSER:
01415                                 strcpy(widgets[i].file.userchain, widget->file.userchain);
01416                                 SendMessage(editwin[i], WM_SETTEXT, 0, (LPARAM) widget->file.userchain);
01417                                 break;
01418                         }
01419                 }
01420         }
01421 //      fclose(fsg) ; // SG
01422 }
01423 
01424 DWORD MenuFunc(const char *tempfilename) 
01425 {
01426   
01427   HMENU hMenu, hPopupMenu;
01428   HWND win_menu;
01429   RECT rect;
01430   MSG msg;
01431   char wintitle[256]; 
01432   char winhelpfile[256];
01433   char line[256];
01434   char label[256];
01435   int i = 0;
01436   FILE *fd;
01437   //HDC hdcScreen;
01438   fd = fopen(tempfilename, "r");
01439 
01440   fgets(line, 256, fd);
01441   strncpy(wintitle, line, strlen(line) - 1);
01442   wintitle[strlen(line)-1] = 0 ; // SG, otherwise bad trailing characters
01443   fgets(line, 256, fd);
01444   strncpy(winhelpfile, line, strlen(line) - 1);
01445   winhelpfile[strlen(line)-1] = 0 ; // SG, otherwise bad trailing characters
01446   
01447   hMenu = CreateMenu();
01448 
01449   while (fgets(line, 256, fd) != NULL) {
01450     if (strncmp(line, "MENU", 4) == 0) {
01451       fgets(line, 256, fd);
01452 
01453       strncpy(label, line, strlen(line)-1);
01454       label[strlen(line) - 1] = 0;
01455 
01456           hPopupMenu = CreatePopupMenu();
01457           AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT_PTR) hPopupMenu, label);
01458 
01459           for (;;) {
01460             fgets(line, 256, fd);
01461             if (strncmp(line, "ENDMENU", 7) == 0) break;
01462 
01463             strncpy(label, line, strlen(line)-1);
01464             label[strlen(line) - 1] = 0;
01465             AppendMenu(hPopupMenu, MFT_STRING, 100+i, label);
01466 
01467                 fgets(line, 256, fd);
01468                 strncpy(menucommand[i], line, strlen(line)-1);
01469                 menucommand[i][strlen(line) - 1] = 0;
01470 
01471                 i++;
01472       }
01473         }
01474         else {
01475           strncpy(label, line, strlen(line)-1);
01476       label[strlen(line) - 1] = 0;
01477       AppendMenu(hMenu, MFT_STRING, 100+i, label);
01478 
01479       fgets(line, 256, fd);
01480           strncpy(menucommand[i], line, strlen(line)-1);
01481       menucommand[i][strlen(line) - 1] = 0;
01482 
01483           i++;
01484     }
01485   }
01486         
01487   AppendMenu(hMenu, MFT_STRING, 100+i, "Help");
01488   lastid = i;
01489   fclose(fd);
01490   if (unlink(tempfilename) == -1) MyErrorHandler("_unlink", "MenuFunc");
01491   
01492   rect.left = 0; rect.right = screenwidth -2*GetSystemMetrics(SM_CXFIXEDFRAME); rect.top = rect.bottom = 0;
01493   AdjustWindowRect(&rect, WS_CAPTION, 1 /* Menu present*/);
01494  
01495   win_menu = CreateWindowEx(WS_EX_TOOLWINDOW, TEXT("sicmenu"), wintitle,  
01496                                                   WS_CAPTION | WS_SYSMENU,
01497                                               0, 0,
01498                                                   rect.right - rect.left, rect.bottom - rect.top,
01499                                                   (HWND) NULL, hMenu, hInst, (LPSTR) NULL);
01500  
01501   ShowWindow(win_menu, SW_SHOWNORMAL);
01502   SetForegroundWindow(win_menu);
01503   //UpdateWindow(win_menu);
01504 
01505   while (GetMessage(&msg, NULL, 0, 0)) { 
01506    TranslateMessage(&msg);
01507    DispatchMessage(&msg); 
01508   }
01509   
01510   return 1;
01511 }
01512 
01513 BOOL APIENTRY DllMain(HANDLE hInstance, 
01514                                           DWORD ul_reason_being_called, LPVOID lpReserved)
01515 {
01516   WNDCLASS  wc;
01517   HDC hdcScreen;
01518   int i;
01519 
01520   switch( ul_reason_being_called ) {
01521     case DLL_PROCESS_ATTACH:
01522         //CRT_INIT();
01523         //printf("Process attachment to dialogs DLL\n");
01524         
01525         hInst = hInstance;
01526         root_win = GetDesktopWindow();
01527 
01528 
01529     wc.style = 0;
01530         wc.cbClsExtra = 0;
01531     wc.cbWndExtra = 0;
01532     wc.hInstance = hInst;
01533     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
01534     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
01535     wc.hbrBackground = GetStockObject(LTGRAY_BRUSH);
01536         wc.lpszMenuName = NULL; 
01537         
01538         wc.lpfnWndProc = (WNDPROC)MenuWndProc;
01539         wc.lpszClassName = TEXT("sicmenu");
01540         if (!RegisterClass(&wc)) MyErrorHandler("RegisterClass", "DllMain");
01541         
01542     wc.lpfnWndProc = (WNDPROC)MainWndProc;
01543         wc.lpszClassName = TEXT("dialog");
01544         if (!RegisterClass(&wc)) MyErrorHandler("RegisterClass", "DllMain");
01545 
01546         wc.lpfnWndProc = (WNDPROC)HelpWndProc;
01547         wc.lpszClassName = TEXT("helpdialog");
01548         if (!RegisterClass(&wc)) MyErrorHandler("RegisterClass", "DllMain");
01549 
01550         fontheight = GetDialogBaseUnits();
01551         fontheight= fontheight>>16;
01552         //printf("Font Height %d\n", fontheight);
01553     /*
01554         if (SystemParametersInfo(SPI_GETBEEP, 0, &beep_on, 0))
01555                 printf("BEEP %d\n", beep_on); 
01556     
01557           */
01558         DlgFont = GetStockObject(DEFAULT_GUI_FONT);
01559         if (DlgFont == NULL) printf("Could'nt get DEFAULT_GUI_FONT\n");
01560         VarFont = GetStockObject(ANSI_VAR_FONT);
01561         if (VarFont == NULL) printf("Could'nt get ANSI_VAR_FONT\n");
01562         
01563         hdcScreen = CreateDC("DISPLAY", (LPCSTR) NULL, 
01564             (LPCSTR) NULL, (CONST DEVMODE *) NULL); 
01565         
01566     screenheight = GetDeviceCaps(hdcScreen, VERTRES);
01567         screenwidth = GetDeviceCaps(hdcScreen, HORZRES);
01568         //printf("screenwidth %d screenheight %d\n", screenwidth, screenheight);
01569     DeleteDC(hdcScreen);
01570 
01571         for (i = 0; i < NWINDOWS; i++) strcpy(detach_win[i].name, "NONE");
01572         
01573         for (i = 0; i < NMENUCOMMAND; i++) { 
01574                 command[i] = (char *) malloc(256*sizeof(char));
01575                 menucommand[i] = (char *) malloc(256*sizeof(char));
01576         }
01577         for (i = 0; i < MAX_NB_DIALOG; i++) dialoghelpname[i] = (char *) malloc(80*sizeof(char));
01578 
01579         LoadLibrary("RICHED32.DLL");
01580         break;
01581 /*
01582     case DLL_PROCESS_DETACH:
01583         CRT_INIT();
01584         break;
01585         case DLL_THREAD_ATTACH:
01586     break;
01587         case DLL_THREAD_DETACH:
01588     break;*/
01589 
01590   }
01591   return 1;
01592 }

Generated on Tue Mar 13 15:15:40 2007 for SIC by  doxygen 1.5.1