dtinfo subtree dtinfo
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Managers / StyleSheetMgr.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * $XConsortium: StyleSheetMgr.C /main/8 1996/10/07 09:02:45 rcs $
25  *
26  * Copyright (c) 1992 HAL Computer Systems International, Ltd.
27  * All rights reserved.  Unpublished -- rights reserved under
28  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
29  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
30  * OR DISCLOSURE.
31  * 
32  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
34  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
36  * INTERNATIONAL, LTD.
37  * 
38  *                         RESTRICTED RIGHTS LEGEND
39  * Use, duplication, or disclosure by the Government is subject
40  * to the restrictions as set forth in subparagraph (c)(l)(ii)
41  * of the Rights in Technical Data and Computer Software clause
42  * at DFARS 252.227-7013.
43  *
44  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
45  *                  1315 Dell Avenue
46  *                  Campbell, CA  95008
47  * 
48  */
49
50 #include <sstream>
51 using namespace std;
52
53 // NOTE: this is just for the Xassert
54 #define C_MessageMgr
55
56 #define C_StyleSheetMgr
57 #define C_NodeMgr
58 #define L_Managers
59
60 #include "Prelude.h"
61
62 #include "oliasdb/locator_hd.h"
63 #include "oliasdb/stylesheet_hd.h"
64 #include "StyleSheet/StyleSheet.h"
65 #include "StyleSheet/StyleSheetExceptions.h"
66
67 LONG_LIVED_CC(StyleSheetMgr,style_sheet_mgr);
68
69 // set to true when we are updating only for purposes of changed style sheet
70 bool g_style_sheet_update = FALSE ;
71
72 //
73 //  Silly hack. There should be a routine that wraps styleparse and
74 //  takes an istream * as an arg.
75 //
76 extern istream *g_stylein;
77
78 //
79 //  Another silly hack.
80 //
81 #include <stdio.h>
82 extern int styleparse();
83 extern void stylerestart(FILE *);
84
85
86 StyleSheetMgr::StyleSheetMgr(): fLastSS(0), fCurrent(0), fStyleSheetRead(0) {
87 }
88
89 StyleSheetMgr::~StyleSheetMgr()
90 {
91     delete fCurrent;
92 }
93
94
95
96 void
97 StyleSheetMgr::font_preference_modified()
98 {
99 #ifdef JBM
100   // 12/27/95 - this is no longer needed because we are not doing live
101   // updates to the nodes when font scale changes. Instead, the font
102   // scale is only updated when a new node is displayed. This is
103   // managed by having NodeMgr create a CanvasRenderer and passing in
104   // the current font scale value
105   mtry
106     {
107       g_style_sheet_update = TRUE;
108       // load new style sheet
109       node_mgr().re_display_all();
110       g_style_sheet_update = FALSE;
111     }
112   mcatch_any()
113     {
114       Xassert(0 == "StyleSheetMgr::font_preference_modified()");
115       g_style_sheet_update = FALSE ;
116       rethrow;
117     }
118   end_try;
119 #endif
120 }
121
122 //
123 //  SWM: Note: This is my plagarism of the code that exists
124 //  in Doc/Node_mmdb.C. I'm trying to not have any mmdb
125 //  dependencies here. However, the style sheet parsing routines
126 //  exist today in the mmdb, so they'll have to be moved soon.
127 //
128 void
129 StyleSheetMgr::initOnlineStyleSheet (UAS_Pointer<UAS_Common> &doc) {
130     UAS_List<UAS_StyleSheet> ssList = doc->style_sheet_list ();
131     UAS_Pointer<UAS_StyleSheet> onlineSS;
132     for (int i = 0; i < ssList.length(); i ++) {
133         if (ssList[i]->style_sheet_type() == SS_ONLINE) {
134             onlineSS = ssList[i];
135             break;
136         }
137     }
138     if (onlineSS == 0) {
139         //  SWM -- THROW EXCEPTION HERE OR USE A DEFAULT SS.
140     }
141     if ((fLastSS == onlineSS) &&
142         (fLastSS->style_sheet_type() == onlineSS->style_sheet_type())) {
143         fCurrent->use();
144         return;
145     }
146     delete fCurrent;
147     fLastSS = onlineSS;
148     fCurrent = new StyleSheet;
149     UAS_String sstextStr = fLastSS->data();
150     istringstream input ((char *) sstextStr);
151     g_stylein = &input;
152 #ifdef DUMP_STYLESHEETS
153   {
154     ofstream output("stylesheet.dmp");
155     output << (char *) sstextStr;
156   }
157 #endif
158
159     if (fStyleSheetRead)
160         stylerestart (0);
161     fStyleSheetRead = 1;
162
163     mtry {
164         styleparse ();
165     }
166     mcatch_noarg (StyleSheetSyntaxError) {
167         fLastSS = 0;
168         delete fCurrent;
169         { //  Don't remove these curlies. For destructors before rethrow
170         fCurrent = new StyleSheet;
171         const char *def =
172         "* { wrap: \"word\", break: \"line\", margin: { left: 20, right: 20} }";
173         istringstream definput(def);
174         input.unsetf (ios::skipws);
175         g_stylein = &definput;
176         stylerestart(0);
177         styleparse();
178         }
179         rethrow;
180     } end_try;
181 }
182
183
184 //  rcs: Note: This is my plagarism of the above code for print.
185
186 void
187 StyleSheetMgr::initPrintStyleSheet (UAS_Pointer<UAS_Common> &doc) {
188     UAS_List<UAS_StyleSheet> ssList = doc->style_sheet_list ();
189     UAS_Pointer<UAS_StyleSheet> printSS;
190     for (int i = 0; i < ssList.length(); i ++) {
191         if (ssList[i]->style_sheet_type() == SS_HARDCOPY) {
192             printSS = ssList[i];
193             break;
194         }
195     }
196     if (printSS == 0) {
197         //  SWM -- THROW EXCEPTION HERE OR USE A DEFAULT SS.
198     }
199     if ((fLastSS == printSS) &&
200         (fLastSS->style_sheet_type() == printSS->style_sheet_type())) {
201         fCurrent->use();
202         return;
203     }
204     delete fCurrent;
205     fLastSS = printSS;
206     fCurrent = new StyleSheet;
207     UAS_String sstextStr = fLastSS->data();
208     istringstream input ((char *) sstextStr);
209     g_stylein = &input;
210 #ifdef DUMP_STYLESHEETS
211   {
212     ofstream output("stylesheet.dmp");
213     output << (char *) sstextStr;
214   }
215 #endif
216
217     if (fStyleSheetRead)
218         stylerestart (0);
219     fStyleSheetRead = 1;
220
221     mtry {
222         styleparse ();
223     }
224     mcatch_noarg (StyleSheetSyntaxError) {
225         fLastSS = 0;
226         delete fCurrent;
227         { //  Don't remove these curlies. For destructors before rethrow
228         fCurrent = new StyleSheet;
229         const char *def =
230         "* { wrap: \"word\", break: \"line\", margin: { left: 20, right: 20} }";
231         istringstream definput(def);
232         input.unsetf (ios::skipws);
233         g_stylein = &definput;
234         stylerestart(0);
235         styleparse();
236         }
237         rethrow;
238     } end_try;
239 }