1 /* wcbutton.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 "wcbutton.hh"
23 #include "datei.h"
24 #include "aguix/util.h"
25 #include "wcdoubleshortkey.hh"
26 #include "functionproto.h"
27 #include "simplelist.hh"
28 
WCButton()29 WCButton::WCButton()
30 {
31   text=dupstring("");
32   fg=1;
33   bg=0;
34   check=false;
35 }
36 
~WCButton()37 WCButton::~WCButton()
38 {
39   if(text!=NULL) _freesafe(text);
40 }
41 
duplicate() const42 WCButton *WCButton::duplicate() const
43 {
44   WCButton *tbut=new WCButton();
45   tbut->setText(text);
46   tbut->setFG(fg);
47   tbut->setBG(bg);
48   tbut->setComs(com);
49   tbut->setCheck(check);
50   tbut->setDoubleKeys( dkeys );
51   return tbut;
52 }
53 
setText(const char * ntext)54 void WCButton::setText(const char *ntext)
55 {
56   if(text!=NULL) _freesafe(text);
57   if(ntext==NULL) {
58     text=dupstring("");
59   } else {
60     text=dupstring(ntext);
61   }
62 }
63 
setFG(int nfg)64 void WCButton::setFG(int nfg)
65 {
66   fg=nfg;
67 }
68 
setBG(int nbg)69 void WCButton::setBG(int nbg)
70 {
71   bg=nbg;
72 }
73 
getText()74 char *WCButton::getText()
75 {
76   return text;
77 }
78 
getFG() const79 int WCButton::getFG() const
80 {
81   return fg;
82 }
83 
getBG() const84 int WCButton::getBG() const
85 {
86   return bg;
87 }
88 
getComs()89 command_list_t WCButton::getComs()
90 {
91     return com;
92 }
93 
setComs(const command_list_t & ncoms)94 void WCButton::setComs( const command_list_t &ncoms )
95 {
96     com.clear();
97     for ( auto &fp : ncoms ) {
98         com.push_back( std::shared_ptr< FunctionProto >( fp->duplicate() ) );
99     }
100 }
101 
setCheck(bool ncheck)102 void WCButton::setCheck(bool ncheck)
103 {
104   check=ncheck;
105 }
106 
getCheck() const107 bool WCButton::getCheck() const
108 {
109   return check;
110 }
111 
save(Datei * fh) const112 bool WCButton::save(Datei *fh) const
113 {
114   int id;
115   WCDoubleShortkey *dk;
116 
117   if ( fh == NULL ) return false;
118   fh->configPutPairString( "title", text );
119   fh->configPutPairNum( "color", fg, bg );
120 
121   fh->configOpenSection( "shortkeys" );
122   id = dkeys->initEnum();
123   dk = (WCDoubleShortkey*)dkeys->getFirstElement( id );
124   while ( dk != NULL ) {
125     dk->save( fh );
126     dk = (WCDoubleShortkey*)dkeys->getNextElement( id );
127   }
128   dkeys->closeEnum( id );
129   fh->configCloseSection(); //shortkeys
130 
131   fh->configOpenSection( "commands" );
132   for ( auto &f1 : com ) {
133       FunctionProto::presave( fh, f1.get() );
134   }
135   fh->configCloseSection(); //commands
136   return true;
137 }
138