]> git.lyx.org Git - lyx.git/blob - src/LyXView.C
690fb0dc4de3110ff908dabdcf4691eab6561076
[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-2000 The LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include <sys/time.h>
19 #include <unistd.h>
20
21 #include "LyXView.h"
22 #include "lyx_main.h"
23 #include "lyxlookup.h"
24 #include "toolbar.h"
25 #include "minibuffer.h"
26 #include "lyxfunc.h"
27 #include "lyx.xpm"
28 #include "debug.h"
29 #include "layout_forms.h"
30 #include "intl.h"
31 #include "lyxrc.h"
32 #include "support/filetools.h"        // OnlyFilename()
33 #include "layout.h"
34 #include "lyxtext.h"
35
36 using std::endl;
37
38 extern FD_form_document * fd_form_document;
39
40 extern void AutoSave();
41 extern char updatetimer;
42 extern void QuitLyX();
43 LyXTextClass::size_type current_layout = 0;
44
45 // This is very temporary
46 BufferView * current_view;
47
48 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
49
50 LyXView::LyXView(int width, int height)
51 {
52         create_form_form_main(width, height);
53         fl_set_form_atclose(_form, C_LyXView_atCloseMainFormCB, 0);
54         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
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
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 (!updatetimer)
86                 return;
87
88         view->view()->hideCursor();
89         view->view()->update(-2);
90
91         /* This update can happen, even when the work area has lost
92          * the focus. So suppress the cursor in that case */
93         updatetimer = 0;
94 }
95
96
97 // Wrapper for the above
98 extern "C" void C_LyXView_UpdateTimerCB(FL_OBJECT * ob, long data)
99 {
100         LyXView::UpdateTimerCB(ob, data);
101 }
102
103
104 // Callback for autosave timer
105 void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
106 {
107         lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
108         AutoSave();
109 }
110
111
112 // Wrapper for the above
113 extern "C" void C_LyXView_AutosaveTimerCB(FL_OBJECT * ob, long data)
114 {
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 {
138         return LyXView::atCloseMainFormCB(form, p);
139 }
140
141
142 void LyXView::setPosition(int x, int y)
143 {
144         fl_set_form_position(_form, x, y);
145 }
146
147
148 void LyXView::show(int place, int border, char const * title)
149 {
150         fl_show_form(_form, place, border, title);
151         minibuffer->Init();
152         InitLyXLookup(fl_display, _form->window);
153 }
154
155
156 void LyXView::create_form_form_main(int width, int height)
157         /* to make this work as it should, .lyxrc should have been
158          * read first; OR maybe this one should be made dynamic.
159          * Hmmmm. Lgb. 
160          * We will probably not have lyxrc before the main form is
161          * initialized, because error messages from lyxrc parsing 
162          * are presented (and rightly so) in GUI popups. Asger. 
163          */
164 {
165         FD_form_main * fdui = static_cast<FD_form_main *>
166                 (fl_calloc(1, sizeof(FD_form_main)));
167
168         _form_main = fdui;
169
170         // the main form
171         _form = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
172         fdui->form_main->u_vdata = this;
173         FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
174         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
175
176         // Parameters for the appearance of the main form
177         int const air = 2;
178         int const bw = abs(fl_get_border_width());
179         
180         //
181         // THE MENUBAR
182         //
183
184         menus = new Menus(this, air);
185
186         //
187         // TOOLBAR
188         //
189
190         toolbar = new Toolbar(this, air, 30 + air + bw);
191
192         // Setup the toolbar
193         toolbar->set(true);
194
195         //
196         // WORKAREA
197         //
198
199         int const ywork = 60 + 2 * air + bw;
200         int const workheight = height - ywork - (25 + 2 * air);
201
202         ::current_view = bufferview = new BufferView(this, air, ywork,
203                                                      width - 3 * air,
204                                                      workheight);
205
206         //
207         // MINIBUFFER
208         //
209
210         minibuffer = new MiniBuffer(this, air, height - (25 + air), 
211                                     width - (2 * air), 25);
212
213         //
214         // TIMERS
215         //
216         
217         // timer_autosave
218         fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
219                                                   0, 0, 0, 0, "Timer");
220         fl_set_object_callback(obj, C_LyXView_AutosaveTimerCB, 0);
221         
222         // timer_update
223         fdui->timer_update = obj = fl_add_timer(FL_HIDDEN_TIMER,
224                                                 0, 0, 0, 0, "Timer");
225         fl_set_object_callback(obj, C_LyXView_UpdateTimerCB, 0);
226         obj->u_vdata = this;
227
228         //
229         // Misc
230         //
231
232         //  assign an icon to main form
233         unsigned int w, h;
234         Pixmap lyx_p, lyx_mask;
235         lyx_p = fl_create_from_pixmapdata(fl_root,
236                                           const_cast<char**>(lyx_xpm),
237                                           &w,
238                                           &h,
239                                           &lyx_mask,
240                                           0,
241                                           0,
242                                           0); // this leaks
243         fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
244
245         // set min size
246         fl_set_form_minsize(fdui->form_main, 50, 50);
247         
248         fl_end_form();
249 }
250
251
252 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
253
254 void LyXView::init()
255 {
256         // Set the textclass choice
257         invalidateLayoutChoice();
258         updateLayoutChoice();
259         UpdateDocumentClassChoice();
260         
261         // Start autosave timer
262         if (lyxrc.autosave)
263                 fl_set_timer(_form_main->timer_autosave, lyxrc.autosave);
264         
265         
266         // Install the raw callback for keyboard events 
267         fl_register_raw_callback(_form,
268                                  KeyPressMask,
269                                  C_LyXView_KeyPressMask_raw_callback);
270         intl->InitKeyMapper(lyxrc.use_kbmap);
271 }
272
273
274 void LyXView::invalidateLayoutChoice()
275 {
276         last_textclass = -1;
277 }
278
279
280 void LyXView::updateLayoutChoice()
281 {
282         // Update the layout display
283         if (!toolbar->combox) return;
284
285         // This has a side-effect that the layouts are not showed when no
286         // document is loaded.
287         if (bufferview == 0 || bufferview->buffer() == 0) {
288                 toolbar->combox->clear();
289                 toolbar->combox->Redraw();
290                 return; 
291         }
292
293         // If textclass is different, we need to update the list
294         if (toolbar->combox->empty() ||
295             (last_textclass != int(buffer()->params.textclass))) {
296                 toolbar->combox->clear();
297                 LyXTextClass const & tc = textclasslist.TextClass(buffer()->params.textclass);
298                 for (LyXTextClass::const_iterator cit = tc.begin();
299                      cit != tc.end(); ++cit) {
300                         if ((*cit).obsoleted_by().empty())
301                                 toolbar->combox->addline((*cit).name().c_str());
302                         else
303                                 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
304                 }
305                 last_textclass = int(buffer()->params.textclass);
306                 current_layout = 0;
307         }
308         // we need to do this.
309         toolbar->combox->Redraw();
310
311         LyXTextClass::size_type layout = bufferview->text->cursor.par->GetLayout();
312
313         if (layout != current_layout){
314                 toolbar->combox->select(layout + 1);
315                 current_layout = layout;
316         }
317 }
318
319
320 void LyXView::UpdateDocumentClassChoice()
321 {
322         // Update the document class display in the document form
323         if (fd_form_document) {
324                 fl_clear_choice(fd_form_document->choice_class);
325                 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
326                      cit != textclasslist.end(); ++cit) {
327                         fl_addto_choice(fd_form_document->choice_class,
328                                         (*cit).description().c_str());
329                 }
330         }
331 }
332
333
334 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
335 // as FL_KEYBOARD events :-(   Matthias 280596
336 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
337 {
338         LyXView * view = static_cast<LyXView*>(fl->u_vdata);
339         int retval = 0;  // 0 means XForms should have a look at this event
340
341         XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
342         static Time last_time_pressed = 0;
343         static Time last_time_released = 0;
344         static unsigned int last_key_pressed = 0;
345         static unsigned int last_key_released = 0;
346         static unsigned int last_state_pressed = 0;
347         static unsigned int last_state_released = 0;
348
349         // funny. Even though the raw_callback is registered with KeyPressMask,
350         // also KeyRelease-events are passed through:-(
351         // [It seems that XForms puts them in pairs... (JMarc)]
352         if (static_cast<XEvent*>(xev)->type == KeyPress
353             && view->bufferview->focus()
354             && view->bufferview->active())
355                 {
356                 last_time_pressed = xke->time;
357                 last_key_pressed = xke->keycode;
358                 last_state_pressed = xke->state;
359                 retval = view->getLyXFunc()
360                         ->processKeyEvent(static_cast<XEvent*>(xev));
361         }
362         else if (static_cast<XEvent*>(xev)->type == KeyRelease
363                  && view->bufferview->focus()
364                  && view->bufferview->active())
365 {
366                 last_time_released = xke->time;
367                 last_key_released = xke->keycode;
368                 last_state_released = xke->state;
369         }
370
371         if (last_key_released == last_key_pressed
372             && last_state_released == last_state_pressed
373             && last_time_released == last_time_pressed) {
374                 // When the diff between last_time_released and
375                 // last_time_pressed is 0, that sinifies an autoreapeat
376                 // at least on my system. It like some feedback from
377                 // others, especially from user running LyX remote.
378                 //lyxerr << "Syncing - purging X events." << endl;
379                 XSync(fl_get_display(), 1);
380                 // This purge make f.ex. scrolling stop imidiatly when
381                 // releaseing the PageDown button. The question is if this
382                 // purging of XEvents can cause any harm...after some testing
383                 // I can see no problems, but I'd like other reports too.
384         }
385         return retval;
386 }
387
388
389 // wrapper for the above
390 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
391 {
392         return LyXView::KeyPressMask_raw_callback(fl, xev);
393 }
394
395
396 // Updates the title of the window with the filename of the current document
397 void LyXView::updateWindowTitle()
398 {
399         static string last_title = "LyX";
400         string title = "LyX";
401
402         if (view()->available()) {
403                 string cur_title = buffer()->fileName();
404                 if (!cur_title.empty()){
405                         title += ": " + OnlyFilename(cur_title);
406                         if (!buffer()->isLyxClean())
407                                 title += _(" (Changed)");
408                         if (buffer()->isReadonly())
409                                 title += _(" (read only)");
410                 }
411         }
412         // Don't update title if it's the same as last time
413         if (title != last_title) {
414                 fl_set_form_title(_form, title.c_str());
415                 last_title = title;
416         }
417 }