home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / PrefsFont.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  13.2 KB  |  540 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. /*
  20.    PrefsFont.cpp -- The fontuage dialog for preference, packaged from 3.0 code
  21.    Created: Linda Wei <lwei@netscape.com>, 25-Feb-97.
  22.  */
  23.  
  24.  
  25. #include "felocale.h"
  26. #include "structs.h"
  27. #include "fonts.h"
  28. #include "xpassert.h"
  29. #include "xfe.h"
  30. #include "e_kit.h"
  31. #include "PrefsFont.h"
  32. #include "nf.h"
  33. #include "Mnffbu.h"
  34.  
  35. #include <Xm/ArrowBG.h>
  36. #include <Xm/Label.h>
  37. #include <Xm/LabelG.h>
  38. #include <Xm/PushB.h>
  39. #include <Xm/PushBG.h>
  40. #include <Xm/ScrolledW.h>
  41. #include <Xm/ToggleB.h>
  42. #include <Xm/Form.h>
  43. #include <Xm/Frame.h>
  44. #include <Xm/TextF.h> 
  45. #include <Xm/ToggleBG.h> 
  46. #include <Xfe/Xfe.h>
  47.  
  48. extern "C"
  49. {
  50.     char *XP_GetString(int i);
  51. }
  52.  
  53. #define OUTLINER_GEOMETRY_PREF "preferences.font.outliner_geometry"
  54.  
  55. extern int XFE_FONT_COL_ORDER;
  56. extern int XFE_FONT_COL_DISPLAYER;
  57.  
  58. const int XFE_PrefsFontDialog::OUTLINER_COLUMN_ORDER     = 0;
  59. const int XFE_PrefsFontDialog::OUTLINER_COLUMN_DISPLAYER = 1;
  60.  
  61. const int XFE_PrefsFontDialog::OUTLINER_COLUMN_MAX_LENGTH = 256;
  62. const int XFE_PrefsFontDialog::OUTLINER_INIT_POS = (-1);
  63.  
  64. #define STRING_COL_ORDER      "Order"
  65. #define STRING_COL_DISPLAYER  "Displayer"
  66.  
  67.  
  68. // ==================== Public Member Functions ====================
  69.  
  70. // Member:       XFE_PrefsFontDialog
  71. // Description:  Constructor
  72. // Inputs:
  73. // Side effects: Creates the Font Displayer dialog
  74.  
  75. XFE_PrefsFontDialog::XFE_PrefsFontDialog(XFE_PrefsDialog *prefsDialog,
  76.                                          Widget     parent,  // dialog parent
  77.                                          char      *name,    // dialog name
  78.                                          Boolean    modal)   // modal dialog?
  79.     : XFE_Dialog(parent, 
  80.                  name,
  81.                  TRUE,     // ok
  82.                  TRUE,     // cancel
  83.                  FALSE,    // help
  84.                  FALSE,    // apply
  85.                  FALSE,    // separator
  86.                  modal     // modal
  87.                  ),
  88.       m_prefsDialog(prefsDialog),
  89.       m_prefsDataFont(0)
  90. {
  91.     PrefsDataFont *fep = NULL;
  92.  
  93.     fep = new PrefsDataFont;
  94.     memset(fep, 0, sizeof(PrefsDataFont));
  95.     m_prefsDataFont = fep;
  96.  
  97.     Widget     kids[100];
  98.     Arg        av[50];
  99.     int        ac;
  100.     int        i;
  101.  
  102.     Widget     form;
  103.     form = XtVaCreateWidget("form", xmFormWidgetClass, m_chrome,
  104.                             XmNmarginWidth, 8,
  105.                             XmNtopAttachment, XmATTACH_FORM,
  106.                             XmNleftAttachment, XmATTACH_FORM,
  107.                             XmNrightAttachment, XmATTACH_FORM,
  108.                             XmNbottomAttachment, XmATTACH_FORM,
  109.                             NULL);
  110.     XtManageChild (form);
  111.  
  112.     Widget         font_label;
  113.     Widget         font_list;
  114.     Widget         up_button;
  115.     Widget         down_button;
  116.  
  117.     ac = 0;
  118.     i = 0;
  119.  
  120.     kids[i++] = font_label = XmCreateLabelGadget(form, "fontLabel", av, ac);
  121.  
  122.     kids[i++] = up_button = XmCreateArrowButtonGadget(form, "upButton", av, ac);
  123.  
  124.     kids[i++] = down_button = XmCreateArrowButtonGadget(form, "downButton", av, ac);
  125.  
  126.     fep->up_button = up_button;
  127.     fep->down_button = down_button;
  128.  
  129.     // Outliner
  130.  
  131.     int           num_columns = 2;
  132.     static int    default_column_widths[] = {8, 40};
  133.     XFE_Outliner *outliner;
  134.  
  135.     outliner = new XFE_Outliner("fontList",            // name
  136.                                 this,                  // outlinable
  137.                                 getPrefsDialog(),      // top level                                  
  138.                                 form,                  // parent
  139.                                 FALSE,                 // constant size
  140.                                 FALSE,                 // has headings
  141.                                 num_columns,           // number of columns
  142.                                 num_columns,           // number of visible columns
  143.                                 default_column_widths, // default column widths
  144.                                 OUTLINER_GEOMETRY_PREF
  145.                                 );
  146.  
  147.     font_list = fep->font_list = outliner->getBaseWidget();
  148.     fep->font_outliner = outliner;
  149.  
  150.     XtVaSetValues(outliner->getBaseWidget(),
  151.                   XtVaTypedArg, XmNblankBackground, XmRString, "white", 6,
  152.                   XmNvisibleRows, 10,
  153.                   NULL);
  154.     XtVaSetValues(outliner->getBaseWidget(),
  155.                   XmNcellDefaults, True,
  156.                   XtVaTypedArg, XmNcellBackground, XmRString, "white", 6,
  157.                   NULL);
  158.  
  159.     outliner->setColumnResizable(OUTLINER_COLUMN_ORDER, False);
  160.     outliner->setColumnResizable(OUTLINER_COLUMN_DISPLAYER, False);
  161.     outliner->show();
  162.     
  163.     XtVaSetValues(font_label,
  164.                   XmNalignment, XmALIGNMENT_BEGINNING,
  165.                   XmNtopAttachment, XmATTACH_FORM,
  166.                   XmNleftAttachment, XmATTACH_FORM,
  167.                   XmNrightAttachment, XmATTACH_NONE,
  168.                   XmNbottomAttachment, XmATTACH_NONE,
  169.                   NULL);
  170.  
  171.     XtVaSetValues(fep->font_list,
  172.                   XmNtopOffset, 8,
  173.                   XmNtopAttachment, XmATTACH_WIDGET,
  174.                   XmNtopWidget, font_label,
  175.                   XmNleftAttachment, XmATTACH_FORM,
  176.                   XmNrightAttachment, XmATTACH_NONE,
  177.                   XmNbottomAttachment, XmATTACH_NONE,
  178.                   NULL);
  179.  
  180.     Dimension list_height;
  181.     Dimension up_button_height;
  182.     int       down_button_top_offset;
  183.     int       up_button_top_offset;
  184.  
  185.     XtVaGetValues(down_button,
  186.                   XmNtopOffset, &down_button_top_offset,
  187.                   NULL);
  188.  
  189.     list_height = XfeHeight(font_list);
  190.     up_button_height = XfeHeight(up_button);
  191.     up_button_top_offset = list_height/2 - up_button_height - down_button_top_offset/2;
  192.  
  193.     XtVaSetValues(up_button,
  194.                   XmNarrowDirection, XmARROW_UP,
  195.                   XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  196.                   XmNtopWidget, font_list,
  197.                   XmNtopOffset, up_button_top_offset,
  198.                   XmNleftAttachment, XmATTACH_WIDGET,
  199.                   XmNleftWidget, font_list,
  200.                   XmNleftOffset, 8,
  201.                   XmNrightAttachment, XmATTACH_NONE,
  202.                   XmNbottomAttachment, XmATTACH_NONE,
  203.                   NULL);
  204.                   
  205.     XtVaSetValues(down_button,
  206.                   XmNarrowDirection, XmARROW_DOWN,
  207.                   XmNtopAttachment, XmATTACH_WIDGET,
  208.                   XmNtopWidget, up_button,
  209.                   XmNtopOffset, 4,
  210.                   XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
  211.                   XmNleftWidget, up_button,
  212.                   XmNrightAttachment, XmATTACH_NONE,
  213.                   XmNbottomAttachment, XmATTACH_NONE,
  214.                   NULL);
  215.  
  216.     // Add callbacks
  217.  
  218.     XtAddCallback(m_chrome, XmNokCallback, cb_ok, this);
  219.     XtAddCallback(m_chrome, XmNcancelCallback, cb_cancel, this);
  220.     XtAddCallback(up_button, XmNactivateCallback, cb_promote, this);
  221.     XtAddCallback(down_button, XmNactivateCallback, cb_demote, this);
  222.  
  223.     XtManageChildren(kids, i);
  224.     XtManageChild(form);
  225. }
  226.  
  227. // Member:       ~XFE_PrefsFontDialog
  228. // Description:  Destructor
  229. // Inputs:
  230. // Side effects: 
  231.  
  232. XFE_PrefsFontDialog::~XFE_PrefsFontDialog()
  233. {
  234.     if (m_prefsDataFont) {
  235.         if (m_prefsDataFont->font_displayers) 
  236.             nffbu_free(WF_fbu, m_prefsDataFont->font_displayers, NULL);
  237.     }
  238.  
  239.     delete m_prefsDataFont;
  240. }
  241.  
  242. // Member:       show
  243. // Description:  Pop up dialog
  244. // Inputs:
  245. // Side effects: 
  246.  
  247. void XFE_PrefsFontDialog::show()
  248. {
  249.     // TODO: Initialize the dialog
  250.  
  251.     // Manage the top level
  252.  
  253.     XFE_Dialog::show();
  254.  
  255.     // Set focus to the OK button
  256.  
  257.     XmProcessTraversal(m_okButton, XmTRAVERSE_CURRENT);
  258. }
  259.  
  260. // Member:       initPage
  261. // Description:  Initializes page for Font
  262. // Inputs:
  263. // Side effects: 
  264.  
  265. void XFE_PrefsFontDialog::initPage()
  266. {
  267.     XP_ASSERT(m_prefsDataFont);
  268.  
  269.     PrefsDataFont    *fep = m_prefsDataFont;
  270.  
  271.     // Retrieve the list of available font displayers
  272.  
  273.     fep->font_displayers = (char **)nffbu_ListFontDisplayers(WF_fbu, NULL);
  274.  
  275.     fep->font_displayers_count = 0;
  276.     if (fep->font_displayers != 0) {
  277.         for (int i=0; fep->font_displayers[i] == 0; i++) {
  278.             if (fep->font_displayers[i] != 0) fep->font_displayers_count++;
  279.         }
  280.     }
  281.  
  282.     fep->font_outliner->change(0, fep->font_displayers_count, fep->font_displayers_count);
  283.     setSelectionPos(OUTLINER_INIT_POS);
  284. }
  285.  
  286. // Member:       verifyPage
  287. // Description:  verify page for Font
  288. // Inputs:
  289. // Side effects: 
  290.  
  291. Boolean XFE_PrefsFontDialog::verifyPage()
  292. {
  293.     return TRUE;
  294. }
  295.  
  296. // Member:       getContext
  297. // Description:  returns context
  298. // Inputs:
  299. // Side effects: 
  300.  
  301. MWContext *XFE_PrefsFontDialog::getContext()
  302. {
  303.     return (m_prefsDialog->getContext());
  304. }
  305.  
  306. // Member:       getPrefsDialog
  307. // Description:  returns preferences dialog
  308. // Inputs:
  309. // Side effects: 
  310.  
  311. XFE_PrefsDialog *XFE_PrefsFontDialog::getPrefsDialog()
  312. {
  313.     return (m_prefsDialog);
  314. }
  315.  
  316. // Member:       setSelelctionPos
  317. // Description:  
  318. // Inputs:
  319. // Side effects: 
  320.  
  321. void XFE_PrefsFontDialog::setSelectionPos(int pos)
  322. {
  323.     PrefsDataFont *fep = m_prefsDataFont;
  324.     int            count = fep->font_displayers_count;
  325.  
  326.     if (pos >= count) return;
  327.  
  328.     if (pos == OUTLINER_INIT_POS) {
  329.         XtVaSetValues(fep->up_button, XmNsensitive, False, NULL);
  330.         XtVaSetValues(fep->down_button, XmNsensitive, False, NULL);
  331.     }
  332.     else {
  333.         fep->font_outliner->selectItemExclusive(pos);
  334.         XtVaSetValues(fep->up_button, XmNsensitive, (pos != 0), NULL);
  335.         XtVaSetValues(fep->down_button, XmNsensitive, (pos != (count-1)), NULL);
  336.     }
  337. }
  338.  
  339. // Member:       deselelctionPos
  340. // Description:  
  341. // Inputs:
  342. // Side effects: 
  343.  
  344. void XFE_PrefsFontDialog::deselectPos(int pos)
  345. {
  346.     PrefsDataFont *fep = m_prefsDataFont;
  347.     int            count = fep->font_displayers_count;
  348.  
  349.     if (pos >= count) return;
  350.  
  351.     fep->font_outliner->deselectItem(pos);
  352. }
  353.  
  354. // Member:       prefsFontCb_ok
  355. // Description:  
  356. // Inputs:
  357. // Side effects: 
  358.  
  359. void XFE_PrefsFontDialog::cb_ok(Widget    w,
  360.                                 XtPointer closure,
  361.                                 XtPointer callData)
  362. {
  363.     XFE_PrefsFontDialog *theDialog = (XFE_PrefsFontDialog *)closure;
  364.     //    PrefsDataFont       *fep = theDialog->m_prefsDataFont;
  365.  
  366.     // Simulate a cancel
  367.  
  368.     theDialog->cb_cancel(w, closure, callData);
  369. }
  370.  
  371. // Member:       prefsFontCb_cancel
  372. // Description:  
  373. // Inputs:
  374. // Side effects: 
  375.  
  376. void XFE_PrefsFontDialog::cb_cancel(Widget    /* w */,
  377.                                     XtPointer closure,
  378.                                     XtPointer /* callData */)
  379. {
  380.     XFE_PrefsFontDialog *theDialog = (XFE_PrefsFontDialog *)closure;
  381.  
  382.     XtRemoveCallback(theDialog->m_chrome, XmNokCallback, cb_ok, theDialog);
  383.     XtRemoveCallback(theDialog->m_chrome, XmNcancelCallback, cb_cancel, theDialog);
  384.  
  385.     // Delete the dialog
  386.  
  387.     delete theDialog;
  388. }
  389.  
  390. // Member:       prefsFontCb_promote
  391. // Description:  
  392. // Inputs:
  393. // Side effects: 
  394.  
  395. void XFE_PrefsFontDialog::cb_promote(Widget    /* w */,
  396.                                      XtPointer /* closure */,
  397.                                      XtPointer /* callData */)
  398. {
  399.     // XFE_PrefsFontDialog *theDialog = (XFE_PrefsFontDialog *)closure;
  400. }
  401.  
  402. // Member:       prefsFontCb_demote
  403. // Description:  
  404. // Inputs:
  405. // Side effects: 
  406.  
  407. void XFE_PrefsFontDialog::cb_demote(Widget    /* w */,
  408.                                     XtPointer /* closure */,
  409.                                     XtPointer /* callData */)
  410. {
  411.     // XFE_PrefsFontDialog *theDialog = (XFE_PrefsFontDialog *)closure;
  412. }
  413.  
  414. /* Outlinable interface methods */
  415.  
  416. void *XFE_PrefsFontDialog::ConvFromIndex(int /* index */)
  417. {
  418.     return (void *)NULL;
  419. }
  420.  
  421. int XFE_PrefsFontDialog::ConvToIndex(void * /* item */)
  422. {
  423.     return 0;
  424. }
  425.  
  426. char*XFE_PrefsFontDialog::getColumnName(int column)
  427. {
  428.     switch (column){
  429.     case OUTLINER_COLUMN_ORDER:
  430.         return STRING_COL_ORDER;
  431.     case OUTLINER_COLUMN_DISPLAYER:
  432.         return STRING_COL_DISPLAYER;
  433.     default:
  434.         XP_ASSERT(0); 
  435.         return 0;
  436.     }
  437. }
  438.  
  439. char *XFE_PrefsFontDialog::getColumnHeaderText(int column)
  440. {
  441.   switch (column) 
  442.     {
  443.     case OUTLINER_COLUMN_ORDER:
  444.     case OUTLINER_COLUMN_DISPLAYER:
  445.       return "";
  446.     default:
  447.       XP_ASSERT(0);
  448.       return 0;
  449.     }
  450. }
  451.  
  452. fe_icon *XFE_PrefsFontDialog::getColumnHeaderIcon(int /* column */)
  453. {
  454.     return 0;
  455.  
  456. }
  457.  
  458. EOutlinerTextStyle XFE_PrefsFontDialog::getColumnHeaderStyle(int /* column */)
  459. {
  460.     return OUTLINER_Default;
  461. }
  462.  
  463. // This method acquires one line of data.
  464. void *XFE_PrefsFontDialog::acquireLineData(int line)
  465. {
  466.     PrefsDataFont *fep = m_prefsDataFont;
  467.     fep->m_rowIndex = line;
  468.     return (void *)1;
  469. }
  470.  
  471. void XFE_PrefsFontDialog::getTreeInfo(Boolean * /* expandable */,
  472.                                       Boolean * /* is_expanded */,
  473.                                       int     * /* depth */,
  474.                                       OutlinerAncestorInfo ** /* ancestor */)
  475. {
  476.     // No-op
  477. }
  478.  
  479.  
  480. EOutlinerTextStyle XFE_PrefsFontDialog::getColumnStyle(int /*column*/)
  481. {
  482.     return OUTLINER_Default;
  483. }
  484.  
  485. char *XFE_PrefsFontDialog::getColumnText(int column)
  486.     PrefsDataFont *fep = m_prefsDataFont;
  487.     static char    line[OUTLINER_COLUMN_MAX_LENGTH+1];
  488.  
  489.     *line = 0;
  490.  
  491.     switch (column) {
  492.  
  493.     case OUTLINER_COLUMN_ORDER:
  494.         sprintf(line, "%d", fep->m_rowIndex+1); 
  495.         break;
  496.  
  497.     case OUTLINER_COLUMN_DISPLAYER:
  498.         sprintf(line, "%s", fep->font_displayers[fep->m_rowIndex]);
  499.         break;
  500.  
  501.     default:
  502.         break;
  503.     }
  504.  
  505.     return line;
  506. }
  507.  
  508. fe_icon *XFE_PrefsFontDialog::getColumnIcon(int /* column */)
  509. {
  510.     return 0;
  511. }
  512.  
  513. void XFE_PrefsFontDialog::releaseLineData()
  514. {
  515.     // No-op
  516. }
  517.  
  518. void XFE_PrefsFontDialog::Buttonfunc(const OutlineButtonFuncData *data)
  519. {
  520.     int row = data->row;
  521.  
  522.     if (row < 0) {
  523.         // header
  524.         return;
  525.     } 
  526.  
  527.     setSelectionPos(data->row);
  528. }
  529.  
  530. void XFE_PrefsFontDialog::Flippyfunc(const OutlineFlippyFuncData * /* data */)
  531. {
  532.     // No-op
  533. }
  534.  
  535. XFE_Outliner *XFE_PrefsFontDialog::getOutliner()
  536. {
  537.     return m_prefsDataFont->font_outliner;
  538. }
  539.