1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * This file is Copyright 1996-1998
12 * ====================================================== */
14 // Added pseudo-action handling, asierra 180296
19 #pragma implementation
22 #include "Toolbar_pimpl.h"
26 #include "func_status.h"
27 #include "BufferView.h"
29 #include "LyXAction.h"
30 #include "MathsSymbols.h"
31 #include "support/filetools.h"
32 #include "support/lstrings.h"
37 extern LyXAction lyxaction;
40 const int standardspacing = 2; // the usual space between items
41 const int sepspace = 6; // extra space
42 const int buttonwidth = 30; // the standard button width
43 const int height = 30; // the height of all items in the toolbar
45 Toolbar::Pimpl::toolbarItem::toolbarItem() {
46 action = LFUN_NOACTION;
51 Toolbar::Pimpl::toolbarItem::~toolbarItem() {
56 void Toolbar::Pimpl::toolbarItem::clean() {
58 fl_delete_object(icon);
65 Toolbar::Pimpl::toolbarItem &
66 Toolbar::Pimpl::toolbarItem::operator=(toolbarItem const & ti) {
67 // Are we assigning the object onto itself?
71 // If we already have an icon, release it.
74 // do we have to check icon too?
76 icon = 0; // locally we need to get the icon anew
83 Toolbar::Pimpl::Pimpl(LyXView * o, int x, int y)
84 : owner(o), sxpos(x), sypos(y)
96 // timer-cb for bubble-help (Matthias)
97 void BubbleTimerCB(FL_OBJECT *, long data)
99 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
100 // The trick we use to get the help text is to read the
101 // argument of the callback that has been registered for
102 // ToolBarCB. (JMarc)
103 string help = _(lyxaction.helpText(ob->argument));
104 fl_show_oneliner(help.c_str(), ob->form->x + ob->x,
105 ob->form->y + ob->y + ob->h);
109 extern "C" void C_Toolbar_BubbleTimerCB(FL_OBJECT * ob, long data)
111 BubbleTimerCB(ob, data);
115 // post_handler for bubble-help (Matthias)
116 int BubblePost(FL_OBJECT *ob, int event,
117 FL_Coord /*mx*/, FL_Coord /*my*/,
118 int /*key*/, void * /*xev*/)
120 FL_OBJECT * bubble_timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
122 // We do not test for empty help here, since this can never happen
123 if (event == FL_ENTER){
124 fl_set_object_callback(bubble_timer,
125 C_Toolbar_BubbleTimerCB,
126 reinterpret_cast<long>(ob));
127 fl_set_timer(bubble_timer, 1);
129 else if (event != FL_MOTION){
130 fl_set_timer(bubble_timer, 0);
137 extern "C" int C_Toolbar_BubblePost(FL_OBJECT * ob, int event,
138 FL_Coord /*mx*/, FL_Coord /*my*/,
141 return BubblePost(ob, event, 0, 0, key, xev);
148 // this one is not "C" because combox callbacks are really C++ %-|
149 void Toolbar::Pimpl::layoutSelectedCB(int sel, void * arg, Combox *)
151 Toolbar::Pimpl * tb = reinterpret_cast<Toolbar::Pimpl *>(arg);
153 tb->layoutSelected(sel);
157 void Toolbar::Pimpl::layoutSelected(int sel)
159 string const tmp = tostr(sel);
160 owner->getLyXFunc()->Dispatch(LFUN_LAYOUTNO, tmp);
164 void Toolbar::Pimpl::activate()
166 ToolbarList::const_iterator p = toollist.begin();
167 ToolbarList::const_iterator end = toollist.end();
168 for (; p != end; ++p) {
170 fl_activate_object(p->icon);
176 void Toolbar::Pimpl::deactivate()
178 ToolbarList::const_iterator p = toollist.begin();
179 ToolbarList::const_iterator end = toollist.end();
180 for (; p != end; ++p) {
182 fl_deactivate_object(p->icon);
187 void Toolbar::Pimpl::update()
189 ToolbarList::const_iterator p = toollist.begin();
190 ToolbarList::const_iterator end = toollist.end();
191 for (; p != end; ++p) {
193 int status = owner->getLyXFunc()->getStatus(p->action);
194 if (status & func_status::ToggleOn) {
195 // I'd like to use a different color
196 // here, but then the problem is to
197 // know how to use transparency with
198 // Xpm library. It seems pretty
199 // complicated to me (JMarc)
200 fl_set_object_color(p->icon, FL_LEFT_BCOL, FL_BLUE);
201 fl_set_object_boxtype(p->icon, FL_DOWN_BOX);
203 fl_set_object_color(p->icon, FL_MCOL, FL_BLUE);
204 fl_set_object_boxtype(p->icon, FL_UP_BOX);
207 if (status & func_status::Disabled) {
208 // Is there a way here to specify a
209 // mask in order to show that the
210 // button is disabled? (JMarc)
211 fl_deactivate_object(p->icon);
214 fl_activate_object(p->icon);
220 void Toolbar::Pimpl::setLayout(int layout) {
222 combox->select(layout+1);
226 void Toolbar::Pimpl::updateLayoutList(bool force)
228 // Update the layout display
231 // If textclass is different, we need to update the list
232 if (combox->empty() || force) {
234 LyXTextClass const & tc =
235 textclasslist.TextClass(owner->buffer()->
237 LyXTextClass::const_iterator end = tc.end();
238 for (LyXTextClass::const_iterator cit = tc.begin();
240 if ((*cit).obsoleted_by().empty())
241 combox->addline(_((*cit).name()));
243 combox->addline("@N" + _((*cit).name()));
246 // we need to do this.
251 void Toolbar::Pimpl::clearLayoutList()
260 void Toolbar::Pimpl::openLayoutList()
269 void ToolbarCB(FL_OBJECT * ob, long ac)
271 LyXView * owner = static_cast<LyXView *>(ob->u_vdata);
273 string res = owner->getLyXFunc()->Dispatch(int(ac));
275 lyxerr[Debug::GUI] << "ToolbarCB: Function returned: "
282 extern "C" void C_Toolbar_ToolbarCB(FL_OBJECT * ob, long data)
289 // What are we supposed to do with that??
290 int Toolbar::get_toolbar_func(string const & func)
292 int action = lyxaction.LookupFunc(func);
294 if (func == "separator"){
295 action = TOOL_SEPARATOR;
296 } else if (func == "layouts"){
297 action = TOOL_LAYOUTS;
307 void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height) {
308 string name, arg, xpm_name;
311 if (lyxaction.isPseudoAction(action)) {
312 act = lyxaction.retrieveActionArg(action, arg);
313 name = lyxaction.getActionName(act);
314 xpm_name = subst(name + ' ' + arg, ' ','_');
316 act = (kb_action)action;
317 name = lyxaction.getActionName(action);
321 string fullname = LibFileSearch("images", xpm_name, "xpm");
323 if (!fullname.empty()) {
324 lyxerr[Debug::GUI] << "Full icon name is `"
325 << fullname << "'" << endl;
326 fl_set_pixmapbutton_file(obj, fullname.c_str());
330 if (act == LFUN_INSERT_MATH && !arg.empty()) {
331 char const ** pixmap = get_pixmap_from_symbol(arg.c_str(),
335 lyxerr[Debug::GUI] << "Using mathed-provided icon"
337 fl_set_pixmapbutton_data(obj,
338 const_cast<char **>(pixmap));
343 lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
344 fullname = LibFileSearch("images", "unknown", "xpm");
345 if (!fullname.empty()) {
346 lyxerr[Debug::GUI] << "Using default `unknown' icon"
348 fl_set_pixmapbutton_file(obj, fullname.c_str());
355 void Toolbar::Pimpl::set(bool doingmain)
357 // we shouldn't set if we have not cleaned
358 if (!cleaned) return;
363 fl_freeze_form(owner->getForm());
364 fl_addto_form(owner->getForm());
368 // add the time if it don't exist
369 if (bubble_timer == 0)
370 bubble_timer = fl_add_timer(FL_HIDDEN_TIMER,
371 xpos, ypos, 0, 0, "Timer");
374 ToolbarList::iterator item = toollist.begin();
375 ToolbarList::iterator end = toollist.end();
376 for (; item != end; ++item) {
377 switch (item->action){
378 case ToolbarDefaults::SEPARATOR:
381 case ToolbarDefaults::NEWLINE:
382 // Not supported yet.
384 case ToolbarDefaults::LAYOUTS:
385 xpos += standardspacing;
387 combox = new Combox(FL_COMBOX_DROPLIST);
388 combox->add(xpos, ypos, 135, height, 400);
389 combox->setcallback(layoutSelectedCB, this);
390 combox->resize(FL_RESIZE_ALL);
391 combox->gravity(NorthWestGravity, NorthWestGravity);
395 xpos += standardspacing;
397 fl_add_pixmapbutton(FL_NORMAL_BUTTON,
401 fl_set_object_resize(obj, FL_RESIZE_ALL);
402 fl_set_object_gravity(obj,
405 fl_set_object_callback(obj, C_Toolbar_ToolbarCB,
406 static_cast<long>(item->action));
407 // Remove the blue feedback rectangle
408 fl_set_pixmapbutton_focus_outline(obj, 0);
411 #if FL_REVISION >= 89
412 string help = _(lyxaction.helpText(item->action));
413 fl_set_object_helper(obj, help.c_str());
415 fl_set_object_posthandler(obj, C_Toolbar_BubblePost);
416 obj->u_cdata = reinterpret_cast<char *>(bubble_timer);
419 // The view that this object belongs to.
420 obj->u_vdata = owner;
422 setPixmap(obj, item->action, buttonwidth, height);
423 // we must remember to update the positions
426 /* Here will come a check to see if the new
427 * pos is within the bounds of the main frame,
428 * and perhaps wrap the toolbar if not.
436 fl_unfreeze_form(owner->getForm());
437 // Should be safe to do this here.
438 owner->updateLayoutChoice();
441 // set the state of the icons
448 void Toolbar::Pimpl::add(int action, bool doclean)
450 if (doclean && !cleaned) clean();
452 // this is what we do if we want to add to an existing
454 if (!doclean && owner) {
455 // first "hide" the toolbar buttons. This is not a real hide
456 // actually it deletes and frees the button altogether.
457 lyxerr << "Toolbar::add: \"hide\" the toolbar buttons."
462 fl_freeze_form(owner->getForm());
464 ToolbarList::iterator p = toollist.begin();
465 ToolbarList::iterator end = toollist.end();
466 for (; p != end; ++p) {
474 fl_unfreeze_form(owner->getForm());
475 cleaned = true; // this is not completely true, but OK anyway
478 // there exist some special actions not part of
479 // kb_action: SEPARATOR, LAYOUTS
482 newItem.action = action;
483 toollist.push_back(newItem);
487 void Toolbar::Pimpl::clean()
489 //reset(); // I do not understand what this reset() is, anyway
491 //now delete all the objects..
493 fl_freeze_form(owner->getForm());
495 // G++ vector does not have clear defined
497 toollist.erase(toollist.begin(), toollist.end());
499 lyxerr[Debug::GUI] << "Combox: " << combox << endl;
506 fl_unfreeze_form(owner->getForm());
507 lyxerr[Debug::GUI] << "toolbar cleaned" << endl;
512 void Toolbar::Pimpl::push(int nth)
514 lyxerr[Debug::GUI] << "Toolbar::push: trying to trigger no `"
515 << nth << '\'' << endl;
517 if (nth <= 0 || nth >= int(toollist.size())) {
518 // item nth not found...
522 fl_trigger_object(toollist[nth - 1].icon);
526 void Toolbar::Pimpl::reset()
528 //toollist = 0; // what is this supposed to do?
533 void Toolbar::Pimpl::lightReset() {
534 xpos = sxpos - standardspacing;