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