summaryrefslogtreecommitdiff
path: root/ldmicro/coildialog.cpp
blob: 48630d4a1710a65feeabed316ccc213e7bee6f1b (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//-----------------------------------------------------------------------------
// Copyright 2007 Jonathan Westhues
//
// This file is part of LDmicro.
// 
// LDmicro is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// LDmicro is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with LDmicro.  If not, see <http://www.gnu.org/licenses/>.
//------
//
// Dialog for setting the properties of a relay coils: negated or not,
// plus the name, plus set-only or reset-only
// Jonathan Westhues, Oct 2004
//-----------------------------------------------------------------------------
#include <linuxUI.h>
#include <stdio.h>
//#include <commctrl.h>

#include "ldmicro.h"

static HWID CoilDialog;

static HWID SourceInternalRelayRadio;
static HWID SourceMcuPinRadio;
static HWID NegatedRadio;
static HWID NormalRadio;
static HWID SetOnlyRadio;
static HWID ResetOnlyRadio;
static HWID NameTextbox;
static HWID OkButton;
static HWID CancelButton;

static LONG_PTR PrevNameProc;

static HWID CoilGrid;
static HWID CoilPackingBox;
static bool* tmpnegated;
static bool* tmpsetOnly ;
static bool* tmpresetOnly;

//-----------------------------------------------------------------------------
// Don't allow any characters other than A-Za-z0-9_ in the name.
//-----------------------------------------------------------------------------

void CoilDialogMyNameProc (GtkEditable *editable, gchar *NewText, gint length, 
    gint *position, gpointer data){
    for (int i = 0; i < length; i++){
        if (!(isalpha (NewText[i]) || NewText[i] == '_' || isdigit (NewText[i])
                                     || NewText[i] == '\b' )){
            g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
            return;
        }
    }
}

static void MakeControls(void)
{
    NormalRadio = gtk_radio_button_new_with_label (NULL, "( ) Normal");
    NegatedRadio = gtk_radio_button_new_with_label_from_widget
                        (GTK_RADIO_BUTTON (NormalRadio), "(/) Negated");
    SetOnlyRadio = gtk_radio_button_new_with_label_from_widget
                        (GTK_RADIO_BUTTON (NormalRadio), "(S) Set-Only");
    ResetOnlyRadio = gtk_radio_button_new_with_label_from_widget
                        (GTK_RADIO_BUTTON (NormalRadio), "(R) Reset-Only");
    
    SourceInternalRelayRadio = gtk_radio_button_new_with_label (NULL, "Internal Relay");
    SourceMcuPinRadio = gtk_radio_button_new_with_label_from_widget
                        (GTK_RADIO_BUTTON (SourceInternalRelayRadio), "Pin on MCU");
    
    HWID textLabel = gtk_label_new ("Name:");
    
    NameTextbox = gtk_entry_new();
    gtk_entry_set_max_length (GTK_ENTRY (NameTextbox), 0);

    OkButton = gtk_button_new_with_label ("OK");
    CancelButton = gtk_button_new_with_label ("Cancel");

    gtk_grid_attach (GTK_GRID (CoilGrid), NormalRadio, 0, 1, 1, 1);
    gtk_grid_attach (GTK_GRID (CoilGrid), NegatedRadio, 0, 2, 1, 1);
    gtk_grid_attach (GTK_GRID (CoilGrid), SetOnlyRadio, 0, 3, 1, 1);
    gtk_grid_attach (GTK_GRID (CoilGrid), ResetOnlyRadio, 0, 4, 1, 1);
    gtk_grid_attach (GTK_GRID (CoilGrid), SourceInternalRelayRadio, 1, 1, 1, 1);
    gtk_grid_attach (GTK_GRID (CoilGrid), SourceMcuPinRadio, 1, 2, 1, 1);
    gtk_grid_attach (GTK_GRID (CoilGrid), textLabel, 1, 4, 1, 1);
    gtk_grid_attach (GTK_GRID (CoilGrid), NameTextbox, 2, 4, 1, 1);
    gtk_grid_attach (GTK_GRID (CoilGrid), OkButton, 4, 2, 1, 1);
    gtk_grid_attach (GTK_GRID (CoilGrid), CancelButton, 4, 3, 1, 1);

    gtk_grid_set_column_spacing (GTK_GRID (CoilGrid), 1);
    gtk_box_pack_start(GTK_BOX(CoilPackingBox), CoilGrid, TRUE, TRUE, 0);

    g_signal_connect (G_OBJECT (NameTextbox), "insert-text",
                    G_CALLBACK (CoilDialogMyNameProc), NULL);
}

void CoilDialogGetData (char* name){

    bool* negated = tmpnegated;
    bool* resetOnly = tmpresetOnly;
    bool* setOnly = tmpsetOnly;

    if(gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
    (SourceInternalRelayRadio))) {
            name[0] = 'R';
        }
        else {
            name[0] = 'Y';
        }
        strcpy (name+1, gtk_entry_get_text (GTK_ENTRY (NameTextbox)));

        if(gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
        (NormalRadio))) {
            *negated = FALSE;
            *setOnly = FALSE;
            *resetOnly = FALSE;
        }
        else if(gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
        (NegatedRadio))) {
            *negated = TRUE;
            *setOnly = FALSE;
            *resetOnly = FALSE;
        }
        else if(gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
        (SetOnlyRadio))) {
            *negated = FALSE;
            *setOnly = TRUE;
            *resetOnly = FALSE;
        }
        else if(gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
        (ResetOnlyRadio))){
            *negated = FALSE;
            *setOnly = FALSE;
            *resetOnly = TRUE;
        }

    gtk_widget_set_sensitive (MainWindow, TRUE);
    DestroyWindow (CoilDialog);
    ProgramChanged();
}

// Mouse click callback
void CoilDialogMouseClick (HWID widget, gpointer data){
    CoilDialogGetData((char*)data);
}

// Checks for the required key press
gboolean CoilDialogKeyPress (HWID widget, GdkEventKey* event, gpointer data){
    if (event -> keyval == GDK_KEY_Return){
        CoilDialogGetData((char*)data);
    }
    else if (event -> keyval == GDK_KEY_Escape){
        DestroyWindow (CoilDialog);
        ProgramChanged();
        gtk_widget_set_sensitive (MainWindow, TRUE);
    }
    return FALSE;
}

// Calls DestroyWindow
void CoilCallDestroyWindow (HWID widget, gpointer data){
    DestroyWindow (CoilDialog);
    ProgramChanged();
    gtk_widget_set_sensitive (MainWindow, TRUE);
}

void ShowCoilDialog(BOOL *negated, BOOL *setOnly, BOOL *resetOnly, char *name)
{
    CoilGrid = gtk_grid_new();
    CoilPackingBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);

    CoilDialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(CoilDialog), "Coil");
    gtk_window_set_default_size(GTK_WINDOW(CoilDialog), 100, 50);
    gtk_window_set_resizable (GTK_WINDOW (CoilDialog), FALSE);
    gtk_container_add(GTK_CONTAINER(CoilDialog), CoilPackingBox);
    gtk_widget_add_events (CoilDialog, GDK_KEY_PRESS_MASK);
    gtk_widget_add_events (CoilDialog, GDK_BUTTON_PRESS_MASK);

    MakeControls();
   
    if(name[0] == 'R') {
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (SourceInternalRelayRadio), TRUE);
    }
    else {
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (SourceMcuPinRadio), TRUE);
    }
    gtk_entry_set_text (GTK_ENTRY (NameTextbox), name+1);
    if(*negated) {
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (NegatedRadio), TRUE);
    }
    else if(*setOnly) {
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (SetOnlyRadio), TRUE);
    }
    else if(*resetOnly) {
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ResetOnlyRadio), TRUE);
    }
    else {
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (NormalRadio), TRUE);
    }

    gtk_widget_set_sensitive (MainWindow, FALSE);
    gtk_widget_show_all (CoilDialog);
    gtk_widget_grab_focus (NameTextbox);

    tmpnegated = negated;
    tmpresetOnly = resetOnly;
    tmpsetOnly = setOnly;

    g_signal_connect (G_OBJECT (CoilDialog), "key-press-event",
                    G_CALLBACK(CoilDialogKeyPress), (gpointer)name);
    g_signal_connect (G_OBJECT (OkButton), "clicked",
                    G_CALLBACK(CoilDialogMouseClick), (gpointer)name);
    g_signal_connect (G_OBJECT (CancelButton), "clicked",
                    G_CALLBACK(CoilCallDestroyWindow), NULL);

    return;
}