home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / ComposeAttachDialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  12.0 KB  |  400 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* 
  19.    ComposeAttachDialog.cpp -- compose attachment dialogs
  20.    Created: Alastair Gourlay(SGI) c/o Dora Hsu<dora@netscape.com>, 26 Nov 1996
  21.  */
  22.  
  23.  
  24.  
  25. // Classes in this file:
  26. //      XFE_ComposeAttachLocationDialog
  27. //      XFE_ComposeAttachFileDialog
  28. //
  29.  
  30. #include "ComposeAttachDialog.h"
  31. #include "DesktopTypes.h"
  32. #include <stdlib.h>
  33. #include <Xm/XmAll.h>
  34. #include "xfe.h"
  35. #include "felocale.h"
  36.  
  37. #ifdef DEBUG_sgidev
  38. #define XDEBUG(x) x
  39. #else
  40. #define XDEBUG(x)
  41. #endif
  42.  
  43. //
  44. // XFE_ComposeAttachLocationDialog
  45. //
  46.  
  47. // callback stubs
  48.  
  49. void XFE_ComposeAttachLocationDialog::OkCb(Widget,XtPointer cd,XtPointer) {
  50.     XFE_ComposeAttachLocationDialog *ad=(XFE_ComposeAttachLocationDialog*)cd;
  51.     if (ad)
  52.         ad->okCb();
  53. }
  54.  
  55. void XFE_ComposeAttachLocationDialog::ClearCb(Widget,XtPointer cd,XtPointer) {
  56.     XFE_ComposeAttachLocationDialog *ad=(XFE_ComposeAttachLocationDialog*)cd;
  57.     if (ad)
  58.         ad->clearCb();
  59. }
  60.  
  61. void XFE_ComposeAttachLocationDialog::CancelCb(Widget,XtPointer cd,XtPointer) {
  62.     XFE_ComposeAttachLocationDialog *ad=(XFE_ComposeAttachLocationDialog*)cd;
  63.     if (ad)
  64.         ad->cancelCb();
  65. }
  66.  
  67. // constructor
  68.  
  69. XFE_ComposeAttachLocationDialog::XFE_ComposeAttachLocationDialog(XFE_ComposeAttachFolderView *folder)
  70.     : XFE_Component()
  71. {
  72.     _attachFolder=folder;
  73.     
  74.     _parent=NULL;
  75.     _dialog=NULL;
  76.     _locationText=NULL;
  77. }
  78.  
  79. XFE_ComposeAttachLocationDialog::~XFE_ComposeAttachLocationDialog()
  80. {
  81. }
  82.  
  83. void XFE_ComposeAttachLocationDialog::createWidgets(Widget parent)
  84. {
  85.     _parent=parent;
  86.  
  87.     Arg args[20];
  88.     int n;
  89.  
  90.     Visual *v = 0;
  91.     Colormap cmap = 0;
  92.     Cardinal depth = 0;
  93.  
  94.     Widget shell=parent;
  95.     while (!XtIsShell(shell)) shell=XtParent(shell);
  96.     XtVaGetValues(shell,XtNvisual,&v,XtNcolormap,&cmap,XtNdepth,&depth,NULL);
  97.  
  98.     n=0;
  99.     XtSetArg (args[n], XmNvisual, v); n++;
  100.     XtSetArg (args[n], XmNdepth, depth); n++;
  101.     XtSetArg (args[n], XmNcolormap, cmap); n++;
  102.     XtSetArg (args[n], XmNautoUnmanage, False); n++;
  103.     XtSetArg (args[n], XmNdeleteResponse, XmUNMAP); n++;
  104.     XtSetArg (args[n], XmNdialogStyle, XmDIALOG_MODELESS); n++;
  105.     _dialog = XmCreateMessageDialog (_parent, "location_popup", args, n);
  106.     fe_UnmanageChild_safe(XmMessageBoxGetChild(_dialog,XmDIALOG_MESSAGE_LABEL));
  107.     fe_UnmanageChild_safe(XmMessageBoxGetChild(_dialog,XmDIALOG_SYMBOL_LABEL));
  108.     fe_UnmanageChild_safe(XmMessageBoxGetChild(_dialog,XmDIALOG_HELP_BUTTON));
  109.     XtAddCallback(_dialog,XmNunmapCallback,CancelCb,(XtPointer)this);
  110.     XtAddCallback(_dialog,XmNokCallback,OkCb,(XtPointer)this);
  111.     XtAddCallback(_dialog,XmNcancelCallback,CancelCb,(XtPointer)this);
  112.  
  113.     Widget clearButton=XmCreatePushButtonGadget (_dialog,"clear",NULL,0);
  114.     XtAddCallback(clearButton,XmNactivateCallback,ClearCb,(XtPointer)this);
  115.     XtManageChild(clearButton);
  116.     
  117.     Widget form=XmCreateForm(_dialog,"form",NULL,0);
  118.  
  119.     Widget label=XmCreateLabelGadget(form,"label",NULL,0);
  120.     XtVaSetValues(label,
  121.                   XmNtopAttachment, XmATTACH_FORM,
  122.                   XmNbottomAttachment, XmATTACH_NONE,
  123.                   XmNleftAttachment, XmATTACH_FORM,
  124.                   XmNrightAttachment, XmATTACH_FORM,
  125.                   NULL);
  126.     XtManageChild(label);
  127.     
  128.     Widget locationLabel=XmCreateLabelGadget(form,"locationLabel",NULL,0);
  129.     XtVaSetValues(locationLabel,
  130.                   XmNtopAttachment, XmATTACH_WIDGET,
  131.                   XmNtopWidget, label,
  132.                   XmNbottomAttachment, XmATTACH_NONE,
  133.                   XmNleftAttachment, XmATTACH_FORM,
  134.                   XmNrightAttachment, XmATTACH_NONE,
  135.                   NULL);
  136.     XtManageChild(locationLabel);
  137.     
  138.     _locationText=fe_CreateTextField(form,"locationText",NULL,0);
  139.     XtVaSetValues(_locationText,
  140.                   XmNtopAttachment, XmATTACH_WIDGET,
  141.                   XmNtopWidget, label,
  142.                   XmNbottomAttachment, XmATTACH_NONE,
  143.                   XmNleftAttachment, XmATTACH_WIDGET,
  144.                   XmNleftWidget, locationLabel,
  145.                   XmNrightAttachment, XmATTACH_FORM,
  146.                   NULL);
  147.     if (fe_globalData.nonterminal_text_translations)
  148.         XtOverrideTranslations (_locationText, fe_globalData.nonterminal_text_translations);
  149.     XtAddCallback(_locationText,XmNactivateCallback,OkCb,(XtPointer)this);
  150.     XtManageChild(_locationText);
  151.     
  152.     Dimension height;
  153.     XtVaGetValues(_locationText,XmNheight,&height,NULL);
  154.     XtVaSetValues(locationLabel,XmNheight,height,NULL);
  155.  
  156.     XtManageChild(form);
  157.     
  158.     fe_HackDialogTranslations(form);
  159.  
  160.     XtVaSetValues(_dialog,XmNinitialFocus,form,NULL);
  161.  
  162.     setBaseWidget(_dialog);
  163. }
  164.  
  165. void  XFE_ComposeAttachLocationDialog::show()
  166. {
  167.     XFE_Component::show();
  168.     if (_dialog)
  169.         XMapRaised(XtDisplay(_dialog),XtWindow(XtParent(_dialog)));
  170. }
  171.  
  172. //
  173. // callback methods
  174. //
  175.  
  176. void XFE_ComposeAttachLocationDialog::okCb()
  177. {
  178.     char *url=fe_GetTextField(_locationText);
  179.  
  180.     if (!url || strlen(url)==0) {
  181.         XtFree(url);
  182.         return;
  183.     }
  184.     
  185.     // check for Netscape desktop file
  186.     char *desktopUrl=NULL;
  187.     char *attachUrl=url;
  188.     XFE_DesktopTypeTranslate(url,&desktopUrl,FALSE);        
  189.     if (desktopUrl && strlen(desktopUrl)>0)
  190.         attachUrl=desktopUrl;
  191.         
  192.     // validate final url and attach
  193.     if (XFE_ComposeAttachFolderView::validateAttachment(_dialog,attachUrl)) {
  194.         _attachFolder->addAttachment(attachUrl);
  195.         _attachFolder->scrollToItem(_attachFolder->numAttachments()-1);
  196.         hide();
  197.     }
  198.     
  199.     if (desktopUrl)
  200.         XP_FREE(desktopUrl);
  201.     
  202.     if (url)
  203.         XtFree(url);
  204. }
  205.  
  206. void XFE_ComposeAttachLocationDialog::clearCb()
  207. {
  208.     fe_SetTextFieldAndCallBack(_locationText,"");
  209.     XmProcessTraversal(_locationText,XmTRAVERSE_CURRENT);
  210. }
  211.  
  212. void XFE_ComposeAttachLocationDialog::cancelCb()
  213. {
  214.     hide();
  215. }
  216.  
  217.  
  218.  
  219.  
  220. //
  221. // XFE_ComposeAttachFileDialog
  222. //
  223.  
  224. // callback stubs
  225.  
  226. void XFE_ComposeAttachFileDialog::OkCb(Widget,XtPointer cd,XtPointer cb) {
  227.     XFE_ComposeAttachFileDialog *ad=(XFE_ComposeAttachFileDialog*)cd;
  228.     if (ad) {
  229.         char *filename=NULL;
  230.         XmFileSelectionBoxCallbackStruct *fcb=(XmFileSelectionBoxCallbackStruct*)cb;
  231.         if (fcb && fcb->value)
  232.             XmStringGetLtoR(fcb->value,XmSTRING_DEFAULT_CHARSET,&filename);
  233.  
  234.         ad->okCb(filename);
  235.  
  236.         if (filename)
  237.             XtFree(filename);
  238.     }
  239. }
  240.  
  241. void XFE_ComposeAttachFileDialog::CancelCb(Widget,XtPointer cd,XtPointer) {
  242.     XFE_ComposeAttachFileDialog *ad=(XFE_ComposeAttachFileDialog*)cd;
  243.     if (ad)
  244.         ad->cancelCb();
  245. }
  246.  
  247. void XFE_ComposeAttachFileDialog::SetFileAttachBinaryCb(Widget widget,XtPointer cd,XtPointer) {
  248.     XFE_ComposeAttachFileDialog *ad=(XFE_ComposeAttachFileDialog*)cd;
  249. #ifdef DEBUG
  250.     printf("XFE_ComposeAttachFileDialog::SetFileAttachBinaryCb\n");
  251. #endif
  252.     if (ad)
  253.         ad->setFileAttachBinaryCb(widget);
  254. }
  255.  
  256.  
  257. // constructor
  258.  
  259. XFE_ComposeAttachFileDialog::XFE_ComposeAttachFileDialog(XFE_ComposeAttachFolderView *folder)
  260.     : XFE_Component()
  261. {
  262.     _attachFolder=folder;
  263.     
  264.     _parent=NULL;
  265.     _dialog=NULL;
  266.     _attachEncodingMenu=NULL;
  267.     _attachBinaryButton=NULL;
  268. }
  269.  
  270. XFE_ComposeAttachFileDialog::~XFE_ComposeAttachFileDialog()
  271. {
  272. }
  273.  
  274. void XFE_ComposeAttachFileDialog::createWidgets(Widget parent)
  275. {
  276.     _parent=parent;
  277.     _dialog=_parent;
  278.  
  279.     Arg args[20];
  280.     int n;
  281.  
  282.     Visual *v = 0;
  283.     Colormap cmap = 0;
  284.     Cardinal depth = 0;
  285.  
  286.     Widget shell=parent;
  287.     while (!XtIsShell(shell)) shell=XtParent(shell);
  288.     XtVaGetValues(shell,XtNvisual,&v,XtNcolormap,&cmap,XtNdepth,&depth,NULL);
  289.  
  290.     n=0;
  291.     XtSetArg (args[n], XmNvisual, v); n++;
  292.     XtSetArg (args[n], XmNdepth, depth); n++;
  293.     XtSetArg (args[n], XmNcolormap, cmap); n++;
  294.     XtSetArg(args[n],XmNdeleteResponse,XmUNMAP);n++;
  295.     XtSetArg(args[n],XmNdialogStyle,XmDIALOG_MODELESS);n++;
  296.     XtSetArg(args[n],XmNfileTypeMask,XmFILE_REGULAR);n++;
  297.     _dialog=fe_CreateFileSelectionDialog(_parent,"fileBrowser",args,n);
  298. #ifdef NO_HELP
  299.     fe_UnmanageChild_safe(XmSelectionBoxGetChild(_dialog,XmDIALOG_HELP_BUTTON));
  300. #endif
  301.  
  302.     XtAddCallback(_dialog,XmNokCallback,OkCb,(XtPointer)this);
  303.     XtAddCallback(_dialog,XmNcancelCallback,CancelCb,(XtPointer)this);
  304.     XtAddCallback(_dialog,XmNunmapCallback,CancelCb,(XtPointer)this);
  305.  
  306.     // create selector for attachment transfer encoding
  307.     n=0;
  308.     XtSetArg(args[n],XmNshadowType,XmSHADOW_ETCHED_IN);n++;
  309.     Widget optionFrame=XmCreateFrame(_dialog,"encodingFrame",args,n);
  310.     
  311.     n=0;
  312.     XtSetArg(args[n],XmNvisual,v);n++;
  313.     XtSetArg(args[n],XmNdepth,depth);n++;
  314.     XtSetArg(args[n],XmNcolormap,cmap);n++;
  315.     Widget optionPopup=XmCreatePulldownMenu(optionFrame,"optionPopup",args,n);
  316.  
  317.     Widget attachAutoDetectButton=XmCreatePushButtonGadget(optionPopup,"attachAutoDetect",NULL,0);
  318.     XtAddCallback(attachAutoDetectButton, XmNactivateCallback, SetFileAttachBinaryCb, (XtPointer)this);
  319.     XtManageChild(attachAutoDetectButton);
  320.     _attachBinaryButton=XmCreatePushButtonGadget(optionPopup,"attachBinary",NULL,0);
  321.     XtAddCallback(_attachBinaryButton, XmNactivateCallback, SetFileAttachBinaryCb, (XtPointer)this);
  322.     XtManageChild(_attachBinaryButton);
  323.  
  324.     n=0;
  325.     XtSetArg(args[n],XmNsubMenuId,optionPopup);n++;
  326.     if (fe_globalPrefs.file_attach_binary) {
  327.         XtSetArg(args[n],XmNmenuHistory,_attachBinaryButton);n++;
  328.     }
  329.     else {
  330.         XtSetArg(args[n],XmNmenuHistory,attachAutoDetectButton);n++;
  331.     }
  332.     _attachEncodingMenu=XmCreateOptionMenu(optionFrame,"optionMenu",args,n);
  333.     XtManageChild(_attachEncodingMenu);
  334.  
  335.     XtManageChild(optionFrame);
  336.  
  337.     fe_HackDialogTranslations(_dialog);
  338.     fe_NukeBackingStore(_dialog);
  339.     
  340.     setBaseWidget(_dialog);
  341. }
  342.  
  343. void  XFE_ComposeAttachFileDialog::show()
  344. {
  345.     XFE_Component::show();
  346.     if (_dialog)
  347.         XMapRaised(XtDisplay(_dialog),XtWindow(XtParent(_dialog)));
  348. }
  349.  
  350. //
  351. // callback methods
  352. //
  353.  
  354. void XFE_ComposeAttachFileDialog::okCb(const char *filename)
  355. {
  356.     if (!filename || strlen(filename)==0)
  357.         return;
  358.     
  359.     // check for Netscape desktop file
  360.     char *desktopUrl=NULL;
  361.     const char *attachUrl=filename;
  362.     XFE_DesktopTypeTranslate(filename,&desktopUrl,FALSE);
  363.     if (desktopUrl && strlen(desktopUrl)>0)
  364.         attachUrl=desktopUrl;
  365.  
  366.     // validate final url and attach
  367.     if (XFE_ComposeAttachFolderView::validateAttachment(_dialog,attachUrl)) {
  368.         // check whether user selected binary as the tranfer encoding
  369.         Boolean attach_binary=FALSE;
  370.         if (_attachEncodingMenu && _attachBinaryButton) {
  371.             Widget attachEncodingWidget=NULL;
  372.             XtVaGetValues(_attachEncodingMenu,XmNmenuHistory,&attachEncodingWidget,NULL);
  373.             attach_binary=(attachEncodingWidget==_attachBinaryButton)? TRUE:FALSE;
  374.         }
  375.         _attachFolder->addAttachment(attachUrl,FALSE,attach_binary);
  376.         _attachFolder->scrollToItem(_attachFolder->numAttachments()-1);
  377.         hide();
  378.     }
  379.  
  380.     if (desktopUrl)
  381.         XP_FREE(desktopUrl);
  382. }
  383.  
  384. void XFE_ComposeAttachFileDialog::cancelCb()
  385. {
  386.     hide();
  387. }
  388.  
  389. void XFE_ComposeAttachFileDialog::setFileAttachBinaryCb(XtPointer widget)
  390. {
  391.     XP_Bool attach_binary;
  392.  
  393.     if ((Widget)widget == _attachBinaryButton)
  394.         attach_binary = True;
  395.     else
  396.         attach_binary = False;
  397.     fe_globalPrefs.file_attach_binary = attach_binary;
  398. }
  399.  
  400.