1
/****************************************************************************
3
GLUI User Interface Toolkit
4
---------------------------
6
glui_commandline.cpp - GLUI_CommandLine control class
9
--------------------------------------------------
11
Copyright (c) 1998 Paul Rademacher, 2005 William Baxter
13
This library is free software; you can redistribute it and/or modify
14
it under the terms of the GNU Lesser General Public License as
15
published by the Free Software Foundation; either version 2.1 of the
16
License, or (at your option) any later version.
18
This library is distributed in the hope that it will be useful, but
19
WITHOUT ANY WARRANTY; without even the implied warranty of
20
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21
Lesser General Public License for more details.
23
You should have received a copy of the GNU Lesser General Public
24
License along with this library; if not, write to the Free Software
25
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28
This program is -not- in the public domain.
30
*****************************************************************************/
33
#include "glui_internal.h"
35
/****************************** GLUI_CommandLine::GLUI_CommandLine() **********/
36
GLUI_CommandLine::GLUI_CommandLine( GLUI_Node *parent, const char *name,
37
void *data, int id, GLUI_CB cb )
42
data_type = GLUI_EDITTEXT_TEXT;
47
live_type = GLUI_LIVE_TEXT;
49
parent->add_control( this );
54
/****************************** GLUI_CommandLine::key_handler() **********/
56
int GLUI_CommandLine::key_handler( unsigned char key,int modifiers )
64
dump( stdout, "-> CMD_TEXT KEY HANDLER" );
66
if ( key == 13 ) { /* RETURN */
70
ret = Super::key_handler( key, modifiers );
73
dump( stdout, "<- CMD_TEXT KEY HANDLER" );
79
/****************************** GLUI_CommandLine::deactivate() **********/
81
void GLUI_CommandLine::deactivate( void )
83
// if the commit_flag is set, add the current command to
84
// history and call deactivate as normal
86
// Trick deactivate into calling callback if and only if commit_flag set.
87
// A bit subtle, but deactivate checks that orig_text and text
88
// are the same to decide whether or not to call the callback.
89
// Force them to be different for commit, and the same for no commit.
91
add_to_history(text.c_str());
102
/**************************** GLUI_CommandLine::special_handler() **********/
104
int GLUI_CommandLine::special_handler( int key,int modifiers )
110
printf( "CMD_TEXT SPECIAL:%d - mod:%d subs:%d/%d ins:%d sel:%d/%d\n",
111
key, modifiers, substring_start, substring_end,insertion_pt,
112
sel_start, sel_end );
114
if ( key == GLUT_KEY_UP ) // PREVIOUS HISTORY
118
else if ( key == GLUT_KEY_DOWN ) // NEXT HISTORY
123
return Super::special_handler( key, modifiers );
130
/**************************** GLUI_CommandLine::scroll_history() ********/
132
void GLUI_CommandLine::scroll_history( int direction )
134
recall_history(curr_hist + direction);
137
/**************************** GLUI_CommandLine::recall_history() ********/
139
void GLUI_CommandLine::recall_history( int hist_num )
141
if (hist_num < oldest_hist OR
142
hist_num > newest_hist OR
143
hist_num == curr_hist)
146
// Commit the current text first before we blow it away!
147
if (curr_hist == newest_hist) {
148
get_history_str(newest_hist) = text;
151
curr_hist = hist_num;
152
set_text(get_history_str(curr_hist));
153
sel_end = sel_start = insertion_pt = (int)text.length();
154
update_and_draw_text();
157
/**************************** GLUI_CommandLine::add_to_history() ********/
159
void GLUI_CommandLine::add_to_history( const char *cmd )
161
if (cmd[0]=='\0') return; // don't add if it's empty
163
curr_hist = newest_hist;
164
get_history_str(newest_hist) = text;
166
newest_hist = ++curr_hist;
167
if ( newest_hist >= HIST_SIZE )
169
// bump oldest off the list
170
hist_list.erase(hist_list.begin());
171
hist_list.push_back("");
177
/**************************** GLUI_CommandLine::reset_history() ********/
179
void GLUI_CommandLine::reset_history( void )
181
oldest_hist = newest_hist = curr_hist = 0;
186
/*************************************** GLUI_CommandLine::dump() **************/
188
void GLUI_CommandLine::dump( FILE *out, const char *name )
191
"%s (commandline@%p): ins_pt:%d subs:%d/%d sel:%d/%d len:%d\n",
193
insertion_pt, substring_start, substring_end, sel_start, sel_end,