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