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