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