/* Preferences.c: set user preferences
 *
 * This file is part of Aileron, a WINGs email client. 
 *
 * Copyright (c) 1999-2000 Nwanua Elumeze
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#define FRAME_TOP  50
#define FRAME_LEFT 0
#define FRAME_WIDTH 290
#define FRAME_HEIGHT 300


#include "Aileron.h"

typedef struct {
 char *key;
 char *value;
} header;


struct {
  WMWindow  *window;
  
  WMPopUpButton *mainp;
  Bool    saved;
  WMButton  *revertb, *setb, *balloonb;
  
  WMFrame   *generalf;
  WMFrame   *newmailf;
  WMButton  *manualb, *autob;
  WMTextField *queryt;
  WMButton  *soundb, *soundsetb;
  WMLabel *soundl;
  WMFrame   *mboxwinf;
  WMButton  *msgtotb, *msgsizeb, *msgdelb;
  WMFrame   *mailappf;
  WMButton  *compactb, *geomb;
  
  
  WMFrame   *composerf;
  WMFrame   *composef;
  WMFrame   *replyf;
  WMButton  *replycopyb;
  WMTextField *fromt, *replyt; 
  WMFrame   *optionsf;
  WMButton  *archiveb, *sendb;
  WMButton  *xfaceb, *xfacesetb, *sigb, *sigsetb;
  WMLabel   *xfacel, *sigl; 
  
  WMTabView *tabv;
  WMFrame   *headersf;
  WMArray   *headersa;
  WMList   *showl;
  WMTextField *keyt;
  
  WMButton  *keys[9], *vals[9], *pressed;
  char      *keytext[9], *valtext[9], **pressedtext;
  WMTextField *hiddenkey;
  
  WMButton  *quoteb;
  WMTextField *introt;
  WMTextField *prefixt;
  WMPopUpButton  *datep;
  
  WMFrame   *mimef;
  WMButton  *saveattachb;
  WMLabel   *attachdirl, *handlerl;
  WMList   *mimel;
  WMButton  *mimesaveb, *mimehandleb, *mimehandlesetb;

  WMFrame   *networkf, *retrievalf, *outserverf;
  WMButton  *secureb, *fetchb, *autonextb, *offlineb, *sslb;
  WMPopUpButton  *outhowp;
  WMLabel *outhowl1, *outhowl2, *outhowl3;
  WMTextField *outservert, *outnamet, *outpasst;

} PrefsPanel;

enum { PPGeneral=0, PPCompose, PPHeaders, PPMIME, PPNetwork, PPOutHeaders};
enum { OHSMTP=0, OHApp, OHServer};

extern char *getNameFromPath(const char *cpath);

static void updateForItemType(int which)
{
  WMSetUDIntegerForKey(Aileron.defaultsDB, which, "PreferencePage");

  if (which == PPGeneral)
    WMMapWidget(PrefsPanel.generalf);
  else
    WMUnmapWidget(PrefsPanel.generalf);
  
  if (which == PPCompose)
    WMMapWidget(PrefsPanel.composerf);
  else
    WMUnmapWidget(PrefsPanel.composerf);
  
  if (which == PPHeaders || which == PPOutHeaders) {
    if(which == PPOutHeaders)  
      WMSelectLastTabViewItem(PrefsPanel.tabv);

    WMMapWidget(PrefsPanel.headersf);
  } else {
    WMUnmapWidget(PrefsPanel.headersf);
  }
  
  if (which == PPMIME)
    WMMapWidget(PrefsPanel.mimef);
  else
    WMUnmapWidget(PrefsPanel.mimef);
  
  if (which == PPNetwork)
    WMMapWidget(PrefsPanel.networkf);
  else
    WMUnmapWidget(PrefsPanel.networkf);
}

static void
changeForItemType(WMWidget *w, void *data)
{
  updateForItemType(
    (int) WMGetPopUpButtonSelectedItem((WMPopUpButton*) w));
}

void didSelectItem(struct WMTabViewDelegate *self, WMTabView *tabView,
  WMTabViewItem *item)
{
  WMSetUDIntegerForKey(Aileron.defaultsDB, 
  (WMGetTabViewItemIdentifier(item)==0)?2:5, "PreferencePage");
}

static WMTabViewDelegate tabViewDelegate = {
  NULL,
  NULL,
  didSelectItem,
  NULL,
  NULL
};



static void updateOutHowForItemType(int which)
{
  WMSetUDIntegerForKey(Aileron.defaultsDB, which, "OutgoingMailAgent");
// always has 6 fields which may be blank...
// if set, always leave it so you can fill out textfield...
//MailAgent = {SMTP, NIL, NIL, NIL, NIL, NIL}
//MailAgent = {Agent, "/thi/here ... %d", NIL, NIL, NIL, NIL}
//MailAgent = {app, "/thi/here ... %d", foo.bar.edu, me, boo, secure}

  WMSetPopUpButtonSelectedItem(PrefsPanel.outhowp,which);

  switch (which) { 

    case OHSMTP:
      WMUnmapWidget(PrefsPanel.outhowl1);
      WMUnmapWidget(PrefsPanel.outhowl2);
      WMUnmapWidget(PrefsPanel.outhowl3);
      WMUnmapWidget(PrefsPanel.outnamet);
      WMUnmapWidget(PrefsPanel.outservert);
      WMUnmapWidget(PrefsPanel.outpasst);
      WMUnmapWidget(PrefsPanel.sslb);
    break;

    case OHApp:
      WMSetLabelText(PrefsPanel.outhowl1, "Agent:");
      WMSetBalloonTextForView("Enter the application path and arguments",
        WMWidgetView(PrefsPanel.outhowl1));
      WMMapWidget(PrefsPanel.outhowl1);
      WMUnmapWidget(PrefsPanel.outhowl2);
      WMUnmapWidget(PrefsPanel.outhowl3);
      WMMapWidget(PrefsPanel.outservert);
      WMUnmapWidget(PrefsPanel.outnamet);
      WMUnmapWidget(PrefsPanel.outpasst);
      WMUnmapWidget(PrefsPanel.sslb);
    break;

    case OHServer:
      WMSetLabelText(PrefsPanel.outhowl1, "Server:");
      WMSetBalloonTextForView("Enter the machine name of the server",
        WMWidgetView(PrefsPanel.outhowl1));
      WMMapWidget(PrefsPanel.outhowl1);
      WMMapWidget(PrefsPanel.outhowl2);
      WMMapWidget(PrefsPanel.outhowl3);
      WMMapWidget(PrefsPanel.outservert);
      WMMapWidget(PrefsPanel.outnamet);
      WMMapWidget(PrefsPanel.outpasst);
      WMMapWidget(PrefsPanel.sslb);
  }

}
 

static void
changeOutHowForItemType(WMWidget *w, void *data)
{
  updateOutHowForItemType(
    (int) WMGetPopUpButtonSelectedItem((WMPopUpButton*) w));
}

static void
textGenericAction(XEvent *event, void *client)
{
  PrefsPanel.saved = False;
  WMSetWindowDocumentEdited(PrefsPanel.window, True);
}


static void buttonExpertKeyCB(WMWidget *w, void *client)
{
  WMPoint pos;
  WMSize size;
  int i=0;
  WMButton *b = NULL;

    
/*      
    if (event->type != FocusOut || event->type != KeyPress ||
        !WMIsDoubleClick(event))
        return;
*/
  WMSetFocusToWidget(PrefsPanel.hiddenkey);

  if(PrefsPanel.pressed && PrefsPanel.pressedtext) { 
    wfree(*PrefsPanel.pressedtext);
    *PrefsPanel.pressedtext = WMGetTextFieldText(PrefsPanel.hiddenkey);
    WMSetButtonText(PrefsPanel.pressed, *PrefsPanel.pressedtext);
  } 

  PrefsPanel.pressed = (WMButton *) w;
  WMMapWidget(PrefsPanel.hiddenkey);

  for(i=0; i<=8; i++) {

    if ( w == PrefsPanel.keys[i]) {
      b = PrefsPanel.keys[i];
      PrefsPanel.pressedtext = &PrefsPanel.keytext[i];
      break;
    }   
        
    if ( w == PrefsPanel.vals[i]) {
      b = PrefsPanel.vals[i];
      PrefsPanel.pressedtext = &PrefsPanel.valtext[i];
      break;
    }   
  }

  if (!b) {
    WMMapWidget(PrefsPanel.pressed);
    WMUnmapWidget(PrefsPanel.hiddenkey);
    PrefsPanel.pressed = NULL;
    PrefsPanel.pressedtext = NULL;
    return;
  }
    
  pos = WMGetViewPosition(WMWidgetView(b));
  size = WMGetViewSize(WMWidgetView(b));
  WMResizeWidget(PrefsPanel.hiddenkey, size.width-2, size.height-3);
  WMMoveWidget(PrefsPanel.hiddenkey, pos.x+2, pos.y+1);
  WMMapWidget(PrefsPanel.hiddenkey);
  WMSetTextFieldText(PrefsPanel.hiddenkey, *PrefsPanel.pressedtext);

#if 0
    {
        XEvent event;
        event.xfocus.mode = NotifyNormal;
        event.xfocus.type = FocusOut;
        event.type = ButtonPress;
    
        W_DispatchMessage(WMWidgetView(PrefsPanel.hiddenkey), &event);
    }
#endif


}
    

static void
handleHiddenKeyPress(XEvent *event, void *client)
{   
    
  switch(event->type) {   
            
    case KeyPress:
      PrefsPanel.saved = False;
      WMSetWindowDocumentEdited(PrefsPanel.window, True);

      if (event->xkey.keycode !=
        XKeysymToKeycode(Aileron.display, XK_Return)) {
        return;
      }
 
      
    case FocusOut: 
      if(PrefsPanel.pressed) {
        WMUnmapWidget(PrefsPanel.hiddenkey);
        WMMapWidget(PrefsPanel.pressed);
        wfree(*PrefsPanel.pressedtext);
        *PrefsPanel.pressedtext = WMGetTextFieldText(PrefsPanel.hiddenkey);
        WMSetButtonText(PrefsPanel.pressed, *PrefsPanel.pressedtext);
        PrefsPanel.pressed = NULL;
        PrefsPanel.pressedtext = NULL;
      } 
 
  }

            
}


static void buttonGenericAction(WMWidget *w, void *client)
{   
  PrefsPanel.saved = False;
  WMSetWindowDocumentEdited(PrefsPanel.window, True);
        
  WMSetButtonEnabled(PrefsPanel.soundsetb,
    WMGetButtonSelected(PrefsPanel.soundb));
        
  WMSetButtonEnabled(PrefsPanel.xfacesetb,
    WMGetButtonSelected(PrefsPanel.xfaceb));

  WMSetButtonEnabled(PrefsPanel.sigsetb,
    WMGetButtonSelected(PrefsPanel.sigb));
}


static void showListAction(WMWidget *w, void *client)
{
  WMListItem *item = WMGetListSelectedItem(PrefsPanel.showl);
  WMSetTextFieldText(PrefsPanel.keyt, item->text);
}


static void addHeaderToShow(WMWidget *w, void *client)
{
  char *text = WMGetTextFieldText(PrefsPanel.keyt);

  if (strlen(text)>0) {
    
    WMAddListItem(PrefsPanel.showl, text);
            
    WMUnselectAllListItems(PrefsPanel.showl);
    WMSelectListItem(PrefsPanel.showl, 
      WMGetListNumberOfRows(PrefsPanel.showl)-1);
            
    WMSetListPosition(PrefsPanel.showl,
      WMGetListNumberOfRows(PrefsPanel.showl));
            
    WMSetTextFieldText(PrefsPanel.keyt, NULL);
  }   

  wfree(text);
}   


static void removeHeaderToShow(WMWidget *w, void *client)
{
  WMArray *items = WMGetListSelectedItems(PrefsPanel.showl);
  int i=0, c;
    
  if (items) {
    c = WMGetArrayItemCount(items)-1;
    while(WMGetListSelectedItem(PrefsPanel.showl)) {
      if (i==c) {
        WMListItem *item = WMGetListSelectedItem(PrefsPanel.showl);
        if (item && item->text)
          WMSetTextFieldText(PrefsPanel.keyt, item->text);
      }       
            
      WMRemoveListItem(PrefsPanel.showl,
        WMGetListSelectedItemRow(PrefsPanel.showl));
      i++;
    }   
        
  }   
}   



static void
shuffleList(XEvent *event, void *client)
{ 
  KeySym ksym;
  WMList *l; 
  WMListItem *item;
  char *text;
  int r, n, w;
  
  w = (int) WMGetPopUpButtonSelectedItem(PrefsPanel.mainp);
  if (w != PPMIME && w != PPHeaders)  
    return;

  XLookupString(&event->xkey, NULL, 0, &ksym, NULL);
  if (ksym != XK_Up && ksym != XK_Down)
    return;
    
  l = (w==PPMIME)? PrefsPanel.mimel : PrefsPanel.showl;
  
  r = WMGetListSelectedItemRow(l);
  n = WMGetListNumberOfRows(l);
  if ( ( (ksym==XK_Up)? (r<1) : (r+1>=n)) || r == WLNotFound)
    return;
    
  item = WMGetListSelectedItem(l);
  if (!item || !item->text)
    return; 
    
  text = wstrdup(item->text);
  WMUnselectAllListItems(l);
  WMRemoveListItem(l, r);
  WMInsertListItem(l, ((ksym==XK_Up)?(r-1):(r+1)), text);
  WMSelectListItem(l, ((ksym==XK_Up)?(r-1):(r+1)));
  
  if (w==PPMIME) {
    char *lt = getNameFromPath(text);

    WMSetLabelText(PrefsPanel.handlerl, lt);
    wfree(lt);
    
  } else
    WMSetTextFieldText(PrefsPanel.keyt, text);
    
  wfree(text);
} 


static void
fillMIMETypes(void)
{ 
  int i, nelem;
  WMListItem *item;
  WMPropList *dic, *keys, *key, *val;
  char *option;
  
  WMClearList(PrefsPanel.mimel);
    
  if (!(dic = WMGetUDObjectForKey(Aileron.defaultsDB, "MIMEHandlers")))
    goto _fillMIME;
  
  if (!(keys = WMGetPLDictionaryKeys(dic)))
    goto _fillMIME;
  
  nelem = WMGetPropListItemCount(keys);
  for(i=0; i<nelem; i++) {
    if ((key = WMGetFromPLArray(keys, i))) {
      if (!(option = WMGetFromPLString(key)))
        continue;
      item = WMInsertListItem(PrefsPanel.mimel, -1, option);
      val = WMGetFromPLDictionary(dic, key);
      option =  WMGetFromPLString(val);
      if (val && option) { 
        if (strcmp(option, "None"))
          item->clientData = wstrdup(WMGetFromPLString(val));
        else
        item->clientData = NULL;
       
      }
    }
  }
  return;

 
 _fillMIME:
    item = WMInsertListItem(PrefsPanel.mimel, -1, "application/postscript");
    item->clientData = wstrdup("/usr/X11R6/bin/gv"); 
    item = WMInsertListItem(PrefsPanel.mimel, -1, "video/mpeg");
    item->clientData = NULL;
}   


static void setMIMEType(WMWidget *w, void *client)
{ 
  char *path, *lt;
  struct stat status;
  WMOpenPanel *panel;
  WMListItem *item = WMGetListSelectedItem(PrefsPanel.mimel);
  
  if (!item)
    return;
  
  if (w == PrefsPanel.mimesaveb) {
    if (item->clientData)
      wfree(item->clientData);
    item->clientData = NULL;
    PrefsPanel.saved = False;
    WMSetWindowDocumentEdited(PrefsPanel.window, True);
    WMSetLabelText(PrefsPanel.handlerl, NULL);
    return;
  } 
    
  panel = WMGetOpenPanel(Aileron.screen);
  
  WMSetFilePanelAutoCompletion(panel, True); 
  if (WMRunModalFilePanelForDirectory(panel, PrefsPanel.window,
   item->clientData?item->clientData:"/usr/X11R6/bin/", NULL, NULL)) {
    
  
    PrefsPanel.saved = False;
    WMSetWindowDocumentEdited(PrefsPanel.window, True);
    path = WMGetFilePanelFileName(panel);
    if (path) {
      if (stat(path, &status) != 0) {
        RunAlertPanel("Set MIME Handler",
          "The executable does not exist.",
          "I understand", NULL, NULL);
        WMFreeFilePanel(panel);
        return;
      }   
        
      if (S_ISDIR(status.st_mode) || !(status.st_mode & S_IXUSR)) {
        RunAlertPanel("Set MIME Handler",
          "The chosen entity is not executable by you.",
          "I understand", NULL, NULL);
        WMFreeFilePanel(panel);
        return;
      }   
        
      lt = getNameFromPath(path);
      WMSetLabelText(PrefsPanel.handlerl, lt);
      wfree(lt);
      
      if (item->clientData)
        wfree(item->clientData);
      item->clientData = wstrdup(path);
    } 
  }     
  WMFreeFilePanel(panel);
}   
  

static void setAttachDir(WMWidget *w, void *client)
{

  char *path;
  
  path = SetEntity((WMWindow *)client, "SaveAttachmentsTo", 1,
      "Set Attachment Folder", 0);
  if(!path)
    return;
  WMSetLabelText(PrefsPanel.attachdirl, path);
  wfree(path);
  PrefsPanel.saved = False;
  WMSetWindowDocumentEdited(PrefsPanel.window, True);
}     
    
static void setSigFile(WMWidget *w, void *client)
{
  char *path;

  path = SetEntity((WMWindow *)client, "Signature", 0,
    "Set Signature File", 0);

  WMSetLabelText(PrefsPanel.sigl, path);
  wfree(path);
}

static void balloonAction(WMWidget *w, void *client)
{ 
  int en = WMGetButtonSelected(PrefsPanel.balloonb);
  WMSetBalloonEnabled(Aileron.screen, en);
  WMSetUDBoolForKey(Aileron.defaultsDB, en, "BalloonHelp");
}   
  

static void MIMEListAction(WMWidget *w, void *client)
{
  WMListItem *item = WMGetListSelectedItem(PrefsPanel.mimel);
  char *lt;
  
  
  if (item->clientData) {
    lt = getNameFromPath(item->clientData);
    WMSetLabelText(PrefsPanel.handlerl, lt);
    wfree(lt);
    WMPerformButtonClick(PrefsPanel.mimehandleb);
    
  } else {
    WMSetLabelText(PrefsPanel.handlerl, NULL);
    WMPerformButtonClick(PrefsPanel.mimesaveb);
  } 
  
} 
    
static void MIMEListDAction(WMWidget *w, void *client)
{
  setMIMEType(PrefsPanel.mimehandlesetb, NULL);
} 


static void
closePanel(WMWidget *self, void *client)
{
  WMSetUDIntegerForKey(Aileron.defaultsDB,
    (int) WMGetPopUpButtonSelectedItem(PrefsPanel.mainp),
    "PreferencePage");

  if (!PrefsPanel.saved) {
     if(!RunAlertPanel("Preferences",
       "You have modified, but not yet saved your preferences.\n"
       "If you quit Aileron without saving, you will lose all changes.\n",
       "Don't Close", "Close", NULL))
        return;
  }
  
  WMUnmapWidget(PrefsPanel.window);
}


static void
fillHeaderToShow(void)
{ 
  WMPropList *array, *val;
  int i, num;
  WMList *l = PrefsPanel.showl;
    
  WMClearList(l);
  
  array = WMGetUDObjectForKey(Aileron.defaultsDB, "HeadersToShow");
  if (!array || !WMIsPLArray(array)) 
    array = WMGetUDObjectForKey(Aileron.defaultsDB, "HiddenHeaders");
    
  if (!array || !WMIsPLArray(array)) {
    WMInsertListItem(l, -1, "To");
    WMInsertListItem(l, -1, "Date");
    WMInsertListItem(l, -1, "From");
    WMInsertListItem(l, -1, "Return-Path");
  } else {
    num = WMGetPropListItemCount(array);
    for (i=0; i<num; i++) {
      val = WMGetFromPLArray(array, i);
      WMInsertListItem(l, -1, WMGetFromPLString(val));
    }
 }
 
 array = WMGetUDObjectForKey(Aileron.defaultsDB, "HiddenHeaders");
 if (array && WMIsPLArray(array))
   WMRemoveUDObjectForKey(Aileron.defaultsDB, "HiddenHeaders");

}


static void
saveHeaderToShow(void)
{
  WMPropList *headerList, *elem;
  WMListItem *item;
  int i, rows;

  headerList = WMCreatePLArray(NULL);
  rows = WMGetListNumberOfRows(PrefsPanel.showl);
  for(i=0; i<rows; i++) {
    item = WMGetListItem(PrefsPanel.showl, i);
    elem = WMCreatePLString(item->text);
    WMAddToPLArray(headerList, elem);
  }
  WMSetUDObjectForKey(Aileron.defaultsDB, headerList, "HeadersToShow");
  WMReleasePropList(headerList);
}


static void
fillOutGoingHeaders(void)
{ 
 // WMPropList *array, *val;
printf("fillOutGoingHeaders \n");
}


static void  
fillOutHow(void)
{ 
  int which;
  char* option = NULL;


  which = WMGetUDIntegerForKey(Aileron.defaultsDB, "OutgoingMailAgent");

  switch (which) {

    case OHSMTP:
    break;

    case OHServer:
      option = WMGetUDStringForKey(Aileron.defaultsDB, "OutgoingName");
      if (option)
	    WMSetTextFieldText(PrefsPanel.outnamet, option);

      option = WMGetUDStringForKey(Aileron.defaultsDB, "OutgoingPasswd");
      if (option)
	    WMSetTextFieldText(PrefsPanel.outpasst, option);
      WMMapWidget(PrefsPanel.sslb);

    case OHApp:
      option = WMGetUDStringForKey(Aileron.defaultsDB, "OutgoingServer");
      if (option)
	WMSetTextFieldText(PrefsPanel.outservert, option);
      break;

  default:
    fprintf(stderr,"Unkown Outgoing Method\n");
  }
}


void putPrefsInWidgets(void)
{   
  char *option, string[5];
  int bool;
    
  if (WMGetUDBoolForKey(Aileron.defaultsDB, "NewMailManual"))
    WMPerformButtonClick(PrefsPanel.manualb);
  else
    WMPerformButtonClick(PrefsPanel.autob);
    
  WMSetButtonAction(PrefsPanel.manualb, buttonGenericAction, NULL);
  WMSetButtonAction(PrefsPanel.autob, buttonGenericAction, NULL);
    
  snprintf(string, 4, "%d", 
    WMGetUDIntegerForKey(Aileron.defaultsDB, "NewMailQueryMinutes"));
  WMSetTextFieldText(PrefsPanel.queryt, string);
  
#if MAILSOUND
  WMSetButtonSelected(PrefsPanel.soundb, 
    WMGetUDBoolForKey(Aileron.defaultsDB, "UseNewMailSound"));
  option = WMGetUDStringForKey(Aileron.defaultsDB, "NewMailSound");
  if(option) {
     WMSetLabelText(PrefsPanel.soundl, option);
  } else {
    WMSetButtonSelected(PrefsPanel.soundb, False);
    WMSetButtonEnabled(PrefsPanel.soundsetb, False);
  }
#else
   WMSetButtonSelected(PrefsPanel.soundb, False);
   WMSetButtonEnabled(PrefsPanel.soundsetb, False);
#endif

  WMSetButtonSelected(PrefsPanel.msgtotb, 
    WMGetUDBoolForKey(Aileron.defaultsDB, "ShowMessageTotals"));
  WMSetButtonSelected(PrefsPanel.msgsizeb, 
    WMGetUDBoolForKey(Aileron.defaultsDB, "ShowMessageSizes"));
  WMSetButtonSelected(PrefsPanel.msgdelb, 
    WMGetUDBoolForKey(Aileron.defaultsDB, "ShowDeletedMessages"));
  WMSetPopUpButtonSelectedItem(PrefsPanel.datep,
    WMGetUDBoolForKey(Aileron.defaultsDB, "MonthFirstInDate"));

  WMSetButtonSelected(PrefsPanel.compactb,
    WMGetUDBoolForKey(Aileron.defaultsDB, "AutoCompact"));
  WMSetButtonSelected(PrefsPanel.geomb,
    WMGetUDBoolForKey(Aileron.defaultsDB, "RememberGeometries"));
  
#if 0
  bool =  WMGetUDBoolForKey(Aileron.defaultsDB, "UseXFace");
  WMSetButtonSelected(PrefsPanel.xfaceb, bool);
  WMSetButtonEnabled(PrefsPanel.xfacesetb, bool);
  option = WMGetUDStringForKey(Aileron.defaultsDB, "XFace");
  if(option) 
    WMSetLabelText(PrefsPanel.xfacel, option);

#else
  WMSetButtonEnabled(PrefsPanel.xfaceb, 0);
  WMSetButtonEnabled(PrefsPanel.xfacesetb, 0);
#endif

  bool = WMGetUDBoolForKey(Aileron.defaultsDB, "UseSignature");
  WMSetButtonSelected(PrefsPanel.sigb, bool);
  WMSetButtonEnabled(PrefsPanel.sigsetb, bool);
  option = WMGetUDStringForKey(Aileron.defaultsDB, "Signature");
  if(option) 
    WMSetLabelText(PrefsPanel.sigl, option);

  option = WMGetUDStringForKey(Aileron.defaultsDB, "OtherAddressesForSelf");
  if(option) 
    WMSetTextFieldText(PrefsPanel.fromt, option);
  else
    WMSetTextFieldText(PrefsPanel.fromt, myusername());

  WMSetButtonSelected(PrefsPanel.replycopyb, 
    WMGetUDBoolForKey(Aileron.defaultsDB, "CopyMessageIntoReply"));

  WMSetButtonSelected(PrefsPanel.quoteb, 
    WMGetUDBoolForKey(Aileron.defaultsDB, "QuoteReplies"));

  option = WMGetUDStringForKey(Aileron.defaultsDB, "ReplyIntroduction");
  if(option) 
    WMSetTextFieldText(PrefsPanel.introt, option);
  else
    WMSetTextFieldText(PrefsPanel.introt, 
      "On day, name arranged electrons thus");

  option = WMGetUDStringForKey(Aileron.defaultsDB, "ReplyPrefix");
  if(option) 
    WMSetTextFieldText(PrefsPanel.prefixt, option);
  else
    WMSetTextFieldText(PrefsPanel.prefixt, "> ");

  option = WMGetUDStringForKey(Aileron.defaultsDB, "ReplyTo");
  if(option) 
    WMSetTextFieldText(PrefsPanel.replyt, option);
  else {
    char *replyto = wstrdup(myusername());

    replyto = wstrappend(replyto, "@");
    replyto = wstrappend(replyto, mylocalhost());
    WMSetTextFieldText(PrefsPanel.replyt, replyto);
    wfree(replyto);
  }

    
  WMSetButtonSelected(PrefsPanel.archiveb, 
    WMGetUDBoolForKey(Aileron.defaultsDB, "ArchiveOutgoingMail"));
  WMSetButtonSelected(PrefsPanel.sendb, 
    WMGetUDBoolForKey(Aileron.defaultsDB, "SendMIMEAlternatives"));


  fillHeaderToShow();
  fillOutGoingHeaders();
  fillMIMETypes();
  fillOutHow();


  WMSetButtonSelected(PrefsPanel.saveattachb, 
    WMGetUDBoolForKey(Aileron.defaultsDB, "AutoSaveAttachments"));

  option = WMGetUDStringForKey(Aileron.defaultsDB, "SaveAttachmentsTo");
  if(option) 
    WMSetLabelText(PrefsPanel.attachdirl, option);
  else
    WMSetLabelText(PrefsPanel.attachdirl, "/tmp");
  
  WMSetButtonSelected(PrefsPanel.autonextb,
    WMGetUDBoolForKey(Aileron.defaultsDB, "AutoNext"));

  bool = WMGetUDBoolForKey(Aileron.defaultsDB, "BalloonHelp");
#if 0
  printf("balloon %sabled\n", bool?"en":"dis");
#endif
  WMSetBalloonEnabled(Aileron.screen, bool);
  WMSetButtonSelected(PrefsPanel.balloonb, bool);
}




static void revertCB(WMWidget *w, void *client)
{
  putPrefsInWidgets();
  PrefsPanel.saved = True;
  WMSetWindowDocumentEdited(PrefsPanel.window, False);
}


static void setPrefsCB(WMWidget *w, void *client)
{
  char *option;
    
  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.manualb), "NewMailManual");
    
  WMSetUDIntegerForKey(Aileron.defaultsDB, 
    strtol(WMGetTextFieldText(PrefsPanel.queryt), NULL, 0),
    "NewMailQueryMinutes");

printf("DBStrings are a single space if blank.. check for that in Get code\n");

#if MAILSOUND
  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.soundb, "UseNewMailSound");
  option = WMGetLabelText(PrefsPanel.soundl);
  if(option && strlen(option)>1)
    WMSetUDStringForKey(Aileron.defaultsDB, option, "NewMailSound");
  else
    WMSetUDStringForKey(Aileron.defaultsDB, " ", "NewMailSound");
#else
  WMSetUDBoolForKey(Aileron.defaultsDB, 0, "UseNewMailSound");
#endif


  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.msgtotb), "ShowMessageTotals");
  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.msgdelb), "ShowDeletedMessages");
  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.msgsizeb), "ShowMessageSizes");
  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.msgtotb), "ShowMessageTotals");
  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetPopUpButtonSelectedItem(PrefsPanel.datep), "MonthFirstInDate");

  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.compactb), "AutoCompact");
  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.geomb), "RememberGeometries");

  
  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.xfaceb), "UseXFace");
  option = WMGetLabelText(PrefsPanel.xfacel);
  if(option && strlen(option)>1)
    WMSetUDStringForKey(Aileron.defaultsDB, option, "XFace");
  else
    WMSetUDStringForKey(Aileron.defaultsDB, " ", "XFace");

  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.sigb), "UseSignature");
  option = WMGetLabelText(PrefsPanel.sigl);
  if(option && strlen(option)>1)
    WMSetUDStringForKey(Aileron.defaultsDB, option, "Signature");
  else
    WMSetUDStringForKey(Aileron.defaultsDB, " ", "Signature");

  option = WMGetTextFieldText(PrefsPanel.fromt);
  if(strlen(option)>1)
    WMSetUDStringForKey(Aileron.defaultsDB, option, "OtherAddressesForSelf");
  else
    WMSetUDStringForKey(Aileron.defaultsDB, " ", "OtherAddressesForSelf");
  wfree(option);

  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.replycopyb), "CopyMessageIntoReply");
  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.quoteb), "QuoteReplies");

  option = WMGetLabelText(PrefsPanel.sigl);
  if(option && strlen(option)>1)
    WMSetUDStringForKey(Aileron.defaultsDB, option, "Signature");
  else
    WMSetUDStringForKey(Aileron.defaultsDB, " ", "Signature");

  option = WMGetTextFieldText(PrefsPanel.introt);
  if(strlen(option)>1)
    WMSetUDStringForKey(Aileron.defaultsDB, option, "ReplyIntroduction");
  else
    WMSetUDStringForKey(Aileron.defaultsDB, " ", "ReplyIntroduction");
  wfree(option);

  option = WMGetTextFieldText(PrefsPanel.prefixt);
  if(strlen(option)>1)
    WMSetUDStringForKey(Aileron.defaultsDB, option, "ReplyPrefix");
  else
    WMSetUDStringForKey(Aileron.defaultsDB, " ", "ReplyPrefix");
  wfree(option);

  option = WMGetTextFieldText(PrefsPanel.replyt);
  if(strlen(option)>1)
    WMSetUDStringForKey(Aileron.defaultsDB, option, "ReplyTo");
  else
    WMSetUDStringForKey(Aileron.defaultsDB, " ", "ReplyTo");
  wfree(option);

  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.archiveb), "ArchiveOutgoingMail");
  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.sendb), "SendMIMEAlternatives");

  saveHeaderToShow();
#if 0
  fillOutGoingHeaders();
  fillMIMETypes();
#endif

  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.saveattachb), "AutoSaveAttachments");
  
  option = WMGetLabelText(PrefsPanel.attachdirl);
  if(option && strlen(option)>1)
    WMSetUDStringForKey(Aileron.defaultsDB, option, "SaveAttachmentsTo");
  else
    WMSetUDStringForKey(Aileron.defaultsDB, " ", "SaveAttachmentsTo");

		    /* The Outgoing Mail settings */

  WMSetUDStringForKey(Aileron.defaultsDB, 
      WMGetTextFieldText(PrefsPanel.outservert), "OutgoingServer");
  WMSetUDStringForKey(Aileron.defaultsDB, 
      WMGetTextFieldText(PrefsPanel.outnamet), "OutgoingName");
  WMSetUDStringForKey(Aileron.defaultsDB, 
      WMGetTextFieldText(PrefsPanel.outpasst), "OutgoingPasswd");


  WMSetUDBoolForKey(Aileron.defaultsDB, 
    WMGetButtonSelected(PrefsPanel.autonextb), "AutoNext");

  WMSynchronizeUserDefaults(Aileron.defaultsDB);
  PrefsPanel.saved = True;
  WMSetWindowDocumentEdited(PrefsPanel.window, False);
 
}


void 
SaveGeometriesFor(char *what, WMSize size, WMPoint pos, int misc)
{
  WMPropList *dic, *nmbox, *props;
  char buffer[16];

  if(!WMGetUDBoolForKey(Aileron.defaultsDB, "RememberGeometries"))
    return;

  if(!what)
    return;

  nmbox = WMCreatePLString(what);
  props = WMCreatePLArray(NULL);

  snprintf(buffer, 15, "%i", size.width);
  WMAddToPLArray(props, WMCreatePLString(buffer));

  snprintf(buffer, 15, "%i", size.height);
  WMAddToPLArray(props, WMCreatePLString(buffer));

  snprintf(buffer, 15, "%i", pos.x);
  WMAddToPLArray(props, WMCreatePLString(buffer));

  snprintf(buffer, 15, "%i", pos.y);
  WMAddToPLArray(props, WMCreatePLString(buffer));

  snprintf(buffer, 15, "%i", misc);
  WMAddToPLArray(props, WMCreatePLString(buffer));

  dic = WMGetUDObjectForKey(Aileron.defaultsDB, "WindowGeometries");
  if (!dic) {
    dic = WMCreatePLDictionary(nmbox, props, NULL);
    WMSetUDObjectForKey(Aileron.defaultsDB, dic, "WindowGeometries");
  } else {
    WMPutInPLDictionary(dic, nmbox, props);
  }

}

Bool
GetGeometriesFor(char *what, WMSize *size, WMPoint *pos, int *misc)
{
  WMPropList *dic, *keys, *winp, *props;
  char *name;
  int i, nelem, found = 0;
  int tmp;

  if(!WMGetUDBoolForKey(Aileron.defaultsDB, "RememberGeometries"))
    return False;

  if(!what)
    return False;

  dic = WMGetUDObjectForKey(Aileron.defaultsDB, "WindowGeometries");
  if (!dic) 
    return False;

  keys = WMGetPLDictionaryKeys(dic);
  if (!keys)  
    return False;

  nelem = WMGetPropListItemCount(keys);
        
  for(i=0; i<nelem; i++) {
    if (!(winp = WMGetFromPLArray(keys, i)))
      continue; 
            
    if (!(name = WMGetFromPLString(winp)))
      continue;  

    if (!strcmp(name, what)) {
      found = 1;
      break;
    }
  }


  if(!found)
    return 0;

  if(!(props = WMGetFromPLDictionary(dic, winp)))
    return 0;
  if(WMGetPropListItemCount(props) != 5)
    return 0;

  
  if(size) {
    (*size).width = (sscanf(WMGetFromPLString(WMGetFromPLArray(props, 0)),
                            "%i", &tmp)!=1)?0:tmp;
    (*size).height = (sscanf(WMGetFromPLString(WMGetFromPLArray(props, 1)),
                            "%i", &tmp)!=1)?0:tmp;
  }

  if(pos) {
    (*pos).x = (sscanf(WMGetFromPLString(WMGetFromPLArray(props, 2)),
                       "%i", &tmp)!=1)?0:tmp;
    (*pos).y = (sscanf(WMGetFromPLString(WMGetFromPLArray(props, 3)),
                       "%i", &tmp)!=1)?0:tmp;
  }

  if(misc) { 
    *misc = (sscanf(WMGetFromPLString(WMGetFromPLArray(props, 4)),
                    "%i", &tmp)!=1)?0:tmp;
  }

  return True;
}

void
RunPrefsPanel(void)
{
  WMLabel *label;
  WMFrame *line, *frame;
  WMButton *b;
  int which;
  WMTabViewItem *tab;
  int i, y;

  if (WMHashGet(Aileron.windowTable, "_PREFERENCES_PANEL_")) { 
    WMSetWindowCloseAction(PrefsPanel.window, closePanel, NULL);
    XRaiseWindow(Aileron.display, WMWidgetXID(PrefsPanel.window));
    WMMapWidget(PrefsPanel.window);
    return;
  }

  PrefsPanel.window = WMCreateWindowWithStyle(Aileron.screen, 
    "Aileron Preferences", WMTitledWindowMask | WMClosableWindowMask);
  WMSetWindowTitle(PrefsPanel.window, "Aileron: Preferences");
  WMSetWindowCloseAction(PrefsPanel.window, closePanel, NULL);
  WMResizeWidget(PrefsPanel.window, 290, 390);
  WMRealizeWidget(PrefsPanel.window);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.window), 
    KeyPressMask, shuffleList, NULL);
  

  PrefsPanel.mainp = WMCreatePopUpButton(PrefsPanel.window);
  WMResizeWidget(PrefsPanel.mainp, 100, 20);
  WMMoveWidget(PrefsPanel.mainp, 95, 10);
  WMAddPopUpButtonItem(PrefsPanel.mainp, "General");
  WMAddPopUpButtonItem(PrefsPanel.mainp, "Compose");
  WMAddPopUpButtonItem(PrefsPanel.mainp, "Headers");
  WMAddPopUpButtonItem(PrefsPanel.mainp, "Attachments");
  WMAddPopUpButtonItem(PrefsPanel.mainp, "Network");
  WMSetPopUpButtonAction(PrefsPanel.mainp, changeForItemType, NULL);
  WMRealizeWidget(PrefsPanel.mainp);

  line = WMCreateFrame(PrefsPanel.window);
  WMResizeWidget(line, 290, 2);
  WMMoveWidget(line, 0, 40);
  WMSetFrameRelief(line, WRGroove);
  WMRealizeWidget(line);


  PrefsPanel.generalf = WMCreateFrame(PrefsPanel.window);
  WMSetFrameRelief(PrefsPanel.generalf, WRFlat); 
  WMResizeWidget(PrefsPanel.generalf, FRAME_WIDTH, FRAME_HEIGHT);
  WMMoveWidget(PrefsPanel.generalf, FRAME_LEFT, FRAME_TOP);

  PrefsPanel.newmailf = WMCreateFrame(PrefsPanel.generalf);
  WMResizeWidget(PrefsPanel.newmailf, 270, 111);
  WMMoveWidget(PrefsPanel.newmailf, 10, 0);
  WMSetFrameTitle(PrefsPanel.newmailf, " New Mail Retrieval ");

  PrefsPanel.manualb = WMCreateRadioButton(PrefsPanel.newmailf);
  WMResizeWidget(PrefsPanel.manualb, 80, 20);
  WMMoveWidget(PrefsPanel.manualb, 55, 25);
  WMSetButtonText(PrefsPanel.manualb, "Manual");

  PrefsPanel.autob = WMCreateRadioButton(PrefsPanel.newmailf);
  WMResizeWidget(PrefsPanel.autob, 80, 20);
  WMMoveWidget(PrefsPanel.autob, 150, 25);
  WMSetButtonText(PrefsPanel.autob, "Automatic");

  WMGroupButtons(PrefsPanel.manualb, PrefsPanel.autob);

  label = WMCreateLabel(PrefsPanel.newmailf);
  WMResizeWidget(label, 90, 20);
  WMMoveWidget(label, 5, 55);
  WMSetLabelText(label, "Query Every:");

  PrefsPanel.queryt = WMCreateTextField(PrefsPanel.newmailf);
  WMResizeWidget(PrefsPanel.queryt, 120, 20);
  WMMoveWidget(PrefsPanel.queryt, 90, 55);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.queryt), 
    KeyPressMask, textGenericAction, NULL);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.queryt), 
    KeyPressMask, shuffleList, NULL);

  label = WMCreateLabel(PrefsPanel.newmailf);
  WMResizeWidget(label, 50, 20);
  WMMoveWidget(label, 210, 55);
  WMSetLabelText(label, "minutes");

  PrefsPanel.soundb = WMCreateSwitchButton(PrefsPanel.newmailf);
  WMResizeWidget(PrefsPanel.soundb, 80, 20);
  WMMoveWidget(PrefsPanel.soundb, 20, 80);
  WMSetButtonText(PrefsPanel.soundb, "Sound:");
  WMSetButtonAction(PrefsPanel.soundb, buttonGenericAction, NULL);

  PrefsPanel.soundl = WMCreateLabel(PrefsPanel.newmailf);
  WMResizeWidget(PrefsPanel.soundl, 120, 20);
  WMMoveWidget(PrefsPanel.soundl, 90, 80);
  WMSetLabelTextAlignment(PrefsPanel.soundl, WARight);
  WMSetLabelRelief(PrefsPanel.soundl, WRSunken);

#if !MAILSOUND
  WMSetButtonEnabled(PrefsPanel.soundb, False);
  WMSetLabelText(PrefsPanel.soundl, "disabled");
  { WMColor *color = WMDarkGrayColor(Aileron.screen);
    WMSetLabelTextColor(PrefsPanel.soundl, color);
    WMReleaseColor(color);
  }
#endif

  PrefsPanel.soundsetb = WMCreateCommandButton(PrefsPanel.newmailf);
  WMResizeWidget(PrefsPanel.soundsetb, 45, 20);
  WMMoveWidget(PrefsPanel.soundsetb, 215, 80);
  WMSetButtonText(PrefsPanel.soundsetb, "Set...");
#if MAILSOUND
  WMSetButtonAction(PrefsPanel.soundsetb, buttonSetSoundCB, NULL);
#endif


  WMMapSubwidgets(PrefsPanel.newmailf);


  PrefsPanel.mboxwinf = WMCreateFrame(PrefsPanel.generalf);
  WMResizeWidget(PrefsPanel.mboxwinf, 270, 108);
  WMMoveWidget(PrefsPanel.mboxwinf, 10, 117);
  WMSetFrameTitle(PrefsPanel.mboxwinf, " Mailbox Window ");


  PrefsPanel.msgtotb = WMCreateSwitchButton(PrefsPanel.mboxwinf);
  WMResizeWidget(PrefsPanel.msgtotb, 180, 20);
  WMMoveWidget(PrefsPanel.msgtotb, 20, 18);
  WMSetButtonText(PrefsPanel.msgtotb, "Show Message Totals");
  WMSetButtonAction(PrefsPanel.msgtotb, buttonGenericAction, NULL);

  PrefsPanel.msgsizeb = WMCreateSwitchButton(PrefsPanel.mboxwinf);
  WMResizeWidget(PrefsPanel.msgsizeb, 180, 20);
  WMMoveWidget(PrefsPanel.msgsizeb, 20, 38);
  WMSetButtonText(PrefsPanel.msgsizeb, "Show Message Sizes");
  WMSetButtonAction(PrefsPanel.msgsizeb, buttonGenericAction, NULL);

  PrefsPanel.msgdelb = WMCreateSwitchButton(PrefsPanel.mboxwinf);
  WMResizeWidget(PrefsPanel.msgdelb, 180, 20);
  WMMoveWidget(PrefsPanel.msgdelb, 20, 58);
  WMSetButtonText(PrefsPanel.msgdelb, "Show Deleted Messages");
  WMSetButtonAction(PrefsPanel.msgdelb, buttonGenericAction, NULL);

  label = WMCreateLabel(PrefsPanel.mboxwinf);
  WMResizeWidget(label, 100, 20);
  WMMoveWidget(label, 16, 82);
  WMSetLabelText(label, "Display Dates as");

  PrefsPanel.datep = WMCreatePopUpButton(PrefsPanel.mboxwinf);
  WMResizeWidget(PrefsPanel.datep, 65, 20);
  WMMoveWidget(PrefsPanel.datep, 118, 82);
  WMAddPopUpButtonItem(PrefsPanel.datep, "Jun 30");
  WMAddPopUpButtonItem(PrefsPanel.datep, "30 Jun");
  WMSetPopUpButtonAction(PrefsPanel.datep, buttonGenericAction, NULL);

  WMMapSubwidgets(PrefsPanel.mboxwinf);

  PrefsPanel.mailappf = WMCreateFrame(PrefsPanel.generalf);
  WMResizeWidget(PrefsPanel.mailappf, 270, 65);
  WMMoveWidget(PrefsPanel.mailappf, 10, 231);
  WMSetFrameTitle(PrefsPanel.mailappf, " Mail Application ");

#if 0
  PrefsPanel.hideb = WMCreateSwitchButton(PrefsPanel.mailappf);
  WMResizeWidget(PrefsPanel.hideb, 180, 20);
  WMMoveWidget(PrefsPanel.hideb, 20, 15);
  WMSetButtonText(PrefsPanel.hideb, "Hide on Launch");
  WMSetButtonAction(PrefsPanel.hideb, buttonGenericAction, NULL);
#endif

  PrefsPanel.geomb = WMCreateSwitchButton(PrefsPanel.mailappf);
  WMSetButtonText(PrefsPanel.geomb, "Remember Window Geometries");
  WMResizeWidget(PrefsPanel.geomb, 240, 20);
  WMMoveWidget(PrefsPanel.geomb, 20, 15);

  PrefsPanel.compactb = WMCreateSwitchButton(PrefsPanel.mailappf);
  WMResizeWidget(PrefsPanel.compactb, 240, 20);
  WMMoveWidget(PrefsPanel.compactb, 20, 35);
  WMSetButtonText(PrefsPanel.compactb, "Automatically Compact Mailboxes");
  WMSetButtonAction(PrefsPanel.compactb, buttonGenericAction, NULL);

  WMMapSubwidgets(PrefsPanel.mailappf);

  
  WMRealizeWidget(PrefsPanel.generalf);
  WMMapSubwidgets(PrefsPanel.generalf);


  PrefsPanel.composerf = WMCreateFrame(PrefsPanel.window);
  WMSetFrameRelief(PrefsPanel.composerf, WRFlat); 
  WMResizeWidget(PrefsPanel.composerf, FRAME_WIDTH, FRAME_HEIGHT);
  WMMoveWidget(PrefsPanel.composerf, FRAME_LEFT, FRAME_TOP);

  
  PrefsPanel.composef = WMCreateFrame(PrefsPanel.composerf);
  WMResizeWidget(PrefsPanel.composef, 270, 94);
  WMMoveWidget(PrefsPanel.composef, 10, 0);
  WMSetFrameTitle(PrefsPanel.composef, " Compose ");


  PrefsPanel.xfaceb = WMCreateSwitchButton(PrefsPanel.composef);
  WMResizeWidget(PrefsPanel.xfaceb, 70, 20);
  WMMoveWidget(PrefsPanel.xfaceb, 20, 18);
  WMSetButtonText(PrefsPanel.xfaceb, "XFace:");
  WMSetButtonAction(PrefsPanel.xfaceb, buttonGenericAction, NULL);

  PrefsPanel.xfacel = WMCreateLabel(PrefsPanel.composef);
  WMResizeWidget(PrefsPanel.xfacel, 97, 20);
  WMMoveWidget(PrefsPanel.xfacel, 105, 18);
  WMSetLabelRelief(PrefsPanel.xfacel, WRSunken);
  WMSetLabelTextAlignment(PrefsPanel.xfacel, WARight);
  
  PrefsPanel.xfacesetb = WMCreateCommandButton(PrefsPanel.composef);
  WMResizeWidget(PrefsPanel.xfacesetb, 45, 20);
  WMMoveWidget(PrefsPanel.xfacesetb, 215, 18);
  WMSetButtonText(PrefsPanel.xfacesetb, "Set...");
  WMSetButtonEnabled(PrefsPanel.xfacesetb, False);

  PrefsPanel.sigb = WMCreateSwitchButton(PrefsPanel.composef);
  WMResizeWidget(PrefsPanel.sigb, 90, 20);
  WMMoveWidget(PrefsPanel.sigb, 20, 40);
  WMSetButtonText(PrefsPanel.sigb, "Signature:");
  WMSetButtonAction(PrefsPanel.sigb, buttonGenericAction, NULL);

  PrefsPanel.sigl = WMCreateLabel(PrefsPanel.composef);
  WMResizeWidget(PrefsPanel.sigl, 97, 20);
  WMMoveWidget(PrefsPanel.sigl, 105, 40);
  WMSetLabelRelief(PrefsPanel.sigl, WRSunken);
  WMSetLabelTextAlignment(PrefsPanel.sigl, WARight);
  
  PrefsPanel.sigsetb = WMCreateCommandButton(PrefsPanel.composef);
  WMResizeWidget(PrefsPanel.sigsetb, 45, 20);
  WMMoveWidget(PrefsPanel.sigsetb, 215, 40);
  WMSetButtonText(PrefsPanel.sigsetb, "Set...");
  WMSetButtonEnabled(PrefsPanel.sigsetb, True);
  WMSetButtonAction(PrefsPanel.sigsetb, setSigFile, PrefsPanel.window);

  label = WMCreateLabel(PrefsPanel.composef);
  WMResizeWidget(label, 50, 20);
  WMMoveWidget(label, 10, 65);
  WMSetLabelText(label, "From:");

  PrefsPanel.fromt = WMCreateTextField(PrefsPanel.composef);
  WMResizeWidget(PrefsPanel.fromt, 210, 20);
  WMMoveWidget(PrefsPanel.fromt, 50, 65);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.fromt), 
    KeyPressMask, textGenericAction, NULL);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.fromt), 
    KeyPressMask, shuffleList, NULL);


  WMMapSubwidgets(PrefsPanel.composef);


  PrefsPanel.replyf = WMCreateFrame(PrefsPanel.composerf);
  WMResizeWidget(PrefsPanel.replyf, 270, 131);
  WMMoveWidget(PrefsPanel.replyf, 10, 98);
  WMSetFrameTitle(PrefsPanel.replyf, " Reply ");

  PrefsPanel.replycopyb = WMCreateSwitchButton(PrefsPanel.replyf);
  WMResizeWidget(PrefsPanel.replycopyb, 220, 20);
  WMMoveWidget(PrefsPanel.replycopyb, 20, 14);
  WMSetButtonText(PrefsPanel.replycopyb, "Include Original Message in Reply");
  WMSetButtonAction(PrefsPanel.replycopyb, buttonGenericAction, NULL);

  PrefsPanel.quoteb = WMCreateSwitchButton(PrefsPanel.replyf);
  WMResizeWidget(PrefsPanel.quoteb, 140, 20);
  WMMoveWidget(PrefsPanel.quoteb, 20, 33);
  WMSetButtonText(PrefsPanel.quoteb, "Quote Reply");
  WMSetButtonAction(PrefsPanel.quoteb, buttonGenericAction, NULL);

  label = WMCreateLabel(PrefsPanel.replyf);
  WMResizeWidget(label, 80, 20);
  WMMoveWidget(label, 12, 55);
  WMSetLabelText(label, "Introduction:");

  PrefsPanel.introt = WMCreateTextField(PrefsPanel.replyf);
  WMResizeWidget(PrefsPanel.introt, 170, 20);
  WMMoveWidget(PrefsPanel.introt, 90, 55);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.introt), 
    KeyPressMask, textGenericAction, NULL);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.introt), 
    KeyPressMask, shuffleList, NULL);

  label = WMCreateLabel(PrefsPanel.replyf);
  WMResizeWidget(label, 70, 20);
  WMMoveWidget(label, 17, 78);
  WMSetLabelText(label, "Line Prefix:");

  PrefsPanel.prefixt = WMCreateTextField(PrefsPanel.replyf);
  WMResizeWidget(PrefsPanel.prefixt, 170, 20);
  WMMoveWidget(PrefsPanel.prefixt, 90, 78);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.prefixt), 
    KeyPressMask, textGenericAction, NULL);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.prefixt), 
    KeyPressMask, shuffleList, NULL);

  label = WMCreateLabel(PrefsPanel.replyf);
  WMResizeWidget(label, 65, 20);
  WMMoveWidget(label, 22, 101);
  WMSetLabelText(label, "Reply-To:");

  PrefsPanel.replyt = WMCreateTextField(PrefsPanel.replyf);
  WMResizeWidget(PrefsPanel.replyt, 170, 20);
  WMMoveWidget(PrefsPanel.replyt, 90, 101);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.replyt), 
    KeyPressMask, textGenericAction, NULL);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.replyt), 
    KeyPressMask, shuffleList, NULL);

  WMMapSubwidgets(PrefsPanel.replyf);


  PrefsPanel.optionsf = WMCreateFrame(PrefsPanel.composerf);
  WMResizeWidget(PrefsPanel.optionsf, 270, 63);
  WMMoveWidget(PrefsPanel.optionsf, 10, 233);
  WMSetFrameTitle(PrefsPanel.optionsf, " Send Options ");

  PrefsPanel.archiveb = WMCreateSwitchButton(PrefsPanel.optionsf);
  WMResizeWidget(PrefsPanel.archiveb, 180, 20);
  WMMoveWidget(PrefsPanel.archiveb, 20, 15);
  WMSetButtonText(PrefsPanel.archiveb, "Archive Outgoing Mail");
  WMSetButtonAction(PrefsPanel.archiveb, buttonGenericAction, NULL);

  PrefsPanel.sendb = WMCreateSwitchButton(PrefsPanel.optionsf);
  WMResizeWidget(PrefsPanel.sendb, 180, 20);
  WMMoveWidget(PrefsPanel.sendb, 20, 34);
  WMSetButtonText(PrefsPanel.sendb, "Send MIME Alternatives");
  WMSetButtonEnabled(PrefsPanel.sendb, False);
  WMSetButtonAction(PrefsPanel.sendb, buttonGenericAction, NULL);

  WMMapSubwidgets(PrefsPanel.optionsf);

  WMRealizeWidget(PrefsPanel.composerf);
  WMMapSubwidgets(PrefsPanel.composerf);


  PrefsPanel.headersf = WMCreateFrame(PrefsPanel.window);
  WMSetFrameRelief(PrefsPanel.headersf, WRFlat); 
  WMResizeWidget(PrefsPanel.headersf, FRAME_WIDTH, FRAME_HEIGHT);
  WMMoveWidget(PrefsPanel.headersf, FRAME_LEFT, FRAME_TOP);

  PrefsPanel.tabv = WMCreateTabView(PrefsPanel.headersf);
  WMSetTabViewDelegate(PrefsPanel.tabv, &tabViewDelegate);
  WMMoveWidget(PrefsPanel.tabv, 10, 0);
  WMResizeWidget(PrefsPanel.tabv, 270, 296);

  frame = WMCreateFrame(PrefsPanel.headersf);
  WMSetFrameRelief(frame, WRFlat); 
  
  tab = WMCreateTabViewItemWithIdentifier(0);
  WMSetTabViewItemView(tab, WMWidgetView(frame));
  WMAddItemInTabView(PrefsPanel.tabv, tab);
  WMSetTabViewItemLabel(tab, "Shown Headers");


  PrefsPanel.showl = WMCreateList(frame);
  WMResizeWidget(PrefsPanel.showl, 250, 194);
  WMMoveWidget(PrefsPanel.showl, 10, 10);
	WMSetListAllowEmptySelection(PrefsPanel.showl, True);
  WMSetListAllowMultipleSelection(PrefsPanel.showl, True);
  WMSetListAction(PrefsPanel.showl, showListAction, NULL);

  WMSetBalloonTextForView("This list contains headers that you\n"
    " would like to see in messages you receive, \nunless overridden "
    "by \"Show All headers\"\nYou can change the order in the list by "
    "selecting a\nsingle item, then pressing the up/down arrow keys.",
    WMWidgetView(PrefsPanel.showl));

  label = WMCreateLabel(frame);
  WMResizeWidget(label, 40, 20);
  WMMoveWidget(label, 6, 216);
  WMSetLabelText(label, "Key:");

  PrefsPanel.keyt = WMCreateTextField(frame);
  WMResizeWidget(PrefsPanel.keyt, 220, 20);
  WMMoveWidget(PrefsPanel.keyt, 40, 216);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.keyt), 
    KeyPressMask, textGenericAction, NULL);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.keyt), 
    KeyPressMask, shuffleList, NULL);

#if 0
  b = WMCreateCommandButton(frame);
  WMResizeWidget(b, 20, 20);
  WMMoveWidget(b, 40, 266);
  WMSetButtonAction(b, removeHeaderToShow, NULL);
  WMSetButtonImagePosition(b, WIPImageOnly);
  WMSetButtonImage(b, WMGetSystemPixmap(Aileron.screen, WSIArrowUp));
  WMSetButtonAltImage(b,
   WMGetSystemPixmap(Aileron.screen, WSIHighlightedArrowUp));


  b = WMCreateCommandButton(frame);
  WMResizeWidget(b, 20, 20);
  WMMoveWidget(b, 65, 266);
  WMSetButtonAction(b, removeHeaderToShow, NULL);
  WMSetButtonImagePosition(b, WIPImageOnly);
  WMSetButtonImage(b, WMGetSystemPixmap(Aileron.screen, WSIArrowDown));
  WMSetButtonAltImage(b,
   WMGetSystemPixmap(Aileron.screen, WSIHighlightedArrowDown));
#endif

  b = WMCreateCommandButton(frame);
  WMResizeWidget(b, 55, 20);
  WMMoveWidget(b, 145, 246);
  WMSetButtonText(b, "Remove");
  WMSetButtonAction(b, removeHeaderToShow, NULL);

  b = WMCreateCommandButton(frame);
  WMResizeWidget(b, 55, 20);
  WMMoveWidget(b, 205, 246);
  WMSetButtonText(b, "Add");
  WMSetButtonAction(b, addHeaderToShow, NULL);

  WMMapSubwidgets(frame);

  frame = WMCreateFrame(PrefsPanel.headersf);
  WMSetFrameRelief(frame, WRSunken); 
  
  tab = WMCreateTabViewItemWithIdentifier(1);
  WMSetTabViewItemView(tab, WMWidgetView(frame));
  WMAddItemInTabView(PrefsPanel.tabv, tab);
  WMSetTabViewItemLabel(tab, "Outgoing Headers");

  WMResizeWidget(frame, 250, 258);
  WMMoveWidget(frame, 10, 28);

  
  label = WMCreateLabel(frame);
  WMResizeWidget(label, 95, 19);
  WMMoveWidget(label, 3, 3);
  WMSetLabelFont(label, WMBoldSystemFontOfSize(Aileron.screen, 12));
  //WMSetLabelRelief(label, WRSunken);
  WMSetLabelTextColor(label, WMWhiteColor(Aileron.screen));
  WMSetWidgetBackgroundColor(label, WMDarkGrayColor(Aileron.screen));
  WMSetLabelText(label, "Key");

  label = WMCreateLabel(frame);
  WMResizeWidget(label, 147, 19);
  WMMoveWidget(label, 100, 3);
  WMSetLabelFont(label, WMBoldSystemFontOfSize(Aileron.screen, 12));
  //WMSetLabelRelief(label, WRSunken);
  WMSetLabelTextColor(label, WMWhiteColor(Aileron.screen));
  WMSetWidgetBackgroundColor(label, WMDarkGrayColor(Aileron.screen));
  WMSetLabelText(label, "Value");

  line = WMCreateFrame(frame);
  WMResizeWidget(line, 2, 231);
  WMMoveWidget(line, 98, 24);
  WMSetFrameRelief(line, WRGroove);

  PrefsPanel.pressed = NULL;
  PrefsPanel.pressedtext = NULL;

  i=0;
  y=25;
  for(i=0; i<=8; i++) {

    PrefsPanel.keys[i] = b = WMCreateCustomButton(frame, WBBStateChangeMask);
    WMResizeWidget(b, 95, 20);
    WMMoveWidget(b, 3, y);
    WMSetButtonBordered(b, False);
    WMSetButtonTextAlignment(b, WALeft);
    WMSetButtonAction(b, buttonExpertKeyCB, NULL);
    WMSetBalloonTextForView("Click in the Key field and "
      "type the name of the header.\n", WMWidgetView(b));
    PrefsPanel.keytext[i] = wstrdup("\0");

    PrefsPanel.vals[i] = b = WMCreateCustomButton(frame, WBBStateChangeMask);
    WMResizeWidget(b, 147, 22);
    WMMoveWidget(b, 100, y);
    WMSetButtonBordered(b, False);
    WMSetButtonTextAlignment(b, WALeft);
    WMSetButtonAction(b, buttonExpertKeyCB, NULL);
    WMSetBalloonTextForView("Click in the Value field and type an optional "
      "\"value\" \n- the text that appears with the header.\n",
      WMWidgetView(b));
    PrefsPanel.valtext[i] = wstrdup("\0");

    if(i < 8) {
      line = WMCreateFrame(frame);
      WMResizeWidget(line, 246, 2);
      WMMoveWidget(line, 2, y+22);
      WMSetFrameRelief(line, WRGroove);
    }

    y+=26;
  }


  PrefsPanel.hiddenkey = WMCreateTextField(frame);
  WMSetTextFieldBordered(PrefsPanel.hiddenkey, False);
  WMResizeWidget(PrefsPanel.hiddenkey, 10, 10);
  WMMoveWidget(PrefsPanel.hiddenkey, 1, 1);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.hiddenkey), 
    KeyPressMask |FocusChangeMask | LeaveWindowMask, 
    handleHiddenKeyPress, NULL);

  WMMapSubwidgets(frame);


  WMRealizeWidget(PrefsPanel.headersf);
  WMMapSubwidgets(PrefsPanel.headersf);




  PrefsPanel.mimef = WMCreateFrame(PrefsPanel.window);
  WMSetFrameRelief(PrefsPanel.mimef, WRFlat); 
  WMResizeWidget(PrefsPanel.mimef, FRAME_WIDTH, FRAME_HEIGHT);
  WMMoveWidget(PrefsPanel.mimef, FRAME_LEFT, FRAME_TOP);



  frame = WMCreateFrame(PrefsPanel.mimef);
  WMResizeWidget(frame, 270, 216);
  WMMoveWidget(frame, 10, 0);
  WMSetFrameTitle(frame, " MIME Description (not ready yet) ");

  PrefsPanel.mimel = WMCreateList(frame);
  WMResizeWidget(PrefsPanel.mimel, 250, 140);
  WMMoveWidget(PrefsPanel.mimel, 9, 20);
	WMSetListAllowEmptySelection(PrefsPanel.mimel, True);
  WMSetListAllowMultipleSelection(PrefsPanel.mimel, True);
  WMSetListAction(PrefsPanel.mimel, MIMEListAction, NULL);
  WMSetListDoubleAction(PrefsPanel.mimel, MIMEListDAction, NULL);
  WMSetBalloonTextForView("Aileron will fill this list with new MIME types "
   " as they occur.\nAt that time, it will also ask what to do with that "
   "type.\nThis means you don't have to bother with knowing MIME types.\n"
   "Edit the defaults file if you want to take matters into your own hands",
   WMWidgetView(PrefsPanel.mimel));


  PrefsPanel.mimesaveb = WMCreateRadioButton(frame);
  WMResizeWidget(PrefsPanel.mimesaveb, 100, 20);
  WMMoveWidget(PrefsPanel.mimesaveb, 12, 165);
  WMSetButtonText(PrefsPanel.mimesaveb, "Save to Disk");
  //WMSetButtonText(PrefsPanel.mimesaveb, "gv or DISABLEgv");
  WMSetButtonAction(PrefsPanel.mimesaveb, setMIMEType, NULL);

  PrefsPanel.mimehandleb = WMCreateRadioButton(frame);
  WMResizeWidget(PrefsPanel.mimehandleb, 100, 20);
  WMMoveWidget(PrefsPanel.mimehandleb, 12, 185);
  WMSetButtonText(PrefsPanel.mimehandleb, "Handle with:");
  WMSetButtonAction(PrefsPanel.mimehandleb, buttonGenericAction, NULL);

  WMGroupButtons(PrefsPanel.mimesaveb, PrefsPanel.mimehandleb);

  PrefsPanel.handlerl = WMCreateLabel(frame);
  WMResizeWidget(PrefsPanel.handlerl, 96, 20);
  WMMoveWidget(PrefsPanel.handlerl, 109, 185);
  WMSetLabelRelief(PrefsPanel.handlerl, WRSunken);
  WMSetLabelTextAlignment(PrefsPanel.handlerl, WARight);

  PrefsPanel.mimehandlesetb = WMCreateCommandButton(frame);
  WMResizeWidget(PrefsPanel.mimehandlesetb, 45, 20);
  WMMoveWidget(PrefsPanel.mimehandlesetb, 215, 185);
  WMSetButtonText(PrefsPanel.mimehandlesetb, "Set...");
  WMSetButtonAction(PrefsPanel.mimehandlesetb, setMIMEType, NULL);


  WMMapSubwidgets(frame);


  frame = WMCreateFrame(PrefsPanel.mimef);
  WMResizeWidget(frame, 270, 73);
  WMMoveWidget(frame, 10, 223);
  WMSetFrameTitle(frame, " Attachments ");

  PrefsPanel.saveattachb = WMCreateSwitchButton(frame);
  WMResizeWidget(PrefsPanel.saveattachb, 210, 20);
  WMMoveWidget(PrefsPanel.saveattachb, 12, 15);
  WMSetButtonText(PrefsPanel.saveattachb, "Automatically Save Attachments");
  WMSetButtonAction(PrefsPanel.saveattachb, buttonGenericAction, NULL);

  label = WMCreateLabel(frame);
  WMResizeWidget(label, 60, 20);
  WMMoveWidget(label, 10, 40);
  WMSetLabelText(label, "Save in:");

  PrefsPanel.attachdirl = WMCreateLabel(frame);
  WMResizeWidget(PrefsPanel.attachdirl, 140, 20);
  WMMoveWidget(PrefsPanel.attachdirl, 65, 40);
  WMSetLabelTextAlignment(PrefsPanel.attachdirl, WARight);
  WMSetLabelRelief(PrefsPanel.attachdirl, WRSunken);

  b = WMCreateCommandButton(frame);
  WMResizeWidget(b, 45, 20);
  WMMoveWidget(b, 215, 40);
  WMSetButtonText(b, "Set...");
  WMSetButtonAction(b, setAttachDir, PrefsPanel.window);

  WMMapSubwidgets(frame);

  WMRealizeWidget(PrefsPanel.mimef);
  WMMapSubwidgets(PrefsPanel.mimef);


  PrefsPanel.networkf = WMCreateFrame(PrefsPanel.window);
  WMSetFrameRelief(PrefsPanel.networkf, WRFlat); 
  WMResizeWidget(PrefsPanel.networkf, FRAME_WIDTH, FRAME_HEIGHT);
  WMMoveWidget(PrefsPanel.networkf, FRAME_LEFT, FRAME_TOP);
  WMRealizeWidget(PrefsPanel.networkf);
  
  PrefsPanel.retrievalf = WMCreateFrame(PrefsPanel.networkf);
  WMResizeWidget(PrefsPanel.retrievalf, 270, 140);
  WMMoveWidget(PrefsPanel.retrievalf, 10, 0);
  WMSetFrameTitle(PrefsPanel.retrievalf, " Remote Retrieval ");
  
  PrefsPanel.fetchb = WMCreateSwitchButton(PrefsPanel.retrievalf);
  WMSetButtonText(PrefsPanel.fetchb, "Automatically fetch attachments");
  WMResizeWidget(PrefsPanel.fetchb, 250, 20);
  WMMoveWidget(PrefsPanel.fetchb, 12, 17);
  
  PrefsPanel.offlineb = WMCreateSwitchButton(PrefsPanel.retrievalf);
  WMSetButtonText(PrefsPanel.offlineb, 
    "Download messages for offline viewing");
  WMResizeWidget(PrefsPanel.offlineb, 250, 20);
  WMMoveWidget(PrefsPanel.offlineb, 12, 40);

  PrefsPanel.autonextb = WMCreateSwitchButton(PrefsPanel.retrievalf);
  WMSetButtonText(PrefsPanel.autonextb, "Open Next Message after Deletes");
  WMResizeWidget(PrefsPanel.autonextb, 240, 20);
  WMMoveWidget(PrefsPanel.autonextb, 12, 63);
  
#if 0
  PrefsPanel.secureb = WMCreateSwitchButton(PrefsPanel.retrievalf);
  WMSetButtonText(PrefsPanel.secureb, "Securely retrieve messages");
  WMResizeWidget(PrefsPanel.secureb, 250, 20);
  WMMoveWidget(PrefsPanel.secureb, 12, 86);
#endif
  
  
  WMRealizeWidget(PrefsPanel.retrievalf);
  WMMapSubwidgets(PrefsPanel.retrievalf);
  


  PrefsPanel.outserverf = WMCreateFrame(PrefsPanel.networkf);
  WMResizeWidget(PrefsPanel.outserverf, 270, 150);
  WMMoveWidget(PrefsPanel.outserverf, 10, 146);
  WMSetFrameTitle(PrefsPanel.outserverf, " Outgoing Mail ");

  label = WMCreateLabel(PrefsPanel.outserverf);
  WMResizeWidget(label, 60, 20);
  WMMoveWidget(label, 10, 20);
  WMSetLabelText(label, "Method:");

  PrefsPanel.outhowp = WMCreatePopUpButton(PrefsPanel.outserverf);
  WMResizeWidget(PrefsPanel.outhowp, 100, 20);
  WMMoveWidget(PrefsPanel.outhowp, 74, 20);
  WMAddPopUpButtonItem(PrefsPanel.outhowp, "SMTP");
  WMAddPopUpButtonItem(PrefsPanel.outhowp, "Agent");
  WMAddPopUpButtonItem(PrefsPanel.outhowp, "Server");
  WMSetPopUpButtonAction(PrefsPanel.outhowp, changeOutHowForItemType, NULL);


  PrefsPanel.outhowl1 = WMCreateLabel(PrefsPanel.outserverf);
  WMResizeWidget(PrefsPanel.outhowl1, 60, 20);
  WMMoveWidget(PrefsPanel.outhowl1, 8, 48);
  WMSetLabelText(PrefsPanel.outhowl1, "Agent:");


  PrefsPanel.outservert = WMCreateTextField(PrefsPanel.outserverf);
  WMResizeWidget(PrefsPanel.outservert, 184, 20);
  WMMoveWidget(PrefsPanel.outservert, 74, 48);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.outservert), 
    KeyPressMask, textGenericAction, NULL);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.outservert), 
    KeyPressMask, shuffleList, NULL);

  PrefsPanel.outhowl2 = WMCreateLabel(PrefsPanel.outserverf);
  WMResizeWidget(PrefsPanel.outhowl2, 70, 20);
  WMMoveWidget(PrefsPanel.outhowl2, 8, 73);
  WMSetLabelText(PrefsPanel.outhowl2, "Username:");

  PrefsPanel.outnamet = WMCreateTextField(PrefsPanel.outserverf);
  WMResizeWidget(PrefsPanel.outnamet, 184, 20);
  WMMoveWidget(PrefsPanel.outnamet, 74, 73);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.outnamet), 
    KeyPressMask, textGenericAction, NULL);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.outnamet), 
    KeyPressMask, shuffleList, NULL);

  PrefsPanel.outhowl3 = WMCreateLabel(PrefsPanel.outserverf);
  WMResizeWidget(PrefsPanel.outhowl3, 70, 20);
  WMMoveWidget(PrefsPanel.outhowl3, 8, 98);
  WMSetLabelText(PrefsPanel.outhowl3, "Password:");

  PrefsPanel.outpasst = WMCreateTextField(PrefsPanel.outserverf);
  WMResizeWidget(PrefsPanel.outpasst, 184, 20);
  WMMoveWidget(PrefsPanel.outpasst, 74, 98);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.outpasst), 
    KeyPressMask, textGenericAction, NULL);
  WMCreateEventHandler(WMWidgetView(PrefsPanel.outpasst), 
    KeyPressMask, shuffleList, NULL);

  PrefsPanel.sslb = WMCreateSwitchButton(PrefsPanel.outserverf);
  WMSetButtonText(PrefsPanel.sslb, "Send messages securely");
  WMResizeWidget(PrefsPanel.sslb, 200, 20);
  WMMoveWidget(PrefsPanel.sslb, 12, 123);

  WMRealizeWidget(PrefsPanel.outserverf);
  WMMapSubwidgets(PrefsPanel.outserverf);
  
  WMMapSubwidgets(PrefsPanel.networkf);


  which = WMGetUDIntegerForKey(Aileron.defaultsDB, "OutgoingMailAgent");
  updateOutHowForItemType(which);

  PrefsPanel.balloonb = WMCreateSwitchButton(PrefsPanel.window);
 printf(" WMSetButtonSelected(PrefsPanel.balloonb, Prefs.BalloonHelp);\n");
  WMResizeWidget(PrefsPanel.balloonb, 100, 20);
  WMMoveWidget(PrefsPanel.balloonb, 10, 360);
  WMSetButtonText(PrefsPanel.balloonb, "Balloon Help");
  WMSetButtonAction(PrefsPanel.balloonb, balloonAction, NULL);

  PrefsPanel.revertb = WMCreateCommandButton(PrefsPanel.window);
  WMResizeWidget(PrefsPanel.revertb, 65, 25);
  WMMoveWidget(PrefsPanel.revertb, 140, 355);
  WMSetButtonText(PrefsPanel.revertb, "Revert");
  WMSetButtonAction(PrefsPanel.revertb, revertCB, NULL);
  WMSetBalloonTextForView("Click to revert to old settings.",
    WMWidgetView(PrefsPanel.revertb));

  PrefsPanel.setb = WMCreateCommandButton(PrefsPanel.window);
  WMResizeWidget(PrefsPanel.setb, 65, 25);
  WMMoveWidget(PrefsPanel.setb, 215, 355);
  WMSetButtonText(PrefsPanel.setb, "Set");
  WMSetButtonImageDefault(PrefsPanel.setb);
  WMSetButtonImagePosition(PrefsPanel.setb, WIPRight);
  WMSetButtonAction(PrefsPanel.setb, setPrefsCB, NULL);
  WMSetBalloonTextForView("Click to put new settings into effect.",
    WMWidgetView(PrefsPanel.setb));

  WMRealizeWidget(PrefsPanel.balloonb);
  WMRealizeWidget(PrefsPanel.revertb);
  WMRealizeWidget(PrefsPanel.setb);

  WMMapWidget(PrefsPanel.balloonb);
  WMMapWidget(PrefsPanel.revertb);
  WMMapWidget(PrefsPanel.setb);

  WMMapWidget(PrefsPanel.mainp);
  //WMMapWidget(line);

  WMAppAddWindow(Aileron.appContext, WMWidgetXID(PrefsPanel.window));

  WMHashInsert(Aileron.windowTable, "_PREFERENCES_PANEL_", &PrefsPanel);
  
  WMMapWidget(PrefsPanel.window);
  WMMapSubwidgets(PrefsPanel.window);
  WMUnmapWidget(PrefsPanel.hiddenkey);

  which = WMGetUDIntegerForKey(Aileron.defaultsDB, 
    "PreferencePage");
  if (which < 0 || which > 5) 
    which = 0;
  WMSetPopUpButtonSelectedItem(PrefsPanel.mainp, (which<5?which:2));
  updateForItemType(which);
  PrefsPanel.saved = True;

  putPrefsInWidgets();

  //WMSetPopUpButtonText(PrefsPanel.formatp, 
  //  (Prefs.ComposeMailFormat?"MIME":"Plain Text"));


  WMSetWindowDocumentEdited(PrefsPanel.window, !PrefsPanel.saved);

}





