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