home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / BookmarkPropDialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  12.1 KB  |  418 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.    BookmarkPropDialog.cpp -- Dialog to edit bookmark properties.
  20.    Created: Stephen Lamm <slamm@netscape.com>, 10-Mar-97.
  21.  */
  22.  
  23.  
  24.  
  25. #include "BookmarkPropDialog.h"
  26. #include "BookmarkView.h"
  27.  
  28. #include "bkmks.h"
  29. #include "felocale.h"  // for fe_GetTextField()
  30. #include "xfe.h"
  31. #include "xfe2_extern.h"
  32. #include "xpgetstr.h"
  33.  
  34. #include <Xm/Form.h>
  35. #include <Xm/LabelG.h> 
  36. #include <Xm/PushBG.h>
  37. #include <Xm/SelectioB.h>
  38. #include <Xm/TextF.h> 
  39.  
  40. extern int XFE_BM_ALIAS;  // "This is an alias to the following Bookmark:"
  41.  
  42. XFE_BookmarkPropDialog::XFE_BookmarkPropDialog(MWContext *context, Widget parent)
  43.   : XFE_Dialog(parent, "bookmarkProps",
  44.                TRUE,   // ok
  45.                TRUE,   // cancel
  46.                FALSE,  // help
  47.                FALSE,  // apply
  48.                TRUE,   // Separator
  49.                FALSE  // Modal
  50.                )
  51. {
  52.   int ac;
  53.   Arg av[20];
  54.   Widget kids[20];
  55.   int i;
  56.  
  57.   m_context = context;
  58.   m_entry = NULL;
  59.  
  60.   Widget form = 0;
  61.   Widget name_label = 0;
  62.   Widget description_label = 0;
  63.   Widget addedOn_label = 0;
  64.  
  65.  
  66.   fe_UnmanageChild_safe (XmSelectionBoxGetChild (m_chrome,
  67.                                                  XmDIALOG_SELECTION_LABEL));
  68.   fe_UnmanageChild_safe (XmSelectionBoxGetChild (m_chrome,
  69.                                                  XmDIALOG_TEXT));
  70.   fe_UnmanageChild_safe (XmSelectionBoxGetChild (m_chrome,
  71.                                                  XmDIALOG_APPLY_BUTTON));
  72.   fe_UnmanageChild_safe (XmSelectionBoxGetChild (m_chrome,
  73.                                                  XmDIALOG_HELP_BUTTON));
  74.  
  75.   XtAddCallback(m_chrome, XmNdestroyCallback, destroy_cb, this);
  76.   XtAddCallback(m_chrome, XmNokCallback, ok_cb, this);
  77.   XtAddCallback(m_chrome, XmNcancelCallback, close_cb, this);
  78.  
  79.   ac = 0;
  80.   form =              XmCreateForm (m_chrome, "form", av, ac);
  81.   m_title =           XmCreateLabelGadget(form, "title", av, ac);
  82.   m_aliasLabel =      XmCreateLabelGadget(form, "aliasLabel", av, ac);
  83.   m_aliasButton =     XmCreatePushButtonGadget (form, "aliasButton", av, ac);
  84.   name_label =        XmCreateLabelGadget(form, "nameLabel", av, ac);
  85.   m_locationLabel =   XmCreateLabelGadget(form, "locationLabel", av, ac);
  86.   description_label = XmCreateLabelGadget(form, "descriptionLabel", av, ac);
  87.  
  88.   if (m_context->type == MWContextBookmarks) {
  89.     m_lastVisitedLabel = XmCreateLabelGadget(form, "lastvisitedLabel", av, ac);
  90.     addedOn_label = XmCreateLabelGadget(form, "addedonLabel", av, ac);
  91.  
  92.   }
  93.  
  94.   XtAddCallback(m_aliasButton, XmNactivateCallback, selectalias_cb, this);
  95.  
  96.   ac = 0;
  97.   m_name = fe_CreateTextField(form, "nameText", av, ac);
  98.   m_location = fe_CreateTextField(form, "locationText", av, ac);
  99.  
  100.   ac = 0;
  101.   XtSetArg (av[ac], XmNeditMode, XmMULTI_LINE_EDIT); ac++;
  102.   m_description = fe_CreateText(form, "descriptionText", av, ac);
  103.   
  104.   ac = 0;
  105.   m_lastVisited = XmCreateLabelGadget(form, "lastVisited", av, ac);
  106.   m_addedOn = XmCreateLabelGadget(form, "addedOn", av, ac);
  107.   
  108.   XtVaSetValues(m_title,
  109.         XmNtopAttachment, XmATTACH_FORM,
  110.         XmNleftAttachment, XmATTACH_FORM,
  111.         XmNrightAttachment, XmATTACH_FORM,
  112.         0);
  113.   XtVaSetValues(name_label,
  114.         XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  115.         XmNtopWidget, m_name,
  116.         XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
  117.         XmNbottomWidget, m_name,
  118.         XmNleftAttachment, XmATTACH_FORM,
  119.         XmNrightAttachment, XmATTACH_WIDGET,
  120.         XmNrightWidget, m_name,
  121.         0);
  122.   XtVaSetValues(m_locationLabel,
  123.         XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  124.         XmNtopWidget, m_location,
  125.         XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
  126.         XmNbottomWidget, m_location,
  127.         XmNleftAttachment, XmATTACH_FORM,
  128.         XmNrightAttachment, XmATTACH_WIDGET,
  129.         XmNrightWidget, m_location,
  130.         0);
  131.   XtVaSetValues(description_label,
  132.         XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  133.         XmNtopWidget, m_description,
  134.         XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
  135.         XmNbottomWidget, m_description,
  136.         XmNleftAttachment, XmATTACH_FORM,
  137.         XmNrightAttachment, XmATTACH_WIDGET,
  138.         XmNrightWidget, m_description,
  139.         0);
  140.   XtVaSetValues(m_lastVisitedLabel,
  141.         XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  142.         XmNtopWidget, m_lastVisited,
  143.         XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
  144.         XmNbottomWidget, m_lastVisited,
  145.         XmNleftAttachment, XmATTACH_FORM,
  146.         XmNrightAttachment, XmATTACH_WIDGET,
  147.         XmNrightWidget, m_lastVisited,
  148.         0);
  149.   XtVaSetValues(addedOn_label,
  150.         XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  151.         XmNtopWidget, m_addedOn,
  152.         XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
  153.         XmNbottomWidget, m_addedOn,
  154.         XmNleftAttachment, XmATTACH_FORM,
  155.         XmNrightAttachment, XmATTACH_WIDGET,
  156.         XmNrightWidget, m_addedOn,
  157.         0);
  158.   ac = 0;
  159.   kids[ac++] = m_title;
  160.   kids[ac++] = m_name;
  161.   kids[ac++] = m_location;
  162.   kids[ac++] = m_description;
  163.   kids[ac++] = m_lastVisited;
  164.   kids[ac++] = m_addedOn;
  165.   XtVaSetValues(kids[1],
  166.         XmNtopAttachment, XmATTACH_WIDGET,
  167.         XmNtopWidget, kids[0],
  168.         XmNleftAttachment, XmATTACH_NONE,
  169.         XmNrightAttachment, XmATTACH_FORM,
  170.         XmNbottomAttachment, XmATTACH_NONE,
  171.         0);
  172.   for (i=2 ; i<ac ; i++) {
  173.     XtVaSetValues(kids[i],
  174.           XmNtopAttachment, XmATTACH_WIDGET,
  175.           XmNtopWidget, kids[i-1],
  176.           XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
  177.           XmNleftWidget, kids[i-1],
  178.           XmNrightAttachment, XmATTACH_FORM,
  179.           XmNbottomAttachment, XmATTACH_NONE,
  180.           0);
  181.   }
  182.   XtVaSetValues(m_aliasLabel,
  183.         XmNtopAttachment, XmATTACH_WIDGET,
  184.         XmNtopWidget, kids[ac-1],
  185.         XmNleftAttachment, XmATTACH_FORM,
  186.         XmNbottomAttachment, XmATTACH_FORM,
  187.         XmNrightAttachment, XmATTACH_WIDGET,
  188.         XmNrightWidget, m_aliasButton,
  189.         0);
  190.   XtVaSetValues(m_aliasButton,
  191.         XmNtopAttachment, XmATTACH_WIDGET,
  192.         XmNtopWidget, kids[ac-1],
  193.         XmNleftAttachment, XmATTACH_NONE,
  194.         XmNbottomAttachment, XmATTACH_FORM,
  195.         XmNrightAttachment, XmATTACH_FORM,
  196.         0);
  197.  
  198.   fe_attach_field_to_labels(m_name, name_label, m_locationLabel,
  199.                 description_label, m_lastVisitedLabel,
  200.                 addedOn_label, 0);
  201.   
  202.   kids[ac++] = m_aliasLabel;
  203.   kids[ac++] = m_aliasButton;
  204.   kids[ac++] = name_label;
  205.   kids[ac++] = m_locationLabel;
  206.   kids[ac++] = description_label;
  207.   kids[ac++] = m_lastVisitedLabel;
  208.   kids[ac++] = addedOn_label;
  209.  
  210.   XtManageChildren(kids, ac);
  211.   XtManageChild(form);
  212.  
  213.   fe_HackDialogTranslations (m_chrome);
  214. }
  215.  
  216. XFE_BookmarkPropDialog::~XFE_BookmarkPropDialog() 
  217. {
  218.   close();
  219. }
  220.  
  221. void
  222. XFE_BookmarkPropDialog::selectAlias()
  223. {
  224.   if (m_entry)
  225.     BM_SelectAliases(m_context, m_entry);
  226. }
  227.  
  228. void
  229. XFE_BookmarkPropDialog::selectalias_cb(Widget widget,
  230.                                        XtPointer closure, XtPointer call_data)
  231. {
  232.   XFE_BookmarkPropDialog* obj = (XFE_BookmarkPropDialog *)closure;
  233.   obj->selectAlias();
  234. }
  235.  
  236. void
  237. XFE_BookmarkPropDialog::destroy_cb(Widget widget,
  238.                                    XtPointer closure, XtPointer call_data)
  239. {
  240.   XFE_BookmarkPropDialog* obj = (XFE_BookmarkPropDialog *)closure;
  241.  
  242.   obj->close();
  243. }
  244.  
  245. void
  246. XFE_BookmarkPropDialog::close()
  247. {
  248.   if (m_entry)
  249.     {
  250.       BM_CancelEdit(m_context, m_entry);
  251.       m_entry = NULL;
  252.     }
  253. }
  254.  
  255. void
  256. XFE_BookmarkPropDialog::close_cb(Widget widget,
  257.                                  XtPointer closure, XtPointer call_data)
  258. {
  259.   XFE_BookmarkPropDialog* obj = (XFE_BookmarkPropDialog *)closure;
  260.  
  261.   obj->close();
  262.  
  263.   XtDestroyWidget(obj->getBaseWidget());
  264. }
  265.  
  266. void
  267. XFE_BookmarkPropDialog::ok()
  268. {
  269.   if (m_entry)
  270.     {
  271.       commitChanges();
  272.       m_entry = NULL;
  273.     }
  274. }
  275.  
  276. void
  277. XFE_BookmarkPropDialog::ok_cb(Widget widget,
  278.                               XtPointer closure, XtPointer call_data)
  279. {
  280.   XFE_BookmarkPropDialog* obj = (XFE_BookmarkPropDialog *)closure;
  281.  
  282.   obj->ok();
  283.  
  284.   XtDestroyWidget(obj->getBaseWidget());
  285. }
  286.  
  287.  
  288. void
  289. XFE_BookmarkPropDialog::editItem(BM_Entry *entry)
  290. {
  291.   const char* str;
  292.   char* tmp;
  293.  
  294.   /* Raise the edit shell to view */
  295.   if (XtIsManaged (CONTEXT_WIDGET (m_context))) {
  296.     XRaiseWindow(XtDisplay(CONTEXT_WIDGET (m_context)),
  297.          XtWindow(CONTEXT_WIDGET (m_context)));
  298.   }
  299.  
  300.   commitChanges();
  301.  
  302.   /* Alias stuff */
  303.   if (BM_IsAlias(entry)) {
  304.     fe_SetString(m_title, XmNlabelString, XP_GetString(XFE_BM_ALIAS));
  305.     entry = BM_GetAliasOriginal(entry);
  306.   }
  307.   else
  308.     fe_SetString(m_title, XmNlabelString, " ");
  309.  
  310.   m_entry = entry;
  311.  
  312.   if (BM_IsUrl(entry)) {
  313.     str = BM_GetName(entry);
  314.     fe_SetTextField(m_name, str?str:"");
  315.     str = BM_GetAddress(entry);
  316.     fe_SetTextField(m_location, str?str:"");
  317.     XtVaSetValues(m_location, XmNsensitive, True, 0);
  318.     XtVaSetValues(m_locationLabel, XmNsensitive, True, 0);
  319.     str =  BM_GetDescription(entry);
  320.     fe_SetTextField(m_description, str?str:"");
  321.     tmp = BM_PrettyLastVisitedDate(entry);
  322.     if (!tmp || !*tmp) tmp = " ";
  323.     fe_SetString(m_lastVisited, XmNlabelString, tmp);
  324.     XtVaSetValues(m_lastVisited, XmNsensitive, True, 0);
  325.     XtVaSetValues(m_lastVisitedLabel, XmNsensitive, True, 0);
  326.     fe_SetString(m_addedOn, XmNlabelString, BM_PrettyAddedOnDate(entry));
  327.     fe_SetString(m_aliasLabel, XmNlabelString,
  328.             BM_PrettyAliasCount(m_context, entry));
  329.     if (BM_CountAliases(m_context, entry) > 0)
  330.       XtVaSetValues(m_aliasButton, XmNsensitive, True, 0);
  331.     else
  332.       XtVaSetValues(m_aliasButton, XmNsensitive, False, 0);
  333.   }
  334.   else if (BM_IsHeader(entry)) {
  335.     str = BM_GetName(entry);
  336.     fe_SetTextField(m_name, str?str:"");
  337.     fe_SetTextField(m_location, "");
  338.     XtVaSetValues(m_location, XmNsensitive, False, 0);
  339.     XtVaSetValues(m_locationLabel, XmNsensitive, False, 0);
  340.     str =  BM_GetDescription(entry);
  341.     fe_SetTextField(m_description, str?str:"");
  342.     fe_SetString(m_lastVisited, XmNlabelString, " ");
  343.     XtVaSetValues(m_lastVisited, XmNsensitive, False, 0);
  344.     XtVaSetValues(m_lastVisitedLabel, XmNsensitive, False, 0);
  345.     fe_SetString(m_addedOn, XmNlabelString, BM_PrettyAddedOnDate(entry));
  346.     fe_SetString(m_aliasLabel, XmNlabelString, "" );
  347.     XtVaSetValues(m_aliasButton, XmNsensitive, False, 0);
  348.   }
  349. }
  350.  
  351. void
  352. XFE_BookmarkPropDialog::entryGoingAway(BM_Entry *entry)
  353. {
  354.   if (m_entry == entry) {
  355.     m_entry = NULL;
  356.     if (m_chrome) {
  357.       fe_SetTextField(m_name, "");
  358.       fe_SetTextField(m_location, "");
  359.       fe_SetTextField(m_description, "");
  360.       if (m_lastVisited) fe_SetString(m_lastVisited, XmNlabelString, " ");
  361.       if (m_addedOn) fe_SetString(m_addedOn, XmNlabelString, " ");
  362.     }
  363.   }
  364. }
  365.  
  366. char *
  367. XFE_BookmarkPropDialog::getAndCleanText(Widget widget,
  368.                                          Boolean new_lines_too_p)
  369. {
  370.   char    *str;
  371.  
  372.   str = fe_GetTextField(widget);
  373.   if (!str) {
  374.     return NULL;
  375.   }
  376.   fe_forms_clean_text(m_context, INTL_DefaultWinCharSetID(NULL), str,
  377.                       new_lines_too_p);
  378.  
  379.   return str;
  380. }
  381.  
  382.  
  383. XP_Bool
  384. XFE_BookmarkPropDialog::commitChanges()
  385. {
  386.   XFE_BookmarkView *view = (XFE_BookmarkView*)BM_GetFEData(m_context);
  387.   char* ptr = 0;
  388.  
  389.   if (!m_entry) return(True);
  390.   ptr = getAndCleanText(m_name, True);
  391.   if (!ptr) {
  392.     return(False);
  393.   }
  394.  
  395.   // Have the view set the name so it can keep track
  396.   // of the personal toolbar.
  397.   if (view)
  398.     view->setName(m_entry, ptr);
  399.   XP_FREE(ptr);
  400.  
  401.   ptr = getAndCleanText(m_location, True);
  402.   if (!ptr) {
  403.     return(False);
  404.   }
  405.   BM_SetAddress(m_context, m_entry, ptr);
  406.   XP_FREE(ptr);
  407.  
  408.   ptr = getAndCleanText(m_description, False);
  409.   if (!ptr) {
  410.     return(False);
  411.   }
  412.   BM_SetDescription(m_context, m_entry, ptr);
  413.   XP_FREE(ptr);
  414.  
  415.   return(True);
  416. }
  417.  
  418.