~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/bullet/Extras/glui/glui_commandline.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
  
 
3
  GLUI User Interface Toolkit
 
4
  ---------------------------
 
5
 
 
6
     glui_commandline.cpp - GLUI_CommandLine control class
 
7
 
 
8
 
 
9
      --------------------------------------------------
 
10
 
 
11
  Copyright (c) 1998 Paul Rademacher, 2005 William Baxter
 
12
 
 
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.
 
17
 
 
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.
 
22
 
 
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
 
26
  USA
 
27
 
 
28
  This program is -not- in the public domain.
 
29
 
 
30
*****************************************************************************/
 
31
 
 
32
#include "GL/glui.h"
 
33
#include "glui_internal.h"
 
34
 
 
35
/****************************** GLUI_CommandLine::GLUI_CommandLine() **********/
 
36
GLUI_CommandLine::GLUI_CommandLine( GLUI_Node *parent, const char *name, 
 
37
                                    void *data, int id, GLUI_CB cb )
 
38
{
 
39
  common_init();
 
40
  set_name( name );
 
41
    
 
42
  data_type   = GLUI_EDITTEXT_TEXT;
 
43
  ptr_val     = data;
 
44
  user_id     = id;
 
45
  callback    = cb;
 
46
    
 
47
  live_type = GLUI_LIVE_TEXT;
 
48
 
 
49
  parent->add_control( this );
 
50
 
 
51
  init_live();
 
52
}
 
53
 
 
54
/****************************** GLUI_CommandLine::key_handler() **********/
 
55
 
 
56
int    GLUI_CommandLine::key_handler( unsigned char key,int modifiers )
 
57
{
 
58
  int ret;
 
59
 
 
60
  if ( NOT glui )
 
61
    return false;
 
62
 
 
63
  if ( debug )
 
64
    dump( stdout, "-> CMD_TEXT KEY HANDLER" );
 
65
 
 
66
  if ( key == 13 ) {           /* RETURN */
 
67
    commit_flag = true;
 
68
  }
 
69
 
 
70
  ret = Super::key_handler( key, modifiers );
 
71
 
 
72
  if ( debug )
 
73
    dump( stdout, "<- CMD_TEXT KEY HANDLER" );
 
74
 
 
75
  return ret;
 
76
}
 
77
 
 
78
 
 
79
/****************************** GLUI_CommandLine::deactivate() **********/
 
80
 
 
81
void    GLUI_CommandLine::deactivate( void )
 
82
{
 
83
  // if the commit_flag is set, add the current command to 
 
84
  // history and call deactivate as normal
 
85
 
 
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.
 
90
  if (commit_flag) {
 
91
    add_to_history(text.c_str());
 
92
    orig_text = "";
 
93
    Super::deactivate( );
 
94
    set_text( "" );
 
95
    commit_flag = false;
 
96
  }
 
97
  else {
 
98
    orig_text = text;
 
99
  }
 
100
}
 
101
 
 
102
/**************************** GLUI_CommandLine::special_handler() **********/
 
103
 
 
104
int    GLUI_CommandLine::special_handler( int key,int modifiers )
 
105
{
 
106
  if ( NOT glui )
 
107
    return false;
 
108
  
 
109
  if ( debug )
 
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 );        
 
113
 
 
114
  if ( key == GLUT_KEY_UP )  // PREVIOUS HISTORY
 
115
  {
 
116
    scroll_history(-1);
 
117
  }
 
118
  else if ( key == GLUT_KEY_DOWN )  // NEXT HISTORY
 
119
  {
 
120
    scroll_history(+1);
 
121
  }
 
122
  else {
 
123
    return Super::special_handler( key, modifiers );
 
124
  }
 
125
  return false;
 
126
}
 
127
 
 
128
 
 
129
 
 
130
/**************************** GLUI_CommandLine::scroll_history() ********/
 
131
 
 
132
void    GLUI_CommandLine::scroll_history( int direction )
 
133
{
 
134
  recall_history(curr_hist + direction);
 
135
}
 
136
 
 
137
/**************************** GLUI_CommandLine::recall_history() ********/
 
138
 
 
139
void    GLUI_CommandLine::recall_history( int hist_num )
 
140
{
 
141
  if (hist_num < oldest_hist OR
 
142
      hist_num > newest_hist OR
 
143
      hist_num == curr_hist)
 
144
    return;
 
145
 
 
146
  // Commit the current text first before we blow it away!
 
147
  if (curr_hist == newest_hist) {
 
148
    get_history_str(newest_hist) = text;
 
149
  }
 
150
 
 
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();
 
155
}
 
156
 
 
157
/**************************** GLUI_CommandLine::add_to_history() ********/
 
158
 
 
159
void    GLUI_CommandLine::add_to_history( const char *cmd )
 
160
{
 
161
  if (cmd[0]=='\0') return; // don't add if it's empty
 
162
 
 
163
  curr_hist = newest_hist;
 
164
  get_history_str(newest_hist) = text;
 
165
 
 
166
  newest_hist = ++curr_hist;
 
167
  if ( newest_hist >= HIST_SIZE )
 
168
  {
 
169
    // bump oldest off the list
 
170
    hist_list.erase(hist_list.begin());
 
171
    hist_list.push_back("");
 
172
 
 
173
    oldest_hist++;
 
174
  }
 
175
}
 
176
 
 
177
/**************************** GLUI_CommandLine::reset_history() ********/
 
178
 
 
179
void    GLUI_CommandLine::reset_history( void )
 
180
{
 
181
  oldest_hist = newest_hist = curr_hist = 0;
 
182
}
 
183
 
 
184
 
 
185
 
 
186
/*************************************** GLUI_CommandLine::dump() **************/
 
187
 
 
188
void   GLUI_CommandLine::dump( FILE *out, const char *name )
 
189
{
 
190
  fprintf( out, 
 
191
           "%s (commandline@%p):  ins_pt:%d  subs:%d/%d  sel:%d/%d   len:%d\n",
 
192
           name, this, 
 
193
           insertion_pt, substring_start, substring_end, sel_start, sel_end,
 
194
           (int)text.length());
 
195
}
 
196
 
 
197