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