1 /* wconfig_hotkey.cc
2  * This file belongs to Worker, a file manager for UN*X/X11.
3  * Copyright (C) 2006-2020 Ralf Hoffmann.
4  * You can contact me at: ralf@boomerangsworld.de
5  *   or http://www.boomerangsworld.de/worker
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include "wconfig_hotkey.hh"
23 #include "wconfig.h"
24 #include "worker.h"
25 #include "wchotkey.hh"
26 #include "worker_locale.h"
27 #include "aguix/fieldlistview.h"
28 #include "aguix/button.h"
29 #include "simplelist.hh"
30 
HotkeyPanel(AWindow & basewin,WConfig & baseconfig)31 HotkeyPanel::HotkeyPanel( AWindow &basewin, WConfig &baseconfig ) : WConfigPanel( basewin, baseconfig )
32 {
33 }
34 
~HotkeyPanel()35 HotkeyPanel::~HotkeyPanel()
36 {
37 }
38 
create()39 int HotkeyPanel::create()
40 {
41   Panel::create();
42 
43   int row;
44 
45   AContainer *ac1 = setContainer( new AContainer( this, 1, 3 ), true );
46   ac1->setMinSpace( 5 );
47   ac1->setMaxSpace( 5 );
48 
49   addMultiLineText( catalog.getLocale( 685 ),
50                     *ac1,
51                     0, 0,
52                     NULL, NULL );
53 
54   lv = (FieldListView*)ac1->add( new FieldListView( _aguix, 0, 0, 200, 150, 0 ), 0, 1, AContainer::CO_MIN );
55   lv->setHBarState(2);
56   lv->setVBarState(2);
57   lv->setAcceptFocus( true );
58   lv->setDisplayFocus( true );
59 
60   AContainer *ac1_1 = ac1->add( new AContainer( this, 4, 1 ), 0, 2 );
61   ac1_1->setMinSpace( 0 );
62   ac1_1->setMaxSpace( 0 );
63   ac1_1->setBorderWidth( 0 );
64 
65   newb = (Button*)ac1_1->add( new Button( _aguix, 0, 0, catalog.getLocale( 154 ), 0 ), 0, 0, AContainer::CO_INCW );
66   newb->connect( this );
67   dupb = (Button*)ac1_1->add( new Button( _aguix, 0, 0, catalog.getLocale( 155 ), 0 ), 1, 0, AContainer::CO_INCW );
68   dupb->connect( this );
69   delb = (Button*)ac1_1->add( new Button( _aguix, 0, 0, catalog.getLocale( 156 ), 0 ), 2, 0, AContainer::CO_INCW );
70   delb->connect( this );
71   editb = (Button*)ac1_1->add( new Button( _aguix, 0, 0, catalog.getLocale( 157 ), 0 ), 3, 0, AContainer::CO_INCW );
72   editb->connect( this );
73 
74   int id = _baseconfig.getHotkeys()->initEnum();
75   WCHotkey *h1=(WCHotkey*)_baseconfig.getHotkeys()->getFirstElement(id);
76   while(h1!=NULL) {
77     row = lv->addRow();
78     lv->setText( row, 0, h1->getName() );
79     lv->setPreColors( row, FieldListView::PRECOLOR_ONLYACTIVE );
80     h1=(WCHotkey*)_baseconfig.getHotkeys()->getNextElement(id);
81   }
82   lv->redraw();
83   _baseconfig.getHotkeys()->closeEnum(id);
84 
85   contMaximize( true );
86   return 0;
87 }
88 
saveValues()89 int HotkeyPanel::saveValues()
90 {
91   return 0;
92 }
93 
run(Widget * elem,const AGMessage & msg)94 void HotkeyPanel::run( Widget *elem, const AGMessage &msg )
95 {
96   List *hotkeys = _baseconfig.getHotkeys();
97   int trow;
98 
99   if ( _need_recreate == true ) return;
100 
101   if ( msg.type == AG_BUTTONCLICKED ) {
102     if ( msg.button.button == newb ) {
103       WCHotkey *th1 = new WCHotkey();
104       hotkeys->addElement( th1 );
105       trow = lv->addRow();
106       lv->setText( trow, 0, th1->getName() );
107       lv->setPreColors( trow, FieldListView::PRECOLOR_ONLYACTIVE );
108       lv->setActiveRow( trow );
109       lv->showActive();
110       int pos = hotkeys->getIndex( th1 );
111       if ( _baseconfig.configureHotkeyIndex( pos ) == true ) {
112 	lv->setText( trow, 0, th1->getName() );
113 	lv->redraw();
114       }
115     } else if ( msg.button.button == dupb ) {
116       trow = lv->getActiveRow();
117       if ( lv->isValidRow( trow ) == true ) {
118 	int pos = trow;
119 	WCHotkey *th1 = (WCHotkey*)hotkeys->getElementAt( pos );
120 	if ( th1 != NULL ) {
121 	  WCHotkey *th2 = th1->duplicate();
122 	  // reset shortkey to avoid duplicates
123 	  th2->setDoubleKeys( NULL );
124 
125 	  hotkeys->addElement(th2);
126 	  trow = lv->addRow();
127 	  lv->setText( trow, 0, th2->getName() );
128 	  lv->setPreColors( trow, FieldListView::PRECOLOR_ONLYACTIVE );
129 	  lv->setActiveRow( trow );
130 	  lv->showActive();
131 	  lv->redraw();
132 	}
133       }
134     } else if ( msg.button.button == delb ) {
135       trow = lv->getActiveRow();
136       if ( lv->isValidRow( trow ) == true ) {
137 	int pos = trow;
138 	WCHotkey *th1 = (WCHotkey*)hotkeys->getElementAt( pos );
139 	if ( th1 != NULL ) {
140 	  delete th1;
141 	  hotkeys->removeElementAt( pos );
142 	  lv->deleteRow( pos );
143 	  lv->redraw();
144 	}
145       }
146     } else if ( msg.button.button == editb ) {
147       trow = lv->getActiveRow();
148       if ( lv->isValidRow( trow ) == true ) {
149 	int pos = trow;
150 	WCHotkey *th1 = (WCHotkey*)hotkeys->getElementAt( pos );
151 	if ( th1 != NULL ) {
152 	  if ( _baseconfig.configureHotkeyIndex( pos ) == true ) {
153 	    lv->setText( trow, 0, th1->getName() );
154 	    lv->redraw();
155 	  }
156 	}
157       }
158     }
159   }
160 }
161 
addButtons(List * buttons,WConfigPanelCallBack::add_action_t action)162 int HotkeyPanel::addButtons( List *buttons,
163                              WConfigPanelCallBack::add_action_t action )
164 {
165   int erg = 0;
166 
167   if ( buttons != NULL && action == WConfigPanelCallBack::CHECK_DOUBLEKEYS ) {
168     erg = _baseconfig.fixDoubleKeys( NULL, NULL, _baseconfig.getHotkeys(), buttons, NULL );
169   }
170   _need_recreate = true;
171   return erg;
172 }
173 
addHotkeys(List * hotkeys,WConfigPanelCallBack::add_action_t action)174 int HotkeyPanel::addHotkeys( List *hotkeys,
175                              WConfigPanelCallBack::add_action_t action )
176 {
177   int erg = 0;
178 
179   if ( hotkeys != NULL && action == WConfigPanelCallBack::DO_IMPORT ) {
180     erg = _baseconfig.addHotkeys( hotkeys );
181   }
182   _need_recreate = true;
183   return erg;
184 }
185