]> git.lyx.org Git - lyx.git/blob - src/LyXView.C
new painter,workarea and lcolor. Read the diff/sources and ChangeLog...
[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 <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 "lyxscreen.h"
33 #include "support/filetools.h"        // OnlyFilename()
34 #include "layout.h"
35 #include "lyxtext.h"
36
37 extern FD_form_document * fd_form_document;
38
39 extern void AutoSave();
40 extern char updatetimer;
41 extern void QuitLyX();
42 int current_layout = 0;
43
44 // This is very temporary
45 BufferView * current_view;
46
47 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
48
49 LyXView::LyXView(int width, int height)
50 {
51         create_form_form_main(width, height);
52         fl_set_form_atclose(_form, C_LyXView_atCloseMainFormCB, 0);
53         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
54         lyxfunc = new LyXFunc(this);
55         intl = new Intl;
56 }
57
58
59 LyXView::~LyXView()
60 {
61         delete menus;
62         delete toolbar;
63         delete bufferview;
64         delete minibuffer;
65         delete lyxfunc;
66         delete intl;
67 }
68
69
70 /// Redraw the main form.
71 void LyXView::redraw() {
72         lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
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 = static_cast<LyXView*>(ob->u_vdata);
82         if (!view->view()->available()) 
83                 return;
84         if (!view->view()->getScreen() || !updatetimer)
85                 return;
86
87         view->view()->getScreen()->HideCursor();
88         view->view()->update(-2);
89
90         /* This update can happen, even when the work area has lost
91          * the focus. So suppress the cursor in that case */
92         updatetimer = 0;
93 }
94
95
96 // Wrapper for the above
97 extern "C" void C_LyXView_UpdateTimerCB(FL_OBJECT * ob, long data)
98 {
99         LyXView::UpdateTimerCB(ob, data);
100 }
101
102
103 // Callback for autosave timer
104 void LyXView::AutosaveTimerCB(FL_OBJECT *, long)
105 {
106         lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
107         AutoSave();
108 }
109
110
111 // Wrapper for the above
112 extern "C" void C_LyXView_AutosaveTimerCB(FL_OBJECT * ob, long data)
113 {
114         LyXView::AutosaveTimerCB(ob, data);
115 }
116
117
118 /// Reset autosave timer
119 void LyXView::resetAutosaveTimer()
120 {
121         if (lyxrc->autosave)
122                 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
123 }
124
125
126 // Callback for close main form from window manager
127 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
128 {
129         QuitLyX();
130         return FL_IGNORE;
131 }
132
133
134 // Wrapper for the above
135 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
136 {
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 void 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         FD_form_main * fdui = static_cast<FD_form_main *>
165                 (fl_calloc(1, sizeof(FD_form_main)));
166
167         _form_main = fdui;
168
169         // the main form
170         _form = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
171         fdui->form_main->u_vdata = this;
172         FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
173         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
174
175         // Parameters for the appearance of the main form
176         int const air = 2;
177         int const bw = abs(fl_get_border_width());
178         
179         //
180         // THE MENUBAR
181         //
182
183         menus = new Menus(this, air);
184
185         //
186         // TOOLBAR
187         //
188
189         toolbar = new Toolbar(lyxrc->toolbar, this, air, 30 + air + bw);
190
191         // Setup the toolbar
192         toolbar->set(true);
193
194         //
195         // WORKAREA
196         //
197
198         int const ywork = 60 + 2 * air + bw;
199         int const workheight = height - ywork - (25 + 2 * air);
200
201         ::current_view = bufferview = new BufferView(this, air, ywork,
202                                                      width - 3 * air,
203                                                      workheight);
204
205         //
206         // MINIBUFFER
207         //
208
209         minibuffer = new MiniBuffer(this, air, height - (25 + air), 
210                                     width - (2 * air), 25);
211
212         //
213         // TIMERS
214         //
215         
216         // timer_autosave
217         fdui->timer_autosave = obj = fl_add_timer(FL_HIDDEN_TIMER,
218                                                   0, 0, 0, 0, "Timer");
219         fl_set_object_callback(obj, C_LyXView_AutosaveTimerCB, 0);
220         
221         // timer_update
222         fdui->timer_update = obj = fl_add_timer(FL_HIDDEN_TIMER,
223                                                 0, 0, 0, 0, "Timer");
224         fl_set_object_callback(obj, C_LyXView_UpdateTimerCB, 0);
225         obj->u_vdata = this;
226
227         //
228         // Misc
229         //
230
231         //  assign an icon to main form
232         unsigned int w, h;
233         Pixmap lyx_p, lyx_mask;
234         lyx_p = fl_create_from_pixmapdata(fl_root,
235                                           const_cast<char**>(lyx_xpm),
236                                           &w,
237                                           &h,
238                                           &lyx_mask,
239                                           0,
240                                           0,
241                                           0); // this leaks
242         fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
243
244         // set min size
245         fl_set_form_minsize(fdui->form_main, 50, 50);
246         
247         fl_end_form();
248 }
249
250
251 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
252
253 void LyXView::init()
254 {
255         // Set the textclass choice
256         invalidateLayoutChoice();
257         updateLayoutChoice();
258         UpdateDocumentClassChoice();
259         
260         // Start autosave timer
261         if (lyxrc->autosave)
262                 fl_set_timer(_form_main->timer_autosave, lyxrc->autosave);
263         
264         
265         // Install the raw callback for keyboard events 
266         fl_register_raw_callback(_form,
267                                  KeyPressMask,
268                                  C_LyXView_KeyPressMask_raw_callback);
269         intl->InitKeyMapper(lyxrc->use_kbmap);
270 }
271
272
273 void LyXView::invalidateLayoutChoice()
274 {
275         last_textclass = -1;
276 }
277
278
279 void LyXView::updateLayoutChoice()
280 {
281         // Update the layout display
282         if (!toolbar->combox) return;
283
284         // This has a side-effect that the layouts are not showed when no
285         // document is loaded.
286         if (bufferview == 0 || bufferview->buffer() == 0) {
287                 toolbar->combox->clear();
288                 toolbar->combox->Redraw();
289                 return; 
290         }
291
292         // If textclass is different, we need to update the list
293         if (toolbar->combox->empty() ||
294             (last_textclass != int(buffer()->params.textclass))) {
295                 toolbar->combox->clear();
296                 LyXTextClass const & tc = textclasslist.TextClass(buffer()->params.textclass);
297                 for (LyXTextClass::const_iterator cit = tc.begin();
298                      cit != tc.end(); ++cit) {
299                         if ((*cit).obsoleted_by().empty())
300                                 toolbar->combox->addline((*cit).name().c_str());
301                         else
302                                 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
303                 }
304                 last_textclass = int(buffer()->params.textclass);
305                 current_layout = 0;
306         }
307         // we need to do this.
308         toolbar->combox->Redraw();
309
310         char layout = bufferview->text->cursor.par->GetLayout();
311
312         if (layout != current_layout){
313                 toolbar->combox->select(layout + 1);
314                 current_layout = layout;
315         }
316 }
317
318
319 void LyXView::UpdateDocumentClassChoice()
320 {
321         // Update the document class display in the document form
322         if (fd_form_document) {
323                 fl_clear_choice(fd_form_document->choice_class);
324                 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
325                      cit != textclasslist.end(); ++cit) {
326                         fl_addto_choice(fd_form_document->choice_class,
327                                         (*cit).description().c_str());
328                 }
329         }
330 }
331
332
333 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
334 // as FL_KEYBOARD events :-(   Matthias 280596
335 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
336 {
337         LyXView * view = static_cast<LyXView*>(fl->u_vdata);
338         int retval = 0;  // 0 means XForms should have a look at this event
339
340 #define USE_XSYNC 1
341 #ifdef USE_XSYNC
342         XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
343         static Time last_time_pressed = 0;
344         static Time last_time_released = 0;
345         static unsigned int last_key_pressed = 0;
346         static unsigned int last_key_released = 0;
347         static unsigned int last_state_pressed = 0;
348         static unsigned int last_state_released = 0;
349 #endif
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 #ifdef NEW_WA
355             && view->bufferview->focus()
356             && view->bufferview->active())
357 #else
358             && view->bufferview->getWorkArea()->focus
359             && view->bufferview->getWorkArea()->active)
360 #endif
361                 {
362 #ifdef USE_XSYNC
363                 last_time_pressed = xke->time;
364                 last_key_pressed = xke->keycode;
365                 last_state_pressed = xke->state;
366 #endif
367                 retval = view->getLyXFunc()
368                         ->processKeyEvent(static_cast<XEvent*>(xev));
369         }
370 #ifdef USE_XSYNC
371         else if (static_cast<XEvent*>(xev)->type == KeyRelease
372 #ifdef NEW_WA
373                  && view->bufferview->focus()
374                  && view->bufferview->active())
375 #else
376                    && view->bufferview->getWorkArea()->focus
377                    && view->bufferview->getWorkArea()->active)
378 #endif
379 {
380                 last_time_released = xke->time;
381                 last_key_released = xke->keycode;
382                 last_state_released = xke->state;
383         }
384
385         if (last_key_released == last_key_pressed
386             && last_state_released == last_state_pressed
387             && last_time_released == last_time_pressed) {
388                 // When the diff between last_time_released and
389                 // last_time_pressed is 0, that sinifies an autoreapeat
390                 // at least on my system. It like some feedback from
391                 // others, especially from user running LyX remote.
392                 //lyxerr << "Syncing - purging X events." << endl;
393                 XSync(fl_get_display(), 1);
394                 // This purge make f.ex. scrolling stop imidiatly when
395                 // releaseing the PageDown button. The question is if this
396                 // purging of XEvents can cause any harm...after some testing
397                 // I can see no problems, but I'd like other reports too.
398         }
399 #endif
400         return retval;
401 }
402
403
404 // wrapper for the above
405 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
406 {
407         return LyXView::KeyPressMask_raw_callback(fl, xev);
408 }
409
410
411 // Updates the title of the window with the filename of the current document
412 void LyXView::updateWindowTitle()
413 {
414         static string last_title = "LyX";
415         string title = "LyX";
416
417         if (view()->available()) {
418                 string cur_title = buffer()->fileName();
419                 if (!cur_title.empty()){
420                         title += ": " + OnlyFilename(cur_title);
421                         if (!buffer()->isLyxClean())
422                                 title += _(" (Changed)");
423                         if (buffer()->isReadonly())
424                                 title += _(" (read only)");
425                 }
426         }
427         // Don't update title if it's the same as last time
428         if (title != last_title) {
429                 fl_set_form_title(_form, title.c_str());
430                 last_title = title;
431         }
432 }