]> git.lyx.org Git - lyx.git/blob - src/bullet_forms_cb.C
7218992bec9ccedcb209f0452a7e7d5c265ad804
[lyx.git] / src / bullet_forms_cb.C
1 /* Bullet form callbacks */
2 #include <config.h>
3 #include <cstdlib>
4 #include FORMS_H_LOCATION
5 #include XPM_H_LOCATION
6 #include "bullet_forms.h"
7 #include "bmtable.h"
8 #include "buffer.h"
9 #include "bufferparams.h"
10 #include "support/filetools.h"
11 #include "gettext.h"
12 #include "lyx_gui_misc.h" // CancelCloseBoxCB + WriteAlert
13
14 extern int mono_video;
15
16 extern BufferView * current_view;
17 FD_form_bullet * fd_form_bullet;
18
19 static int current_bullet_panel;
20 static int current_bullet_depth;
21
22 /** Creates or raises the bullet popup and sets appropriate form values
23   */
24 void bulletForm()
25 {
26         if (!fd_form_bullet) {
27                 if (!mono_video &&
28                     (XpmVersion < 4
29                         || (XpmVersion == 4 && XpmRevision < 7))) {
30                         WriteAlert(_("Sorry, your libXpm is too old."),
31                         _("This feature requires xpm-4.7 (a.k.a 3.4g) or newer."),
32                         _("Try running LyX in mono mode (lyx  -Mono)."));
33                         return;
34                 }
35                 fd_form_bullet = create_form_form_bullet();
36                 fl_addto_choice(fd_form_bullet->choice_bullet_size,
37                                 _(" default | tiny | script | footnote | small |"
38                                 " normal | large | Large | LARGE | huge | Huge"));
39                 fl_set_choice(fd_form_bullet->choice_bullet_size, 1);
40                 fl_set_form_atclose(fd_form_bullet->form_bullet,
41                                     CancelCloseBoxCB, 0);
42         }
43
44         if (updateBulletForm()) {
45                 // Show form
46                 if (fd_form_bullet->form_bullet->visible) {
47                         fl_raise_form(fd_form_bullet->form_bullet);
48                 }
49                 else {
50                         fl_show_form(fd_form_bullet->form_bullet,
51                                      FL_PLACE_MOUSE, FL_FULLBORDER,
52                                      _("Itemize Bullet Selection"));
53                 }
54         }
55 }
56
57 bool updateBulletForm()
58 {
59         bool update = true;
60         if (!fd_form_bullet) {
61                 return false;
62         }
63         if (!current_view->available()) {
64                 update = false;
65         } else if (current_view->buffer()->isReadonly()
66                    || current_view->buffer()->isLinuxDoc()) {
67                 fl_deactivate_object (fd_form_bullet->button_ok);
68                 fl_deactivate_object (fd_form_bullet->button_apply);
69                 fl_set_object_lcol (fd_form_bullet->button_ok, FL_INACTIVE);
70                 fl_set_object_lcol (fd_form_bullet->button_apply, FL_INACTIVE);
71                 fl_deactivate_object (fd_form_bullet->bmtable_bullet_panel);
72                 fl_deactivate_object (fd_form_bullet->choice_bullet_size);
73                 fl_deactivate_object (fd_form_bullet->input_bullet_latex);
74                 update = false;
75         } else {
76                 fl_activate_object (fd_form_bullet->button_ok);
77                 fl_activate_object (fd_form_bullet->button_apply);
78                 fl_set_object_lcol (fd_form_bullet->button_ok, FL_BLACK);
79                 fl_set_object_lcol (fd_form_bullet->button_apply, FL_BLACK);
80                 fl_activate_object (fd_form_bullet->bmtable_bullet_panel);
81                 fl_activate_object (fd_form_bullet->choice_bullet_size);
82                 fl_activate_object (fd_form_bullet->input_bullet_latex);
83         }
84
85         if (update) {
86                 // any settings that need doing each time
87                 fl_set_button(fd_form_bullet->radio_bullet_depth_1, 1);
88                 fl_set_input(fd_form_bullet->input_bullet_latex,
89                              current_view->buffer()
90                              ->params.user_defined_bullets[0].c_str());
91                 fl_set_choice(fd_form_bullet->choice_bullet_size,
92                               current_view->buffer()
93                               ->params.user_defined_bullets[0].getSize() + 2);
94         } else {
95                 if (fd_form_bullet->form_bullet->visible) {
96                         fl_hide_form(fd_form_bullet->form_bullet);
97                 }
98         }
99         return update;
100 }
101
102 /*---------------------------------------*/
103 /* callbacks for form form_bullet        */
104
105 void BulletOKCB(FL_OBJECT *ob, long data)
106 {
107         BulletApplyCB(ob, data);
108         BulletCancelCB(ob, data);
109 }
110
111
112 void BulletApplyCB(FL_OBJECT * /*ob*/, long /*data*/ )
113 {
114         // update the bullet settings
115         BufferParams & param = current_view->buffer()->params;
116
117         // a little bit of loop unrolling
118         param.user_defined_bullets[0] = param.temp_bullets[0];
119         param.user_defined_bullets[1] = param.temp_bullets[1];
120         param.user_defined_bullets[2] = param.temp_bullets[2];
121         param.user_defined_bullets[3] = param.temp_bullets[3];
122         current_view->buffer()->markDirty();
123 }
124
125
126 void BulletCancelCB(FL_OBJECT * /*ob*/, long /*data*/ )
127 {
128         fl_hide_form(fd_form_bullet->form_bullet);
129         // this avoids confusion when reopening
130         BufferParams & param = current_view->buffer()->params;
131         param.temp_bullets[0] = param.user_defined_bullets[0];
132         param.temp_bullets[1] = param.user_defined_bullets[1];
133         param.temp_bullets[2] = param.user_defined_bullets[2];
134         param.temp_bullets[3] = param.user_defined_bullets[3];
135 }
136
137
138 void InputBulletLaTeXCB(FL_OBJECT *, long)
139 {
140         // fill-in code for callback
141         BufferParams & param = current_view->buffer()->params;
142
143         param.temp_bullets[current_bullet_depth].setText(
144                 fl_get_input(fd_form_bullet->input_bullet_latex));
145 }
146
147
148 void ChoiceBulletSizeCB(FL_OBJECT * ob, long /*data*/ )
149 {
150         BufferParams & param = current_view->buffer()->params;
151
152         // convert from 1-6 range to -1-4 
153         param.temp_bullets[current_bullet_depth].setSize(fl_get_choice(ob) - 2);
154         fl_set_input(fd_form_bullet->input_bullet_latex,
155                                  param.temp_bullets[current_bullet_depth].c_str());
156 }
157
158
159 void BulletDepthCB(FL_OBJECT * ob, long data)
160 {
161         /* Should I do the following:                                 */
162         /*  1. change to the panel that the current bullet belongs in */
163         /*  2. show that bullet as selected                           */
164         /*  3. change the size setting to the size of the bullet in Q.*/
165         /*  4. display the latex equivalent in the latex box          */
166         /*                                                            */
167         /* I'm inclined to just go with 3 and 4 at the moment and     */
168         /* maybe try to support the others later                      */
169         BufferParams & param = current_view->buffer()->params;
170
171         switch (fl_get_button_numb(ob)) {
172         case 3:
173                 // right mouse button resets to default
174                 param.temp_bullets[data] = ITEMIZE_DEFAULTS[data];
175         default:
176                 current_bullet_depth = data;
177                 fl_set_input(fd_form_bullet->input_bullet_latex,
178                              param.temp_bullets[data].c_str());
179                 fl_set_choice(fd_form_bullet->choice_bullet_size,
180                               param.temp_bullets[data].getSize() + 2);
181         }
182 }
183
184
185 void BulletPanelCB(FL_OBJECT * /*ob*/, long data)
186 {
187         /* Here we have to change the background pixmap to that selected */
188         /* by the user. (eg. standard.xpm, psnfss1.xpm etc...)           */
189
190         if (data != current_bullet_panel) {
191                 fl_freeze_form(fd_form_bullet->form_bullet);
192                 current_bullet_panel = data;
193
194                 /* free the current pixmap */
195                 fl_free_bmtable_pixmap(fd_form_bullet->bmtable_bullet_panel);
196                 string new_panel;
197                 switch (data) {
198                         /* display the new one */
199                 case 0 :
200                         new_panel = "standard";
201                         break;
202                 case 1 :
203                         new_panel = "amssymb";
204                         break;
205                 case 2 :
206                         new_panel = "psnfss1";
207                         break;
208                 case 3 :
209                         new_panel = "psnfss2";
210                         break;
211                 case 4 :
212                         new_panel = "psnfss3";
213                         break;
214                 case 5 :
215                         new_panel = "psnfss4";
216                         break;
217                 default :
218                         /* something very wrong happened */
219                         // play it safe for now but should be an exception
220                         current_bullet_panel = 0;  // standard panel
221                         new_panel = "standard";
222                         break;
223                 }
224                 if (mono_video) {
225                         new_panel += ".xbm";
226                         fl_set_bmtable_file(fd_form_bullet->bmtable_bullet_panel, 6, 6,
227                                LibFileSearch("images", new_panel.c_str()).c_str());
228                 } else {
229                         new_panel += ".xpm";
230                         fl_set_bmtable_pixmap_file(fd_form_bullet->bmtable_bullet_panel, 6, 6,
231                                LibFileSearch("images", new_panel.c_str()).c_str());
232                 }
233                 fl_redraw_object(fd_form_bullet->bmtable_bullet_panel);
234                 fl_unfreeze_form(fd_form_bullet->form_bullet);
235         }
236 }
237
238
239 void BulletBMTableCB(FL_OBJECT *ob, long /*data*/ )
240 {
241         /* handle the user input by setting the current bullet depth's pixmap */
242         /* to that extracted from the current chosen position of the BMTable  */
243         /* Don't forget to free the button's old pixmap first.                */
244
245         BufferParams & param = current_view->buffer()->params;
246         int bmtable_button = fl_get_bmtable(ob);
247
248         /* try to keep the button held down till another is pushed */
249         /*  fl_set_bmtable(ob, 1, bmtable_button); */
250         param.temp_bullets[current_bullet_depth].setFont(current_bullet_panel);
251         param.temp_bullets[current_bullet_depth].setCharacter(bmtable_button);
252         fl_set_input(fd_form_bullet->input_bullet_latex,
253                      param.temp_bullets[current_bullet_depth].c_str());
254 }