]> git.lyx.org Git - lyx.git/blob - src/LyXView.C
29d6ae71a0d3c0f13133467728908f8d09f314cc
[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 #include "buffer.h"
36 #include "menus.h"
37 #include "frontends/Dialogs.h"
38 #include "lyx_gui_misc.h"       // [update,Close]AllBufferRelatedDialogs
39
40 using std::endl;
41
42 extern FD_form_document * fd_form_document;
43
44 extern void AutoSave(BufferView *);
45 extern void QuitLyX();
46 LyXTextClass::size_type current_layout = 0;
47
48 // This is very temporary
49 BufferView * current_view;
50
51 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM *, void *);
52
53 #ifdef SIGC_CXX_NAMESPACES
54 using SigC::Connection;
55 using SigC::slot;
56 #endif
57
58 LyXView::LyXView(int width, int height)
59 //      : update_timeout(300)
60 {
61         create_form_form_main(width, height);
62         fl_set_form_atclose(form_, C_LyXView_atCloseMainFormCB, 0);
63         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
64         lyxfunc = new LyXFunc(this);
65         intl = new Intl;
66
67         dialogs_ = new Dialogs(this);
68         // temporary until all dialogs moved into Dialogs.
69         dialogs_->updateBufferDependent
70                 .connect(slot(&updateAllVisibleBufferRelatedDialogs));
71         dialogs_->hideBufferDependent
72                 .connect(slot(&CloseAllBufferRelatedDialogs));
73 }
74
75
76 LyXView::~LyXView()
77 {
78         delete menus;
79         delete toolbar;
80         delete bufferview;
81         delete minibuffer;
82         delete lyxfunc;
83         delete intl;
84         delete dialogs_;
85 }
86
87
88 /// Redraw the main form.
89 void LyXView::redraw() {
90         lyxerr[Debug::INFO] << "LyXView::redraw()" << endl;
91         fl_redraw_form(form_);
92         minibuffer->Activate();
93 }
94
95
96 /// returns the buffer currently shown in the main form.
97 Buffer * LyXView::buffer() const
98 {
99         return bufferview->buffer();
100 }
101
102
103 BufferView * LyXView::view() const
104 {
105         return bufferview;
106 }
107
108
109 FD_form_main * LyXView::getMainForm() const
110 {
111         return form_main_;
112 }
113
114
115 FL_FORM * LyXView::getForm() const
116 {
117         return form_;
118 }
119
120
121 Toolbar * LyXView::getToolbar() const
122 {
123         return toolbar;
124 }
125
126
127 LyXFunc * LyXView::getLyXFunc() const
128 {
129         return lyxfunc;
130 }
131
132
133 MiniBuffer * LyXView::getMiniBuffer() const
134 {
135         return minibuffer;
136 }
137
138
139 Menus * LyXView::getMenus() const
140 {
141         return menus;
142 }
143
144
145 Intl * LyXView::getIntl() const
146 {
147         return intl;
148 }
149
150
151 // Callback for autosave timer
152 void LyXView::AutoSave()
153 {
154         lyxerr[Debug::INFO] << "Running AutoSave()" << endl;
155         if (view()->available())
156                 ::AutoSave(view());
157 }
158
159
160 // Wrapper for the above
161 extern "C" {
162         static
163         void C_LyXView_AutosaveTimerCB(void * ob)
164         {
165                 LyXView * view = static_cast<LyXView*>(ob);
166                 view->AutoSave();
167         }
168 }
169
170 /// Reset autosave timer
171 void LyXView::resetAutosaveTimer()
172 {
173         if (lyxrc.autosave)
174                 autosave_timeout.restart();
175 }
176
177
178 // Callback for close main form from window manager
179 int LyXView::atCloseMainFormCB(FL_FORM *, void *)
180 {
181         QuitLyX();
182         return FL_IGNORE;
183 }
184
185
186 // Wrapper for the above
187 extern "C" int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
188 {
189         return LyXView::atCloseMainFormCB(form, p);
190 }
191
192
193 void LyXView::setPosition(int x, int y)
194 {
195         fl_set_form_position(form_, x, y);
196 }
197
198
199 void LyXView::show(int place, int border, char const * title)
200 {
201         fl_show_form(form_, place, border, title);
202         minibuffer->Init();
203         InitLyXLookup(fl_display, form_->window);
204 }
205
206
207 void LyXView::create_form_form_main(int width, int height)
208         /* to make this work as it should, .lyxrc should have been
209          * read first; OR maybe this one should be made dynamic.
210          * Hmmmm. Lgb. 
211          * We will probably not have lyxrc before the main form is
212          * initialized, because error messages from lyxrc parsing 
213          * are presented (and rightly so) in GUI popups. Asger. 
214          */
215 {
216         FD_form_main * fdui = static_cast<FD_form_main *>
217                 (fl_calloc(1, sizeof(FD_form_main)));
218
219         form_main_ = fdui;
220
221         // the main form
222         form_ = fdui->form_main = fl_bgn_form(FL_NO_BOX, width, height);
223         fdui->form_main->u_vdata = this;
224         FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
225         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
226
227         // Parameters for the appearance of the main form
228         int const air = 2;
229         int const bw = abs(fl_get_border_width());
230         
231         //
232         // THE MENUBAR
233         //
234
235         menus = new Menus(this, air);
236
237         //
238         // TOOLBAR
239         //
240
241         toolbar = new Toolbar(this, air, 30 + air + bw);
242
243         // Setup the toolbar
244         toolbar->set(true);
245
246         //
247         // WORKAREA
248         //
249
250         int const ywork = 60 + 2 * air + bw;
251         int const workheight = height - ywork - (25 + 2 * air);
252
253         ::current_view = bufferview = new BufferView(this, air, ywork,
254                                                      width - 3 * air,
255                                                      workheight);
256
257         //
258         // MINIBUFFER
259         //
260
261         minibuffer = new MiniBuffer(this, air, height - (25 + air), 
262                                     width - (2 * air), 25);
263
264         //
265         // TIMERS
266         //
267
268         autosave_timeout.callback(C_LyXView_AutosaveTimerCB, this);
269
270         //
271         // Misc
272         //
273
274         //  assign an icon to main form
275         unsigned int w, h;
276         Pixmap lyx_p, lyx_mask;
277         lyx_p = fl_create_from_pixmapdata(fl_root,
278                                           const_cast<char**>(lyx_xpm),
279                                           &w,
280                                           &h,
281                                           &lyx_mask,
282                                           0,
283                                           0,
284                                           0); // this leaks
285         fl_set_form_icon(fdui->form_main, lyx_p, lyx_mask);
286
287         // set min size
288         fl_set_form_minsize(fdui->form_main, 50, 50);
289         
290         fl_end_form();
291 }
292
293
294 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
295
296 void LyXView::init()
297 {
298         // Set the textclass choice
299         invalidateLayoutChoice();
300         updateLayoutChoice();
301         UpdateDocumentClassChoice();
302         
303         // Start autosave timer
304         if (lyxrc.autosave)
305 #if 0
306                 fl_set_timer(form_main_->timer_autosave, lyxrc.autosave);
307 #else
308         autosave_timeout.setTimeout(lyxrc.autosave * 1000);
309         autosave_timeout.start();
310 #endif
311         
312         // Install the raw callback for keyboard events 
313         fl_register_raw_callback(form_,
314                                  KeyPressMask,
315                                  C_LyXView_KeyPressMask_raw_callback);
316         intl->InitKeyMapper(lyxrc.use_kbmap);
317 }
318
319
320 void LyXView::invalidateLayoutChoice()
321 {
322         last_textclass = -1;
323 }
324
325
326 void LyXView::updateLayoutChoice()
327 {
328         // Update the layout display
329         if (!toolbar->combox) return;
330
331         // This has a side-effect that the layouts are not showed when no
332         // document is loaded.
333         if (bufferview == 0 || bufferview->buffer() == 0) {
334                 toolbar->combox->clear();
335                 toolbar->combox->Redraw();
336                 return; 
337         }
338
339         // If textclass is different, we need to update the list
340         if (toolbar->combox->empty() ||
341             (last_textclass != int(buffer()->params.textclass))) {
342                 toolbar->combox->clear();
343                 LyXTextClass const & tc = textclasslist.TextClass(buffer()->params.textclass);
344                 for (LyXTextClass::const_iterator cit = tc.begin();
345                      cit != tc.end(); ++cit) {
346                         if ((*cit).obsoleted_by().empty())
347                                 toolbar->combox->addline((*cit).name().c_str());
348                         else
349                                 toolbar->combox->addline(("@N" + (*cit).name()).c_str());
350                 }
351                 last_textclass = int(buffer()->params.textclass);
352                 current_layout = 0;
353         }
354         // we need to do this.
355         toolbar->combox->Redraw();
356
357         LyXTextClass::size_type layout = bufferview->text->cursor.par()->GetLayout();
358
359         if (layout != current_layout){
360                 toolbar->combox->select(layout + 1);
361                 current_layout = layout;
362         }
363 }
364
365
366 void LyXView::UpdateDocumentClassChoice()
367 {
368         // Update the document class display in the document form
369         if (fd_form_document) {
370                 fl_clear_choice(fd_form_document->choice_class);
371                 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
372                      cit != textclasslist.end(); ++cit) {
373                         fl_addto_choice(fd_form_document->choice_class,
374                                         (*cit).description().c_str());
375                 }
376         }
377 }
378
379
380 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
381 // as FL_KEYBOARD events :-(   Matthias 280596
382 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
383 {
384         LyXView * view = static_cast<LyXView*>(fl->u_vdata);
385         int retval = 0;  // 0 means XForms should have a look at this event
386
387         XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
388         static Time last_time_pressed = 0;
389         static Time last_time_released = 0;
390         static unsigned int last_key_pressed = 0;
391         static unsigned int last_key_released = 0;
392         static unsigned int last_state_pressed = 0;
393         static unsigned int last_state_released = 0;
394
395         // funny. Even though the raw_callback is registered with KeyPressMask,
396         // also KeyRelease-events are passed through:-(
397         // [It seems that XForms puts them in pairs... (JMarc)]
398         if (static_cast<XEvent*>(xev)->type == KeyPress
399             && view->bufferview->focus()
400             && view->bufferview->active())
401                 {
402                 last_time_pressed = xke->time;
403                 last_key_pressed = xke->keycode;
404                 last_state_pressed = xke->state;
405                 retval = view->getLyXFunc()
406                         ->processKeyEvent(static_cast<XEvent*>(xev));
407         }
408         else if (static_cast<XEvent*>(xev)->type == KeyRelease
409                  && view->bufferview->focus()
410                  && view->bufferview->active())
411 {
412                 last_time_released = xke->time;
413                 last_key_released = xke->keycode;
414                 last_state_released = xke->state;
415         }
416
417         if (last_key_released == last_key_pressed
418             && last_state_released == last_state_pressed
419             && last_time_released == last_time_pressed) {
420                 // When the diff between last_time_released and
421                 // last_time_pressed is 0, that sinifies an autoreapeat
422                 // at least on my system. It like some feedback from
423                 // others, especially from user running LyX remote.
424                 //lyxerr << "Syncing - purging X events." << endl;
425                 XSync(fl_get_display(), 1);
426                 // This purge make f.ex. scrolling stop imidiatly when
427                 // releaseing the PageDown button. The question is if this
428                 // purging of XEvents can cause any harm...after some testing
429                 // I can see no problems, but I'd like other reports too.
430         }
431         return retval;
432 }
433
434
435 // wrapper for the above
436 extern "C" int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
437 {
438         return LyXView::KeyPressMask_raw_callback(fl, xev);
439 }
440
441
442 // Updates the title of the window with the filename of the current document
443 void LyXView::updateWindowTitle()
444 {
445         static string last_title = "LyX";
446         string title = "LyX";
447
448         if (view()->available()) {
449                 string cur_title = buffer()->fileName();
450                 if (!cur_title.empty()){
451                         title += ": " + OnlyFilename(cur_title);
452                         if (!buffer()->isLyxClean())
453                                 title += _(" (Changed)");
454                         if (buffer()->isReadonly())
455                                 title += _(" (read only)");
456                 }
457         }
458         // Don't update title if it's the same as last time
459         if (title != last_title) {
460                 fl_set_form_title(form_, title.c_str());
461                 last_title = title;
462         }
463 }