LOGIN / SIGN UP

/ src / CmdDialog.cc

2 Commit: 42b9c649886f08ae339ff3104dbc3d7190e445ba
Author: Claes Nästén
Date: Mon Mar 08 21:07:35 +0100 2010
Subject: Add Menu and State completions and make action names nicer.
Show file history
difference betwen
and
CmdDialog.cc
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// CmdDialog.cc for pekwm
// Copyright © 2004-2009 Claes Nästén <me@pekdon.net>
//
// This program is licensed under the GNU GPL.
// See the LICENSE file for more information.
//

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H

#include <iostream>
#include <list>
#include <cwctype>

extern "C" {
#include <X11/Xlib.h>
#include <X11/Xutil.h> // XLookupString
#include <X11/keysym.h>
}

#include "PWinObj.hh"
#include "PDecor.hh"
#include "CmdDialog.hh"
#include "Config.hh"
#include "PScreen.hh"
#include "PixmapHandler.hh"
#include "KeyGrabber.hh"
#include "ScreenResources.hh"
#include "Workspaces.hh"

using std::cerr;
using std::endl;
using std::list;
using std::string;
using std::wstring;

/**
* CmdDialog constructor, init and load history file.
*
* @todo Make size configurable.
*/

CmdDialog::CmdDialog(Display *dpy, Theme *theme)
: InputDialog(dpy, theme, L"Enter command"),
_completer(L";"), _exec_count(0)
{
_type = PWinObj::WO_CMD_DIALOG;

// Setup completer
_completer.add_method(new ActionCompleterMethod());
_completer.add_method(new PathCompleterMethod());

if (Config::instance()->getCmdDialogHistoryFile().size() > 0) {
_hist_list.load(Config::instance()->getCmdDialogHistoryFile());
}
}

/**
* CmdDialog de-structor, clean up and save history file.
*/

CmdDialog::~CmdDialog(void)
{
if (Config::instance()->getCmdDialogHistoryFile().size() > 0) {
_hist_list.save(Config::instance()->getCmdDialogHistoryFile());
}
}


//! @brief Parses _buf and tries to generate an ActionEvent
//! @return Pointer to ActionEvent.
ActionEvent*
CmdDialog::exec(void)
{
// Update history
if (Config::instance()->isCmdDialogHistoryUnique()) {
_hist_list.push_back_unique(_buf);
} else {
_hist_list.push_back(_buf);
}
if (_hist_list.size() > static_cast<uint>(Config::instance()->getCmdDialogHistorySize())) {
_hist_list.pop_front();
}

// Persist changes
if (Config::instance()->getCmdDialogHistorySaveInterval() > 0
&& Config::instance()->getCmdDialogHistoryFile().size() > 0
&& ++_exec_count > Config::instance()->getCmdDialogHistorySaveInterval()) {
_hist_list.save(Config::instance()->getCmdDialogHistoryFile());
_exec_count = 0;
}


// Check if it's a valid Action, if not we assume it's a command and try
// to execute it.
string buf_mb(Util::to_mb_str(_buf));
if (! Config::instance()->parseAction(buf_mb, _ae.action_list.back(), KEYGRABBER_OK)) {
_ae.action_list.back().setAction(ACTION_EXEC);
_ae.action_list.back().setParamS(buf_mb);
}

return &_ae;
}

//! @brief Unmaps window, overloaded to clear buffer.
void
CmdDialog::unmapWindow(void)
{
if (_mapped) {
InputDialog::unmapWindow();
setWORef(0);
bufClear();
}
}

/**
* Tab completion, complete word at cursor position.
*/

void
CmdDialog::complete(void)
{
// Find completion if changed since last time.
if (_buf != _buf_on_complete_result) {
InputDialog::complete();
_complete_list = _completer.find_completions(_buf, _pos);
_complete_it = _complete_list.begin();
}

if (_complete_list.size()) {
_buf = _completer.do_complete(_buf_on_complete, _pos, _complete_list, _complete_it);
_buf_on_complete_result = _buf;
}
}

/**
* Map CmdDialog centered on wo_ref is specified, else _wo_ref.
*/

void
CmdDialog::mapCentered(const std::string &buf, bool focus, PWinObj *wo_ref)
{
if (wo_ref != 0) {
setWORef(wo_ref);
}
InputDialog::mapCentered(buf, focus, wo_ref);
}