]> git.lyx.org Git - lyx.git/blob - src/LyXView.C
Initial revision
[lyx.git] / src / LyXView.C
1 // -*- C++ -*-
2 /* This file is part of
3 * ======================================================
4
5 *           LyX, The Document Processor
6 *        
7 *           Copyright (C) 1995 Matthias Ettrich
8 *           Copyright (C) 1995-1998 The LyX Team.
9 *
10 *======================================================*/
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "LyXView.h"
19 #include "lyx_main.h"
20 #include "lyxlookup.h"
21 #include "toolbar.h"
22 #include "minibuffer.h"
23 #include "lyxfunc.h"
24 #include "lyx.xpm"
25 #include "error.h"
26 #include "layout_forms.h"
27 #include "intl.h"
28 #include "lyxrc.h"
29 #include "lyxscreen.h"
30 #include "filetools.h"        // OnlyFilename()
31 #include "layout.h"
32 #include "lyxtext.h"
33
34 //      $Id: LyXView.C,v 1.1 1999/09/27 18:44:36 larsbj Exp $   
35
36 #if !defined(lint) && !defined(WITH_WARNINGS)
37 static char vcid[] = "$Id: LyXView.C,v 1.1 1999/09/27 18:44:36 larsbj Exp $";
38 #endif /* lint */
39
40 extern FD_form_document *fd_form_document;
41 FD_form_main *fd_form_main; /* a pointer to the one in LyXView
42                                should be removed as soon as possible */
43
44 MiniBuffer *minibuffer;/* a pointer to the one in LyXView
45                           should be removed as soon as possible */
46
47 extern void AutoSave();
48 extern char updatetimer;
49 extern void QuitLyX();
50 int current_layout = 0;
51
52 // This is very temporary
53 BufferView *current_view;
54
55
56 LyXView::LyXView(int width, int height)
57 {
58         fd_form_main = create_form_form_main(width,height);
59         fl_set_form_atclose(_form, atCloseMainFormCB, NULL);
60         lyxerr.debug("Initializing LyXFunc");
61         lyxfunc = new LyXFunc(this);
62         intl = new Intl;
63 }
64
65
66 LyXView::~LyXView()
67 {
68         delete menus;
69         delete toolbar;
70         delete bufferview;
71         delete minibuffer;
72         delete lyxfunc;
73         delete intl;
74 }
75
76 /// Redraw the main form.
77 void LyXView::redraw() {
78         lyxerr.debug("LyXView::redraw()");
79         fl_redraw_form(_form);
80         minibuffer->Activate();
81 }
82
83
84 // Callback for update timer
85 void LyXView::UpdateTimerCB(FL_OBJECT *ob, long)
86 {
87         LyXView *view = (LyXView*) ob->u_vdata;
88         if (!view->currentView()->available()) 
89                 return;
90         if (!view->currentView()->getScreen() || !updatetimer)
91                 return;
92
93         view->currentView()->getScreen()->HideCursor();
94         view->currentBuffer()->update(-2);
95         /* This update can happen, even when the work area has lost
96          * the focus. So suppress the cursor in that case */
97         updatetimer = 0;
98 }
99
100
101 // Callback for autosave timer
102 void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
103 {
104         lyxerr.debug("Running AutoSave()");
105         AutoSave();
106 }
107
108
109 /// Reset autosave timer
110 void LyXView::resetAutosaveTimer()
111 {
112         if (lyxrc->autosave)
113                 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
114 }
115
116
117 // Callback for close main form from window manager
118 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
119 {
120         QuitLyX();
121         return FL_IGNORE;
122 }
123
124
125 void LyXView::setPosition(int x, int y)
126 {
127         fl_set_form_position(_form, x, y);
128 }
129
130
131 void LyXView::show(int place, int border, char const* title)
132 {
133         fl_show_form(_form, place, border, title);
134         minibuffer->Init();
135         InitLyXLookup(fl_display, _form->window);
136 }
137
138
139 FD_form_main *LyXView::create_form_form_main(int width, int height)
140         /* to make this work as it should, .lyxrc should have been
141          * read first; OR maybe this one should be made dynamic.
142          * Hmmmm. Lgb. 
143          * We will probably not have lyxrc before the main form is
144          * initialized, because error messages from lyxrc parsing 
145          * are presented (and rightly so) in GUI popups. Asger. 
146          */
147 {
148         FL_OBJECT *obj;
149         
150         FD_form_main *fdui = (FD_form_main *)
151                 fl_calloc(1, sizeof(FD_form_main));
152
153         _form_main = fdui;
154
155         // the main form
156         _form = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
157         fdui->form_main->u_vdata = (void*) this;
158         obj = fl_add_box(FL_FLAT_BOX,0,0,width,height,"");
159         fl_set_object_color(obj,FL_MCOL,FL_MCOL);
160
161         // Parameters for the appearance of the main form
162         const int air = 2;
163         const int bw = abs(fl_get_border_width());
164         
165         //
166         // THE MENUBAR
167         //
168
169         menus = new Menus(this, air);
170
171         //
172         // TOOLBAR
173         //
174
175         toolbar = new Toolbar(lyxrc->toolbar, this, air, 30 + air + bw);
176
177         // Setup the toolbar
178         toolbar->set(true);
179
180         //
181         // WORKAREA
182         //
183
184         const int ywork = 60 + 2*air + bw;
185         const int workheight = height - ywork - (25 + 2*air);
186
187         ::current_view = bufferview = new BufferView(this, air, ywork,
188                                                      width - 3*air,
189                                                      workheight);
190
191         //
192         // MINIBUFFER
193         //
194
195         minibuffer = new MiniBuffer(this, air, height-(25+air), 
196                                           width-(2*air), 25);
197         ::minibuffer = minibuffer; // to be removed later
198
199
200         //
201         // TIMERS
202         //
203         
204         // timer_autosave
205         fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
206                                                   0,0,0,0,"Timer");
207         fl_set_object_callback(obj,AutosaveTimerCB,0);
208         
209         // timer_update
210         fdui->timer_update = obj = fl_add_timer(FL_HIDDEN_TIMER,
211                                                 0,0,0,0,"Timer");
212         fl_set_object_callback(obj,UpdateTimerCB,0);
213         obj->u_vdata = (void*) this;
214
215         //
216         // Misc
217         //
218
219         //  assign an icon to main form
220         unsigned int w, h;
221         Pixmap lyx_p, lyx_mask;
222         lyx_p = fl_create_from_pixmapdata(fl_root,
223                                           lyx_xpm,
224                                           &w,
225                                           &h,
226                                           &lyx_mask,
227                                           0,
228                                           0,
229                                           0); // this leaks
230         fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
231
232         // set min size
233         fl_set_form_minsize(fdui->form_main, 50, 50);
234         
235         fl_end_form();
236
237         return fdui;
238 }
239
240
241 void LyXView::init()
242 {
243         // Set the textclass choice
244         invalidateLayoutChoice();
245         updateLayoutChoice();
246         UpdateDocumentClassChoice();
247         
248         // Start autosave timer
249         if (lyxrc->autosave)
250                 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
251         
252         
253         // Install the raw callback for keyboard events 
254         fl_register_raw_callback(_form,
255                                  KeyPressMask,
256                                  KeyPressMask_raw_callback);
257         intl->InitKeyMapper(lyxrc->use_kbmap);
258 }
259
260
261 void LyXView::invalidateLayoutChoice()
262 {
263         last_textclass = -1;
264 }
265
266
267 void LyXView::updateLayoutChoice()
268 {
269         /* update the layout display */
270         if (!toolbar->combox) return;
271
272         // this has a bi-effect that the layouts are not showed when no
273         // document is loaded.
274         if (bufferview==0 || bufferview->currentBuffer()==0) {
275                 toolbar->combox->clear();
276                 toolbar->combox->Redraw();
277                 return; 
278         }
279
280         // If textclass is different, we need to update the list
281         if (toolbar->combox->empty() ||
282             (last_textclass != currentBuffer()->params.textclass)) {
283                 toolbar->combox->clear();
284                 for (int i = 0;
285                      lyxstyle.NameOfLayout(currentBuffer()->
286                                            params.textclass, i) !="@@end@@";
287                      i++) {
288                         LyXLayout *layout = lyxstyle.
289                                 Style(currentBuffer()->params.textclass, i);
290                         if (layout->obsoleted_by.empty())
291                                 toolbar->combox->addline(layout->name.c_str());
292                         else
293                                 toolbar->combox->addline(("@N"+layout->name).c_str());
294                 }
295                 last_textclass = currentBuffer()->params.textclass;
296                 current_layout = 0;
297         }
298         // we need to do this.
299         toolbar->combox->Redraw();
300         
301         char layout = currentBuffer()->text->cursor.par->GetLayout();
302
303         if (layout != current_layout){
304                 toolbar->combox->select(layout + 1);
305                 current_layout = layout;
306         }
307 }
308
309
310 void LyXView::UpdateDocumentClassChoice()
311 {
312         /* update the document class display in the document form */
313         int i;
314         if (fd_form_document) {
315                 fl_clear_choice(fd_form_document->choice_class);
316                 for (i = 0; lyxstyle.DescOfClass (i)!="@@end@@"; i++) {
317                         fl_addto_choice(fd_form_document->choice_class,
318                                         lyxstyle.DescOfClass(i).c_str());
319                 }
320         }
321 }
322
323
324 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
325 // as FL_KEYBOARD events :-(   Matthias 280596
326 int LyXView::KeyPressMask_raw_callback(FL_FORM *fl, void *xev)
327 {
328         LyXView *view = (LyXView*) fl->u_vdata;
329         int retval = 0;  // 0 means XForms should have a look at this event
330
331         // funny. Even though the raw_callback is registered with KeyPressMask,
332         // also KeyRelease-events are passed through:-(
333         // [It seems that xforms puts them in pairs... (JMarc)]
334         if (((XEvent*)xev)->type == KeyPress
335             && view->bufferview->getWorkArea()->focus
336             && view->bufferview->getWorkArea()->active)
337                 retval = view->getLyXFunc()->processKeyEvent((XEvent*)xev);
338
339         return retval;
340 }
341
342
343 // Updates the title of the window with the filename of the current document
344 void LyXView::updateWindowTitle() {
345         static LString last_title="LyX";
346         LString title = "LyX";
347
348         if (currentView()->available()) {
349                 LString cur_title = currentBuffer()->getFileName();
350                 if (!cur_title.empty()){
351                         title += ": " + OnlyFilename(cur_title);
352                         if (!currentBuffer()->isLyxClean())
353                                 title += _(" (Changed)");
354                         if (currentBuffer()->isReadonly())
355                                 title += _(" (read only)");
356                 }
357         }
358         // Don't update title if it's the same as last time
359         if (title != last_title) {
360                 fl_set_form_title(_form, title.c_str());
361                 last_title = title;
362         }
363 }