Add GNU LGPL headers to all .c .C and .h files
[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 <stream.h>
51
52 // NOTE: this is just for the Xassert
53 #define C_MessageMgr
54
55 #define C_StyleSheetMgr
56 #define C_NodeMgr
57 #define L_Managers
58
59 #include "Prelude.h"
60
61 #include "oliasdb/locator_hd.h"
62 #include "oliasdb/stylesheet_hd.h"
63 #include "StyleSheet/StyleSheet.h"
64 #include "StyleSheet/StyleSheetExceptions.h"
65
66 LONG_LIVED_CC(StyleSheetMgr,style_sheet_mgr);
67
68 // set to true when we are updating only for purposes of changed style sheet
69 bool g_style_sheet_update = FALSE ;
70
71 //
72 //  Silly hack. There should be a routine that wraps styleparse and
73 //  takes an istream * as an arg.
74 //
75 extern istream *g_stylein;
76
77 //
78 //  Another silly hack.
79 //
80 #include <stdio.h>
81 extern int styleparse();
82 extern void stylerestart(FILE *);
83
84
85 StyleSheetMgr::StyleSheetMgr(): fLastSS(0), fCurrent(0), fStyleSheetRead(0) {
86 }
87
88 StyleSheetMgr::~StyleSheetMgr()
89 {
90     delete fCurrent;
91 }
92
93
94
95 void
96 StyleSheetMgr::font_preference_modified()
97 {
98 #ifdef JBM
99   // 12/27/95 - this is no longer needed because we are not doing live
100   // updates to the nodes when font scale changes. Instead, the font
101   // scale is only updated when a new node is displayed. This is
102   // managed by having NodeMgr create a CanvasRenderer and passing in
103   // the current font scale value
104   try
105     {
106       g_style_sheet_update = TRUE;
107       // load new style sheet
108       node_mgr().re_display_all();
109       g_style_sheet_update = FALSE;
110     }
111   catch_any()
112     {
113       Xassert(0 == "StyleSheetMgr::font_preference_modified()");
114       g_style_sheet_update = FALSE ;
115       rethrow;
116     }
117   end_try;
118 #endif
119 }
120
121 //
122 //  SWM: Note: This is my plagarism of the code that exists
123 //  in Doc/Node_mmdb.C. I'm trying to not have any mmdb
124 //  dependencies here. However, the style sheet parsing routines
125 //  exist today in the mmdb, so they'll have to be moved soon.
126 //
127 void
128 StyleSheetMgr::initOnlineStyleSheet (UAS_Pointer<UAS_Common> &doc) {
129     UAS_List<UAS_StyleSheet> ssList = doc->style_sheet_list ();
130     UAS_Pointer<UAS_StyleSheet> onlineSS;
131     for (int i = 0; i < ssList.length(); i ++) {
132         if (ssList[i]->style_sheet_type() == SS_ONLINE) {
133             onlineSS = ssList[i];
134             break;
135         }
136     }
137     if (onlineSS == 0) {
138         //  SWM -- THROW EXCEPTION HERE OR USE A DEFAULT SS.
139     }
140     if ((fLastSS == onlineSS) &&
141         (fLastSS->style_sheet_type() == onlineSS->style_sheet_type())) {
142         fCurrent->use();
143         return;
144     }
145     delete fCurrent;
146     fLastSS = onlineSS;
147     fCurrent = new StyleSheet;
148     UAS_String sstextStr = fLastSS->data();
149     istrstream input ((char *) sstextStr);
150     g_stylein = &input;
151 #ifdef DUMP_STYLESHEETS
152   {
153     ofstream output("stylesheet.dmp");
154     output << (char *) sstextStr;
155   }
156 #endif
157
158     if (fStyleSheetRead)
159         stylerestart (0);
160     fStyleSheetRead = 1;
161
162     try {
163         styleparse ();
164     }
165     catch_noarg (StyleSheetSyntaxError) {
166         fLastSS = 0;
167         delete fCurrent;
168         { //  Don't remove these curlies. For destructors before rethrow
169         fCurrent = new StyleSheet;
170         const char *def =
171         "* { wrap: \"word\", break: \"line\", margin: { left: 20, right: 20} }";
172         istrstream definput(def);
173         input.unsetf (ios::skipws);
174         g_stylein = &definput;
175         stylerestart(0);
176         styleparse();
177         }
178         rethrow;
179     } end_try;
180 }
181
182
183 //  rcs: Note: This is my plagarism of the above code for print.
184
185 void
186 StyleSheetMgr::initPrintStyleSheet (UAS_Pointer<UAS_Common> &doc) {
187     UAS_List<UAS_StyleSheet> ssList = doc->style_sheet_list ();
188     UAS_Pointer<UAS_StyleSheet> printSS;
189     for (int i = 0; i < ssList.length(); i ++) {
190         if (ssList[i]->style_sheet_type() == SS_HARDCOPY) {
191             printSS = ssList[i];
192             break;
193         }
194     }
195     if (printSS == 0) {
196         //  SWM -- THROW EXCEPTION HERE OR USE A DEFAULT SS.
197     }
198     if ((fLastSS == printSS) &&
199         (fLastSS->style_sheet_type() == printSS->style_sheet_type())) {
200         fCurrent->use();
201         return;
202     }
203     delete fCurrent;
204     fLastSS = printSS;
205     fCurrent = new StyleSheet;
206     UAS_String sstextStr = fLastSS->data();
207     istrstream input ((char *) sstextStr);
208     g_stylein = &input;
209 #ifdef DUMP_STYLESHEETS
210   {
211     ofstream output("stylesheet.dmp");
212     output << (char *) sstextStr;
213   }
214 #endif
215
216     if (fStyleSheetRead)
217         stylerestart (0);
218     fStyleSheetRead = 1;
219
220     try {
221         styleparse ();
222     }
223     catch_noarg (StyleSheetSyntaxError) {
224         fLastSS = 0;
225         delete fCurrent;
226         { //  Don't remove these curlies. For destructors before rethrow
227         fCurrent = new StyleSheet;
228         const char *def =
229         "* { wrap: \"word\", break: \"line\", margin: { left: 20, right: 20} }";
230         istrstream definput(def);
231         input.unsetf (ios::skipws);
232         g_stylein = &definput;
233         stylerestart(0);
234         styleparse();
235         }
236         rethrow;
237     } end_try;
238 }