00001 // Copyright (c) 2003 - 2004 Anselm R. Garbe <anselmg at t-online.de> 00002 // See ../LICENSE.txt for license details. 00003 // 00004 // $Id: action.cpp 7 2007-05-24 11:03:53Z eg1981 $ 00005 00006 #include <string> 00007 #include "ncwm.h" 00008 00009 #include "action.h" 00010 #include "actions.h" 00011 #include "kernel.h" 00012 #include "monitor.h" 00013 #include "prompt.h" 00014 #include "shortcut.h" 00015 #include "validators.h" 00016 00017 Action::Action(string id, ToPerform toPerform, 00018 IsValid isValid, Type type, char *argument) 00019 { 00020 id_ = id; 00021 toPerform_ = toPerform; 00022 isValid_ = isValid; 00023 type_ = type; 00024 argument_ = argument; 00025 shortcut_ = 0; 00026 listenOn_ = 0; 00027 } 00028 00029 Action::~Action() { 00030 if (argument_) { 00031 free(argument_); 00032 } 00033 } 00034 00035 void Action::perform() { 00036 // great HACK to return from maximized frames back to normality 00037 Monitor *focusedMonitor = KERNEL->focusedMonitor(); 00038 00039 if (focusedMonitor->isThingMaximized() && 00040 (toPerform_ != &Actions::cycleClientNext) && 00041 (toPerform_ != &Actions::cycleClientPrev) && 00042 (toPerform_ != &Actions::toggleBar) && 00043 (toPerform_ != &Actions::toggleMaximization)) 00044 { 00045 00046 focusedMonitor->toggleThingMaximization(); 00047 } 00048 00049 if (KERNEL->isRecording() && 00050 (toPerform_ != &Actions::endChainRecord) && 00051 (toPerform_ != &Actions::endScriptRecord) && 00052 (toPerform_ != &Actions::inputMode)) 00053 { 00054 KERNEL->recordedActions()->push_back( 00055 new Action(id_, 0, 0, type_, argument_ ? 00056 strdup(argument_) : 0)); 00057 } 00058 00059 Actions actions = *Actions::instance(); 00060 00061 if (isValid()) { 00062 (actions.*toPerform_)(this, argument_); // call action 00063 } 00064 } 00065 00066 bool Action::isValid() { 00067 Validators validators = *Validators::instance(); 00068 00069 return (validators.*isValid_)(); 00070 } 00071 00072 Prompt *Action::prompt(unsigned int index) { 00073 00074 unsigned int i = 0; 00075 for (LPrompt::iterator it = prompts_.begin(); 00076 it != prompts_.end(); it++) 00077 { 00078 if (i == index) { 00079 return (*it); 00080 } 00081 i++; 00082 } 00083 00084 // should never occure 00085 return 0; 00086 } 00087 00088 string Action::id() const { 00089 return id_; 00090 } 00091 00092 unsigned int Action::promptsCount() const { 00093 return prompts_.size(); 00094 } 00095 00096 00097 IsValid Action::getIsValid() const { 00098 return isValid_; 00099 } 00100 00101 void Action::setShortcut(Shortcut *shortcut) { 00102 shortcut_ = shortcut; 00103 listenOn_ = shortcut; 00104 } 00105 00106 Shortcut *Action::shortcut() const { 00107 return shortcut_; 00108 } 00109 00110 void Action::setArgument(char *argument) { 00111 argument_ = argument; 00112 } 00113 00114 char *Action::argument() const { 00115 return argument_; 00116 } 00117 00118 Action::Type Action::type() const { 00119 return type_; 00120 } 00121 00122 void Action::setType(Type type) { 00123 type_ = type; 00124 } 00125 00126 00127 LPrompt *Action::prompts() { 00128 return &prompts_; 00129 } 00130 00131 ToPerform Action::getToPerform() const { 00132 return toPerform_; 00133 } 00134 00135 void Action::setListenOn(Shortcut *listenOn) { 00136 listenOn_ = listenOn; 00137 } 00138 00139 Shortcut *Action::listenOn() const { 00140 return listenOn_; 00141 }