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