]> git.lyx.org Git - lyx.git/blob - src/LyXView.C
more keyboard/keysym changes
[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"
215 int C_LyXView_atCloseMainFormCB(FL_FORM * form, void * p)
216 {
217         return LyXView::atCloseMainFormCB(form, p);
218 }
219
220
221 void LyXView::setPosition(int x, int y)
222 {
223         fl_set_form_position(form_, x, y);
224 }
225
226
227 void LyXView::show(int place, int border, char const * title)
228 {
229         fl_show_form(form_, place, border, title);
230         minibuffer->Init();
231         InitLyXLookup(fl_display, form_->window);
232 }
233
234
235 void LyXView::create_form_form_main(int width, int height)
236         /* to make this work as it should, .lyxrc should have been
237          * read first; OR maybe this one should be made dynamic.
238          * Hmmmm. Lgb. 
239          * We will probably not have lyxrc before the main form is
240          * initialized, because error messages from lyxrc parsing 
241          * are presented (and rightly so) in GUI popups. Asger. 
242          */
243 {
244         // the main form
245         form_ = fl_bgn_form(FL_NO_BOX, width, height);
246         form_->u_vdata = this;
247         FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
248         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
249
250         // Parameters for the appearance of the main form
251         int const air = 2;
252         int const bw = abs(fl_get_border_width());
253         
254         //
255         // THE MENUBAR
256         //
257 #ifdef NEW_MENUBAR
258         menubar = new Menubar(this, menubackend);
259 #else   
260         menus = new Menus(this, air);
261 #endif
262
263         //
264         // TOOLBAR
265         //
266
267         toolbar = new Toolbar(this, air, 30 + air + bw, toolbardefaults);
268
269         // Setup the toolbar
270         toolbar->set(true);
271
272         //
273         // WORKAREA
274         //
275
276         int const ywork = 60 + 2 * air + bw;
277         int const workheight = height - ywork - (25 + 2 * air);
278
279         ::current_view = bufferview = new BufferView(this, air, ywork,
280                                                      width - 3 * air,
281                                                      workheight);
282
283         //
284         // MINIBUFFER
285         //
286
287         minibuffer = new MiniBuffer(this, air, height - (25 + air), 
288                                     width - (2 * air), 25);
289
290         //
291         // TIMERS
292         //
293
294         autosave_timeout.timeout.connect(slot(this, &LyXView::AutoSave));
295         
296         //
297         // Misc
298         //
299
300         //  assign an icon to main form
301         string iconname = LibFileSearch("images", "lyx", "xpm");
302         if (!iconname.empty()) {
303                 unsigned int w, h;
304                 Pixmap lyx_p, lyx_mask;
305                 lyx_p = fl_read_pixmapfile(fl_root,
306                                            iconname.c_str(),
307                                            &w,
308                                            &h,
309                                            &lyx_mask,
310                                            0,
311                                            0,
312                                            0); // this leaks
313                 fl_set_form_icon(form_, lyx_p, lyx_mask);
314         }
315
316         // set min size
317         fl_set_form_minsize(form_, 50, 50);
318         
319         fl_end_form();
320 }
321
322
323 #if 0
324 extern "C"
325 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev);
326 #endif
327
328
329 void LyXView::init()
330 {
331         // Set the textclass choice
332         invalidateLayoutChoice();
333         updateLayoutChoice();
334         UpdateDocumentClassChoice();
335 #ifdef NEW_MENUBAR
336         updateMenubar();
337 #endif
338         
339         // Start autosave timer
340         if (lyxrc.autosave) {
341                 autosave_timeout.setTimeout(lyxrc.autosave * 1000);
342                 autosave_timeout.start();
343         }
344
345 #if 0
346         // Install the raw callback for keyboard events 
347         fl_register_raw_callback(form_,
348                                  KeyPressMask,
349                                  C_LyXView_KeyPressMask_raw_callback);
350 #endif
351         intl->InitKeyMapper(lyxrc.use_kbmap);
352 }
353
354
355 void LyXView::invalidateLayoutChoice()
356 {
357         last_textclass = -1;
358 }
359
360
361 void LyXView::updateLayoutChoice()
362 {
363         // This has a side-effect that the layouts are not showed when no
364         // document is loaded.
365         if (!view() || !view()->buffer()) {
366                 toolbar->clearLayoutList();
367                 return; 
368         }
369
370         // Update the layout display
371         if (last_textclass != int(buffer()->params.textclass)) {
372                 toolbar->updateLayoutList(true);
373                 last_textclass = int(buffer()->params.textclass);
374                 current_layout = 0;
375         } else
376                 toolbar->updateLayoutList(false);
377
378         
379
380         LyXTextClass::size_type layout =
381                 bufferview->text->cursor.par()->GetLayout();
382
383         if (layout != current_layout){
384                 toolbar->setLayout(layout);
385                 current_layout = layout;
386         }
387 }
388
389
390 void LyXView::UpdateDocumentClassChoice()
391 {
392         // Update the document class display in the document form
393         if (fd_form_document) {
394                 fl_clear_choice(fd_form_document->choice_class);
395                 for (LyXTextClassList::const_iterator cit = textclasslist.begin();
396                      cit != textclasslist.end(); ++cit) {
397                         fl_addto_choice(fd_form_document->choice_class,
398                                         (*cit).description().c_str());
399                 }
400         }
401 }
402
403
404 #if 0
405 // This is necessary, since FL_FREE-Objects doesn't get all keypress events
406 // as FL_KEYBOARD events :-(   Matthias 280596
407 int LyXView::KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
408 {
409         LyXView * view = static_cast<LyXView*>(fl->u_vdata);
410         int retval = 0;  // 0 means XForms should have a look at this event
411
412         XKeyEvent * xke = static_cast<XKeyEvent*>(xev);
413         static Time last_time_pressed = 0;
414         static Time last_time_released = 0;
415         static unsigned int last_key_pressed = 0;
416         static unsigned int last_key_released = 0;
417         static unsigned int last_state_pressed = 0;
418         static unsigned int last_state_released = 0;
419
420         // funny. Even though the raw_callback is registered with KeyPressMask,
421         // also KeyRelease-events are passed through:-(
422         // [It seems that XForms puts them in pairs... (JMarc)]
423         if (static_cast<XEvent*>(xev)->type == KeyPress
424             && view->bufferview->focus()
425             && view->bufferview->active())
426                 {
427                 last_time_pressed = xke->time;
428                 last_key_pressed = xke->keycode;
429                 last_state_pressed = xke->state;
430                 retval = view->getLyXFunc()
431                         ->processKeyEvent(static_cast<XEvent*>(xev));
432         }
433         else if (static_cast<XEvent*>(xev)->type == KeyRelease
434                  && view->bufferview->focus()
435                  && view->bufferview->active())
436 {
437                 last_time_released = xke->time;
438                 last_key_released = xke->keycode;
439                 last_state_released = xke->state;
440         }
441
442         if (last_key_released == last_key_pressed
443             && last_state_released == last_state_pressed
444             && last_time_released == last_time_pressed) {
445                 // When the diff between last_time_released and
446                 // last_time_pressed is 0, that sinifies an autoreapeat
447                 // at least on my system. It like some feedback from
448                 // others, especially from user running LyX remote.
449                 lyxerr[Debug::KEY] << "Syncing - purging X events." << endl;
450                 XSync(fl_get_display(), 1);
451                 // This purge make f.ex. scrolling stop imidiatly when
452                 // releaseing the PageDown button. The question is if this
453                 // purging of XEvents can cause any harm...after some testing
454                 // I can see no problems, but I'd like other reports too.
455         }
456         return retval;
457 }
458
459
460 // wrapper for the above
461 extern "C"
462 int C_LyXView_KeyPressMask_raw_callback(FL_FORM * fl, void * xev)
463 {
464         return LyXView::KeyPressMask_raw_callback(fl, xev);
465 }
466 #endif
467
468
469 // Updates the title of the window with the filename of the current document
470 void LyXView::updateWindowTitle()
471 {
472         static string last_title = "LyX";
473         string title = "LyX";
474
475         if (view()->available()) {
476                 string cur_title = buffer()->fileName();
477                 if (!cur_title.empty()){
478                         title += ": " + OnlyFilename(cur_title);
479                         if (!buffer()->isLyxClean())
480                                 title += _(" (Changed)");
481                         if (buffer()->isReadonly())
482                                 title += _(" (read only)");
483                 }
484         }
485         // Don't update title if it's the same as last time
486         if (title != last_title) {
487                 fl_set_form_title(form_, title.c_str());
488                 last_title = title;
489         }
490 }
491
492
493 void LyXView::showState()
494 {
495         getMiniBuffer()->Set(CurrentState(view()));
496         getToolbar()->update();
497         menubar->update();
498 }